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
4import { ListView } from "std-widgets.slint";
5
6Item := Text {
7 property <int> index;
8 text: "I'm item #" + index;
9 Rectangle {
10 border-width: 1px;
11 border-color: red;
12 }
13}
14
15export TestCase := Window {
16 width: 300phx;
17 height: 300phx;
18
19 property <int> last_clicked: -1;
20
21 property <length> item-height: 25phx;
22 property <length> listview-y <=> lv.viewport_y;
23
24 lv := ListView {
25 for i in 200: r := Item {
26 index: i;
27 height: item-height;
28 property <bool> open;
29 HorizontalLayout {
30 TouchArea {
31 clicked => {
32 if (open) {
33 r.height = item-height;
34 open = false;
35 } else {
36 lv.viewport_y = -r.y;
37 r.height = lv.visible-height;
38 open = true;
39 }
40 }
41 }
42 TouchArea {
43 clicked => {
44 last-clicked = i;
45 }
46 }
47 }
48 }
49 }
50}
51
52/*
53```rust
54let instance = TestCase::new().unwrap();
55
56// Open the item 6
57slint_testing::send_mouse_click(&instance, 50., 25. * 6. + 10.);
58slint_testing::send_mouse_click(&instance, 250., 10.);
59assert_eq!(instance.get_last_clicked(), 6);
60instance.set_last_clicked(-1);
61slint_testing::send_mouse_click(&instance, 250., 270.);
62assert_eq!(instance.get_last_clicked(), 6);
63
64// Close the item 6
65slint_testing::send_mouse_click(&instance, 50., 160.);
66// Item 6 should stay the first, so in position 3 we have the 9th item
67slint_testing::send_mouse_click(&instance, 250., 25. * 3. + 10.);
68assert_eq!(instance.get_last_clicked(), 9);
69
70// Open the 10th item (position 4)
71slint_testing::send_mouse_click(&instance, 50., 25. * 4. + 10.);
72slint_testing::send_mouse_click(&instance, 250., 10.);
73assert_eq!(instance.get_last_clicked(), 10);
74instance.set_last_clicked(-1);
75slint_testing::send_mouse_click(&instance, 250., 270.);
76assert_eq!(instance.get_last_clicked(), 10);
77```
78*/
79