Send macOS Option as Meta and restore arrow shortcuts

This commit is contained in:
James Magahern
2026-11-17 16:10:18 -08:00
parent 57ec12d238
commit cb842c66cb
4 changed files with 120 additions and 37 deletions
+52 -27
View File
@@ -67,29 +67,24 @@ pub fn action_for_key(key: &Keystroke) -> Option<CommandAction> {
if !modifiers.alt || modifiers.control || modifiers.platform {
return None;
}
if modifiers.shift {
// Wayland keyboard layouts can report the shifted symbol for brackets.
Some(match name.as_str() {
"[" | "{" => CommandAction::PreviousTab,
"]" | "}" => CommandAction::NextTab,
"n" | "right" => CommandAction::NewTab,
"up" => CommandAction::StackAbove,
"down" => CommandAction::StackBelow,
_ => return None,
})
} else {
Some(match name.as_str() {
// GPUI consumes Shift for punctuation, leaving Alt+{ / Alt+}.
"{" => CommandAction::PreviousTab,
"}" => CommandAction::NextTab,
"left" => CommandAction::FocusLeft,
"right" => CommandAction::FocusRight,
"up" => CommandAction::FocusAbove,
"down" => CommandAction::FocusBelow,
"\\" => CommandAction::SplitPane,
_ => return None,
})
}
Some(match (modifiers.shift, name.as_str()) {
(false, "left") => CommandAction::FocusLeft,
(false, "right") => CommandAction::FocusRight,
(false, "up") => CommandAction::FocusAbove,
(false, "down") => CommandAction::FocusBelow,
(true, "right") => CommandAction::NewTab,
#[cfg(target_os = "macos")]
(true, "left") => CommandAction::NewTab,
(true, "up") => CommandAction::StackAbove,
(true, "down") => CommandAction::StackBelow,
_ if cfg!(target_os = "macos") => return None,
// Linux Zellij aliases. GPUI can fold Shift into a brace.
(true, "[") | (_, "{") => CommandAction::PreviousTab,
(true, "]") | (_, "}") => CommandAction::NextTab,
(true, "n") => CommandAction::NewTab,
(false, "\\") => CommandAction::SplitPane,
_ => return None,
})
}
#[cfg(test)]
@@ -101,6 +96,7 @@ mod tests {
}
#[test]
#[cfg(not(target_os = "macos"))]
fn zellij_tab_bindings_accept_shifted_and_unshifted_brackets() {
for key in ["alt-shift-[", "alt-shift-{", "alt-{"] {
assert!(matches!(action(key), Some(CommandAction::PreviousTab)));
@@ -108,19 +104,47 @@ mod tests {
for key in ["alt-shift-]", "alt-shift-}", "alt-}"] {
assert!(matches!(action(key), Some(CommandAction::NextTab)));
}
for key in ["alt-shift-n", "alt-shift-right"] {
assert!(matches!(action(key), Some(CommandAction::NewTab)));
}
assert!(matches!(action("alt-shift-n"), Some(CommandAction::NewTab)));
assert!(matches!(action("alt-\\"), Some(CommandAction::SplitPane)));
}
#[test]
#[cfg(not(target_os = "macos"))]
fn zellij_horizontal_focus_and_existing_shortcuts_coexist() {
assert!(matches!(action("alt-left"), Some(CommandAction::FocusLeft)));
assert!(matches!(
action("alt-right"),
Some(CommandAction::FocusRight)
));
assert!(matches!(action("alt-\\"), Some(CommandAction::SplitPane)));
assert!(matches!(
action("alt-shift-right"),
Some(CommandAction::NewTab)
));
}
#[test]
#[cfg(target_os = "macos")]
fn mac_option_arrows_navigate_and_editing_chords_reach_the_terminal() {
for key in [
"alt-backspace",
"alt-shift-n",
"alt-shift-[",
"alt-{",
"alt-shift-]",
"alt-}",
"alt-\\",
] {
assert!(action(key).is_none(), "{key}");
}
for key in ["alt-left", "cmd-alt-left"] {
assert!(matches!(action(key), Some(CommandAction::FocusLeft)));
}
for key in ["alt-right", "cmd-alt-right"] {
assert!(matches!(action(key), Some(CommandAction::FocusRight)));
}
for key in ["alt-shift-left", "alt-shift-right"] {
assert!(matches!(action(key), Some(CommandAction::NewTab)), "{key}");
}
}
#[test]
@@ -229,6 +253,7 @@ mod tests {
"alt-h",
"alt-ctrl-left",
"left",
"shift-left",
"shift-right",
] {
assert!(action(key).is_none(), "{key} must reach the terminal");
+2 -2
View File
@@ -204,7 +204,7 @@ pub fn root_items(snapshot: Option<&Snapshot>) -> Vec<PaletteItem> {
"focus-left",
"Focus pane or tab left",
"navigate previous horizontal",
shortcut("Cmd+Option+Left", "Alt+Left"),
shortcut("Option+Left", "Alt+Left"),
FocusLeft,
)
.available(pane),
@@ -212,7 +212,7 @@ pub fn root_items(snapshot: Option<&Snapshot>) -> Vec<PaletteItem> {
"focus-right",
"Focus pane or tab right",
"navigate next horizontal",
shortcut("Cmd+Option+Right", "Alt+Right"),
shortcut("Option+Right", "Alt+Right"),
FocusRight,
)
.available(pane),
+56
View File
@@ -115,6 +115,17 @@ pub fn encode_key(key: &Keystroke, mode: TermMode) -> Option<Vec<u8>> {
}
"tab" => vec![b'\t'],
_ if modifiers.control => vec![control_byte(&key.key)?],
// AppKit's key_char includes Option's symbol or accent. GPUI's
// logical key keeps the underlying character and folds in shifted
// punctuation, but leaves ASCII letters lowercase with Shift set.
#[cfg(target_os = "macos")]
name if modifiers.alt && !modifiers.function && name.chars().count() == 1 => {
let mut bytes = name.as_bytes().to_vec();
if modifiers.shift {
bytes.make_ascii_uppercase();
}
bytes
}
// Do not fall back to `key`: a missing key_char can mean that a dead
// key or compose sequence is still waiting for its next character.
_ => key.key_char.as_ref()?.as_bytes().to_vec(),
@@ -185,7 +196,11 @@ mod tests {
("ctrl-6", b"\x1e"),
("ctrl-/", b"\x1f"),
("alt-ctrl-c", b"\x1b\x03"),
("alt-b->b", b"\x1bb"),
("alt-f->f", b"\x1bf"),
("alt-space-> ", b"\x1b "),
("backspace", b"\x7f"),
("alt-backspace", b"\x1b\x7f"),
("ctrl-backspace", b"\x08"),
("enter", b"\r"),
("shift-tab", b"\x1b[Z"),
@@ -195,6 +210,22 @@ mod tests {
assert_eq!(encode("enter", TermMode::LINE_FEED_NEW_LINE), b"\r\n");
}
#[cfg(target_os = "macos")]
#[test]
fn option_sends_meta_for_letters_symbols_and_dead_keys() {
for (key, expected) in [
("alt-b->∫", b"\x1bb".as_slice()),
("alt-f->ƒ", b"\x1bf"),
("alt-shift-b->ı", b"\x1bB"),
("alt-/->÷", b"\x1b/"),
("alt-?->¿", b"\x1b?"),
("alt-e->´", b"\x1be"),
("alt-e", b"\x1be"),
] {
assert_eq!(encode(key, TermMode::empty()), expected, "{key}");
}
}
#[test]
fn application_cursor_and_modified_navigation() {
assert_eq!(encode("up", TermMode::empty()), b"\x1b[A");
@@ -206,6 +237,31 @@ mod tests {
assert_eq!(encode("pagedown", TermMode::empty()), b"\x1b[6~");
}
#[test]
fn modified_arrows_preserve_direction_and_modifiers() {
for (mods, parameter) in [
("shift", 2),
("alt", 3),
("alt-shift", 4),
("ctrl", 5),
("ctrl-shift", 6),
("ctrl-alt", 7),
("ctrl-alt-shift", 8),
] {
for (key, end) in [("up", 'A'), ("down", 'B'), ("right", 'C'), ("left", 'D')] {
let binding = format!("{mods}-{key}");
let csi = format!("\x1b[1;{parameter}{end}");
for mode in [
TermMode::empty(),
TermMode::APP_CURSOR,
TermMode::ALT_SCREEN,
] {
assert_eq!(encode(&binding, mode), csi.as_bytes(), "{binding} {mode:?}");
}
}
}
}
#[test]
fn function_keys_use_xterm_sequences() {
assert_eq!(encode("f1", TermMode::empty()), b"\x1bOP");