1use crate::{
2 AnonymousElementKind, AnyElement, Bounds, ClickListeners, Clickable, ClickableElement,
3 ClickableElementState, Element, ElementId, ElementKind, Hoverable, HoverableElement,
4 IdentifiedElement, IdentifiedElementKind, IntoAnyElement, LayoutId, LayoutNodeElement,
5 Overflow, ParentElement, Pixels, Point, SharedString, Style, StyleCascade, StyleRefinement,
6 Styled, ViewContext,
7};
8use parking_lot::Mutex;
9use smallvec::SmallVec;
10use std::sync::Arc;
11
12#[derive(Default, Clone)]
13pub struct ScrollState(Arc<Mutex<Point<Pixels>>>);
14
15impl ScrollState {
16 pub fn x(&self) -> Pixels {
17 self.0.lock().x
18 }
19
20 pub fn set_x(&self, value: Pixels) {
21 self.0.lock().x = value;
22 }
23
24 pub fn y(&self) -> Pixels {
25 self.0.lock().y
26 }
27
28 pub fn set_y(&self, value: Pixels) {
29 self.0.lock().y = value;
30 }
31}
32
33pub fn div<S>() -> Div<S, AnonymousElementKind>
34where
35 S: 'static + Send + Sync,
36{
37 Div(ClickableElement::new(HoverableElement::new(
38 LayoutNodeElement::new(),
39 )))
40}
41
42pub struct Div<V: 'static + Send + Sync, K: ElementKind = AnonymousElementKind>(
43 ClickableElement<HoverableElement<LayoutNodeElement<V, K>>>,
44);
45
46impl<V: 'static + Send + Sync, K: ElementKind> Div<V, K> {
47 pub fn group(mut self, group: impl Into<SharedString>) -> Self {
48 *self.0.group_mut() = Some(group.into());
49 self
50 }
51
52 pub fn z_index(mut self, z_index: u32) -> Self {
53 self.base_style().z_index = Some(z_index);
54 self
55 }
56
57 pub fn overflow_hidden(mut self) -> Self {
58 self.base_style().overflow.x = Some(Overflow::Hidden);
59 self.base_style().overflow.y = Some(Overflow::Hidden);
60 self
61 }
62
63 pub fn overflow_hidden_x(mut self) -> Self {
64 self.base_style().overflow.x = Some(Overflow::Hidden);
65 self
66 }
67
68 pub fn overflow_hidden_y(mut self) -> Self {
69 self.base_style().overflow.y = Some(Overflow::Hidden);
70 self
71 }
72
73 pub fn overflow_scroll(mut self, _scroll_state: ScrollState) -> Self {
74 // todo!("impl scrolling")
75 // self.scroll_state = Some(scroll_state);
76 self.base_style().overflow.x = Some(Overflow::Scroll);
77 self.base_style().overflow.y = Some(Overflow::Scroll);
78 self
79 }
80
81 pub fn overflow_x_scroll(mut self, _scroll_state: ScrollState) -> Self {
82 // todo!("impl scrolling")
83 // self.scroll_state = Some(scroll_state);
84 self.base_style().overflow.x = Some(Overflow::Scroll);
85 self
86 }
87
88 pub fn overflow_y_scroll(mut self, _scroll_state: ScrollState) -> Self {
89 // todo!("impl scrolling")
90 // self.scroll_state = Some(scroll_state);
91 self.base_style().overflow.y = Some(Overflow::Scroll);
92 self
93 }
94
95 fn base_style(&mut self) -> &mut StyleRefinement {
96 self.style_cascade().base()
97 }
98}
99
100impl<V: 'static + Send + Sync> Div<V, AnonymousElementKind> {
101 pub fn id(self, id: impl Into<ElementId>) -> Div<V, IdentifiedElementKind> {
102 Div(self.0.replace_child(|hoverable| {
103 hoverable.replace_child(|layout_node| layout_node.identify(id))
104 }))
105 }
106}
107
108impl<V: 'static + Send + Sync> IdentifiedElement for Div<V, IdentifiedElementKind> {
109 fn id(&self) -> ElementId {
110 IdentifiedElement::id(&self.0)
111 }
112}
113
114impl<V: 'static + Send + Sync, K: ElementKind> ParentElement for Div<V, K> {
115 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
116 self.0.children_mut()
117 }
118
119 fn group_mut(&mut self) -> &mut Option<SharedString> {
120 self.0.group_mut()
121 }
122}
123
124impl<V: 'static + Send + Sync, K: ElementKind> Styled for Div<V, K> {
125 fn style_cascade(&mut self) -> &mut StyleCascade {
126 self.0.style_cascade()
127 }
128
129 fn computed_style(&mut self) -> &Style {
130 self.0.computed_style()
131 }
132}
133
134impl<V: 'static + Send + Sync, K: ElementKind> Hoverable for Div<V, K> {
135 fn hover_style(&mut self) -> &mut StyleRefinement {
136 self.0.hover_style()
137 }
138}
139
140impl<V: 'static + Send + Sync> Clickable for Div<V, IdentifiedElementKind> {
141 fn active_style(&mut self) -> &mut StyleRefinement {
142 self.0.active_style()
143 }
144
145 fn listeners(&mut self) -> &mut ClickListeners<V> {
146 self.0.listeners()
147 }
148}
149
150impl<V, K> IntoAnyElement<V> for Div<V, K>
151where
152 V: 'static + Send + Sync,
153 K: ElementKind,
154{
155 fn into_any(self) -> AnyElement<V> {
156 AnyElement::new(self)
157 }
158}
159
160impl<V, K> Element for Div<V, K>
161where
162 V: 'static + Send + Sync,
163 K: ElementKind,
164{
165 type ViewState = V;
166 type ElementState = ClickableElementState<()>;
167
168 fn id(&self) -> Option<ElementId> {
169 self.0.id()
170 }
171
172 fn layout(
173 &mut self,
174 state: &mut Self::ViewState,
175 element_state: Option<Self::ElementState>,
176 cx: &mut ViewContext<Self::ViewState>,
177 ) -> (LayoutId, Self::ElementState) {
178 self.0.layout(state, element_state, cx)
179 }
180
181 fn paint(
182 &mut self,
183 bounds: Bounds<Pixels>,
184 state: &mut Self::ViewState,
185 element_state: &mut Self::ElementState,
186 cx: &mut ViewContext<Self::ViewState>,
187 ) {
188 self.0.paint(bounds, state, element_state, cx);
189 }
190}