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()
.child(Kbd::new(Keystroke::parse("cmd-shift-p").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("backspace").unwrap()))
.child(Kbd::new(Keystroke::parse("/").unwrap()))

View file

@ -107,13 +107,8 @@ impl Kbd {
}
let mut keys = String::new();
for key in key.key.split("-") {
if parts.len() > 0 && keys.len() > 0 {
keys.push_str(DIVIDER);
}
match key {
let key_str = key.key.as_str();
match key_str {
#[cfg(target_os = "macos")]
"ctrl" => keys.push('⌃'),
#[cfg(not(target_os = "macos"))]
@ -167,14 +162,13 @@ impl Kbd {
#[cfg(not(target_os = "macos"))]
"down" => keys.push_str("Down"),
_ => {
if key.len() == 1 {
keys.push_str(&key.to_uppercase());
if key_str.len() == 1 {
keys.push_str(&key_str.to_uppercase());
} else {
if let Some(first_char) = key.chars().next() {
keys.push_str(&format!("{}{}", first_char.to_uppercase(), &key[1..]));
if let Some(first_char) = key_str.chars().next() {
keys.push_str(&format!("{}{}", first_char.to_uppercase(), &key_str[1..]));
} else {
keys.push_str(&key);
}
keys.push_str(&key_str);
}
}
}
@ -224,6 +218,8 @@ mod tests {
if cfg!(target_os = "macos") {
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("secondary-f12").unwrap()),