Log prettified element debug JSON to on cmd-alt-i

This commit is contained in:
Nathan Sobo 2021-04-07 20:51:14 -06:00
parent 0664321125
commit cbb23a93a6
3 changed files with 69 additions and 15 deletions

View file

@ -310,6 +310,7 @@ pub struct MutableAppContext {
window_invalidations: HashMap<usize, WindowInvalidation>, window_invalidations: HashMap<usize, WindowInvalidation>,
invalidation_callbacks: invalidation_callbacks:
HashMap<usize, Box<dyn FnMut(WindowInvalidation, &mut MutableAppContext)>>, HashMap<usize, Box<dyn FnMut(WindowInvalidation, &mut MutableAppContext)>>,
debug_elements_callbacks: HashMap<usize, Box<dyn Fn(&AppContext) -> crate::json::Value>>,
foreground: Rc<executor::Foreground>, foreground: Rc<executor::Foreground>,
future_handlers: Rc<RefCell<HashMap<usize, FutureHandler>>>, future_handlers: Rc<RefCell<HashMap<usize, FutureHandler>>>,
stream_handlers: Rc<RefCell<HashMap<usize, StreamHandler>>>, stream_handlers: Rc<RefCell<HashMap<usize, StreamHandler>>>,
@ -347,6 +348,7 @@ impl MutableAppContext {
observations: HashMap::new(), observations: HashMap::new(),
window_invalidations: HashMap::new(), window_invalidations: HashMap::new(),
invalidation_callbacks: HashMap::new(), invalidation_callbacks: HashMap::new(),
debug_elements_callbacks: HashMap::new(),
foreground, foreground,
future_handlers: Default::default(), future_handlers: Default::default(),
stream_handlers: Default::default(), stream_handlers: Default::default(),
@ -373,16 +375,29 @@ impl MutableAppContext {
&self.ctx.background &self.ctx.background
} }
pub fn on_window_invalidated<F: 'static + FnMut(WindowInvalidation, &mut MutableAppContext)>( pub fn on_window_invalidated<F>(&mut self, window_id: usize, callback: F)
&mut self, where
window_id: usize, F: 'static + FnMut(WindowInvalidation, &mut MutableAppContext),
callback: F, {
) {
self.invalidation_callbacks self.invalidation_callbacks
.insert(window_id, Box::new(callback)); .insert(window_id, Box::new(callback));
self.update_windows(); self.update_windows();
} }
pub fn on_debug_elements<F>(&mut self, window_id: usize, callback: F)
where
F: 'static + Fn(&AppContext) -> crate::json::Value,
{
self.debug_elements_callbacks
.insert(window_id, Box::new(callback));
}
pub fn debug_elements(&self, window_id: usize) -> Option<crate::json::Value> {
self.debug_elements_callbacks
.get(&window_id)
.map(|debug_elements| debug_elements(&self.ctx))
}
pub fn add_action<S, V, T, F>(&mut self, name: S, mut handler: F) pub fn add_action<S, V, T, F>(&mut self, name: S, mut handler: F)
where where
S: Into<String>, S: Into<String>,
@ -692,11 +707,19 @@ impl MutableAppContext {
})); }));
} }
self.on_window_invalidated(window_id, move |invalidation, ctx| { {
let mut presenter = presenter.borrow_mut(); let presenter = presenter.clone();
presenter.invalidate(invalidation, ctx.downgrade()); self.on_window_invalidated(window_id, move |invalidation, ctx| {
let scene = presenter.build_scene(window.size(), window.scale_factor(), ctx); let mut presenter = presenter.borrow_mut();
window.present_scene(scene); presenter.invalidate(invalidation, ctx.downgrade());
let scene =
presenter.build_scene(window.size(), window.scale_factor(), ctx);
window.present_scene(scene);
});
}
self.on_debug_elements(window_id, move |ctx| {
presenter.borrow().debug_elements(ctx).unwrap()
}); });
} }
} }
@ -1573,6 +1596,10 @@ impl<'a, T: View> ViewContext<'a, T> {
&self.app.ctx.background &self.app.ctx.background
} }
pub fn debug_elements(&self) -> crate::json::Value {
self.app.debug_elements(self.window_id).unwrap()
}
pub fn focus<S>(&mut self, handle: S) pub fn focus<S>(&mut self, handle: S)
where where
S: Into<AnyViewHandle>, S: Into<AnyViewHandle>,

View file

@ -2,7 +2,7 @@ use crate::{
app::{AppContext, MutableAppContext, WindowInvalidation}, app::{AppContext, MutableAppContext, WindowInvalidation},
elements::Element, elements::Element,
font_cache::FontCache, font_cache::FontCache,
json::ToJson, json::{self, ToJson},
platform::Event, platform::Event,
text_layout::TextLayoutCache, text_layout::TextLayoutCache,
AssetCache, ElementBox, Scene, AssetCache, ElementBox, Scene,
@ -130,6 +130,18 @@ impl Presenter {
Vec::new() Vec::new()
} }
} }
pub fn debug_elements(&self, ctx: &AppContext) -> Option<json::Value> {
ctx.root_view_id(self.window_id)
.and_then(|root_view_id| self.rendered_views.get(&root_view_id))
.map(|root_element| {
root_element.debug(&DebugContext {
rendered_views: &self.rendered_views,
font_cache: &self.font_cache,
app: ctx,
})
})
}
} }
pub struct ActionToDispatch { pub struct ActionToDispatch {
@ -227,7 +239,7 @@ impl<'a> EventContext<'a> {
} }
pub struct DebugContext<'a> { pub struct DebugContext<'a> {
rendered_views: &'a mut HashMap<usize, ElementBox>, rendered_views: &'a HashMap<usize, ElementBox>,
pub font_cache: &'a FontCache, pub font_cache: &'a FontCache,
pub app: &'a AppContext, pub app: &'a AppContext,
} }

View file

@ -2,15 +2,19 @@ use super::{pane, Pane, PaneGroup, SplitDirection, Workspace};
use crate::{settings::Settings, watch}; use crate::{settings::Settings, watch};
use futures_core::future::LocalBoxFuture; use futures_core::future::LocalBoxFuture;
use gpui::{ use gpui::{
color::rgbu, elements::*, keymap::Binding, AnyViewHandle, App, AppContext, Entity, ModelHandle, color::rgbu, elements::*, json::to_string_pretty, keymap::Binding, AnyViewHandle, App,
MutableAppContext, View, ViewContext, ViewHandle, AppContext, Entity, ModelHandle, MutableAppContext, View, ViewContext, ViewHandle,
}; };
use log::{error, info}; use log::{error, info};
use std::{collections::HashSet, path::PathBuf}; use std::{collections::HashSet, path::PathBuf};
pub fn init(app: &mut App) { pub fn init(app: &mut App) {
app.add_action("workspace:save", WorkspaceView::save_active_item); app.add_action("workspace:save", WorkspaceView::save_active_item);
app.add_bindings(vec![Binding::new("cmd-s", "workspace:save", None)]); app.add_action("workspace:debug_elements", WorkspaceView::debug_elements);
app.add_bindings(vec![
Binding::new("cmd-s", "workspace:save", None),
Binding::new("cmd-alt-i", "workspace:debug_elements", None),
]);
} }
pub trait ItemView: View { pub trait ItemView: View {
@ -251,6 +255,17 @@ impl WorkspaceView {
}); });
} }
pub fn debug_elements(&mut self, _: &(), ctx: &mut ViewContext<Self>) {
match to_string_pretty(&ctx.debug_elements()) {
Ok(json) => {
log::info!("{}", json);
}
Err(error) => {
log::error!("error debugging elements: {}", error);
}
};
}
fn workspace_updated(&mut self, _: ModelHandle<Workspace>, ctx: &mut ViewContext<Self>) { fn workspace_updated(&mut self, _: ModelHandle<Workspace>, ctx: &mut ViewContext<Self>) {
ctx.notify(); ctx.notify();
} }