tg/paged-panel.go

109 lines
1.7 KiB
Go

package tg
type PanelPager interface {
GetPanelPage(c Context, page, size int) PanelPage
}
type PanelPage struct {
Next, Prev bool
Rows []ButtonRow
}
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 PanelPagerCompo struct {
PanelCompo
page int
size int
nextFormat, prevFormat, delFormat string
pager PanelPager
}
func (compo *MessageCompo) PanelPager(
c Context,
startPage, size int,
pager PanelPager,
) (*PanelPagerCompo) {
ret := &PanelPagerCompo{}
ret.page = startPage
ret.size = size
ret.pager = pager
ret.prevFormat = "<<<"
ret.nextFormat = ">>>"
ret.delFormat = "..."
ret.PanelCompo = (*compo.Panel(
c, ret,
))
return ret
}
func (panel *PanelPagerCompo) MakeRows(
c Context,
) []ButtonRow {
page := panel.pager.GetPanelPage(
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,
)
}