Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/gpui/src/app/visual_test_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl VisualTestAppContext {
modifiers: Modifiers::default(),
key: key.clone(),
key_char: Some(key),
code: None,
};
self.dispatch_keystroke(window, keystroke);
}
Expand Down
1 change: 1 addition & 0 deletions crates/gpui/src/platform.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod app_menu;
mod keyboard;
mod keystroke;
pub mod keystroke_code;

#[cfg(all(target_os = "linux", feature = "wayland"))]
#[expect(missing_docs)]
Expand Down
45 changes: 45 additions & 0 deletions crates/gpui/src/platform/keystroke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub struct Keystroke {
/// this binding was pressed.
/// e.g. for s this is "s", for option-s "ß", and cmd-s None
pub key_char: Option<String>,

/// The physical key that was pressed, layout-independent (`"KeyS"`, `"Digit1"`,
/// `"ArrowLeft"`), in the DOM `KeyboardEvent.code` spelling. `None` when the
/// platform cannot report one.
#[serde(default)]
pub code: Option<String>,
}

/// Represents a keystroke that can be used in keybindings and displayed to the user.
Expand Down Expand Up @@ -216,6 +222,7 @@ impl Keystroke {
modifiers,
key,
key_char,
code: None,
})
}

Expand Down Expand Up @@ -774,3 +781,41 @@ fn unparse(modifiers: &Modifiers, key: &str) -> String {
result.push_str(&key);
result
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn parse_does_not_set_the_physical_key() {
let keystroke = Keystroke::parse("ctrl-shift-s").expect("valid keystroke");
assert_eq!(keystroke.key, "s");
assert_eq!(keystroke.code, None);
assert_eq!(keystroke.unparse(), "ctrl-shift-s");
}

/// The physical key is extra metadata: a binding written without one still matches.
#[test]
fn matching_ignores_the_physical_key() {
let typed = Keystroke {
modifiers: Modifiers::control(),
key: "s".to_string(),
key_char: None,
code: Some("KeyS".to_string()),
};
let target = Keystroke::parse("ctrl-s").expect("valid keystroke");

assert!(typed.should_match(&KeybindingKeystroke::from_keystroke(target)));
}

/// Keystrokes serialized before the physical key existed still deserialize.
#[test]
fn deserializes_keystrokes_without_a_physical_key() {
let keystroke: Keystroke =
serde_json::from_str(r#"{"modifiers":{"control":true},"key":"s","key_char":null}"#)
.expect("keystroke without a physical key should deserialize");

assert!(keystroke.modifiers.control);
assert_eq!(keystroke.code, None);
}
}
Loading