Wire up click handlers on Buttons

This commit is contained in:
Marshall Bowers 2023-10-10 17:19:18 -04:00
parent f2ee61553f
commit 48d9b49ada

View file

@ -1,7 +1,7 @@
use std::marker::PhantomData; 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::prelude::*;
use crate::{h_stack, theme, Icon, IconColor, IconElement, Label, LabelColor, LabelSize}; use crate::{h_stack, theme, Icon, IconColor, IconElement, Label, LabelColor, LabelSize};
@ -20,15 +20,15 @@ pub enum ButtonVariant {
Filled, Filled,
} }
// struct ButtonHandlers<V> { struct ButtonHandlers<S: 'static + Send + Sync> {
// click: Option<Rc<dyn Fn(&mut V, &mut EventContext<V>)>>, click: Option<Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>>,
// } }
// impl<V> Default for ButtonHandlers<V> { impl<S: 'static + Send + Sync> Default for ButtonHandlers<S> {
// fn default() -> Self { fn default() -> Self {
// Self { click: None } Self { click: None }
// } }
// } }
#[derive(Element)] #[derive(Element)]
pub struct Button<S: 'static + Send + Sync + Clone> { pub struct Button<S: 'static + Send + Sync + Clone> {
@ -39,7 +39,7 @@ pub struct Button<S: 'static + Send + Sync + Clone> {
icon: Option<Icon>, icon: Option<Icon>,
icon_position: Option<IconPosition>, icon_position: Option<IconPosition>,
width: Option<DefiniteLength>, width: Option<DefiniteLength>,
// handlers: ButtonHandlers<S>, handlers: ButtonHandlers<S>,
} }
impl<S: 'static + Send + Sync + Clone> Button<S> { impl<S: 'static + Send + Sync + Clone> Button<S> {
@ -55,7 +55,7 @@ impl<S: 'static + Send + Sync + Clone> Button<S> {
icon: None, icon: None,
icon_position: None, icon_position: None,
width: Default::default(), width: Default::default(),
// handlers: ButtonHandlers::default(), handlers: ButtonHandlers::default(),
} }
} }
@ -94,10 +94,13 @@ impl<S: 'static + Send + Sync + Clone> Button<S> {
self self
} }
// pub fn on_click(mut self, handler: impl Fn(&mut S, &mut EventContext<S>) + 'static) -> Self { pub fn on_click(
// self.handlers.click = Some(Rc::new(handler)); mut self,
// self handler: impl Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync,
// } ) -> Self {
self.handlers.click = Some(Arc::new(handler));
self
}
fn background_color(&self, cx: &mut ViewContext<S>) -> Hsla { fn background_color(&self, cx: &mut ViewContext<S>) -> Hsla {
let theme = theme(cx); let theme = theme(cx);
@ -193,11 +196,11 @@ impl<S: 'static + Send + Sync + Clone> Button<S> {
el = el.w(width).justify_center(); el = el.w(width).justify_center();
} }
// if let Some(click_handler) = self.handlers.click.clone() { if let Some(click_handler) = self.handlers.click.clone() {
// el = el.on_mouse_down(MouseButton::Left, move |view, event, cx| { el = el.on_click(MouseButton::Left, move |state, event, cx| {
// click_handler(view, cx); click_handler(state, cx);
// }); });
// } }
el el
} }
@ -398,9 +401,9 @@ mod stories {
) )
.child(Story::label(cx, "Button with `on_click`")) .child(Story::label(cx, "Button with `on_click`"))
.child( .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. Button::new("Label")
// So adding additional buttons with `on_click`s after this one will cause this `on_click` to not fire. .variant(ButtonVariant::Ghost)
// .on_click(|_view, _cx| println!("Button clicked.")), .on_click(|_view, _cx| println!("Button clicked.")),
) )
} }
} }