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 { StandardListView } from "std-widgets.slint";
5
6export component TestCase inherits Window {
7 callback set-current-item(int);
8
9 out property <int> count: list.model.length;
10 in-out property <int> callback-current-item: -1;
11
12 in-out property<[StandardListViewItem]> model: [
13 { text: "Item 1" },
14 { text: "Item 2" },
15 { text: "Item 3" },
16 ];
17 in-out property <int> current-item <=> list.current-item;
18 out property has-focus <=> list.has-focus;
19
20 list := StandardListView {
21 model: root.model;
22
23 current-item-changed(index) => {
24 root.callback-current-item = index;
25 }
26 }
27
28
29 set-current-item(index) => {
30 list.set-current-item(index);
31 }
32}
33
34/*
35
36```rust
37use slint::platform::Key;
38use slint::SharedString;
39
40let instance = TestCase::new().unwrap();
41assert_eq!(instance.get_count(), 3);
42assert_eq!(instance.get_current_item(), -1);
43
44assert_eq!(instance.get_callback_current_item(), -1);
45instance.invoke_set_current_item(1);
46assert_eq!(instance.get_callback_current_item(), 1);
47assert_eq!(instance.get_current_item(), 1);
48
49instance.set_callback_current_item(-1);
50instance.invoke_set_current_item(1);
51assert_eq!(instance.get_callback_current_item(), -1);
52assert_eq!(instance.get_current_item(), 1);
53
54instance.invoke_set_current_item(0);
55assert_eq!(instance.get_has_focus(), false);
56
57// Focus the listview
58slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Tab));
59assert_eq!(instance.get_has_focus(), true);
60assert_eq!(instance.get_current_item(), 0);
61
62// Up and Down arrow keys move the selection
63slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow));
64assert_eq!(instance.get_current_item(), 0);
65slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
66assert_eq!(instance.get_current_item(), 1);
67slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
68assert_eq!(instance.get_current_item(), 2);
69slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
70assert_eq!(instance.get_current_item(), 2);
71slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow));
72assert_eq!(instance.get_current_item(), 1);
73
74// Pressing the Home key selects the first item
75instance.invoke_set_current_item(2);
76slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Home));
77assert_eq!(instance.get_current_item(), 0);
78
79// Pressing the End key selects the last item
80slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::End));
81assert_eq!(instance.get_current_item(), 2);
82
83// Pressing the space bar on the selected item does not deselect it
84slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
85assert_eq!(instance.get_current_item(), 2);
86
87// Control+Space deselects the currently focused item
88slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
89slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
90assert_eq!(instance.get_current_item(), -1);
91
92// Control+Space selects the currently focused item
93slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
94slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
95assert_eq!(instance.get_current_item(), 2);
96
97// Control+UpArrow moves the focus up one item but not the selection
98slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
99slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow));
100assert_eq!(instance.get_current_item(), 2);
101
102// Pressing the space bar selects the currently focused item if it is not
103slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
104assert_eq!(instance.get_current_item(), 1);
105
106// Control+DownArrow moves the focus down one item but not the selection
107slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
108slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow));
109assert_eq!(instance.get_current_item(), 1);
110slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
111assert_eq!(instance.get_current_item(), 2);
112
113// Control+Home moves the focus to the first item but not the selection
114slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
115slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Home));
116assert_eq!(instance.get_current_item(), 2);
117slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
118assert_eq!(instance.get_current_item(), 0);
119
120// Control+End moves the focus to the last item but not the selection
121slint_testing::send_keyboard_char(&instance, Key::Control.into(), true);
122slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::End));
123assert_eq!(instance.get_current_item(), 0);
124slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::Space));
125assert_eq!(instance.get_current_item(), 2);
126
127// Invoking the accessible default action of an item selects it
128let mut item2_search = slint_testing::ElementHandle::find_by_accessible_label(&instance, "Item 2");
129let item2 = item2_search.next().unwrap();
130item2.invoke_accessible_default_action();
131assert_eq!(instance.get_current_item(), 1);
132```
133
134*/
135