diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 38ec0f3d11e3a1f3258d66c066ff8a7c906cee48..f8386ee271e658148d7edb9b32fec5c67a044443 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -2640,15 +2640,6 @@ impl Element for EditorElement { cx.request_layout(&style, None) } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut Editor, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - } - fn paint( &mut self, bounds: Bounds, diff --git a/crates/gpui2/src/element.rs b/crates/gpui2/src/element.rs index 1dc9c155eb7501daeb949b106641eca8abe1a733..9ee9eaa7c335960f3e3c6974b0a8798c3d13f9c4 100644 --- a/crates/gpui2/src/element.rs +++ b/crates/gpui2/src/element.rs @@ -26,14 +26,6 @@ pub trait Element { cx: &mut ViewContext, ) -> LayoutId; - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ); - fn paint( &mut self, bounds: Bounds, @@ -70,7 +62,6 @@ pub trait ParentElement { trait ElementObject { fn initialize(&mut self, view_state: &mut V, cx: &mut ViewContext); fn layout(&mut self, view_state: &mut V, cx: &mut ViewContext) -> LayoutId; - fn prepaint(&mut self, view_state: &mut V, cx: &mut ViewContext); fn paint(&mut self, view_state: &mut V, cx: &mut ViewContext); fn measure( &mut self, @@ -208,36 +199,6 @@ where }; } - fn prepaint(&mut self, view_state: &mut V, cx: &mut ViewContext) { - self.phase = match mem::take(&mut self.phase) { - ElementRenderPhase::LayoutRequested { - layout_id, - mut frame_state, - } - | ElementRenderPhase::LayoutComputed { - layout_id, - mut frame_state, - .. - } => { - let bounds = cx.layout_bounds(layout_id); - if let Some(id) = self.element.id() { - cx.with_element_state(id, |element_state, cx| { - let mut element_state = element_state.unwrap(); - self.element - .prepaint(bounds, view_state, &mut element_state, cx); - ((), element_state) - }); - } else { - self.element - .prepaint(bounds, view_state, frame_state.as_mut().unwrap(), cx); - } - ElementRenderPhase::Painted - } - - _ => panic!("must call layout before paint"), - }; - } - fn measure( &mut self, available_space: Size, @@ -322,10 +283,6 @@ impl AnyElement { self.0.paint(view_state, cx) } - pub fn prepaint(&mut self, view_state: &mut V, cx: &mut ViewContext) { - self.0.prepaint(view_state, cx) - } - /// Initializes this element and performs layout within the given available space to determine its size. pub fn measure( &mut self, @@ -419,16 +376,6 @@ where rendered_element.layout(view_state, cx) } - fn prepaint( - &mut self, - _bounds: Bounds, - view_state: &mut V, - rendered_element: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - rendered_element.prepaint(view_state, cx) - } - fn paint( &mut self, _bounds: Bounds, diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index 1211a6bc12a0faac992d4b4d2936ba339ccf8462..25c13d6980f3c8b3e3cdfd80570e603c574e795d 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -265,16 +265,6 @@ where }) } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - todo!() - } - fn paint( &mut self, bounds: Bounds, diff --git a/crates/gpui2/src/elements/img.rs b/crates/gpui2/src/elements/img.rs index 137dde7dfed2a17398edaf0c848b1437f42270d5..1ff088c1afc4a2098dc2ba41e37d0ae2ff6eef55 100644 --- a/crates/gpui2/src/elements/img.rs +++ b/crates/gpui2/src/elements/img.rs @@ -94,15 +94,6 @@ where self.base.layout(view_state, element_state, cx) } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - } - fn paint( &mut self, bounds: Bounds, diff --git a/crates/gpui2/src/elements/node.rs b/crates/gpui2/src/elements/node.rs index bcf3772aed018107929adb767208bd314894acd8..06d4ae2636921d48caa95d3395cb7a452097e0c5 100644 --- a/crates/gpui2/src/elements/node.rs +++ b/crates/gpui2/src/elements/node.rs @@ -274,9 +274,7 @@ pub trait InteractiveComponent { } } -pub trait StatefulInteractiveComponent { - fn interactivity(&mut self) -> &mut StatefulInteractivity; - +pub trait StatefulInteractiveComponent: InteractiveComponent { fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self where Self: Sized, @@ -525,23 +523,14 @@ pub struct FocusEvent { } pub struct Node { - style: StyleRefinement, key_context: KeyContext, interactivity: Interactivity, children: Vec>, } -impl Node { - fn compute_style(&self) -> Style { - let mut style = Style::default(); - style.refine(&self.style); - style - } -} - impl Styled for Node { fn style(&mut self) -> &mut StyleRefinement { - &mut self.style + &mut self.interactivity.base_style } } @@ -582,7 +571,7 @@ impl Element for Node { element_state: &mut Self::ElementState, cx: &mut ViewContext, ) -> crate::LayoutId { - let style = self.compute_style(); + let style = self.interactivity().compute_style(None, cx); style.with_text_style(cx, |cx| { element_state.child_layout_ids = self .children @@ -593,20 +582,6 @@ impl Element for Node { }) } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut V, - _: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - for child in &mut self.children { - child.prepaint(view_state, cx); - } - self.interactivity - .refine_style(&mut self.style, bounds, view_state, cx); - } - fn paint( &mut self, bounds: Bounds, @@ -614,7 +589,7 @@ impl Element for Node { element_state: &mut Self::ElementState, cx: &mut ViewContext, ) { - let style = self.compute_style(); + let style = self.interactivity.compute_style(Some(bounds), cx); if style.visibility == Visibility::Hidden { return; } @@ -671,12 +646,37 @@ impl Element for Node { } } +pub struct ComputedStyle { + base: StyleRefinement, + focus: StyleRefinement, + hover: StyleRefinement, + active: StyleRefinement, +} + +pub struct StyleCascade { + pub base: StyleRefinement, + pub focus: StyleRefinement, + pub hover: StyleRefinement, + pub dragged_over: StyleRefinement, + pub active: StyleRefinement, +} + pub struct Interactivity { + pub active: bool, + pub group_active: bool, + pub hovered: bool, + pub group_hovered: bool, + pub focused: bool, + pub scroll_offset: Point, + pub base_style: StyleRefinement, + pub focus_style: StyleRefinement, pub hover_style: StyleRefinement, pub group_hover_style: Option, + pub active_style: StyleRefinement, + pub group_active_style: Option, pub drag_over_styles: SmallVec<[(TypeId, StyleRefinement); 2]>, pub group_drag_over_styles: SmallVec<[(TypeId, GroupStyle); 2]>, - group: Option, + pub group: Option, pub dispatch_context: KeyContext, pub mouse_down_listeners: SmallVec<[MouseDownListener; 2]>, pub mouse_up_listeners: SmallVec<[MouseUpListener; 2]>, @@ -685,49 +685,68 @@ pub struct Interactivity { pub key_down_listeners: SmallVec<[KeyDownListener; 2]>, pub key_up_listeners: SmallVec<[KeyUpListener; 2]>, pub action_listeners: SmallVec<[(TypeId, ActionListener); 8]>, - drop_listeners: SmallVec<[(TypeId, Box>); 2]>, - scroll_offset: Point, + pub drop_listeners: SmallVec<[(TypeId, Box>); 2]>, + pub click_listeners: SmallVec<[ClickListener; 2]>, + pub drag_listener: Option>, + pub hover_listener: Option>, + pub tooltip_builder: Option>, } impl Interactivity { - fn refine_style( - &self, - style: &mut StyleRefinement, - bounds: Bounds, - cx: &mut ViewContext, - ) { - let mouse_position = cx.mouse_position(); - if let Some(group_hover) = self.group_hover_style.as_ref() { - if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) { - if group_bounds.contains_point(&mouse_position) { - style.refine(&group_hover.style); + fn compute_style(&self, bounds: Option>, cx: &mut ViewContext) -> Style { + let mut style = Style::default(); + style.refine(&self.base_style); + if self.focused { + style.refine(&self.focus_style); + } + + if let Some(bounds) = bounds { + let mouse_position = cx.mouse_position(); + if let Some(group_hover) = self.group_hover_style.as_ref() { + if let Some(group_bounds) = GroupBounds::get(&group_hover.group, cx) { + if group_bounds.contains_point(&mouse_position) { + style.refine(&group_hover.style); + } } } - } - if bounds.contains_point(&mouse_position) { - style.refine(&self.hover_style); - } + if bounds.contains_point(&mouse_position) { + style.refine(&self.hover_style); + } + + if let Some(drag) = cx.active_drag.take() { + for (state_type, group_drag_style) in &self.group_drag_over_styles { + if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) { + if *state_type == drag.view.entity_type() + && group_bounds.contains_point(&mouse_position) + { + style.refine(&group_drag_style.style); + } + } + } - if let Some(drag) = cx.active_drag.take() { - for (state_type, group_drag_style) in &self.group_drag_over_styles { - if let Some(group_bounds) = GroupBounds::get(&group_drag_style.group, cx) { + for (state_type, drag_over_style) in &self.drag_over_styles { if *state_type == drag.view.entity_type() - && group_bounds.contains_point(&mouse_position) + && bounds.contains_point(&mouse_position) { - style.refine(&group_drag_style.style); + style.refine(drag_over_style); } } + + cx.active_drag = Some(drag); } + } - for (state_type, drag_over_style) in &self.drag_over_styles { - if *state_type == drag.view.entity_type() && bounds.contains_point(&mouse_position) - { - style.refine(drag_over_style); - } + if self.group_active { + if let Some(group) = self.group_active_style.as_ref() { + style.refine(&group.style) } + } - cx.active_drag = Some(drag); + if self.active { + style.refine(&self.active_style) } + + style } fn paint(&mut self, bounds: Bounds, cx: &mut ViewContext) { @@ -863,32 +882,15 @@ impl> InteractiveComponent for Focusab impl> StatefulInteractiveComponent for Focusable { - fn interactivity(&mut self) -> &mut StatefulInteractivity { - self.element.interactivity() - } } pub struct Stateful { id: SharedString, - interactivity: StatefulInteractivity, view_type: PhantomData, element: E, } -pub struct StatefulInteractivity { - click_listeners: SmallVec<[ClickListener; 2]>, - active_style: StyleRefinement, - group_active_style: Option, - drag_listener: Option>, - hover_listener: Option>, - tooltip_builder: Option>, -} - -impl StatefulInteractiveComponent for Stateful { - fn interactivity(&mut self) -> &mut StatefulInteractivity { - &mut self.interactivity - } -} +impl> StatefulInteractiveComponent for Stateful {} impl> InteractiveComponent for Stateful { fn interactivity(&mut self) -> &mut Interactivity { diff --git a/crates/gpui2/src/elements/svg.rs b/crates/gpui2/src/elements/svg.rs index 124c578b28761964bb6e64b7c09c5d7ce15b6d85..bafedb7f2d8469f4c143b59d6f8c743710ebef8e 100644 --- a/crates/gpui2/src/elements/svg.rs +++ b/crates/gpui2/src/elements/svg.rs @@ -84,15 +84,6 @@ where self.base.layout(view_state, element_state, cx) } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - } - fn paint( &mut self, bounds: Bounds, diff --git a/crates/gpui2/src/elements/text.rs b/crates/gpui2/src/elements/text.rs index 0794ef431adbfbfdc0d77afcf49dca359f72eaaf..5c5709d32e5a12e247726789a35dfd21806c6c7c 100644 --- a/crates/gpui2/src/elements/text.rs +++ b/crates/gpui2/src/elements/text.rs @@ -121,15 +121,6 @@ impl Element for Text { layout_id } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - } - fn paint( &mut self, bounds: Bounds, diff --git a/crates/gpui2/src/elements/uniform_list.rs b/crates/gpui2/src/elements/uniform_list.rs index 16abbcf883a45f5c222de8a53054ede499caa2fa..521df8699a47fb4ec7f0590fcff8f8fee83a0fbf 100644 --- a/crates/gpui2/src/elements/uniform_list.rs +++ b/crates/gpui2/src/elements/uniform_list.rs @@ -152,16 +152,6 @@ impl Element for UniformList { ) } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut V, - element_state: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - todo!() - } - fn paint( &mut self, bounds: crate::Bounds, diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rs index 9b2a06191f66e257387a9f0a1a5710d94c438e2d..d12d84f43b424f83c5785d7dd0b33c776054bbb3 100644 --- a/crates/gpui2/src/view.rs +++ b/crates/gpui2/src/view.rs @@ -147,7 +147,6 @@ pub struct AnyView { model: AnyModel, initialize: fn(&AnyView, &mut WindowContext) -> AnyBox, layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId, - prepaint: fn(&AnyView, &mut AnyBox, &mut WindowContext), paint: fn(&AnyView, &mut AnyBox, &mut WindowContext), } @@ -157,7 +156,6 @@ impl AnyView { model: self.model.downgrade(), initialize: self.initialize, layout: self.layout, - prepaint: self.prepaint, paint: self.paint, } } @@ -169,7 +167,6 @@ impl AnyView { model, initialize: self.initialize, layout: self.layout, - prepaint: self.prepaint, paint: self.paint, }), } @@ -201,7 +198,6 @@ impl From> for AnyView { model: value.model.into_any(), initialize: any_view::initialize::, layout: any_view::layout::, - prepaint: any_view::prepaint::, paint: any_view::paint::, } } @@ -232,16 +228,6 @@ impl Element for AnyView { (self.layout)(self, rendered_element, cx) } - fn prepaint( - &mut self, - bounds: Bounds, - view_state: &mut ParentViewState, - rendered_element: &mut Self::ElementState, - cx: &mut ViewContext, - ) { - (self.prepaint)(self, rendered_element, cx) - } - fn paint( &mut self, _bounds: Bounds, @@ -257,7 +243,6 @@ pub struct AnyWeakView { model: AnyWeakModel, initialize: fn(&AnyView, &mut WindowContext) -> AnyBox, layout: fn(&AnyView, &mut AnyBox, &mut WindowContext) -> LayoutId, - prepaint: fn(&AnyView, &mut AnyBox, &mut WindowContext), paint: fn(&AnyView, &mut AnyBox, &mut WindowContext), } @@ -268,7 +253,6 @@ impl AnyWeakView { model, initialize: self.initialize, layout: self.layout, - prepaint: self.prepaint, paint: self.paint, }) } @@ -280,7 +264,6 @@ impl From> for AnyWeakView { model: view.model.into(), initialize: any_view::initialize::, layout: any_view::layout::, - prepaint: any_view::prepaint::, paint: any_view::paint::, } } @@ -326,18 +309,6 @@ mod any_view { }) } - pub(crate) fn prepaint( - view: &AnyView, - element: &mut Box, - cx: &mut WindowContext, - ) { - cx.with_element_id(view.model.entity_id, |_, cx| { - let view = view.clone().downcast::().unwrap(); - let element = element.downcast_mut::>().unwrap(); - view.update(cx, |view, cx| element.prepaint(view, cx)) - }) - } - pub(crate) fn paint( view: &AnyView, element: &mut Box,