-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathToolbox.cs
More file actions
96 lines (78 loc) · 2.75 KB
/
Copy pathToolbox.cs
File metadata and controls
96 lines (78 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Collections.Generic;
using System.Linq;
namespace PixUI.Dynamic.Design;
public sealed class Toolbox : View
{
public Toolbox(DesignController designController)
{
_designController = designController;
_treeController.SelectionChanged += OnSelectionChanged;
BuildTreeDataSource();
Child = new Column
{
Children =
{
new TextInput(_searchKey) { Suffix = new Icon(MaterialIcons.Search) },
new TreeView<ToolboxNode>(_treeController, BuildTreeNode, data => data.Children!)
{
AllowDrag = true,
OnAllowDrag = OnAllowDrag,
}
}
};
}
private readonly DesignController _designController;
private readonly State<string> _searchKey = string.Empty;
private readonly TreeController<ToolboxNode> _treeController = new();
private static void BuildTreeNode(TreeNode<ToolboxNode> node)
{
var data = node.Data;
node.Label = new Text(data.IsCatalog ? data.CatalogName! : data.DynamicWidgetMeta!.Name);
node.Icon = data.IsCatalog ? new(MaterialIcons.Folder) : new(data.DynamicWidgetMeta!.Icon);
node.IsLeaf = !data.IsCatalog;
node.IsExpanded = true;
}
private void BuildTreeDataSource()
{
var all = DynamicWidgetManager.GetAll()
.Where(t => t.ShowOnToolbox)
.GroupBy(w => w.Catalog);
var treeList = new List<ToolboxNode>();
foreach (var group in all)
{
var groupIndex = treeList.FindIndex(n => n.CatalogName == group.Key);
if (groupIndex < 0)
{
treeList.Add(new ToolboxNode(group.Key));
groupIndex = treeList.Count - 1;
}
foreach (var meta in group)
{
treeList[groupIndex].Children!.Add(new ToolboxNode(meta));
}
}
_treeController.DataSource = treeList;
}
public void Rebuild() => BuildTreeDataSource();
private static bool OnAllowDrag(TreeNode<ToolboxNode> node) => !node.Data.IsCatalog;
private void OnSelectionChanged()
{
_designController.CurrentToolboxItem = _treeController.FirstSelectedNode?.Data.DynamicWidgetMeta;
}
}
public sealed class ToolboxNode
{
public ToolboxNode(string catalogName)
{
CatalogName = catalogName;
Children = new List<ToolboxNode>();
}
public ToolboxNode(DynamicWidgetMeta widgetMeta)
{
DynamicWidgetMeta = widgetMeta;
}
public string? CatalogName { get; }
public IList<ToolboxNode>? Children { get; }
public DynamicWidgetMeta? DynamicWidgetMeta { get; }
public bool IsCatalog => CatalogName != null;
}