From d5b7e2d4e35d22a33c9899f4f1b2ec7159001ec7 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 27 Aug 2021 17:25:13 +0200 Subject: [PATCH] Pass a MutableAppContext in `render` Co-Authored-By: Nathan Sobo --- gpui/src/app.rs | 120 ++++++++++++++++++++++-------------------- gpui/src/presenter.rs | 10 ++-- 2 files changed, 69 insertions(+), 61 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index a37e6e415723e83f1872c1755b74e8a64638650d..0aa85a33b05260e8e0daa0cb35b6ae721c15f305 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -806,12 +806,49 @@ impl MutableAppContext { self.cx.focused_view_id(window_id) } + pub fn render_view( + &mut self, + window_id: usize, + view_id: usize, + titlebar_height: f32, + refreshing: bool, + ) -> Result { + let view = self + .cx + .views + .remove(&(window_id, view_id)) + .ok_or(anyhow!("view not found"))?; + let element = view.render(window_id, view_id, titlebar_height, refreshing, self); + self.cx.views.insert((window_id, view_id), view); + Ok(element) + } + pub fn render_views( - &self, + &mut self, window_id: usize, titlebar_height: f32, ) -> HashMap { - self.cx.render_views(window_id, titlebar_height) + let view_ids = self + .views + .keys() + .filter_map(|(win_id, view_id)| { + if *win_id == window_id { + Some(*view_id) + } else { + None + } + }) + .collect::>(); + view_ids + .into_iter() + .map(|view_id| { + ( + view_id, + self.render_view(window_id, view_id, titlebar_height, false) + .unwrap(), + ) + }) + .collect() } pub fn update T>(&mut self, callback: F) -> T { @@ -1204,7 +1241,7 @@ impl MutableAppContext { }); } - pub fn build_presenter(&self, window_id: usize, titlebar_height: f32) -> Presenter { + pub fn build_presenter(&mut self, window_id: usize, titlebar_height: f32) -> Presenter { Presenter::new( window_id, titlebar_height, @@ -1215,6 +1252,23 @@ impl MutableAppContext { ) } + pub fn render_cx( + &mut self, + window_id: usize, + view_id: usize, + titlebar_height: f32, + refreshing: bool, + ) -> RenderContext { + RenderContext { + app: self, + titlebar_height, + refreshing, + window_id, + view_id, + view_type: PhantomData, + } + } + pub fn add_view(&mut self, window_id: usize, build_view: F) -> ViewHandle where T: View, @@ -1357,7 +1411,7 @@ impl MutableAppContext { { { let mut presenter = presenter.borrow_mut(); - presenter.invalidate(invalidation, self.as_ref()); + presenter.invalidate(invalidation, self); let scene = presenter.build_scene(window.size(), window.scale_factor(), self); window.present_scene(scene); } @@ -1382,7 +1436,7 @@ impl MutableAppContext { .invalidation .take(); let mut presenter = presenter.borrow_mut(); - presenter.refresh(invalidation, self.as_ref()); + presenter.refresh(invalidation, self); let scene = presenter.build_scene(window.size(), window.scale_factor(), self); window.present_scene(scene); } @@ -1633,56 +1687,6 @@ impl AppContext { .map(|window| window.focused_view_id) } - pub fn render_view( - &self, - window_id: usize, - view_id: usize, - titlebar_height: f32, - refreshing: bool, - ) -> Result { - self.views - .get(&(window_id, view_id)) - .map(|v| v.render(window_id, view_id, titlebar_height, refreshing, self)) - .ok_or(anyhow!("view not found")) - } - - pub fn render_views( - &self, - window_id: usize, - titlebar_height: f32, - ) -> HashMap { - self.views - .iter() - .filter_map(|((win_id, view_id), view)| { - if *win_id == window_id { - Some(( - *view_id, - view.render(*win_id, *view_id, titlebar_height, false, self), - )) - } else { - None - } - }) - .collect::>() - } - - pub fn render_cx( - &self, - window_id: usize, - view_id: usize, - titlebar_height: f32, - refreshing: bool, - ) -> RenderContext { - RenderContext { - app: self, - titlebar_height, - refreshing, - window_id, - view_id, - view_type: PhantomData, - } - } - pub fn background(&self) -> &Arc { &self.background } @@ -1834,7 +1838,7 @@ pub trait AnyView { view_id: usize, titlebar_height: f32, refreshing: bool, - cx: &AppContext, + cx: &mut MutableAppContext, ) -> 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); @@ -1867,7 +1871,7 @@ where view_id: usize, titlebar_height: f32, refreshing: bool, - cx: &AppContext, + cx: &mut MutableAppContext, ) -> ElementBox { View::render( self, @@ -2244,7 +2248,7 @@ impl<'a, T: View> ViewContext<'a, T> { } pub struct RenderContext<'a, T: View> { - pub app: &'a AppContext, + pub app: &'a mut MutableAppContext, pub titlebar_height: f32, pub refreshing: bool, window_id: usize, diff --git a/gpui/src/presenter.rs b/gpui/src/presenter.rs index d03e6462790b72ec2a55422e8cb44c087aa48767..06d16269199762ebd5b37324234f1a03c90d335a 100644 --- a/gpui/src/presenter.rs +++ b/gpui/src/presenter.rs @@ -32,7 +32,7 @@ impl Presenter { font_cache: Arc, text_layout_cache: TextLayoutCache, asset_cache: Arc, - cx: &MutableAppContext, + cx: &mut MutableAppContext, ) -> Self { Self { window_id, @@ -57,7 +57,7 @@ impl Presenter { path } - pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, cx: &AppContext) { + pub fn invalidate(&mut self, mut invalidation: WindowInvalidation, cx: &mut MutableAppContext) { for view_id in invalidation.removed { invalidation.updated.remove(&view_id); self.rendered_views.remove(&view_id); @@ -72,7 +72,11 @@ impl Presenter { } } - pub fn refresh(&mut self, invalidation: Option, cx: &AppContext) { + pub fn refresh( + &mut self, + invalidation: Option, + cx: &mut MutableAppContext, + ) { if let Some(invalidation) = invalidation { for view_id in invalidation.removed { self.rendered_views.remove(&view_id);