diff --git a/crates/ui2/src/components/icon_button.rs b/crates/ui2/src/components/icon_button.rs index 0ece22b651..b08800f344 100644 --- a/crates/ui2/src/components/icon_button.rs +++ b/crates/ui2/src/components/icon_button.rs @@ -1,8 +1,21 @@ use std::marker::PhantomData; +use std::sync::Arc; + +use gpui3::{Interactive, MouseButton}; use crate::prelude::*; use crate::{theme, Icon, IconColor, IconElement}; +struct IconButtonHandlers { + click: Option) + 'static + Send + Sync>>, +} + +impl Default for IconButtonHandlers { + fn default() -> Self { + Self { click: None } + } +} + #[derive(Element)] pub struct IconButton { state_type: PhantomData, @@ -10,6 +23,7 @@ pub struct IconButton { color: IconColor, variant: ButtonVariant, state: InteractionState, + handlers: IconButtonHandlers, } impl IconButton { @@ -20,6 +34,7 @@ impl IconButton { color: IconColor::default(), variant: ButtonVariant::default(), state: InteractionState::default(), + handlers: IconButtonHandlers::default(), } } @@ -43,6 +58,14 @@ impl IconButton { 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 render(&mut self, cx: &mut ViewContext) -> impl Element { let theme = theme(cx); @@ -56,6 +79,12 @@ impl IconButton { div = div.fill(theme.highest.on.default.background); } + if let Some(click_handler) = self.handlers.click.clone() { + div = div.on_click(MouseButton::Left, move |state, event, cx| { + click_handler(state, cx); + }); + } + div.w_7() .h_6() .flex()