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
//! The shapes the components share: focus ring, tick, cross, chevron,
//! magnifier, spinner and catalogued icons. Marks are drawn as paths, never as
//! font glyphs, so they look the same with any font.
use crate::theme::{Theme, RING};
use eframe::egui::{self, Color32, Pos2, Rect, Stroke, Vec2};

/// A text galley on one line, in the colour given when it is painted.
pub fn galley(ui: &egui::Ui, text: &str, font: egui::FontId) -> std::sync::Arc<egui::Galley> {
    ui.painter().layout_no_wrap(text.to_owned(), font, Color32::PLACEHOLDER)
}

/// The 2 px accent focus ring OUTSIDE `rect`'s border.
pub fn focus_ring(ui: &egui::Ui, rect: Rect, radius: f32, theme: &Theme) {
    ui.painter().rect_stroke(
        rect.expand(1.0),
        radius + 1.0,
        Stroke::new(RING, theme.colors.accent_ring),
        egui::StrokeKind::Outside,
    );
}

/// A check mark filling `rect` (a square).
pub fn tick(painter: &egui::Painter, rect: Rect, stroke: Stroke) {
    let p = |x: f32, y: f32| rect.min + Vec2::new(x * rect.width(), y * rect.height());
    painter.add(egui::Shape::line(vec![p(0.2, 0.52), p(0.42, 0.74), p(0.8, 0.28)], stroke));
}

/// The indeterminate dash.
pub fn dash(painter: &egui::Painter, rect: Rect, stroke: Stroke) {
    let y = rect.center().y;
    painter.line_segment([Pos2::new(rect.left() + rect.width() * 0.24, y), Pos2::new(rect.right() - rect.width() * 0.24, y)], stroke);
}

/// A close cross of half-size `r` at `center`.
pub fn cross(painter: &egui::Painter, center: Pos2, r: f32, stroke: Stroke) {
    painter.line_segment([center + Vec2::new(-r, -r), center + Vec2::new(r, r)], stroke);
    painter.line_segment([center + Vec2::new(-r, r), center + Vec2::new(r, -r)], stroke);
}

/// A chevron of half-width `r` at `center`: pointing down at `turn` 0, up at
/// `turn` 1 (the dropdown's chevron rotates as it opens); `right` points it right.
pub fn chevron(painter: &egui::Painter, center: Pos2, r: f32, turn: f32, stroke: Stroke) {
    let angle = turn * std::f32::consts::PI;
    let rot = |v: Vec2| Vec2::new(v.x * angle.cos() - v.y * angle.sin(), v.x * angle.sin() + v.y * angle.cos());
    let points = [Vec2::new(-r, -r * 0.5), Vec2::new(0.0, r * 0.5), Vec2::new(r, -r * 0.5)].map(|v| center + rot(v));
    painter.add(egui::Shape::line(points.to_vec(), stroke));
}

/// A chevron pointing right (collapsed) that turns down as `open` goes to 1.
pub fn disclosure(painter: &egui::Painter, center: Pos2, r: f32, open: f32, stroke: Stroke) {
    chevron(painter, center, r, -0.5 + 0.5 * open, stroke);
}

/// The search field's magnifier.
pub fn magnifier(painter: &egui::Painter, center: Pos2, r: f32, stroke: Stroke) {
    let lens = center + Vec2::new(-r * 0.2, -r * 0.2);
    painter.circle_stroke(lens, r * 0.62, stroke);
    let d = Vec2::splat(std::f32::consts::FRAC_1_SQRT_2);
    painter.line_segment([lens + d * r * 0.62, lens + d * r * 1.3], stroke);
}

/// The busy spinner: three quarters of a ring that turns, or stands still under
/// reduce-motion. Asks for the next frame while it turns.
pub fn spinner(ui: &egui::Ui, center: Pos2, r: f32, color: Color32, theme: &Theme) {
    let start = if theme.reduce_motion {
        0.0
    } else {
        ui.ctx().request_repaint();
        (ui.input(|i| i.time) * std::f64::consts::TAU * 1.1) as f32
    };
    let points: Vec<Pos2> = (0..=18)
        .map(|i| {
            let a = start + i as f32 / 18.0 * std::f32::consts::TAU * 0.75;
            center + Vec2::new(a.cos(), a.sin()) * r
        })
        .collect();
    ui.painter().add(egui::Shape::line(points, Stroke::new(1.6, color)));
}

/// The catalogued artwork for `glyph` in the square `rect`: monochrome artwork
/// takes `color`, colour artwork keeps its own. Returns whether there was any.
pub fn icon(ui: &egui::Ui, glyph: &str, rect: Rect, color: Color32) -> bool {
    let Some(icon) = crate::icons::artwork(glyph) else { return false };
    egui_extras::install_image_loaders(ui.ctx());
    let mut image = egui::Image::new(egui::ImageSource::Bytes {
        uri: format!("bytes://brep-toolbar/{}.svg", icon.name).into(),
        bytes: egui::load::Bytes::Static(icon.artwork_svg.as_bytes()),
    });
    if icon.mono {
        image = image.tint(color);
    }
    image.paint_at(ui, rect);
    true
}

/// A key chip (`Ctrl+S`) at `min`; returns its rect.
pub fn key_chip(ui: &egui::Ui, min: Pos2, text: &str, theme: &Theme) -> Rect {
    let g = galley(ui, text, theme.font(crate::theme::Text::Caption));
    let rect = Rect::from_min_size(min, g.size() + Vec2::new(8.0, 4.0));
    ui.painter().rect(rect, 3.0, theme.colors.bg_input, Stroke::new(1.0, theme.colors.border_strong), egui::StrokeKind::Inside);
    ui.painter().galley(rect.min + Vec2::new(4.0, 2.0), g, theme.colors.text_secondary);
    rect
}

/// `a` to `b` by `t`, per channel, alpha included.
pub fn lerp_color(a: Color32, b: Color32, t: f32) -> Color32 {
    let t = t.clamp(0.0, 1.0);
    let [ar, ag, ab, aa] = a.to_array();
    let [br, bg, bb, ba] = b.to_array();
    let ch = |x: u8, y: u8| (f32::from(x) + (f32::from(y) - f32::from(x)) * t).round() as u8;
    Color32::from_rgba_premultiplied(ch(ar, br), ch(ag, bg), ch(ab, bb), ch(aa, ba))
}