BREP_app 0.36.1

The next.BREP.io CAD application on the Rust B-rep kernel: an eframe (egui + wgpu) host that draws the brep-render 3D engine into an egui frame, native + wasm from one codebase.
Documentation
//! Small pieces the app's chrome (rollout step 4: cards over the viewport,
//! toolbars, menus, dialogs) shares on top of the components: the overlay
//! card frame and its title / caption / hint text roles. Owned by the
//! UI-CHROME lane; fold into the components when convenient.
use super::surface;
use crate::theme::{radius, space, Text, Theme, BORDER, RING};
use eframe::egui::{self, Rect, Stroke, Vec2};

/// The frame of a card floating over the viewport (the mode-exit card, the
/// pick bar): the popup surface with the card's inner padding.
pub fn overlay_card(theme: &Theme) -> egui::Frame {
    surface::popup_frame(theme).inner_margin(egui::Margin::same(space::MD as i8))
}

/// A card or dialog title: label weight, primary text.
pub fn title(text: impl Into<String>, theme: &Theme) -> egui::RichText {
    egui::RichText::new(text).font(theme.font(Text::Label)).color(theme.colors.text_primary)
}

/// A caption under a title: caption size, secondary text.
pub fn caption(text: impl Into<String>, theme: &Theme) -> egui::RichText {
    egui::RichText::new(text).font(theme.font(Text::Caption)).color(theme.colors.text_secondary)
}

/// Body text in the secondary colour (placeholders like "nothing picked yet").
pub fn muted(text: impl Into<String>, theme: &Theme) -> egui::RichText {
    egui::RichText::new(text).font(theme.font(Text::Body)).color(theme.colors.text_secondary)
}

/// The width [`super::Button`] draws `text` at (its size `Auto`), with or
/// without a leading icon: what a column of buttons is sized to before any
/// is drawn. Kept equal to the button's own layout (a test pins it).
pub fn button_width(ui: &egui::Ui, text: &str, icon: bool) -> f32 {
    let theme = Theme::of(ui);
    let pad = theme.control_padding() + 2.0;
    let galley = super::paint::galley(ui, text, theme.font(Text::Body));
    let lead = if icon { BUTTON_ICON + space::SM } else { 0.0 };
    (pad * 2.0 + lead + galley.size().x).max(theme.control_height())
}

/// `ui_kit::button`'s icon slot.
const BUTTON_ICON: f32 = 16.0;

/// The frame of a dialog (a modal or a window's body): the raised surface,
/// the medium shadow and the card padding.
pub fn dialog_frame(theme: &Theme) -> egui::Frame {
    theme.raised_frame().shadow(theme.shadow_medium()).inner_margin(egui::Margin::same(space::LG as i8))
}

/// A plain menu entry (no icon, no shortcut) that greys with its `Ui`, for
/// a menu whose entries can be disabled.
pub fn menu_entry(ui: &mut egui::Ui, label: &str) -> egui::Response {
    let height = Theme::of(ui).row_height();
    super::chrome_toolbar::ToolRow::new(super::chrome_toolbar::RowIcon::None, label, Vec2::new(ui.available_width(), height)).show(ui).0
}

/// A pick as a removable chip: a pill with the name and a ×, the WHOLE chip
/// the click target (a finger has no room to aim at the ×). Its height is the
/// theme's small control height, so it follows the touch target.
pub fn removable_chip(ui: &mut egui::Ui, name: &str) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let height = theme.control_height_sm();
    let g = super::paint::galley(ui, name, theme.font(Text::Body));
    let width = space::MD + g.size().x + space::SM + CHIP_CROSS + space::SM;
    let (rect, response) = ui.allocate_exact_size(Vec2::new(width, height), egui::Sense::click());
    response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, format!("Remove {name}")));
    if ui.is_rect_visible(rect) {
        let fill = if response.is_pointer_button_down_on() {
            crate::theme::over(c.bg_raised, c.pressed)
        } else if response.hovered() {
            crate::theme::over(c.bg_raised, c.hover)
        } else {
            c.bg_raised
        };
        ui.painter().rect(rect, height / 2.0, fill, Stroke::new(BORDER, c.border_strong), egui::StrokeKind::Inside);
        if response.has_focus() {
            super::paint::focus_ring(ui, rect, height / 2.0, &theme);
        }
        ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.center().y - g.size().y / 2.0), g, c.text_primary);
        let cross = Rect::from_center_size(egui::pos2(rect.right() - space::SM - CHIP_CROSS / 2.0, rect.center().y), Vec2::splat(CHIP_CROSS));
        let ink = if response.hovered() { c.text_primary } else { c.text_secondary };
        super::paint::cross(ui.painter(), cross.center(), 3.0, Stroke::new(1.3, ink));
    }
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

const CHIP_CROSS: f32 = 14.0;

/// A selectable row of a given size (a finger-sized menu entry): hover
/// overlay, the selected row on `accent.muted` with a 2 px accent left edge,
/// the text elided to fit.
pub fn select_row(ui: &mut egui::Ui, size: Vec2, text: &str, selected: bool) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let (rect, response) = ui.allocate_exact_size(size, egui::Sense::click());
    response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, selected, text));
    if ui.is_rect_visible(rect) {
        if selected {
            ui.painter().rect_filled(rect, radius::INPUT, c.accent_muted);
            let edge = Rect::from_min_max(rect.left_top() + Vec2::new(0.0, space::XS), egui::pos2(rect.left() + RING, rect.bottom() - space::XS));
            ui.painter().rect_filled(edge, 1.0, c.accent_ring);
        }
        if response.hovered() {
            let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
            ui.painter().rect_filled(rect, radius::INPUT, overlay);
        }
        let mut job = egui::text::LayoutJob::simple_singleline(text.to_owned(), theme.font(Text::Body), c.text_primary);
        job.wrap = egui::text::TextWrapping::truncate_at_width((rect.width() - space::MD * 2.0).max(0.0));
        let g = ui.fonts_mut(|f| f.layout_job(job));
        ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.center().y - g.size().y / 2.0), g, c.text_primary);
    }
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

/// A square close button of `edge` points: a × that gets a hover square.
pub fn close_square(ui: &mut egui::Ui, edge: f32, label: &str) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let (rect, response) = ui.allocate_exact_size(Vec2::splat(edge), egui::Sense::click());
    response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, label));
    if ui.is_rect_visible(rect) {
        if response.hovered() {
            let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
            ui.painter().rect_filled(rect, radius::BUTTON, overlay);
        }
        if response.has_focus() {
            super::paint::focus_ring(ui, rect, radius::BUTTON, &theme);
        }
        let ink = if response.hovered() { c.text_primary } else { c.text_secondary };
        super::paint::cross(ui.painter(), rect.center(), 4.0, Stroke::new(1.5, ink));
    }
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

/// A button of exactly `size` (a table cell's button, an icon tile), on the
/// secondary look: `bg.raised` with a `border.subtle` outline, the hover and
/// pressed overlays, the selected one on `accent.muted` with an accent
/// outline. `text` is centred in it ("" draws none: the caller paints its own
/// content in [`fill_button_ink`]). Disabled with its `Ui`, like the
/// `egui::Button` it replaces, so `add_enabled_ui` greys it.
pub fn fill_button(ui: &mut egui::Ui, size: Vec2, text: &str, selected: bool) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let (rect, response) = ui.allocate_exact_size(size, egui::Sense::click());
    let enabled = ui.is_enabled();
    response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, enabled, selected, text));
    if ui.is_rect_visible(rect) {
        let base = if selected { crate::theme::over(c.bg_raised, c.accent_muted) } else { c.bg_raised };
        let fill = if !enabled {
            base
        } else if response.is_pointer_button_down_on() {
            crate::theme::over(base, c.pressed)
        } else if response.hovered() {
            crate::theme::over(base, c.hover)
        } else {
            base
        };
        let border = if selected { c.accent_ring } else { c.border_subtle };
        ui.painter().rect(rect, radius::BUTTON, fill, Stroke::new(BORDER, border), egui::StrokeKind::Inside);
        if response.has_focus() {
            super::paint::focus_ring(ui, rect, radius::BUTTON, &theme);
        }
        if !text.is_empty() {
            let mut job = egui::text::LayoutJob::simple_singleline(text.to_owned(), theme.font(Text::Body), fill_button_ink(ui, selected));
            job.wrap = egui::text::TextWrapping::truncate_at_width((rect.width() - 2.0 * space::XS).max(0.0));
            let g = ui.fonts_mut(|f| f.layout_job(job));
            ui.painter().galley(rect.center() - g.size() / 2.0, g, fill_button_ink(ui, selected));
        }
    }
    if enabled { response.on_hover_cursor(egui::CursorIcon::PointingHand) } else { response }
}

/// A ghost button of a bar that keeps the classic row height (the status
/// bar: its height must not change when the button appears, or the 3D view
/// above it would resize with the selection): exactly the `Ui`'s interact
/// height (18 pt on the desktop, the touch target on touch), a hover and
/// pressed overlay, `icon` in `text.secondary` before `text`. Its widget label
/// is "`icon` `text`".
pub fn bar_button(ui: &mut egui::Ui, icon: &str, text: &str) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let font = theme.font(Text::Body);
    let glyph = super::paint::galley(ui, icon, font.clone());
    let label = super::paint::galley(ui, text, font);
    let height = ui.spacing().interact_size.y;
    let width = space::XS + glyph.size().x + space::XS + label.size().x + space::XS;
    let (rect, response) = ui.allocate_exact_size(Vec2::new(width, height), egui::Sense::click());
    let enabled = ui.is_enabled();
    response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, enabled, format!("{icon} {text}")));
    if ui.is_rect_visible(rect) {
        if enabled && response.hovered() {
            let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
            ui.painter().rect_filled(rect, radius::INPUT, overlay);
        }
        if response.has_focus() {
            super::paint::focus_ring(ui, rect, radius::INPUT, &theme);
        }
        let ink = if enabled { c.text_primary } else { c.text_disabled };
        let mut x = rect.left() + space::XS;
        let gy = rect.center().y - glyph.size().y / 2.0;
        let gw = glyph.size().x;
        ui.painter().galley(egui::pos2(x, gy), glyph, if enabled { c.text_secondary } else { c.text_disabled });
        x += gw + space::XS;
        ui.painter().galley(egui::pos2(x, rect.center().y - label.size().y / 2.0), label, ink);
    }
    if enabled { response.on_hover_cursor(egui::CursorIcon::PointingHand) } else { response }
}

/// The text and monochrome-artwork colour inside a [`fill_button`].
pub fn fill_button_ink(ui: &egui::Ui, selected: bool) -> egui::Color32 {
    let c = Theme::of(ui).colors;
    if !ui.is_enabled() {
        c.text_disabled
    } else if selected {
        c.accent_text
    } else {
        c.text_primary
    }
}

/// The colours a feature's status messages read in, from the theme: an
/// error, a partial fulfilment (between the warning and the danger colour), a
/// repair note (the warning colour) and an approximation (always BLUE, the
/// proposed accent's text colour for this mode, so it reads apart from the
/// other three whatever accent the user chose).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct StatusColors {
    pub error: egui::Color32,
    pub partial: egui::Color32,
    pub note: egui::Color32,
    pub info: egui::Color32,
}

pub fn status_colors(theme: &Theme) -> StatusColors {
    let c = &theme.colors;
    StatusColors {
        error: c.danger,
        partial: crate::theme::mix(c.warning, c.danger, 0.5),
        note: c.warning,
        info: c.info,
    }
}