brep_app/panels/toolbar_button.rs
1//! The SINGLE source of truth for toolbar-button styling — shared by the main
2//! toolbar (`toolbar.rs`), the context bar (`context_bar.rs`), and the 2D sketcher
3//! toolbar (`sketch.rs`). Every toolbar button goes through here, so sizing/style
4//! changes land in ONE place. The human label is in the hover TOOLTIP, and every
5//! button is SQUARE (min-width == height).
6//!
7//! Artwork is ALWAYS the catalogued SVG ([`crate::icons`]) — colour icons paint
8//! their own colours, monochrome ones are tinted to the button's live text
9//! colour. There is no font path: this app ships no icon font, and a glyph
10//! character is only ever a KEY into the catalog.
11//!
12//! There used to be two other routes. Several private-use font glyphs painted at
13//! one origin in different colours was the only way to get multi-colour artwork
14//! out of a monochrome TrueType face; that went when each became a single colour
15//! SVG. Then monochrome glyphs were left to `BrepIcons.ttf` on the reasoning that
16//! a rasterised image "would buy nothing" — true in isolation, but it was the
17//! last thing keeping a whole font in the binary, so it went too.
18use crate::ui_kit::chrome_toolbar::{LargeButton, RowIcon, ToolButton, ToolRow};
19use eframe::egui;
20
21/// The catalogued artwork for a button glyph — what lets a plain
22/// `button`/`button_enabled`/`toggle` call site render its icon with no per-site
23/// plumbing, so the toolbar, the workbench registry and the sketch tool row all
24/// light up from the glyph alone. Shared with the tree and palette rows so all
25/// three agree on what is an icon.
26use crate::icons::artwork;
27
28/// Square edge length of a single-glyph toolbar button (min-width == height).
29pub const TOOLBAR_BTN: f32 = 28.0;
30
31/// Height of a Ribbon Large button; the row height every ribbon group reserves.
32pub const LARGE_BTN: f32 = 64.0;
33
34/// Shared flow layout for toolbar controls in every workbench. Individual
35/// controls wrap at the available width; nested non-wrapping groups would hide
36/// their buttons on narrow windows. The parent panel grows to contain each row.
37pub fn wrapped<R>(ui: &mut egui::Ui, contents: impl FnOnce(&mut egui::Ui) -> R) -> egui::InnerResponse<R> {
38 ui.horizontal_wrapped(contents)
39}
40
41fn square(ui: &egui::Ui) -> egui::Vec2 {
42 crate::viewport::touch::target_size(ui.ctx(), egui::vec2(TOOLBAR_BTN, TOOLBAR_BTN))
43}
44
45/// The button for one glyph: its catalogued artwork if it has any, else the
46/// characters themselves as text, on a touch-sized square.
47///
48/// Monochrome artwork is drawn in the button's live text colour (primary, or
49/// disabled), so hover, pressed and disabled states read as they did; colour
50/// artwork keeps its own colours and is only dimmed when disabled.
51pub(crate) fn glyph_button<'a>(ui: &egui::Ui, glyph: &'a str) -> ToolButton<'a> {
52 ToolButton::new(glyph, square(ui))
53}
54
55/// A plain action toolbar button: `glyph` shown, `tooltip` on hover. Square.
56pub fn button(ui: &mut egui::Ui, glyph: &str, tooltip: &str) -> egui::Response {
57 let button = glyph_button(ui, glyph);
58 ui.add(button).on_hover_text(tooltip)
59}
60
61/// Ribbon's Large presentation: catalogue artwork above the canonical label.
62pub fn large_button(ui: &mut egui::Ui, glyph: &str, label: &str, selected: bool, toggle: bool, enabled: bool) -> egui::Response {
63 ui.add_enabled(enabled, LargeButton::new(glyph, label, LARGE_BTN).selected(toggle && selected))
64}
65
66/// A toolbar button enabled only when `enabled` (undo/redo, selection actions).
67pub fn button_enabled(
68 ui: &mut egui::Ui,
69 enabled: bool,
70 glyph: &str,
71 tooltip: &str,
72) -> egui::Response {
73 let button = glyph_button(ui, glyph);
74 ui.add_enabled(enabled, button).on_hover_text(tooltip)
75}
76
77/// A toolbar TOGGLE button (draw-tool selection, wireframe): pressed when `selected`.
78pub fn toggle(ui: &mut egui::Ui, selected: bool, glyph: &str, tooltip: &str) -> egui::Response {
79 let button = glyph_button(ui, glyph).selected(selected);
80 ui.add(button).on_hover_text(tooltip)
81}
82
83/// Disable a toggle as one atomic widget, preserving the wrapping row's layout.
84pub fn toggle_enabled(ui: &mut egui::Ui, enabled: bool, selected: bool, glyph: &str, tooltip: &str) -> egui::Response {
85 ui.add_enabled(enabled, glyph_button(ui, glyph).selected(selected)).on_hover_text(tooltip)
86}
87
88/// The outcome of a toolbar [`select`] combo for one frame.
89pub struct SelectResult {
90 /// The option `id` the user picked THIS frame — `Some` only on a real change
91 /// (differs from `current`); `None` otherwise.
92 pub changed: Option<String>,
93 /// The combo HEADER's screen rect (the verifier clicks this to open the menu).
94 pub header_rect: egui::Rect,
95 /// `(id, rect)` per menu item while the menu is OPEN (empty when closed) — the
96 /// verifier clicks an item rect to drive a SELECTION.
97 pub item_rects: Vec<(String, egui::Rect)>,
98}
99
100/// Painted edge of the workbench icon on one switcher row.
101const SELECT_ICON: f32 = 18.0;
102
103/// The switcher header's "there is a menu here" chevron (U+25BE) and the tick
104/// beside the ACTIVE workbench (U+2713). Both are catalogued artwork, like every
105/// other picture in this module: nothing here renders a character as text, so no
106/// font has to have it. They used to be `right_text("▾")` / `right_text("✓")`,
107/// which drew correctly only where the OS monospace happened to carry them —
108/// on wasm, where `fonts::install` falls back to egui's four bundled faces,
109/// NEITHER codepoint is covered (`Ubuntu-Light`/`NotoEmoji`/`emoji-icon-font`
110/// have no U+25BE, and nothing bundled has U+2713 at all), so both rendered as
111/// a tofu box in the browser build.
112const SELECT_CHEVRON: &str = "\u{25BE}";
113const SELECT_TICK: &str = "\u{2713}";
114
115/// One switcher row: the workbench's own icon at its canvas proportions, its
116/// label, and a right-aligned marker SLOT (the chevron on the closed header,
117/// the tick on the active item). Every row reserves the slot, so the header and
118/// every item put their labels at the same x and their markers in one column.
119fn switcher_row<'a>(label: &'a str, glyph: &'a str, width: f32) -> ToolRow<'a> {
120 assert!(artwork(glyph).is_some(), "workbench icon must be catalogued");
121 ToolRow::new(RowIcon::Workbench(glyph, SELECT_ICON), label, egui::vec2(width, TOOLBAR_BTN))
122}
123
124/// Draw one switcher row `width` wide with `marker` (if any) in its slot.
125fn switcher_row_ui(
126 ui: &mut egui::Ui,
127 label: &str,
128 glyph: &str,
129 width: f32,
130 selected: bool,
131 marker: Option<&str>,
132) -> egui::Response {
133 if let Some(marker) = marker {
134 assert!(artwork(marker).is_some(), "switcher marker must be catalogued");
135 }
136 switcher_row(label, glyph, width).selected(selected).marker(marker).show(ui).0
137}
138
139/// The width the switcher is drawn at: the NATURAL width of its widest option,
140/// so the longest label fits exactly and nothing carries dead space. Every row
141/// is measured with its marker slot, so the header's chevron and an item's
142/// tick are both accounted for whichever row is widest.
143fn switcher_width(ui: &egui::Ui, options: &[(&str, &str, &str)]) -> f32 {
144 options
145 .iter()
146 .fold(0.0_f32, |widest, (label, _, glyph)| widest.max(ToolRow::width(ui, RowIcon::Workbench(glyph, SELECT_ICON), label, true)))
147}
148
149/// An icon-and-label toolbar dropdown, as tall as the surrounding buttons and
150/// sized to its current label; the menu fits its widest option.
151/// Options are `(label, id, glyph)` entries from the workbench registry.
152pub fn select(
153 ui: &mut egui::Ui,
154 id_source: &str,
155 current: &str,
156 options: &[(&str, &str, &str)],
157) -> SelectResult {
158 egui_extras::install_image_loaders(ui.ctx());
159 let width = switcher_width(ui, options);
160 let current_option = options.iter().find(|(_, id, _)| *id == current)
161 .expect("selected workbench must be registered");
162 let header_width = switcher_width(ui, &[*current_option]);
163 let mut changed = None;
164 let mut item_rects = Vec::new();
165 let header = ui
166 .push_id(("toolbar-select", id_source), |ui| {
167 let (label, _, glyph) = current_option;
168 let phone = crate::viewport::touch::enabled(ui.ctx())
169 && ui.ctx().content_rect().width() < crate::viewport::touch::NARROW_WIDTH;
170 let response = if phone {
171 button(ui, glyph, &format!("{label} — Switch workbench"))
172 } else {
173 switcher_row_ui(ui, label, glyph, header_width, false, Some(SELECT_CHEVRON))
174 .on_hover_text("Switch workbench")
175 };
176 egui::Popup::menu(&response).width(width).show(|ui| {
177 let height = (ui.ctx().content_rect().height() - 80.0).max(80.0);
178 ui.set_max_height(height);
179 egui::ScrollArea::vertical()
180 .max_height(height)
181 .show(ui, |ui| {
182 for (label, id, glyph) in options {
183 let selected = *id == current;
184 let marker = selected.then_some(SELECT_TICK);
185 let resp = switcher_row_ui(ui, label, glyph, width, selected, marker);
186 item_rects.push(((*id).to_string(), resp.rect));
187 if resp.clicked() {
188 if !selected {
189 changed = Some((*id).to_string());
190 }
191 ui.close();
192 }
193 }
194 });
195 });
196 response
197 })
198 .inner;
199 SelectResult {
200 changed,
201 header_rect: header.rect,
202 item_rects,
203 }
204}
205
206/// One MENU row: a toolbar button's own artwork and its human label, as a
207/// selectable row inside a popup. This is what a toolbar menu entry
208/// looks like — the same glyph it draws in the row, plus the label that is only
209/// a tooltip while it is on the strip.
210///
211/// `selected` is the entry's live PRESSED state, so menus and toolbar buttons
212/// communicate their current state with the same selected fill.
213///
214/// No marker slot, unlike [`switcher_row`]: a menu of toolbar buttons has no
215/// "which one is active" tick to reserve room for — several can be pressed at
216/// once (the three display toggles), and the pressed FILL says so. Every
217/// icon's artwork takes the same square slot, so the labels of a menu start at
218/// one x (lane U audit item 6); a glyph with no artwork is drawn as text
219/// before the label, as it always was.
220pub(crate) fn menu_row_widget<'a>(glyph: &'a str, label: &'a str, width: f32, selected: bool) -> MenuRow<'a> {
221 MenuRow { glyph, label, width, selected }
222}
223
224/// A [`menu_row_widget`], an `egui::Widget` so it can be added disabled.
225pub(crate) struct MenuRow<'a> {
226 glyph: &'a str,
227 label: &'a str,
228 width: f32,
229 selected: bool,
230}
231
232impl egui::Widget for MenuRow<'_> {
233 fn ui(self, ui: &mut egui::Ui) -> egui::Response {
234 let size = egui::vec2(self.width, TOOLBAR_BTN);
235 if artwork(self.glyph).is_some() {
236 ToolRow::new(RowIcon::Glyph(self.glyph), self.label, size).selected(self.selected).show(ui).0
237 } else {
238 let text = format!("{} {}", self.glyph, self.label);
239 ToolRow::new(RowIcon::None, &text, size).selected(self.selected).show(ui).0
240 }
241 }
242}
243
244/// Draw one overflow-menu row `width` wide. Returns its response, so the caller
245/// publishes the hit-rect and reads the click.
246pub fn menu_row(
247 ui: &mut egui::Ui,
248 glyph: &str,
249 label: &str,
250 selected: bool,
251 width: f32,
252) -> egui::Response {
253 ui.add(menu_row_widget(glyph, label, width, selected))
254}
255
256/// The natural width of one menu row.
257fn menu_row_width(ui: &egui::Ui, glyph: &str, label: &str) -> f32 {
258 if artwork(glyph).is_some() {
259 ToolRow::width(ui, RowIcon::Glyph(glyph), label, false)
260 } else {
261 ToolRow::width(ui, RowIcon::None, &format!("{glyph} {label}"), false)
262 }
263}
264
265/// The width an overflow menu of `rows` — `(glyph, label)` pairs — is drawn at:
266/// the NATURAL width of its widest row, by the rows' own measure.
267pub fn menu_width(ui: &egui::Ui, rows: &[(&str, &str)]) -> f32 {
268 menu_width_salted(ui, "menu-measure", rows)
269}
270
271/// [`menu_width`] for a menu that measures two columns' rows in one frame
272/// (the File menu's workbench and document columns). The measure registers
273/// no widget any more, so the salt is kept only for its callers.
274pub fn menu_width_salted(ui: &egui::Ui, _salt: &str, rows: &[(&str, &str)]) -> f32 {
275 rows.iter().fold(0.0_f32, |widest, (glyph, label)| widest.max(menu_row_width(ui, glyph, label)))
276}
277