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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
//! ButtonGroup atom - Radio-style button group with normalized output
//!
//! A group of mutually exclusive buttons (like radio buttons) that returns
//! a normalized 0.0-1.0 value based on selection position.
//!
//! # Example
//! ```ignore
//! // Wave selector (returns 0.0, 0.33, 0.67, 1.0)
//! ButtonGroup::new(&["Sin", "Saw", "Sqr", "Tri"])
//! .show_with(ctx, model.wave_type, Msg::SetWaveType);
//!
//! // With icons
//! ButtonGroup::new(&["◐", "●", "◑"])
//! .show_with(ctx, model.pan_mode, Msg::SetPanMode);
//! ```
use crate::Theme;
use egui::{Response, Sense, Ui, Vec2};
use egui_cha::ViewCtx;
/// Button group orientation
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GroupOrientation {
/// Horizontal layout (default)
#[default]
Horizontal,
/// Vertical layout
Vertical,
}
/// Button group size variants
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GroupSize {
/// Compact size
Compact,
/// Medium size (default)
#[default]
Medium,
/// Large size
Large,
}
/// A radio-style button group that returns normalized 0.0-1.0 values
pub struct ButtonGroup<'a> {
labels: &'a [&'a str],
orientation: GroupOrientation,
size: GroupSize,
disabled: bool,
/// Whether to stretch to fill available width
expand: bool,
}
impl<'a> ButtonGroup<'a> {
/// Create a new button group with the given labels
pub fn new(labels: &'a [&'a str]) -> Self {
Self {
labels,
orientation: GroupOrientation::default(),
size: GroupSize::default(),
disabled: false,
expand: false,
}
}
/// Set the orientation
pub fn orientation(mut self, orientation: GroupOrientation) -> Self {
self.orientation = orientation;
self
}
/// Use vertical orientation
pub fn vertical(mut self) -> Self {
self.orientation = GroupOrientation::Vertical;
self
}
/// Set the size variant
pub fn size(mut self, size: GroupSize) -> Self {
self.size = size;
self
}
/// Use compact size
pub fn compact(mut self) -> Self {
self.size = GroupSize::Compact;
self
}
/// Use large size
pub fn large(mut self) -> Self {
self.size = GroupSize::Large;
self
}
/// Set disabled state
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
/// Expand to fill available width
pub fn expand(mut self) -> Self {
self.expand = true;
self
}
/// Convert normalized value (0.0-1.0) to index
fn value_to_index(&self, value: f64) -> usize {
if self.labels.len() <= 1 {
return 0;
}
let max_idx = self.labels.len() - 1;
(value * max_idx as f64).round() as usize
}
/// Convert index to normalized value (0.0-1.0)
fn index_to_value(&self, index: usize) -> f64 {
if self.labels.len() <= 1 {
return 0.0;
}
let max_idx = self.labels.len() - 1;
index as f64 / max_idx as f64
}
/// TEA-style: Show button group with normalized value, emit Msg on change
pub fn show_with<Msg>(
self,
ctx: &mut ViewCtx<'_, Msg>,
value: f64,
on_change: impl FnOnce(f64) -> Msg,
) {
let mut current = value;
let response = self.show_internal(ctx.ui, &mut current);
if response.changed() {
ctx.emit(on_change(current));
}
}
/// Show button group (modifies normalized value in place)
pub fn show(self, ui: &mut Ui, value: &mut f64) -> Response {
self.show_internal(ui, value)
}
/// Show and return selected index instead of normalized value
pub fn show_index(self, ui: &mut Ui, index: &mut usize) -> Response {
let labels_len = self.labels.len();
let mut value = self.index_to_value(*index);
let response = self.show_internal(ui, &mut value);
if response.changed() {
// Recalculate index from value
if labels_len <= 1 {
*index = 0;
} else {
let max_idx = labels_len - 1;
*index = (value * max_idx as f64).round() as usize;
}
}
response
}
fn show_internal(self, ui: &mut Ui, value: &mut f64) -> Response {
let theme = Theme::current(ui.ctx());
let selected_idx = self.value_to_index(*value);
// Calculate button dimensions
let (button_height, font_size, padding_h) = match self.size {
GroupSize::Compact => (
theme.spacing_md + theme.spacing_sm,
theme.font_size_xs,
theme.spacing_sm,
),
GroupSize::Medium => (
theme.spacing_lg + theme.spacing_sm,
theme.font_size_sm,
theme.spacing_md,
),
GroupSize::Large => (theme.spacing_xl, theme.font_size_md, theme.spacing_lg),
};
// Calculate total size
let available_width = if self.expand {
ui.available_width()
} else {
0.0
};
let mut total_response: Option<Response> = None;
let mut changed = false;
match self.orientation {
GroupOrientation::Horizontal => {
ui.horizontal(|ui| {
let button_width = if self.expand && !self.labels.is_empty() {
available_width / self.labels.len() as f32
} else {
0.0 // Will be calculated per button
};
for (idx, label) in self.labels.iter().enumerate() {
let is_selected = idx == selected_idx;
let is_first = idx == 0;
let is_last = idx == self.labels.len() - 1;
let response = self.draw_button(
ui,
label,
is_selected,
is_first,
is_last,
button_width,
button_height,
font_size,
padding_h,
&theme,
);
if response.clicked() && !self.disabled {
*value = self.index_to_value(idx);
changed = true;
}
if let Some(ref mut total) = total_response {
*total = total.union(response);
} else {
total_response = Some(response);
}
}
});
}
GroupOrientation::Vertical => {
ui.vertical(|ui| {
for (idx, label) in self.labels.iter().enumerate() {
let is_selected = idx == selected_idx;
let is_first = idx == 0;
let is_last = idx == self.labels.len() - 1;
let button_width = if self.expand { available_width } else { 0.0 };
let response = self.draw_button(
ui,
label,
is_selected,
is_first,
is_last,
button_width,
button_height,
font_size,
padding_h,
&theme,
);
if response.clicked() && !self.disabled {
*value = self.index_to_value(idx);
changed = true;
}
if let Some(ref mut total) = total_response {
*total = total.union(response);
} else {
total_response = Some(response);
}
}
});
}
}
let mut response =
total_response.unwrap_or_else(|| ui.allocate_response(Vec2::ZERO, Sense::hover()));
if changed {
response.mark_changed();
}
response
}
fn draw_button(
&self,
ui: &mut Ui,
label: &str,
is_selected: bool,
is_first: bool,
is_last: bool,
min_width: f32,
height: f32,
font_size: f32,
padding_h: f32,
theme: &Theme,
) -> Response {
// Calculate text size for button width
let text_width = ui.fonts_mut(|f| {
f.glyph_width(&egui::FontId::proportional(font_size), 'M') * label.len() as f32
});
let button_width = if min_width > 0.0 {
min_width
} else {
text_width + padding_h * 2.0
};
let (rect, response) = ui.allocate_exact_size(
Vec2::new(button_width, height),
if self.disabled {
Sense::hover()
} else {
Sense::click()
},
);
if ui.is_rect_visible(rect) {
let painter = ui.painter();
// Determine colors
let (bg_color, text_color) = if self.disabled {
(theme.bg_tertiary, theme.text_muted)
} else if is_selected {
(theme.primary, theme.primary_text)
} else if response.hovered() {
(theme.bg_tertiary, theme.text_primary)
} else {
(theme.bg_secondary, theme.text_secondary)
};
// Calculate corner radius (only round outer corners)
let radius = theme.radius_sm;
let r = radius as u8;
let rounding = match (is_first, is_last, &self.orientation) {
(true, true, _) => egui::CornerRadius::same(r),
(true, false, GroupOrientation::Horizontal) => egui::CornerRadius {
nw: r,
sw: r,
ne: 0,
se: 0,
},
(false, true, GroupOrientation::Horizontal) => egui::CornerRadius {
nw: 0,
sw: 0,
ne: r,
se: r,
},
(true, false, GroupOrientation::Vertical) => egui::CornerRadius {
nw: r,
ne: r,
sw: 0,
se: 0,
},
(false, true, GroupOrientation::Vertical) => egui::CornerRadius {
nw: 0,
ne: 0,
sw: r,
se: r,
},
_ => egui::CornerRadius::ZERO,
};
// Draw background
painter.rect_filled(rect, rounding, bg_color);
// Draw border
if !is_selected {
painter.rect_stroke(
rect,
rounding,
egui::Stroke::new(theme.border_width, theme.border),
egui::StrokeKind::Inside,
);
}
// Draw text
painter.text(
rect.center(),
egui::Align2::CENTER_CENTER,
label,
egui::FontId::proportional(font_size),
text_color,
);
}
response
}
}