Skip to main content

brep_app/ui_kit/
icon_button.rs

1//! Icon buttons for the ribbon (32 px), toolbars and rails (28 px): a square
2//! hit area, the icon centred, a rounded hover square, and an accent bar when
3//! the tool is active.
4use super::{paint, Interact, Show};
5use crate::theme::{radius, Text, Theme, RING};
6use eframe::egui::{self, Rect, Vec2};
7
8/// Where an active icon button draws its 2 px accent bar.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
10pub enum BarSide {
11    /// Under the icon: the ribbon and toolbars.
12    #[default]
13    Bottom,
14    /// Beside the icon: vertical rails.
15    Left,
16}
17
18#[must_use = "an icon button does nothing until it is added to a Ui"]
19pub struct IconButton<'a> {
20    glyph: &'a str,
21    name: &'a str,
22    description: Option<&'a str>,
23    shortcut: Option<&'a str>,
24    size: f32,
25    active: bool,
26    bar: BarSide,
27    enabled: bool,
28    show: Show,
29}
30
31impl<'a> IconButton<'a> {
32    /// `glyph` is a key of the icon catalog; `name` is the tooltip's bold line
33    /// and the accessible label.
34    pub fn new(glyph: &'a str, name: &'a str) -> Self {
35        Self { glyph, name, description: None, shortcut: None, size: 28.0, active: false, bar: BarSide::Bottom, enabled: true, show: Show::Live }
36    }
37    /// The 32 px ribbon size.
38    pub fn ribbon(mut self) -> Self {
39        self.size = 32.0;
40        self
41    }
42    pub fn description(mut self, text: &'a str) -> Self {
43        self.description = Some(text);
44        self
45    }
46    pub fn shortcut(mut self, keys: &'a str) -> Self {
47        self.shortcut = Some(keys);
48        self
49    }
50    /// Toggle on, or the tool in use.
51    pub fn active(mut self, active: bool) -> Self {
52        self.active = active;
53        self
54    }
55    pub fn bar(mut self, side: BarSide) -> Self {
56        self.bar = side;
57        self
58    }
59    pub fn enabled(mut self, enabled: bool) -> Self {
60        self.enabled = enabled;
61        self
62    }
63    pub fn show(mut self, show: Show) -> Self {
64        self.show = show;
65        self
66    }
67}
68
69/// The tooltip card: the name in bold, a one-line description, the shortcut in a key chip.
70pub fn tooltip_card(ui: &mut egui::Ui, name: &str, description: Option<&str>, shortcut: Option<&str>) {
71    let theme = Theme::of(ui);
72    ui.horizontal(|ui| {
73        ui.label(egui::RichText::new(name).font(theme.font(Text::Label)).color(theme.colors.text_primary));
74        if let Some(keys) = shortcut {
75            let g = paint::galley(ui, keys, theme.font(Text::Caption));
76            let (rect, _) = ui.allocate_exact_size(g.size() + Vec2::new(8.0, 4.0), egui::Sense::hover());
77            paint::key_chip(ui, rect.min, keys, &theme);
78        }
79    });
80    if let Some(text) = description {
81        ui.label(egui::RichText::new(text).font(theme.font(Text::Caption)).color(theme.colors.text_secondary));
82    }
83}
84
85impl egui::Widget for IconButton<'_> {
86    fn ui(self, ui: &mut egui::Ui) -> egui::Response {
87        let theme = Theme::of(ui);
88        let c = &theme.colors;
89        let sense = if self.enabled { egui::Sense::click() } else { egui::Sense::hover() };
90        let (rect, response) = ui.allocate_exact_size(Vec2::splat(self.size), sense);
91        response.widget_info(|| {
92            let mut info = egui::WidgetInfo::labeled(egui::WidgetType::Button, self.enabled, self.name);
93            info.selected = Some(self.active);
94            info
95        });
96        if ui.is_rect_visible(rect) {
97            let state = Interact::of(&response, self.show, self.enabled);
98            let hover = ui.ctx().animate_bool_with_time(response.id.with("hover"), state.hovered, theme.motion_fast());
99            let on = ui.ctx().animate_bool_with_time(response.id.with("on"), self.active, theme.motion_base());
100            let pad = rect.shrink(1.0);
101            if on > 0.0 {
102                ui.painter().rect_filled(pad, radius::BUTTON, paint::lerp_color(egui::Color32::TRANSPARENT, c.accent_muted, on));
103            }
104            let overlay = if state.pressed { c.pressed } else { paint::lerp_color(egui::Color32::TRANSPARENT, c.hover, hover) };
105            ui.painter().rect_filled(pad, radius::BUTTON, overlay);
106            if on > 0.0 {
107                let bar = match self.bar {
108                    BarSide::Bottom => Rect::from_min_max(egui::pos2(rect.left() + 6.0, rect.bottom() - RING - 1.0), egui::pos2(rect.right() - 6.0, rect.bottom() - 1.0)),
109                    BarSide::Left => Rect::from_min_max(egui::pos2(rect.left() + 1.0, rect.top() + 6.0), egui::pos2(rect.left() + 1.0 + RING, rect.bottom() - 6.0)),
110                };
111                ui.painter().rect_filled(bar, 1.0, paint::lerp_color(egui::Color32::TRANSPARENT, c.accent_ring, on));
112            }
113            if state.focused {
114                paint::focus_ring(ui, pad, radius::BUTTON, &theme);
115            }
116            let ink = if self.enabled { c.text_primary } else { c.text_disabled };
117            let art = (self.size * 0.56).round();
118            let nudge = Vec2::new(0.0, if state.pressed { 1.0 } else { 0.0 });
119            let slot = Rect::from_center_size(rect.center() + nudge, Vec2::splat(art));
120            if !paint::icon(ui, self.glyph, slot, ink) {
121                let g = paint::galley(ui, self.glyph, theme.font(Text::Body));
122                ui.painter().galley(slot.center() - g.size() / 2.0, g, ink);
123            }
124        }
125        let (name, description, shortcut) = (self.name, self.description, self.shortcut);
126        let response = if self.enabled { response.on_hover_cursor(egui::CursorIcon::PointingHand) } else { response };
127        response.on_hover_ui(|ui| tooltip_card(ui, name, description, shortcut))
128    }
129}
130
131/// A ribbon group: its buttons in a row, the group label in caption size under
132/// them. Groups are separated by [`group_separator`].
133pub fn ribbon_group<R>(ui: &mut egui::Ui, label: &str, contents: impl FnOnce(&mut egui::Ui) -> R) -> R {
134    let theme = Theme::of(ui);
135    ui.vertical(|ui| {
136        ui.spacing_mut().item_spacing = Vec2::new(2.0, 2.0);
137        let row = ui.horizontal(contents);
138        let g = paint::galley(ui, label, theme.font(Text::Caption));
139        let (rect, _) = ui.allocate_exact_size(Vec2::new(row.response.rect.width().max(g.size().x), g.size().y), egui::Sense::hover());
140        ui.painter().galley(egui::pos2(rect.center().x - g.size().x / 2.0, rect.top()), g, theme.colors.text_secondary);
141        row.inner
142    })
143    .inner
144}
145
146/// The thin `border.subtle` line between two ribbon groups.
147pub fn group_separator(ui: &mut egui::Ui, height: f32) {
148    let theme = Theme::of(ui);
149    let (rect, _) = ui.allocate_exact_size(Vec2::new(9.0, height), egui::Sense::hover());
150    ui.painter().vline(rect.center().x, rect.y_range(), egui::Stroke::new(1.0, theme.colors.border_subtle));
151}