tg/paged-panel.go

100 lines
1.5 KiB
Go

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,
)
}