diff --git a/base/page.go b/base/page.go index 5b5b85ba..8b3b79a2 100644 --- a/base/page.go +++ b/base/page.go @@ -811,11 +811,15 @@ func (p *Page) Paint(Surface *gfx.Canvas, Size Size) { } // Draw declares view in bounds of canvas. When a Memo's copy does not fit where -// the page put it, what the page painted is taken back and it is painted again, -// with that Memo declared. A page drawn into a frame slot by Slot.Draw is laid -// out at the slot's rectangle; a page given no Tree names its controls in the -// canvas's. A host that draws a page this way routes its input with Key, -// Pointer, Wheel and TextInput; Config does that for a whole window. +// the page put it, or a container query got another answer than the layout +// gives, what the page painted is taken back and it is painted again, with that +// Memo declared and the query answered from the layout. In an event pass the +// second paint does not deliver the event again: the first one ran the handlers +// it reached, and what they changed is not taken back. A page drawn into a frame +// slot by Slot.Draw is laid out at the slot's rectangle; a page given no Tree +// names its controls in the canvas's. A host that draws a page this way routes +// its input with Key, Pointer, Wheel and TextInput; Config does that for a whole +// window. // // Clicks are the part of that routing a host completes itself when it paints and // presents the canvas on its own. Clicked(id) is true only in an event pass, @@ -843,12 +847,16 @@ func (p *Page) Draw(Surface *gfx.Canvas, bounds Rect, View func(C)) { Surface.SetTree(p.tree) } start := Surface.Mark() - for try := 0; ; try++ { - p.paintOnce(Surface, bounds, View) - if !p.retry || try > 0 || !Surface.Rewind(start) { - break - } + p.paintOnce(Surface, bounds, View) + if p.retry && Surface.Rewind(start) { frametrace.Retry() + // The view declares its handlers again. The event pass's event already + // reached them in the paint taken back, so it is held out of this one: a + // Shortcut that folds a sidebar a Container measures would otherwise fold it + // back. + held := Surface.SuspendDelivery() + p.paintOnce(Surface, bounds, View) + Surface.ResumeDelivery(held) } p.retry = false p.settleHeld() diff --git a/gfx/canvas.go b/gfx/canvas.go index ba0861b2..7250e330 100644 --- a/gfx/canvas.go +++ b/gfx/canvas.go @@ -2732,6 +2732,42 @@ func (canvas *Canvas) DeliverPointer(event PointerEvent, size Size, paint func(* return consumed } +// Delivery is the event of a pass, a click's or a pointer event's, as +// SuspendDelivery took it out of the canvas for ResumeDelivery to put back. +type Delivery struct { + clicked ID + clickInfo ClickInfo + eventPass bool + pointer PointerEvent + hasPointer bool +} + +// SuspendDelivery takes the event out of the pass until ResumeDelivery puts it +// back: what is painted in between is read-only, as an ordinary paint is. +// Clicked, ClickedIn, ClickTarget, ClickInfo, PointerOn, PointerIn and +// PointerPass answer nothing, Delivering reports false, and AfterPaint and +// Consume do nothing. What the pass did before stays done: the functions it +// deferred, the event a control consumed, the focus it asked for. +// +// A paint that declares the same handlers again within one pass suspends the +// event around the second declaration: a page that paints once more to answer +// a container query from the layout it just found (ui.Page.Draw) does, since +// its first paint ran the handlers the event reached and what they changed is +// not taken back. Outside a pass both are free and change nothing. +func (canvas *Canvas) SuspendDelivery() Delivery { + held := Delivery{clicked: canvas.clicked, clickInfo: canvas.clickInfo, eventPass: canvas.eventPass, pointer: canvas.pointer, hasPointer: canvas.hasPointer} + canvas.clicked, canvas.clickInfo, canvas.eventPass = ID{}, ClickInfo{}, false + canvas.pointer, canvas.hasPointer = PointerEvent{}, false + return held +} + +// ResumeDelivery puts back the event SuspendDelivery took, for what is painted +// after: the rest of a host's paint that declares its own handlers once. +func (canvas *Canvas) ResumeDelivery(held Delivery) { + canvas.clicked, canvas.clickInfo, canvas.eventPass = held.clicked, held.clickInfo, held.eventPass + canvas.pointer, canvas.hasPointer = held.pointer, held.hasPointer +} + // monoWidth is the width of value set in the fixed-pitch face of style: the cells // monoCells counts for its characters. func (canvas *Canvas) monoWidth(value string, style TextStyle) float32 { diff --git a/gfx/identity_test.go b/gfx/identity_test.go index f12bc561..9f7ce601 100644 --- a/gfx/identity_test.go +++ b/gfx/identity_test.go @@ -401,3 +401,66 @@ func TestAfterPaintDefersOnlyInTheEventPass(t *testing.T) { t.Fatalf("deferred functions ran as %v, want [1 2] once", order) } } + +// While delivery is suspended a paint reads no event and can defer nothing, and +// what the pass did before it stays done; resumed, the event is there again for +// what is painted after. +func TestSuspendedDeliveryIsReadOnlyUntilResumed(t *testing.T) { + var canvas Canvas + var tree Tree + id := tree.ID(tree.Feature("page"), "go") + list := tree.Sub(tree.Feature("page"), tree.Name("rows")) + ran := 0 + canvas.Reset() + + info := ClickInfo{Modifiers: ModifierShift, Clicks: 2} + canvas.BeginEventPass(id, info) + canvas.AfterPaint(func() { ran++ }) + held := canvas.SuspendDelivery() + if canvas.Clicked(id) || canvas.Delivering() { + t.Fatal("a suspended click pass still delivers the click") + } + if _, ok := canvas.ClickTarget(); ok { + t.Fatal("a suspended click pass still names a target") + } + if _, ok := canvas.ClickInfo(); ok { + t.Fatal("a suspended click pass still describes the click") + } + canvas.AfterPaint(func() { ran += 10 }) + canvas.ResumeDelivery(held) + if got, ok := canvas.ClickInfo(); !canvas.Clicked(id) || !ok || got != info { + t.Fatalf("the resumed pass delivers Clicked %v, info %v %v; want the click as it began", canvas.Clicked(id), got, ok) + } + canvas.EndEventPass() + canvas.RunAfterPaint() + if ran != 1 { + t.Fatalf("deferred work ran to %d, want only what the pass deferred before it was suspended (1)", ran) + } + + event := PointerEvent{Kind: PointerDown, Button: PointerButtonPrimary, Target: list.At(3)} + canvas.BeginPointerPass(event) + canvas.Consume() + held = canvas.SuspendDelivery() + if _, ok := canvas.PointerOn(list.At(3)); ok { + t.Fatal("a suspended pointer pass still delivers the event") + } + if _, _, ok := canvas.PointerIn(list); ok { + t.Fatal("a suspended pointer pass still delivers the event to its list") + } + if _, _, ok := canvas.PointerPass(); ok || canvas.Delivering() { + t.Fatal("a suspended pointer pass still reports the event") + } + canvas.ResumeDelivery(held) + if got, ok := canvas.PointerOn(list.At(3)); !ok || got != event { + t.Fatal("the resumed pass does not deliver the pointer event") + } + if !canvas.EndPointerPass() { + t.Fatal("the event consumed before the pass was suspended is no longer consumed") + } + + held = canvas.SuspendDelivery() + canvas.ResumeDelivery(held) + if canvas.Delivering() || canvas.Clicked(id) { + t.Fatal("suspending and resuming an ordinary paint made it deliver") + } +} diff --git a/ui/repaint_delivery_test.go b/ui/repaint_delivery_test.go new file mode 100644 index 00000000..84553467 --- /dev/null +++ b/ui/repaint_delivery_test.go @@ -0,0 +1,105 @@ +package ui_test + +import ( + "testing" + + "github.com/vivaui/viva/gfx" + "github.com/vivaui/viva/ui" +) + +// foldModel is a window whose sidebar folds away and comes back by a Shortcut, a +// mouse button's Shortcut, a button clicked or pressed with Enter, and a press on a +// pad. Beside the sidebar is a Container whose query the fold answers the other +// way, so the paint that delivers each of them finds the answer wrong and is done +// again before the pass ends. +type foldModel struct { + hidden bool + wide bool // the Container's query, as the last paint answered it + // How many times each handler ran, and the work the button deferred. + shortcuts, backs, clicks, afters, presses int +} + +func (m *foldModel) page(c ui.C) { + if ui.Shortcut(c, ui.Primary, "b") { + m.shortcuts++ + m.hidden = !m.hidden + } + if ui.Shortcut(c, 0, "mouse_back") { + m.backs++ + m.hidden = !m.hidden + } + ui.Column(c).Fill().Children(func() { + ui.Row(c).Pad(8).Gap(8).Children(func() { + if ui.Button(c, "Fold").Clicked() { + m.clicks++ + m.hidden = !m.hidden + c.After(func() { m.afters++ }) + } + pad := ui.ID(c) + ui.Column(c).Size(60, 30).Hit(pad, "Pad") + if event, ok := c.Canvas().PointerOn(pad); ok && event.Kind == gfx.PointerDown && event.Button == gfx.PointerButtonPrimary { + m.presses++ + m.hidden = !m.hidden + } + }) + ui.Row(c).FillWidth().Grow(1).Children(func() { + if !m.hidden { + ui.Column(c).Width(200).FillHeight() + } + ui.Container(c).Grow(1).FillHeight().Children(func() { + m.wide = c.AtLeast(ui.Width(400)) + ui.Text(c, "Page") + }) + }) + }) +} + +// A paint done again within one pass, because a Container's query got another +// answer than the layout gives, reads the event of the pass as delivered: each +// shortcut, click, key and press runs its handler once, and what a handler +// deferred runs once. +func TestAPaintDoneAgainDeliversTheEventOnce(t *testing.T) { + m := &foldModel{} + d := openPage(t, m.page) + if m.hidden || m.wide { + t.Fatalf("the window opens with the sidebar hidden %v and the page wide %v; want it shown and the page narrow", m.hidden, m.wide) + } + fold := func(what string, count *int, act func()) { + t.Helper() + before, hidden := *count, m.hidden + act() + if *count != before+1 { + t.Fatalf("%s ran its handler %d times, want once", what, *count-before) + } + if m.hidden == hidden { + t.Fatalf("%s left the sidebar hidden %v", what, m.hidden) + } + if m.wide != m.hidden { + t.Fatalf("after %s the page is wide %v with the sidebar hidden %v: the query did not follow", what, m.wide, m.hidden) + } + } + + // Enter first, while nothing else has taken the focus: Tab gives it to the button. + d.key(gfx.KeyTab) + if got := d.Focused(); got != "Fold" { + t.Fatalf("Tab focused %q, want the button", got) + } + fold("Enter on the button", &m.clicks, func() { d.key(gfx.KeyEnter) }) + if m.afters != 1 { + t.Fatalf("Enter's deferred work ran %d times, want once", m.afters) + } + fold("a click", &m.clicks, func() { d.click("Fold") }) + if m.afters != 2 { + t.Fatalf("after a click the deferred work has run %d times, want twice in all", m.afters) + } + + shortcut := func() { + if _, err := d.driver.ShortcutChord(ui.Primary, 'b'); err != nil { + t.Fatal(err) + } + } + fold("a Shortcut", &m.shortcuts, shortcut) + fold("a Shortcut again", &m.shortcuts, shortcut) + fold("a mouse button's Shortcut", &m.backs, func() { d.press("Pad", gfx.PointerButtonBack) }) + fold("a press on the pad", &m.presses, func() { d.press("Pad", gfx.PointerButtonPrimary) }) +}