div.rs

  1use crate::{
  2    point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementFocus, ElementId,
  3    ElementInteraction, FocusDisabled, FocusEnabled, FocusHandle, FocusListeners, Focusable,
  4    GlobalElementId, GroupBounds, InteractiveElementState, LayoutId, Overflow, ParentElement,
  5    Pixels, Point, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
  6    StatelessInteractive, Style, StyleRefinement, Styled, ViewContext, Visibility,
  7};
  8use refineable::Refineable;
  9use smallvec::SmallVec;
 10
 11pub struct Div<
 12    V: 'static,
 13    I: ElementInteraction<V> = StatelessInteraction<V>,
 14    F: ElementFocus<V> = FocusDisabled,
 15> {
 16    interaction: I,
 17    focus: F,
 18    children: SmallVec<[AnyElement<V>; 2]>,
 19    group: Option<SharedString>,
 20    base_style: StyleRefinement,
 21}
 22
 23pub fn div<V: 'static>() -> Div<V, StatelessInteraction<V>, FocusDisabled> {
 24    Div {
 25        interaction: StatelessInteraction::default(),
 26        focus: FocusDisabled,
 27        children: SmallVec::new(),
 28        group: None,
 29        base_style: StyleRefinement::default(),
 30    }
 31}
 32
 33impl<V, F> Div<V, StatelessInteraction<V>, F>
 34where
 35    V: 'static,
 36    F: ElementFocus<V>,
 37{
 38    pub fn id(self, id: impl Into<ElementId>) -> Div<V, StatefulInteraction<V>, F> {
 39        Div {
 40            interaction: id.into().into(),
 41            focus: self.focus,
 42            children: self.children,
 43            group: self.group,
 44            base_style: self.base_style,
 45        }
 46    }
 47}
 48
 49impl<V, I, F> Div<V, I, F>
 50where
 51    I: ElementInteraction<V>,
 52    F: ElementFocus<V>,
 53{
 54    pub fn group(mut self, group: impl Into<SharedString>) -> Self {
 55        self.group = Some(group.into());
 56        self
 57    }
 58
 59    pub fn z_index(mut self, z_index: u32) -> Self {
 60        self.base_style.z_index = Some(z_index);
 61        self
 62    }
 63
 64    pub fn overflow_hidden(mut self) -> Self {
 65        self.base_style.overflow.x = Some(Overflow::Hidden);
 66        self.base_style.overflow.y = Some(Overflow::Hidden);
 67        self
 68    }
 69
 70    pub fn overflow_hidden_x(mut self) -> Self {
 71        self.base_style.overflow.x = Some(Overflow::Hidden);
 72        self
 73    }
 74
 75    pub fn overflow_hidden_y(mut self) -> Self {
 76        self.base_style.overflow.y = Some(Overflow::Hidden);
 77        self
 78    }
 79
 80    fn with_element_id<R>(
 81        &mut self,
 82        cx: &mut ViewContext<V>,
 83        f: impl FnOnce(&mut Self, Option<GlobalElementId>, &mut ViewContext<V>) -> R,
 84    ) -> R {
 85        if let Some(id) = self.id() {
 86            cx.with_element_id(id, |global_id, cx| f(self, Some(global_id), cx))
 87        } else {
 88            f(self, None, cx)
 89        }
 90    }
 91
 92    pub fn compute_style(
 93        &self,
 94        bounds: Bounds<Pixels>,
 95        element_state: &DivState,
 96        cx: &mut ViewContext<V>,
 97    ) -> Style {
 98        let mut computed_style = Style::default();
 99        computed_style.refine(&self.base_style);
100        self.focus.refine_style(&mut computed_style, cx);
101        self.interaction
102            .refine_style(&mut computed_style, bounds, &element_state.interactive, cx);
103        computed_style
104    }
105}
106
107impl<V: 'static> Div<V, StatefulInteraction<V>, FocusDisabled> {
108    pub fn focusable(self) -> Div<V, StatefulInteraction<V>, FocusEnabled<V>> {
109        Div {
110            interaction: self.interaction,
111            focus: FocusEnabled::new(),
112            children: self.children,
113            group: self.group,
114            base_style: self.base_style,
115        }
116    }
117
118    pub fn track_focus(
119        self,
120        handle: &FocusHandle,
121    ) -> Div<V, StatefulInteraction<V>, FocusEnabled<V>> {
122        Div {
123            interaction: self.interaction,
124            focus: FocusEnabled::tracked(handle),
125            children: self.children,
126            group: self.group,
127            base_style: self.base_style,
128        }
129    }
130
131    pub fn overflow_scroll(mut self) -> Self {
132        self.base_style.overflow.x = Some(Overflow::Scroll);
133        self.base_style.overflow.y = Some(Overflow::Scroll);
134        self
135    }
136
137    pub fn overflow_x_scroll(mut self) -> Self {
138        self.base_style.overflow.x = Some(Overflow::Scroll);
139        self
140    }
141
142    pub fn overflow_y_scroll(mut self) -> Self {
143        self.base_style.overflow.y = Some(Overflow::Scroll);
144        self
145    }
146}
147
148impl<V: 'static> Div<V, StatelessInteraction<V>, FocusDisabled> {
149    pub fn track_focus(
150        self,
151        handle: &FocusHandle,
152    ) -> Div<V, StatefulInteraction<V>, FocusEnabled<V>> {
153        Div {
154            interaction: self.interaction.into_stateful(handle),
155            focus: handle.clone().into(),
156            children: self.children,
157            group: self.group,
158            base_style: self.base_style,
159        }
160    }
161}
162
163impl<V, I> Focusable<V> for Div<V, I, FocusEnabled<V>>
164where
165    V: 'static,
166    I: ElementInteraction<V>,
167{
168    fn focus_listeners(&mut self) -> &mut FocusListeners<V> {
169        &mut self.focus.focus_listeners
170    }
171
172    fn set_focus_style(&mut self, style: StyleRefinement) {
173        self.focus.focus_style = style;
174    }
175
176    fn set_focus_in_style(&mut self, style: StyleRefinement) {
177        self.focus.focus_in_style = style;
178    }
179
180    fn set_in_focus_style(&mut self, style: StyleRefinement) {
181        self.focus.in_focus_style = style;
182    }
183}
184
185#[derive(Default)]
186pub struct DivState {
187    interactive: InteractiveElementState,
188    focus_handle: Option<FocusHandle>,
189    child_layout_ids: SmallVec<[LayoutId; 4]>,
190}
191
192impl<V, I, F> Element<V> for Div<V, I, F>
193where
194    I: ElementInteraction<V>,
195    F: ElementFocus<V>,
196{
197    type ElementState = DivState;
198
199    fn id(&self) -> Option<ElementId> {
200        self.interaction
201            .as_stateful()
202            .map(|identified| identified.id.clone())
203    }
204
205    fn initialize(
206        &mut self,
207        view_state: &mut V,
208        element_state: Option<Self::ElementState>,
209        cx: &mut ViewContext<V>,
210    ) -> Self::ElementState {
211        let mut element_state = element_state.unwrap_or_default();
212        self.focus
213            .initialize(element_state.focus_handle.take(), cx, |focus_handle, cx| {
214                element_state.focus_handle = focus_handle;
215                self.interaction.initialize(cx, |cx| {
216                    for child in &mut self.children {
217                        child.initialize(view_state, cx);
218                    }
219                })
220            });
221        element_state
222    }
223
224    fn layout(
225        &mut self,
226        view_state: &mut V,
227        element_state: &mut Self::ElementState,
228        cx: &mut ViewContext<V>,
229    ) -> LayoutId {
230        let style = self.compute_style(Bounds::default(), element_state, cx);
231        style.apply_text_style(cx, |cx| {
232            self.with_element_id(cx, |this, _global_id, cx| {
233                let layout_ids = this
234                    .children
235                    .iter_mut()
236                    .map(|child| child.layout(view_state, cx))
237                    .collect::<SmallVec<_>>();
238                element_state.child_layout_ids = layout_ids.clone();
239                cx.request_layout(&style, layout_ids)
240            })
241        })
242    }
243
244    fn paint(
245        &mut self,
246        bounds: Bounds<Pixels>,
247        view_state: &mut V,
248        element_state: &mut Self::ElementState,
249        cx: &mut ViewContext<V>,
250    ) {
251        self.with_element_id(cx, |this, _global_id, cx| {
252            let style = this.compute_style(bounds, element_state, cx);
253            if style.visibility == Visibility::Hidden {
254                return;
255            }
256
257            if let Some(group) = this.group.clone() {
258                GroupBounds::push(group, bounds, cx);
259            }
260
261            let z_index = style.z_index.unwrap_or(0);
262
263            let mut child_min = point(Pixels::MAX, Pixels::MAX);
264            let mut child_max = Point::default();
265
266            let content_size = if element_state.child_layout_ids.is_empty() {
267                bounds.size
268            } else {
269                for child_layout_id in &element_state.child_layout_ids {
270                    let child_bounds = cx.layout_bounds(*child_layout_id);
271                    child_min = child_min.min(&child_bounds.origin);
272                    child_max = child_max.max(&child_bounds.lower_right());
273                }
274                (child_max - child_min).into()
275            };
276
277            cx.stack(z_index, |cx| {
278                cx.stack(0, |cx| {
279                    style.paint(bounds, cx);
280                    this.focus.paint(bounds, cx);
281                    this.interaction.paint(
282                        bounds,
283                        content_size,
284                        style.overflow,
285                        &mut element_state.interactive,
286                        cx,
287                    );
288                });
289                cx.stack(1, |cx| {
290                    style.apply_text_style(cx, |cx| {
291                        style.apply_overflow(bounds, cx, |cx| {
292                            let scroll_offset = element_state.interactive.scroll_offset();
293                            cx.with_element_offset(scroll_offset, |cx| {
294                                for child in &mut this.children {
295                                    child.paint(view_state, cx);
296                                }
297                            });
298                        })
299                    })
300                });
301            });
302
303            if let Some(group) = this.group.as_ref() {
304                GroupBounds::pop(group, cx);
305            }
306        })
307    }
308}
309
310impl<V, I, F> Component<V> for Div<V, I, F>
311where
312    I: ElementInteraction<V>,
313    F: ElementFocus<V>,
314{
315    fn render(self) -> AnyElement<V> {
316        AnyElement::new(self)
317    }
318}
319
320impl<V, I, F> ParentElement<V> for Div<V, I, F>
321where
322    I: ElementInteraction<V>,
323    F: ElementFocus<V>,
324{
325    fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
326        &mut self.children
327    }
328}
329
330impl<V, I, F> Styled for Div<V, I, F>
331where
332    I: ElementInteraction<V>,
333    F: ElementFocus<V>,
334{
335    fn style(&mut self) -> &mut StyleRefinement {
336        &mut self.base_style
337    }
338}
339
340impl<V, I, F> StatelessInteractive<V> for Div<V, I, F>
341where
342    I: ElementInteraction<V>,
343    F: ElementFocus<V>,
344{
345    fn stateless_interaction(&mut self) -> &mut StatelessInteraction<V> {
346        self.interaction.as_stateless_mut()
347    }
348}
349
350impl<V, F> StatefulInteractive<V> for Div<V, StatefulInteraction<V>, F>
351where
352    F: ElementFocus<V>,
353{
354    fn stateful_interaction(&mut self) -> &mut StatefulInteraction<V> {
355        &mut self.interaction
356    }
357}