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 | |
4 | import { ComboBox } from "std-widgets.slint" ; |
5 | export component TestCase inherits Window { |
6 | width: 200px; |
7 | height: 200px; |
8 | |
9 | in-out property <string> output; |
10 | TouchArea { |
11 | clicked => { output += "clicked-under\n" ; } |
12 | } |
13 | |
14 | VerticalLayout { |
15 | alignment: center; |
16 | box := ComboBox { |
17 | model: ["Aaa" , "Bbb" , "Ccc" ]; |
18 | selected => { |
19 | output += "selected(" +self.current-value+"," +self.current-index+")\n" ; |
20 | } |
21 | } |
22 | } |
23 | |
24 | in-out property current-index <=> box.current-index; |
25 | in-out property current-value <=> box.current-value; |
26 | out property has-focus <=> box.has-focus; |
27 | } |
28 | |
29 | /* |
30 | |
31 | |
32 | ```rust |
33 | use slint::platform::Key; |
34 | use slint::SharedString; |
35 | |
36 | let instance = TestCase::new().unwrap(); |
37 | |
38 | assert_eq!(instance.get_current_value(), "Aaa"); |
39 | assert_eq!(instance.get_current_index(), 0); |
40 | assert_eq!(instance.get_has_focus(), false); |
41 | |
42 | // Change the index programmatically |
43 | instance.set_current_index(1); |
44 | assert_eq!(instance.get_current_value(), "Bbb"); |
45 | assert_eq!(instance.get_current_index(), 1); |
46 | assert_eq!(instance.get_output(), ""); |
47 | instance.set_current_index(0); |
48 | assert_eq!(instance.get_current_value(), "Aaa"); |
49 | assert_eq!(instance.get_current_index(), 0); |
50 | assert_eq!(instance.get_output(), ""); |
51 | assert_eq!(instance.get_has_focus(), false); |
52 | |
53 | // Open the combobox |
54 | slint_testing::send_mouse_click(&instance, 100., 100.); |
55 | assert_eq!(instance.get_output(), ""); |
56 | assert_eq!(instance.get_has_focus(), true); |
57 | |
58 | // click outside of the combobox, this should close it |
59 | slint_testing::send_mouse_click(&instance, 100., 10.); |
60 | assert_eq!(instance.get_output(), ""); |
61 | assert_eq!(instance.get_current_value(), "Aaa"); |
62 | assert_eq!(instance.get_current_index(), 0); |
63 | assert_eq!(instance.get_has_focus(), true); |
64 | |
65 | // click outside of the combobox again |
66 | slint_testing::send_mouse_click(&instance, 100., 10.); |
67 | assert_eq!(instance.get_output(), "clicked-under\n"); |
68 | instance.set_output(Default::default()); |
69 | assert_eq!(instance.get_current_value(), "Aaa"); |
70 | assert_eq!(instance.get_current_index(), 0); |
71 | assert_eq!(instance.get_has_focus(), true); |
72 | |
73 | |
74 | // The arrow change the values |
75 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); |
76 | assert_eq!(instance.get_current_value(), "Bbb"); |
77 | assert_eq!(instance.get_current_index(), 1); |
78 | assert_eq!(instance.get_output(), "selected(Bbb,1)\n"); |
79 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::DownArrow)); |
80 | assert_eq!(instance.get_current_value(), "Ccc"); |
81 | assert_eq!(instance.get_current_index(), 2); |
82 | assert_eq!(instance.get_output(), "selected(Bbb,1)\nselected(Ccc,2)\n"); |
83 | slint_testing::send_keyboard_string_sequence(&instance, &SharedString::from(Key::UpArrow)); |
84 | assert_eq!(instance.get_current_value(), "Bbb"); |
85 | assert_eq!(instance.get_current_index(), 1); |
86 | assert_eq!(instance.get_output(), "selected(Bbb,1)\nselected(Ccc,2)\nselected(Bbb,1)\n"); |
87 | instance.set_output(Default::default()); |
88 | |
89 | |
90 | ``` |
91 | |
92 | */ |
93 | |