Add MaxPageButtons to the Pager widget

This commit is contained in:
Noah 2020-11-19 20:58:41 -08:00
parent 8433b1b216
commit c32601391a

View File

@ -11,10 +11,13 @@ import (
type Pager struct {
BaseWidget
// Config settings.
// Config settings. NOTE: these are copied in the constructor,
// be sure to update it there too if you add a new option!
Name string // totally optional name
Page int // default 1
Pages int
PerPage int // default 20
MaxPageButtons int // max no. of individual pages to show, 0 = no limit
Font render.Text
OnChange func(page, perPage int)
@ -34,6 +37,7 @@ func NewPager(config Pager) *Pager {
Page: config.Page,
Pages: config.Pages,
PerPage: config.PerPage,
MaxPageButtons: config.MaxPageButtons,
Font: config.Font,
OnChange: config.OnChange,
buttons: []Widget{},
@ -93,6 +97,18 @@ func (w *Pager) setup() *Frame {
for i := 1; i <= w.Pages; i++ {
page := fmt.Sprintf("%d", i)
if w.MaxPageButtons > 0 {
if i > w.MaxPageButtons {
break
}
// The final button shown; make this one always reflect the
// current page IF the current page is greater than this one.
if w.Page >= i && w.Page != i {
continue
}
}
btn := NewRadioButton(
"Page "+page,
&w.page,