Change switch use mouse_down to handle event fix Windows switch theme support.

This commit is contained in:
Jason Lee 2024-07-11 21:52:07 +08:00
parent e1aed0a03d
commit 9e935bed56
4 changed files with 24 additions and 24 deletions

View file

@ -249,10 +249,10 @@ impl Render for StoryWorkspace {
.checked(cx.theme().mode.is_dark()) .checked(cx.theme().mode.is_dark())
.label_side(LabelSide::Left) .label_side(LabelSide::Left)
.label("Dark Mode") .label("Dark Mode")
.on_click(move |_, cx| { .on_click(move |checked, cx| {
let mode = match cx.theme().mode.is_dark() { let mode = match checked {
false => ui::theme::ThemeMode::Dark, true => ui::theme::ThemeMode::Dark,
true => ui::theme::ThemeMode::Light, false => ui::theme::ThemeMode::Light,
}; };
Theme::change(mode, cx); Theme::change(mode, cx);

View file

@ -118,8 +118,8 @@ impl Render for PopoverStory {
Switch::new("switch-window-mode") Switch::new("switch-window-mode")
.checked(self.window_mode) .checked(self.window_mode)
.label("Use Window Popover") .label("Use Window Popover")
.on_click(cx.listener(|this, _, _| { .on_click(cx.listener(|this, checked, _| {
this.window_mode = !this.window_mode; this.window_mode = *checked;
})), })),
) )
.child( .child(

View file

@ -72,8 +72,8 @@ impl Render for SwitchStory {
.checked(self.switch1) .checked(self.switch1)
.label_side(LabelSide::Left) .label_side(LabelSide::Left)
.label("Subscribe") .label("Subscribe")
.on_click(cx.listener(move |view, _, cx| { .on_click(cx.listener(move |view, checked, cx| {
view.switch1 = !view.switch1; view.switch1 = *checked;
cx.notify(); cx.notify();
})), })),
), ),
@ -88,18 +88,19 @@ impl Render for SwitchStory {
.child( .child(
Switch::new("switch2") Switch::new("switch2")
.checked(self.switch2) .checked(self.switch2)
.on_click(cx.listener(move |view, _, cx| { .on_click(cx.listener(move |view, checked, cx| {
view.switch2 = !view.switch2; view.switch2 = *checked;
cx.notify(); cx.notify();
})), })),
), ),
) )
.child( .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() h_flex().items_center()
.gap_6() .gap_6()
.child(Switch::new("switch3").disabled(true).on_click(|ev, _| { .child(Switch::new("switch3").disabled(true).on_click(|v, _| {
println!("Switch value changed: {:?}", ev); println!("Switch value changed: {:?}", v);
})) }))
.child( .child(
Switch::new("switch3_1").label("Airplane Mode") Switch::new("switch3_1").label("Airplane Mode")
@ -111,11 +112,12 @@ impl Render for SwitchStory {
)) ))
) )
.child( .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() h_flex().items_center()
.gap_6() .gap_6()
.child(Switch::new("switch3").checked(self.switch3).label("Small Size").size(ButtonSize::Small).on_click(cx.listener(move |view, _, cx| { .child(Switch::new("switch3").checked(self.switch3).label("Small Size").size(ButtonSize::Small).on_click(cx.listener(move |view, checked, cx| {
view.switch3 = !view.switch3; view.switch3 = *checked;
cx.notify(); cx.notify();
})), })),
) )

View file

@ -5,12 +5,11 @@ use crate::{
Disableable, Disableable,
}; };
use gpui::{ use gpui::{
div, prelude::FluentBuilder as _, ClickEvent, Div, InteractiveElement, IntoElement, div, prelude::FluentBuilder as _, Div, InteractiveElement, IntoElement, ParentElement as _,
ParentElement as _, RenderOnce, SharedString, Stateful, StatefulInteractiveElement, RenderOnce, SharedString, Stateful, Styled as _, WindowContext,
Styled as _, WindowContext,
}; };
type OnClick = Box<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>; type OnClick = Box<dyn Fn(&bool, &mut WindowContext) + 'static>;
pub enum LabelSide { pub enum LabelSide {
Left, Left,
@ -66,7 +65,7 @@ impl Switch {
self 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.on_click = Some(Box::new(handler));
self self
} }
@ -140,9 +139,8 @@ impl RenderOnce for Switch {
.when_some( .when_some(
self.on_click.filter(|_| !self.disabled), self.on_click.filter(|_| !self.disabled),
|this, on_click| { |this, on_click| {
this.on_click(move |ev, cx| { this.on_mouse_down(gpui::MouseButton::Left, move |_, cx| {
cx.stop_propagation(); on_click(&!self.checked, cx);
on_click(ev, cx);
}) })
}, },
) )