diff --git a/ui/elements.go b/ui/elements.go index 3d43de938..231a5bc92 100644 --- a/ui/elements.go +++ b/ui/elements.go @@ -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 } @@ -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 } @@ -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 } @@ -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 } diff --git a/ui/list.go b/ui/list.go index 8a04d617d..7fe901a43 100644 --- a/ui/list.go +++ b/ui/list.go @@ -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 @@ -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 diff --git a/ui/names.go b/ui/names.go index c6f7e790c..c9d71361b 100644 --- a/ui/names.go +++ b/ui/names.go @@ -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 } diff --git a/ui/page.go b/ui/page.go index e47c91ee5..129f33288 100644 --- a/ui/page.go +++ b/ui/page.go @@ -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 } @@ -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 { @@ -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} } } @@ -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 @@ -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) } diff --git a/ui/popover_wheel_test.go b/ui/popover_wheel_test.go new file mode 100644 index 000000000..5a1a7cfe3 --- /dev/null +++ b/ui/popover_wheel_test.go @@ -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) + } + } +} diff --git a/ui/scroll.go b/ui/scroll.go index 0d3293b11..766dea436 100644 --- a/ui/scroll.go +++ b/ui/scroll.go @@ -205,34 +205,90 @@ func (s *ScrollState) Wheel(event gfx.ScrollEvent) gfx.EventResult { // by side each scroll alone. A sideways wheel goes the same way, to the innermost // Sideways area that can move along it. A wheel over a ScrollAt area goes to the // application's own Config.OnScroll instead: ScrollAt declares the area, and the -// event is handed back unhandled rather than bubbled to an outer ScrollArea. It is -// the function to give as Config.OnScroll for a page whose areas are all -// ScrollAreas; handle a Select's open list and a self-scrolling Table first, as -// they own the wheel while they are open or under the pointer. It reports an event -// that no area under the pointer can use as not handled. +// event is handed back unhandled rather than bubbled to an outer ScrollArea. Over a +// panel painted on the page that the pointer cannot reach through (an open Popover's +// panel, a Dialog, a modal Drawer) only the areas inside the panel are offered the +// wheel, wherever in the page the panel was declared, and an event none of them can +// use stops at the panel, handled, rather than scrolling what is under it. It is the +// function to give as Config.OnScroll for a page whose areas are all ScrollAreas; +// handle a Select's open list and a self-scrolling Table first, as they own the +// wheel while they are open or under the pointer. It reports an event that no area +// under the pointer can use, beside every such panel, as not handled. func (view *view) Wheel(event gfx.ScrollEvent) gfx.EventResult { f := view.frame if f == nil { return gfx.EventResult{} } - for i := len(f.nodes) - 1; i >= 0; i-- { + panel := f.wheelPanel(event.X, event.Y) + result, manual := f.wheelArea(panel, event) + if panel < 0 || manual || result.Handled || result.Repaint { + return result + } + return gfx.EventResult{Handled: true} +} + +// wheelArea is Wheel's search for the area under the pointer, among the areas inside +// panel when it is a panel's root (wheelPanel's) and among all of them when it is -1. +// manual reports that the pointer is over a ScrollAt area, whose wheel is the +// application's: the event comes back unhandled. +func (f *frame) wheelArea(panel int32, event gfx.ScrollEvent) (result gfx.EventResult, manual bool) { + first := int(max(panel, 0)) // what a panel holds is declared after its root + for i := len(f.nodes) - 1; i >= first; i-- { n := &f.nodes[i] if n.ext == 0 || f.exts[n.ext].scrollState == nil { continue } - if !f.containsPoint(i, event.X, event.Y) { + if !f.containsPoint(i, event.X, event.Y) || !f.within(int32(i), panel) { continue } if f.exts[n.ext].scrollManual { - return gfx.EventResult{} + return gfx.EventResult{}, true } s := f.exts[n.ext].scrollState if !s.canScroll(s.wheelSteps(event)) { continue } - return s.Wheel(event) + return s.Wheel(event), false + } + return gfx.EventResult{}, false +} + +// wheelPanel is the root of the panel under the pointer (x, y) that keeps the wheel +// from the page painted beneath it, the last painted of them when several are: an +// open Popover's panel, a Dialog or a modal Drawer, the last two covering the whole +// window with their scrim. It is -1 when the pointer is over none. A non-modal Drawer +// leaves the page beside it within reach, and an application's Overlay the wheel to +// the application, so neither is one. +func (f *frame) wheelPanel(x, y float32) int32 { + for k := len(f.floats) - 1; k >= 0; k-- { + i := f.floats[k] + n := &f.nodes[i] + switch { + case n.flags&(flagPopover|flagDialog|flagDrawer) == 0: + continue + case n.flags&flagDrawer != 0 && f.widgets.drawers[n.aux].spec.NonModal: + continue + } + if pointInRect(x, y, n.rect) { + return i + } + } + return -1 +} + +// within says whether element i is declared inside panel, a root wheelPanel gave; +// every element is when panel is -1. +func (f *frame) within(i, panel int32) bool { + return panel < 0 || f.rootOf(i) == panel +} + +// rootOf is the root of the tree element i is in: the page's, or a Popup's for what is +// declared inside one. It is -1 for -1. +func (f *frame) rootOf(i int32) int32 { + for i >= 0 && f.nodes[i].parent >= 0 { + i = f.nodes[i].parent } - return gfx.EventResult{} + return i } // containsPoint says whether a point is inside element i and inside every clip around diff --git a/ui/table.go b/ui/table.go index 58ea088ec..8870218c8 100644 --- a/ui/table.go +++ b/ui/table.go @@ -89,6 +89,7 @@ type TableSpec struct { // tableRoute is a Table of the last paint that scrolls itself, and where: the // page gives it the wheel and the keys that reach it. type tableRoute struct { + root int32 // the root of the tree the table is in: the page's or a panel's (frame.rootOf) state *TableState bounds Rect scrolls bool @@ -371,7 +372,7 @@ func (f *frame) paintTable(n *node) { } if f.page != nil { f.page.tables = append(f.page.tables, tableRoute{ - state: t.state, bounds: r, scrolls: scrolls, sideways: sideways, + root: f.rootOf(n.parent), state: t.state, bounds: r, scrolls: scrolls, sideways: sideways, bar: t.spec.Scrollbar, horizontal: t.spec.HorizontalScrollbar, }) }