From 7965052757f2ce235eea72cf40d9d992f00b5527 Mon Sep 17 00:00:00 2001 From: Anthony Eid <56899983+Anthony-Eid@users.noreply.github.com> Date: Mon, 11 Aug 2025 12:33:21 -0400 Subject: [PATCH] Make SwitchField component clickable from the keyboard when focused (#35830) Release Notes: - N/A --- crates/ui/src/components/toggle.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/ui/src/components/toggle.rs b/crates/ui/src/components/toggle.rs index 59c056859de4b41b3ceb80eabe55ef9bc6414e05..e5f28e3b255b92744c3286243bc6cbadbd7375a5 100644 --- a/crates/ui/src/components/toggle.rs +++ b/crates/ui/src/components/toggle.rs @@ -420,7 +420,7 @@ pub struct Switch { id: ElementId, toggle_state: ToggleState, disabled: bool, - on_click: Option>, + on_click: Option>, label: Option, key_binding: Option, 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(