use crate::{ div::div, element::{Element, ParentElement}, style::StyleHelpers, text::ArcCow, themes::rose_pine, }; use gpui::ViewContext; use playground_macros::Element; use std::{marker::PhantomData, rc::Rc}; struct ButtonHandlers { click: Option)>>, } impl Default for ButtonHandlers { fn default() -> Self { Self { click: None } } } use crate as playground; #[derive(Element)] pub struct Button { 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 { handlers: ButtonHandlers::default(), label: None, icon: None, data: Rc::new(()), view_type: PhantomData, } } pub fn data(self, data: D) -> Button { Button { 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(); // Self::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 = div() .fill(rose_pine::dawn().error(0.5)) .h_4() .children(self.label.clone()); button // TODO: Event handling // 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 // } } }