From 48d9b49adaf3232e557462eda65acda04bc421d0 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 10 Oct 2023 17:19:18 -0400 Subject: [PATCH] Wire up click handlers on `Button`s --- crates/ui2/src/elements/button.rs | 51 ++++++++++++++++--------------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/crates/ui2/src/elements/button.rs b/crates/ui2/src/elements/button.rs index a8f40c1e68..a8c3b2694d 100644 --- a/crates/ui2/src/elements/button.rs +++ b/crates/ui2/src/elements/button.rs @@ -1,7 +1,7 @@ use std::marker::PhantomData; -use std::rc::Rc; +use std::sync::Arc; -use gpui3::{DefiniteLength, Hsla, MouseButton, WindowContext}; +use gpui3::{DefiniteLength, Hsla, Interactive, MouseButton, WindowContext}; use crate::prelude::*; use crate::{h_stack, theme, Icon, IconColor, IconElement, Label, LabelColor, LabelSize}; @@ -20,15 +20,15 @@ pub enum ButtonVariant { Filled, } -// struct ButtonHandlers { -// click: Option)>>, -// } +struct ButtonHandlers { + click: Option) + 'static + Send + Sync>>, +} -// impl Default for ButtonHandlers { -// fn default() -> Self { -// Self { click: None } -// } -// } +impl Default for ButtonHandlers { + fn default() -> Self { + Self { click: None } + } +} #[derive(Element)] pub struct Button { @@ -39,7 +39,7 @@ pub struct Button { icon: Option, icon_position: Option, width: Option, - // handlers: ButtonHandlers, + handlers: ButtonHandlers, } impl Button { @@ -55,7 +55,7 @@ impl Button { icon: None, icon_position: None, width: Default::default(), - // handlers: ButtonHandlers::default(), + handlers: ButtonHandlers::default(), } } @@ -94,10 +94,13 @@ impl Button { self } - // pub fn on_click(mut self, handler: impl Fn(&mut S, &mut EventContext) + 'static) -> Self { - // self.handlers.click = Some(Rc::new(handler)); - // self - // } + pub fn on_click( + mut self, + handler: impl Fn(&mut S, &mut ViewContext) + 'static + Send + Sync, + ) -> Self { + self.handlers.click = Some(Arc::new(handler)); + self + } fn background_color(&self, cx: &mut ViewContext) -> Hsla { let theme = theme(cx); @@ -193,11 +196,11 @@ impl Button { el = el.w(width).justify_center(); } - // if let Some(click_handler) = self.handlers.click.clone() { - // el = el.on_mouse_down(MouseButton::Left, move |view, event, cx| { - // click_handler(view, cx); - // }); - // } + if let Some(click_handler) = self.handlers.click.clone() { + el = el.on_click(MouseButton::Left, move |state, event, cx| { + click_handler(state, cx); + }); + } el } @@ -398,9 +401,9 @@ mod stories { ) .child(Story::label(cx, "Button with `on_click`")) .child( - Button::new("Label").variant(ButtonVariant::Ghost), // NOTE: There currently appears to be a bug in GPUI2 where only the last event handler will fire. - // So adding additional buttons with `on_click`s after this one will cause this `on_click` to not fire. - // .on_click(|_view, _cx| println!("Button clicked.")), + Button::new("Label") + .variant(ButtonVariant::Ghost) + .on_click(|_view, _cx| println!("Button clicked.")), ) } }