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