Skip to main content

widgetkit_render/
canvas.rs

1use crate::raw;
2use crate::{Stroke, TextStyle};
3use widgetkit_core::{Color, Point, Rect, Size};
4
5#[derive(Debug)]
6pub struct Canvas {
7    size: Size,
8    commands: Vec<raw::Command>,
9}
10
11impl Canvas {
12    pub fn new(size: Size) -> Self {
13        Self {
14            size,
15            commands: Vec::new(),
16        }
17    }
18
19    pub fn size(&self) -> Size {
20        self.size
21    }
22
23    pub fn clear(&mut self, color: Color) {
24        self.commands.push(raw::Command::Clear { color });
25    }
26
27    pub fn rect(&mut self, rect: Rect, color: Color) {
28        self.commands.push(raw::Command::Rect { rect, color });
29    }
30
31    pub fn round_rect(&mut self, rect: Rect, radius: f32, color: Color) {
32        self.commands.push(raw::Command::RoundRect {
33            rect,
34            radius: radius.max(0.0),
35            color,
36        });
37    }
38
39    pub fn line(&mut self, start: Point, end: Point, stroke: Stroke, color: Color) {
40        self.commands.push(raw::Command::Line {
41            start,
42            end,
43            stroke,
44            color,
45        });
46    }
47
48    pub fn circle(&mut self, center: Point, radius: f32, color: Color) {
49        self.commands.push(raw::Command::Circle {
50            center,
51            radius: radius.max(0.0),
52            color,
53        });
54    }
55
56    pub fn text(&mut self, position: Point, text: impl Into<String>, style: TextStyle, color: Color) {
57        self.commands.push(raw::Command::Text {
58            position,
59            text: text.into(),
60            style,
61            color,
62        });
63    }
64
65    pub fn image_placeholder(&mut self, rect: Rect, color: Color) {
66        self.commands.push(raw::Command::ImagePlaceholder { rect, color });
67    }
68
69    pub(crate) fn into_scene(self) -> raw::Scene {
70        raw::Scene {
71            size: self.size,
72            commands: self.commands,
73        }
74    }
75}