Thread click handler through from workspace to language selector

This commit is contained in:
Marshall Bowers 2023-10-10 18:02:08 -04:00
parent c142676b20
commit 95ef61bc45
4 changed files with 46 additions and 18 deletions

View file

@ -20,8 +20,10 @@ pub enum ButtonVariant {
Filled,
}
pub type ClickHandler<S> = Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>;
struct ButtonHandlers<S: 'static + Send + Sync> {
click: Option<Arc<dyn Fn(&mut S, &mut ViewContext<S>) + 'static + Send + Sync>>,
click: Option<ClickHandler<S>>,
}
impl<S: 'static + Send + Sync> Default for ButtonHandlers<S> {
@ -94,11 +96,8 @@ impl<S: 'static + Send + Sync + Clone> Button<S> {
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));
pub fn on_click(mut self, handler: ClickHandler<S>) -> Self {
self.handlers.click = Some(handler);
self
}
@ -403,7 +402,7 @@ mod stories {
.child(
Button::new("Label")
.variant(ButtonVariant::Ghost)
.on_click(|_view, _cx| println!("Button clicked.")),
.on_click(Arc::new(|_view, _cx| println!("Button clicked."))),
)
}
}