Store new_parents on Window instead of layout context

This commit is contained in:
Nathan Sobo 2023-08-29 21:43:48 -06:00
parent 42cd257287
commit 2e7356a53e
6 changed files with 22 additions and 86 deletions

View file

@ -3204,14 +3204,8 @@ mod tests {
Point::new(5, 6)..Point::new(6, 0), 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 notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
cx,
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
element.layout( element.layout(
SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)),
editor, editor,
@ -3296,14 +3290,8 @@ mod tests {
DisplayPoint::new(10, 0)..DisplayPoint::new(13, 0), 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 notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
cx,
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
element.layout( element.layout(
SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)),
editor, editor,
@ -3363,14 +3351,8 @@ mod tests {
let mut element = EditorElement::new(editor.read_with(cx, |editor, cx| editor.style(cx))); let mut element = EditorElement::new(editor.read_with(cx, |editor, cx| editor.style(cx)));
let (size, mut state) = editor.update(cx, |editor, 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 notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
cx,
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
element.layout( element.layout(
SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)),
editor, editor,
@ -3567,14 +3549,8 @@ mod tests {
editor.set_soft_wrap_mode(language_settings::SoftWrap::EditorWidth, cx); editor.set_soft_wrap_mode(language_settings::SoftWrap::EditorWidth, cx);
editor.set_wrap_width(Some(editor_width), 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 notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
cx,
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
element.layout( element.layout(
SizeConstraint::new(vec2f(editor_width, 500.), vec2f(editor_width, 500.)), SizeConstraint::new(vec2f(editor_width, 500.), vec2f(editor_width, 500.)),
editor, editor,

View file

@ -3461,7 +3461,6 @@ pub trait RenderContext<'a, 'b, V> {
pub struct LayoutContext<'a, 'b, 'c, V> { pub struct LayoutContext<'a, 'b, 'c, V> {
// Nathan: Making this is public while I work on playground. // Nathan: Making this is public while I work on playground.
pub view_context: &'c mut ViewContext<'a, 'b, V>, pub view_context: &'c mut ViewContext<'a, 'b, V>,
new_parents: &'c mut HashMap<usize, usize>,
views_to_notify_if_ancestors_change: &'c mut HashMap<usize, SmallVec<[usize; 2]>>, views_to_notify_if_ancestors_change: &'c mut HashMap<usize, SmallVec<[usize; 2]>>,
pub refreshing: bool, pub refreshing: bool,
} }
@ -3469,13 +3468,11 @@ pub struct LayoutContext<'a, 'b, 'c, V> {
impl<'a, 'b, 'c, V> LayoutContext<'a, 'b, 'c, V> { impl<'a, 'b, 'c, V> LayoutContext<'a, 'b, 'c, V> {
pub fn new( pub fn new(
view_context: &'c mut ViewContext<'a, 'b, V>, view_context: &'c mut ViewContext<'a, 'b, V>,
new_parents: &'c mut HashMap<usize, usize>,
views_to_notify_if_ancestors_change: &'c mut HashMap<usize, SmallVec<[usize; 2]>>, views_to_notify_if_ancestors_change: &'c mut HashMap<usize, SmallVec<[usize; 2]>>,
refreshing: bool, refreshing: bool,
) -> Self { ) -> Self {
Self { Self {
view_context, view_context,
new_parents,
views_to_notify_if_ancestors_change, views_to_notify_if_ancestors_change,
refreshing, refreshing,
} }
@ -6529,14 +6526,9 @@ mod tests {
view_1.update(cx, |_, cx| { view_1.update(cx, |_, cx| {
view_2.update(cx, |_, cx| { view_2.update(cx, |_, cx| {
// Sanity check // Sanity check
let mut new_parents = Default::default();
let mut notify_views_if_parents_change = Default::default(); let mut notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx =
cx, LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
assert_eq!( assert_eq!(
layout_cx layout_cx
.keystrokes_for_action(view_1_id, &Action1) .keystrokes_for_action(view_1_id, &Action1)

View file

@ -56,6 +56,7 @@ pub struct Window {
pub(crate) rendered_views: HashMap<usize, Box<dyn AnyRootElement>>, pub(crate) rendered_views: HashMap<usize, Box<dyn AnyRootElement>>,
pub(crate) text_style_stack: Vec<TextStyle>, pub(crate) text_style_stack: Vec<TextStyle>,
pub(crate) theme_stack: Vec<Box<dyn Any>>, pub(crate) theme_stack: Vec<Box<dyn Any>>,
pub(crate) new_parents: HashMap<usize, usize>,
titlebar_height: f32, titlebar_height: f32,
appearance: Appearance, appearance: Appearance,
cursor_regions: Vec<CursorRegion>, cursor_regions: Vec<CursorRegion>,
@ -92,6 +93,9 @@ impl Window {
is_fullscreen: false, is_fullscreen: false,
platform_window, platform_window,
rendered_views: Default::default(), rendered_views: Default::default(),
text_style_stack: Vec::new(),
theme_stack: Vec::new(),
new_parents: HashMap::default(),
cursor_regions: Default::default(), cursor_regions: Default::default(),
mouse_regions: Default::default(), mouse_regions: Default::default(),
event_handlers: Default::default(), event_handlers: Default::default(),
@ -103,8 +107,6 @@ impl Window {
clicked_region: None, clicked_region: None,
titlebar_height, titlebar_height,
appearance, appearance,
text_style_stack: Vec::new(),
theme_stack: Vec::new(),
}; };
let mut window_context = WindowContext::mutable(cx, &mut window, handle); 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 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(); let mut views_to_notify_if_ancestors_change = HashMap::default();
rendered_root.layout( rendered_root.layout(
SizeConstraint::new(window_size, window_size), SizeConstraint::new(window_size, window_size),
&mut new_parents,
&mut views_to_notify_if_ancestors_change, &mut views_to_notify_if_ancestors_change,
refreshing, refreshing,
self, self,
@ -1012,7 +1012,7 @@ impl<'a> WindowContext<'a> {
let mut current_view_id = view_id; let mut current_view_id = view_id;
loop { loop {
let old_parent_id = self.window.parents.get(&current_view_id); let old_parent_id = self.window.parents.get(&current_view_id);
let new_parent_id = new_parents.get(&current_view_id); let new_parent_id = self.window.new_parents.get(&current_view_id);
if old_parent_id.is_none() && new_parent_id.is_none() { if old_parent_id.is_none() && new_parent_id.is_none() {
break; break;
} else if old_parent_id == new_parent_id { } 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); let old_parents = mem::replace(&mut self.window.parents, new_parents);
self.window self.window
.rendered_views .rendered_views
@ -1634,11 +1635,11 @@ impl<V: 'static> Element<V> for ChildView {
cx: &mut LayoutContext<V>, cx: &mut LayoutContext<V>,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
if let Some(mut rendered_view) = cx.window.rendered_views.remove(&self.view_id) { 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 let size = rendered_view
.layout( .layout(
constraint, constraint,
cx.new_parents,
cx.views_to_notify_if_ancestors_change, cx.views_to_notify_if_ancestors_change,
cx.refreshing, cx.refreshing,
cx.view_context, cx.view_context,

View file

@ -649,7 +649,6 @@ pub trait AnyRootElement {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
new_parents: &mut HashMap<usize, usize>,
views_to_notify_if_ancestors_change: &mut HashMap<usize, SmallVec<[usize; 2]>>, views_to_notify_if_ancestors_change: &mut HashMap<usize, SmallVec<[usize; 2]>>,
refreshing: bool, refreshing: bool,
cx: &mut WindowContext, cx: &mut WindowContext,
@ -674,7 +673,6 @@ impl<V: View> AnyRootElement for RootElement<V> {
fn layout( fn layout(
&mut self, &mut self,
constraint: SizeConstraint, constraint: SizeConstraint,
new_parents: &mut HashMap<usize, usize>,
views_to_notify_if_ancestors_change: &mut HashMap<usize, SmallVec<[usize; 2]>>, views_to_notify_if_ancestors_change: &mut HashMap<usize, SmallVec<[usize; 2]>>,
refreshing: bool, refreshing: bool,
cx: &mut WindowContext, cx: &mut WindowContext,
@ -684,12 +682,7 @@ impl<V: View> AnyRootElement for RootElement<V> {
.upgrade(cx) .upgrade(cx)
.ok_or_else(|| anyhow!("layout called on a root element for a dropped view"))?; .ok_or_else(|| anyhow!("layout called on a root element for a dropped view"))?;
view.update(cx, |view, cx| { view.update(cx, |view, cx| {
let mut cx = LayoutContext::new( let mut cx = LayoutContext::new(cx, views_to_notify_if_ancestors_change, refreshing);
cx,
new_parents,
views_to_notify_if_ancestors_change,
refreshing,
);
Ok(self.element.layout(constraint, view, &mut cx)) Ok(self.element.layout(constraint, view, &mut cx))
}) })
} }

View file

@ -666,14 +666,8 @@ mod tests {
}); });
let mut list = List::new(state.clone()); let mut list = List::new(state.clone());
let mut new_parents = Default::default();
let mut notify_views_if_parents_change = Default::default(); let mut notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
cx,
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
let (size, _) = list.layout(constraint, &mut view, &mut layout_cx); let (size, _) = list.layout(constraint, &mut view, &mut layout_cx);
assert_eq!(size, vec2f(100., 40.)); assert_eq!(size, vec2f(100., 40.));
assert_eq!( assert_eq!(
@ -698,12 +692,7 @@ mod tests {
cx, cx,
); );
let mut layout_cx = LayoutContext::new( let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
cx,
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
let (_, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx); let (_, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx);
assert_eq!( assert_eq!(
logical_scroll_top, logical_scroll_top,
@ -728,12 +717,7 @@ mod tests {
} }
); );
let mut layout_cx = LayoutContext::new( let mut layout_cx = LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
cx,
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
let (size, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx); let (size, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx);
assert_eq!(size, vec2f(100., 40.)); assert_eq!(size, vec2f(100., 40.));
assert_eq!( assert_eq!(
@ -852,14 +836,9 @@ mod tests {
let mut list = List::new(state.clone()); let mut list = List::new(state.clone());
let window_size = vec2f(width, height); let window_size = vec2f(width, height);
let mut new_parents = Default::default();
let mut notify_views_if_parents_change = Default::default(); let mut notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx =
cx, LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
let (size, logical_scroll_top) = list.layout( let (size, logical_scroll_top) = list.layout(
SizeConstraint::new(vec2f(0., 0.), window_size), SizeConstraint::new(vec2f(0., 0.), window_size),
&mut view, &mut view,

View file

@ -411,14 +411,9 @@ mod tests {
let mut view = TestView; let mut view = TestView;
fonts::with_font_cache(cx.font_cache().clone(), || { fonts::with_font_cache(cx.font_cache().clone(), || {
let mut text = Text::new("Hello\r\n", Default::default()).with_soft_wrap(true); 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 notify_views_if_parents_change = Default::default();
let mut layout_cx = LayoutContext::new( let mut layout_cx =
cx, LayoutContext::new(cx, &mut notify_views_if_parents_change, false);
&mut new_parents,
&mut notify_views_if_parents_change,
false,
);
let (_, state) = text.layout( let (_, state) = text.layout(
SizeConstraint::new(Default::default(), vec2f(f32::INFINITY, f32::INFINITY)), SizeConstraint::new(Default::default(), vec2f(f32::INFINITY, f32::INFINITY)),
&mut view, &mut view,