From 16683307645a355c95df6b39ac6ab87c3c73242e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 13 Nov 2023 22:51:44 -0700 Subject: [PATCH] Checkpoint --- crates/gpui2/src/elements/node.rs | 89 +++++++++++++++---------------- 1 file changed, 43 insertions(+), 46 deletions(-) diff --git a/crates/gpui2/src/elements/node.rs b/crates/gpui2/src/elements/node.rs index f3d2b38dfb..7129220e6a 100644 --- a/crates/gpui2/src/elements/node.rs +++ b/crates/gpui2/src/elements/node.rs @@ -1,6 +1,6 @@ use crate::{ point, px, Action, AnyDrag, AnyElement, AnyTooltip, AnyView, AppContext, BorrowAppContext, - BorrowWindow, Bounds, ClickEvent, DispatchPhase, Element, FocusHandle, KeyContext, + 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, @@ -550,8 +550,8 @@ impl InteractiveComponent for Node { impl Element for Node { type ElementState = NodeState; - fn id(&self) -> Option { - None + fn id(&self) -> Option { + self.interactivity.element_id.clone() } fn initialize( @@ -560,13 +560,12 @@ impl Element for Node { element_state: Option, cx: &mut ViewContext, ) -> Self::ElementState { - let interactive_state = - self.interactivity - .initialize(element_state.map(|s| s.interactive_state), cx, |cx| { - for child in &mut self.children { - child.initialize(view_state, cx); - } - }); + let interactive_state = self + .interactivity + .initialize(element_state.map(|s| s.interactive_state), cx); + for child in &mut self.children { + child.initialize(view_state, cx); + } NodeState { interactive_state, @@ -656,13 +655,8 @@ pub struct NodeState { interactive_state: InteractiveElementState, } -impl AsMut for InteractiveElementState { - fn as_mut(&mut self) -> &mut InteractiveElementState { - self - } -} - pub struct Interactivity { + element_id: Option, key_context: KeyContext, tracked_focus_handle: Option, focusable: bool, @@ -700,9 +694,9 @@ where &mut self, element_state: Option, cx: &mut ViewContext, - f: impl FnOnce(&mut ViewContext), ) -> InteractiveElementState { let mut element_state = element_state.unwrap_or_default(); + // Ensure we store a focus handle in our element state if we're focusable. // If there's an explicit focus handle we're tracking, use that. Otherwise // create a new handle and store it in the element state, which lives for as @@ -724,11 +718,13 @@ where f: impl FnOnce(Style, &mut ViewContext) -> LayoutId, ) -> LayoutId { let style = self.compute_style(None, element_state, cx); - cx.with_key_dispatch( - self.key_context.clone(), - self.tracked_focus_handle.clone(), - |_, cx| f(style, cx), - ) + cx.with_element_id(self.element_id.clone(), |cx| { + cx.with_key_dispatch( + self.key_context.clone(), + self.tracked_focus_handle.clone(), + |_, cx| f(style, cx), + ) + }) } fn paint( @@ -1008,31 +1004,31 @@ where GroupBounds::push(group, bounds, cx); } - todo!(); - // cx.with_element_id(self.i, f); - cx.with_key_dispatch( - self.key_context.clone(), - self.tracked_focus_handle.clone(), - |_, cx| { - for listener in self.key_down_listeners.drain(..) { - cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| { - listener(state, event, phase, cx); - }) - } + cx.with_element_id(self.element_id.clone(), |cx| { + cx.with_key_dispatch( + self.key_context.clone(), + self.tracked_focus_handle.clone(), + |_, cx| { + for listener in self.key_down_listeners.drain(..) { + cx.on_key_event(move |state, event: &KeyDownEvent, phase, cx| { + listener(state, event, phase, cx); + }) + } - for listener in self.key_up_listeners.drain(..) { - cx.on_key_event(move |state, event: &KeyUpEvent, phase, cx| { - listener(state, event, phase, cx); - }) - } + for listener in self.key_up_listeners.drain(..) { + cx.on_key_event(move |state, event: &KeyUpEvent, phase, cx| { + listener(state, event, phase, cx); + }) + } - for (action_type, listener) in self.action_listeners.drain(..) { - cx.on_action(action_type, listener) - } + for (action_type, listener) in self.action_listeners.drain(..) { + cx.on_action(action_type, listener) + } - f(style, self.scroll_offset, cx) - }, - ); + f(style, self.scroll_offset, cx) + }, + ); + }); if let Some(group) = self.group.as_ref() { GroupBounds::pop(group, cx); @@ -1116,6 +1112,7 @@ where impl Default for Interactivity { fn default() -> Self { Self { + element_id: None, key_context: KeyContext::default(), tracked_focus_handle: None, scroll_offset: Point::default(), @@ -1243,7 +1240,7 @@ where { type ElementState = E::ElementState; - fn id(&self) -> Option { + fn id(&self) -> Option { self.element.id() } @@ -1313,7 +1310,7 @@ where { type ElementState = E::ElementState; - fn id(&self) -> Option { + fn id(&self) -> Option { self.element.id() }