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

This commit is contained in:
Nathan Sobo
2021-04-07 21:54:14 -06:00
parent 0664321125
commit cbb23a93a6
3 changed files with 69 additions and 15 deletions
+37 -10
View File
@@ -310,6 +310,7 @@ pub struct MutableAppContext {
window_invalidations: HashMap<usize, WindowInvalidation>,
invalidation_callbacks:
HashMap<usize, Box<dyn FnMut(WindowInvalidation, &mut MutableAppContext)>>,
debug_elements_callbacks: HashMap<usize, Box<dyn Fn(&AppContext) -> crate::json::Value>>,
foreground: Rc<executor::Foreground>,
future_handlers: Rc<RefCell<HashMap<usize, FutureHandler>>>,
stream_handlers: Rc<RefCell<HashMap<usize, StreamHandler>>>,
@@ -347,6 +348,7 @@ impl MutableAppContext {
observations: HashMap::new(),
window_invalidations: HashMap::new(),
invalidation_callbacks: HashMap::new(),
debug_elements_callbacks: HashMap::new(),
foreground,
future_handlers: Default::default(),
stream_handlers: Default::default(),
@@ -373,16 +375,29 @@ impl MutableAppContext {
&self.ctx.background
}
pub fn on_window_invalidated<F: 'static + FnMut(WindowInvalidation, &mut MutableAppContext)>(
&mut self,
window_id: usize,
callback: F,
) {
pub fn on_window_invalidated<F>(&mut self, window_id: usize, callback: F)
where
F: 'static + FnMut(WindowInvalidation, &mut MutableAppContext),
{
self.invalidation_callbacks
.insert(window_id, Box::new(callback));
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)
where
S: Into<String>,
@@ -692,11 +707,19 @@ impl MutableAppContext {
}));
}
self.on_window_invalidated(window_id, move |invalidation, ctx| {
let mut presenter = presenter.borrow_mut();
presenter.invalidate(invalidation, ctx.downgrade());
let scene = presenter.build_scene(window.size(), window.scale_factor(), ctx);
window.present_scene(scene);
{
let presenter = presenter.clone();
self.on_window_invalidated(window_id, move |invalidation, ctx| {
let mut presenter = presenter.borrow_mut();
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
}
pub fn debug_elements(&self) -> crate::json::Value {
self.app.debug_elements(self.window_id).unwrap()
}
pub fn focus<S>(&mut self, handle: S)
where
S: Into<AnyViewHandle>,
+14 -2
View File
@@ -2,7 +2,7 @@ use crate::{
app::{AppContext, MutableAppContext, WindowInvalidation},
elements::Element,
font_cache::FontCache,
json::ToJson,
json::{self, ToJson},
platform::Event,
text_layout::TextLayoutCache,
AssetCache, ElementBox, Scene,
@@ -130,6 +130,18 @@ impl Presenter {
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 {
@@ -227,7 +239,7 @@ impl<'a> EventContext<'a> {
}
pub struct DebugContext<'a> {
rendered_views: &'a mut HashMap<usize, ElementBox>,
rendered_views: &'a HashMap<usize, ElementBox>,
pub font_cache: &'a FontCache,
pub app: &'a AppContext,
}