diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs deleted file mode 100644 index 537c14633948d4077becf489ce1095403f7945c5..0000000000000000000000000000000000000000 --- a/crates/gpui2/src/elements/div.rs +++ /dev/null @@ -1,334 +0,0 @@ -use std::fmt::Debug; - -use crate::{ - point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, ElementInteractivity, - FocusHandle, FocusListeners, Focusable, FocusableKeyDispatch, GroupBounds, - InteractiveElementState, KeyContext, KeyDispatch, LayoutId, NonFocusableKeyDispatch, Overflow, - ParentElement, Pixels, Point, SharedString, StatefulInteractive, StatefulInteractivity, - StatelessInteractive, StatelessInteractivity, Style, StyleRefinement, Styled, ViewContext, - Visibility, -}; -use refineable::Refineable; -use smallvec::SmallVec; -use util::ResultExt; - -pub struct Div< - V: 'static, - I: ElementInteractivity = StatelessInteractivity, - K: KeyDispatch = NonFocusableKeyDispatch, -> { - interactivity: I, - key_dispatch: K, - children: SmallVec<[AnyElement; 2]>, - group: Option, - base_style: StyleRefinement, -} - -pub fn div() -> Div, NonFocusableKeyDispatch> { - Div { - interactivity: StatelessInteractivity::default(), - key_dispatch: NonFocusableKeyDispatch::default(), - children: SmallVec::new(), - group: None, - base_style: StyleRefinement::default(), - } -} - -impl Div, F> -where - V: 'static, - F: KeyDispatch, -{ - pub fn id(self, id: impl Into) -> Div, F> { - Div { - interactivity: StatefulInteractivity::new(id.into(), self.interactivity), - key_dispatch: self.key_dispatch, - children: self.children, - group: self.group, - base_style: self.base_style, - } - } -} - -impl Div -where - I: ElementInteractivity, - F: KeyDispatch, -{ - pub fn context(mut self, context: C) -> Self - where - Self: Sized, - C: TryInto, - C::Error: Debug, - { - if let Some(context) = context.try_into().log_err() { - *self.key_dispatch.key_context_mut() = context; - } - self - } - - pub fn compute_style( - &self, - bounds: Bounds, - element_state: &DivState, - cx: &mut ViewContext, - ) -> Style { - let mut computed_style = Style::default(); - computed_style.refine(&self.base_style); - self.key_dispatch.refine_style(&mut computed_style, cx); - self.interactivity.refine_style( - &mut computed_style, - bounds, - &element_state.interactive, - cx, - ); - computed_style - } -} - -impl Div, NonFocusableKeyDispatch> { - pub fn focusable(self) -> Div, FocusableKeyDispatch> { - Div { - interactivity: self.interactivity, - key_dispatch: FocusableKeyDispatch::new(self.key_dispatch), - children: self.children, - group: self.group, - base_style: self.base_style, - } - } - - pub fn track_focus( - self, - handle: &FocusHandle, - ) -> Div, FocusableKeyDispatch> { - Div { - interactivity: self.interactivity, - key_dispatch: FocusableKeyDispatch::tracked(self.key_dispatch, handle), - children: self.children, - group: self.group, - base_style: self.base_style, - } - } -} - -impl Div, NonFocusableKeyDispatch> { - pub fn track_focus( - self, - handle: &FocusHandle, - ) -> Div, FocusableKeyDispatch> { - Div { - interactivity: self.interactivity.into_stateful(handle), - key_dispatch: FocusableKeyDispatch::tracked(self.key_dispatch, handle), - children: self.children, - group: self.group, - base_style: self.base_style, - } - } -} - -impl Focusable for Div> -where - V: 'static, - I: ElementInteractivity, -{ - fn focus_listeners(&mut self) -> &mut FocusListeners { - &mut self.key_dispatch.focus_listeners - } - - fn set_focus_style(&mut self, style: StyleRefinement) { - self.key_dispatch.focus_style = style; - } - - fn set_focus_in_style(&mut self, style: StyleRefinement) { - self.key_dispatch.focus_in_style = style; - } - - fn set_in_focus_style(&mut self, style: StyleRefinement) { - self.key_dispatch.in_focus_style = style; - } -} - -#[derive(Default)] -pub struct DivState { - interactive: InteractiveElementState, - focus_handle: Option, - child_layout_ids: SmallVec<[LayoutId; 4]>, -} - -impl Element for Div -where - I: ElementInteractivity, - F: KeyDispatch, -{ - type ElementState = DivState; - - fn id(&self) -> Option { - self.interactivity - .as_stateful() - .map(|identified| identified.id.clone()) - } - - fn initialize( - &mut self, - view_state: &mut V, - element_state: Option, - cx: &mut ViewContext, - ) -> Self::ElementState { - let mut element_state = element_state.unwrap_or_default(); - cx.with_element_id(self.id(), |cx| { - self.key_dispatch.initialize( - element_state.focus_handle.take(), - cx, - |focus_handle, cx| { - self.interactivity.initialize(cx); - element_state.focus_handle = focus_handle; - for child in &mut self.children { - child.initialize(view_state, cx); - } - }, - ); - }); - element_state - } - - fn layout( - &mut self, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) -> LayoutId { - let style = self.compute_style(Bounds::default(), element_state, cx); - style.apply_text_style(cx, |cx| { - cx.with_element_id(self.id(), |cx| { - let layout_ids = self - .children - .iter_mut() - .map(|child| child.layout(view_state, cx)) - .collect::>(); - element_state.child_layout_ids = layout_ids.clone(); - cx.request_layout(&style, layout_ids) - }) - }) - } - - fn paint( - &mut self, - bounds: Bounds, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - cx.with_element_id(self.id(), |cx| { - let style = self.compute_style(bounds, element_state, cx); - if style.visibility == Visibility::Hidden { - return; - } - - if let Some(mouse_cursor) = style.mouse_cursor { - let hovered = bounds.contains_point(&cx.mouse_position()); - if hovered { - cx.set_cursor_style(mouse_cursor); - } - } - - if let Some(group) = self.group.clone() { - GroupBounds::push(group, bounds, cx); - } - - let z_index = style.z_index.unwrap_or(0); - - let mut child_min = point(Pixels::MAX, Pixels::MAX); - let mut child_max = Point::default(); - - let content_size = if element_state.child_layout_ids.is_empty() { - bounds.size - } else { - for child_layout_id in &element_state.child_layout_ids { - let child_bounds = cx.layout_bounds(*child_layout_id); - child_min = child_min.min(&child_bounds.origin); - child_max = child_max.max(&child_bounds.lower_right()); - } - (child_max - child_min).into() - }; - - cx.with_z_index(z_index, |cx| { - cx.with_z_index(0, |cx| { - style.paint(bounds, cx); - self.key_dispatch.paint(bounds, cx); - self.interactivity.handle_events( - bounds, - content_size, - style.overflow, - &mut element_state.interactive, - cx, - ); - }); - cx.with_z_index(1, |cx| { - style.apply_text_style(cx, |cx| { - style.apply_overflow(bounds, cx, |cx| { - let scroll_offset = element_state.interactive.scroll_offset(); - cx.with_element_offset(scroll_offset.unwrap_or_default(), |cx| { - for child in &mut self.children { - child.paint(view_state, cx); - } - }); - }) - }) - }); - }); - - if let Some(group) = self.group.as_ref() { - GroupBounds::pop(group, cx); - } - }) - } -} - -impl Component for Div -where - I: ElementInteractivity, - F: KeyDispatch, -{ - fn render(self) -> AnyElement { - AnyElement::new(self) - } -} - -impl ParentElement for Div -where - I: ElementInteractivity, - F: KeyDispatch, -{ - fn children_mut(&mut self) -> &mut SmallVec<[AnyElement; 2]> { - &mut self.children - } -} - -impl Styled for Div -where - I: ElementInteractivity, - F: KeyDispatch, -{ - fn style(&mut self) -> &mut StyleRefinement { - &mut self.base_style - } -} - -impl StatelessInteractive for Div -where - I: ElementInteractivity, - F: KeyDispatch, -{ - fn stateless_interactivity(&mut self) -> &mut StatelessInteractivity { - self.interactivity.as_stateless_mut() - } -} - -impl StatefulInteractive for Div, F> -where - F: KeyDispatch, -{ - fn stateful_interactivity(&mut self) -> &mut StatefulInteractivity { - &mut self.interactivity - } -}