1 module actions.branch_action;
2 
3 import fluid;
4 import fluid.future.branch_action;
5 
6 @safe:
7 
8 class NodeCountAction : BranchAction {
9 
10     int nodeCount;
11     int starts;
12     int stops;
13 
14     override void started() {
15         nodeCount = 0;
16         starts++;
17     }
18 
19     override void beforeDraw(Node, Rectangle) {
20         nodeCount++;
21         assert(starts > stops);
22     }
23 
24     override void stopped() {
25         stops++;
26         assert(stops == starts);
27     }
28 
29 }
30 
31 class NodeCounter : Space {
32 
33     NodeCountAction nodeCountAction;
34 
35     this(Node[] nodes...) {
36         super(nodes);
37         nodeCountAction = new NodeCountAction;
38     }
39 
40     int nodeCount() const {
41         return nodeCountAction.nodeCount;
42     }
43 
44     int runs() const {
45         return nodeCountAction.stops;
46     }
47 
48     override void drawImpl(Rectangle outer, Rectangle inner) {
49         const originalRuns = runs;
50         assert(nodeCountAction.starts == nodeCountAction.stops);
51         {
52             auto frame = startBranchAction(nodeCountAction);
53             assert(nodeCountAction.starts == nodeCountAction.stops + 1);
54             super.drawImpl(outer, inner);
55         }
56         assert(nodeCountAction.starts == nodeCountAction.stops);
57         assert(runs == originalRuns + 1);
58     }
59 
60 }
61 
62 alias nodeCounter = nodeBuilder!NodeCounter;
63 
64 @("startBranchAction launches a scope-bound action when drawn")
65 unittest {
66 
67     auto counter = nodeCounter(
68         vspace(
69             hspace(),
70         ),
71         hspace(),
72     );
73     auto root = vspace(
74         counter,
75         vspace()
76     );
77     assert(counter.nodeCount == 0);
78     assert(counter.runs == 0);
79 
80     root.draw();
81     assert(counter.nodeCount == 3);
82     assert(counter.runs == 1);
83 
84     root.draw();
85     assert(counter.runs == 2);
86 
87     counter.hide();
88     root.draw();
89     assert(counter.runs == 2);
90 
91     counter.show();
92     root.draw();
93     assert(counter.runs == 3);
94 
95 }