From 9382a304c4fe4404920905ff7436cf72e1a0620a Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 13 Nov 2023 23:03:14 -0700 Subject: [PATCH] Checkpoint --- crates/gpui2/src/elements/node.rs | 94 +++++++++++++----------------- crates/ui2/src/components/modal.rs | 2 +- 2 files changed, 41 insertions(+), 55 deletions(-) diff --git a/crates/gpui2/src/elements/node.rs b/crates/gpui2/src/elements/node.rs index 7129220e6a714c9389299b3c63251bcf63f9c6c0..f69ad25ab0e4c2aae8dfa577c0af1b9c04c50979 100644 --- a/crates/gpui2/src/elements/node.rs +++ b/crates/gpui2/src/elements/node.rs @@ -1,9 +1,9 @@ use crate::{ point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext, - BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId, FocusHandle, KeyContext, - KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, - Pixels, Point, Render, ScrollWheelEvent, SharedString, Size, Style, StyleRefinement, Styled, - Task, View, ViewContext, Visibility, + BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, ElementId, FocusEvent, FocusHandle, + KeyContext, KeyDownEvent, KeyUpEvent, LayoutId, MouseButton, MouseDownEvent, MouseMoveEvent, + MouseUpEvent, Pixels, Point, Render, ScrollWheelEvent, SharedString, Size, Style, + StyleRefinement, Styled, Task, View, ViewContext, Visibility, }; use collections::HashMap; use parking_lot::Mutex; @@ -371,14 +371,12 @@ pub trait StatefulInteractiveComponent>: InteractiveCo } } -pub trait FocusableComponent { - fn focusability(&mut self) -> &mut Focusability; - +pub trait FocusableComponent: InteractiveComponent { fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self where Self: Sized, { - self.focusability().focus_style = f(StyleRefinement::default()); + self.interactivity().focus_style = f(StyleRefinement::default()); self } @@ -386,7 +384,7 @@ pub trait FocusableComponent { where Self: Sized, { - self.focusability().focus_in_style = f(StyleRefinement::default()); + self.interactivity().focus_in_style = f(StyleRefinement::default()); self } @@ -394,7 +392,7 @@ pub trait FocusableComponent { where Self: Sized, { - self.focusability().in_focus_style = f(StyleRefinement::default()); + self.interactivity().in_focus_style = f(StyleRefinement::default()); self } @@ -405,13 +403,13 @@ pub trait FocusableComponent { where Self: Sized, { - self.focusability() - .focus_listeners - .push(Box::new(move |view, focus_handle, event, cx| { + self.interactivity().focus_listeners.push(Box::new( + move |view, focus_handle, event, cx| { if event.focused.as_ref() == Some(focus_handle) { listener(view, event, cx) } - })); + }, + )); self } @@ -422,13 +420,13 @@ pub trait FocusableComponent { where Self: Sized, { - self.focusability() - .focus_listeners - .push(Box::new(move |view, focus_handle, event, cx| { + self.interactivity().focus_listeners.push(Box::new( + move |view, focus_handle, event, cx| { if event.blurred.as_ref() == Some(focus_handle) { listener(view, event, cx) } - })); + }, + )); self } @@ -439,9 +437,8 @@ pub trait FocusableComponent { where Self: Sized, { - self.focusability() - .focus_listeners - .push(Box::new(move |view, focus_handle, event, cx| { + self.interactivity().focus_listeners.push(Box::new( + move |view, focus_handle, event, cx| { let descendant_blurred = event .blurred .as_ref() @@ -454,7 +451,8 @@ pub trait FocusableComponent { if !descendant_blurred && descendant_focused { listener(view, event, cx) } - })); + }, + )); self } @@ -465,9 +463,8 @@ pub trait FocusableComponent { where Self: Sized, { - self.focusability() - .focus_listeners - .push(Box::new(move |view, focus_handle, event, cx| { + self.interactivity().focus_listeners.push(Box::new( + move |view, focus_handle, event, cx| { let descendant_blurred = event .blurred .as_ref() @@ -479,7 +476,8 @@ pub trait FocusableComponent { if descendant_blurred && !descendant_focused { listener(view, event, cx) } - })); + }, + )); self } } @@ -525,11 +523,6 @@ pub type KeyUpListener = pub type ActionListener = Box) + 'static>; -pub struct FocusEvent { - pub blurred: Option, - pub focused: Option, -} - pub struct Node { interactivity: Interactivity, children: Vec>, @@ -658,8 +651,9 @@ pub struct NodeState { pub struct Interactivity { element_id: Option, key_context: KeyContext, - tracked_focus_handle: Option, focusable: bool, + tracked_focus_handle: Option, + focus_listeners: FocusListeners, scroll_offset: Point, group: Option, base_style: StyleRefinement, @@ -1007,7 +1001,7 @@ where cx.with_element_id(self.element_id.clone(), |cx| { cx.with_key_dispatch( self.key_context.clone(), - self.tracked_focus_handle.clone(), + element_state.focus_handle.clone(), |_, cx| { for listener in self.key_down_listeners.drain(..) { cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| { @@ -1025,6 +1019,15 @@ where cx.on_action(action_type, listener) } + if let Some(focus_handle) = element_state.focus_handle.as_ref() { + for listener in self.focus_listeners.drain(..) { + let focus_handle = focus_handle.clone(); + cx.on_focus_changed(move |view, event, cx| { + listener(view, &focus_handle, event, cx) + }); + } + } + f(style, self.scroll_offset, cx) }, ); @@ -1114,7 +1117,9 @@ impl Default for Interactivity { Self { element_id: None, key_context: KeyContext::default(), + focusable: false, tracked_focus_handle: None, + focus_listeners: SmallVec::default(), scroll_offset: Point::default(), group: None, base_style: StyleRefinement::default(), @@ -1139,7 +1144,6 @@ impl Default for Interactivity { drag_listener: None, hover_listener: None, tooltip_builder: None, - focusable: false, } } } @@ -1199,24 +1203,11 @@ impl GroupBounds { } pub struct Focusable { - focusability: Focusability, view_type: PhantomData, element: E, } -pub struct Focusability { - focus_handle: Option, - focus_listeners: FocusListeners, - focus_style: StyleRefinement, - focus_in_style: StyleRefinement, - in_focus_style: StyleRefinement, -} - -impl FocusableComponent for Focusable { - fn focusability(&mut self) -> &mut Focusability { - &mut self.focusability - } -} +impl> FocusableComponent for Focusable {} impl InteractiveComponent for Focusable where @@ -1274,7 +1265,6 @@ where } pub struct Stateful { - id: SharedString, view_type: PhantomData, element: E, } @@ -1297,11 +1287,7 @@ where } } -impl> FocusableComponent for Stateful { - fn focusability(&mut self) -> &mut Focusability { - self.element.focusability() - } -} +impl> FocusableComponent for Stateful {} impl Element for Stateful where diff --git a/crates/ui2/src/components/modal.rs b/crates/ui2/src/components/modal.rs index 805bbe95b2b4d4798a380efad6e2bc68f623f3fa..75528b5c3460defeb139f7a16d8d311d87a5093b 100644 --- a/crates/ui2/src/components/modal.rs +++ b/crates/ui2/src/components/modal.rs @@ -1,4 +1,4 @@ -use gpui::{AnyElement, Pixels}; +use gpui::AnyElement; use smallvec::SmallVec; use crate::{h_stack, prelude::*, v_stack, Button, Icon, IconButton, Label};