diff --git a/cmd/test/panel.go b/cmd/test/panel.go index fd77ff7..9e20efa 100644 --- a/cmd/test/panel.go +++ b/cmd/test/panel.go @@ -5,45 +5,29 @@ import ( ) var DynamicPanelWidget = tg.RenderFunc(func(c tg.Context) tg.UI { - var ( - n = 0 - ln = 4 - panel *tg.PanelCompo - ) - - panel = tg.Messagef( - "Some panel", - ).Panel(c, tg.RowserFunc(func(c tg.Context) []tg.ButtonRow { - btns := []tg.ButtonRow{ - tg.ButtonRow{tg.Buttonf("Page %d", n)}, - } - for i := 0; i < ln; i++ { - num := 1 + n*ln + i - btns = append(btns, tg.ButtonRow{ - tg.Buttonf("%d", num).Rand().WithAction(tg.Func(func(c tg.Context) { - _, err := c.Sendf("%d", num*num) - if err != nil { - } - })), - tg.Buttonf("%d", num*num), - }) - } - btns = append(btns, tg.ButtonRow{ - tg.Buttonf("Prev").WithAction(tg.Func(func(c tg.Context) { - n-- - panel.Update(c) - })), - tg.Buttonf("Next").WithAction(tg.Func(func(c tg.Context) { - n++ - panel.Update(c) - })), - }) - - return btns - })) - return tg.UI{ - panel, + tg.Messagef("Paged panel").PagedPanel( + c, 0, 5, + func(c tg.Context, page, size int) tg.PanelPage { + rows := []tg.ButtonRow{} + for i := 0; i < size; i++ { + num := 1 + page*size + i + rows = append(rows, tg.ButtonRow{ + tg.Buttonf("%d", num).Rand().WithAction(tg.Func(func(c tg.Context) { + _, err := c.Sendf("%d", num*num) + if err != nil { + } + })), + tg.Buttonf("%d", num*num), + }) + } + return tg.PanelPage{ + Rows: rows, + Next: page < 3, + Prev: page != 0, + } + }, + ), tg.Messagef("").Reply( BackKeyboard.Reply(), ), diff --git a/paged-panel.go b/paged-panel.go new file mode 100644 index 0000000..b8b66c1 --- /dev/null +++ b/paged-panel.go @@ -0,0 +1,100 @@ +package tg + +type PanelPage struct { + Next, Prev bool + Rows []ButtonRow +} + +type PanelPageFunc func( + c Context, page, size int, +) PanelPage + +type PagedPanelCompo struct { + PanelCompo + page int + size int + nextFormat, prevFormat, delFormat string + fn PanelPageFunc +} + +func (compo *MessageCompo) PagedPanel( + c Context, + startPage, size int, + fn PanelPageFunc, +) (*PagedPanelCompo) { + ret := &PagedPanelCompo{} + ret.page = startPage + ret.size = size + ret.fn = fn + ret.prevFormat = "<<<" + ret.nextFormat = ">>>" + ret.delFormat = "..." + + ret.PanelCompo = (*compo.Panel( + c, ret, + )) + + return ret +} + +func (panel *PagedPanelCompo) MakeRows( + c Context, +) []ButtonRow { + page := panel.fn( + c, panel.page, panel.size, + ) + + controlRow := ButtonRow{} + + rows := page.Rows + next := func(c Context){ + panel.page++ + panel.Update(c) + } + prev := func(c Context){ + panel.page-- + panel.Update(c) + } + + if page.Prev { + if panel.delFormat != "" { + rows = append( + []ButtonRow{ + ButtonRow{ + Buttonf(panel.delFormat).Rand(). + WithAction(Func(prev)), + }, + }, + rows..., + ) + } + controlRow = append( + controlRow, + Buttonf(panel.prevFormat).Rand(). + WithAction(Func(prev)), + ) + } + + if page.Next { + if panel.delFormat != "" { + rows = append( + rows, + ButtonRow{ + Buttonf(panel.delFormat).Rand(). + WithAction(Func(next)), + }, + ) + } + controlRow = append( + controlRow, + Buttonf(panel.nextFormat).Rand(). + WithAction(Func(next)), + ) + } + + return append( + rows, + controlRow, + ) +} +