From ce30a689a0df57ef0b501ac63c8872c4fff2e55f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 13 Nov 2023 23:15:45 -0700 Subject: [PATCH] Checkpoint --- crates/gpui2/src/elements/node.rs | 118 +++++++++++++++--------------- 1 file changed, 58 insertions(+), 60 deletions(-) diff --git a/crates/gpui2/src/elements/node.rs b/crates/gpui2/src/elements/node.rs index f69ad25ab0e4c2aae8dfa577c0af1b9c04c50979..82a952fc16c5df4df611067fa90c5c3c7f8409bf 100644 --- a/crates/gpui2/src/elements/node.rs +++ b/crates/gpui2/src/elements/node.rs @@ -11,12 +11,14 @@ use refineable::Refineable; use smallvec::SmallVec; use std::{ any::{Any, TypeId}, + fmt::Debug, marker::PhantomData, mem, sync::Arc, time::Duration, }; use taffy::style::Overflow; +use util::ResultExt; const DRAG_THRESHOLD: f64 = 2.; const TOOLTIP_DELAY: Duration = Duration::from_millis(500); @@ -30,10 +32,36 @@ pub struct GroupStyle { pub trait InteractiveComponent: Sized + Element { fn interactivity(&mut self) -> &mut Interactivity; - fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self + fn id(mut self, id: impl Into) -> Stateful { + self.interactivity().element_id = Some(id.into()); + + Stateful { + element: self, + view_type: PhantomData, + } + } + + fn track_focus(mut self, focus_handle: FocusHandle) -> Focusable { + self.interactivity().focusable = true; + self.interactivity().tracked_focus_handle = Some(focus_handle); + Focusable { + element: self, + view_type: PhantomData, + } + } + + fn key_context(mut self, key_context: C) -> Self where - Self: Sized, + C: TryInto, + E: Debug, { + if let Some(key_context) = key_context.try_into().log_err() { + self.interactivity().key_context = key_context; + } + self + } + + fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self { self.interactivity().hover_style = f(StyleRefinement::default()); self } @@ -42,10 +70,7 @@ pub trait InteractiveComponent: Sized + Element { mut self, group_name: impl Into, f: impl FnOnce(StyleRefinement) -> StyleRefinement, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().group_hover_style = Some(GroupStyle { group: group_name.into(), style: f(StyleRefinement::default()), @@ -57,10 +82,7 @@ pub trait InteractiveComponent: Sized + Element { mut self, button: MouseButton, handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().mouse_down_listeners.push(Box::new( move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble @@ -78,10 +100,7 @@ pub trait InteractiveComponent: Sized + Element { mut self, button: MouseButton, handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().mouse_up_listeners.push(Box::new( move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble @@ -98,10 +117,7 @@ pub trait InteractiveComponent: Sized + Element { fn on_mouse_down_out( mut self, handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().mouse_down_listeners.push(Box::new( move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) { @@ -116,10 +132,7 @@ pub trait InteractiveComponent: Sized + Element { mut self, button: MouseButton, handler: impl Fn(&mut V, &MouseUpEvent, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().mouse_up_listeners.push(Box::new( move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Capture @@ -136,10 +149,7 @@ pub trait InteractiveComponent: Sized + Element { fn on_mouse_move( mut self, handler: impl Fn(&mut V, &MouseMoveEvent, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().mouse_move_listeners.push(Box::new( move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { @@ -153,10 +163,7 @@ pub trait InteractiveComponent: Sized + Element { fn on_scroll_wheel( mut self, handler: impl Fn(&mut V, &ScrollWheelEvent, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().scroll_wheel_listeners.push(Box::new( move |view, event, bounds, phase, cx| { if phase == DispatchPhase::Bubble && bounds.contains_point(&event.position) { @@ -171,10 +178,7 @@ pub trait InteractiveComponent: Sized + Element { fn capture_action( mut self, listener: impl Fn(&mut V, &A, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().action_listeners.push(( TypeId::of::(), Box::new(move |view, action, phase, cx| { @@ -191,10 +195,7 @@ pub trait InteractiveComponent: Sized + Element { fn on_action( mut self, listener: impl Fn(&mut V, &A, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().action_listeners.push(( TypeId::of::(), Box::new(move |view, action, phase, cx| { @@ -210,10 +211,7 @@ pub trait InteractiveComponent: Sized + Element { fn on_key_down( mut self, listener: impl Fn(&mut V, &KeyDownEvent, DispatchPhase, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity() .key_down_listeners .push(Box::new(move |view, event, phase, cx| { @@ -225,10 +223,7 @@ pub trait InteractiveComponent: Sized + Element { fn on_key_up( mut self, listener: impl Fn(&mut V, &KeyUpEvent, DispatchPhase, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity() .key_up_listeners .push(Box::new(move |view, event, phase, cx| { @@ -237,10 +232,7 @@ pub trait InteractiveComponent: Sized + Element { self } - fn drag_over(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self - where - Self: Sized, - { + fn drag_over(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self { self.interactivity() .drag_over_styles .push((TypeId::of::(), f(StyleRefinement::default()))); @@ -251,10 +243,7 @@ pub trait InteractiveComponent: Sized + Element { mut self, group_name: impl Into, f: impl FnOnce(StyleRefinement) -> StyleRefinement, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().group_drag_over_styles.push(( TypeId::of::(), GroupStyle { @@ -268,10 +257,7 @@ pub trait InteractiveComponent: Sized + Element { fn on_drop( mut self, listener: impl Fn(&mut V, View, &mut ViewContext) + 'static, - ) -> Self - where - Self: Sized, - { + ) -> Self { self.interactivity().drop_listeners.push(( TypeId::of::(), Box::new(move |view, dragged_view, cx| { @@ -283,6 +269,11 @@ pub trait InteractiveComponent: Sized + Element { } pub trait StatefulInteractiveComponent>: InteractiveComponent { + fn focusable(mut self) -> Self { + self.interactivity().focusable = true; + self + } + fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self where Self: Sized, @@ -523,6 +514,13 @@ pub type KeyUpListener = pub type ActionListener = Box) + 'static>; +pub fn node() -> Node { + Node { + interactivity: Interactivity::default(), + children: Vec::default(), + } +} + pub struct Node { interactivity: Interactivity, children: Vec>, @@ -1203,8 +1201,8 @@ impl GroupBounds { } pub struct Focusable { - view_type: PhantomData, element: E, + view_type: PhantomData, } impl> FocusableComponent for Focusable {} @@ -1265,8 +1263,8 @@ where } pub struct Stateful { - view_type: PhantomData, element: E, + view_type: PhantomData, } impl StatefulInteractiveComponent for Stateful