Skip to main content

mitsuami_core/
draw.rs

1//! The drawn tier of custom widgets: a small, platform-neutral 2D API.
2//!
3//! Drawing records a [`DisplayList`]: plain data the core sends to the
4//! backend as a prop. Backends rasterize it with the platform's own 2D
5//! library (CoreGraphics, Direct2D, Cairo/GSK) and resolve semantic colors
6//! against the current appearance, so drawn widgets follow dark mode and
7//! the accent color without redrawing in app code.
8
9use crate::backend::PlatformMetrics;
10use crate::geometry::{Point, Rect, Size};
11
12/// Semantic colors follow the platform's appearance; `Rgba` is fixed.
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum Color {
15    Label,
16    SecondaryLabel,
17    Accent,
18    Separator,
19    ControlBackground,
20    WindowBackground,
21    Rgba(u8, u8, u8, u8),
22}
23
24impl Color {
25    pub const fn rgb(r: u8, g: u8, b: u8) -> Color {
26        Color::Rgba(r, g, b, 255)
27    }
28}
29
30#[derive(Clone, Copy, Debug, PartialEq)]
31pub enum PathElement {
32    MoveTo(Point),
33    LineTo(Point),
34    /// A cubic Bézier curve to `to`.
35    CurveTo {
36        c1: Point,
37        c2: Point,
38        to: Point,
39    },
40    Close,
41}
42
43/// A vector path, built by chaining: `Path::new().move_to(0.0, 0.0).line_to(10.0, 0.0).close()`.
44#[derive(Clone, Debug, Default, PartialEq)]
45pub struct Path {
46    elements: Vec<PathElement>,
47}
48
49impl Path {
50    pub fn new() -> Path {
51        Path::default()
52    }
53
54    pub fn move_to(mut self, x: f32, y: f32) -> Path {
55        self.elements.push(PathElement::MoveTo(Point::new(x, y)));
56        self
57    }
58
59    pub fn line_to(mut self, x: f32, y: f32) -> Path {
60        self.elements.push(PathElement::LineTo(Point::new(x, y)));
61        self
62    }
63
64    pub fn curve_to(mut self, c1: Point, c2: Point, to: Point) -> Path {
65        self.elements.push(PathElement::CurveTo { c1, c2, to });
66        self
67    }
68
69    pub fn close(mut self) -> Path {
70        self.elements.push(PathElement::Close);
71        self
72    }
73
74    /// A closed polygon through `points`.
75    pub fn polygon(points: impl IntoIterator<Item = Point>) -> Path {
76        let mut path = Path::new();
77        for (i, p) in points.into_iter().enumerate() {
78            path = if i == 0 { path.move_to(p.x, p.y) } else { path.line_to(p.x, p.y) };
79        }
80        path.close()
81    }
82
83    pub fn elements(&self) -> &[PathElement] {
84        &self.elements
85    }
86}
87
88#[derive(Clone, Debug, PartialEq)]
89pub enum Shape {
90    Rect(Rect),
91    RoundedRect(Rect, f32),
92    Ellipse(Rect),
93    Path(Path),
94}
95
96impl From<Rect> for Shape {
97    fn from(rect: Rect) -> Shape {
98        Shape::Rect(rect)
99    }
100}
101
102impl From<Path> for Shape {
103    fn from(path: Path) -> Shape {
104        Shape::Path(path)
105    }
106}
107
108#[derive(Clone, Debug, PartialEq)]
109pub enum DrawOp {
110    Fill { shape: Shape, color: Color },
111    Stroke { shape: Shape, color: Color, width: f32 },
112}
113
114/// What a drawn widget looks like, in its own coordinates (logical units,
115/// origin at the top-left).
116#[derive(Clone, Debug, Default, PartialEq)]
117pub struct DisplayList(pub Vec<DrawOp>);
118
119impl DisplayList {
120    pub fn ops(&self) -> &[DrawOp] {
121        &self.0
122    }
123}
124
125/// Where a drawn widget draws. Records operations; draws nothing itself.
126pub struct Canvas<'a> {
127    size: Size,
128    metrics: &'a PlatformMetrics,
129    ops: Vec<DrawOp>,
130}
131
132impl<'a> Canvas<'a> {
133    pub fn new(size: Size, metrics: &'a PlatformMetrics) -> Canvas<'a> {
134        Canvas { size, metrics, ops: Vec::new() }
135    }
136
137    /// The widget's laid-out size.
138    pub fn size(&self) -> Size {
139        self.size
140    }
141
142    /// Font sizes and spacing tokens, for sizing things like the platform does.
143    pub fn metrics(&self) -> &PlatformMetrics {
144        self.metrics
145    }
146
147    pub fn fill(&mut self, shape: impl Into<Shape>, color: Color) {
148        self.ops.push(DrawOp::Fill { shape: shape.into(), color });
149    }
150
151    pub fn stroke(&mut self, shape: impl Into<Shape>, color: Color, width: f32) {
152        self.ops.push(DrawOp::Stroke { shape: shape.into(), color, width });
153    }
154
155    pub fn finish(self) -> DisplayList {
156        DisplayList(self.ops)
157    }
158}