Fixes for regression in tree UI layout after recent refactor

This commit is contained in:
Wojciech Figat
2021-12-07 14:58:54 +01:00
parent ea71e8cbe6
commit 1cc26c871d
5 changed files with 76 additions and 72 deletions

View File

@@ -333,30 +333,6 @@ namespace FlaxEditor.GUI.Tree
}
}
/// <summary>
/// Updates the tree size.
/// </summary>
public void UpdateSize()
{
if (!_autoSize)
return;
// Use max of parent clint area width and root node width
float width = 0;
if (HasParent)
width = Parent.GetClientArea().Width;
var rightBottom = Vector2.Zero;
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is TreeNode node && node.Visible)
{
width = Mathf.Max(width, node.MinimumWidth);
rightBottom = Vector2.Max(rightBottom, node.BottomRight);
}
}
Size = new Vector2(width + _margin.Width, rightBottom.Y + _margin.Bottom);
}
/// <inheritdoc />
public override void Update(float deltaTime)
{
@@ -521,25 +497,43 @@ namespace FlaxEditor.GUI.Tree
base.OnGotFocus();
}
/// <inheritdoc />
public override void OnChildResized(Control control)
{
UpdateSize();
base.OnChildResized(control);
}
/// <inheritdoc />
public override void OnParentResized()
{
UpdateSize();
PerformLayout();
base.OnParentResized();
}
/// <inheritdoc />
protected override void PerformLayoutBeforeChildren()
{
if (_autoSize)
{
// Use max of parent clint area width and root node width
var parent = Parent;
var width = parent != null ? Mathf.Max(parent.GetClientArea().Width, 0) : 0.0f;
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is TreeNode node && node.Visible)
width = Mathf.Max(width, node.MinimumWidth);
}
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is TreeNode node && node.Visible)
node.Width = width;
}
Width = width + _margin.Width;
}
base.PerformLayoutBeforeChildren();
}
/// <inheritdoc />
protected override void PerformLayoutAfterChildren()
{
base.PerformLayoutAfterChildren();
// Arrange children
float y = _margin.Top;
for (int i = 0; i < _children.Count; i++)
@@ -551,7 +545,17 @@ namespace FlaxEditor.GUI.Tree
}
}
UpdateSize();
if (_autoSize)
{
// Update height based on the nodes
var bottom = 0.0f;
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is TreeNode node && node.Visible)
bottom = Mathf.Max(bottom, node.Bottom);
}
Height = bottom + _margin.Bottom;
}
}
}
}

View File

@@ -325,23 +325,18 @@ namespace FlaxEditor.GUI.Tree
ExpandAllParents(noAnimation);
// Change state
if (_opened && _animationProgress >= 1.0f)
return;
bool prevState = _opened;
_opened = true;
if (prevState != _opened)
_animationProgress = 1.0f - _animationProgress;
if (noAnimation)
{
// Speed up an animation
_animationProgress = 1.0f;
}
else if (prevState != _opened)
_animationProgress = 1.0f - _animationProgress;
// Update
OnExpandedChanged();
if (HasParent)
Parent.PerformLayout();
else
PerformLayout();
OnExpandAnimationChanged();
}
/// <summary>
@@ -351,23 +346,18 @@ namespace FlaxEditor.GUI.Tree
public void Collapse(bool noAnimation = false)
{
// Change state
if (!_opened && _animationProgress >= 1.0f)
return;
bool prevState = _opened;
_opened = false;
if (prevState != _opened)
_animationProgress = 1.0f - _animationProgress;
if (noAnimation)
{
// Speed up an animation
_animationProgress = 1.0f;
}
else if (prevState != _opened)
_animationProgress = 1.0f - _animationProgress;
// Update
OnExpandedChanged();
if (HasParent)
Parent.PerformLayout();
else
PerformLayout();
OnExpandAnimationChanged();
}
/// <summary>
@@ -433,7 +423,7 @@ namespace FlaxEditor.GUI.Tree
if (_animationProgress < 1.0f)
{
_animationProgress = 1.0f;
PerformLayout();
OnExpandAnimationChanged();
}
}
@@ -521,6 +511,19 @@ namespace FlaxEditor.GUI.Tree
{
}
/// <summary>
/// Called when expand/collapse animation progress changes.
/// </summary>
protected virtual void OnExpandAnimationChanged()
{
if (ParentTree != null)
ParentTree.PerformLayout();
else if (Parent != null)
Parent.PerformLayout();
else
PerformLayout();
}
/// <summary>
/// Tests the header hit.
/// </summary>
@@ -595,7 +598,7 @@ namespace FlaxEditor.GUI.Tree
}
// Arrange controls
PerformLayout();
OnExpandAnimationChanged();
}
// Check for long press
@@ -894,7 +897,6 @@ namespace FlaxEditor.GUI.Tree
return;
PerformLayout();
ParentTree.UpdateSize();
base.OnChildResized(control);
}
@@ -1022,9 +1024,14 @@ namespace FlaxEditor.GUI.Tree
if (!wasLocked)
LockChildrenRecursive();
// Auto-size tree nodes to match the parent size
var parent = Parent;
var width = parent is TreeNode ? parent.Width : Width;
// Optimize layout logic if node is collapsed
if (_opened || _animationProgress < 1.0f)
{
Width = width;
PerformLayoutBeforeChildren();
for (int i = 0; i < _children.Count; i++)
_children[i].PerformLayout(true);
@@ -1035,7 +1042,7 @@ namespace FlaxEditor.GUI.Tree
// TODO: perform layout for any non-TreeNode controls
_cachedHeight = _headerHeight;
_cachedTextColor = CacheTextColor();
Size = new Vector2(Parent?.Width ?? Width, _headerHeight);
Size = new Vector2(width, _headerHeight);
}
if (!wasLocked)
@@ -1070,8 +1077,6 @@ namespace FlaxEditor.GUI.Tree
if (_opened || _animationProgress < 1.0f)
{
y -= _cachedHeight * (_opened ? 1.0f - _animationProgress : _animationProgress);
// Arrange children
for (int i = 0; i < _children.Count; i++)
{
if (_children[i] is TreeNode node && node.Visible)
@@ -1085,21 +1090,15 @@ namespace FlaxEditor.GUI.Tree
}
}
// Cache data
_cachedHeight = height;
_cachedTextColor = CacheTextColor();
// Set bounds
Size = new Vector2(Parent?.Width ?? Width, Mathf.Max(_headerHeight, y));
Height = Mathf.Max(_headerHeight, y);
}
/// <inheritdoc />
protected override void OnParentChangedInternal()
{
// Clear cached tree
_tree = null;
if (Parent != null)
Width = Parent.Width;
base.OnParentChangedInternal();
}

View File

@@ -412,11 +412,8 @@ namespace FlaxEditor.Modules
// Add to the tree
var rootNode = Root.TreeNode;
rootNode.IsLayoutLocked = true;
//
sceneNode.ParentNode = Root;
rootNode.SortChildren();
//
treeNode.UnlockChildrenRecursive();
rootNode.IsLayoutLocked = false;
rootNode.Parent.PerformLayout();

View File

@@ -350,7 +350,7 @@ namespace FlaxEditor.Modules
{
// Perform layout
var windowGUI = window.GUI;
windowGUI.UnlockChildrenRecursive();
windowGUI.IsLayoutLocked = false;
windowGUI.PerformLayout();
// Show
@@ -379,6 +379,10 @@ namespace FlaxEditor.Modules
Editor.LogWarning(ex);
return false;
}
finally
{
masterPanel.PerformLayout();
}
return true;
}

View File

@@ -803,7 +803,7 @@ namespace FlaxEditor.Windows
NavigationClearHistory();
// Update UI layout
UnlockChildrenRecursive();
_isLayoutLocked = false;
PerformLayout();
// Load last viewed folder