1 module nodes.slider;
2 
3 import std.range;
4 
5 import fluid;
6 
7 @safe:
8 
9 @("Slider draws a rail and a handle")
10 unittest {
11 
12     auto input = slider!int(
13         .layout!"fill",
14         iota(1, 4)
15     );
16     auto root = sizeLock!testSpace(
17         .sizeLimit(500, 200),
18         nullTheme.derive(
19             rule!AbstractSlider(
20                 Rule.backgroundColor = color("#000"),
21                 Rule.lineColor = color("#f00"),
22             ),
23             rule!SliderHandle(
24                 Rule.backgroundColor = color("#0f0"),
25             ),
26         ),
27         input,
28     );
29 
30     root.drawAndAssert(
31 
32         // Rail
33         input.drawsRectangle(0, 8, 500, 4).ofColor("#000"),
34 
35         // Marks
36         input.drawsLine().from(  8, 12).to(  8, 20).ofWidth(1).ofColor("#f00"),
37         input.drawsLine().from(250, 12).to(250, 20).ofWidth(1).ofColor("#f00"),
38         input.drawsLine().from(492, 12).to(492, 20).ofWidth(1).ofColor("#f00"),
39 
40         // Handle
41         input.handle.drawsRectangle(0, 0, 16, 20).ofColor("#0f0"),
42 
43     );
44 
45 }
46 
47 @("Slider can be changed with mouse movements")
48 unittest {
49 
50     const size = Vector2(500, 200);
51     const rect = Rectangle(0, 0, size.tupleof);
52 
53     auto input = sizeLock!(slider!int)(
54         .sizeLimit(500, 200),
55         iota(1, 4)
56     );
57     auto hover = hoverChain(input);
58     auto root = hover;
59 
60     root.draw();
61 
62     // Default value
63     assert(input.index == 0);
64     assert(input.value == 1);
65 
66     // Press at the center
67     hover.point(center(rect))
68         .then((pointer) {
69 
70             pointer.press;
71 
72             // This should have switched to the second value
73             assert(input.index == 1);
74             assert(input.value == 2);
75 
76             // Move the mouse below the bar
77             return pointer.move(Vector2(0, end(rect).y + 100));
78 
79         })
80         .then((pointer) {
81 
82             // Keep pressing
83             pointer.press(false);
84 
85             // The slider should still be affected
86             assert(input.index == 0);
87             assert(input.value == 1);
88 
89             return pointer.stayIdle;
90 
91         })
92         .then((pointer) {
93 
94             assert(input.index == 0);
95             assert(input.value == 1);
96 
97             // Now the mouse should be released
98             return pointer.move(Vector2(center(rect).x, end(rect).y + 100));
99 
100         })
101         .then((pointer) {
102 
103             // No change now
104             assert(input.index == 0);
105             assert(input.value == 1);
106 
107         })
108         .runWhileDrawing(root);
109 
110     assert(input.value == 1);
111 
112 }
113 
114 @("Slider reacts to input actions")
115 unittest {
116 
117     auto input = slider!string(["One", "Two", "Three"]);
118     assert(input.index == 0);
119     assert(input.value == "One");
120 
121     input.runInputAction!(FluidInputAction.scrollRight);
122     assert(input.index == 1);
123     assert(input.value == "Two");
124 
125 }
126 
127 @("Pressing a slider with keyboard has no effect")
128 unittest {
129 
130     auto input = slider!string(["One", "Two", "Three"]);
131     auto focus = focusChain();
132     auto root = chain(hoverChain(), focus, input);
133     root.draw();
134     input.increment();
135     assert(input.value == "Two");
136 
137     input.actionImpl(focus, 0, inputActionID!(FluidInputAction.press), true);
138     assert(input.value == "Two");
139 
140 }