Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cmd/viva/initfiles/AGENTS.md.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)`,
Expand Down
21 changes: 19 additions & 2 deletions ui/scroll.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions ui/scroll_bars_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
})
}
1 change: 1 addition & 0 deletions ui/scrollbars.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", "").
Expand Down