Checkpoint

This commit is contained in:
Nathan Sobo 2023-08-22 09:07:45 -06:00
parent ff7b25c538
commit 53679ce045
8 changed files with 228 additions and 263 deletions

View file

@ -1,131 +1,101 @@
// use crate::{ use crate::{
// element::{Element, ElementMetadata, ParentElement}, div::div,
// frame, element::{Element, ParentElement},
// text::ArcCow, style::StyleHelpers,
// themes::rose_pine, text::ArcCow,
// }; themes::rose_pine,
// use gpui::{platform::MouseButton, ViewContext}; };
// use playground_macros::Element; use gpui::ViewContext;
// use std::{marker::PhantomData, rc::Rc}; use playground_macros::Element;
// struct ButtonHandlers<V, D> { use std::{marker::PhantomData, rc::Rc};
// click: Option<Rc<dyn Fn(&mut V, &D, &mut ViewContext<V>)>>,
// }
// impl<V, D> Default for ButtonHandlers<V, D> { struct ButtonHandlers<V, D> {
// fn default() -> Self { click: Option<Rc<dyn Fn(&mut V, &D, &mut ViewContext<V>)>>,
// Self { click: None } }
// }
// }
// #[derive(Element)] impl<V, D> Default for ButtonHandlers<V, D> {
// #[element_crate = "crate"] fn default() -> Self {
// pub struct Button<V: 'static, D: 'static> { Self { click: None }
// metadata: ElementMetadata<V>, }
// handlers: ButtonHandlers<V, D>, }
// label: Option<ArcCow<'static, str>>,
// icon: Option<ArcCow<'static, str>>,
// data: Rc<D>,
// view_type: PhantomData<V>,
// }
// // Impl block for buttons without data. use crate as playground;
// // See below for an impl block for any button. #[derive(Element)]
// impl<V: 'static> Button<V, ()> { pub struct Button<V: 'static, D: 'static> {
// fn new() -> Self { handlers: ButtonHandlers<V, D>,
// Self { label: Option<ArcCow<'static, str>>,
// metadata: Default::default(), icon: Option<ArcCow<'static, str>>,
// handlers: ButtonHandlers::default(), data: Rc<D>,
// label: None, view_type: PhantomData<V>,
// icon: None, }
// data: Rc::new(()),
// view_type: PhantomData,
// }
// }
// pub fn data<D: 'static>(self, data: D) -> Button<V, D> { // Impl block for buttons without data.
// Button { // See below for an impl block for any button.
// metadata: Default::default(), impl<V: 'static> Button<V, ()> {
// handlers: ButtonHandlers::default(), fn new() -> Self {
// label: self.label, Self {
// icon: self.icon, handlers: ButtonHandlers::default(),
// data: Rc::new(data), label: None,
// view_type: PhantomData, icon: None,
// } data: Rc::new(()),
// } view_type: PhantomData,
// } }
}
// // Impl block for *any* button. pub fn data<D: 'static>(self, data: D) -> Button<V, D> {
// impl<V: 'static, D: 'static> Button<V, D> { Button {
// pub fn label(mut self, label: impl Into<ArcCow<'static, str>>) -> Self { handlers: ButtonHandlers::default(),
// self.label = Some(label.into()); label: self.label,
// self icon: self.icon,
// } data: Rc::new(data),
view_type: PhantomData,
}
}
}
// pub fn icon(mut self, icon: impl Into<ArcCow<'static, str>>) -> Self { // Impl block for *any* button.
// self.icon = Some(icon.into()); impl<V: 'static, D: 'static> Button<V, D> {
// self pub fn label(mut self, label: impl Into<ArcCow<'static, str>>) -> Self {
// } self.label = Some(label.into());
self
}
// pub fn click(self, handler: impl Fn(&mut V, &D, &mut ViewContext<V>) + 'static) -> Self { pub fn icon(mut self, icon: impl Into<ArcCow<'static, str>>) -> Self {
// let data = self.data.clone(); self.icon = Some(icon.into());
// Element::click(self, MouseButton::Left, move |view, _, cx| { self
// handler(view, data.as_ref(), cx); }
// })
// }
// }
// pub fn button<V>() -> Button<V, ()> { // pub fn click(self, handler: impl Fn(&mut V, &D, &mut ViewContext<V>) + 'static) -> Self {
// Button::new() // let data = self.data.clone();
// } // Self::click(self, MouseButton::Left, move |view, _, cx| {
// handler(view, data.as_ref(), cx);
// })
// }
}
// impl<V: 'static, D: 'static> Button<V, D> { pub fn button<V>() -> Button<V, ()> {
// fn render(&mut self, view: &mut V, cx: &mut ViewContext<V>) -> impl Element<V> { Button::new()
// // TODO: Drive theme from the context }
// let button = frame()
// .fill(rose_pine::dawn().error(0.5))
// .h_4()
// .children(self.label.clone());
// if let Some(handler) = self.handlers.click.clone() { impl<V: 'static, D: 'static> Button<V, D> {
// let data = self.data.clone(); fn render(&mut self, view: &mut V, cx: &mut ViewContext<V>) -> impl Element<V> {
// button.mouse_down(MouseButton::Left, move |view, event, cx| { // TODO: Drive theme from the context
// handler(view, data.as_ref(), cx) let button = div()
// }) .fill(rose_pine::dawn().error(0.5))
// } else { .h_4()
// button .children(self.label.clone());
// }
// }
// }
// // impl<V: 'static, D> Element<V> for Button<V, D> { button
// // type Layout = AnyElement<V>;
// // fn style_mut(&mut self) -> &mut crate::style::ElementStyle { // TODO: Event handling
// // &mut self.metadata.style // if let Some(handler) = self.handlers.click.clone() {
// // } // let data = self.data.clone();
// // button.mouse_down(MouseButton::Left, move |view, event, cx| {
// // fn handlers_mut(&mut self) -> &mut crate::element::ElementHandlers<V> { // // handler(view, data.as_ref(), cx)
// // &mut self.metadata.handlers // // })
// // } // } else {
// button
// // fn layout( // }
// // &mut self, }
// // view: &mut V, }
// // cx: &mut crate::element::LayoutContext<V>,
// // ) -> anyhow::Result<(taffy::tree::NodeId, Self::Layout)> {
// // let mut element = self.render(view, cx).into_any();
// // let node_id = element.layout(view, cx)?;
// // Ok((node_id, element))
// // }
// // fn paint<'a>(
// // &mut self,
// // layout: crate::element::Layout<'a, Self::Layout>,
// // view: &mut V,
// // cx: &mut crate::element::PaintContext<V>,
// // ) -> anyhow::Result<()> {
// // layout.from_element.paint(view, cx)?;
// // Ok(())
// // }
// // }

View file

@ -1,8 +1,8 @@
use crate::{ use crate::{
element::{AnyElement, Element, Layout}, element::{AnyElement, Element, Layout, ParentElement},
layout_context::LayoutContext, layout_context::LayoutContext,
paint_context::PaintContext, paint_context::PaintContext,
style::{Style, StyleRefinement, Styleable}, style::{Style, StyleHelpers, StyleRefinement, Styleable},
}; };
use anyhow::Result; use anyhow::Result;
use gpui::{platform::MouseMovedEvent, EventContext, LayoutId}; use gpui::{platform::MouseMovedEvent, EventContext, LayoutId};
@ -14,14 +14,6 @@ pub struct Div<V> {
children: SmallVec<[AnyElement<V>; 2]>, children: SmallVec<[AnyElement<V>; 2]>,
} }
impl<V> Styleable for Div<V> {
type Style = Style;
fn declared_style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
pub fn div<V>() -> Div<V> { pub fn div<V>() -> Div<V> {
Div { Div {
style: Default::default(), style: Default::default(),
@ -55,6 +47,22 @@ impl<V: 'static> Element<V> for Div<V> {
} }
} }
impl<V> Styleable for Div<V> {
type Style = Style;
fn declared_style(&mut self) -> &mut crate::style::StyleRefinement {
&mut self.style
}
}
impl<V> StyleHelpers for Div<V> {}
impl<V: 'static> ParentElement<V> for Div<V> {
fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]> {
&mut self.children
}
}
pub trait Interactive<V> { pub trait Interactive<V> {
fn declared_interactions(&mut self) -> &mut Interactions<V>; fn declared_interactions(&mut self) -> &mut Interactions<V>;

View file

@ -1,12 +1,12 @@
use anyhow::Result; use anyhow::Result;
use derive_more::{Deref, DerefMut}; use derive_more::{Deref, DerefMut};
use gpui::geometry::rect::RectF; use gpui::{geometry::rect::RectF, EngineLayout};
use gpui::EngineLayout; use smallvec::SmallVec;
use std::marker::PhantomData; use std::marker::PhantomData;
use util::ResultExt; use util::ResultExt;
use crate::layout_context::LayoutContext; pub use crate::layout_context::LayoutContext;
use crate::paint_context::PaintContext; pub use crate::paint_context::PaintContext;
type LayoutId = gpui::LayoutId; type LayoutId = gpui::LayoutId;
@ -21,11 +21,11 @@ pub struct Layout<V, D> {
} }
impl<V: 'static, D> Layout<V, D> { impl<V: 'static, D> Layout<V, D> {
pub fn new(id: LayoutId, engine_layout: Option<EngineLayout>, element_data: D) -> Self { pub fn new(id: LayoutId, element_data: D) -> Self {
Self { Self {
id, id,
engine_layout, engine_layout: None,
element_data, element_data: element_data,
view_type: PhantomData, view_type: PhantomData,
} }
} }
@ -44,6 +44,14 @@ impl<V: 'static, D> Layout<V, D> {
} }
} }
impl<V: 'static> Layout<V, Option<AnyElement<V>>> {
pub fn paint(&mut self, view: &mut V, cx: &mut PaintContext<V>) {
let mut element = self.element_data.take().unwrap();
element.paint(view, self.id, cx);
self.element_data = Some(element);
}
}
pub trait Element<V: 'static>: 'static { pub trait Element<V: 'static>: 'static {
type Layout; type Layout;
@ -97,6 +105,9 @@ impl<V, E: Element<V>> ElementStateObject<V> for ElementState<V, E> {
fn paint(&mut self, view: &mut V, layout_id: LayoutId, cx: &mut PaintContext<V>) { fn paint(&mut self, view: &mut V, layout_id: LayoutId, cx: &mut PaintContext<V>) {
let layout = self.layout.as_mut().expect("paint called before layout"); let layout = self.layout.as_mut().expect("paint called before layout");
if layout.engine_layout.is_none() {
layout.engine_layout = cx.computed_layout(layout_id).log_err()
}
self.element.paint(view, layout, cx) self.element.paint(view, layout, cx)
} }
} }
@ -115,7 +126,7 @@ impl<V> AnyElement<V> {
} }
pub trait ParentElement<V: 'static> { pub trait ParentElement<V: 'static> {
fn children_mut(&mut self) -> &mut Vec<AnyElement<V>>; fn children_mut(&mut self) -> &mut SmallVec<[AnyElement<V>; 2]>;
fn child(mut self, child: impl IntoElement<V>) -> Self fn child(mut self, child: impl IntoElement<V>) -> Self
where where

View file

@ -49,6 +49,6 @@ impl<'a, 'b, 'c, 'd, V: 'static> LayoutContext<'a, 'b, 'c, 'd, V> {
.ok_or_else(|| anyhow!("no layout engine"))? .ok_or_else(|| anyhow!("no layout engine"))?
.add_node(style.to_taffy(rem_size), children)?; .add_node(style.to_taffy(rem_size), children)?;
Ok(Layout::new(id, None, element_data)) Ok(Layout::new(id, element_data))
} }
} }

View file

@ -1,8 +1,14 @@
#![allow(dead_code, unused_variables)] #![allow(dead_code, unused_variables)]
use crate::{color::black, style::StyleHelpers};
use element::Element;
use gpui::{
geometry::{rect::RectF, vector::vec2f},
platform::WindowOptions,
};
use log::LevelFilter; use log::LevelFilter;
use simplelog::SimpleLogger; use simplelog::SimpleLogger;
use themes::{rose_pine, ThemeColors};
use themes::ThemeColors; use view::view;
mod adapter; mod adapter;
mod color; mod color;
@ -21,32 +27,33 @@ fn main() {
SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger"); SimpleLogger::init(LevelFilter::Info, Default::default()).expect("could not initialize logger");
gpui::App::new(()).unwrap().run(|cx| { gpui::App::new(()).unwrap().run(|cx| {
// cx.add_window( cx.add_window(
// WindowOptions { WindowOptions {
// bounds: gpui::platform::WindowBounds::Fixed(RectF::new( bounds: gpui::platform::WindowBounds::Fixed(RectF::new(
// vec2f(0., 0.), vec2f(0., 0.),
// vec2f(400., 300.), vec2f(400., 300.),
// )), )),
// center: true, center: true,
// ..Default::default() ..Default::default()
// }, },
// |_| view(|_| playground(&rose_pine::moon())), |_| view(|_| playground(&rose_pine::moon())),
// ); );
cx.platform().activate(true); cx.platform().activate(true);
}); });
} }
// fn playground<V: 'static>(theme: &ThemeColors) -> impl Element<V> { fn playground<V: 'static>(theme: &ThemeColors) -> impl Element<V> {
// todo!() use div::div;
// // frame()
// // .text_color(black()) div()
// // .h_full() .text_color(black())
// // .w_half() .h_full()
// // .fill(theme.success(0.5)) .w_24()
// // .hover() .fill(theme.success(0.5))
// // .fill(theme.error(0.5)) // .hover()
// // .child(button().label("Hello").click(|_, _, _| println!("click!"))) // .fill(theme.error(0.5))
// } // .child(button().label("Hello").click(|_, _, _| println!("click!")))
}
// todo!() // todo!()
// // column() // // column()

View file

@ -257,7 +257,7 @@ pub trait Styleable {
} }
} }
// Tailwind-style helpers methods that take and return mut self // Helpers methods that take and return mut self. This includes tailwind style methods for standard sizes etc.
// //
// Example: // Example:
// // Sets the padding to 0.5rem, just like class="p-2" in Tailwind. // // Sets the padding to 0.5rem, just like class="p-2" in Tailwind.
@ -265,4 +265,22 @@ pub trait Styleable {
use crate as playground; // Macro invocation references this crate as playground. use crate as playground; // Macro invocation references this crate as playground.
pub trait StyleHelpers: Styleable<Style = Style> { pub trait StyleHelpers: Styleable<Style = Style> {
styleable_helpers!(); styleable_helpers!();
fn fill<F>(mut self, fill: F) -> Self
where
F: Into<Fill>,
Self: Sized,
{
self.declared_style().fill = Some(fill.into());
self
}
fn text_color<C>(mut self, color: C) -> Self
where
C: Into<Hsla>,
Self: Sized,
{
self.declared_style().text_color = Some(color.into());
self
}
} }

View file

@ -1,72 +1,57 @@
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::{format_ident, quote}; use proc_macro2::Ident;
use syn::{ use quote::quote;
parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics, Ident, Lit, Meta, use syn::{parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics};
WhereClause,
};
use crate::derive_into_element::impl_into_element; use crate::derive_into_element::impl_into_element;
pub fn derive_element(input: TokenStream) -> TokenStream { pub fn derive_element(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput); let ast = parse_macro_input!(input as DeriveInput);
let type_name = ast.ident; let type_name = ast.ident;
let crate_name: String = ast
.attrs
.iter()
.find_map(|attr| {
if attr.path.is_ident("element_crate") {
match attr.parse_meta() {
Ok(Meta::NameValue(nv)) => {
if let Lit::Str(s) = nv.lit {
Some(s.value())
} else {
None
}
}
_ => None,
}
} else {
None
}
})
.unwrap_or_else(|| String::from("playground"));
let crate_name = format_ident!("{}", crate_name);
let placeholder_view_generics: Generics = parse_quote! { <V: 'static> }; let placeholder_view_generics: Generics = parse_quote! { <V: 'static> };
let placeholder_view_type_name: Ident = parse_quote! { V };
let view_type_name: Ident;
let impl_generics: syn::ImplGenerics<'_>;
let type_generics: Option<syn::TypeGenerics<'_>>;
let where_clause: Option<&'_ WhereClause>;
match ast.generics.params.iter().find_map(|param| { let (impl_generics, type_generics, where_clause, view_type_name, lifetimes) =
if let Some(first_type_param) = ast.generics.params.iter().find_map(|param| {
if let GenericParam::Type(type_param) = param { if let GenericParam::Type(type_param) = param {
Some(type_param.ident.clone()) Some(type_param.ident.clone())
} else { } else {
None None
} }
}) { }) {
Some(type_name) => { let mut lifetimes = vec![];
view_type_name = type_name; for param in ast.generics.params.iter() {
if let GenericParam::Lifetime(lifetime_def) = param {
lifetimes.push(lifetime_def.lifetime.clone());
}
}
let generics = ast.generics.split_for_impl(); let generics = ast.generics.split_for_impl();
impl_generics = generics.0; (
type_generics = Some(generics.1); generics.0,
where_clause = generics.2; Some(generics.1),
} generics.2,
_ => { first_type_param,
view_type_name = placeholder_view_type_name; lifetimes,
)
} else {
let generics = placeholder_view_generics.split_for_impl(); let generics = placeholder_view_generics.split_for_impl();
impl_generics = generics.0; let placeholder_view_type_name: Ident = parse_quote! { V };
type_generics = None; (
where_clause = generics.2; generics.0,
} None,
} generics.2,
placeholder_view_type_name,
vec![],
)
};
let lifetimes = if !lifetimes.is_empty() {
quote! { <#(#lifetimes),*> }
} else {
quote! {}
};
let impl_into_element = impl_into_element( let impl_into_element = impl_into_element(
&impl_generics, &impl_generics,
&crate_name,
&view_type_name, &view_type_name,
&type_name, &type_name,
&type_generics, &type_generics,
@ -74,37 +59,28 @@ pub fn derive_element(input: TokenStream) -> TokenStream {
); );
let gen = quote! { let gen = quote! {
impl #impl_generics #crate_name::element::Element<#view_type_name> for #type_name #type_generics impl #impl_generics playground::element::Element<#view_type_name> for #type_name #type_generics
#where_clause #where_clause
{ {
type Layout = #crate_name::element::AnyElement<V>; type Layout = Option<playground::element::AnyElement<#view_type_name #lifetimes>>;
fn declared_style(&mut self) -> &mut #crate_name::style::StyleRefinement {
&mut self.metadata.style
}
fn handlers_mut(&mut self) -> &mut Vec<#crate_name::element::EventHandler<V>> {
&mut self.metadata.handlers
}
fn layout( fn layout(
&mut self, &mut self,
view: &mut V, view: &mut V,
cx: &mut #crate_name::element::LayoutContext<V>, cx: &mut playground::element::LayoutContext<V>,
) -> anyhow::Result<(taffy::tree::NodeId, Self::Layout)> { ) -> anyhow::Result<playground::element::Layout<V, Self::Layout>> {
let mut element = self.render(view, cx).into_any(); let mut element = self.render(view, cx).into_any();
let node_id = element.layout(view, cx)?; let layout_id = element.layout(view, cx)?;
Ok((node_id, element)) Ok(playground::element::Layout::new(layout_id, Some(element)))
} }
fn paint<'a>( fn paint(
&mut self, &mut self,
layout: #crate_name::element::Layout<'a, Self::Layout>,
view: &mut V, view: &mut V,
cx: &mut #crate_name::element::PaintContext<V>, layout: &mut playground::element::Layout<V, Self::Layout>,
) -> anyhow::Result<()> { cx: &mut playground::element::PaintContext<V>,
layout.from_element.paint(view, cx)?; ) {
Ok(()) layout.paint(view, cx);
} }
} }

View file

@ -1,36 +1,13 @@
use proc_macro::TokenStream; use proc_macro::TokenStream;
use quote::{format_ident, quote}; use quote::quote;
use syn::{ use syn::{
parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics, Ident, Lit, Meta, parse_macro_input, parse_quote, DeriveInput, GenericParam, Generics, Ident, WhereClause,
WhereClause,
}; };
pub fn derive_into_element(input: TokenStream) -> TokenStream { pub fn derive_into_element(input: TokenStream) -> TokenStream {
let ast = parse_macro_input!(input as DeriveInput); let ast = parse_macro_input!(input as DeriveInput);
let type_name = ast.ident; let type_name = ast.ident;
let crate_name: String = ast
.attrs
.iter()
.find_map(|attr| {
if attr.path.is_ident("element_crate") {
match attr.parse_meta() {
Ok(Meta::NameValue(nv)) => {
if let Lit::Str(s) = nv.lit {
Some(s.value())
} else {
None
}
}
_ => None,
}
} else {
None
}
})
.unwrap_or_else(|| String::from("playground"));
let crate_name = format_ident!("{}", crate_name);
let placeholder_view_generics: Generics = parse_quote! { <V: 'static> }; let placeholder_view_generics: Generics = parse_quote! { <V: 'static> };
let placeholder_view_type_name: Ident = parse_quote! { V }; let placeholder_view_type_name: Ident = parse_quote! { V };
let view_type_name: Ident; let view_type_name: Ident;
@ -63,7 +40,6 @@ pub fn derive_into_element(input: TokenStream) -> TokenStream {
impl_into_element( impl_into_element(
&impl_generics, &impl_generics,
&crate_name,
&view_type_name, &view_type_name,
&type_name, &type_name,
&type_generics, &type_generics,
@ -74,14 +50,13 @@ pub fn derive_into_element(input: TokenStream) -> TokenStream {
pub fn impl_into_element( pub fn impl_into_element(
impl_generics: &syn::ImplGenerics<'_>, impl_generics: &syn::ImplGenerics<'_>,
crate_name: &Ident,
view_type_name: &Ident, view_type_name: &Ident,
type_name: &Ident, type_name: &Ident,
type_generics: &Option<syn::TypeGenerics<'_>>, type_generics: &Option<syn::TypeGenerics<'_>>,
where_clause: &Option<&WhereClause>, where_clause: &Option<&WhereClause>,
) -> proc_macro2::TokenStream { ) -> proc_macro2::TokenStream {
quote! { quote! {
impl #impl_generics #crate_name::element::IntoElement<#view_type_name> for #type_name #type_generics impl #impl_generics playground::element::IntoElement<#view_type_name> for #type_name #type_generics
#where_clause #where_clause
{ {
type Element = Self; type Element = Self;