Add MaxPageButtons to the Pager widget

master
Noah 2020-11-19 20:58:41 -08:00
parent 8433b1b216
commit c32601391a
1 changed files with 28 additions and 12 deletions

View File

@ -11,12 +11,15 @@ import (
type Pager struct { type Pager struct {
BaseWidget BaseWidget
// Config settings. // Config settings. NOTE: these are copied in the constructor,
Page int // default 1 // be sure to update it there too if you add a new option!
Pages int Name string // totally optional name
PerPage int // default 20 Page int // default 1
Font render.Text Pages int
OnChange func(page, perPage 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)
supervisor *Supervisor supervisor *Supervisor
child Widget child Widget
@ -31,12 +34,13 @@ type Pager struct {
// NewPager creates a new Pager. // NewPager creates a new Pager.
func NewPager(config Pager) *Pager { func NewPager(config Pager) *Pager {
w := &Pager{ w := &Pager{
Page: config.Page, Page: config.Page,
Pages: config.Pages, Pages: config.Pages,
PerPage: config.PerPage, PerPage: config.PerPage,
Font: config.Font, MaxPageButtons: config.MaxPageButtons,
OnChange: config.OnChange, Font: config.Font,
buttons: []Widget{}, OnChange: config.OnChange,
buttons: []Widget{},
} }
// default settings // default settings
@ -93,6 +97,18 @@ func (w *Pager) setup() *Frame {
for i := 1; i <= w.Pages; i++ { for i := 1; i <= w.Pages; i++ {
page := fmt.Sprintf("%d", 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( btn := NewRadioButton(
"Page "+page, "Page "+page,
&w.page, &w.page,