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
10 changes: 8 additions & 2 deletions ui/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 3 additions & 2 deletions ui/shortcut.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down
26 changes: 26 additions & 0 deletions ui/shortcut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down