diff --git a/crates/gpui/playground/src/components.rs b/crates/gpui/playground/src/components.rs index 77855b07c2..0ea1b6d02b 100644 --- a/crates/gpui/playground/src/components.rs +++ b/crates/gpui/playground/src/components.rs @@ -1,131 +1,131 @@ -use crate::{ - element::{Element, ElementMetadata, ParentElement}, - frame, - text::ArcCow, - themes::rose_pine, -}; -use gpui::{platform::MouseButton, ViewContext}; -use playground_macros::Element; -use std::{marker::PhantomData, rc::Rc}; +// use crate::{ +// element::{Element, ElementMetadata, ParentElement}, +// frame, +// text::ArcCow, +// themes::rose_pine, +// }; +// use gpui::{platform::MouseButton, ViewContext}; +// use playground_macros::Element; +// use std::{marker::PhantomData, rc::Rc}; -struct ButtonHandlers { - click: Option)>>, -} +// struct ButtonHandlers { +// click: Option)>>, +// } -impl Default for ButtonHandlers { - fn default() -> Self { - Self { click: None } - } -} - -#[derive(Element)] -#[element_crate = "crate"] -pub struct Button { - metadata: ElementMetadata, - handlers: ButtonHandlers, - label: Option>, - icon: Option>, - data: Rc, - view_type: PhantomData, -} - -// Impl block for buttons without data. -// See below for an impl block for any button. -impl Button { - fn new() -> Self { - Self { - metadata: Default::default(), - handlers: ButtonHandlers::default(), - label: None, - icon: None, - data: Rc::new(()), - view_type: PhantomData, - } - } - - pub fn data(self, data: D) -> Button { - Button { - metadata: Default::default(), - handlers: ButtonHandlers::default(), - label: self.label, - icon: self.icon, - data: Rc::new(data), - view_type: PhantomData, - } - } -} - -// Impl block for *any* button. -impl Button { - pub fn label(mut self, label: impl Into>) -> Self { - self.label = Some(label.into()); - self - } - - pub fn icon(mut self, icon: impl Into>) -> Self { - self.icon = Some(icon.into()); - self - } - - pub fn click(self, handler: impl Fn(&mut V, &D, &mut ViewContext) + 'static) -> Self { - let data = self.data.clone(); - Element::click(self, MouseButton::Left, move |view, _, cx| { - handler(view, data.as_ref(), cx); - }) - } -} - -pub fn button() -> Button { - Button::new() -} - -impl Button { - fn render(&mut self, view: &mut V, cx: &mut ViewContext) -> impl Element { - // 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() { - let data = self.data.clone(); - button.mouse_down(MouseButton::Left, move |view, event, cx| { - handler(view, data.as_ref(), cx) - }) - } else { - button - } - } -} - -// impl Element for Button { -// type Layout = AnyElement; - -// fn style_mut(&mut self) -> &mut crate::style::ElementStyle { -// &mut self.metadata.style -// } - -// fn handlers_mut(&mut self) -> &mut crate::element::ElementHandlers { -// &mut self.metadata.handlers -// } - -// fn layout( -// &mut self, -// view: &mut V, -// cx: &mut crate::element::LayoutContext, -// ) -> 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, -// ) -> anyhow::Result<()> { -// layout.from_element.paint(view, cx)?; -// Ok(()) +// impl Default for ButtonHandlers { +// fn default() -> Self { +// Self { click: None } // } // } + +// #[derive(Element)] +// #[element_crate = "crate"] +// pub struct Button { +// metadata: ElementMetadata, +// handlers: ButtonHandlers, +// label: Option>, +// icon: Option>, +// data: Rc, +// view_type: PhantomData, +// } + +// // Impl block for buttons without data. +// // See below for an impl block for any button. +// impl Button { +// fn new() -> Self { +// Self { +// metadata: Default::default(), +// handlers: ButtonHandlers::default(), +// label: None, +// icon: None, +// data: Rc::new(()), +// view_type: PhantomData, +// } +// } + +// pub fn data(self, data: D) -> Button { +// Button { +// metadata: Default::default(), +// handlers: ButtonHandlers::default(), +// label: self.label, +// icon: self.icon, +// data: Rc::new(data), +// view_type: PhantomData, +// } +// } +// } + +// // Impl block for *any* button. +// impl Button { +// pub fn label(mut self, label: impl Into>) -> Self { +// self.label = Some(label.into()); +// self +// } + +// pub fn icon(mut self, icon: impl Into>) -> Self { +// self.icon = Some(icon.into()); +// self +// } + +// pub fn click(self, handler: impl Fn(&mut V, &D, &mut ViewContext) + 'static) -> Self { +// let data = self.data.clone(); +// Element::click(self, MouseButton::Left, move |view, _, cx| { +// handler(view, data.as_ref(), cx); +// }) +// } +// } + +// pub fn button() -> Button { +// Button::new() +// } + +// impl Button { +// fn render(&mut self, view: &mut V, cx: &mut ViewContext) -> impl Element { +// // 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() { +// let data = self.data.clone(); +// button.mouse_down(MouseButton::Left, move |view, event, cx| { +// handler(view, data.as_ref(), cx) +// }) +// } else { +// button +// } +// } +// } + +// // impl Element for Button { +// // type Layout = AnyElement; + +// // fn style_mut(&mut self) -> &mut crate::style::ElementStyle { +// // &mut self.metadata.style +// // } + +// // fn handlers_mut(&mut self) -> &mut crate::element::ElementHandlers { +// // &mut self.metadata.handlers +// // } + +// // fn layout( +// // &mut self, +// // view: &mut V, +// // cx: &mut crate::element::LayoutContext, +// // ) -> 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, +// // ) -> anyhow::Result<()> { +// // layout.from_element.paint(view, cx)?; +// // Ok(()) +// // } +// // } diff --git a/crates/gpui/playground/src/div.rs b/crates/gpui/playground/src/div.rs index 1cc1d1761e..98a5e74dfb 100644 --- a/crates/gpui/playground/src/div.rs +++ b/crates/gpui/playground/src/div.rs @@ -78,7 +78,6 @@ pub trait Element { } } -use crate as playground; // Macro invocation below references this crate as playground. pub trait Styleable { type Style: refineable::Refineable; @@ -89,12 +88,15 @@ pub trait Styleable { style.refine(self.declared_style()); style } +} - // Tailwind-style helpers methods that take and return mut self - // - // Example: - // // Sets the padding to 0.5rem, just like class="p-2" in Tailwind. - // fn p_2(mut self) -> Self where Self: Sized; +// Tailwind-style helpers methods that take and return mut self +// +// Example: +// // Sets the padding to 0.5rem, just like class="p-2" in Tailwind. +// fn p_2(mut self) -> Self where Self: Sized; +use crate as playground; // Macro invocation references this crate as playground. +pub trait StyleHelpers: Styleable