1use crate::backend::PlatformMetrics;
10use crate::geometry::{Point, Rect, Size};
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum Color {
15 Label,
16 SecondaryLabel,
17 Accent,
18 Separator,
19 ControlBackground,
20 WindowBackground,
21 Error,
23 Warning,
25 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 CurveTo {
42 c1: Point,
43 c2: Point,
44 to: Point,
45 },
46 Close,
47}
48
49#[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 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#[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
131pub 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 pub fn size(&self) -> Size {
145 self.size
146 }
147
148 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}