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 | import { Palette } from "std-widgets.slint" ; |
5 | |
6 | export component TestCase { |
7 | width: 300px; |
8 | height: 300px; |
9 | |
10 | in-out property <bool> popup-created; |
11 | in-out property <int> click-count; |
12 | in-out property <int> popup-clicked; |
13 | out property <length> last-underneath-mouse-x: ta.mouse-x; |
14 | out property <length> last-underneath-mouse-y: ta.mouse-y; |
15 | |
16 | context-menu := PopupWindow { |
17 | x: 10px; |
18 | y: 10px; |
19 | width: parent.width - 20px; |
20 | height: parent.height - 20px; |
21 | |
22 | Rectangle { |
23 | border-width: 2px; |
24 | border-color: Palette.alternate-background; |
25 | } |
26 | |
27 | Text { |
28 | text: "I'm a context menu." ; |
29 | } |
30 | init => { |
31 | root.popup-created = true; |
32 | } |
33 | |
34 | TouchArea { |
35 | width: 7px; x: 0px; |
36 | clicked => { |
37 | root.popup-clicked += 1; |
38 | } |
39 | } |
40 | } |
41 | |
42 | ta := TouchArea { |
43 | clicked => { |
44 | root.click-count += 1; |
45 | } |
46 | pointer-event(event) => { |
47 | if (event.kind == PointerEventKind.down && event.button == PointerEventButton.right) { |
48 | context-menu.show(); |
49 | } |
50 | } |
51 | } |
52 | } |
53 | /* |
54 | |
55 | ```rust |
56 | use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition}; |
57 | |
58 | let instance = TestCase::new().unwrap(); |
59 | |
60 | assert_eq!(instance.get_click_count(), 0); |
61 | assert_eq!(instance.get_popup_created(), false); |
62 | |
63 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(15.0, 15.0) }); |
64 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
65 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
66 | instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(15.0, 15.0), button: PointerEventButton::Right }); |
67 | assert_eq!(instance.get_popup_created(), true); |
68 | |
69 | instance.window().dispatch_event(WindowEvent::PointerReleased { position: LogicalPosition::new(15.0, 15.0), button: PointerEventButton::Right }); |
70 | |
71 | // Popup is still visible, as it gets the move event instead of the underlying touch area |
72 | instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(11.0, 11.0) }); |
73 | assert_eq!(instance.get_last_underneath_mouse_x(), 15.); |
74 | assert_eq!(instance.get_last_underneath_mouse_y(), 15.); |
75 | |
76 | slint_testing::send_mouse_click(&instance, 15., 15.); |
77 | assert_eq!(instance.get_click_count(), 0); |
78 | // Subsequent click to verify that it was closed |
79 | slint_testing::send_mouse_click(&instance, 15., 15.); |
80 | assert_eq!(instance.get_click_count(), 1); |
81 | assert_eq!(instance.get_popup_clicked(), 1); |
82 | |
83 | ``` |
84 | |
85 | ```cpp |
86 | auto handle = TestCase::create(); |
87 | const TestCase &instance = *handle; |
88 | |
89 | assert_eq(instance.get_click_count(), 0); |
90 | assert_eq(instance.get_popup_created(), false); |
91 | |
92 | instance.window().dispatch_pointer_move_event(slint::LogicalPosition({15.0, 15.0})); |
93 | assert_eq(instance.get_last_underneath_mouse_x(), 15.); |
94 | assert_eq(instance.get_last_underneath_mouse_y(), 15.); |
95 | instance.window().dispatch_pointer_press_event(slint::LogicalPosition({15.0, 15.0}), slint::PointerEventButton::Right); |
96 | assert_eq(instance.get_popup_created(), true); |
97 | |
98 | instance.window().dispatch_pointer_release_event(slint::LogicalPosition({15.0, 15.0}), slint::PointerEventButton::Right); |
99 | |
100 | // Popup is still visible, as it gets the move event instead of the underlying touch area |
101 | instance.window().dispatch_pointer_move_event(slint::LogicalPosition({11.0, 11.0})); |
102 | assert_eq(instance.get_last_underneath_mouse_x(), 15.); |
103 | assert_eq(instance.get_last_underneath_mouse_y(), 15.); |
104 | |
105 | slint_testing::send_mouse_click(&instance, 15., 15.); |
106 | assert_eq(instance.get_click_count(), 0); |
107 | // Subsequent click to verify that it was closed |
108 | slint_testing::send_mouse_click(&instance, 15., 15.); |
109 | assert_eq(instance.get_click_count(), 1); |
110 | assert_eq(instance.get_popup_clicked(), 1); |
111 | ``` |
112 | |
113 | */ |
114 | |