Add on_click to IconButton

This commit is contained in:
Marshall Bowers 2023-10-10 17:26:33 -04:00
parent 48d9b49ada
commit be843227a1

View file

@ -1,8 +1,21 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use std::sync::Arc;
use gpui3::{Interactive, MouseButton};
use crate::prelude::*; use crate::prelude::*;
use crate::{theme, Icon, IconColor, IconElement}; use crate::{theme, Icon, IconColor, IconElement};
struct IconButtonHandlers<S: 'static + Send + Sync> {
click: Option<Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>>,
}
impl<S: 'static + Send + Sync> Default for IconButtonHandlers<S> {
fn default() -> Self {
Self { click: None }
}
}
#[derive(Element)] #[derive(Element)]
pub struct IconButton<S: 'static + Send + Sync> { pub struct IconButton<S: 'static + Send + Sync> {
state_type: PhantomData<S>, state_type: PhantomData<S>,
@ -10,6 +23,7 @@ pub struct IconButton<S: 'static + Send + Sync> {
color: IconColor, color: IconColor,
variant: ButtonVariant, variant: ButtonVariant,
state: InteractionState, state: InteractionState,
handlers: IconButtonHandlers<S>,
} }
impl<S: 'static + Send + Sync> IconButton<S> { impl<S: 'static + Send + Sync> IconButton<S> {
@ -20,6 +34,7 @@ impl<S: 'static + Send + Sync> IconButton<S> {
color: IconColor::default(), color: IconColor::default(),
variant: ButtonVariant::default(), variant: ButtonVariant::default(),
state: InteractionState::default(), state: InteractionState::default(),
handlers: IconButtonHandlers::default(),
} }
} }
@ -43,6 +58,14 @@ impl<S: 'static + Send + Sync> IconButton<S> {
self self
} }
pub fn on_click(
mut self,
handler: impl Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync,
) -> Self {
self.handlers.click = Some(Arc::new(handler));
self
}
fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> { fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
let theme = theme(cx); let theme = theme(cx);
@ -56,6 +79,12 @@ impl<S: 'static + Send + Sync> IconButton<S> {
div = div.fill(theme.highest.on.default.background); 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() div.w_7()
.h_6() .h_6()
.flex() .flex()