1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-26 13:49:06 +08:00

TreeView scrolling now allows current selection to go out of view. Resolves #613

This commit is contained in:
Oliver 2021-06-01 10:44:49 +02:00
parent 807e706f86
commit 25fe827270

View File

@ -15,6 +15,8 @@ const (
treePageDown treePageDown
treeParent treeParent
treeChild treeChild
treeScrollUp // Move up without changing the selection, even when off screen.
treeScrollDown
) )
// TreeNode represents one node in a tree view. // TreeNode represents one node in a tree view.
@ -589,11 +591,13 @@ func (t *TreeView) process() {
selectedIndex = newSelectedIndex selectedIndex = newSelectedIndex
// Move selection into viewport. // Move selection into viewport.
if selectedIndex-t.offsetY >= height { if t.movement != treeScrollDown && t.movement != treeScrollUp {
t.offsetY = selectedIndex - height + 1 if selectedIndex-t.offsetY >= height {
} t.offsetY = selectedIndex - height + 1
if selectedIndex < t.offsetY { }
t.offsetY = selectedIndex if selectedIndex < t.offsetY {
t.offsetY = selectedIndex
}
} }
} else { } else {
// If selection is not visible or selectable, select the first candidate. // If selection is not visible or selectable, select the first candidate.
@ -625,9 +629,9 @@ func (t *TreeView) Draw(screen tcell.Screen) {
// Scroll the tree. // Scroll the tree.
x, y, width, height := t.GetInnerRect() x, y, width, height := t.GetInnerRect()
switch t.movement { switch t.movement {
case treeUp: case treeUp, treeScrollUp:
t.offsetY-- t.offsetY--
case treeDown: case treeDown, treeScrollDown:
t.offsetY++ t.offsetY++
case treeHome: case treeHome:
t.offsetY = 0 t.offsetY = 0
@ -810,10 +814,10 @@ func (t *TreeView) MouseHandler() func(action MouseAction, event *tcell.EventMou
} }
consumed = true consumed = true
case MouseScrollUp: case MouseScrollUp:
t.offsetY-- t.movement = treeScrollUp
consumed = true consumed = true
case MouseScrollDown: case MouseScrollDown:
t.offsetY++ t.movement = treeScrollDown
consumed = true consumed = true
} }