Skip to main content

egui_pop_core/kit/
paint.rs

1//! What kit handlers paint with: boxes, indicators, focus rings and
2//! labels, on whole physical pixels.
3
4use egui::epaint::{CornerRadius, StrokeKind};
5use egui::{Rect, Stroke, Vec2, pos2};
6
7use super::{Align, BoxStyle, ComponentKit, KitCx, Part, Side};
8
9/// Paints a box: its fill and border with `corners` rounded by its
10/// radius, and its indicator on `side`. Strokes are whole physical
11/// pixels.
12pub fn paint_box(cx: &KitCx<'_>, rect: Rect, b: &BoxStyle, corners: Corners, side: Side) {
13    let r = b.radius.round().clamp(0.0, 255.0) as u8;
14    let radius = corners.radius(r);
15    if b.fill.a() > 0 {
16        cx.painter.rect_filled(rect, radius, b.fill);
17    }
18    if b.border_width > 0.0 && b.border_color.a() > 0 {
19        let w = crate::scale::snap(b.border_width, cx.ppp);
20        cx.painter.rect_stroke(
21            rect,
22            radius,
23            Stroke::new(w, b.border_color),
24            StrokeKind::Inside,
25        );
26    }
27    paint_indicator(cx, rect, b, side);
28}
29
30pub fn paint_indicator(cx: &KitCx<'_>, rect: Rect, b: &BoxStyle, side: Side) {
31    if b.indicator_size <= 0.0 || b.indicator_color.a() == 0 {
32        return;
33    }
34    let s = crate::scale::snap_size(b.indicator_size, cx.ppp);
35    let bar = match side {
36        Side::Top => Rect::from_min_max(rect.min, pos2(rect.max.x, rect.min.y + s)),
37        Side::Bottom => Rect::from_min_max(pos2(rect.min.x, rect.max.y - s), rect.max),
38        Side::Left => Rect::from_min_max(rect.min, pos2(rect.min.x + s, rect.max.y)),
39        Side::Right => Rect::from_min_max(pos2(rect.max.x - s, rect.min.y), rect.max),
40    };
41    cx.painter.rect_filled(bar, 0.0, b.indicator_color);
42}
43
44/// Paints a box's focus ring, if its state gives one: an outline
45/// `ring_offset` outside the box, following its corners. Every handler
46/// calls it last, so the ring is above the box and its label.
47pub fn paint_ring(cx: &KitCx<'_>, rect: Rect, b: &BoxStyle, corners: Corners) {
48    if b.ring_width <= 0.0 || b.ring_color.a() == 0 {
49        return;
50    }
51    let w = crate::scale::snap(b.ring_width, cx.ppp);
52    // The outline's corners keep the box's centre: larger outside it,
53    // smaller inside (a ring over the border has the border's corners).
54    let r = (b.radius + b.ring_offset).round().clamp(0.0, 255.0) as u8;
55    cx.painter.rect_stroke(
56        rect.expand(b.ring_offset),
57        corners.radius(r),
58        Stroke::new(w, b.ring_color),
59        StrokeKind::Outside,
60    );
61}
62
63/// Which corners of a box are rounded.
64#[derive(Clone, Copy, Debug, PartialEq, Eq)]
65pub enum Corners {
66    All,
67    Top,
68    None,
69}
70
71impl Corners {
72    pub fn radius(self, r: u8) -> CornerRadius {
73        match self {
74            Corners::All => CornerRadius::same(r),
75            Corners::Top => CornerRadius {
76                nw: r,
77                ne: r,
78                sw: 0,
79                se: 0,
80            },
81            Corners::None => CornerRadius::ZERO,
82        }
83    }
84}
85
86/// Where a label of `size` goes in a part: along it by the component's
87/// `align`, inside its room less the padding, centred across; on whole
88/// physical pixels is the painter's business.
89pub fn label_rect(data: &ComponentKit, part: &Part<'_>, size: Vec2) -> Rect {
90    let inner = part.room.shrink2(Vec2::new(data.padding_x, 0.0));
91    let x = match data.align {
92        Align::Start => inner.left(),
93        Align::End => inner.right() - size.x,
94        Align::Centre => (part.rect.center().x - size.x / 2.0)
95            .clamp(inner.left(), (inner.right() - size.x).max(inner.left())),
96    };
97    let y = part.rect.center().y - size.y / 2.0;
98    Rect::from_min_size(pos2(x, y), size)
99}
100
101/// Paints a part's label where [`label_rect`] puts it, in its state's
102/// `text`, on whole physical pixels. What handlers do unless they draw
103/// labels their own way.
104pub fn paint_label(cx: &KitCx<'_>, data: &ComponentKit, part: &Part<'_>) {
105    let Some(galley) = part.label else { return };
106    let b = data.states.get(part.state);
107    let at = label_rect(data, part, galley.size()).min;
108    let pos = pos2(
109        (at.x * cx.ppp).round() / cx.ppp,
110        (at.y * cx.ppp).round() / cx.ppp,
111    );
112    cx.painter.galley(pos, galley.clone(), b.text);
113}