Make titlebar_height available during render

Max Brunsfeld and Antonio Scandurra created

Co-Authored-By: Antonio Scandurra <me@as-cii.com>

Change summary

gpui/src/app.rs       | 56 +++++++++++++++++++++++++++++++++-----------
gpui/src/presenter.rs | 17 ++++++++++---
2 files changed, 55 insertions(+), 18 deletions(-)

Detailed changes

gpui/src/app.rs 🔗

@@ -747,12 +747,12 @@ impl MutableAppContext {
         self.cx.focused_view_id(window_id)
     }
 
-    pub fn render_view(&self, window_id: usize, view_id: usize) -> Result<ElementBox> {
-        self.cx.render_view(window_id, view_id)
-    }
-
-    pub fn render_views(&self, window_id: usize) -> HashMap<usize, ElementBox> {
-        self.cx.render_views(window_id)
+    pub fn render_views(
+        &self,
+        window_id: usize,
+        titlebar_height: f32,
+    ) -> HashMap<usize, ElementBox> {
+        self.cx.render_views(window_id, titlebar_height)
     }
 
     pub fn update<T, F: FnOnce() -> T>(&mut self, callback: F) -> T {
@@ -1019,6 +1019,7 @@ impl MutableAppContext {
         let text_layout_cache = TextLayoutCache::new(self.cx.platform.fonts());
         let presenter = Rc::new(RefCell::new(Presenter::new(
             window_id,
+            window.titlebar_height(),
             self.cx.font_cache.clone(),
             text_layout_cache,
             self.assets.clone(),
@@ -1211,10 +1212,11 @@ impl MutableAppContext {
             {
                 {
                     let mut presenter = presenter.borrow_mut();
-                    presenter.invalidate(invalidation, self.as_ref());
+                    let titlebar_height = window.titlebar_height();
+                    presenter.invalidate(invalidation, titlebar_height, self.as_ref());
                     let scene = presenter.build_scene(
                         window.size(),
-                        window.titlebar_height(),
+                        titlebar_height,
                         window.scale_factor(),
                         self,
                     );
@@ -1540,19 +1542,31 @@ impl AppContext {
             .map(|window| window.focused_view_id)
     }
 
-    pub fn render_view(&self, window_id: usize, view_id: usize) -> Result<ElementBox> {
+    pub fn render_view(
+        &self,
+        window_id: usize,
+        view_id: usize,
+        titlebar_height: f32,
+    ) -> Result<ElementBox> {
         self.views
             .get(&(window_id, view_id))
-            .map(|v| v.render(window_id, view_id, self))
+            .map(|v| v.render(window_id, view_id, titlebar_height, self))
             .ok_or(anyhow!("view not found"))
     }
 
-    pub fn render_views(&self, window_id: usize) -> HashMap<usize, ElementBox> {
+    pub fn render_views(
+        &self,
+        window_id: usize,
+        titlebar_height: f32,
+    ) -> HashMap<usize, ElementBox> {
         self.views
             .iter()
             .filter_map(|((win_id, view_id), view)| {
                 if *win_id == window_id {
-                    Some((*view_id, view.render(*win_id, *view_id, self)))
+                    Some((
+                        *view_id,
+                        view.render(*win_id, *view_id, titlebar_height, self),
+                    ))
                 } else {
                     None
                 }
@@ -1703,7 +1717,13 @@ pub trait AnyView: Send + Sync {
     fn as_any_mut(&mut self) -> &mut dyn Any;
     fn release(&mut self, cx: &mut MutableAppContext);
     fn ui_name(&self) -> &'static str;
-    fn render<'a>(&self, window_id: usize, view_id: usize, cx: &AppContext) -> ElementBox;
+    fn render<'a>(
+        &self,
+        window_id: usize,
+        view_id: usize,
+        titlebar_height: f32,
+        cx: &AppContext,
+    ) -> ElementBox;
     fn on_focus(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
     fn on_blur(&mut self, cx: &mut MutableAppContext, window_id: usize, view_id: usize);
     fn keymap_context(&self, cx: &AppContext) -> keymap::Context;
@@ -1729,7 +1749,13 @@ where
         T::ui_name()
     }
 
-    fn render<'a>(&self, window_id: usize, view_id: usize, cx: &AppContext) -> ElementBox {
+    fn render<'a>(
+        &self,
+        window_id: usize,
+        view_id: usize,
+        titlebar_height: f32,
+        cx: &AppContext,
+    ) -> ElementBox {
         View::render(
             self,
             &RenderContext {
@@ -1737,6 +1763,7 @@ where
                 view_id,
                 app: cx,
                 view_type: PhantomData::<T>,
+                titlebar_height,
             },
         )
     }
@@ -2170,6 +2197,7 @@ impl<'a, T: View> ViewContext<'a, T> {
 
 pub struct RenderContext<'a, T: View> {
     pub app: &'a AppContext,
+    pub titlebar_height: f32,
     window_id: usize,
     view_id: usize,
     view_type: PhantomData<T>,

gpui/src/presenter.rs 🔗

@@ -28,6 +28,7 @@ pub struct Presenter {
 impl Presenter {
     pub fn new(
         window_id: usize,
+        titlebar_height: f32,
         font_cache: Arc<FontCache>,
         text_layout_cache: TextLayoutCache,
         asset_cache: Arc<AssetCache>,
@@ -35,7 +36,7 @@ impl Presenter {
     ) -> Self {
         Self {
             window_id,
-            rendered_views: cx.render_views(window_id),
+            rendered_views: cx.render_views(window_id, titlebar_height),
             parents: HashMap::new(),
             font_cache,
             text_layout_cache,
@@ -55,15 +56,23 @@ impl Presenter {
         path
     }
 
-    pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, cx: &AppContext) {
+    pub fn invalidate(
+        &mut self,
+        mut invalidation: WindowInvalidation,
+        titlebar_height: f32,
+        cx: &AppContext,
+    ) {
         for view_id in invalidation.removed {
             invalidation.updated.remove(&view_id);
             self.rendered_views.remove(&view_id);
             self.parents.remove(&view_id);
         }
         for view_id in invalidation.updated {
-            self.rendered_views
-                .insert(view_id, cx.render_view(self.window_id, view_id).unwrap());
+            self.rendered_views.insert(
+                view_id,
+                cx.render_view(self.window_id, view_id, titlebar_height)
+                    .unwrap(),
+            );
         }
     }