From 473ac5a8425af4611346b609540dd87aa9f3e65d Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Sun, 13 Aug 2023 02:27:36 -0600 Subject: [PATCH] WIP --- Cargo.lock | 1 - crates/gpui/playground/src/frame.rs | 1183 +---------------- crates/gpui/playground_macros/Cargo.toml | 1 - .../src/playground_macros.rs | 80 +- 4 files changed, 127 insertions(+), 1138 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cb3cce0b67c6befdd0c93fdf414a03930961e00a..37af2d52f327d4e4029b76e3eea901ad0980c817 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5315,7 +5315,6 @@ dependencies = [ name = "playground_macros" version = "0.1.0" dependencies = [ - "once_cell", "proc-macro2", "quote", "syn 1.0.109", diff --git a/crates/gpui/playground/src/frame.rs b/crates/gpui/playground/src/frame.rs index 285ff2fe24bc228be28298635dea2b2bfa1ffd70..023cac0bdfe81209405ac62a45dbfc19b8301b9a 100644 --- a/crates/gpui/playground/src/frame.rs +++ b/crates/gpui/playground/src/frame.rs @@ -1,43 +1,22 @@ use playground_macros::tailwind_lengths; use taffy::style::{Position, *}; -#[derive(Clone, Debug)] +#[derive(Clone, Debug, Default)] struct Style { display: Display, position: Position, overflow: Point, inset: Edges, + size: Size, + max_size: Size, + min_size: Size, } -#[derive(Clone, Copy, Debug)] -pub enum Length { - Rems(f32), - Pixels(f32), - Percent(f32), - Auto, -} - -impl Default for Length { - fn default() -> Self { - Self::Rems(0.) +impl Style { + pub fn new() -> Self { + Self::default() } -} -#[derive(Clone, Default, Debug)] -pub struct Edges { - top: T, - bottom: T, - left: T, - right: T, -} - -#[derive(Clone, Copy, Debug)] -pub struct Point { - x: T, - y: T, -} - -impl Style { // Display //////////////////// fn block(mut self) -> Self { @@ -126,1111 +105,61 @@ impl Style { self.inset.left = length; self } -} - -// mod traits { -// pub trait Element { -// type Layout; - -// /// Add the element and its children to the layout tree. -// fn layout(&mut self, &view) - -// // fn layout(&self) -> -// } -// } - -// #![allow(unused_variables, dead_code)] - -// use self::length::rems; -// use crate::color::Hsla; -// use derive_more::{Add, Deref, DerefMut}; -// use gpui::{ -// color::Color, -// fonts::{HighlightStyle, Underline}, -// geometry::{ -// rect::RectF, -// vector::{vec2f, Vector2F}, -// }, -// text_layout::{Line, ShapedBoundary}, -// AppContext, Entity, LayoutContext, SceneBuilder, View, ViewContext, WindowContext, -// }; -// use length::{Length, Rems}; -// use optional_struct::*; -// use std::{any::Any, borrow::Cow, f32, ops::Range, sync::Arc}; - -// mod traits { -// use super::*; - -// pub trait Element { -// type Paint; - -// fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> (NodeId, Self::Paint); -// fn paint( -// &mut self, -// view: &mut V, -// layout: &Layout, -// paint: &mut Self::Paint, -// cx: &mut PaintContext, -// ); - -// /// Convert this element into a dynamically-typed element. -// fn into_any(self) -> super::AnyElement -// where -// Self: Sized, -// { -// super::AnyElement { -// element: Box::new(self), -// paint: None, -// } -// } -// } - -// pub trait AnyElement { -// fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> (NodeId, Box); -// fn paint( -// &mut self, -// view: &mut V, -// layout: &Layout, -// paint: &mut dyn Any, -// cx: &mut PaintContext, -// ); -// } - -// impl AnyElement for E -// where -// E: Element, -// { -// fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> (NodeId, Box) { -// let (node_id, paint_state) = self.layout(view, cx); -// (node_id, Box::new(paint_state)) -// } - -// fn paint( -// &mut self, -// view: &mut V, -// layout: &Layout, -// paint: &mut dyn Any, -// cx: &mut PaintContext, -// ) { -// let paint = paint.downcast_mut().unwrap(); -// self.paint(view, layout, paint, cx); -// } -// } -// } - -// /// A dynamically-typed element. -// struct AnyElement { -// // An element as a trait object. -// element: Box>, -// // Data computed during layout that is used during paint. -// paint: Option>, -// } - -// impl AnyElement { -// pub fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> NodeId { -// let (id, paint_data) = self.element.layout(view, cx); -// self.paint = Some(paint_data); -// id -// } - -// pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext) { -// let layout = cx.layout_engine().layout(node) -// let paint = self -// .paint -// .as_mut() -// .expect("called paint before calling layout"); -// self.element.paint(view, paint, cx); -// } -// } - -// // impl traits::AnyElement for AnyElement { -// // fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> NodeId { -// // self.element.layout(view, cx) -// // } - -// // fn paint(&mut self, view: &mut V, cx: &mut PaintContext) { -// // self.element.paint(view, cx) -// // } -// // } - -// // impl traits::AnyElement for E -// // where -// // E: Element, -// // { -// // fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> NodeId { -// // todo!() -// // } - -// // fn paint(&mut self, view: &mut V, cx: &mut PaintContext) { -// // todo!() -// // } -// // } - -// /// A basic stylable element, akin to a `div` in HTML. -// pub struct Frame { -// style: FrameStyle, -// metadata: FrameMetadata, -// children: Vec>, -// id: Option>, -// before_paint: Option)>>, -// } - -// /// The final result of the layout engine for a single node. -// struct Layout { -// /// The relative ordering of the node -// /// -// /// Nodes with a higher order should be rendered on top of those with a lower order. -// /// This is effectively a topological sort of each tree. -// pub order: u32, -// /// The position and size of the element in the layout. -// pub bounds: RectF, -// } - -// type NodeId = taffy::prelude::NodeId; - -// #[derive(Deref, DerefMut)] -// pub struct PaintContext<'a, 'b, 'c, V>(gpui::PaintContext<'a, 'b, 'c, V>); - -// impl<'a, 'b, 'c, V> PaintContext<'a, 'b, 'c, V> { -// fn set_stroke_style(&mut self, stroke: Stroke) {} -// fn stroke_rect(&mut self, rect: RectF, edges: Edges) {} - -// fn set_fill_style(&mut self, fill: Fill) {} -// fn fill_rect(&mut self, rect: RectF) {} -// } - -// impl traits::Element for Frame { -// type Paint = (); - -// fn layout(&mut self, view: &mut V, cx: &mut LayoutContext) -> (NodeId, ()) { -// let children_ids: Vec<_> = self.children.iter_mut().map(|child| todo!()).collect(); -// let node_id = cx -// .layout_engine() -// .new_with_children(self.style.layout.clone(), &children_ids) -// .unwrap(); -// (node_id, ()) -// } - -// fn paint(&mut self, view: &mut V, layout: &Layout, paint: &mut (), cx: &mut PaintContext) { -// // Fill the rectangle with the background color -// cx.set_fill_style(self.style.fill); -// cx.fill_rect(layout.bounds); - -// // Paint child elements -// for child in &mut self.children { -// child.paint(view, cx); -// } - -// // Applying border style if present -// if self.style.borders.is_visible() { -// cx.set_stroke_style(self.style.borders.stroke); -// cx.stroke_rect(layout.bounds, self.style.borders.edges()); // Draw the border -// } -// } -// } - -// impl Frame { -// pub fn id(mut self, id: impl Into>) -> Self { -// self.id = Some(id.into()); -// self -// } - -// pub fn child(mut self, child: impl traits::Element) -> Self { -// self.children.push(child.into_any()); -// self -// } - -// pub fn children(mut self, children: I) -> Self -// where -// I: IntoIterator, -// E: traits::Element, -// { -// self.children -// .extend(children.into_iter().map(|child| child.into_any())); -// self -// } - -// pub fn fill(mut self, fill: impl Into) -> Self { -// self.style.fill = fill.into(); -// self -// } - -// pub fn text_size(mut self, text_size: Rems) -> Self { -// self.style.text.size = Some(text_size); -// self -// } - -// pub fn text_color(mut self, color: Hsla) -> Self { -// self.style.text.color = Some(color); -// self -// } - -// fn before_paint(mut self, handler: H) -> Self -// where -// H: 'static + FnMut(RectF, &mut (), &mut PaintContext), -// { -// self.before_paint = Some(Box::new(handler)); -// self -// } -// } - -// pub struct TopBottom { -// top: Length, -// bottom: Length, -// } - -// impl> From<(T, T)> for TopBottom { -// fn from((top, bottom): (T, T)) -> Self { -// Self { -// top: top.into(), -// bottom: bottom.into(), -// } -// } -// } - -// impl> From for TopBottom { -// fn from(both: T) -> Self { -// Self { -// top: both.into(), -// bottom: both.into(), -// } -// } -// } - -// pub struct LeftRight { -// left: Length, -// right: Length, -// } - -// impl From<(Length, Length)> for LeftRight { -// fn from((left, right): (Length, Length)) -> Self { -// Self { left, right } -// } -// } - -// impl From for LeftRight { -// fn from(both: Length) -> Self { -// Self { -// left: both, -// right: both, -// } -// } -// } - -// struct Interactive