diff --git a/ui/list.go b/ui/list.go index e7901d5dc..79f6fb7aa 100644 --- a/ui/list.go +++ b/ui/list.go @@ -829,14 +829,20 @@ func List(c C, state *ListState, n int, row func(ListRow)) Box { return area } -// listRowScope is the scope of the rows of the List whose area is area, so a -// Shortcut on the box List returns covers them as well as the area's own scope. +// listRowScope is the scope of the rows of the List or Tree whose area is area, +// so a Shortcut on the box List or Tree returns covers them as well as the +// area's own scope. func (p *Page) listRowScope(area int32) gfx.Scope { for _, state := range p.listStates { if state.list.area == area { return state.list.rows } } + for _, state := range p.trees { + if state.list.area == area { + return state.rows + } + } return 0 } diff --git a/ui/shortcut.go b/ui/shortcut.go index d485be9e2..6f90a0d98 100644 --- a/ui/shortcut.go +++ b/ui/shortcut.go @@ -53,8 +53,9 @@ func Shortcut(c C, modifiers gfx.Modifiers, key string) bool { // Shortcut is ui.Shortcut for while the keyboard focus is on the element or in it: a // list's Delete acts on the list being worked in, a panel's Primary+F searches it. // Where Shortcuts of one chord nest, the innermost one with the focus acts. The -// element is a control, or a container of the page; the rows of a List count as -// inside the box List returns, so its Shortcut acts while a row has the focus. +// element is a control, or a container of the page; the rows of a List or a Tree +// count as inside the box List or Tree returns, so its Shortcut acts while a row +// has the focus. // // list := ui.Column(c).Children(rows) // if list.Shortcut(0, "delete") { diff --git a/ui/shortcut_test.go b/ui/shortcut_test.go index 01f40cefc..ebba95abe 100644 --- a/ui/shortcut_test.go +++ b/ui/shortcut_test.go @@ -141,6 +141,32 @@ func TestShortcutLeavesPlainKeysToAField(t *testing.T) { } } +// A Tree's rows are inside the box Tree returns, as a List's are, so its Shortcut +// acts while one of them has the keyboard focus. +func TestTreeShortcutActsWhileARowHasFocus(t *testing.T) { + rows := []string{"alpha", "beta"} + var state ui.TreeState + state.Node = func(int) ui.TreeNode { return ui.TreeNode{} } + state.Label = func(i int) string { return rows[i] } + var opens int + d := openPage(t, func(c ui.C) { + ui.Column(c).Fill().Pad(16).Children(func() { + tree := ui.Tree(c, &state, len(rows), state.Label, nil).Grow(1) + if tree.Shortcut(0, "f4") { + opens++ + } + }) + }) + d.click("alpha") + if got := d.focused(); got != "alpha" { + t.Fatalf("focus = %q, want the row under the pointer", got) + } + d.key(gfx.KeyF4) + if opens != 1 { + t.Fatalf("the Tree's F4 acted %d times with a row focused", opens) + } +} + // A List's rows are inside the box List returns, so its Shortcut acts while one // of them has the keyboard focus. func TestListShortcutActsWhileARowHasFocus(t *testing.T) {