Include constraints in element tree JSON debug output

Co-Authored-By: Nathan Sobo <nathan@zed.dev>
This commit is contained in:
Max Brunsfeld 2021-04-14 15:09:38 -07:00
parent f4538e9eb5
commit f5752969ab
2 changed files with 25 additions and 2 deletions

View file

@ -91,6 +91,6 @@ impl Element for ConstrainedBox {
_: &Self::PaintState, _: &Self::PaintState,
ctx: &DebugContext, ctx: &DebugContext,
) -> json::Value { ) -> json::Value {
json!({"type": "ConstrainedBox", "constraint": self.constraint.to_json(), "child": self.child.debug(ctx)}) json!({"type": "ConstrainedBox", "set_constraint": self.constraint.to_json(), "child": self.child.debug(ctx)})
} }
} }

View file

@ -4,6 +4,7 @@ use crate::{
SizeConstraint, SizeConstraint,
}; };
use core::panic; use core::panic;
use json::ToJson;
use replace_with::replace_with_or_abort; use replace_with::replace_with_or_abort;
use std::{any::Any, borrow::Cow}; use std::{any::Any, borrow::Cow};
@ -90,11 +91,13 @@ pub enum Lifecycle<T: Element> {
}, },
PostLayout { PostLayout {
element: T, element: T,
constraint: SizeConstraint,
size: Vector2F, size: Vector2F,
layout: T::LayoutState, layout: T::LayoutState,
}, },
PostPaint { PostPaint {
element: T, element: T,
constraint: SizeConstraint,
bounds: RectF, bounds: RectF,
layout: T::LayoutState, layout: T::LayoutState,
paint: T::PaintState, paint: T::PaintState,
@ -119,6 +122,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
result = Some(size); result = Some(size);
Lifecycle::PostLayout { Lifecycle::PostLayout {
element, element,
constraint,
size, size,
layout, layout,
} }
@ -132,6 +136,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
element, element,
size, size,
layout, layout,
..
} = self } = self
{ {
element.after_layout(*size, layout, ctx); element.after_layout(*size, layout, ctx);
@ -144,6 +149,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
replace_with_or_abort(self, |me| { replace_with_or_abort(self, |me| {
if let Lifecycle::PostLayout { if let Lifecycle::PostLayout {
mut element, mut element,
constraint,
size, size,
mut layout, mut layout,
} = me } = me
@ -152,6 +158,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
let paint = element.paint(bounds, &mut layout, ctx); let paint = element.paint(bounds, &mut layout, ctx);
Lifecycle::PostPaint { Lifecycle::PostPaint {
element, element,
constraint,
bounds, bounds,
layout, layout,
paint, paint,
@ -168,6 +175,7 @@ impl<T: Element> AnyElement for Lifecycle<T> {
bounds, bounds,
layout, layout,
paint, paint,
..
} = self } = self
{ {
element.dispatch_event(event, *bounds, layout, paint, ctx) element.dispatch_event(event, *bounds, layout, paint, ctx)
@ -196,10 +204,25 @@ impl<T: Element> AnyElement for Lifecycle<T> {
match self { match self {
Lifecycle::PostPaint { Lifecycle::PostPaint {
element, element,
constraint,
bounds, bounds,
layout, layout,
paint, paint,
} => element.debug(*bounds, layout, paint, ctx), } => {
let mut value = element.debug(*bounds, layout, paint, ctx);
if let json::Value::Object(map) = &mut value {
let mut new_map: crate::json::Map<String, serde_json::Value> =
Default::default();
if let Some(typ) = map.remove("type") {
new_map.insert("type".into(), typ);
}
new_map.insert("constraint".into(), constraint.to_json());
new_map.append(map);
json::Value::Object(new_map)
} else {
value
}
}
_ => panic!("invalid element lifecycle state"), _ => panic!("invalid element lifecycle state"),
} }
} }