1 module rpui.widgets.tree_list_node.widget;
2 
3 import rpui.events;
4 import rpui.primitives;
5 import rpui.math;
6 import rpui.widget;
7 import rpui.widgets.button.widget;
8 import rpui.widgets.tree_list.widget;
9 import rpui.widgets.tree_list_node.renderer;
10 
11 class TreeListNode : Button {
12     @field bool isOpen = true;
13     @field bool allowHide = true;
14 
15     @property TreeList treeList() { return treeList_; }
16     private TreeList treeList_;
17 
18     private float innerHeight;
19     private bool lastDepth = 0;
20     package bool isExpandButtonEnter = false;
21 
22     this(in string style = "TreeListNode") {
23         super(style);
24         textAlign = Align.left;
25         drawChildren = true;
26         renderer = new TreeListNodeRenderer("TreeList");
27     }
28 
29     override void onCreate() {
30         super.onCreate();
31         setTreeList();
32     }
33 
34     private void setTreeList() {
35         if (auto treeList = cast(TreeList) parent) {
36             treeList_ = treeList;
37         } else {
38             auto treeListNode = cast(TreeListNode) parent;
39             treeList_ = treeListNode.treeList;
40         }
41     }
42 
43     override void onRender() {
44         renderer.onRender();
45     }
46 
47     override void onProgress(in ProgressEvent event) {
48         debug assert(treeList !is null);
49 
50         super.onProgress(event);
51 
52         locator.updateAbsolutePosition();
53         position.x = treeList.measure.nodeLeftOffset;
54 
55         innerHeight = size.y;
56 
57         if (!isOpen)
58             return;
59 
60         foreach (Widget widget; children) {
61             auto treeListNode = cast(TreeListNode) widget;
62 
63             if (treeListNode is null)
64                 continue;
65 
66             treeListNode.position.y = innerHeight;
67             innerHeight += treeListNode.innerHeight;
68             treeListNode.onProgress(event);
69         }
70 
71         updateSize();
72     }
73 
74     protected override void updateSize() {
75         overSize.y = innerHeight;
76         size.x = treeList.size.x - treeDepth * 20;
77         size.y = 21;
78     }
79 
80     @property uint treeDepth() {
81         return depth - treeList.depth;
82     }
83 
84     override void onMouseDown(in MouseDownEvent event) {
85         super.onMouseDown(event);
86 
87         if (isExpandButtonEnter) {
88             isOpen = !isOpen;
89         }
90     }
91 }