1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
use NonNull;
use crate::;
use ;
/// A single choice is a toolbar item that can be positioned on the top or bottom part of a window
/// and can have two states (selected or unselected).
///
/// Within a toolbar group, only one single choice item can be selected at a time, making these items
/// suitable for mutually exclusive options. Single choice items are similar to radio buttons in
/// traditional UIs.
///
/// They can also have hotkeys defined using the '&' character in the caption. For example,
/// the caption "First &choice" will set 'Alt+C' as a hotkey for the single choice item.
///
/// To intercept single choice selections, implement the `ToolBarEvents` trait for the window containing
/// the single choice items and implement the `on_choice_selected` method.
///
/// # Example
///
/// The following example creates a window with two single choice items that display their selection state in a label:
///
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// #[Window(events = ToolBarEvents)]
/// struct SingleChoiceWindow {
/// option_one: Handle<toolbar::SingleChoice>,
/// option_two: Handle<toolbar::SingleChoice>,
/// status_label: Handle<Label>,
/// }
///
/// impl SingleChoiceWindow {
/// fn new() -> Self {
/// let mut win = SingleChoiceWindow {
/// base: window!("'Single Choice Demo',a:c,w:40,h:6"),
/// option_one: Handle::None,
/// option_two: Handle::None,
/// status_label: Handle::None,
/// };
///
/// // Create a toolbar group at the bottom left of the window
/// let group = win.toolbar().create_group(toolbar::GroupPosition::BottomLeft);
///
/// // Add single choice items to the toolbar group
/// let mut opt1 = toolbar::SingleChoice::new("Option 1");
/// opt1.set_tooltip("First option");
/// win.option_one = win.toolbar().add(group, opt1);
///
/// let mut opt2 = toolbar::SingleChoice::new("Option 2");
/// opt2.set_tooltip("Second option");
/// win.option_two = win.toolbar().add(group, opt2);
///
/// // Select the first option by default
/// let h = win.option_one;
/// if let Some(choice) = win.toolbar().get_mut(h) {
/// choice.select();
/// }
///
/// // Add a label to display the selection state
/// win.status_label = win.add(label!("'Option 1 is selected',a:c,w:30,h:1"));
///
/// win
/// }
/// }
///
/// impl ToolBarEvents for SingleChoiceWindow {
/// fn on_choice_selected(&mut self, handle: Handle<toolbar::SingleChoice>) -> EventProcessStatus {
/// let message = if handle == self.option_one {
/// "Option 1 is selected"
/// } else if handle == self.option_two {
/// "Option 2 is selected"
/// } else {
/// return EventProcessStatus::Ignored;
/// };
///
/// let h = self.status_label;
/// if let Some(label) = self.control_mut(h) {
/// label.set_caption(message);
/// }
/// EventProcessStatus::Processed
/// }
/// }
///
/// fn main() -> Result<(), appcui::system::Error> {
/// let mut app = App::new().build()?;
/// app.add_window(SingleChoiceWindow::new());
/// app.run();
/// Ok(())
/// }
/// ```
add_to_toolbar_impl!;