Skip to content

Commit

Permalink
Some TreeView fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rds1983 committed Sep 1, 2024
1 parent 88cf89d commit 13e6bb7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/Myra/Graphics2D/UI/Misc/ITreeViewNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Myra.Graphics2D.UI
{
public interface ITreeViewNode
{
int ChildNodesCount { get; }

TreeViewNode AddSubNode(Widget content);
TreeViewNode GetSubNode(int index);

void RemoveAllSubNodes();
}
}
20 changes: 19 additions & 1 deletion src/Myra/Graphics2D/UI/Misc/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace Myra.Graphics2D.UI
{
public class TreeView : Widget
public class TreeView : Widget, ITreeViewNode
{
private readonly StackPanelLayout _layout = new StackPanelLayout(Orientation.Vertical);
private readonly List<TreeViewNode> _allNodes = new List<TreeViewNode>();
Expand All @@ -25,6 +25,8 @@ public class TreeView : Widget

internal List<TreeViewNode> AllNodes => _allNodes;

public int ChildNodesCount => Children.Count;

private TreeViewNode HoverRow { get; set; }

public TreeViewNode SelectedRow
Expand Down Expand Up @@ -243,8 +245,11 @@ public TreeViewNode AddSubNode(Widget content)
return result;
}

public TreeViewNode GetSubNode(int index) => (TreeViewNode)Children[index];

public void RemoveAllSubNodes()
{
Children.Clear();
_allNodes.Clear();
_selectedRow = null;
HoverRow = null;
Expand Down Expand Up @@ -413,6 +418,19 @@ public void ApplyTreeViewStyle(TreeStyle style)
SelectionHoverBackground = style.SelectionHoverBackground;
}

public TreeViewNode FindNode(Func<TreeViewNode, bool> predicate)
{
foreach (var node in AllNodes)
{
if (predicate(node))
{
return node;
}
}

return null;
}

protected internal override void CopyFrom(Widget w)
{
base.CopyFrom(w);
Expand Down
7 changes: 4 additions & 3 deletions src/Myra/Graphics2D/UI/Misc/TreeViewNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Myra.Graphics2D.UI
{
public class TreeViewNode : ContentControl
public class TreeViewNode : ContentControl, ITreeViewNode
{
private readonly GridLayout _layout = new GridLayout();
private readonly TreeView _topTree;
Expand Down Expand Up @@ -71,7 +71,6 @@ internal TreeViewNode(TreeView topTree, string styleName = Stylesheet.DefaultSty
_layout.RowSpacing = 2;
ChildrenLayout = _layout;


_topTree = topTree;

if (_topTree != null)
Expand Down Expand Up @@ -157,6 +156,7 @@ public TreeViewNode GetSubNode(int index)
public void RemoveSubNode(TreeViewNode subNode)
{
_childNodesStackPanel.Children.Remove(subNode);
_topTree.AllNodes.Remove(subNode);
if (_topTree != null && _topTree.SelectedRow == subNode)
{
_topTree.SelectedRow = null;
Expand All @@ -165,8 +165,9 @@ public void RemoveSubNode(TreeViewNode subNode)

public void RemoveSubNodeAt(int index)
{
var subNode = _childNodesStackPanel.Children[index];
var subNode = (TreeViewNode)_childNodesStackPanel.Children[index];
_childNodesStackPanel.Children.RemoveAt(index);
_topTree.AllNodes.Remove(subNode);
if (_topTree.SelectedRow == subNode)
{
_topTree.SelectedRow = null;
Expand Down

0 comments on commit 13e6bb7

Please sign in to comment.