From 1e4b2ad047812034145b93cc1810da09f3eab00c Mon Sep 17 00:00:00 2001 From: lick <2188718831@qq.com> Date: Wed, 11 Nov 2026 21:20:28 -0800 Subject: [PATCH] feat(ui): let a scroll area draw its bars with the product's own geometry ScrollArea.Bars hands a components.ScrollBarStyle to the area's own state, which the paint of the bars reads, so a product drawing eleven point tracks with five point thumbs sets that once per area instead of living with Viva's ten and six. The zero style keeps Viva's geometry, so nothing changes for a page that never asks for its own bars. The init guide lists the method. Tests check that the style reaches the area's state and that an area without one keeps the zero style. Co-Authored-By: AtomCode (deepseek-v4.1-flash) --- cmd/viva/initfiles/AGENTS.md.txt | 2 ++ ui/scroll.go | 21 +++++++++++-- ui/scroll_bars_test.go | 51 ++++++++++++++++++++++++++++++++ ui/scrollbars.go | 1 + 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 ui/scroll_bars_test.go diff --git a/cmd/viva/initfiles/AGENTS.md.txt b/cmd/viva/initfiles/AGENTS.md.txt index 821cccf1d..4b879a3d4 100644 --- a/cmd/viva/initfiles/AGENTS.md.txt +++ b/cmd/viva/initfiles/AGENTS.md.txt @@ -97,6 +97,8 @@ func (m *model) page(c ui.C) { `Viewport`, `Max()`, `Pinned()`, `ScrollTo(px)` and `ScrollToEnd()` (also for the area a `ui.List` or `ui.Tree` returns); ask for it in each paint and do not keep the pointer. `.Sideways()` scrolls left and right too (`OffsetX`, `ContentWidth`, `ViewportWidth`, `MaxX()` and `ScrollToX(px)`), and `.Scrollbars()` draws draggable bars over the overflowing edges. + `.Bars(components.ScrollBarStyle{Size, HoverSize, Inset, MinThumb})` gives those bars your own geometry; the zero style + keeps Viva's, so set only the fields your product changes. - `ui.Choice` (a segmented single choice) and `ui.Tabs` are one tab stop each; the arrow keys, Home and End move to an option and choose it, so do not wire `components.SegmentedControlKeyDown` or `TabsKeyDown` yourself. - A tree: keep a `ui.TreeState` with `Node` describing each visible row, `ui.Tree(c, &m.tree, n, label, nil)`, diff --git a/ui/scroll.go b/ui/scroll.go index 95f9ef221..cf58f84a7 100644 --- a/ui/scroll.go +++ b/ui/scroll.go @@ -1,6 +1,9 @@ package ui -import "github.com/vivaui/viva/gfx" +import ( + "github.com/vivaui/viva/components" + "github.com/vivaui/viva/gfx" +) // ScrollState is the scroll position of one ScrollArea. It lives in the view, keyed // by the area's identity, so the application keeps nothing: declare the area with an @@ -40,7 +43,10 @@ type ScrollState struct { toEnd bool // ScrollToEnd was called and no layout has honored it yet sideways bool // Box.Sideways: the area scrolls left and right too bars bool // Box.Scrollbars: the area draws its own scrollbars - dragging bool // a press on one of the area's scrollbars has the pointer + // barStyle is Box.Bars: the geometry of the bars the area draws, or Viva's own when + // it is the zero style. + barStyle components.ScrollBarStyle + dragging bool // a press on one of the area's scrollbars has the pointer } // scrollStore keeps the ScrollStates of a view, by the identity of their areas. A slot @@ -307,6 +313,17 @@ func (b Box) areaState() *ScrollState { return nil } +// Bars overrides the geometry of the scrollbars the area draws: the track width, the +// hover overlay width, the inset from the track's edge and the least thumb. The zero +// style keeps Viva's own. It applies to the bars Box.Scrollbars turns on, so call it +// alongside, and has no effect on a box that is not a ScrollArea. +func (b Box) Bars(style components.ScrollBarStyle) Box { + if s := b.areaState(); s != nil { + s.barStyle = style + } + return b +} + // ScrollState returns the scroll position of a scroll area: the one ui.Scroll // declares, or the area of a List or a Tree. Read Offset, Content and Viewport (as of // the last layout), or move it by program with ScrollTo and ScrollToEnd, for diff --git a/ui/scroll_bars_test.go b/ui/scroll_bars_test.go new file mode 100644 index 000000000..96747dfeb --- /dev/null +++ b/ui/scroll_bars_test.go @@ -0,0 +1,51 @@ +package ui + +import ( + "testing" + + "github.com/vivaui/viva/components" + "github.com/vivaui/viva/gfx" +) + +// Box.Bars hands the style to the area's own state, which is what the paint of the bars +// reads, so a product whose bars are its own measures them itself. The area is declared +// without Box.Scrollbars here: this checks the plumbing, and the paint path reads the +// same field. +func TestBarsReachTheAreasScrollState(t *testing.T) { + var view View + var canvas gfx.Canvas + style := components.ScrollBarStyle{Size: 11, HoverSize: 5, Inset: 1, MinThumb: 20} + view.Draw(&canvas, gfx.LightTheme(), gfx.Rect{Width: 100, Height: 100}, func(f *Frame) { + f.Column().Fill().Children(func() { + b := f.ScrollArea(listID).Grow(1).Bars(style) + state := b.areaState() + if state == nil { + t.Fatal("the scroll area has no state") + } + if state.barStyle != style { + t.Fatalf("bar style = %+v, want %+v", state.barStyle, style) + } + f.Slot().Height(10) + }) + }) +} + +// An area that sets no style keeps Viva's own geometry, so nothing changes for a page +// that never asks for its own bars. +func TestBarsDefaultToTheSharedGeometry(t *testing.T) { + var view View + var canvas gfx.Canvas + view.Draw(&canvas, gfx.LightTheme(), gfx.Rect{Width: 100, Height: 100}, func(f *Frame) { + f.Column().Fill().Children(func() { + b := f.ScrollArea(listID).Grow(1) + state := b.areaState() + if state == nil { + t.Fatal("the scroll area has no state") + } + if state.barStyle != (components.ScrollBarStyle{}) { + t.Fatalf("bar style = %+v, want the zero style", state.barStyle) + } + f.Slot().Height(10) + }) + }) +} diff --git a/ui/scrollbars.go b/ui/scrollbars.go index 95b77a9e3..26ca0a08b 100644 --- a/ui/scrollbars.go +++ b/ui/scrollbars.go @@ -16,6 +16,7 @@ func (f *frame) paintScrollbars(n *node, s *ScrollState) { parts := f.canvas.Tree().Sub(s.id.Scope(), s.id.Name()) vertical, horizontal := parts.At(0), parts.At(1) area := components.BeginScrollArea(f.canvas, f.theme, n.rect, content). + Bars(s.barStyle). Offset(s.OffsetX, s.Offset). HoverScrollbars(true). // thin, over the content, shown while it overflows VerticalScrollbar(vertical, "Vertical scrollbar", "").