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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use super::events::EventData;
use super::DropDownListType;
use super::Flags;
use crate::prelude::*;
use crate::ui::components::{ComboBoxComponent, ComboBoxComponentDataProvider};
struct DataProvider<T: DropDownListType> {
items: Vec<T>,
}
impl<T> ComboBoxComponentDataProvider for DataProvider<T>
where
T: DropDownListType + 'static,
{
fn count(&self) -> u32 {
self.items.len() as u32
}
fn name(&self, index: u32) -> Option<&str> {
self.items.get(index as usize).map(|item| DropDownListType::name(item))
}
fn description(&self, index: u32) -> Option<&str> {
self.items.get(index as usize).map(|item| DropDownListType::description(item))
}
fn symbol(&self, index: u32) -> Option<&str> {
self.items.get(index as usize).map(|item| DropDownListType::symbol(item))
}
}
#[CustomControl(overwrite=OnPaint+OnDefaultAction+OnKeyPressed+OnMouseEvent+OnExpand, internal=true)]
pub struct DropDownList<T>
where
T: DropDownListType + 'static,
{
component: ComboBoxComponent<DataProvider<T>>,
data: DataProvider<T>,
flags: Flags,
}
impl<T> DropDownList<T>
where
T: DropDownListType + 'static,
{
/// Creates a new DropDownList control with the specified layout and flags.
/// The flags can be a combination of the following values:
/// * `Flags::AllowNoneSelection` - if set, the user can select no item from the DropDownList
/// * `Flags::ShowDescription` - if set, the description of the selected item will be displayed in the DropDownList
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// struct MyObject { name: String, description: String }
///
/// impl DropDownListType for MyObject {
/// fn name(&self) -> &str { &self.name }
/// fn description(&self) -> &str { &self.description }
/// fn symbol(&self) -> &str { "" }
/// }
///
/// let mut db = DropDownList::<MyObject>::new(layout!("x:1,y:1,w:30"), dropdownlist::Flags::ShowDescription);
/// db.add(MyObject { name: "Item 1".to_string(), description: "Description 1".to_string() });
/// db.add(MyObject { name: "Item 2".to_string(), description: "Description 2".to_string() });
/// db.add(MyObject { name: "Item 3".to_string(), description: "Description 3".to_string() });
/// ```
pub fn new(layout: Layout, flags: Flags) -> Self {
Self::with_symbol(0, layout, flags)
}
/// Creates a new DropDownList control with the specified layout, symbol size and flags.
/// The flags can be a combination of the following values:
/// * `Flags::AllowNoneSelection` - if set, the user can select no item from the DropDownList
/// * `Flags::ShowDescription` - if set, the description of the selected item will be displayed in the DropDownList
///
/// The symbol size can be one of the following values: 0, 1, 2 or 3
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// struct MyObject { name: String, symbol: &'static str }
///
/// impl DropDownListType for MyObject {
/// fn name(&self) -> &str { &self.name }
/// fn description(&self) -> &str { "" }
/// fn symbol(&self) -> &str { self.symbol }
/// }
///
/// let mut db = DropDownList::<MyObject>::with_symbol(1, layout!("x:1,y:1,w:30"), dropdownlist::Flags::None);
/// db.add(MyObject { name: "Sum".to_string(), symbol: "∑" });
/// db.add(MyObject { name: "Product".to_string(), symbol: "∏" });
/// db.add(MyObject { name: "Integral".to_string(), symbol: "∫" });
/// ```
pub fn with_symbol(symbol_size: u8, layout: Layout, flags: Flags) -> Self {
let mut obj = Self {
base: ControlBase::with_status_flags(layout, StatusFlags::Visible | StatusFlags::Enabled | StatusFlags::AcceptInput),
component: ComboBoxComponent::new(
flags.contains(Flags::AllowNoneSelection),
flags.contains(Flags::ShowDescription),
0,
symbol_size,
),
data: DataProvider { items: Vec::new() },
flags,
};
if flags.contains(Flags::AllowNoneSelection) {
obj.component.set_none_string("None");
}
obj.set_size_bounds(7, 1, u16::MAX, 1);
obj
}
/// Adds a new item to the DropDownList control
///
/// # Example
/// ```rust, no_run
/// use appcui::prelude::*;
///
/// struct MyObject { name: String, description: String, symbol: String }
///
/// impl MyObject {
/// fn new(name: &str, description: &str, symbol: &str) -> MyObject {
/// MyObject {
/// name: name.to_string(),
/// description: description.to_string(),
/// symbol: symbol.to_string()
/// }
/// }
/// }
///
/// impl DropDownListType for MyObject {
/// fn name(&self) -> &str { &self.name }
/// fn description(&self) -> &str { &self.description }
/// fn symbol(&self) -> &str { &self.symbol }
/// }
///
/// let mut db = DropDownList::<MyObject>::new(layout!("x:1,y:1,w:20"), dropdownlist::Flags::None);
/// db.add(MyObject::new("Heart", "Symbol of love", "❤"));
/// db.add(MyObject::new("Star", "Symbol of hope", "⭐"));
/// db.add(MyObject::new("Sun", "Symbol of light", "☀"));
///
///
/// ```
pub fn add(&mut self, value: T) {
self.data.items.push(value);
self.component.update_count(&mut self.base, self.data.items.len() as u32);
}
/// Returns the selected item from the ComboBox control. If no item is selected, the code will return None
pub fn selected_item(&self) -> Option<&T> {
let idx = self.component.current_index;
if idx >= self.data.count() {
None
} else {
Some(&self.data.items[idx as usize])
}
}
/// Returns the selected item from the ComboBox control. If no item is selected, the code will return None
pub fn selected_item_mut(&mut self) -> Option<&mut T> {
let idx = self.component.current_index;
if idx >= self.data.count() {
None
} else {
Some(&mut self.data.items[idx as usize])
}
}
/// Returns the index of the selected item. If no item is selected, the code will return None
pub fn index(&self) -> Option<u32> {
let idx = self.component.current_index;
if idx >= self.data.count() {
None
} else {
Some(idx)
}
}
/// Returns the item at the specified index. If the index is invalid, the code will return None
pub fn item(&self, index: u32) -> Option<&T> {
if index >= self.data.count() {
None
} else {
Some(&self.data.items[index as usize])
}
}
/// Returns the item at the specified index. If the index is invalid, the code will return None
pub fn item_mut(&mut self, index: u32) -> Option<&mut T> {
if index >= self.data.count() {
None
} else {
Some(&mut self.data.items[index as usize])
}
}
/// Sets the selected item based on the provided index. If the index is invalid, the index will be ignored
pub fn set_index(&mut self, index: u32) {
if index < self.data.count() {
self.component.update_current_index(index);
}
}
/// Clears all items from the ComboBox control
pub fn clear(&mut self) {
self.data.items.clear();
self.component.clear();
}
/// Returns true if the ComboBox control has a selected item
#[inline(always)]
pub fn has_selection(&self) -> bool {
self.component.current_index < self.data.count()
}
/// Returns the number of items in the ComboBox control
#[inline(always)]
pub fn count(&self) -> u32 {
self.data.count()
}
/// Sets the string that will be displayed when no item is selected. By default, this is "None" if the flag `AllowNoneSelection` is set or an empty string otherwise
#[inline(always)]
pub fn set_none_string(&mut self, text: &str) {
self.component.set_none_string(text);
}
fn emit_on_selection_changed_event(&mut self) {
self.raise_event(ControlEvent {
emitter: self.handle,
receiver: self.event_processor,
data: ControlEventData::DropDownList(EventData {
type_id: std::any::TypeId::of::<T>(),
}),
});
}
}
impl<T> OnPaint for DropDownList<T>
where
T: DropDownListType,
{
fn on_paint(&self, surface: &mut Surface, theme: &Theme) {
self.component.on_paint(&self.base, &self.data, surface, theme);
}
}
impl<T> OnExpand for DropDownList<T>
where
T: DropDownListType,
{
fn on_expand(&mut self, direction: ExpandedDirection) {
self.component.on_expand(&mut self.base, direction);
}
fn on_pack(&mut self) {
self.component.on_pack();
}
}
impl<T> OnDefaultAction for DropDownList<T>
where
T: DropDownListType,
{
fn on_default_action(&mut self) {
self.component.on_default_action(&mut self.base);
}
}
impl<T> OnKeyPressed for DropDownList<T>
where
T: DropDownListType,
{
fn on_key_pressed(&mut self, key: Key, character: char) -> EventProcessStatus {
let orig_index = self.component.current_index;
let result = self.component.on_key_pressed(&mut self.base, &self.data, key, character);
if orig_index != self.component.current_index {
self.emit_on_selection_changed_event();
}
result
}
}
impl<T> OnMouseEvent for DropDownList<T>
where
T: DropDownListType,
{
fn on_mouse_event(&mut self, event: &MouseEvent) -> EventProcessStatus {
let orig_index = self.component.current_index;
let result = self.component.on_mouse_event(&mut self.base, &self.data, event);
if orig_index != self.component.current_index {
self.emit_on_selection_changed_event();
}
result
}
}