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
8 changes: 4 additions & 4 deletions ui/elements.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func textFieldOf[T TextValue](c C, value T, label, placeholder string, base bool
b = c.f.Field(field, id, label, placeholder)
}
c.f.bindField(b, field)
p.fields = append(p.fields, fieldEntry{id: id, field: field})
p.fields = append(p.fields, fieldEntry{id: id, field: field, box: b.i})
return b
}

Expand All @@ -312,7 +312,7 @@ func TextFieldID(c C, id gfx.ID, field *Field, label, placeholder string) Box {
p := c.page
b := c.f.Field(field, id, label, placeholder)
c.f.bindField(b, field)
p.fields = append(p.fields, fieldEntry{id: id, field: field})
p.fields = append(p.fields, fieldEntry{id: id, field: field, box: b.i})
return b
}

Expand All @@ -329,7 +329,7 @@ func TextArea[T TextValue](c C, value T, label, placeholder string) Box {
if p.areaKey == nil {
p.keyRoute, p.areaKey = (*Page).routeKey, (*Field).Key // Up and Down by row
}
p.fields = append(p.fields, fieldEntry{id: id, field: field})
p.fields = append(p.fields, fieldEntry{id: id, field: field, box: b.i})
return b
}

Expand All @@ -349,7 +349,7 @@ func TextAreaID(c C, id gfx.ID, field *Field, label, placeholder string) Box {
if p.areaKey == nil {
p.keyRoute, p.areaKey = (*Page).routeKey, (*Field).Key // Up and Down by row
}
p.fields = append(p.fields, fieldEntry{id: id, field: field})
p.fields = append(p.fields, fieldEntry{id: id, field: field, box: b.i})
return b
}

Expand Down
10 changes: 5 additions & 5 deletions ui/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -1210,10 +1210,10 @@ func (p *Page) listKey(event gfx.KeyEvent) (gfx.EventResult, bool) {
}

// listWheelX scrolls the rows of the List with RowWidth under the pointer sideways,
// by the horizontal wheel or by the wheel with Shift. It reports whether it moved a
// list, and whether the event was a sideways one alone, left with nothing to scroll
// down.
func (p *Page) listWheelX(event gfx.ScrollEvent) (moved, only bool) {
// by the horizontal wheel or by the wheel with Shift: a list inside panel only, when
// the pointer is over one (frame.wheelPanel). It reports whether it moved a list, and
// whether the event was a sideways one alone, left with nothing to scroll down.
func (p *Page) listWheelX(event gfx.ScrollEvent, panel int32) (moved, only bool) {
dx, only := event.DeltaX, event.DeltaY == 0
if dx == 0 && event.Modifiers&gfx.ModifierShift != 0 {
dx, only = event.DeltaY, true
Expand All @@ -1224,7 +1224,7 @@ func (p *Page) listWheelX(event gfx.ScrollEvent) (moved, only bool) {
dx = wheelStep(dx, event.Precise) // a notch moves at least a line, as down the list
for _, s := range p.listStates {
in := &s.list
if s.RowWidth <= 0 || in.f == nil || !in.f.containsPoint(int(in.area), event.X, event.Y) {
if s.RowWidth <= 0 || in.f == nil || !in.f.containsPoint(int(in.area), event.X, event.Y) || !in.f.within(in.area, panel) {
continue
}
room := s.RowWidth - in.view.Width
Expand Down
1 change: 1 addition & 0 deletions ui/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,6 @@ func movedFrom(prev []string, label string, number int) bool {
type fieldEntry struct {
id gfx.ID
field *Field
box int32 // the field's element, for the wheel over a panel
key func(gfx.KeyEvent) gfx.EventResult // Box.OnKey, while this field has the keyboard
}
36 changes: 25 additions & 11 deletions ui/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ type Page struct {
sliderRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // the keys of a Slider
comboRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool)
treeRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool)
listRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // moves and chooses rows of a List
wheelXRoute func(*Page, gfx.ScrollEvent) (moved, only bool) // the sideways wheel of a List with RowWidth
typeRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // a Tree's typeahead, from OnShortcut
listRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // moves and chooses rows of a List
wheelXRoute func(*Page, gfx.ScrollEvent, int32) (moved, only bool) // the sideways wheel of a List with RowWidth, inside a wheel panel
typeRoute func(*Page, gfx.KeyEvent) (gfx.EventResult, bool) // a Tree's typeahead, from OnShortcut
wheelRoute func(*Page, gfx.ScrollEvent) (gfx.EventResult, bool)
textRoute func(*Page, gfx.ID) *app.TextInput
}
Expand Down Expand Up @@ -1155,8 +1155,10 @@ func (p *Page) pointerOver(r Rect) bool {
}

// Wheel scrolls an open Select, else the scroll area under the pointer, and gives
// an event neither takes to the OnScroll Config was given. It is the Config's
// OnScroll; a host that draws a page into a region calls it itself.
// an event neither takes to the OnScroll Config was given. Over an open Popover's
// panel, a Dialog or a modal Drawer only what is inside the panel scrolls, and an
// event nothing there takes stops at the panel. It is the Config's OnScroll; a host
// that draws a page into a region calls it itself.
func (p *Page) Wheel(event gfx.ScrollEvent) gfx.EventResult {
if h := p.controlOf(event.Target); h != nil && h.Scroll != nil {
if result := h.Scroll(event); result.Handled {
Expand All @@ -1168,10 +1170,15 @@ func (p *Page) Wheel(event gfx.ScrollEvent) gfx.EventResult {
return state.Wheel(event)
}
}
f := p.view.frame
panel := int32(-1)
if f != nil {
panel = f.wheelPanel(event.X, event.Y)
}
// A TextArea under the pointer scrolls its own rows first; at its end the wheel goes on
// to the page, as in a browser.
for _, entry := range p.fields {
if entry.field.rows && entry.field.area != nil && entry.field.area.Wheel(event) {
if entry.field.rows && entry.field.area != nil && f.within(entry.box, panel) && entry.field.area.Wheel(event) {
return gfx.EventResult{Handled: true, Repaint: true}
}
}
Expand All @@ -1183,17 +1190,21 @@ func (p *Page) Wheel(event gfx.ScrollEvent) gfx.EventResult {
moved := false
if p.wheelXRoute != nil {
var only bool
if moved, only = p.wheelXRoute(p, event); only && moved {
if moved, only = p.wheelXRoute(p, event, panel); only && moved {
return gfx.EventResult{Handled: true, Repaint: true}
}
}
if result := p.view.Wheel(event); result.Handled || result.Repaint {
result.Repaint = result.Repaint || moved
return result
manual := false
if f != nil {
var result gfx.EventResult
if result, manual = f.wheelArea(panel, event); result.Handled || result.Repaint {
result.Repaint = result.Repaint || moved
return result
}
}
for i := len(p.tables) - 1; i >= 0; i-- {
route := &p.tables[i]
if !route.scrolls && !route.sideways || !pointInRect(event.X, event.Y, route.bounds) {
if !route.scrolls && !route.sideways || !pointInRect(event.X, event.Y, route.bounds) || panel >= 0 && route.root != panel {
continue
}
state := &route.state.Scroll
Expand All @@ -1208,6 +1219,9 @@ func (p *Page) Wheel(event gfx.ScrollEvent) gfx.EventResult {
if moved {
return gfx.EventResult{Handled: true, Repaint: true}
}
if panel >= 0 && !manual {
return gfx.EventResult{Handled: true} // what the panel covers is out of the wheel's reach
}
if p.baseScroll != nil {
return p.baseScroll(event)
}
Expand Down
152 changes: 152 additions & 0 deletions ui/popover_wheel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package ui_test

import (
"testing"

"github.com/vivaui/viva/components"
"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/ui"
)

// A wheel over a Popover is the panel's. It scrolls a list in the panel, and when that
// list is at its end, or the pointer is on the panel's padding, it stops at the panel
// instead of scrolling the page area painted under it. The panel hangs off a toolbar
// button and is declared before the page area, as a title bar's menu is, so the area
// comes later in the page than the panel's list. Outside the panel the wheel scrolls the
// page as before: a Popover is not modal.
func TestAWheelOverAPopoverStaysInThePanel(t *testing.T) {
open := true
var list, area ui.Box
d := openPage(t, func(c ui.C) {
ui.Column(c).Fill().Children(func() {
projects := ui.Button(c, "Projects")
ui.PopoverWith(c, projects, ui.PopoverSpec{Width: 200}, &open, func() {
list = ui.Scroll(c).FillWidth().MaxHeight(150).Children(func() {
for range 20 {
ui.Slot(c).Height(30)
}
})
})
area = ui.Scroll(c).Grow(1).FillWidth().Children(func() {
for range 40 {
ui.Slot(c).Height(30)
}
})
})
})
wheel := func(x, y, dy float32) {
t.Helper()
if err := d.driver.Scroll(gfx.ScrollEvent{X: x, Y: y, DeltaY: dy, Precise: true}); err != nil {
t.Fatal(err)
}
d.repaint()
}
panel, page := list.ScrollState(), area.ScrollState()
r := list.Rect()
if pageRect := area.Rect(); r.Y < pageRect.Y || r.Y+r.Height > pageRect.Y+pageRect.Height {
t.Fatalf("the panel's list %v is not over the page area %v", r, pageRect)
}
x, y := r.X+r.Width/2, r.Y+r.Height/2

wheel(x, y, 40)
if panel.Offset != 40 || page.Offset != 0 {
t.Fatalf("a wheel over the panel's list moved it to %v and the page under it to %v, want 40 and 0", panel.Offset, page.Offset)
}
wheel(x, y, 5000)
if panel.Offset != panel.Max() {
t.Fatalf("the panel's list is at %v, want its end %v", panel.Offset, panel.Max())
}
wheel(x, y, 60)
if page.Offset != 0 {
t.Errorf("a wheel over the panel's list at its end scrolled the page under the panel to %v", page.Offset)
}
padding := pageTheme.Metrics.PadSurface
wheel(x, r.Y-padding/2, 60)
if page.Offset != 0 {
t.Errorf("a wheel over the panel's padding scrolled the page under the panel to %v", page.Offset)
}
pageRect := area.Rect()
wheel(r.X+r.Width+padding+40, pageRect.Y+pageRect.Height-20, 30)
if page.Offset != 30 {
t.Errorf("a wheel over the page beside the panel moved it to %v, want 30", page.Offset)
}
}

// A Table that scrolls itself takes the wheel inside a Popover as it does on the page,
// and one painted under the panel does not: past the panel's table's end the wheel
// stops at the panel. Beside the panel the page's table scrolls.
func TestATableInAPopoverTakesTheWheelAndTheTableUnderItDoesNot(t *testing.T) {
rows := make(ui.TableRows, 60)
for i := range rows {
rows[i] = []string{"row"}
}
open := true
var inner, under ui.TableState
var panelTable, pageTable ui.Box
d := openPage(t, func(c ui.C) {
ui.Column(c).Fill().Children(func() {
projects := ui.Button(c, "Projects")
ui.PopoverWith(c, projects, ui.PopoverSpec{Width: 200}, &open, func() {
panelTable = ui.Table(c, &inner, ui.TableSpec{
Columns: []components.TableColumn{{}}, Rows: gfx.Item(1).Scope(), Source: &rows,
}).FillWidth().Height(150)
})
pageTable = ui.Table(c, &under, ui.TableSpec{
Columns: []components.TableColumn{{}}, Rows: gfx.Item(2).Scope(), Source: &rows,
}).FillWidth().Height(320)
})
})
wheel := func(x, y, dy float32) {
t.Helper()
if err := d.driver.Scroll(gfx.ScrollEvent{X: x, Y: y, DeltaY: dy, Precise: true}); err != nil {
t.Fatal(err)
}
d.repaint()
}
r, pageRect := panelTable.Rect(), pageTable.Rect()
if r.Y < pageRect.Y || r.Y+r.Height > pageRect.Y+pageRect.Height {
t.Fatalf("the panel's table %v is not over the page's %v", r, pageRect)
}
x, y := r.X+r.Width/2, r.Y+r.Height/2

wheel(x, y, 40)
if inner.Scroll.Offset != 40 || under.Scroll.Offset != 0 {
t.Fatalf("a wheel over the panel's table moved it to %v and the table under the panel to %v, want 40 and 0", inner.Scroll.Offset, under.Scroll.Offset)
}
wheel(x, y, 50000)
wheel(x, y, 60)
if inner.Scroll.Offset != inner.Scroll.Max() || under.Scroll.Offset != 0 {
t.Errorf("past its end the panel's table is at %v (end %v) and the table under the panel at %v, want 0", inner.Scroll.Offset, inner.Scroll.Max(), under.Scroll.Offset)
}
wheel(pageRect.X+pageRect.Width-20, pageRect.Y+pageRect.Height-20, 30)
if under.Scroll.Offset != 30 {
t.Errorf("a wheel over the page's table beside the panel moved it to %v, want 30", under.Scroll.Offset)
}
}

// A Modal keeps the wheel from the page under its scrim, over its surface and beside it.
func TestAWheelOverAModalLeavesThePageUnderIt(t *testing.T) {
open := true
var area ui.Box
d := openPage(t, func(c ui.C) {
ui.Column(c).Fill().Children(func() {
area = ui.Scroll(c).Grow(1).FillWidth().Children(func() {
for range 40 {
ui.Slot(c).Height(30)
}
})
})
ui.Modal(c, &open, "Rename", func() { ui.Text(c, "A short body") }, nil)
})
page := area.ScrollState()
r := area.Rect()
for _, at := range [][2]float32{{r.X + r.Width/2, r.Y + r.Height/2}, {r.X + 10, r.Y + r.Height - 10}} {
if err := d.driver.Scroll(gfx.ScrollEvent{X: at[0], Y: at[1], DeltaY: 60, Precise: true}); err != nil {
t.Fatal(err)
}
d.repaint()
if page.Offset != 0 {
t.Fatalf("a wheel at %v with a Modal open scrolled the page under it to %v", at, page.Offset)
}
}
}
Loading
Loading