1 | #![allow (deprecated)]slint::slint!{#[include_path=r#"/input/slint/tests/driver/driverlib/../../cases/elements"# ] |
2 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
3 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-1.1 OR LicenseRef-Slint-commercial |
4 | |
5 | import { Palette } from "std-widgets.slint" ; |
6 | |
7 | export component TestCase { |
8 | width: 300px; |
9 | height: 300px; |
10 | |
11 | in-out property <bool> popup-created; |
12 | in-out property <int> click-count; |
13 | in-out property <int> popup-clicked; |
14 | out property <length> last-underneath-mouse-x: ta.mouse-x; |
15 | out property <length> last-underneath-mouse-y: ta.mouse-y; |
16 | |
17 | context-menu := PopupWindow { |
18 | x: 10px; |
19 | y: 10px; |
20 | width: parent.width - 20px; |
21 | height: parent.height - 20px; |
22 | |
23 | Rectangle { |
24 | border-width: 2px; |
25 | border-color: Palette.alternate-background; |
26 | } |
27 | |
28 | Text { |
29 | text: "I'm a context menu." ; |
30 | } |
31 | init => { |
32 | root.popup-created = true; |
33 | } |
34 | |
35 | TouchArea { |
36 | width: 7px; x: 0px; |
37 | clicked => { |
38 | root.popup-clicked += 1; |
39 | } |
40 | } |
41 | } |
42 | |
43 | ta := TouchArea { |
44 | clicked => { |
45 | root.click-count += 1; |
46 | } |
47 | pointer-event(event) => { |
48 | if (event.kind == PointerEventKind.down && event.button == PointerEventButton.right) { |
49 | context-menu.show(); |
50 | } |
51 | } |
52 | } |
53 | } |
54 | /* |
55 | |
56 | ```rust |
57 | use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition}; |
58 | |
59 | let instance = TestCase::new().unwrap(); |
60 | |
61 | assert_eq!(instance.get_click_count(), 0); |
62 | assert_eq!(instance.get_popup_created(), false); |
63 | |
64 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(15.0, 15.0) }); |
65 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
66 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
67 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(15.0, 15.0), button: PointerEventButton::Right }); |
68 | assert_eq!(instance.get_popup_created(), true); |
69 | |
70 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(15.0, 15.0), button: PointerEventButton::Right }); |
71 | |
72 | // Popup is still visible, as it gets the move event instead of the underlying touch area |
73 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(11.0, 11.0) }); |
74 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
75 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
76 | |
77 | slint_testing::send_mouse_click(&instance, 15., 15.); |
78 | assert_eq!(instance.get_click_count(), 0); |
79 | // Subsequent click to verify that it was closed |
80 | slint_testing::send_mouse_click(&instance, 15., 15.); |
81 | assert_eq!(instance.get_click_count(), 1); |
82 | assert_eq!(instance.get_popup_clicked(), 1); |
83 | |
84 | ``` |
85 | |
86 | ```cpp |
87 | auto handle = TestCase::create(); |
88 | const TestCase &instance = *handle; |
89 | |
90 | assert_eq(instance.get_click_count(), 0); |
91 | assert_eq(instance.get_popup_created(), false); |
92 | |
93 | instance.window().dispatch_pointer_move_event(slint::LogicalPosition({15.0, 15.0})); |
94 | assert_eq(instance.get_last_underneath_mouse_x(), 15.); |
95 | assert_eq(instance.get_last_underneath_mouse_y(), 15.); |
96 | instance.window().dispatch_pointer_press_event(slint::LogicalPosition({15.0, 15.0}), slint::PointerEventButton::Right); |
97 | assert_eq(instance.get_popup_created(), true); |
98 | |
99 | instance.window().dispatch_pointer_release_event(slint::LogicalPosition({15.0, 15.0}), slint::PointerEventButton::Right); |
100 | |
101 | // Popup is still visible, as it gets the move event instead of the underlying touch area |
102 | instance.window().dispatch_pointer_move_event(slint::LogicalPosition({11.0, 11.0})); |
103 | assert_eq(instance.get_last_underneath_mouse_x(), 15.); |
104 | assert_eq(instance.get_last_underneath_mouse_y(), 15.); |
105 | |
106 | slint_testing::send_mouse_click(&instance, 15., 15.); |
107 | assert_eq(instance.get_click_count(), 0); |
108 | // Subsequent click to verify that it was closed |
109 | slint_testing::send_mouse_click(&instance, 15., 15.); |
110 | assert_eq(instance.get_click_count(), 1); |
111 | assert_eq(instance.get_popup_clicked(), 1); |
112 | ``` |
113 | |
114 | */ |
115 | } |
116 | |
117 | #[test ] fn t_0() -> std::result::Result<(), std::boxed::Box<dyn std::error::Error>> { |
118 | use i_slint_backend_testing as slint_testing; |
119 | slint_testing::init(); |
120 | use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition}; |
121 | |
122 | let instance = TestCase::new().unwrap(); |
123 | |
124 | assert_eq!(instance.get_click_count(), 0); |
125 | assert_eq!(instance.get_popup_created(), false); |
126 | |
127 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(15.0, 15.0) }); |
128 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
129 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
130 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(15.0, 15.0), button: PointerEventButton::Right }); |
131 | assert_eq!(instance.get_popup_created(), true); |
132 | |
133 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(15.0, 15.0), button: PointerEventButton::Right }); |
134 | |
135 | // Popup is still visible, as it gets the move event instead of the underlying touch area |
136 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(11.0, 11.0) }); |
137 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
138 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
139 | |
140 | slint_testing::send_mouse_click(&instance, 15., 15.); |
141 | assert_eq!(instance.get_click_count(), 0); |
142 | // Subsequent click to verify that it was closed |
143 | slint_testing::send_mouse_click(&instance, 15., 15.); |
144 | assert_eq!(instance.get_click_count(), 1); |
145 | assert_eq!(instance.get_popup_clicked(), 1); |
146 | |
147 | Ok(()) |
148 | } |