1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial |
3 | |
4 | TestCase := Window { |
5 | |
6 | |
7 | width: 500phx; |
8 | height: 500phx; |
9 | no-frame: false; |
10 | |
11 | f := Flickable { |
12 | x: 10phx; |
13 | y: 10phx; |
14 | width: parent.width - 20phx; |
15 | height: parent.height - 20phx; |
16 | viewport_width: 2100phx; |
17 | viewport_height: 2100phx; |
18 | |
19 | flicked => { |
20 | root.flicked += viewport_x/1phx * 100000 + viewport_y/1phx; |
21 | } |
22 | |
23 | inner_ta := TouchArea { |
24 | x: 150phx; |
25 | y: 150phx; |
26 | width: 50phx; |
27 | height: 50phx; |
28 | Rectangle { |
29 | background: parent.pressed ? blue : parent.has_hover ? green : red; |
30 | } |
31 | clicked => { |
32 | root.clicked = mouse_x/1phx * 100000 + mouse_y/1phx; |
33 | } |
34 | } |
35 | |
36 | } |
37 | |
38 | property<length> offset_x: -f.viewport_x; |
39 | property<length> offset_y: -f.viewport_y; |
40 | property<bool> inner_ta_pressed: inner_ta.pressed; |
41 | property<bool> inner_ta_has_hover: inner_ta.has_hover; |
42 | property<int> clicked; |
43 | property <int> flicked; |
44 | } |
45 | |
46 | /* |
47 | |
48 | ```rust |
49 | // Test that basic scrolling works, and that releasing the mouse animates |
50 | use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition}; |
51 | let instance = TestCase::new().unwrap(); |
52 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(300.0, 100.0) }); |
53 | slint_testing::mock_elapsed_time(5000); |
54 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(300.0, 100.0), button: PointerEventButton::Left }); |
55 | assert_eq!(instance.get_offset_x(), 0.); |
56 | assert_eq!(instance.get_offset_y(), 0.); |
57 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(200.0, 50.0) }); |
58 | assert_eq!(instance.get_offset_x(), 100.); |
59 | assert_eq!(instance.get_offset_y(), 50.); |
60 | slint_testing::mock_elapsed_time(200); |
61 | assert_eq!(instance.get_offset_x(), 100.); |
62 | assert_eq!(instance.get_offset_y(), 50.); |
63 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 50.0) }); |
64 | assert_eq!(instance.get_offset_x(), 200.); |
65 | assert_eq!(instance.get_offset_y(), 50.); |
66 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 50.0), button: PointerEventButton::Left }); |
67 | // Start of the animation, the position is still unchanged |
68 | assert_eq!(instance.get_offset_x(), 200.); |
69 | assert_eq!(instance.get_offset_y(), 50.); |
70 | slint_testing::mock_elapsed_time(50); |
71 | // middle of the animation |
72 | assert!(instance.get_offset_x() > 210.); |
73 | assert!(instance.get_offset_y() > 60.); |
74 | assert!(instance.get_offset_x() < 290.); |
75 | assert!(instance.get_offset_y() < 70.); |
76 | |
77 | slint_testing::mock_elapsed_time(200); |
78 | // end of the animation |
79 | assert_eq!(instance.get_offset_x(), 450.); |
80 | assert_eq!(instance.get_offset_y(), 112.5); |
81 | slint_testing::mock_elapsed_time(50); |
82 | assert_eq!(instance.get_offset_x(), 450.); |
83 | assert_eq!(instance.get_offset_y(), 112.5); |
84 | |
85 | assert!(!instance.get_inner_ta_pressed()); |
86 | assert!(!instance.get_inner_ta_has_hover()); |
87 | assert_eq!(instance.get_clicked(), 0); |
88 | ``` |
89 | |
90 | ```rust |
91 | // Test interaction with inner mouse area |
92 | use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition}; |
93 | let instance = TestCase::new().unwrap(); |
94 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(175.0, 175.0) }); |
95 | assert!(!instance.get_inner_ta_pressed()); |
96 | assert!(instance.get_inner_ta_has_hover()); |
97 | assert_eq!(instance.get_clicked(), 0); |
98 | slint_testing::mock_elapsed_time(5000); |
99 | assert!(!instance.get_inner_ta_pressed()); |
100 | assert!(instance.get_inner_ta_has_hover()); |
101 | assert_eq!(instance.get_clicked(), 0); |
102 | |
103 | // Start a press |
104 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left }); |
105 | //assert!(!instance.get_inner_ta_pressed()); |
106 | assert!(instance.get_inner_ta_has_hover()); |
107 | assert_eq!(instance.get_clicked(), 0); |
108 | // Release almost immediately |
109 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(178.0, 173.0), button: PointerEventButton::Left }); |
110 | assert!(!instance.get_inner_ta_pressed()); |
111 | assert!(instance.get_inner_ta_has_hover()); |
112 | assert_eq!(instance.get_clicked(), 18_00013); |
113 | assert_eq!(instance.get_offset_x(), 0.); |
114 | assert_eq!(instance.get_offset_y(), 0.); |
115 | |
116 | instance.set_clicked(-1); |
117 | |
118 | slint_testing::mock_elapsed_time(1000); |
119 | |
120 | // - Press, move a triny, then release quickly: should click |
121 | // Start a press |
122 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(165.0, 175.0), button: PointerEventButton::Left }); |
123 | assert!(!instance.get_inner_ta_pressed()); |
124 | assert!(instance.get_inner_ta_has_hover()); |
125 | assert_eq!(instance.get_clicked(), -1); |
126 | // make a small move |
127 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(166.0, 174.0) }); |
128 | assert!(!instance.get_inner_ta_pressed()); |
129 | assert!(instance.get_inner_ta_has_hover()); |
130 | slint_testing::mock_elapsed_time(30); |
131 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(167.0, 175.0) }); |
132 | assert!(!instance.get_inner_ta_pressed()); |
133 | // and release |
134 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(165.0, 174.0), button: PointerEventButton::Left }); |
135 | assert!(!instance.get_inner_ta_pressed()); |
136 | assert!(instance.get_inner_ta_has_hover()); |
137 | assert_eq!(instance.get_clicked(), 5_00014); |
138 | assert_eq!(instance.get_offset_x(), 0.); |
139 | assert_eq!(instance.get_offset_y(), 0.); |
140 | |
141 | instance.set_clicked(-1); |
142 | |
143 | // Start a press |
144 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left }); |
145 | assert!(!instance.get_inner_ta_pressed()); |
146 | assert!(instance.get_inner_ta_has_hover()); |
147 | assert_eq!(instance.get_clicked(), -1); |
148 | assert_eq!(instance.get_offset_x(), 0.); |
149 | assert_eq!(instance.get_offset_y(), 0.); |
150 | |
151 | //wait a delay |
152 | slint_testing::mock_elapsed_time(300); |
153 | assert!(instance.get_inner_ta_pressed()); // only now we are pressed |
154 | assert!(instance.get_inner_ta_has_hover()); |
155 | assert_eq!(instance.get_clicked(), -1); |
156 | assert_eq!(instance.get_offset_x(), 0.); |
157 | assert_eq!(instance.get_offset_y(), 0.); |
158 | |
159 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 120.0) }); |
160 | assert!(!instance.get_inner_ta_pressed()); // We should no longer be pressed |
161 | // no hover when the flickable is flicking |
162 | assert!(!instance.get_inner_ta_has_hover()); |
163 | assert_eq!(instance.get_clicked(), -1); // not clicked |
164 | assert_eq!(instance.get_offset_x(), 75.); |
165 | assert_eq!(instance.get_offset_y(), 55.); |
166 | |
167 | slint_testing::mock_elapsed_time(10000); |
168 | |
169 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 120.0), button: PointerEventButton::Left }); |
170 | assert!(!instance.get_inner_ta_pressed()); |
171 | assert!(instance.get_inner_ta_has_hover()); |
172 | assert_eq!(instance.get_clicked(), -1); // not clicked |
173 | assert_eq!(instance.get_offset_x(), 75.); |
174 | assert_eq!(instance.get_offset_y(), 55.); |
175 | |
176 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(200.0, 50.0) }); |
177 | slint_testing::mock_elapsed_time(1000); |
178 | |
179 | assert!(!instance.get_inner_ta_pressed()); |
180 | assert!(!instance.get_inner_ta_has_hover()); |
181 | assert_eq!(instance.get_clicked(), -1); |
182 | assert!((instance.get_offset_x() - 75.).abs() < 5.); // only small animation on release |
183 | assert!((instance.get_offset_y() - 55.).abs() < 5.); |
184 | |
185 | ``` |
186 | |
187 | ```rust |
188 | // Test wheel events |
189 | use slint::{LogicalPosition, platform::{WindowEvent, Key} }; |
190 | let instance = TestCase::new().unwrap(); |
191 | instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(175.0, 175.0), delta_x: -30.0, delta_y: -50.0 }); |
192 | assert_eq!(instance.get_offset_x(), 30.); |
193 | assert_eq!(instance.get_offset_y(), 50.); |
194 | |
195 | // When shift is pressed, it invert the direction |
196 | // (this test don't work on macos because on macos the backend does the inversion, not the core lib) |
197 | if !cfg!(target_os = "macos") { |
198 | slint_testing::send_keyboard_char(&instance, Key::Shift.into(), true); |
199 | instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(175.0, 175.0), delta_x: 15.0, delta_y: -60.0 }); |
200 | slint_testing::send_keyboard_char(&instance, Key::Shift.into(), false); |
201 | assert_eq!(instance.get_offset_x(), 30. + 60.); |
202 | assert_eq!(instance.get_offset_y(), 50. - 15.); |
203 | } |
204 | ``` |
205 | |
206 | ```rust |
207 | // Test flicked-Callback behaviour |
208 | use slint::{LogicalPosition, platform::{WindowEvent, PointerEventButton} }; |
209 | let instance = TestCase::new().unwrap(); |
210 | assert_eq!(instance.get_flicked(), 0); |
211 | |
212 | // test scrolling behaviour |
213 | instance.window().dispatch_event(WindowEvent::PointerScrolled { position: LogicalPosition::new(175.0, 175.0), delta_x: -30.0, delta_y: -50.0 }); |
214 | dbg!(instance.get_flicked()); |
215 | assert_eq!(instance.get_flicked(), -3000050); //flicked got called after scrolling |
216 | instance.set_flicked(0); |
217 | |
218 | // test dragging bevaviour |
219 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(175.0, 175.0), button: PointerEventButton::Left }); |
220 | slint_testing::mock_elapsed_time(300); |
221 | assert_eq!(instance.get_flicked(), 0); //flicked didn't get called by just pressing |
222 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(100.0, 120.0) }); |
223 | slint_testing::mock_elapsed_time(10000); |
224 | assert_eq!(instance.get_flicked(), -10500105); //flicked got called during drag |
225 | instance.set_flicked(0); |
226 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(100.0, 120.0), button: PointerEventButton::Left }); |
227 | assert_eq!(instance.get_flicked(), -10500105); //flicked got called after drag |
228 | instance.set_flicked(0); |
229 | |
230 | ``` |
231 | */ |
232 | |