From 2186de38ab0ec47478430996a10f2e7126ec65c4 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 7 Apr 2023 11:54:08 -0600 Subject: [PATCH] Merge AppContext impl blocks --- crates/gpui/src/app.rs | 188 ++++++++++++++++++++--------------------- 1 file changed, 93 insertions(+), 95 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index bc38b28bba374f4bfdc1618c3b3b7bf49e3205e2..d0e512c0813c9da7ee801ade7533b236d766de0c 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -601,6 +601,30 @@ impl AppContext { } } + pub fn background(&self) -> &Arc { + &self.background + } + + pub fn font_cache(&self) -> &Arc { + &self.font_cache + } + + pub fn platform(&self) -> &Arc { + &self.platform + } + + pub fn has_global(&self) -> bool { + self.globals.contains_key(&TypeId::of::()) + } + + pub fn global(&self) -> &T { + if let Some(global) = self.globals.get(&TypeId::of::()) { + global.downcast_ref().unwrap() + } else { + panic!("no global has been added for {}", type_name::()); + } + } + pub fn upgrade(&self) -> App { App(self.weak_self.as_ref().unwrap().upgrade().unwrap()) } @@ -645,12 +669,6 @@ impl AppContext { &self.foreground } - pub fn debug_elements(&self, window_id: usize) -> Option { - self.presenters_and_platform_windows - .get(&window_id) - .and_then(|(presenter, _)| presenter.borrow().debug_elements(self)) - } - pub fn deserialize_action( &self, name: &str, @@ -810,6 +828,75 @@ impl AppContext { window.screen().display_uuid() } + pub fn root_view(&self, window_id: usize) -> Option { + self.windows + .get(&window_id) + .map(|window| window.root_view.clone()) + } + + pub fn root_view_id(&self, window_id: usize) -> Option { + self.windows + .get(&window_id) + .map(|window| window.root_view.id()) + } + + pub fn focused_view_id(&self, window_id: usize) -> Option { + self.windows + .get(&window_id) + .and_then(|window| window.focused_view_id) + } + + pub fn view_ui_name(&self, window_id: usize, view_id: usize) -> Option<&'static str> { + Some(self.views.get(&(window_id, view_id))?.ui_name()) + } + + pub fn view_type_id(&self, window_id: usize, view_id: usize) -> Option { + self.views + .get(&(window_id, view_id)) + .map(|view| view.as_any().type_id()) + } + + /// Returns an iterator over all of the view ids from the passed view up to the root of the window + /// Includes the passed view itself + fn ancestors(&self, window_id: usize, mut view_id: usize) -> impl Iterator + '_ { + std::iter::once(view_id) + .into_iter() + .chain(std::iter::from_fn(move || { + if let Some(ParentId::View(parent_id)) = self.parents.get(&(window_id, view_id)) { + view_id = *parent_id; + Some(view_id) + } else { + None + } + })) + } + + /// Returns the id of the parent of the given view, or none if the given + /// view is the root. + pub fn parent(&self, window_id: usize, view_id: usize) -> Option { + if let Some(ParentId::View(view_id)) = self.parents.get(&(window_id, view_id)) { + Some(*view_id) + } else { + None + } + } + + pub fn is_child_focused(&self, view: &AnyViewHandle) -> bool { + if let Some(focused_view_id) = self.focused_view_id(view.window_id) { + self.ancestors(view.window_id, focused_view_id) + .skip(1) // Skip self id + .any(|parent| parent == view.view_id) + } else { + false + } + } + + pub fn debug_elements(&self, window_id: usize) -> Option { + self.presenters_and_platform_windows + .get(&window_id) + .and_then(|(presenter, _)| presenter.borrow().debug_elements(self)) + } + pub fn active_labeled_tasks<'a>( &'a self, ) -> impl DoubleEndedIterator + 'a { @@ -2713,95 +2800,6 @@ pub enum ParentId { Root, } -impl AppContext { - pub fn root_view(&self, window_id: usize) -> Option { - self.windows - .get(&window_id) - .map(|window| window.root_view.clone()) - } - - pub fn root_view_id(&self, window_id: usize) -> Option { - self.windows - .get(&window_id) - .map(|window| window.root_view.id()) - } - - pub fn focused_view_id(&self, window_id: usize) -> Option { - self.windows - .get(&window_id) - .and_then(|window| window.focused_view_id) - } - - pub fn view_ui_name(&self, window_id: usize, view_id: usize) -> Option<&'static str> { - Some(self.views.get(&(window_id, view_id))?.ui_name()) - } - - pub fn view_type_id(&self, window_id: usize, view_id: usize) -> Option { - self.views - .get(&(window_id, view_id)) - .map(|view| view.as_any().type_id()) - } - - pub fn background(&self) -> &Arc { - &self.background - } - - pub fn font_cache(&self) -> &Arc { - &self.font_cache - } - - pub fn platform(&self) -> &Arc { - &self.platform - } - - pub fn has_global(&self) -> bool { - self.globals.contains_key(&TypeId::of::()) - } - - pub fn global(&self) -> &T { - if let Some(global) = self.globals.get(&TypeId::of::()) { - global.downcast_ref().unwrap() - } else { - panic!("no global has been added for {}", type_name::()); - } - } - - /// Returns an iterator over all of the view ids from the passed view up to the root of the window - /// Includes the passed view itself - fn ancestors(&self, window_id: usize, mut view_id: usize) -> impl Iterator + '_ { - std::iter::once(view_id) - .into_iter() - .chain(std::iter::from_fn(move || { - if let Some(ParentId::View(parent_id)) = self.parents.get(&(window_id, view_id)) { - view_id = *parent_id; - Some(view_id) - } else { - None - } - })) - } - - /// Returns the id of the parent of the given view, or none if the given - /// view is the root. - pub fn parent(&self, window_id: usize, view_id: usize) -> Option { - if let Some(ParentId::View(view_id)) = self.parents.get(&(window_id, view_id)) { - Some(*view_id) - } else { - None - } - } - - pub fn is_child_focused(&self, view: &AnyViewHandle) -> bool { - if let Some(focused_view_id) = self.focused_view_id(view.window_id) { - self.ancestors(view.window_id, focused_view_id) - .skip(1) // Skip self id - .any(|parent| parent == view.view_id) - } else { - false - } - } -} - pub struct Window { root_view: AnyViewHandle, focused_view_id: Option,