Order debug JSON and allow elements to be named; copy to clipboard

This commit is contained in:
Nathan Sobo
2021-04-07 21:54:14 -06:00
parent cbb23a93a6
commit 9f6f27f305
15 changed files with 104 additions and 32 deletions
+4
View File
@@ -1120,6 +1120,10 @@ impl MutableAppContext {
}
}
}
pub fn copy(&self, text: &str) {
self.platform.copy(text);
}
}
impl ModelAsRef for MutableAppContext {
+1 -1
View File
@@ -91,8 +91,8 @@ impl Element for Align {
) -> json::Value {
json!({
"type": "Align",
"alignment": self.alignment.to_json(),
"bounds": bounds.to_json(),
"alignment": self.alignment.to_json(),
"child": self.child.debug(ctx),
})
}
+1 -1
View File
@@ -201,6 +201,7 @@ impl Element for Container {
) -> serde_json::Value {
json!({
"type": "Container",
"bounds": bounds.to_json(),
"details": {
"margin": self.margin.to_json(),
"padding": self.padding.to_json(),
@@ -209,7 +210,6 @@ impl Element for Container {
"corner_radius": self.corner_radius,
"shadow": self.shadow.to_json(),
},
"bounds": bounds.to_json(),
"child": self.child.debug(ctx),
})
}
+1 -1
View File
@@ -173,8 +173,8 @@ impl Element for Flex {
) -> json::Value {
json!({
"type": "Flex",
"axis": self.axis.to_json(),
"bounds": bounds.to_json(),
"axis": self.axis.to_json(),
"children": self.children.iter().map(|child| child.debug(ctx)).collect::<Vec<json::Value>>()
})
}
+3 -3
View File
@@ -166,11 +166,11 @@ impl Element for Label {
) -> Value {
json!({
"type": "Label",
"font_size": self.font_size,
"bounds": bounds.to_json(),
"text": &self.text,
"family_id": ctx.font_cache.family_name(self.family_id).unwrap(),
"font_family": ctx.font_cache.family_name(self.family_id).unwrap(),
"font_size": self.font_size,
"font_properties": self.font_properties.to_json(),
"text": &self.text,
"highlights": self.highlights.to_json(),
})
}
+1 -1
View File
@@ -99,7 +99,7 @@ impl Element for LineBox {
) -> serde_json::Value {
json!({
"bounds": bounds.to_json(),
"family_id": ctx.font_cache.family_name(self.family_id).unwrap(),
"font_family": ctx.font_cache.family_name(self.family_id).unwrap(),
"font_size": self.font_size,
"font_properties": self.font_properties.to_json(),
"child": self.child.debug(ctx),
+37 -10
View File
@@ -5,7 +5,7 @@ use crate::{
};
use core::panic;
use replace_with::replace_with_or_abort;
use std::any::Any;
use std::{any::Any, borrow::Cow};
trait AnyElement {
fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F;
@@ -67,7 +67,20 @@ pub trait Element {
where
Self: 'static + Sized,
{
ElementBox(Box::new(Lifecycle::Init { element: self }))
ElementBox {
name: None,
element: Box::new(Lifecycle::Init { element: self }),
}
}
fn named(self, name: impl Into<Cow<'static, str>>) -> ElementBox
where
Self: 'static + Sized,
{
ElementBox {
name: Some(name.into()),
element: Box::new(Lifecycle::Init { element: self }),
}
}
}
@@ -87,7 +100,10 @@ pub enum Lifecycle<T: Element> {
paint: T::PaintState,
},
}
pub struct ElementBox(Box<dyn AnyElement>);
pub struct ElementBox {
name: Option<Cow<'static, str>>,
element: Box<dyn AnyElement>,
}
impl<T: Element> AnyElement for Lifecycle<T> {
fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F {
@@ -191,30 +207,41 @@ impl<T: Element> AnyElement for Lifecycle<T> {
impl ElementBox {
pub fn layout(&mut self, constraint: SizeConstraint, ctx: &mut LayoutContext) -> Vector2F {
self.0.layout(constraint, ctx)
self.element.layout(constraint, ctx)
}
pub fn after_layout(&mut self, ctx: &mut AfterLayoutContext) {
self.0.after_layout(ctx);
self.element.after_layout(ctx);
}
pub fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext) {
self.0.paint(origin, ctx);
self.element.paint(origin, ctx);
}
pub fn dispatch_event(&mut self, event: &Event, ctx: &mut EventContext) -> bool {
self.0.dispatch_event(event, ctx)
self.element.dispatch_event(event, ctx)
}
pub fn size(&self) -> Vector2F {
self.0.size()
self.element.size()
}
pub fn metadata(&self) -> Option<&dyn Any> {
self.0.metadata()
self.element.metadata()
}
pub fn debug(&self, ctx: &DebugContext) -> json::Value {
self.0.debug(ctx)
let mut value = self.element.debug(ctx);
if let Some(name) = &self.name {
if let json::Value::Object(map) = &mut value {
let mut new_map: crate::json::Map<String, serde_json::Value> = Default::default();
new_map.insert("name".into(), json::Value::String(name.to_string()));
new_map.append(map);
return json::Value::Object(new_map);
}
}
value
}
}
+19 -2
View File
@@ -1,9 +1,13 @@
use super::{BoolExt as _, Dispatcher, FontSystem, Window};
use crate::{executor, platform};
use anyhow::Result;
use cocoa::base::id;
use cocoa::{
appkit::{NSPasteboard, NSPasteboardTypeString},
base::{id, nil},
foundation::NSData,
};
use objc::{class, msg_send, sel, sel_impl};
use std::{rc::Rc, sync::Arc};
use std::{ffi::c_void, rc::Rc, sync::Arc};
pub struct App {
dispatcher: Arc<Dispatcher>,
@@ -42,4 +46,17 @@ impl platform::App for App {
fn fonts(&self) -> Arc<dyn platform::FontSystem> {
self.fonts.clone()
}
fn copy(&self, text: &str) {
unsafe {
let data = NSData::dataWithBytes_length_(
nil,
text.as_ptr() as *const c_void,
text.len() as u64,
);
let pasteboard = NSPasteboard::generalPasteboard(nil);
pasteboard.clearContents();
pasteboard.setData_forType(data, NSPasteboardTypeString);
}
}
}
+1
View File
@@ -40,6 +40,7 @@ pub trait App {
executor: Rc<executor::Foreground>,
) -> Result<Box<dyn Window>>;
fn fonts(&self) -> Arc<dyn FontSystem>;
fn copy(&self, text: &str);
}
pub trait Dispatcher: Send + Sync {
+2
View File
@@ -46,6 +46,8 @@ impl super::App for App {
fn fonts(&self) -> std::sync::Arc<dyn super::FontSystem> {
self.fonts.clone()
}
fn copy(&self, _: &str) {}
}
impl Window {