From 53a50b8df45ad778f04120ea580b17710669049f Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 8 Sep 2023 13:27:29 -0600 Subject: [PATCH] Move refreshing to window --- crates/editor/src/element.rs | 8 ++++---- crates/gpui/src/app.rs | 10 +++------- crates/gpui/src/app/window.rs | 12 ++++++++++-- crates/gpui/src/elements.rs | 16 +++------------- crates/gpui/src/elements/list.rs | 10 +++++----- crates/gpui/src/elements/text.rs | 2 +- crates/gpui2/src/layout_context.rs | 2 +- 7 files changed, 27 insertions(+), 33 deletions(-) diff --git a/crates/editor/src/element.rs b/crates/editor/src/element.rs index 9a6eb1116c1bcec556a35f875a9e0f046dbed54b..2d157761bfacee8ea1e94ded56b4009d1323fc50 100644 --- a/crates/editor/src/element.rs +++ b/crates/editor/src/element.rs @@ -3204,7 +3204,7 @@ mod tests { Point::new(5, 6)..Point::new(6, 0), ]); }); - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); element.layout( SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), editor, @@ -3289,7 +3289,7 @@ mod tests { DisplayPoint::new(10, 0)..DisplayPoint::new(13, 0), ]); }); - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); element.layout( SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), editor, @@ -3349,7 +3349,7 @@ 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 layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); element.layout( SizeConstraint::new(vec2f(500., 500.), vec2f(500., 500.)), editor, @@ -3546,7 +3546,7 @@ mod tests { editor.set_soft_wrap_mode(language_settings::SoftWrap::EditorWidth, cx); editor.set_wrap_width(Some(editor_width), cx); - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); 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 68b42f6e6e4ecd2fb870aaf87c93a20915323acc..b3f5ab1ff972eab104f7772925a967498418d414 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -3454,15 +3454,11 @@ impl BorrowWindowContext for ViewContext<'_, '_, V> { pub struct LayoutContext<'a, 'b, 'c, V> { // Nathan: Making this is public while I work on gpui2. pub view_context: &'c mut ViewContext<'a, 'b, V>, - pub refreshing: bool, } impl<'a, 'b, 'c, V> LayoutContext<'a, 'b, 'c, V> { - pub fn new(view_context: &'c mut ViewContext<'a, 'b, V>, refreshing: bool) -> Self { - Self { - view_context, - refreshing, - } + pub fn new(view_context: &'c mut ViewContext<'a, 'b, V>) -> Self { + Self { view_context } } pub fn view_context(&mut self) -> &mut ViewContext<'a, 'b, V> { @@ -6482,7 +6478,7 @@ mod tests { view_1.update(cx, |_, cx| { view_2.update(cx, |_, cx| { // Sanity check - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); 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 e0f29fea51b414a50d792f915fd0e8934d53fe0f..c1304f10cd586bcffe9014febc5d0f467a67452e 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -71,6 +71,7 @@ pub struct Window { pub(crate) clicked_region_ids: Vec, pub(crate) clicked_region: Option<(MouseRegionId, MouseButton)>, text_layout_cache: TextLayoutCache, + refreshing: bool, } impl Window { @@ -113,6 +114,7 @@ impl Window { clicked_region: None, titlebar_height, appearance, + refreshing: false, }; let mut window_context = WindowContext::mutable(cx, &mut window, handle); @@ -291,6 +293,10 @@ impl<'a> WindowContext<'a> { self.window.platform_window.mouse_position() } + pub fn refreshing(&self) -> bool { + self.window.refreshing + } + pub fn text_layout_cache(&self) -> &TextLayoutCache { &self.window.text_layout_cache } @@ -1034,7 +1040,9 @@ impl<'a> WindowContext<'a> { let mut rendered_root = self.window.rendered_views.remove(&root_view_id).unwrap(); - rendered_root.layout(SizeConstraint::strict(window_size), refreshing, self)?; + self.window.refreshing = refreshing; + rendered_root.layout(SizeConstraint::strict(window_size), self)?; + self.window.refreshing = false; let views_to_notify_if_ancestors_change = mem::take(&mut self.window.views_to_notify_if_ancestors_change); @@ -1671,7 +1679,7 @@ impl Element for ChildView { let parent_id = cx.view_id(); cx.window.new_parents.insert(self.view_id, parent_id); let size = rendered_view - .layout(constraint, cx.refreshing, cx.view_context) + .layout(constraint, cx.view_context) .log_err() .unwrap_or(Vector2F::zero()); cx.window.rendered_views.insert(self.view_id, rendered_view); diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index da714907c359e6ab4b2b17e3056fb3ab378bf144..790581eb9f4af667618db984e51836c37e064bca 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -644,12 +644,7 @@ impl RootElement { } pub trait AnyRootElement { - fn layout( - &mut self, - constraint: SizeConstraint, - refreshing: bool, - cx: &mut WindowContext, - ) -> Result; + fn layout(&mut self, constraint: SizeConstraint, cx: &mut WindowContext) -> Result; fn paint( &mut self, scene: &mut SceneBuilder, @@ -667,18 +662,13 @@ pub trait AnyRootElement { } impl AnyRootElement for RootElement { - fn layout( - &mut self, - constraint: SizeConstraint, - refreshing: bool, - cx: &mut WindowContext, - ) -> Result { + fn layout(&mut self, constraint: SizeConstraint, cx: &mut WindowContext) -> Result { let view = self .view .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, refreshing); + let mut cx = LayoutContext::new(cx); 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 1f1b2b9fb4f0db55be2e58f640c88d9bd4322f36..bcb80d8c7ecc60335045e36e664eaccadd5a9c32 100644 --- a/crates/gpui/src/elements/list.rs +++ b/crates/gpui/src/elements/list.rs @@ -108,7 +108,7 @@ impl Element for List { item_constraint.min.set_y(0.); item_constraint.max.set_y(f32::INFINITY); - if cx.refreshing || state.last_layout_width != Some(size.x()) { + if cx.refreshing() || state.last_layout_width != Some(size.x()) { state.rendered_range = 0..0; state.items = SumTree::from_iter( (0..state.items.summary().count).map(|_| ListItem::Unrendered), @@ -666,7 +666,7 @@ mod tests { }); let mut list = List::new(state.clone()); - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); let (size, _) = list.layout(constraint, &mut view, &mut layout_cx); assert_eq!(size, vec2f(100., 40.)); assert_eq!( @@ -691,7 +691,7 @@ mod tests { cx, ); - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); let (_, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx); assert_eq!( logical_scroll_top, @@ -716,7 +716,7 @@ mod tests { } ); - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); let (size, logical_scroll_top) = list.layout(constraint, &mut view, &mut layout_cx); assert_eq!(size, vec2f(100., 40.)); assert_eq!( @@ -835,7 +835,7 @@ mod tests { let mut list = List::new(state.clone()); let window_size = vec2f(width, height); - let mut layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); 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 3e9214f12c92b36a8947e72318147b5e8dc025da..3f5888345604a32138a60c4969e853472d502330 100644 --- a/crates/gpui/src/elements/text.rs +++ b/crates/gpui/src/elements/text.rs @@ -411,7 +411,7 @@ 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 layout_cx = LayoutContext::new(cx, false); + let mut layout_cx = LayoutContext::new(cx); let (_, state) = text.layout( SizeConstraint::new(Default::default(), vec2f(f32::INFINITY, f32::INFINITY)), &mut view, diff --git a/crates/gpui2/src/layout_context.rs b/crates/gpui2/src/layout_context.rs index c82a6f89b88454642e363000be851bfe130fbe0d..0249246514e015ab14a515963c10035c1742f2b6 100644 --- a/crates/gpui2/src/layout_context.rs +++ b/crates/gpui2/src/layout_context.rs @@ -1,7 +1,7 @@ use crate::{element::LayoutId, style::Style}; use anyhow::{anyhow, Result}; use derive_more::{Deref, DerefMut}; -use gpui::{geometry::Size, taffy::style::Overflow, MeasureParams}; +use gpui::{geometry::Size, MeasureParams}; pub use gpui::{taffy::tree::NodeId, LayoutContext as LegacyLayoutContext}; #[derive(Deref, DerefMut)]