From 2e7356a53e7750e746a3fe84414d72892f124c1e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 29 Aug 2023 21:43:48 -0600 Subject: [PATCH] Store new_parents on Window instead of layout context --- crates/editor/src/element.rs | 32 ++++---------------------------- crates/gpui/src/app.rs | 12 ++---------- crates/gpui/src/app/window.rs | 15 ++++++++------- crates/gpui/src/elements.rs | 9 +-------- crates/gpui/src/elements/list.rs | 31 +++++-------------------------- crates/gpui/src/elements/text.rs | 9 ++------- 6 files changed, 22 insertions(+), 86 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 62f4c8c806..32b84a76c1 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -3204,14 +3204,8 @@ mod tests { Point::new(5, 6)..Point::new(6, 0), ]); }); - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false); element.layout( SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), editor, @@ -3296,14 +3290,8 @@ mod tests { DisplayPoint::new(10, 0)..DisplayPoint::new(13, 0), ]); }); - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false); element.layout( SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), editor, @@ -3363,14 +3351,8 @@ mod tests { let mut element = EditorElement::new(editor.read_with(cx, |editor, cx| editor.style(cx))); let (size, mut state) = editor.update(cx, |editor, cx| { - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false); element.layout( SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), editor, @@ -3567,14 +3549,8 @@ mod tests { editor.set_soft_wrap_mode(language_settings::SoftWrap::EditorWidth, cx); editor.set_wrap_width(Some(editor_width), cx); - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false); element.layout( SizeConstraint::new(vec2f(editor_width, 500.), vec2f(editor_width, 500.)), editor, diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 2a12470a40..552f57ba91 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -3461,7 +3461,6 @@ pub trait RenderContext<'a, 'b, V> { pub struct LayoutContext<'a, 'b, 'c, V> { // Nathan: Making this is public while I work on playground. pub view_context: &'c mut ViewContext<'a, 'b, V>, - new_parents: &'c mut HashMap, views_to_notify_if_ancestors_change: &'c mut HashMap>, pub refreshing: bool, } @@ -3469,13 +3468,11 @@ pub struct LayoutContext<'a, 'b, 'c, V> { impl<'a, 'b, 'c, V> LayoutContext<'a, 'b, 'c, V> { pub fn new( view_context: &'c mut ViewContext<'a, 'b, V>, - new_parents: &'c mut HashMap, views_to_notify_if_ancestors_change: &'c mut HashMap>, refreshing: bool, ) -> Self { Self { view_context, - new_parents, views_to_notify_if_ancestors_change, refreshing, } @@ -6529,14 +6526,9 @@ mod tests { view_1.update(cx, |_, cx| { view_2.update(cx, |_, cx| { // Sanity check - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = + LayoutContext::new(cx, &mut notify_views_if_parents_change, false); assert_eq!( layout_cx .keystrokes_for_action(view_1_id, &Action1) diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index f912ee46f5..c62722ac2d 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -56,6 +56,7 @@ pub struct Window { pub(crate) rendered_views: HashMap>, pub(crate) text_style_stack: Vec, pub(crate) theme_stack: Vec>, + pub(crate) new_parents: HashMap, titlebar_height: f32, appearance: Appearance, cursor_regions: Vec, @@ -92,6 +93,9 @@ impl Window { is_fullscreen: false, platform_window, rendered_views: Default::default(), + text_style_stack: Vec::new(), + theme_stack: Vec::new(), + new_parents: HashMap::default(), cursor_regions: Default::default(), mouse_regions: Default::default(), event_handlers: Default::default(), @@ -103,8 +107,6 @@ impl Window { clicked_region: None, titlebar_height, appearance, - text_style_stack: Vec::new(), - theme_stack: Vec::new(), }; let mut window_context = WindowContext::mutable(cx, &mut window, handle); @@ -998,11 +1000,9 @@ impl<'a> WindowContext<'a> { let mut rendered_root = self.window.rendered_views.remove(&root_view_id).unwrap(); - let mut new_parents = HashMap::default(); let mut views_to_notify_if_ancestors_change = HashMap::default(); rendered_root.layout( SizeConstraint::new(window_size, window_size), - &mut new_parents, &mut views_to_notify_if_ancestors_change, refreshing, self, @@ -1012,7 +1012,7 @@ impl<'a> WindowContext<'a> { let mut current_view_id = view_id; loop { let old_parent_id = self.window.parents.get(¤t_view_id); - let new_parent_id = new_parents.get(¤t_view_id); + let new_parent_id = self.window.new_parents.get(¤t_view_id); if old_parent_id.is_none() && new_parent_id.is_none() { break; } else if old_parent_id == new_parent_id { @@ -1027,6 +1027,7 @@ impl<'a> WindowContext<'a> { } } + let new_parents = mem::take(&mut self.window.new_parents); let old_parents = mem::replace(&mut self.window.parents, new_parents); self.window .rendered_views @@ -1634,11 +1635,11 @@ impl Element for ChildView { cx: &mut LayoutContext, ) -> (Vector2F, Self::LayoutState) { if let Some(mut rendered_view) = cx.window.rendered_views.remove(&self.view_id) { - cx.new_parents.insert(self.view_id, cx.view_id()); + let parent_id = cx.view_id(); + cx.window.new_parents.insert(self.view_id, parent_id); let size = rendered_view .layout( constraint, - cx.new_parents, cx.views_to_notify_if_ancestors_change, cx.refreshing, cx.view_context, diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index ca0a0b3586..9d5adab3ca 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -649,7 +649,6 @@ pub trait AnyRootElement { fn layout( &mut self, constraint: SizeConstraint, - new_parents: &mut HashMap, views_to_notify_if_ancestors_change: &mut HashMap>, refreshing: bool, cx: &mut WindowContext, @@ -674,7 +673,6 @@ impl AnyRootElement for RootElement { fn layout( &mut self, constraint: SizeConstraint, - new_parents: &mut HashMap, views_to_notify_if_ancestors_change: &mut HashMap>, refreshing: bool, cx: &mut WindowContext, @@ -684,12 +682,7 @@ impl AnyRootElement for RootElement { .upgrade(cx) .ok_or_else(|| anyhow!("layout called on a root element for a dropped view"))?; view.update(cx, |view, cx| { - let mut cx = LayoutContext::new( - cx, - new_parents, - views_to_notify_if_ancestors_change, - refreshing, - ); + let mut cx = LayoutContext::new(cx, views_to_notify_if_ancestors_change, refreshing); Ok(self.element.layout(constraint, view, &mut cx)) }) } diff --git a/crates/gpui/src/elements/list.rs b/crates/gpui/src/elements/list.rs index fd035891a1..34c70eaab7 100644 --- a/crates/gpui/src/elements/list.rs +++ b/crates/gpui/src/elements/list.rs @@ -666,14 +666,8 @@ mod tests { }); let mut list = List::new(state.clone()); - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false); let (size, _) = list.layout(constraint, &mut view, &mut layout_cx); assert_eq!(size, vec2f(100., 40.)); assert_eq!( @@ -698,12 +692,7 @@ mod tests { cx, ); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false); let (_, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx); assert_eq!( logical_scroll_top, @@ -728,12 +717,7 @@ mod tests { } ); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false); let (size, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx); assert_eq!(size, vec2f(100., 40.)); assert_eq!( @@ -852,14 +836,9 @@ mod tests { let mut list = List::new(state.clone()); let window_size = vec2f(width, height); - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = + LayoutContext::new(cx, &mut notify_views_if_parents_change, false); let (size, logical_scroll_top) = list.layout( SizeConstraint::new(vec2f(0., 0.), window_size), &mut view, diff --git a/crates/gpui/src/elements/text.rs b/crates/gpui/src/elements/text.rs index 38aa75a9ed..2db0888029 100644 --- a/crates/gpui/src/elements/text.rs +++ b/crates/gpui/src/elements/text.rs @@ -411,14 +411,9 @@ mod tests { let mut view = TestView; fonts::with_font_cache(cx.font_cache().clone(), || { let mut text = Text::new("Hello\r\n", Default::default()).with_soft_wrap(true); - let mut new_parents = Default::default(); let mut notify_views_if_parents_change = Default::default(); - let mut layout_cx = LayoutContext::new( - cx, - &mut new_parents, - &mut notify_views_if_parents_change, - false, - ); + let mut layout_cx = + LayoutContext::new(cx, &mut notify_views_if_parents_change, false); let (_, state) = text.layout( SizeConstraint::new(Default::default(), vec2f(f32::INFINITY, f32::INFINITY)), &mut view,