1use crate::{
2 AnyElement, Bounds, Element, LayoutId, Overflow, ParentElement, Pixels, Point, Refineable,
3 RefinementCascade, Result, Style, StyleHelpers, Styled, ViewContext,
4};
5use parking_lot::Mutex;
6use smallvec::SmallVec;
7use std::sync::Arc;
8use util::ResultExt;
9
10pub struct Div<S: 'static> {
11 styles: RefinementCascade<Style>,
12 // handlers: InteractionHandlers<V>,
13 children: SmallVec<[AnyElement<S>; 2]>,
14 scroll_state: Option<ScrollState>,
15}
16
17pub fn div<S>() -> Div<S> {
18 Div {
19 styles: Default::default(),
20 // handlers: Default::default(),
21 children: Default::default(),
22 scroll_state: None,
23 }
24}
25
26impl<S: 'static + Send + Sync> Element for Div<S> {
27 type State = S;
28 type FrameState = Vec<LayoutId>;
29
30 fn layout(
31 &mut self,
32 view: &mut S,
33 cx: &mut ViewContext<S>,
34 ) -> Result<(LayoutId, Self::FrameState)> {
35 let style = self.computed_style();
36 let child_layout_ids = style.apply_text_style(cx, |cx| self.layout_children(view, cx))?;
37 let layout_id = cx.request_layout(style.into(), child_layout_ids.clone())?;
38 Ok((layout_id, child_layout_ids))
39 }
40
41 fn paint(
42 &mut self,
43 bounds: Bounds<Pixels>,
44 state: &mut S,
45 child_layouts: &mut Self::FrameState,
46 cx: &mut ViewContext<S>,
47 ) -> Result<()> {
48 let style = self.computed_style();
49 cx.stack(0, |cx| style.paint(bounds, cx));
50
51 let overflow = &style.overflow;
52 style.apply_text_style(cx, |cx| {
53 cx.stack(1, |cx| {
54 style.apply_overflow(bounds, cx, |cx| self.paint_children(overflow, state, cx))
55 })
56 })?;
57 self.handle_scroll(bounds, style.overflow.clone(), child_layouts, cx);
58
59 // todo!("enable inspector")
60 // if cx.is_inspector_enabled() {
61 // self.paint_inspector(parent_origin, layout, cx);
62 // }
63 //
64
65 Ok(())
66 }
67}
68
69impl<S: 'static> Div<S> {
70 pub fn overflow_hidden(mut self) -> Self {
71 self.declared_style().overflow.x = Some(Overflow::Hidden);
72 self.declared_style().overflow.y = Some(Overflow::Hidden);
73 self
74 }
75
76 pub fn overflow_hidden_x(mut self) -> Self {
77 self.declared_style().overflow.x = Some(Overflow::Hidden);
78 self
79 }
80
81 pub fn overflow_hidden_y(mut self) -> Self {
82 self.declared_style().overflow.y = Some(Overflow::Hidden);
83 self
84 }
85
86 pub fn overflow_scroll(mut self, scroll_state: ScrollState) -> Self {
87 self.scroll_state = Some(scroll_state);
88 self.declared_style().overflow.x = Some(Overflow::Scroll);
89 self.declared_style().overflow.y = Some(Overflow::Scroll);
90 self
91 }
92
93 pub fn overflow_x_scroll(mut self, scroll_state: ScrollState) -> Self {
94 self.scroll_state = Some(scroll_state);
95 self.declared_style().overflow.x = Some(Overflow::Scroll);
96 self
97 }
98
99 pub fn overflow_y_scroll(mut self, scroll_state: ScrollState) -> Self {
100 self.scroll_state = Some(scroll_state);
101 self.declared_style().overflow.y = Some(Overflow::Scroll);
102 self
103 }
104
105 fn scroll_offset(&self, overflow: &Point<Overflow>) -> Point<Pixels> {
106 let mut offset = Point::default();
107 if overflow.y == Overflow::Scroll {
108 offset.y = self.scroll_state.as_ref().unwrap().y();
109 }
110 if overflow.x == Overflow::Scroll {
111 offset.x = self.scroll_state.as_ref().unwrap().x();
112 }
113
114 offset
115 }
116
117 fn layout_children(&mut self, view: &mut S, cx: &mut ViewContext<S>) -> Result<Vec<LayoutId>> {
118 self.children
119 .iter_mut()
120 .map(|child| child.layout(view, cx))
121 .collect::<Result<Vec<LayoutId>>>()
122 }
123
124 fn paint_children(
125 &mut self,
126 overflow: &Point<Overflow>,
127 state: &mut S,
128 cx: &mut ViewContext<S>,
129 ) -> Result<()> {
130 let scroll_offset = self.scroll_offset(overflow);
131 for child in &mut self.children {
132 child.paint(state, Some(scroll_offset), cx)?;
133 }
134 Ok(())
135 }
136
137 fn handle_scroll(
138 &mut self,
139 bounds: Bounds<Pixels>,
140 overflow: Point<Overflow>,
141 child_layout_ids: &[LayoutId],
142 cx: &mut ViewContext<S>,
143 ) {
144 if overflow.y == Overflow::Scroll || overflow.x == Overflow::Scroll {
145 let mut scroll_max = Point::default();
146 for child_layout_id in child_layout_ids {
147 if let Some(child_bounds) = cx.layout_bounds(*child_layout_id).log_err() {
148 scroll_max = scroll_max.max(&child_bounds.lower_right());
149 }
150 }
151 scroll_max -= bounds.size;
152
153 // todo!("handle scroll")
154 // let scroll_state = self.scroll_state.as_ref().unwrap().clone();
155 // cx.on_event(order, move |_, event: &ScrollWheelEvent, cx| {
156 // if bounds.contains_point(event.position) {
157 // let scroll_delta = match event.delta {
158 // ScrollDelta::Pixels(delta) => delta,
159 // ScrollDelta::Lines(delta) => cx.text_style().font_size * delta,
160 // };
161 // if overflow.x == Overflow::Scroll {
162 // scroll_state.set_x(
163 // (scroll_state.x() - scroll_delta.x())
164 // .max(px(0.))
165 // .min(scroll_max.x),
166 // );
167 // }
168 // if overflow.y == Overflow::Scroll {
169 // scroll_state.set_y(
170 // (scroll_state.y() - scroll_delta.y())
171 // .max(px(0.))
172 // .min(scroll_max.y),
173 // );
174 // }
175 // cx.repaint();
176 // } else {
177 // cx.bubble_event();
178 // }
179 // })
180 }
181 }
182
183 // fn paint_inspector(
184 // &self,
185 // parent_origin: Point<Pixels>,
186 // layout: &Layout,
187 // cx: &mut ViewContext<V>,
188 // ) {
189 // let style = self.styles.merged();
190 // let bounds = layout.bounds;
191
192 // let hovered = bounds.contains_point(cx.mouse_position());
193 // if hovered {
194 // let rem_size = cx.rem_size();
195 // // cx.scene().push_quad(scene::Quad {
196 // // bounds,
197 // // background: Some(hsla(0., 0., 1., 0.05).into()),
198 // // border: gpui::Border {
199 // // color: hsla(0., 0., 1., 0.2).into(),
200 // // top: 1.,
201 // // right: 1.,
202 // // bottom: 1.,
203 // // left: 1.,
204 // // },
205 // // corner_radii: CornerRadii::default()
206 // // .refined(&style.corner_radii)
207 // // .to_gpui(bounds.size(), rem_size),
208 // // })
209 // }
210
211 // // let pressed = Cell::new(hovered && cx.is_mouse_down(MouseButton::Left));
212 // // cx.on_event(layout.order, move |_, event: &MouseButtonEvent, _| {
213 // // if bounds.contains_point(event.position) {
214 // // if event.is_down {
215 // // pressed.set(true);
216 // // } else if pressed.get() {
217 // // pressed.set(false);
218 // // eprintln!("clicked div {:?} {:#?}", bounds, style);
219 // // }
220 // // }
221 // // });
222
223 // // let hovered = Cell::new(hovered);
224 // // cx.on_event(layout.order, move |_, event: &MouseMovedEvent, cx| {
225 // // cx.bubble_event();
226 // // let hovered_now = bounds.contains_point(event.position);
227 // // if hovered.get() != hovered_now {
228 // // hovered.set(hovered_now);
229 // // cx.repaint();
230 // // }
231 // // });
232 // }
233 //
234}
235
236impl<V> Styled for Div<V> {
237 type Style = Style;
238
239 fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
240 &mut self.styles
241 }
242
243 fn declared_style(&mut self) -> &mut <Self::Style as Refineable>::Refinement {
244 self.styles.base()
245 }
246}
247
248impl<V> StyleHelpers for Div<V> {}
249
250// impl<V> Interactive<V> for Div<V> {
251// fn interaction_handlers(&mut self) -> &mut InteractionHandlers<V> {
252// &mut self.handlers
253// }
254// }
255
256impl<V: 'static> ParentElement<V> for Div<V> {
257 fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
258 &mut self.children
259 }
260}
261
262#[derive(Default, Clone)]
263pub struct ScrollState(Arc<Mutex<Point<Pixels>>>);
264
265impl ScrollState {
266 pub fn x(&self) -> Pixels {
267 self.0.lock().x
268 }
269
270 pub fn set_x(&self, value: Pixels) {
271 self.0.lock().x = value;
272 }
273
274 pub fn y(&self) -> Pixels {
275 self.0.lock().y
276 }
277
278 pub fn set_y(&self, value: Pixels) {
279 self.0.lock().y = value;
280 }
281}