From 4a3a1ad0c35ff622eb8c966e984652f4f0968db2 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 13 Nov 2023 22:42:19 -0700 Subject: [PATCH] Checkpoint --- crates/editor2/src/element.rs | 2 +- crates/gpui2/src/elements/div.rs | 40 +++++++++++-------------------- crates/gpui2/src/elements/node.rs | 2 ++ crates/gpui2/src/view.rs | 6 ++--- crates/gpui2/src/window.rs | 25 +++++++++++-------- 5 files changed, 35 insertions(+), 40 deletions(-) diff --git a/crates/editor2/src/element.rs b/crates/editor2/src/element.rs index 630b062d1f..8ee99e19af 100644 --- a/crates/editor2/src/element.rs +++ b/crates/editor2/src/element.rs @@ -2455,7 +2455,7 @@ impl Element for EditorElement { editor.style = Some(self.style.clone()); // Long-term, we'd like to eliminate this. let dispatch_context = editor.dispatch_context(cx); - cx.with_element_id(cx.view().entity_id(), |global_id, cx| { + cx.with_element_id(Some(cx.view().entity_id()), |cx| { cx.with_key_dispatch( dispatch_context, Some(editor.focus_handle.clone()), diff --git a/crates/gpui2/src/elements/div.rs b/crates/gpui2/src/elements/div.rs index 2b411f7609..1d85450c29 100644 --- a/crates/gpui2/src/elements/div.rs +++ b/crates/gpui2/src/elements/div.rs @@ -2,7 +2,7 @@ use std::fmt::Debug; use crate::{ point, AnyElement, BorrowWindow, Bounds, Component, Element, ElementId, ElementInteractivity, - FocusHandle, FocusListeners, Focusable, FocusableKeyDispatch, GlobalElementId, GroupBounds, + FocusHandle, FocusListeners, Focusable, FocusableKeyDispatch, GroupBounds, InteractiveElementState, KeyContext, KeyDispatch, LayoutId, NonFocusableKeyDispatch, Overflow, ParentElement, Pixels, Point, SharedString, StatefulInteractive, StatefulInteractivity, StatelessInteractive, StatelessInteractivity, Style, StyleRefinement, Styled, ViewContext, @@ -93,18 +93,6 @@ where self } - fn with_element_id( - &mut self, - cx: &mut ViewContext, - f: impl FnOnce(&mut Self, Option, &mut ViewContext) -> R, - ) -> R { - if let Some(id) = self.id() { - cx.with_element_id(id, |global_id, cx| f(self, Some(global_id), cx)) - } else { - f(self, None, cx) - } - } - pub fn compute_style( &self, bounds: Bounds, @@ -229,14 +217,14 @@ where cx: &mut ViewContext, ) -> Self::ElementState { let mut element_state = element_state.unwrap_or_default(); - self.with_element_id(cx, |this, _global_id, cx| { - this.key_dispatch.initialize( + cx.with_element_id(self.id(), |cx| { + self.key_dispatch.initialize( element_state.focus_handle.take(), cx, |focus_handle, cx| { - this.interactivity.initialize(cx); + self.interactivity.initialize(cx); element_state.focus_handle = focus_handle; - for child in &mut this.children { + for child in &mut self.children { child.initialize(view_state, cx); } }, @@ -253,8 +241,8 @@ where ) -> LayoutId { let style = self.compute_style(Bounds::default(), element_state, cx); style.apply_text_style(cx, |cx| { - self.with_element_id(cx, |this, _global_id, cx| { - let layout_ids = this + cx.with_element_id(self.id(), |cx| { + let layout_ids = self .children .iter_mut() .map(|child| child.layout(view_state, cx)) @@ -272,8 +260,8 @@ where element_state: &mut Self::ElementState, cx: &mut ViewContext, ) { - self.with_element_id(cx, |this, _global_id, cx| { - let style = this.compute_style(bounds, element_state, cx); + cx.with_element_id(self.id(), |cx| { + let style = self.compute_style(bounds, element_state, cx); if style.visibility == Visibility::Hidden { return; } @@ -285,7 +273,7 @@ where } } - if let Some(group) = this.group.clone() { + if let Some(group) = self.group.clone() { GroupBounds::push(group, bounds, cx); } @@ -308,8 +296,8 @@ where cx.with_z_index(z_index, |cx| { cx.with_z_index(0, |cx| { style.paint(bounds, cx); - this.key_dispatch.paint(bounds, cx); - this.interactivity.handle_events( + self.key_dispatch.paint(bounds, cx); + self.interactivity.handle_events( bounds, content_size, style.overflow, @@ -322,7 +310,7 @@ where 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 this.children { + for child in &mut self.children { child.paint(view_state, cx); } }); @@ -331,7 +319,7 @@ where }); }); - if let Some(group) = this.group.as_ref() { + if let Some(group) = self.group.as_ref() { GroupBounds::pop(group, cx); } }) diff --git a/crates/gpui2/src/elements/node.rs b/crates/gpui2/src/elements/node.rs index d5bfd63647..f3d2b38dfb 100644 --- a/crates/gpui2/src/elements/node.rs +++ b/crates/gpui2/src/elements/node.rs @@ -1008,6 +1008,8 @@ 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(), diff --git a/crates/gpui2/src/view.rs b/crates/gpui2/src/view.rs index d12d84f43b..3299ce7d45 100644 --- a/crates/gpui2/src/view.rs +++ b/crates/gpui2/src/view.rs @@ -286,7 +286,7 @@ mod any_view { use std::any::Any; pub(crate) fn initialize(view: &AnyView, cx: &mut WindowContext) -> Box { - cx.with_element_id(view.model.entity_id, |_, cx| { + cx.with_element_id(Some(view.model.entity_id), |cx| { let view = view.clone().downcast::().unwrap(); let element = view.update(cx, |view, cx| { let mut element = AnyElement::new(view.render(cx)); @@ -302,7 +302,7 @@ mod any_view { element: &mut Box, cx: &mut WindowContext, ) -> LayoutId { - cx.with_element_id(view.model.entity_id, |_, cx| { + cx.with_element_id(Some(view.model.entity_id), |cx| { let view = view.clone().downcast::().unwrap(); let element = element.downcast_mut::>().unwrap(); view.update(cx, |view, cx| element.layout(view, cx)) @@ -314,7 +314,7 @@ mod any_view { element: &mut Box, cx: &mut WindowContext, ) { - cx.with_element_id(view.model.entity_id, |_, cx| { + cx.with_element_id(Some(view.model.entity_id), |cx| { let view = view.clone().downcast::().unwrap(); let element = element.downcast_mut::>().unwrap(); view.update(cx, |view, cx| element.paint(view, cx)) diff --git a/crates/gpui2/src/window.rs b/crates/gpui2/src/window.rs index edda71f13f..c5d794aa3d 100644 --- a/crates/gpui2/src/window.rs +++ b/crates/gpui2/src/window.rs @@ -1537,16 +1537,19 @@ pub trait BorrowWindow: BorrowMut + BorrowMut { /// used to associate state with identified elements across separate frames. fn with_element_id( &mut self, - id: impl Into, - f: impl FnOnce(GlobalElementId, &mut Self) -> R, + id: Option>, + f: impl FnOnce(&mut Self) -> R, ) -> R { - let window = self.window_mut(); - window.element_id_stack.push(id.into()); - let global_id = window.element_id_stack.clone(); - let result = f(global_id, self); - let window: &mut Window = self.borrow_mut(); - window.element_id_stack.pop(); - result + if let Some(id) = id.map(Into::into) { + let window = self.window_mut(); + window.element_id_stack.push(id.into()); + let result = f(self); + let window: &mut Window = self.borrow_mut(); + window.element_id_stack.pop(); + result + } else { + f(self) + } } /// Invoke the given function with the given content mask after intersecting it @@ -1613,7 +1616,9 @@ pub trait BorrowWindow: BorrowMut + BorrowMut { where S: 'static, { - self.with_element_id(id, |global_id, cx| { + self.with_element_id(Some(id), |cx| { + let global_id = cx.window().element_id_stack.clone(); + if let Some(any) = cx .window_mut() .current_frame