Skip to main content

brep_app/ui_kit/
paint.rs

1//! The shapes the components share: focus ring, tick, cross, chevron,
2//! magnifier, spinner and catalogued icons. Marks are drawn as paths, never as
3//! font glyphs, so they look the same with any font.
4use crate::theme::{Theme, RING};
5use eframe::egui::{self, Color32, Pos2, Rect, Stroke, Vec2};
6
7/// A text galley on one line, in the colour given when it is painted.
8pub fn galley(ui: &egui::Ui, text: &str, font: egui::FontId) -> std::sync::Arc<egui::Galley> {
9    ui.painter().layout_no_wrap(text.to_owned(), font, Color32::PLACEHOLDER)
10}
11
12/// The 2 px accent focus ring OUTSIDE `rect`'s border.
13pub fn focus_ring(ui: &egui::Ui, rect: Rect, radius: f32, theme: &Theme) {
14    ui.painter().rect_stroke(
15        rect.expand(1.0),
16        radius + 1.0,
17        Stroke::new(RING, theme.colors.accent_ring),
18        egui::StrokeKind::Outside,
19    );
20}
21
22/// A check mark filling `rect` (a square).
23pub fn tick(painter: &egui::Painter, rect: Rect, stroke: Stroke) {
24    let p = |x: f32, y: f32| rect.min + Vec2::new(x * rect.width(), y * rect.height());
25    painter.add(egui::Shape::line(vec![p(0.2, 0.52), p(0.42, 0.74), p(0.8, 0.28)], stroke));
26}
27
28/// The indeterminate dash.
29pub fn dash(painter: &egui::Painter, rect: Rect, stroke: Stroke) {
30    let y = rect.center().y;
31    painter.line_segment([Pos2::new(rect.left() + rect.width() * 0.24, y), Pos2::new(rect.right() - rect.width() * 0.24, y)], stroke);
32}
33
34/// A close cross of half-size `r` at `center`.
35pub fn cross(painter: &egui::Painter, center: Pos2, r: f32, stroke: Stroke) {
36    painter.line_segment([center + Vec2::new(-r, -r), center + Vec2::new(r, r)], stroke);
37    painter.line_segment([center + Vec2::new(-r, r), center + Vec2::new(r, -r)], stroke);
38}
39
40/// A chevron of half-width `r` at `center`: pointing down at `turn` 0, up at
41/// `turn` 1 (the dropdown's chevron rotates as it opens); `right` points it right.
42pub fn chevron(painter: &egui::Painter, center: Pos2, r: f32, turn: f32, stroke: Stroke) {
43    let angle = turn * std::f32::consts::PI;
44    let rot = |v: Vec2| Vec2::new(v.x * angle.cos() - v.y * angle.sin(), v.x * angle.sin() + v.y * angle.cos());
45    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));
46    painter.add(egui::Shape::line(points.to_vec(), stroke));
47}
48
49/// A chevron pointing right (collapsed) that turns down as `open` goes to 1.
50pub fn disclosure(painter: &egui::Painter, center: Pos2, r: f32, open: f32, stroke: Stroke) {
51    chevron(painter, center, r, -0.5 + 0.5 * open, stroke);
52}
53
54/// The search field's magnifier.
55pub fn magnifier(painter: &egui::Painter, center: Pos2, r: f32, stroke: Stroke) {
56    let lens = center + Vec2::new(-r * 0.2, -r * 0.2);
57    painter.circle_stroke(lens, r * 0.62, stroke);
58    let d = Vec2::splat(std::f32::consts::FRAC_1_SQRT_2);
59    painter.line_segment([lens + d * r * 0.62, lens + d * r * 1.3], stroke);
60}
61
62/// The busy spinner: three quarters of a ring that turns, or stands still under
63/// reduce-motion. Asks for the next frame while it turns.
64pub fn spinner(ui: &egui::Ui, center: Pos2, r: f32, color: Color32, theme: &Theme) {
65    let start = if theme.reduce_motion {
66        0.0
67    } else {
68        ui.ctx().request_repaint();
69        (ui.input(|i| i.time) * std::f64::consts::TAU * 1.1) as f32
70    };
71    let points: Vec<Pos2> = (0..=18)
72        .map(|i| {
73            let a = start + i as f32 / 18.0 * std::f32::consts::TAU * 0.75;
74            center + Vec2::new(a.cos(), a.sin()) * r
75        })
76        .collect();
77    ui.painter().add(egui::Shape::line(points, Stroke::new(1.6, color)));
78}
79
80/// The catalogued artwork for `glyph` in the square `rect`: monochrome artwork
81/// takes `color`, colour artwork keeps its own. Returns whether there was any.
82pub fn icon(ui: &egui::Ui, glyph: &str, rect: Rect, color: Color32) -> bool {
83    let Some(icon) = crate::icons::artwork(glyph) else { return false };
84    egui_extras::install_image_loaders(ui.ctx());
85    let mut image = egui::Image::new(egui::ImageSource::Bytes {
86        uri: format!("bytes://brep-toolbar/{}.svg", icon.name).into(),
87        bytes: egui::load::Bytes::Static(icon.artwork_svg.as_bytes()),
88    });
89    if icon.mono {
90        image = image.tint(color);
91    }
92    image.paint_at(ui, rect);
93    true
94}
95
96/// A key chip (`Ctrl+S`) at `min`; returns its rect.
97pub fn key_chip(ui: &egui::Ui, min: Pos2, text: &str, theme: &Theme) -> Rect {
98    let g = galley(ui, text, theme.font(crate::theme::Text::Caption));
99    let rect = Rect::from_min_size(min, g.size() + Vec2::new(8.0, 4.0));
100    ui.painter().rect(rect, 3.0, theme.colors.bg_input, Stroke::new(1.0, theme.colors.border_strong), egui::StrokeKind::Inside);
101    ui.painter().galley(rect.min + Vec2::new(4.0, 2.0), g, theme.colors.text_secondary);
102    rect
103}
104
105/// `a` to `b` by `t`, per channel, alpha included.
106pub fn lerp_color(a: Color32, b: Color32, t: f32) -> Color32 {
107    let t = t.clamp(0.0, 1.0);
108    let [ar, ag, ab, aa] = a.to_array();
109    let [br, bg, bb, ba] = b.to_array();
110    let ch = |x: u8, y: u8| (f32::from(x) + (f32::from(y) - f32::from(x)) * t).round() as u8;
111    Color32::from_rgba_premultiplied(ch(ar, br), ch(ag, bg), ch(ab, bb), ch(aa, ba))
112}