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    /// Something went wrong, or is destructive: the platform's red.
22    Error,
23    /// Something needs attention: the platform's orange or yellow.
24    Warning,
25    /// Something went well: the platform's green.
26    Success,
27    Rgba(u8, u8, u8, u8),
28}
29
30impl Color {
31    pub const fn rgb(r: u8, g: u8, b: u8) -> Color {
32        Color::Rgba(r, g, b, 255)
33    }
34}
35
36#[derive(Clone, Copy, Debug, PartialEq)]
37pub enum PathElement {
38    MoveTo(Point),
39    LineTo(Point),
40    /// A cubic Bézier curve to `to`.
41    CurveTo {
42        c1: Point,
43        c2: Point,
44        to: Point,
45    },
46    Close,
47}
48
49/// A vector path, built by chaining: `Path::new().move_to(0.0, 0.0).line_to(10.0, 0.0).close()`.
50#[derive(Clone, Debug, Default, PartialEq)]
51pub struct Path {
52    elements: Vec<PathElement>,
53}
54
55impl Path {
56    pub fn new() -> Path {
57        Path::default()
58    }
59
60    pub fn move_to(mut self, x: f32, y: f32) -> Path {
61        self.elements.push(PathElement::MoveTo(Point::new(x, y)));
62        self
63    }
64
65    pub fn line_to(mut self, x: f32, y: f32) -> Path {
66        self.elements.push(PathElement::LineTo(Point::new(x, y)));
67        self
68    }
69
70    pub fn curve_to(mut self, c1: Point, c2: Point, to: Point) -> Path {
71        self.elements.push(PathElement::CurveTo { c1, c2, to });
72        self
73    }
74
75    pub fn close(mut self) -> Path {
76        self.elements.push(PathElement::Close);
77        self
78    }
79
80    /// A closed polygon through `points`.
81    pub fn polygon(points: impl IntoIterator<Item = Point>) -> Path {
82        let mut path = Path::new();
83        for (i, p) in points.into_iter().enumerate() {
84            path = if i == 0 { path.move_to(p.x, p.y) } else { path.line_to(p.x, p.y) };
85        }
86        path.close()
87    }
88
89    pub fn elements(&self) -> &[PathElement] {
90        &self.elements
91    }
92}
93
94#[derive(Clone, Debug, PartialEq)]
95pub enum Shape {
96    Rect(Rect),
97    RoundedRect(Rect, f32),
98    Ellipse(Rect),
99    Path(Path),
100}
101
102impl From<Rect> for Shape {
103    fn from(rect: Rect) -> Shape {
104        Shape::Rect(rect)
105    }
106}
107
108impl From<Path> for Shape {
109    fn from(path: Path) -> Shape {
110        Shape::Path(path)
111    }
112}
113
114#[derive(Clone, Debug, PartialEq)]
115pub enum DrawOp {
116    Fill { shape: Shape, color: Color },
117    Stroke { shape: Shape, color: Color, width: f32 },
118}
119
120/// What a drawn widget looks like, in its own coordinates (logical units,
121/// origin at the top-left).
122#[derive(Clone, Debug, Default, PartialEq)]
123pub struct DisplayList(pub Vec<DrawOp>);
124
125impl DisplayList {
126    pub fn ops(&self) -> &[DrawOp] {
127        &self.0
128    }
129}
130
131/// Where a drawn widget draws. Records operations; draws nothing itself.
132pub struct Canvas<'a> {
133    size: Size,
134    metrics: &'a PlatformMetrics,
135    ops: Vec<DrawOp>,
136}
137
138impl<'a> Canvas<'a> {
139    pub fn new(size: Size, metrics: &'a PlatformMetrics) -> Canvas<'a> {
140        Canvas { size, metrics, ops: Vec::new() }
141    }
142
143    /// The widget's laid-out size.
144    pub fn size(&self) -> Size {
145        self.size
146    }
147
148    /// Font sizes and spacing tokens, for sizing things like the platform does.
149    pub fn metrics(&self) -> &PlatformMetrics {
150        self.metrics
151    }
152
153    pub fn fill(&mut self, shape: impl Into<Shape>, color: Color) {
154        self.ops.push(DrawOp::Fill { shape: shape.into(), color });
155    }
156
157    pub fn stroke(&mut self, shape: impl Into<Shape>, color: Color, width: f32) {
158        self.ops.push(DrawOp::Stroke { shape: shape.into(), color, width });
159    }
160
161    pub fn finish(self) -> DisplayList {
162        DisplayList(self.ops)
163    }
164}