kbd: Fix Kdb to support display - key. (#1109)

This commit is contained in:
Jason Lee 2025-07-31 20:39:19 +08:00 committed by GitHub
parent efa2c7c9ef
commit 6c7a28c818
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 65 additions and 67 deletions

View file

@ -49,6 +49,8 @@ impl Render for KbdStory {
.gap_2() .gap_2()
.child(Kbd::new(Keystroke::parse("cmd-shift-p").unwrap())) .child(Kbd::new(Keystroke::parse("cmd-shift-p").unwrap()))
.child(Kbd::new(Keystroke::parse("cmd-ctrl-t").unwrap())) .child(Kbd::new(Keystroke::parse("cmd-ctrl-t").unwrap()))
.child(Kbd::new(Keystroke::parse("cmd--").unwrap()))
.child(Kbd::new(Keystroke::parse("cmd-+").unwrap()))
.child(Kbd::new(Keystroke::parse("escape").unwrap())) .child(Kbd::new(Keystroke::parse("escape").unwrap()))
.child(Kbd::new(Keystroke::parse("backspace").unwrap())) .child(Kbd::new(Keystroke::parse("backspace").unwrap()))
.child(Kbd::new(Keystroke::parse("/").unwrap())) .child(Kbd::new(Keystroke::parse("/").unwrap()))

View file

@ -107,74 +107,68 @@ impl Kbd {
} }
let mut keys = String::new(); let mut keys = String::new();
let key_str = key.key.as_str();
for key in key.key.split("-") { match key_str {
if parts.len() > 0 && keys.len() > 0 { #[cfg(target_os = "macos")]
keys.push_str(DIVIDER); "ctrl" => keys.push('⌃'),
} #[cfg(not(target_os = "macos"))]
"ctrl" => keys.push_str("Ctrl"),
match key { #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "alt" => keys.push('⌥'),
"ctrl" => keys.push('⌃'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "alt" => keys.push_str("Alt"),
"ctrl" => keys.push_str("Ctrl"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "shift" => keys.push('⇧'),
"alt" => keys.push('⌥'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "shift" => keys.push_str("Shift"),
"alt" => keys.push_str("Alt"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "cmd" => keys.push('⌘'),
"shift" => keys.push('⇧'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "cmd" => keys.push_str("Win"),
"shift" => keys.push_str("Shift"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "space" => keys.push_str("Space"),
"cmd" => keys.push('⌘'), #[cfg(target_os = "macos")]
#[cfg(not(target_os = "macos"))] "backspace" => keys.push('⌫'),
"cmd" => keys.push_str("Win"), #[cfg(not(target_os = "macos"))]
#[cfg(target_os = "macos")] "backspace" => keys.push_str("Backspace"),
"space" => keys.push_str("Space"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "delete" => keys.push('⌫'),
"backspace" => keys.push('⌫'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "delete" => keys.push_str("Delete"),
"backspace" => keys.push_str("Backspace"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "escape" => keys.push('⎋'),
"delete" => keys.push('⌫'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "escape" => keys.push_str("Esc"),
"delete" => keys.push_str("Delete"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "enter" => keys.push('⏎'),
"escape" => keys.push('⎋'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "enter" => keys.push_str("Enter"),
"escape" => keys.push_str("Esc"), "pagedown" => keys.push_str("Page Down"),
#[cfg(target_os = "macos")] "pageup" => keys.push_str("Page Up"),
"enter" => keys.push('⏎'), #[cfg(target_os = "macos")]
#[cfg(not(target_os = "macos"))] "left" => keys.push('←'),
"enter" => keys.push_str("Enter"), #[cfg(not(target_os = "macos"))]
"pagedown" => keys.push_str("Page Down"), "left" => keys.push_str("Left"),
"pageup" => keys.push_str("Page Up"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "right" => keys.push('→'),
"left" => keys.push('←'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "right" => keys.push_str("Right"),
"left" => keys.push_str("Left"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "up" => keys.push('↑'),
"right" => keys.push('→'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "up" => keys.push_str("Up"),
"right" => keys.push_str("Right"), #[cfg(target_os = "macos")]
#[cfg(target_os = "macos")] "down" => keys.push('↓'),
"up" => keys.push('↑'), #[cfg(not(target_os = "macos"))]
#[cfg(not(target_os = "macos"))] "down" => keys.push_str("Down"),
"up" => keys.push_str("Up"), _ => {
#[cfg(target_os = "macos")] if key_str.len() == 1 {
"down" => keys.push('↓'), keys.push_str(&key_str.to_uppercase());
#[cfg(not(target_os = "macos"))] } else {
"down" => keys.push_str("Down"), if let Some(first_char) = key_str.chars().next() {
_ => { keys.push_str(&format!("{}{}", first_char.to_uppercase(), &key_str[1..]));
if key.len() == 1 {
keys.push_str(&key.to_uppercase());
} else { } else {
if let Some(first_char) = key.chars().next() { keys.push_str(&key_str);
keys.push_str(&format!("{}{}", first_char.to_uppercase(), &key[1..]));
} else {
keys.push_str(&key);
}
} }
} }
} }
@ -224,6 +218,8 @@ mod tests {
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
assert_eq!(Kbd::format(&Keystroke::parse("cmd-a").unwrap()), "⌘A"); assert_eq!(Kbd::format(&Keystroke::parse("cmd-a").unwrap()), "⌘A");
assert_eq!(Kbd::format(&Keystroke::parse("cmd--").unwrap()), "⌘-");
assert_eq!(Kbd::format(&Keystroke::parse("cmd-+").unwrap()), "⌘+");
assert_eq!(Kbd::format(&Keystroke::parse("cmd-enter").unwrap()), "⌘⏎"); assert_eq!(Kbd::format(&Keystroke::parse("cmd-enter").unwrap()), "⌘⏎");
assert_eq!( assert_eq!(
Kbd::format(&Keystroke::parse("secondary-f12").unwrap()), Kbd::format(&Keystroke::parse("secondary-f12").unwrap()),