Files
8bitMyGoUI/data/pager.go
T
ZacharyZhang-NYandClaude Opus 5.5 03e1f91706 Upgrade MyGo to v0.5.5
Elements are now pass-scoped value handles: tests that read bounds
after a frame bind a ui.Handle. pixel tells frames apart by the build
pass's root, since the Tester clock no longer moves on its own.

Fix the regressions the upgrade brought:
- Clipboard reads in input handlers go through c.Services().
- MyGo notes an overlay's opener after building it, so AutoFocus
  inside lost it; pixel.AutoFocus focuses through a Handle, and
  archtest bans the raw call. Dialogs hand the focus back again.
- Elements around a press stay hovered: Tooltip tells a press-given
  focus by focus arriving under the pointer.
- The web host moves the Tester clock with real time, at most 500 ms
  a call, and gives components the real clock.

The gallery's source index also accepts CRLF checkouts.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-17 01:31:08 -04:00

66 lines
2.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package data
import (
"fmt"
"log/slog"
"slices"
"strconv"
"github.com/egoist/mygo/ui"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/icon"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/input"
"git.zacharyzhang.com/ZacharyZhang-NY/8bitMyGoUI/pixel"
)
type PagerOptions struct {
// Sizes offered; nil means 10, 20 and 50.
Sizes []int
Disabled bool
}
type PagerResult struct {
Element ui.Element
Changed bool
}
// Pager pages through total items: "1–20 OF 312".
func Pager(c *ui.Context, text string, total int, page, size *int, o PagerOptions) PagerResult {
k := pixel.K(c)
sizes := o.Sizes
if sizes == nil {
sizes = []int{10, 20, 50}
}
at := slices.Index(sizes, *size)
if total < 0 || *page < 0 || at < 0 || slices.Min(sizes) < 1 {
panic(fmt.Sprintf("data: Pager page %d, size %d of %v, total %d", *page, *size, sizes, total))
}
old, oldSize := *page, *size
last := func() int { return max(0, (total-1) / *size) }
*page = min(*page, last())
row := ui.Row(c).Gap(k.D(6)).AlignItems(ui.Center).Role(ui.RoleGroup).Label(text)
row.Children(func() {
pixel.Text(c, k.Msg("per_page"), pixel.TextOptions{Slot: pixel.Faint})
labels := make([]string, len(sizes))
for i, s := range sizes {
labels[i] = strconv.Itoa(s)
}
if input.Segmented(c, k.Msg("per_page"), labels, &at, input.SegmentedOptions{Disabled: o.Disabled}).Changed {
*size, *page = sizes[at], 0
}
from, to := min(*page**size+1, total), min((*page+1)**size, total)
pixel.Text(c, fmt.Sprintf(k.Msg("range"), from, to, total), pixel.TextOptions{})
if input.IconButton(c, icon.ChevronLeft, k.Msg("prev"), input.IconButtonOptions{Disabled: o.Disabled || *page == 0}).Clicked {
*page--
}
if input.IconButton(c, icon.ChevronRight, k.Msg("next"), input.IconButtonOptions{Disabled: o.Disabled || *page >= last()}).Clicked {
*page++
}
})
res := PagerResult{Element: row, Changed: *page != old || *size != oldSize}
if res.Changed {
slog.Debug("8bit: pager", "label", text, "page", *page, "size", *size)
}
return res
}