1use ansiq_core::{CanvasCell, CanvasProps, Element, ElementKind, Layout, Length, Style};
2
3pub struct Canvas<Message = ()> {
4 element: Element<Message>,
5}
6
7impl<Message> Canvas<Message> {
8 pub fn new() -> Self {
9 Self {
10 element: Element::new(ElementKind::Canvas(CanvasProps {
11 width: 16,
12 height: 8,
13 cells: Vec::new(),
14 }))
15 .with_layout(Layout {
16 width: Length::Fill,
17 height: Length::Fixed(8),
18 }),
19 }
20 }
21
22 pub fn size(mut self, width: u16, height: u16) -> Self {
23 if let ElementKind::Canvas(props) = &mut self.element.kind {
24 props.width = width.max(1);
25 props.height = height.max(1);
26 }
27 self
28 }
29
30 pub fn point(mut self, x: u16, y: u16, symbol: char) -> Self {
31 if let ElementKind::Canvas(props) = &mut self.element.kind {
32 props.cells.push(CanvasCell {
33 x,
34 y,
35 symbol,
36 style: self.element.style,
37 });
38 }
39 self
40 }
41
42 pub fn layout(mut self, layout: Layout) -> Self {
43 self.element.layout = layout;
44 self
45 }
46
47 pub fn style(mut self, style: Style) -> Self {
48 self.element.style = style;
49 if let ElementKind::Canvas(props) = &mut self.element.kind {
50 for cell in &mut props.cells {
51 cell.style = style;
52 }
53 }
54 self
55 }
56
57 pub fn build(self) -> Element<Message> {
58 self.element
59 }
60}