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
use bitflags::bitflags;
use std::borrow::Cow;
use std::ptr;
use std::thread;
use crate::context::Context;
use crate::string::ImStr;
use crate::sys;
use crate::Ui;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ComboBoxHeight {
Small,
Regular,
Large,
Largest,
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum ComboBoxPreviewMode {
Label,
ArrowButton,
Full,
}
bitflags!(
#[repr(transparent)]
pub struct ComboBoxFlags: u32 {
const POPUP_ALIGN_LEFT = sys::ImGuiComboFlags_PopupAlignLeft;
const HEIGHT_SMALL = sys::ImGuiComboFlags_HeightSmall;
const HEIGHT_REGULAR = sys::ImGuiComboFlags_HeightRegular;
const HEIGHT_LARGE = sys::ImGuiComboFlags_HeightLarge;
const HEIGHT_LARGEST = sys::ImGuiComboFlags_HeightLargest;
const NO_ARROW_BUTTON = sys::ImGuiComboFlags_NoArrowButton;
const NO_PREVIEW = sys::ImGuiComboFlags_NoPreview;
}
);
#[derive(Copy, Clone, Debug)]
#[must_use]
pub struct ComboBox<'a> {
label: &'a ImStr,
preview_value: Option<&'a ImStr>,
flags: ComboBoxFlags,
}
impl<'a> ComboBox<'a> {
pub fn new(label: &'a ImStr) -> ComboBox<'a> {
ComboBox {
label,
preview_value: None,
flags: ComboBoxFlags::empty(),
}
}
#[inline]
pub fn preview_value(mut self, preview_value: &'a ImStr) -> Self {
self.preview_value = Some(preview_value);
self
}
#[inline]
pub fn flags(mut self, flags: ComboBoxFlags) -> Self {
self.flags = flags;
self
}
#[inline]
pub fn popup_align_left(mut self, popup_align_left: bool) -> Self {
self.flags
.set(ComboBoxFlags::POPUP_ALIGN_LEFT, popup_align_left);
self
}
#[inline]
pub fn height(mut self, height: ComboBoxHeight) -> Self {
self.flags
.set(ComboBoxFlags::HEIGHT_SMALL, height == ComboBoxHeight::Small);
self.flags.set(
ComboBoxFlags::HEIGHT_REGULAR,
height == ComboBoxHeight::Regular,
);
self.flags
.set(ComboBoxFlags::HEIGHT_LARGE, height == ComboBoxHeight::Large);
self.flags.set(
ComboBoxFlags::HEIGHT_LARGEST,
height == ComboBoxHeight::Largest,
);
self
}
#[inline]
pub fn preview_mode(mut self, preview_mode: ComboBoxPreviewMode) -> Self {
self.flags.set(
ComboBoxFlags::NO_ARROW_BUTTON,
preview_mode == ComboBoxPreviewMode::Label,
);
self.flags.set(
ComboBoxFlags::NO_PREVIEW,
preview_mode == ComboBoxPreviewMode::ArrowButton,
);
self
}
#[must_use]
pub fn begin(self, ui: &Ui) -> Option<ComboBoxToken> {
let should_render = unsafe {
sys::igBeginCombo(
self.label.as_ptr(),
self.preview_value.map(ImStr::as_ptr).unwrap_or(ptr::null()),
self.flags.bits() as i32,
)
};
if should_render {
Some(ComboBoxToken { ctx: ui.ctx })
} else {
None
}
}
pub fn build<F: FnOnce()>(self, ui: &Ui, f: F) {
if let Some(combo) = self.begin(ui) {
f();
combo.end(ui);
}
}
}
#[must_use]
pub struct ComboBoxToken {
ctx: *const Context,
}
impl ComboBoxToken {
pub fn end(mut self, _: &Ui) {
self.ctx = ptr::null();
unsafe { sys::igEndCombo() };
}
}
impl Drop for ComboBoxToken {
fn drop(&mut self) {
if !self.ctx.is_null() && !thread::panicking() {
panic!("A ComboBoxToken was leaked. Did you call .end()?");
}
}
}
impl<'a> ComboBox<'a> {
pub fn build_simple<T, L>(
self,
ui: &Ui,
current_item: &mut usize,
items: &[T],
label_fn: &L,
) -> bool
where
for<'b> L: Fn(&'b T) -> Cow<'b, ImStr>,
{
use crate::widget::selectable::Selectable;
let mut result = false;
let mut cb = self;
let preview_value = items.get(*current_item).map(label_fn);
if cb.preview_value.is_none() {
if let Some(preview_value) = preview_value.as_ref() {
cb = cb.preview_value(preview_value);
}
}
if let Some(_cb) = cb.begin(ui) {
for (idx, item) in items.iter().enumerate() {
let text = label_fn(item);
let selected = idx == *current_item;
if Selectable::new(&text).selected(selected).build(ui) {
*current_item = idx;
result = true;
}
}
_cb.end(ui);
}
result
}
pub fn build_simple_string<S>(self, ui: &Ui, current_item: &mut usize, items: &[&S]) -> bool
where
S: AsRef<ImStr> + ?Sized,
{
self.build_simple(ui, current_item, items, &|&s| s.as_ref().into())
}
}