Fix tree node navigation with key arrows to be more usable for deep hierarchies

This commit is contained in:
Wojciech Figat
2021-12-07 14:59:22 +01:00
parent 1cc26c871d
commit 983d5dee31
2 changed files with 17 additions and 25 deletions

View File

@@ -339,12 +339,10 @@ namespace FlaxEditor.GUI.Tree
var node = SelectedNode; var node = SelectedNode;
// Check if has focus and if any node is focused and it isn't a root // Check if has focus and if any node is focused and it isn't a root
if (ContainsFocus && node != null && !node.IsRoot) if (ContainsFocus && node != null && node.AutoFocus)
{ {
var window = Root; var window = Root;
if (_keyUpdateTime >= KeyUpdateTimeout && window is WindowRootControl windowRoot && windowRoot.Window.IsFocused)
// Check if can perform update
if (_keyUpdateTime >= KeyUpdateTimeout)
{ {
bool keyUpArrow = window.GetKeyDown(KeyboardKeys.ArrowUp); bool keyUpArrow = window.GetKeyDown(KeyboardKeys.ArrowUp);
bool keyDownArrow = window.GetKeyDown(KeyboardKeys.ArrowDown); bool keyDownArrow = window.GetKeyDown(KeyboardKeys.ArrowDown);
@@ -358,38 +356,29 @@ namespace FlaxEditor.GUI.Tree
Assert.AreNotEqual(-1, myIndex); Assert.AreNotEqual(-1, myIndex);
// Up // Up
TreeNode toSelect = null;
if (keyUpArrow) if (keyUpArrow)
{ {
TreeNode toSelect = null;
if (myIndex == 0) if (myIndex == 0)
{ {
// Select parent (if it exists and it isn't a root) // Select parent
if (parentNode != null && !parentNode.IsRoot) toSelect = parentNode;
toSelect = parentNode;
} }
else else
{ {
// Select previous parent child // Select previous parent child
toSelect = nodeParent.GetChild(myIndex - 1) as TreeNode; toSelect = nodeParent.GetChild(myIndex - 1) as TreeNode;
// Check if is valid and expanded and has any children // Select last child if is valid and expanded and has any children
if (toSelect != null && toSelect.IsExpanded && toSelect.HasAnyVisibleChild) if (toSelect != null && toSelect.IsExpanded && toSelect.HasAnyVisibleChild)
{ {
// Select last child
toSelect = toSelect.GetChild(toSelect.ChildrenCount - 1) as TreeNode; toSelect = toSelect.GetChild(toSelect.ChildrenCount - 1) as TreeNode;
} }
} }
if (toSelect != null)
{
// Select
Select(toSelect);
toSelect.Focus();
}
} }
// Down // Down
else else
{ {
TreeNode toSelect = null;
if (node.IsExpanded && node.HasAnyVisibleChild) if (node.IsExpanded && node.HasAnyVisibleChild)
{ {
// Select the first child // Select the first child
@@ -398,13 +387,14 @@ namespace FlaxEditor.GUI.Tree
else if (myIndex == nodeParent.ChildrenCount - 1) else if (myIndex == nodeParent.ChildrenCount - 1)
{ {
// Select next node after parent // Select next node after parent
if (parentNode != null) while (parentNode != null && toSelect == null)
{ {
int parentIndex = parentNode.IndexInParent; int parentIndex = parentNode.IndexInParent;
if (parentIndex != -1 && parentIndex < parentNode.Parent.ChildrenCount - 1) if (parentIndex != -1 && parentIndex < parentNode.Parent.ChildrenCount - 1)
{ {
toSelect = parentNode.Parent.GetChild(parentIndex + 1) as TreeNode; toSelect = parentNode.Parent.GetChild(parentIndex + 1) as TreeNode;
} }
parentNode = parentNode.Parent as TreeNode;
} }
} }
else else
@@ -412,12 +402,12 @@ namespace FlaxEditor.GUI.Tree
// Select next parent child // Select next parent child
toSelect = nodeParent.GetChild(myIndex + 1) as TreeNode; toSelect = nodeParent.GetChild(myIndex + 1) as TreeNode;
} }
if (toSelect != null) }
{ if (toSelect != null && toSelect.AutoFocus)
// Select {
Select(toSelect); // Select
toSelect.Focus(); Select(toSelect);
} toSelect.Focus();
} }
// Reset time // Reset time
@@ -452,7 +442,7 @@ namespace FlaxEditor.GUI.Tree
if (node.IsCollapsed) if (node.IsCollapsed)
{ {
// Select parent if has and is not a root // Select parent if has and is not a root
if (node.HasParent && node.Parent is TreeNode nodeParentNode && !nodeParentNode.IsRoot) if (node.HasParent && node.Parent is TreeNode nodeParentNode && nodeParentNode.AutoFocus)
{ {
Select(nodeParentNode); Select(nodeParentNode);
nodeParentNode.Focus(); nodeParentNode.Focus();

View File

@@ -20,6 +20,7 @@ namespace FlaxEditor.SceneGraph
protected RootNode() protected RootNode()
: base(null, Guid.NewGuid()) : base(null, Guid.NewGuid())
{ {
_treeNode.AutoFocus = false;
} }
/// <summary> /// <summary>
@@ -29,6 +30,7 @@ namespace FlaxEditor.SceneGraph
protected RootNode(Guid id) protected RootNode(Guid id)
: base(null, id) : base(null, id)
{ {
_treeNode.AutoFocus = false;
} }
/// <summary> /// <summary>