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())
.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);

View file

@ -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(

View file

@ -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();
})),
)

View file

@ -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<dyn Fn(&ClickEvent, &mut WindowContext) + 'static>;
type OnClick = Box<dyn Fn(&bool, &mut WindowContext) + 'static>;
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);
})
},
)