From 7de1c6981e22317f00fea629ae32c44b5ef317c6 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Sun, 29 Sep 2024 23:07:45 +0800 Subject: [PATCH] windows: Fix keybinding display on Windows. (#289) image --- crates/story/src/popup_story.rs | 12 ++++++ crates/ui/src/popup_menu.rs | 65 ++++++++++++++++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/crates/story/src/popup_story.rs b/crates/story/src/popup_story.rs index da4e0a9e..2f90d84c 100644 --- a/crates/story/src/popup_story.rs +++ b/crates/story/src/popup_story.rs @@ -28,10 +28,22 @@ impl_actions!(popover_story, [Info]); pub fn init(cx: &mut AppContext) { cx.bind_keys([ + #[cfg(target_os = "macos")] KeyBinding::new("cmd-c", Copy, None), + #[cfg(not(target_os = "macos"))] + KeyBinding::new("ctrl-c", Copy, None), + #[cfg(target_os = "macos")] KeyBinding::new("cmd-v", Paste, None), + #[cfg(not(target_os = "macos"))] + KeyBinding::new("ctrl-v", Paste, None), + #[cfg(target_os = "macos")] KeyBinding::new("cmd-x", Cut, None), + #[cfg(not(target_os = "macos"))] + KeyBinding::new("ctrl-x", Cut, None), + #[cfg(target_os = "macos")] KeyBinding::new("cmd-shift-f", SearchAll, None), + #[cfg(not(target_os = "macos"))] + KeyBinding::new("ctrl-shift-f", SearchAll, None), ]) } diff --git a/crates/ui/src/popup_menu.rs b/crates/ui/src/popup_menu.rs index 8f0a858e..ffe5864b 100644 --- a/crates/ui/src/popup_menu.rs +++ b/crates/ui/src/popup_menu.rs @@ -6,7 +6,7 @@ use gpui::{ FocusHandle, InteractiveElement, IntoElement, KeyBinding, ParentElement, Pixels, Render, SharedString, Styled as _, View, ViewContext, VisualContext as _, WindowContext, }; -use gpui::{anchored, canvas, rems, AnchorCorner, Bounds, FocusableView, WeakView}; +use gpui::{anchored, canvas, rems, AnchorCorner, Bounds, FocusableView, Keystroke, WeakView}; use crate::StyledExt; use crate::{ @@ -372,7 +372,7 @@ impl PopupMenu { keybinding .keystrokes() .into_iter() - .map(|keystroke| format!("{}", keystroke)), + .map(|key| key_shortcut(key.clone())), ); return Some(el); @@ -572,3 +572,64 @@ impl Render for PopupMenu { ) } } + +/// Return the Platform specific keybinding string by KeyStroke +pub fn key_shortcut(key: Keystroke) -> String { + if cfg!(target_os = "macos") { + return format!("{}", key); + } + + let mut parts = vec![]; + if key.modifiers.control { + parts.push("Ctrl"); + } + if key.modifiers.alt { + parts.push("Alt"); + } + if key.modifiers.platform { + parts.push("Win"); + } + if key.modifiers.shift { + parts.push("Shift"); + } + + // Capitalize the first letter + let key = if let Some(first_c) = key.key.chars().next() { + format!("{}{}", first_c.to_uppercase(), &key.key[1..]) + } else { + key.key.to_string() + }; + + parts.push(&key); + parts.join("+") +} + +#[cfg(test)] +mod tests { + #[test] + fn test_key_shortcut() { + use super::key_shortcut; + use gpui::Keystroke; + + if cfg!(target_os = "windows") { + assert_eq!(key_shortcut(Keystroke::parse("a").unwrap()), "A"); + assert_eq!(key_shortcut(Keystroke::parse("ctrl-a").unwrap()), "Ctrl+A"); + assert_eq!( + key_shortcut(Keystroke::parse("ctrl-alt-a").unwrap()), + "Ctrl+Alt+A" + ); + assert_eq!( + key_shortcut(Keystroke::parse("ctrl-alt-shift-a").unwrap()), + "Ctrl+Alt+Shift+A" + ); + assert_eq!( + key_shortcut(Keystroke::parse("ctrl-alt-shift-win-a").unwrap()), + "Ctrl+Alt+Win+Shift+A" + ); + assert_eq!( + key_shortcut(Keystroke::parse("ctrl-shift-backspace").unwrap()), + "Ctrl+Shift+Backspace" + ); + } + } +}