Checkpoint

This commit is contained in:
Nathan Sobo 2023-11-13 22:42:19 -07:00
parent 54a817a5ab
commit 4a3a1ad0c3
5 changed files with 35 additions and 40 deletions

View file

@ -2455,7 +2455,7 @@ impl Element<Editor> 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()),

View file

@ -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<R>(
&mut self,
cx: &mut ViewContext<V>,
f: impl FnOnce(&mut Self, Option<GlobalElementId>, &mut ViewContext<V>) -> 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<Pixels>,
@ -229,14 +217,14 @@ where
cx: &mut ViewContext<V>,
) -> 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<V>,
) {
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);
}
})

View file

@ -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(),

View file

@ -286,7 +286,7 @@ mod any_view {
use std::any::Any;
pub(crate) fn initialize<V: Render>(view: &AnyView, cx: &mut WindowContext) -> Box<dyn Any> {
cx.with_element_id(view.model.entity_id, |_, cx| {
cx.with_element_id(Some(view.model.entity_id), |cx| {
let view = view.clone().downcast::<V>().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<dyn Any>,
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::<V>().unwrap();
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
view.update(cx, |view, cx| element.layout(view, cx))
@ -314,7 +314,7 @@ mod any_view {
element: &mut Box<dyn Any>,
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::<V>().unwrap();
let element = element.downcast_mut::<AnyElement<V>>().unwrap();
view.update(cx, |view, cx| element.paint(view, cx))

View file

@ -1537,16 +1537,19 @@ pub trait BorrowWindow: BorrowMut<Window> + BorrowMut<AppContext> {
/// used to associate state with identified elements across separate frames.
fn with_element_id<R>(
&mut self,
id: impl Into<ElementId>,
f: impl FnOnce(GlobalElementId, &mut Self) -> R,
id: Option<impl Into<ElementId>>,
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<Window> + BorrowMut<AppContext> {
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