From f6d58f76e48e6d9376870bfdf5e64c6fb95c207e Mon Sep 17 00:00:00 2001 From: Finn Evers Date: Mon, 31 Mar 2025 16:51:17 +0200 Subject: [PATCH] ui: Render keybinds for disabled actions with disabled color (#27693) This PR ensures that keybinds for disabled actions in context menus are also colored according th their disabled state. ### Default | Current `main` | This PR | | --- | --- | | main_default | pr_default | ### Vim-Mode | Current `main` | This PR | | --- | --- | | main_vim | pr_vim| Release Notes: - Keybinds in contexts menus will now also be dimmed if the corresponding action is currently disabled. --- crates/ui/src/components/context_menu.rs | 2 +- crates/ui/src/components/keybinding.rs | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crates/ui/src/components/context_menu.rs b/crates/ui/src/components/context_menu.rs index df7de5b2e8..18fea79f16 100644 --- a/crates/ui/src/components/context_menu.rs +++ b/crates/ui/src/components/context_menu.rs @@ -752,7 +752,7 @@ impl ContextMenu { KeyBinding::for_action(&**action, window, cx) }) .map(|binding| { - div().ml_4().child(binding).when( + div().ml_4().child(binding.disabled(*disabled)).when( *disabled && documentation_aside_callback.is_some(), |parent| parent.invisible(), ) diff --git a/crates/ui/src/components/keybinding.rs b/crates/ui/src/components/keybinding.rs index 63b63da45f..5f99b14298 100644 --- a/crates/ui/src/components/keybinding.rs +++ b/crates/ui/src/components/keybinding.rs @@ -20,6 +20,9 @@ pub struct KeyBinding { /// Determines whether the keybinding is meant for vim mode. vim_mode: bool, + + /// Indicates whether the keybinding is currently disabled. + disabled: bool, } struct VimStyle(bool); @@ -64,6 +67,7 @@ impl KeyBinding { platform_style: PlatformStyle::platform(), size: None, vim_mode: KeyBinding::is_vim_mode(cx), + disabled: false, } } @@ -79,6 +83,13 @@ impl KeyBinding { self } + /// Sets whether this keybinding is currently disabled. + /// Disabled keybinds will be rendered in a dimmed state. + pub fn disabled(mut self, disabled: bool) -> Self { + self.disabled = disabled; + self + } + pub fn vim_mode(mut self, enabled: bool) -> Self { self.vim_mode = enabled; self @@ -98,6 +109,7 @@ impl KeyBinding { impl RenderOnce for KeyBinding { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { + let color = self.disabled.then_some(Color::Disabled); let use_text = self.vim_mode || matches!( self.platform_style, @@ -127,7 +139,7 @@ impl RenderOnce for KeyBinding { el.child( Key::new( keystroke_text(&keystroke, self.platform_style, self.vim_mode), - None, + color, ) .size(self.size), ) @@ -136,11 +148,11 @@ impl RenderOnce for KeyBinding { el.children(render_modifiers( &keystroke.modifiers, self.platform_style, - None, + color, self.size, true, )) - .map(|el| el.child(self.render_key(&keystroke, None))) + .map(|el| el.child(self.render_key(&keystroke, color))) }) })) }