1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-28 13:48:53 +08:00

Added hierarchy navigation to TreeView. Resolves #489

This commit is contained in:
Oliver 2021-01-11 16:07:39 +01:00
parent 5406288b8e
commit 4624fa9b67

View File

@ -13,6 +13,8 @@ const (
treeDown treeDown
treePageUp treePageUp
treePageDown treePageDown
treeParent
treeChild
) )
// TreeNode represents one node in a tree view. // TreeNode represents one node in a tree view.
@ -235,6 +237,8 @@ func (n *TreeNode) GetLevel() int {
// - k, up arrow, left arrow: Move (the selection) up by one node. // - k, up arrow, left arrow: Move (the selection) up by one node.
// - g, home: Move (the selection) to the top. // - g, home: Move (the selection) to the top.
// - G, end: Move (the selection) to the bottom. // - G, end: Move (the selection) to the bottom.
// - J: Move (the selection) up one level.
// - K: Move (the selection) down one level (if it is shown).
// - Ctrl-F, page down: Move (the selection) down by one page. // - Ctrl-F, page down: Move (the selection) down by one page.
// - Ctrl-B, page up: Move (the selection) up by one page. // - Ctrl-B, page up: Move (the selection) up by one page.
// //
@ -419,7 +423,7 @@ func (t *TreeView) process() {
_, _, _, height := t.GetInnerRect() _, _, _, height := t.GetInnerRect()
// Determine visible nodes and their placement. // Determine visible nodes and their placement.
var graphicsOffset, maxTextX int var graphicsOffset, maxTextX, parentSelectedIndex int
t.nodes = nil t.nodes = nil
if t.root == nil { if t.root == nil {
return return
@ -469,6 +473,11 @@ func (t *TreeView) process() {
t.nodes = append(t.nodes, node) t.nodes = append(t.nodes, node)
} }
// Keep track of the parent of the selected node.
if selectedIndex < 0 && node.selectable && len(node.children) > 0 && node.expanded {
parentSelectedIndex = len(t.nodes) - 1
}
// Recurse if desired. // Recurse if desired.
return node.expanded return node.expanded
}) })
@ -547,6 +556,16 @@ func (t *TreeView) process() {
} }
} }
newSelectedIndex = selectedIndex newSelectedIndex = selectedIndex
case treeParent:
newSelectedIndex = parentSelectedIndex
case treeChild:
for newSelectedIndex < len(t.nodes)-1 {
newSelectedIndex++
if t.nodes[newSelectedIndex].selectable && t.nodes[newSelectedIndex].parent == t.nodes[selectedIndex] {
break MovementSwitch
}
}
newSelectedIndex = selectedIndex
} }
t.currentNode = t.nodes[newSelectedIndex] t.currentNode = t.nodes[newSelectedIndex]
if newSelectedIndex != selectedIndex { if newSelectedIndex != selectedIndex {
@ -731,16 +750,18 @@ func (t *TreeView) InputHandler() func(event *tcell.EventKey, setFocus func(p Pr
t.movement = treeEnd t.movement = treeEnd
case 'j': case 'j':
t.movement = treeDown t.movement = treeDown
case 'J':
t.movement = treeChild
case 'k': case 'k':
t.movement = treeUp t.movement = treeUp
case 'K':
t.movement = treeParent
case ' ': case ' ':
selectNode() selectNode()
} }
case tcell.KeyEnter: case tcell.KeyEnter:
selectNode() selectNode()
} }
t.process()
}) })
} }