tui_lipan/widgets/canvas/
mod.rs1mod layout;
4mod node;
5mod reconcile;
6
7pub(crate) use self::layout::measure_canvas;
8pub(crate) use self::node::CanvasNode;
9pub(crate) use self::reconcile::{CanvasReconcile, reconcile_canvas};
10
11use crate::core::element::{Element, ElementKind};
12use crate::layout::hash::LayoutHash;
13use crate::style::{Length, Rect, Style};
14
15#[derive(Clone)]
17pub struct CanvasItem {
18 pub rect: Rect,
20 pub element: Element,
22}
23
24impl CanvasItem {
25 pub fn new(rect: Rect, element: impl Into<Element>) -> Self {
27 Self {
28 rect,
29 element: element.into(),
30 }
31 }
32}
33
34impl std::borrow::Borrow<Element> for CanvasItem {
35 fn borrow(&self) -> &Element {
36 &self.element
37 }
38}
39
40#[derive(Clone)]
46pub struct Canvas {
47 pub(crate) items: Vec<CanvasItem>,
48 pub(crate) style: Style,
49 pub(crate) passthrough: bool,
50 pub(crate) width: Length,
51 pub(crate) height: Length,
52}
53
54impl Default for Canvas {
55 fn default() -> Self {
56 Self {
57 items: Vec::new(),
58 style: Style::default(),
59 passthrough: false,
60 width: Length::Flex(1),
61 height: Length::Flex(1),
62 }
63 }
64}
65
66impl Canvas {
67 pub fn new() -> Self {
69 Self::default()
70 }
71
72 pub fn child_at(mut self, rect: Rect, child: impl Into<Element>) -> Self {
74 self.items.push(CanvasItem::new(rect, child));
75 self
76 }
77
78 pub fn items(mut self, items: impl IntoIterator<Item = CanvasItem>) -> Self {
80 self.items = items.into_iter().collect();
81 self
82 }
83
84 pub fn style(mut self, style: Style) -> Self {
86 self.style = style;
87 self
88 }
89
90 pub fn passthrough(mut self, passthrough: bool) -> Self {
92 self.passthrough = passthrough;
93 self
94 }
95
96 pub fn width(mut self, width: Length) -> Self {
98 self.width = width;
99 self
100 }
101
102 pub fn height(mut self, height: Length) -> Self {
104 self.height = height;
105 self
106 }
107}
108
109impl From<Canvas> for Element {
110 fn from(value: Canvas) -> Self {
111 Element::new(ElementKind::Canvas(value))
112 }
113}
114
115impl LayoutHash for Canvas {
116 fn layout_hash(
117 &self,
118 hasher: &mut impl std::hash::Hasher,
119 recurse: &dyn Fn(&Element) -> Option<u64>,
120 ) -> Option<()> {
121 use std::hash::Hash;
122
123 self.passthrough.hash(hasher);
124 self.width.hash(hasher);
125 self.height.hash(hasher);
126 self.items.len().hash(hasher);
127 for item in &self.items {
128 item.rect.hash(hasher);
129 recurse(&item.element)?.hash(hasher);
130 }
131 Some(())
132 }
133}