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
5import { Palette } from "std-widgets.slint";
6
7export 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
57use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition};
58
59let instance = TestCase::new().unwrap();
60
61assert_eq!(instance.get_click_count(), 0);
62assert_eq!(instance.get_popup_created(), false);
63
64instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(15.0, 15.0) });
65assert_eq!(instance.get_last_underneath_mouse_x(), 15.);
66assert_eq!(instance.get_last_underneath_mouse_y(), 15.);
67instance.window().dispatch_event(WindowEvent::PointerPressed { position: LogicalPosition::new(15.0, 15.0), button: PointerEventButton::Right });
68assert_eq!(instance.get_popup_created(), true);
69
70instance.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
73instance.window().dispatch_event(WindowEvent::PointerMoved { position: LogicalPosition::new(11.0, 11.0) });
74assert_eq!(instance.get_last_underneath_mouse_x(), 15.);
75assert_eq!(instance.get_last_underneath_mouse_y(), 15.);
76
77slint_testing::send_mouse_click(&instance, 15., 15.);
78assert_eq!(instance.get_click_count(), 0);
79// Subsequent click to verify that it was closed
80slint_testing::send_mouse_click(&instance, 15., 15.);
81assert_eq!(instance.get_click_count(), 1);
82assert_eq!(instance.get_popup_clicked(), 1);
83
84```
85
86```cpp
87auto handle = TestCase::create();
88const TestCase &instance = *handle;
89
90assert_eq(instance.get_click_count(), 0);
91assert_eq(instance.get_popup_created(), false);
92
93instance.window().dispatch_pointer_move_event(slint::LogicalPosition({15.0, 15.0}));
94assert_eq(instance.get_last_underneath_mouse_x(), 15.);
95assert_eq(instance.get_last_underneath_mouse_y(), 15.);
96instance.window().dispatch_pointer_press_event(slint::LogicalPosition({15.0, 15.0}), slint::PointerEventButton::Right);
97assert_eq(instance.get_popup_created(), true);
98
99instance.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
102instance.window().dispatch_pointer_move_event(slint::LogicalPosition({11.0, 11.0}));
103assert_eq(instance.get_last_underneath_mouse_x(), 15.);
104assert_eq(instance.get_last_underneath_mouse_y(), 15.);
105
106slint_testing::send_mouse_click(&instance, 15., 15.);
107assert_eq(instance.get_click_count(), 0);
108// Subsequent click to verify that it was closed
109slint_testing::send_mouse_click(&instance, 15., 15.);
110assert_eq(instance.get_click_count(), 1);
111assert_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}