Skip to content
Closed
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
52 changes: 52 additions & 0 deletions ui/control_parts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
7 changes: 7 additions & 0 deletions ui/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down
2 changes: 2 additions & 0 deletions ui/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}
Expand Down
Loading