windows: Fix keybinding display on Windows. (#289)
<img width="191" alt="image" src="https://github.com/user-attachments/assets/7cc4d80f-5759-4432-9a13-6fd027af4b27">
This commit is contained in:
parent
e43cc48465
commit
7de1c6981e
2 changed files with 75 additions and 2 deletions
|
|
@ -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),
|
||||
])
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue