1use crate::{
2 AnyElement, BorrowWindow, Bounds, Cascade, Element, ElementId, IdentifiedElement, Interactive,
3 LayoutId, MouseEventListeners, Overflow, ParentElement, Pixels, Point, Refineable, Style,
4 Styled, ViewContext,
5};
6use parking_lot::Mutex;
7use smallvec::SmallVec;
8use std::{marker::PhantomData, sync::Arc};
9
10pub enum HasId {}
11
12pub struct Div<S: 'static, I = ()> {
13 styles: Cascade<Style>,
14 id: Option<ElementId>,
15 listeners: MouseEventListeners<S>,
16 children: SmallVec<[AnyElement<S>; 2]>,
17 scroll_state: Option<ScrollState>,
18 identified: PhantomData<I>,
19}
20
21pub fn div<S>() -> Div<S> {
22 Div {
23 styles: Default::default(),
24 id: None,
25 listeners: Default::default(),
26 children: Default::default(),
27 scroll_state: None,
28 identified: PhantomData,
29 }
30}
31
32impl<S: 'static + Send + Sync, Marker: 'static + Send + Sync> Element for Div<S, Marker> {
33 type ViewState = S;
34 type ElementState = ();
35
36 fn element_id(&self) -> Option<ElementId> {
37 self.id.clone()
38 }
39
40 fn layout(
41 &mut self,
42 view: &mut S,
43 _: Option<Self::ElementState>,
44 cx: &mut ViewContext<S>,
45 ) -> (LayoutId, Self::ElementState) {
46 let style = self.computed_style();
47 let child_layout_ids = style.apply_text_style(cx, |cx| {
48 self.with_element_id(cx, |this, cx| this.layout_children(view, cx))
49 });
50 let layout_id = cx.request_layout(style.into(), child_layout_ids.clone());
51 (layout_id, ())
52 }
53
54 fn paint(
55 &mut self,
56 bounds: Bounds<Pixels>,
57 state: &mut S,
58 _: &mut (),
59 cx: &mut ViewContext<S>,
60 ) {
61 let style = self.computed_style();
62 cx.stack(0, |cx| style.paint(bounds, cx));
63
64 let overflow = &style.overflow;
65
66 style.apply_text_style(cx, |cx| {
67 cx.stack(1, |cx| {
68 style.apply_overflow(bounds, cx, |cx| {
69 self.with_element_id(cx, |this, cx| {
70 this.listeners.paint(bounds, cx);
71 this.paint_children(overflow, state, cx)
72 });
73 })
74 })
75 });
76 }
77}
78
79impl<S> Div<S, ()>
80where
81 S: 'static + Send + Sync,
82{
83 pub fn id(self, id: impl Into<ElementId>) -> Div<S, HasId> {
84 Div {
85 styles: self.styles,
86 id: Some(id.into()),
87 listeners: self.listeners,
88 children: self.children,
89 scroll_state: self.scroll_state,
90 identified: PhantomData,
91 }
92 }
93}
94
95impl<S, Marker> Div<S, Marker>
96where
97 S: 'static + Send + Sync,
98 Marker: 'static + Send + Sync,
99{
100 pub fn overflow_hidden(mut self) -> Self {
101 self.declared_style().overflow.x = Some(Overflow::Hidden);
102 self.declared_style().overflow.y = Some(Overflow::Hidden);
103 self
104 }
105
106 pub fn overflow_hidden_x(mut self) -> Self {
107 self.declared_style().overflow.x = Some(Overflow::Hidden);
108 self
109 }
110
111 pub fn overflow_hidden_y(mut self) -> Self {
112 self.declared_style().overflow.y = Some(Overflow::Hidden);
113 self
114 }
115
116 pub fn overflow_scroll(mut self, scroll_state: ScrollState) -> Self {
117 self.scroll_state = Some(scroll_state);
118 self.declared_style().overflow.x = Some(Overflow::Scroll);
119 self.declared_style().overflow.y = Some(Overflow::Scroll);
120 self
121 }
122
123 pub fn overflow_x_scroll(mut self, scroll_state: ScrollState) -> Self {
124 self.scroll_state = Some(scroll_state);
125 self.declared_style().overflow.x = Some(Overflow::Scroll);
126 self
127 }
128
129 pub fn overflow_y_scroll(mut self, scroll_state: ScrollState) -> Self {
130 self.scroll_state = Some(scroll_state);
131 self.declared_style().overflow.y = Some(Overflow::Scroll);
132 self
133 }
134
135 fn scroll_offset(&self, overflow: &Point<Overflow>) -> Point<Pixels> {
136 let mut offset = Point::default();
137 if overflow.y == Overflow::Scroll {
138 offset.y = self.scroll_state.as_ref().unwrap().y();
139 }
140 if overflow.x == Overflow::Scroll {
141 offset.x = self.scroll_state.as_ref().unwrap().x();
142 }
143
144 offset
145 }
146
147 fn layout_children(&mut self, view: &mut S, cx: &mut ViewContext<S>) -> Vec<LayoutId> {
148 self.children
149 .iter_mut()
150 .map(|child| child.layout(view, cx))
151 .collect()
152 }
153
154 fn paint_children(
155 &mut self,
156 overflow: &Point<Overflow>,
157 state: &mut S,
158 cx: &mut ViewContext<S>,
159 ) {
160 let scroll_offset = self.scroll_offset(overflow);
161 for child in &mut self.children {
162 child.paint(state, Some(scroll_offset), cx);
163 }
164 }
165
166 fn with_element_id<R>(
167 &mut self,
168 cx: &mut ViewContext<S>,
169 f: impl FnOnce(&mut Self, &mut ViewContext<S>) -> R,
170 ) -> R {
171 if let Some(element_id) = self.element_id() {
172 cx.with_element_id(element_id, |cx| f(self, cx))
173 } else {
174 f(self, cx)
175 }
176 }
177}
178
179impl<V: 'static + Send + Sync, Marker: 'static + Send + Sync> Styled for Div<V, Marker> {
180 type Style = Style;
181
182 fn style_cascade(&mut self) -> &mut Cascade<Self::Style> {
183 &mut self.styles
184 }
185
186 fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
187 self.styles.base()
188 }
189}
190
191impl<V: Send + Sync + 'static> IdentifiedElement for Div<V, HasId> {}
192
193impl<V: Send + Sync + 'static> Interactive<V> for Div<V, HasId> {
194 fn listeners(&mut self) -> &mut MouseEventListeners<V> {
195 &mut self.listeners
196 }
197}
198
199impl<V: 'static, Marker: 'static + Send + Sync> ParentElement for Div<V, Marker> {
200 type State = V;
201
202 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
203 &mut self.children
204 }
205}
206
207#[derive(Default, Clone)]
208pub struct ScrollState(Arc<Mutex<Point<Pixels>>>);
209
210impl ScrollState {
211 pub fn x(&self) -> Pixels {
212 self.0.lock().x
213 }
214
215 pub fn set_x(&self, value: Pixels) {
216 self.0.lock().x = value;
217 }
218
219 pub fn y(&self) -> Pixels {
220 self.0.lock().y
221 }
222
223 pub fn set_y(&self, value: Pixels) {
224 self.0.lock().y = value;
225 }
226}