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