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
292
293
294
295
296
//! Styled select (combo-box) widget.
//!
//! Wraps [`egui::ComboBox`] and paints it with the elegance palette: slate
//! input background, 1-px border, sky focus ring, and a matching chevron.
use std::borrow::Cow;
use std::hash::Hash;
use egui::{
Color32, ComboBox, CornerRadius, Pos2, Response, Sense, Stroke, Ui, Vec2, Widget, WidgetInfo,
WidgetText, WidgetType,
};
use crate::theme::{Theme, with_alpha};
/// A styled drop-down select.
///
/// Bind the selection to any `PartialEq + Clone` type — an enum, an index,
/// or a `String` — and supply a list of `(value, label)` pairs. Labels
/// accept `&'static str`, `String`, or any `Cow<'a, str>`, so static option
/// lists don't allocate.
///
/// ```no_run
/// # use elegance::Select;
/// # egui::__run_test_ui(|ui| {
/// #[derive(Clone, PartialEq)]
/// enum Unit { Us, Ms, S }
///
/// let mut unit = Unit::Ms;
/// ui.add(Select::new("unit", &mut unit).options([
/// (Unit::Us, "μs"),
/// (Unit::Ms, "ms"),
/// (Unit::S, "s"),
/// ]));
/// # });
/// ```
///
/// For string-valued selects where each option is both the value and the
/// label, use [`Select::strings`]:
///
/// ```no_run
/// # use elegance::Select;
/// # egui::__run_test_ui(|ui| {
/// let mut unit = String::from("ms");
/// ui.add(Select::strings("unit", &mut unit, ["us", "ms", "s"]));
/// # });
/// ```
#[must_use = "Add with `ui.add(...)`."]
pub struct Select<'a, T: PartialEq + Clone> {
id_salt: egui::Id,
value: &'a mut T,
label: Option<WidgetText>,
options: Vec<(T, Cow<'a, str>)>,
width: Option<f32>,
}
impl<'a, T: PartialEq + Clone> std::fmt::Debug for Select<'a, T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let labels: Vec<&str> = self.options.iter().map(|(_, l)| l.as_ref()).collect();
f.debug_struct("Select")
.field("id_salt", &self.id_salt)
.field("option_labels", &labels)
.field("width", &self.width)
.finish()
}
}
impl<'a, T: PartialEq + Clone> Select<'a, T> {
/// Create a select keyed by `id_salt` and bound to `value`.
/// Add selectable options via [`Select::options`].
pub fn new(id_salt: impl Hash, value: &'a mut T) -> Self {
Self {
id_salt: egui::Id::new(id_salt),
value,
label: None,
options: Vec::new(),
width: None,
}
}
/// Show a label above the select.
pub fn label(mut self, label: impl Into<WidgetText>) -> Self {
self.label = Some(label.into());
self
}
/// Set the selectable options as `(value, label)` pairs. Labels are
/// carried as `Cow<'a, str>`, so `&'static str` labels never allocate.
pub fn options<I, S>(mut self, options: I) -> Self
where
I: IntoIterator<Item = (T, S)>,
S: Into<Cow<'a, str>>,
{
self.options = options.into_iter().map(|(v, l)| (v, l.into())).collect();
self
}
/// Override the select width in points. Defaults to the intrinsic
/// size of the selected label plus padding.
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
}
impl<'a> Select<'a, String> {
/// Convenience constructor for string-valued selects. Each item is used
/// as both the value and the displayed label.
///
/// ```no_run
/// # use elegance::Select;
/// # egui::__run_test_ui(|ui| {
/// let mut unit = String::from("ms");
/// ui.add(Select::strings("unit", &mut unit, ["us", "ms", "s"]));
/// # });
/// ```
pub fn strings<I, S>(id_salt: impl Hash, value: &'a mut String, options: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<Cow<'a, str>>,
{
let options: Vec<(String, Cow<'a, str>)> = options
.into_iter()
.map(|s| {
let label: Cow<'a, str> = s.into();
let value = label.as_ref().to_owned();
(value, label)
})
.collect();
Self {
id_salt: egui::Id::new(id_salt),
value,
label: None,
options,
width: None,
}
}
}
impl<'a, T: PartialEq + Clone> Widget for Select<'a, T> {
fn ui(self, ui: &mut Ui) -> Response {
let theme = Theme::current(ui.ctx());
let p = &theme.palette;
let t = &theme.typography;
let mut changed = false;
let mut response = ui
.vertical(|ui| {
if let Some(label) = &self.label {
let rich = egui::RichText::new(label.text())
.color(p.text_muted)
.size(t.label);
ui.add(egui::Label::new(rich).wrap_mode(egui::TextWrapMode::Extend));
ui.add_space(2.0);
}
let width = self.width.unwrap_or(160.0);
let chevron_color = p.text_muted;
// Resolve the displayed label for the current value. Owned so
// it doesn't conflict with the mutable access to `self.value`
// in the inner closure.
let selected_label: String = self
.options
.iter()
.find(|(v, _)| v == &*self.value)
.map(|(_, l)| l.as_ref().to_owned())
.unwrap_or_default();
let field_label = self.label.as_ref().map(|l| l.text().to_string());
let response = crate::theme::with_themed_visuals(ui, |ui| {
let v = ui.visuals_mut();
crate::theme::themed_input_visuals(v, &theme, p.input_bg);
for w in [
&mut v.widgets.inactive,
&mut v.widgets.hovered,
&mut v.widgets.active,
&mut v.widgets.open,
] {
w.fg_stroke = Stroke::new(1.0, p.text);
}
v.override_text_color = Some(p.text);
ComboBox::from_id_salt(self.id_salt)
.width(width)
.selected_text(
egui::RichText::new(&selected_label)
.color(p.text)
.size(t.body),
)
.icon(move |ui, rect, _visuals, is_popup_open| {
paint_chevron(ui, rect, chevron_color, is_popup_open);
})
.show_ui(ui, |ui| {
ui.set_min_width(width);
// Tight stacking. `select_option` handles its own padding.
ui.spacing_mut().item_spacing.y = 2.0;
for (opt_value, opt_label) in self.options.iter() {
let selected = opt_value == &*self.value;
if select_option(ui, opt_label.as_ref(), selected, &theme).clicked()
&& opt_value != &*self.value
{
*self.value = opt_value.clone();
changed = true;
}
}
})
.response
});
if let Some(field_label) = field_label {
let selected_label = selected_label.clone();
response.widget_info(|| {
let mut info =
WidgetInfo::labeled(WidgetType::ComboBox, true, &field_label);
info.current_text_value = Some(selected_label.clone());
info
});
}
response
})
.inner;
if changed {
response.mark_changed();
}
response
}
}
/// Paint a thin, centered chevron inside `rect`. Points down when the popup is
/// closed (hint to open) and flips up when the popup is open (hint to close).
fn paint_chevron(ui: &egui::Ui, rect: egui::Rect, color: Color32, is_popup_open: bool) {
let painter = ui.painter();
let stroke = Stroke::new(1.4, color);
let half_w = (rect.width() * 0.35).min(5.0);
let half_h = (rect.height() * 0.18).min(3.0);
let c = rect.center();
let (left, right, tip) = if is_popup_open {
(
egui::pos2(c.x - half_w, c.y + half_h * 0.5),
egui::pos2(c.x + half_w, c.y + half_h * 0.5),
egui::pos2(c.x, c.y - half_h * 1.5),
)
} else {
(
egui::pos2(c.x - half_w, c.y - half_h * 0.5),
egui::pos2(c.x + half_w, c.y - half_h * 0.5),
egui::pos2(c.x, c.y + half_h * 1.5),
)
};
painter.line_segment([left, tip], stroke);
painter.line_segment([tip, right], stroke);
}
/// Render a single option row inside the Select popup. Keeps the text anchored
/// at a fixed offset across hover/selected/inactive states. egui's own
/// `ui.selectable_label` goes through `Button::selectable`, whose frame's
/// `expansion` changes between states, which shifts the text by ~1px on hover.
fn select_option(ui: &mut Ui, label: &str, selected: bool, theme: &Theme) -> Response {
let p = &theme.palette;
let t = &theme.typography;
let pad_x = 10.0;
let pad_y = 6.0;
let galley = crate::theme::placeholder_galley(ui, label, t.body, false, f32::INFINITY);
let content_w = galley.size().x;
let desired = Vec2::new(
ui.available_width().max(content_w + pad_x * 2.0),
galley.size().y.max(t.body) + pad_y * 2.0,
);
let (rect, response) = ui.allocate_exact_size(desired, Sense::click());
if ui.is_rect_visible(rect) {
let bg = if response.hovered() {
with_alpha(p.sky, 60)
} else if selected {
with_alpha(p.sky, 40)
} else {
Color32::TRANSPARENT
};
if bg.a() > 0 {
let radius = CornerRadius::same((theme.control_radius as u8).saturating_sub(2));
ui.painter().rect_filled(rect, radius, bg);
}
let label_pos = Pos2::new(rect.min.x + pad_x, rect.center().y - galley.size().y * 0.5);
ui.painter().galley(label_pos, galley, p.text);
}
response
}