BREP_app 0.35.0

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
//! Surfaces: cards and sections, panel headers, popup and menu rows, the
//! dialog frame, toasts and tree rows.
use super::{paint, Button};
use crate::theme::{radius, space, Text, Theme, BORDER};
use eframe::egui::{self, Color32, Rect, Stroke, Vec2};

/// The frame of a popup, menu or context menu: `bg.raised`, the small shadow.
pub fn popup_frame(theme: &Theme) -> egui::Frame {
    theme.raised_frame().shadow(theme.shadow_small()).inner_margin(egui::Margin::same(space::XS as i8))
}

/// A card: `bg.raised`, radius 8, a 1 px `border.subtle` border, 12 px padding.
pub fn card<R>(ui: &mut egui::Ui, contents: impl FnOnce(&mut egui::Ui) -> R) -> egui::InnerResponse<R> {
    let theme = Theme::of(ui);
    theme.raised_frame().inner_margin(egui::Margin::same(space::LG as i8)).show(ui, contents)
}

/// A section: a card under a section header.
pub fn section<R>(ui: &mut egui::Ui, title: &str, contents: impl FnOnce(&mut egui::Ui) -> R) -> egui::InnerResponse<R> {
    let theme = Theme::of(ui);
    card(ui, |ui| {
        ui.label(egui::RichText::new(title).font(theme.font(Text::Section)).color(theme.colors.text_primary));
        ui.add_space(space::SM);
        contents(ui)
    })
}

/// One row of a section's two-column grid: the label left, the control right.
pub fn field_row<R>(ui: &mut egui::Ui, label: &str, label_width: f32, control: impl FnOnce(&mut egui::Ui) -> R) -> R {
    let theme = Theme::of(ui);
    ui.horizontal(|ui| {
        let g = paint::galley(ui, label, theme.font(Text::Label));
        let (rect, _) = ui.allocate_exact_size(Vec2::new(label_width, theme.control_height()), egui::Sense::hover());
        ui.painter().galley(egui::pos2(rect.left(), rect.center().y - g.size().y / 2.0), g, theme.colors.text_secondary);
        control(ui)
    })
    .inner
}

/// A panel's header row: a collapse chevron, the title, and optional actions
/// (ghost icon buttons) on the right. Returns the header's response; a click
/// toggles `collapsed`.
pub fn panel_header(ui: &mut egui::Ui, title: &str, collapsed: &mut bool, actions: impl FnOnce(&mut egui::Ui)) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let height = theme.control_height() + 6.0;
    let (rect, response) = ui.allocate_exact_size(Vec2::new(ui.available_width(), height), egui::Sense::click());
    response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::CollapsingHeader, true, title));
    if response.clicked() {
        *collapsed = !*collapsed;
    }
    if response.hovered() {
        ui.painter().rect_filled(rect, 0.0, c.hover);
    }
    let open = ui.ctx().animate_bool_with_time(response.id.with("open"), !*collapsed, theme.motion_base());
    paint::disclosure(ui.painter(), egui::pos2(rect.left() + space::LG, rect.center().y), 3.5, open, Stroke::new(1.5, c.text_secondary));
    let g = paint::galley(ui, title, theme.font(Text::Section));
    ui.painter().galley(egui::pos2(rect.left() + space::XXL, rect.center().y - g.size().y / 2.0), g, c.text_primary);
    ui.painter().hline(rect.x_range(), rect.bottom() - 0.5, Stroke::new(1.0, c.border_subtle));
    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)));
    actions(&mut right);
    response
}

/// A caption header inside a menu or dropdown.
pub fn menu_header(ui: &mut egui::Ui, text: &str) {
    let theme = Theme::of(ui);
    let g = paint::galley(ui, text, theme.font(Text::Caption));
    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());
    ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.bottom() - g.size().y - 2.0), g, theme.colors.text_secondary);
}

/// A menu separator.
pub fn menu_separator(ui: &mut egui::Ui) {
    let theme = Theme::of(ui);
    let (rect, _) = ui.allocate_exact_size(Vec2::new(ui.available_width(), space::SM + 1.0), egui::Sense::hover());
    ui.painter().hline(rect.x_range(), rect.center().y, Stroke::new(1.0, theme.colors.border_subtle));
}

/// A menu row: 28 px, hover background, shortcut text on the right, an accent
/// check when `checked`.
pub fn menu_row(ui: &mut egui::Ui, text: &str, shortcut: Option<&str>, checked: bool) -> egui::Response {
    menu_row_with(ui, text, shortcut, checked, false)
}

/// [`menu_row`] with the keyboard cursor on it.
pub fn menu_row_with(ui: &mut egui::Ui, text: &str, shortcut: Option<&str>, checked: bool, cursor: bool) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let g = paint::galley(ui, text, theme.font(Text::Body));
    let keys = shortcut.map(|s| paint::galley(ui, s, theme.font(Text::Caption)));
    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;
    let (rect, response) = ui.allocate_exact_size(Vec2::new(ui.available_width().max(need), 28.0), egui::Sense::click());
    response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, checked, text));
    if ui.is_rect_visible(rect) {
        if response.hovered() || cursor {
            let overlay = if response.is_pointer_button_down_on() { c.pressed } else { c.hover };
            ui.painter().rect_filled(rect, radius::INPUT, overlay);
        }
        ui.painter().galley(egui::pos2(rect.left() + space::MD, rect.center().y - g.size().y / 2.0), g, c.text_primary);
        let mut right = rect.right() - space::SM;
        if checked {
            let mark = Rect::from_center_size(egui::pos2(right - 8.0, rect.center().y), Vec2::splat(14.0));
            paint::tick(ui.painter(), mark, Stroke::new(1.8, c.accent_text));
        }
        right -= 16.0 + space::SM;
        if let Some(k) = keys {
            ui.painter().galley(egui::pos2(right - k.size().x, rect.center().y - k.size().y / 2.0), k, c.text_secondary);
        }
    }
    response.on_hover_cursor(egui::CursorIcon::PointingHand)
}

/// What a dialog's footer reported.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct DialogOutput {
    pub confirmed: bool,
    pub cancelled: bool,
}

/// The dialog look: the medium shadow, a title row, the content, and a footer
/// with the primary button on the right and the secondary to its left. Drawn
/// in place (the gallery, and the body of an `egui::Window` in step 4).
pub fn dialog(ui: &mut egui::Ui, title: &str, primary: &str, secondary: &str, width: f32, contents: impl FnOnce(&mut egui::Ui)) -> DialogOutput {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let mut out = DialogOutput::default();
    theme.raised_frame().shadow(theme.shadow_medium()).show(ui, |ui| {
        ui.set_width(width);
        ui.spacing_mut().item_spacing.y = 0.0;
        egui::Frame::new().inner_margin(egui::Margin::symmetric(space::XL as i8, space::LG as i8)).show(ui, |ui| {
            ui.set_width(width - space::XL * 2.0);
            ui.label(egui::RichText::new(title).font(theme.font(Text::Title)).color(c.text_primary));
        });
        let (line, _) = ui.allocate_exact_size(Vec2::new(width, 1.0), egui::Sense::hover());
        ui.painter().hline(line.x_range(), line.center().y, Stroke::new(1.0, c.border_subtle));
        egui::Frame::new().inner_margin(egui::Margin::same(space::XL as i8)).show(ui, |ui| {
            ui.set_width(width - space::XL * 2.0);
            ui.spacing_mut().item_spacing.y = space::SM;
            contents(ui);
        });
        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| {
            ui.set_width(width - space::XL * 2.0);
            let row = Vec2::new(width - space::XL * 2.0, theme.control_height());
            ui.allocate_ui_with_layout(row, egui::Layout::right_to_left(egui::Align::Center), |ui| {
                ui.spacing_mut().item_spacing.x = space::MD;
                out.confirmed = ui.add(Button::primary(primary).min_width(72.0)).clicked();
                out.cancelled = ui.add(Button::new(secondary).min_width(72.0)).clicked();
            });
        });
    });
    out
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
    Info,
    Success,
    Warning,
    Error,
}

impl Kind {
    pub fn color(self, theme: &Theme) -> Color32 {
        match self {
            Kind::Info => theme.colors.accent_ring,
            Kind::Success => theme.colors.success,
            Kind::Warning => theme.colors.warning,
            Kind::Error => theme.colors.danger,
        }
    }
}

/// A toast card: a coloured left bar for the kind, the text, a close ×.
/// Returns the × response.
pub fn toast(ui: &mut egui::Ui, kind: Kind, text: &str, width: f32) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let g = ui.painter().layout(text.to_owned(), theme.font(Text::Body), Color32::PLACEHOLDER, width - 4.0 - space::LG * 2.0 - 20.0);
    let height = (g.size().y + space::MD * 2.0).max(36.0);
    let (rect, _) = ui.allocate_exact_size(Vec2::new(width, height), egui::Sense::hover());
    ui.painter().add(theme.shadow_small().as_shape(rect, radius::CARD));
    ui.painter().rect(rect, radius::CARD, c.bg_raised, Stroke::new(BORDER, c.border_subtle), egui::StrokeKind::Inside);
    let bar = Rect::from_min_max(rect.min + Vec2::new(1.0, 1.0), egui::pos2(rect.left() + 4.0, rect.bottom() - 1.0));
    ui.painter().rect_filled(bar, egui::CornerRadius { nw: 7, sw: 7, ne: 0, se: 0 }, kind.color(&theme));
    ui.painter().galley(egui::pos2(rect.left() + 4.0 + space::LG, rect.center().y - g.size().y / 2.0), g, c.text_primary);
    let close = Rect::from_center_size(egui::pos2(rect.right() - space::LG - 2.0, rect.center().y), Vec2::splat(18.0));
    let response = ui.interact(close, ui.next_auto_id().with("toast-close"), egui::Sense::click());
    response.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, "Dismiss"));
    if response.hovered() {
        ui.painter().rect_filled(close, radius::INPUT, c.hover);
    }
    paint::cross(ui.painter(), close.center(), 3.5, Stroke::new(1.4, c.text_secondary));
    response
}

/// A small status badge (error, warning, suppressed) for the right of a tree row.
pub fn badge(ui: &egui::Ui, center: egui::Pos2, kind: Kind) {
    let theme = Theme::of(ui);
    ui.painter().circle_filled(center, 4.0, kind.color(&theme));
}

/// One row of the sidebar tree.
#[derive(Debug, Clone, Copy)]
pub struct TreeRow<'a> {
    pub depth: usize,
    /// The feature type's icon: a glyph of the icon catalog ("" for none).
    pub glyph: &'a str,
    pub label: &'a str,
    pub selected: bool,
    pub badge: Option<Kind>,
    /// Suppressed features read in `text.disabled`.
    pub suppressed: bool,
}

/// A 24 px tree row: a type icon, the selected row on `accent.muted` with a
/// 2 px accent left edge, faint indent guides, and a status badge on the right.
pub fn tree_row(ui: &mut egui::Ui, row: TreeRow<'_>) -> egui::Response {
    let theme = Theme::of(ui);
    let c = &theme.colors;
    let (rect, response) = ui.allocate_exact_size(Vec2::new(ui.available_width(), theme.row_height()), egui::Sense::click());
    response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::SelectableLabel, true, row.selected, row.label));
    if ui.is_rect_visible(rect) {
        if row.selected {
            ui.painter().rect_filled(rect, 0.0, c.accent_muted);
            ui.painter().rect_filled(Rect::from_min_size(rect.min, Vec2::new(2.0, rect.height())), 0.0, c.accent_ring);
        }
        if response.hovered() {
            ui.painter().rect_filled(rect, 0.0, c.hover);
        }
        if response.has_focus() {
            paint::focus_ring(ui, rect.shrink(2.0), radius::INPUT, &theme);
        }
        let indent = space::XL;
        for level in 0..row.depth {
            let x = rect.left() + space::LG + indent * level as f32 + 4.0;
            ui.painter().vline(x, rect.y_range(), Stroke::new(1.0, c.border_subtle));
        }
        let mut x = rect.left() + space::LG + indent * row.depth as f32;
        let ink = if row.suppressed { c.text_disabled } else { c.text_primary };
        if !row.glyph.is_empty() {
            paint::icon(ui, row.glyph, Rect::from_min_size(egui::pos2(x, rect.center().y - 8.0), Vec2::splat(16.0)), ink);
            x += 16.0 + space::SM;
        }
        let g = paint::galley(ui, row.label, theme.font(Text::Body));
        ui.painter().galley(egui::pos2(x, rect.center().y - g.size().y / 2.0), g, ink);
        if let Some(kind) = row.badge {
            badge(ui, egui::pos2(rect.right() - space::LG, rect.center().y), kind);
        }
    }
    response
}