diff --git a/ui/control_parts_test.go b/ui/control_parts_test.go index 4e6e02972..d6f8ed260 100644 --- a/ui/control_parts_test.go +++ b/ui/control_parts_test.go @@ -154,3 +154,55 @@ func TestControlPartsRepaintAllocates(t *testing.T) { t.Fatalf("%v allocations a repaint", allocs) } } + +// A ContextMenu on a control with parts keeps its rows and its backdrop apart from +// the parts: a click on any row chooses it, and the click that dismisses the menu +// presses no part, rather than either reaching the control as a press on a part. +func TestControlContextMenuIsNotItsParts(t *testing.T) { + s := newStars() + var chosen []string + d := openPage(t, func(c ui.C) { + ui.Column(c).Pad(16).Gap(8).Children(func() { + rating := ui.Control(c, "Rating", &s.handlers).Size(240, 32).Draw(s.drawFn) + ui.ContextMenu(c, rating, func(menu *ui.MenuItems) { + for _, label := range [...]string{"Clear", "Half", "Full"} { + if menu.Item(label).Clicked() { + chosen = append(chosen, label) + } + } + }) + }) + }) + // The control's own area, right of the stars, opens the menu. + rating := d.hit("Rating").Bounds + x, y := rating.X+rating.Width-10, rating.Y+rating.Height/2 + openMenu := func() { + t.Helper() + if err := d.driver.ClickButton(x, y, gfx.PointerButtonSecondary); err != nil { + t.Fatal(err) + } + if !d.has("Clear") { + t.Fatal("a secondary press on the control did not open its menu") + } + } + for _, label := range [...]string{"Clear", "Half", "Full"} { + openMenu() + d.click(label) + if n := len(chosen); n == 0 || chosen[n-1] != label || d.has("Clear") { + t.Fatalf("a click on %s chose %q, menu shown %v", label, chosen, d.has("Clear")) + } + } + openMenu() + if allocs := testing.AllocsPerRun(100, d.repaint); allocs != 0 { + t.Fatalf("%v allocations a repaint with the menu open", allocs) + } + if err := d.driver.Click(rating.X+rating.Width/2, rating.Y+200); err != nil { + t.Fatal(err) + } + if d.has("Clear") { + t.Fatal("a click outside the menu left it open") + } + if len(s.pressed) != 0 || s.value != 0 { + t.Fatalf("the menu pressed the parts %v, value %d", s.pressed, s.value) + } +} diff --git a/ui/menu.go b/ui/menu.go index 03864a30c..862b88ad0 100644 --- a/ui/menu.go +++ b/ui/menu.go @@ -122,6 +122,13 @@ func contextMenu(c C, target Box, bound gfx.Scope, opened *menuOrigin, body func // pointer opened it, and the chosen row (or a dismissal) closes it. open = &menu.shown } + if bound.IsZero() { + // The target's scope holds the identities inside it: the parts of a + // Control (Handlers.Parts) and the children of a container. The rows and + // the backdrop of the menu take a place of their own under it, or a press + // on a row would reach the control as a press on one of its parts. + scope = p.tree.Sub(scope, p.menuName) + } p.declareMenu(c, menu, scope, open, -1, opened, body) } } diff --git a/ui/page.go b/ui/page.go index 74cad6922..433f85c07 100644 --- a/ui/page.go +++ b/ui/page.go @@ -252,6 +252,7 @@ type Page struct { freshAt gfx.Scope kindName [pkKinds]gfx.Name keyName gfx.Name + menuName gfx.Name // the place of a ContextMenu inside its target (contextMenu) held [2]armedButton states map[protoStateKey]protoState fields []fieldEntry @@ -423,6 +424,7 @@ func (p *Page) init(tree *gfx.Tree) { p.kindName[kind] = p.tree.Name(fmt.Sprintf("~k%d", kind)) } p.keyName = p.tree.Name("~key") + p.menuName = p.tree.Name("~menu") p.freshAt = p.tree.Sub(p.root, p.tree.Name("~new")) p.keys = map[keyKey]keyEntry{} p.intKeys = map[intKey]keyEntry{}