Make SwitchField component clickable from the keyboard when focused (#35830)

Release Notes:

- N/A
This commit is contained in:
Anthony Eid 2025-08-11 12:33:21 -04:00 committed by GitHub
parent 62270b33c2
commit 7965052757
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -420,7 +420,7 @@ pub struct Switch {
id: ElementId,
toggle_state: ToggleState,
disabled: bool,
on_click: Option<Box<dyn Fn(&ToggleState, &mut Window, &mut App) + 'static>>,
on_click: Option<Rc<dyn Fn(&ToggleState, &mut Window, &mut App) + 'static>>,
label: Option<SharedString>,
key_binding: Option<KeyBinding>,
color: SwitchColor,
@ -459,7 +459,7 @@ impl Switch {
mut self,
handler: impl Fn(&ToggleState, &mut Window, &mut App) + 'static,
) -> Self {
self.on_click = Some(Box::new(handler));
self.on_click = Some(Rc::new(handler));
self
}
@ -513,10 +513,16 @@ impl RenderOnce for Switch {
.when_some(
self.tab_index.filter(|_| !self.disabled),
|this, tab_index| {
this.tab_index(tab_index).focus(|mut style| {
style.border_color = Some(cx.theme().colors().border_focused);
style
})
this.tab_index(tab_index)
.focus(|mut style| {
style.border_color = Some(cx.theme().colors().border_focused);
style
})
.when_some(self.on_click.clone(), |this, on_click| {
this.on_click(move |_, window, cx| {
on_click(&self.toggle_state.inverse(), window, cx)
})
})
},
)
.child(