diff --git a/crates/gpui/playground/src/components.rs b/crates/gpui/playground/src/components.rs index c966c4d293..055a9d63e6 100644 --- a/crates/gpui/playground/src/components.rs +++ b/crates/gpui/playground/src/components.rs @@ -18,18 +18,22 @@ impl Default for ButtonHandlers { pub struct Button { metadata: ElementMetadata, - button_handlers: ButtonHandlers, - label: Cow<'static, str>, + 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(label: impl Into>) -> Self { + fn new() -> Self { Self { metadata: Default::default(), - button_handlers: ButtonHandlers::default(), - label: label.into(), + handlers: ButtonHandlers::default(), + label: None, + icon: None, data: Rc::new(()), view_type: PhantomData, } @@ -38,15 +42,27 @@ impl Button { pub fn data(self, data: D) -> Button { Button { metadata: Default::default(), - button_handlers: ButtonHandlers::default(), + handlers: ButtonHandlers::default(), label: self.label, + icon: self.icon, data: Rc::new(data), view_type: PhantomData, } } } +// Impl block for *any* button. impl Button { + fn label(mut self, label: impl Into>) -> Self { + self.label = Some(label.into()); + self + } + + fn icon(mut self, icon: impl Into>) -> Self { + self.icon = Some(icon.into()); + self + } + fn click(self, handler: impl Fn(&mut V, &D) + 'static) -> Self { let data = self.data.clone(); Element::click(self, move |view, _| { @@ -55,8 +71,8 @@ impl Button { } } -pub fn button(label: impl Into>) -> Button { - Button::new(label) +pub fn button() -> Button { + Button::new() } impl Button { @@ -64,7 +80,7 @@ impl Button { // TODO: Drive from the context let button = frame().fill(rose_pine::dawn().error(0.5)).h_5().w_9(); - if let Some(handler) = self.button_handlers.click.clone() { + if let Some(handler) = self.handlers.click.clone() { let data = self.data.clone(); button.click(move |view, event| handler(view, data.as_ref())) } else { diff --git a/crates/gpui/playground/src/frame.rs b/crates/gpui/playground/src/frame.rs index 4c2cc5d385..7dba849085 100644 --- a/crates/gpui/playground/src/frame.rs +++ b/crates/gpui/playground/src/frame.rs @@ -64,3 +64,10 @@ impl Element for Frame { Ok(()) } } + +impl Frame { + pub fn child(mut self, child: impl Element) -> Self { + self.children.push(child.into_any()); + self + } +} diff --git a/crates/gpui/playground/src/playground.rs b/crates/gpui/playground/src/playground.rs index 7f354cf3a8..feed975eff 100644 --- a/crates/gpui/playground/src/playground.rs +++ b/crates/gpui/playground/src/playground.rs @@ -1,4 +1,5 @@ #![allow(dead_code, unused_variables)] +use components::button; use element::Element; use frame::frame; use gpui::{ @@ -42,7 +43,11 @@ fn main() { fn workspace(theme: &ThemeColors) -> impl Element { // frame().w_full().h_half().fill(theme.success(0.5)) - frame().h_full().w(percent(50.)).fill(theme.success(0.5)) + frame() + .h_full() + .w(percent(50.)) + .fill(theme.success(0.5)) + .child(button()) } // todo!() // // column()