diff --git a/cmd/test/panel.go b/cmd/test/panel.go index 9e20efa..fc969a6 100644 --- a/cmd/test/panel.go +++ b/cmd/test/panel.go @@ -6,9 +6,9 @@ import ( var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI { return tg.UI{ - tg.Messagef("Paged panel").PagedPanel( + tg.Messagef("Paged panel").PanelPager( c, 0, 5, - func(c tg.Context, page, size int) tg.PanelPage { + tg.PanelPagerFunc(func(c tg.Context, page, size int) tg.PanelPage { rows := []tg.ButtonRow{} for i := 0; i < size; i++ { num := 1 + page*size + i @@ -26,7 +26,7 @@ var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI { Next: page < 3, Prev: page != 0, } - }, + }), ), tg.Messagef("").Reply( BackKeyboard.Reply(), diff --git a/paged-panel.go b/paged-panel.go index b8b66c1..3650a52 100644 --- a/paged-panel.go +++ b/paged-panel.go @@ -1,31 +1,40 @@ package tg +type PanelPager interface { + GetPanelPage(c Context, page, size int) PanelPage +} + type PanelPage struct { Next, Prev bool Rows []ButtonRow } -type PanelPageFunc func( +type PanelPagerFunc func( c Context, page, size int, ) PanelPage +func (fn PanelPagerFunc) GetPanelPage( + c Context, page, size int, +) PanelPage { + return fn(c, page, size) +} -type PagedPanelCompo struct { +type PanelPagerCompo struct { PanelCompo page int size int nextFormat, prevFormat, delFormat string - fn PanelPageFunc + pager PanelPager } -func (compo *MessageCompo) PagedPanel( +func (compo *MessageCompo) PanelPager( c Context, startPage, size int, - fn PanelPageFunc, -) (*PagedPanelCompo) { - ret := &PagedPanelCompo{} + pager PanelPager, +) (*PanelPagerCompo) { + ret := &PanelPagerCompo{} ret.page = startPage ret.size = size - ret.fn = fn + ret.pager = pager ret.prevFormat = "<<<" ret.nextFormat = ">>>" ret.delFormat = "..." @@ -37,10 +46,10 @@ func (compo *MessageCompo) PagedPanel( return ret } -func (panel *PagedPanelCompo) MakeRows( +func (panel *PanelPagerCompo) MakeRows( c Context, ) []ButtonRow { - page := panel.fn( + page := panel.pager.GetPanelPage( c, panel.page, panel.size, )