| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | import { Button } from "std-widgets.slint" ; |
| 5 | |
| 6 | component PopupContainer inherits Rectangle { |
| 7 | width: 400px; |
| 8 | height: 300px; |
| 9 | |
| 10 | in-out property <int> popup_click_count; |
| 11 | |
| 12 | callback first_reached; |
| 13 | callback second_reached; |
| 14 | |
| 15 | property <length> popup_x; |
| 16 | property <bool> first_sent: false; |
| 17 | property <bool> second_sent: false; |
| 18 | |
| 19 | public function show(second: bool) { |
| 20 | popup_x = !second ? 0px : 200px; |
| 21 | popup.show(); |
| 22 | } |
| 23 | |
| 24 | public function close() { |
| 25 | popup.close(); |
| 26 | } |
| 27 | |
| 28 | popup := PopupWindow { |
| 29 | x: popup_x; |
| 30 | y: 100px; |
| 31 | width: 200px; |
| 32 | height: 200px; |
| 33 | close-policy: no-auto-close; |
| 34 | |
| 35 | Rectangle { |
| 36 | background: red; |
| 37 | } |
| 38 | |
| 39 | TouchArea { |
| 40 | clicked => { |
| 41 | popup_click_count += 1; |
| 42 | |
| 43 | if (popup_x == 0px) { |
| 44 | if (!first_sent) { |
| 45 | first_reached(); |
| 46 | first_sent = true; |
| 47 | } |
| 48 | } else { |
| 49 | if (!second_sent ) { |
| 50 | second_reached(); |
| 51 | second_sent = true; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | export component TestCase inherits Window { |
| 60 | width: 400px; |
| 61 | height: 400px; |
| 62 | |
| 63 | in-out property <int> popup_click_count; |
| 64 | |
| 65 | VerticalLayout { |
| 66 | alignment: start; |
| 67 | |
| 68 | HorizontalLayout { |
| 69 | Button { |
| 70 | text: "Open" ; |
| 71 | clicked => { |
| 72 | cnt1.show(false); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | cnt1 := PopupContainer { |
| 79 | x: 0px; |
| 80 | y: 100px; |
| 81 | popup_click_count <=> popup_click_count; |
| 82 | |
| 83 | first_reached => { |
| 84 | cnt1.show(true); |
| 85 | } |
| 86 | |
| 87 | second_reached => { |
| 88 | cnt2.show(false); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | cnt2 := PopupContainer { |
| 93 | x: 0px; |
| 94 | y: 100px; |
| 95 | popup_click_count <=> popup_click_count; |
| 96 | |
| 97 | first_reached => { |
| 98 | cnt2.close(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | |
| 105 | ```rust |
| 106 | #[allow(unused)] |
| 107 | use slint::{platform::WindowEvent, platform::PointerEventButton, LogicalPosition}; |
| 108 | let instance = TestCase::new().unwrap(); |
| 109 | |
| 110 | // open on the left |
| 111 | slint_testing::send_mouse_click(&instance, 20., 10.); |
| 112 | |
| 113 | // use left popup to open on the right |
| 114 | slint_testing::send_mouse_click(&instance, 10., 210.); |
| 115 | assert_eq!(instance.get_popup_click_count(), 1); |
| 116 | |
| 117 | // no longer open on the left |
| 118 | slint_testing::send_mouse_click(&instance, 10., 210.); |
| 119 | assert_eq!(instance.get_popup_click_count(), 1); |
| 120 | |
| 121 | // popup is now open on the right |
| 122 | // open a second popup on the left in the second container |
| 123 | slint_testing::send_mouse_click(&instance, 210., 210.); |
| 124 | assert_eq!(instance.get_popup_click_count(), 2); |
| 125 | |
| 126 | // second popup is still open on the left |
| 127 | // close the second popup |
| 128 | slint_testing::send_mouse_click(&instance, 10., 210.); |
| 129 | assert_eq!(instance.get_popup_click_count(), 3); |
| 130 | |
| 131 | // first popup is still open as multiple of the same "PopupWindow" across different component instances is fine |
| 132 | slint_testing::send_mouse_click(&instance, 210., 210.); |
| 133 | assert_eq!(instance.get_popup_click_count(), 4); |
| 134 | ``` |
| 135 | |
| 136 | ```cpp |
| 137 | auto handle = TestCase::create(); |
| 138 | TestCase &instance = *handle; |
| 139 | |
| 140 | // open on the left |
| 141 | slint_testing::send_mouse_click(&instance, 20., 10.); |
| 142 | |
| 143 | // use left popup to open on the right |
| 144 | slint_testing::send_mouse_click(&instance, 10., 210.); |
| 145 | assert_eq(instance.get_popup_click_count(), 1); |
| 146 | |
| 147 | // no longer open on the left |
| 148 | slint_testing::send_mouse_click(&instance, 10., 210.); |
| 149 | assert_eq(instance.get_popup_click_count(), 1); |
| 150 | |
| 151 | // popup is now open on the right |
| 152 | // open a second popup on the left in the second container |
| 153 | slint_testing::send_mouse_click(&instance, 210., 210.); |
| 154 | assert_eq(instance.get_popup_click_count(), 2); |
| 155 | |
| 156 | // open on the left from the second container |
| 157 | slint_testing::send_mouse_click(&instance, 380., 10.); |
| 158 | |
| 159 | // second popup is still open on the left |
| 160 | // close the second popup |
| 161 | slint_testing::send_mouse_click(&instance, 10., 210.); |
| 162 | assert_eq(instance.get_popup_click_count(), 3); |
| 163 | |
| 164 | // first popup is still open as multiple of the same "PopupWindow" across different component instances is fine |
| 165 | slint_testing::send_mouse_click(&instance, 210., 210.); |
| 166 | assert_eq(instance.get_popup_click_count(), 4); |
| 167 | ``` |
| 168 | |
| 169 | */ |
| 170 | |