Skip to main content

brep_app/ui_kit/
surface.rs

1//! Surfaces: cards and sections, panel headers, popup and menu rows, the
2//! dialog frame, toasts and tree rows.
3use super::{paint, Button};
4use crate::theme::{radius, space, Text, Theme, BORDER};
5use eframe::egui::{self, Color32, Rect, Stroke, Vec2};
6
7/// The frame of a popup, menu or context menu: `bg.raised`, the small shadow.
8pub fn popup_frame(theme: &Theme) -> egui::Frame {
9    theme.raised_frame().shadow(theme.shadow_small()).inner_margin(egui::Margin::same(space::XS as i8))
10}
11
12/// A card: `bg.raised`, radius 8, a 1 px `border.subtle` border, 12 px padding.
13pub fn card<R>(ui: &mut egui::Ui, contents: impl FnOnce(&mut egui::Ui) -> R) -> egui::InnerResponse<R> {
14    let theme = Theme::of(ui);
15    theme.raised_frame().inner_margin(egui::Margin::same(space::LG as i8)).show(ui, contents)
16}
17
18/// A section: a card under a section header.
19pub fn section<R>(ui: &mut egui::Ui, title: &str, contents: impl FnOnce(&mut egui::Ui) -> R) -> egui::InnerResponse<R> {
20    let theme = Theme::of(ui);
21    card(ui, |ui| {
22        ui.label(egui::RichText::new(title).font(theme.font(Text::Section)).color(theme.colors.text_primary));
23        ui.add_space(space::SM);
24        contents(ui)
25    })
26}
27
28/// One row of a section's two-column grid: the label left, the control right.
29pub fn field_row<R>(ui: &mut egui::Ui, label: &str, label_width: f32, control: impl FnOnce(&mut egui::Ui) -> R) -> R {
30    let theme = Theme::of(ui);
31    ui.horizontal(|ui| {
32        let g = paint::galley(ui, label, theme.font(Text::Label));
33        let (rect, _) = ui.allocate_exact_size(Vec2::new(label_width, theme.control_height()), egui::Sense::hover());
34        ui.painter().galley(egui::pos2(rect.left(), rect.center().y - g.size().y / 2.0), g, theme.colors.text_secondary);
35        control(ui)
36    })
37    .inner
38}
39
40/// A panel's header row: a collapse chevron, the title, and optional actions
41/// (ghost icon buttons) on the right. Returns the header's response; a click
42/// toggles `collapsed`.
43pub fn panel_header(ui: &mut egui::Ui, title: &str, collapsed: &mut bool, actions: impl FnOnce(&mut egui::Ui)) -> egui::Response {
44    let theme = Theme::of(ui);
45    let c = &theme.colors;
46    let height = theme.control_height() + 6.0;
47    let (rect, response) = ui.allocate_exact_size(Vec2::new(ui.available_width(), height), egui::Sense::click());
48    response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::CollapsingHeader, true, title));
49    if response.clicked() {
50        *collapsed = !*collapsed;
51    }
52    if response.hovered() {
53        ui.painter().rect_filled(rect, 0.0, c.hover);
54    }
55    let open = ui.ctx().animate_bool_with_time(response.id.with("open"), !*collapsed, theme.motion_base());
56    paint::disclosure(ui.painter(), egui::pos2(rect.left() + space::LG, rect.center().y), 3.5, open, Stroke::new(1.5, c.text_secondary));
57    let g = paint::galley(ui, title, theme.font(Text::Section));
58    ui.painter().galley(egui::pos2(rect.left() + space::XXL, rect.center().y - g.size().y / 2.0), g, c.text_primary);
59    ui.painter().hline(rect.x_range(), rect.bottom() - 0.5, Stroke::new(1.0, c.border_subtle));
60    let mut right = ui.new_child(egui::UiBuilder::new().max_rect(rect.shrink2(Vec2::new(space::SM, 0.0))).layout(egui::Layout::right_to_left(egui::Align::Center)));
61    actions(&mut right);
62    response
63}
64
65/// A caption header inside a menu or dropdown.
66pub fn menu_header(ui: &mut egui::Ui, text: &str) {
67    let theme = Theme::of(ui);
68    let g = paint::galley(ui, text, theme.font(Text::Caption));
69    let (rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width().max(g.size().x + space::LG), g.size().y + space::MD), egui::Sense::hover());
70    ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.bottom() - g.size().y - 2.0), g, theme.colors.text_secondary);
71}
72
73/// A menu separator.
74pub fn menu_separator(ui: &mut egui::Ui) {
75    let theme = Theme::of(ui);
76    let (rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width(), space::SM + 1.0), egui::Sense::hover());
77    ui.painter().hline(rect.x_range(), rect.center().y, Stroke::new(1.0, theme.colors.border_subtle));
78}
79
80/// A menu row: 28 px, hover background, shortcut text on the right, an accent
81/// check when `checked`.
82pub fn menu_row(ui: &mut egui::Ui, text: &str, shortcut: Option<&str>, checked: bool) -> egui::Response {
83    menu_row_with(ui, text, shortcut, checked, false)
84}
85
86/// [`menu_row`] with the keyboard cursor on it.
87pub fn menu_row_with(ui: &mut egui::Ui, text: &str, shortcut: Option<&str>, checked: bool, cursor: bool) -> egui::Response {
88    let theme = Theme::of(ui);
89    let c = &theme.colors;
90    let g = paint::galley(ui, text, theme.font(Text::Body));
91    let keys = shortcut.map(|s| paint::galley(ui, s, theme.font(Text::Caption)));
92    let need = space::MD + g.size().x + space::XL + keys.as_ref().map_or(0.0, |k| k.size().x + space::LG) + 16.0 + space::SM;
93    let (rect, response) = ui.allocate_exact_size(Vec2::new(ui.available_width().max(need), 28.0), egui::Sense::click());
94    response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, checked, text));
95    if ui.is_rect_visible(rect) {
96        if response.hovered() || cursor {
97            let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
98            ui.painter().rect_filled(rect, radius::INPUT, overlay);
99        }
100        ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.center().y - g.size().y / 2.0), g, c.text_primary);
101        let mut right = rect.right() - space::SM;
102        if checked {
103            let mark = Rect::from_center_size(egui::pos2(right - 8.0, rect.center().y), Vec2::splat(14.0));
104            paint::tick(ui.painter(), mark, Stroke::new(1.8, c.accent_text));
105        }
106        right -= 16.0 + space::SM;
107        if let Some(k) = keys {
108            ui.painter().galley(egui::pos2(right - k.size().x, rect.center().y - k.size().y / 2.0), k, c.text_secondary);
109        }
110    }
111    response.on_hover_cursor(egui::CursorIcon::PointingHand)
112}
113
114/// What a dialog's footer reported.
115#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
116pub struct DialogOutput {
117    pub confirmed: bool,
118    pub cancelled: bool,
119}
120
121/// The dialog look: the medium shadow, a title row, the content, and a footer
122/// with the primary button on the right and the secondary to its left. Drawn
123/// in place (the gallery, and the body of an `egui::Window` in step 4).
124pub fn dialog(ui: &mut egui::Ui, title: &str, primary: &str, secondary: &str, width: f32, contents: impl FnOnce(&mut egui::Ui)) -> DialogOutput {
125    let theme = Theme::of(ui);
126    let c = &theme.colors;
127    let mut out = DialogOutput::default();
128    theme.raised_frame().shadow(theme.shadow_medium()).show(ui, |ui| {
129        ui.set_width(width);
130        ui.spacing_mut().item_spacing.y = 0.0;
131        egui::Frame::new().inner_margin(egui::Margin::symmetric(space::XL as i8, space::LG as i8)).show(ui, |ui| {
132            ui.set_width(width - space::XL * 2.0);
133            ui.label(egui::RichText::new(title).font(theme.font(Text::Title)).color(c.text_primary));
134        });
135        let (line, _) = ui.allocate_exact_size(Vec2::new(width, 1.0), egui::Sense::hover());
136        ui.painter().hline(line.x_range(), line.center().y, Stroke::new(1.0, c.border_subtle));
137        egui::Frame::new().inner_margin(egui::Margin::same(space::XL as i8)).show(ui, |ui| {
138            ui.set_width(width - space::XL * 2.0);
139            ui.spacing_mut().item_spacing.y = space::SM;
140            contents(ui);
141        });
142        egui::Frame::new().inner_margin(egui::Margin { left: space::XL as i8, right: space::XL as i8, top: 0, bottom: space::XL as i8 }).show(ui, |ui| {
143            ui.set_width(width - space::XL * 2.0);
144            let row = Vec2::new(width - space::XL * 2.0, theme.control_height());
145            ui.allocate_ui_with_layout(row, egui::Layout::right_to_left(egui::Align::Center), |ui| {
146                ui.spacing_mut().item_spacing.x = space::MD;
147                out.confirmed = ui.add(Button::primary(primary).min_width(72.0)).clicked();
148                out.cancelled = ui.add(Button::new(secondary).min_width(72.0)).clicked();
149            });
150        });
151    });
152    out
153}
154
155#[derive(Debug, Clone, Copy, PartialEq, Eq)]
156pub enum Kind {
157    Info,
158    Success,
159    Warning,
160    Error,
161}
162
163impl Kind {
164    pub fn color(self, theme: &Theme) -> Color32 {
165        match self {
166            Kind::Info => theme.colors.accent_ring,
167            Kind::Success => theme.colors.success,
168            Kind::Warning => theme.colors.warning,
169            Kind::Error => theme.colors.danger,
170        }
171    }
172}
173
174/// A toast card: a coloured left bar for the kind, the text, a close ×.
175/// Returns the × response.
176pub fn toast(ui: &mut egui::Ui, kind: Kind, text: &str, width: f32) -> egui::Response {
177    let theme = Theme::of(ui);
178    let c = &theme.colors;
179    let g = ui.painter().layout(text.to_owned(), theme.font(Text::Body), Color32::PLACEHOLDER, width - 4.0 - space::LG * 2.0 - 20.0);
180    let height = (g.size().y + space::MD * 2.0).max(36.0);
181    let (rect, _) = ui.allocate_exact_size(Vec2::new(width, height), egui::Sense::hover());
182    ui.painter().add(theme.shadow_small().as_shape(rect, radius::CARD));
183    ui.painter().rect(rect, radius::CARD, c.bg_raised, Stroke::new(BORDER, c.border_subtle), egui::StrokeKind::Inside);
184    let bar = Rect::from_min_max(rect.min + Vec2::new(1.0, 1.0), egui::pos2(rect.left() + 4.0, rect.bottom() - 1.0));
185    ui.painter().rect_filled(bar, egui::CornerRadius { nw: 7, sw: 7, ne: 0, se: 0 }, kind.color(&theme));
186    ui.painter().galley(egui::pos2(rect.left() + 4.0 + space::LG, rect.center().y - g.size().y / 2.0), g, c.text_primary);
187    let close = Rect::from_center_size(egui::pos2(rect.right() - space::LG - 2.0, rect.center().y), Vec2::splat(18.0));
188    let response = ui.interact(close, ui.next_auto_id().with("toast-close"), egui::Sense::click());
189    response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Dismiss"));
190    if response.hovered() {
191        ui.painter().rect_filled(close, radius::INPUT, c.hover);
192    }
193    paint::cross(ui.painter(), close.center(), 3.5, Stroke::new(1.4, c.text_secondary));
194    response
195}
196
197/// A small status badge (error, warning, suppressed) for the right of a tree row.
198pub fn badge(ui: &egui::Ui, center: egui::Pos2, kind: Kind) {
199    let theme = Theme::of(ui);
200    ui.painter().circle_filled(center, 4.0, kind.color(&theme));
201}
202
203/// One row of the sidebar tree.
204#[derive(Debug, Clone, Copy)]
205pub struct TreeRow<'a> {
206    pub depth: usize,
207    /// The feature type's icon: a glyph of the icon catalog ("" for none).
208    pub glyph: &'a str,
209    pub label: &'a str,
210    pub selected: bool,
211    pub badge: Option<Kind>,
212    /// Suppressed features read in `text.disabled`.
213    pub suppressed: bool,
214}
215
216/// A 24 px tree row: a type icon, the selected row on `accent.muted` with a
217/// 2 px accent left edge, faint indent guides, and a status badge on the right.
218pub fn tree_row(ui: &mut egui::Ui, row: TreeRow<'_>) -> egui::Response {
219    let theme = Theme::of(ui);
220    let c = &theme.colors;
221    let (rect, response) = ui.allocate_exact_size(Vec2::new(ui.available_width(), theme.row_height()), egui::Sense::click());
222    response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, row.selected, row.label));
223    if ui.is_rect_visible(rect) {
224        if row.selected {
225            ui.painter().rect_filled(rect, 0.0, c.accent_muted);
226            ui.painter().rect_filled(Rect::from_min_size(rect.min, Vec2::new(2.0, rect.height())), 0.0, c.accent_ring);
227        }
228        if response.hovered() {
229            ui.painter().rect_filled(rect, 0.0, c.hover);
230        }
231        if response.has_focus() {
232            paint::focus_ring(ui, rect.shrink(2.0), radius::INPUT, &theme);
233        }
234        let indent = space::XL;
235        for level in 0..row.depth {
236            let x = rect.left() + space::LG + indent * level as f32 + 4.0;
237            ui.painter().vline(x, rect.y_range(), Stroke::new(1.0, c.border_subtle));
238        }
239        let mut x = rect.left() + space::LG + indent * row.depth as f32;
240        let ink = if row.suppressed { c.text_disabled } else { c.text_primary };
241        if !row.glyph.is_empty() {
242            paint::icon(ui, row.glyph, Rect::from_min_size(egui::pos2(x, rect.center().y - 8.0), Vec2::splat(16.0)), ink);
243            x += 16.0 + space::SM;
244        }
245        let g = paint::galley(ui, row.label, theme.font(Text::Body));
246        ui.painter().galley(egui::pos2(x, rect.center().y - g.size().y / 2.0), g, ink);
247        if let Some(kind) = row.badge {
248            badge(ui, egui::pos2(rect.right() - space::LG, rect.center().y), kind);
249        }
250    }
251    response
252}