Skip to main content

egui_macview/controls/
segmented.rs

1//! Authentic macOS NSSegmentedControl primitive.
2//!
3//! Grounded in macOS HIG and W3C DTCG tokens via [`MacTheme`].
4//! Fully data-driven: zero hardcoded metrics.
5
6use crate::theme::MacTheme;
7use egui::{
8    Color32, CornerRadius, Key, Modifiers, Pos2, Rect, Response, Sense, Stroke, StrokeKind, Ui,
9    Vec2,
10};
11
12/// Authentic macOS segmented pill selector (`NSSegmentedControl` equivalent).
13///
14/// Features animated sliding selection indicator pill via `CASpring`, token-driven padding and typography,
15/// ambient drop shadow, keyboard arrow navigation, and crisp border separators.
16///
17/// # Example
18///
19/// ```rust
20/// use egui_macview::{MacSegmentedControl, MacTheme};
21///
22/// # let ctx = egui::Context::default();
23/// # let mut output = ctx.run_ui(egui::RawInput::default(), |ui| {
24/// let theme = MacTheme::default();
25/// let mut selected = 1;
26/// let tabs = ["Day", "Week", "Month", "Year"];
27///
28/// MacSegmentedControl::new(&tabs, selected)
29///     .theme(&theme)
30///     .show(ui, |new_idx| {
31///         selected = new_idx;
32///     });
33/// # });
34/// # output.textures_delta.clear();
35/// ```
36pub struct MacSegmentedControl<'a> {
37    options: &'a [&'a str],
38    selected_idx: usize,
39    enabled: bool,
40    theme: Option<&'a MacTheme>,
41    height: Option<f32>,
42}
43
44impl<'a> MacSegmentedControl<'a> {
45    /// Creates a segmented control with option labels and currently selected index.
46    pub fn new(options: &'a [&'a str], selected_idx: usize) -> Self {
47        Self {
48            options,
49            selected_idx,
50            enabled: true,
51            theme: None,
52            height: None,
53        }
54    }
55
56    /// Enables or disables the segmented control.
57    pub fn enabled(mut self, enabled: bool) -> Self {
58        self.enabled = enabled;
59        self
60    }
61
62    /// Injects dynamic theme to eliminate hardcoded metrics and colors.
63    pub fn theme(mut self, theme: &'a MacTheme) -> Self {
64        self.theme = Some(theme);
65        self
66    }
67
68    /// Overrides control height in points (defaults to token segmented height).
69    pub fn height(mut self, height: f32) -> Self {
70        self.height = Some(height);
71        self
72    }
73
74    /// Renders the segmented control and fires `on_select` when a new segment is clicked.
75    pub fn show(self, ui: &mut Ui, mut on_select: impl FnMut(usize)) -> Response {
76        let theme_cow = MacTheme::resolve(self.theme, ui.ctx());
77        let theme: &MacTheme = &theme_cow;
78
79        let segmented_tokens = theme.segmented();
80        let metrics = &segmented_tokens.regular;
81        let height = self.height.unwrap_or_else(|| metrics.height.points());
82        let corner_radius = metrics.corner_radius.points();
83        let indicator_radius = metrics.indicator_radius.points();
84        let border_width = segmented_tokens.border_width.points();
85        let indicator_inset = segmented_tokens.indicator_inset.points();
86
87        let font_id = egui::FontId::proportional(segmented_tokens.font_size.points());
88        let padding_h = segmented_tokens.padding_horizontal.points();
89        let min_item_w = segmented_tokens.item_width.points();
90
91        let mut max_content_w = min_item_w;
92        let mut galleys = Vec::with_capacity(self.options.len());
93        for &opt in self.options {
94            let galley =
95                ui.painter()
96                    .layout_no_wrap(opt.to_owned(), font_id.clone(), Color32::PLACEHOLDER);
97            let padded_w = galley.size().x + padding_h * 2.0;
98            if padded_w > max_content_w {
99                max_content_w = padded_w;
100            }
101            galleys.push(galley);
102        }
103        let segment_width = max_content_w;
104        let count = self.options.len().max(1);
105        let total_width = segment_width * count as f32;
106
107        let sense = if self.enabled {
108            Sense::click()
109        } else {
110            Sense::hover()
111        };
112        let (rect, mut response) = ui.allocate_exact_size(Vec2::new(total_width, height), sense);
113        let mut active_idx = self.selected_idx.min(count.saturating_sub(1));
114
115        if self.enabled && response.has_focus() {
116            if ui.input_mut(|i| i.consume_key(Modifiers::NONE, Key::ArrowRight))
117                && active_idx + 1 < count
118            {
119                active_idx += 1;
120                on_select(active_idx);
121                response.mark_changed();
122            } else if ui.input_mut(|i| i.consume_key(Modifiers::NONE, Key::ArrowLeft))
123                && active_idx > 0
124            {
125                active_idx -= 1;
126                on_select(active_idx);
127                response.mark_changed();
128            }
129        }
130
131        let alpha_factor = if self.enabled {
132            1.0
133        } else {
134            theme.disabled_opacity()
135        };
136        let painter = ui.painter_at(rect);
137
138        painter.rect(
139            rect,
140            CornerRadius::same(corner_radius as u8),
141            theme.control_background().gamma_multiply(alpha_factor),
142            Stroke::new(border_width, theme.separator().gamma_multiply(alpha_factor)),
143            StrokeKind::Inside,
144        );
145
146        // CASpring sliding pill animation stored in egui temp memory
147        let spring = theme.motion().swiftui_snappy.to_spring();
148        let spring_key = response.id.with("seg_caspring");
149        let target_idx = active_idx as f32;
150        let anim_idx = spring.animate_f32(ui.ctx(), spring_key, target_idx);
151
152        let pill_x = rect.min.x + anim_idx * segment_width;
153        let animated_seg_rect = Rect::from_min_size(
154            Pos2::new(pill_x, rect.min.y),
155            Vec2::new(segment_width, height),
156        );
157        let pill_rect = animated_seg_rect.shrink(indicator_inset);
158
159        painter.rect_filled(
160            pill_rect.translate(Vec2::new(0.0, border_width)),
161            CornerRadius::same(indicator_radius as u8),
162            theme.drop_shadow().gamma_multiply(alpha_factor),
163        );
164
165        painter.rect(
166            pill_rect,
167            CornerRadius::same(indicator_radius as u8),
168            theme.control().gamma_multiply(alpha_factor),
169            Stroke::new(border_width, theme.separator().gamma_multiply(alpha_factor)),
170            StrokeKind::Inside,
171        );
172
173        for (i, galley) in galleys.into_iter().enumerate() {
174            let seg_rect = Rect::from_min_size(
175                Pos2::new(rect.min.x + i as f32 * segment_width, rect.min.y),
176                Vec2::new(segment_width, height),
177            );
178
179            if self.enabled && response.clicked() {
180                if let Some(pos) = ui.input(|inp| inp.pointer.interact_pos()) {
181                    if seg_rect.contains(pos) {
182                        response.request_focus();
183                        on_select(i);
184                        response.mark_changed();
185                    }
186                }
187            }
188
189            let is_hovered = self.enabled && ui.rect_contains_pointer(seg_rect);
190            let text_color = if i == active_idx || is_hovered {
191                theme.label().gamma_multiply(alpha_factor)
192            } else {
193                theme.secondary_label().gamma_multiply(alpha_factor)
194            };
195
196            let text_pos = egui::Align2::CENTER_CENTER
197                .anchor_size(seg_rect.center(), galley.size())
198                .min;
199            painter.galley(text_pos, galley, text_color);
200        }
201
202        response
203    }
204}