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 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 CurveTo {
36 c1: Point,
37 c2: Point,
38 to: Point,
39 },
40 Close,
41}
42
43#[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 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#[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
125pub 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 pub fn size(&self) -> Size {
139 self.size
140 }
141
142 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}