From 9e935bed56f7be62089ce49a88108f380cb5aee3 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 11 Jul 2024 21:52:07 +0800 Subject: [PATCH] Change switch use mouse_down to handle event fix Windows switch theme support. --- crates/app/src/story_workspace.rs | 8 ++++---- crates/story/src/popover_story.rs | 4 ++-- crates/story/src/switch_story.rs | 22 ++++++++++++---------- crates/ui/src/switch.rs | 14 ++++++-------- 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/crates/app/src/story_workspace.rs b/crates/app/src/story_workspace.rs index 5ca92c76..efabbe11 100644 --- a/crates/app/src/story_workspace.rs +++ b/crates/app/src/story_workspace.rs @@ -249,10 +249,10 @@ impl Render for StoryWorkspace { .checked(cx.theme().mode.is_dark()) .label_side(LabelSide::Left) .label("Dark Mode") - .on_click(move |_, cx| { - let mode = match cx.theme().mode.is_dark() { - false => ui::theme::ThemeMode::Dark, - true => ui::theme::ThemeMode::Light, + .on_click(move |checked, cx| { + let mode = match checked { + true => ui::theme::ThemeMode::Dark, + false => ui::theme::ThemeMode::Light, }; Theme::change(mode, cx); diff --git a/crates/story/src/popover_story.rs b/crates/story/src/popover_story.rs index ef885df5..cb0bc9d3 100644 --- a/crates/story/src/popover_story.rs +++ b/crates/story/src/popover_story.rs @@ -118,8 +118,8 @@ impl Render for PopoverStory { Switch::new("switch-window-mode") .checked(self.window_mode) .label("Use Window Popover") - .on_click(cx.listener(|this, _, _| { - this.window_mode = !this.window_mode; + .on_click(cx.listener(|this, checked, _| { + this.window_mode = *checked; })), ) .child( diff --git a/crates/story/src/switch_story.rs b/crates/story/src/switch_story.rs index dc5630d6..85d6d8bc 100644 --- a/crates/story/src/switch_story.rs +++ b/crates/story/src/switch_story.rs @@ -72,8 +72,8 @@ impl Render for SwitchStory { .checked(self.switch1) .label_side(LabelSide::Left) .label("Subscribe") - .on_click(cx.listener(move |view, _, cx| { - view.switch1 = !view.switch1; + .on_click(cx.listener(move |view, checked, cx| { + view.switch1 = *checked; cx.notify(); })), ), @@ -88,18 +88,19 @@ impl Render for SwitchStory { .child( Switch::new("switch2") .checked(self.switch2) - .on_click(cx.listener(move |view, _, cx| { - view.switch2 = !view.switch2; + .on_click(cx.listener(move |view, checked, cx| { + view.switch2 = *checked; cx.notify(); })), ), ) .child( - card(cx).v_flex() .items_start().child(title("Disabled Switchs")).child( + card(cx).v_flex() + .items_start().child(title("Disabled Switchs")).child( h_flex().items_center() .gap_6() - .child(Switch::new("switch3").disabled(true).on_click(|ev, _| { - println!("Switch value changed: {:?}", ev); + .child(Switch::new("switch3").disabled(true).on_click(|v, _| { + println!("Switch value changed: {:?}", v); })) .child( Switch::new("switch3_1").label("Airplane Mode") @@ -111,11 +112,12 @@ impl Render for SwitchStory { )) ) .child( - card(cx).v_flex() .items_start().child(title("Disabled Switchs")).child( + card(cx).v_flex() + .items_start().child(title("Disabled Switchs")).child( h_flex().items_center() .gap_6() - .child(Switch::new("switch3").checked(self.switch3).label("Small Size").size(ButtonSize::Small).on_click(cx.listener(move |view, _, cx| { - view.switch3 = !view.switch3; + .child(Switch::new("switch3").checked(self.switch3).label("Small Size").size(ButtonSize::Small).on_click(cx.listener(move |view, checked, cx| { + view.switch3 = *checked; cx.notify(); })), ) diff --git a/crates/ui/src/switch.rs b/crates/ui/src/switch.rs index be7c8a75..4c14f9b2 100644 --- a/crates/ui/src/switch.rs +++ b/crates/ui/src/switch.rs @@ -5,12 +5,11 @@ use crate::{ Disableable, }; use gpui::{ - div, prelude::FluentBuilder as _, ClickEvent, Div, InteractiveElement, IntoElement, - ParentElement as _, RenderOnce, SharedString, Stateful, StatefulInteractiveElement, - Styled as _, WindowContext, + div, prelude::FluentBuilder as _, Div, InteractiveElement, IntoElement, ParentElement as _, + RenderOnce, SharedString, Stateful, Styled as _, WindowContext, }; -type OnClick = Box; +type OnClick = Box; pub enum LabelSide { Left, @@ -66,7 +65,7 @@ impl Switch { self } - pub fn on_click(mut self, handler: impl Fn(&ClickEvent, &mut WindowContext) + 'static) -> Self { + pub fn on_click(mut self, handler: impl Fn(&bool, &mut WindowContext) + 'static) -> Self { self.on_click = Some(Box::new(handler)); self } @@ -140,9 +139,8 @@ impl RenderOnce for Switch { .when_some( self.on_click.filter(|_| !self.disabled), |this, on_click| { - this.on_click(move |ev, cx| { - cx.stop_propagation(); - on_click(ev, cx); + this.on_mouse_down(gpui::MouseButton::Left, move |_, cx| { + on_click(&!self.checked, cx); }) }, )