Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Fix key equivalents for 'backspace' and 'delete' on macOS (#65744)

dontwanttothinkd01f1258376e
diff --git a/crates/gpui_macos/src/events.rs b/crates/gpui_macos/src/events.rs
index 6ca05849a9b..89ae1117b3a 100644
--- a/crates/gpui_macos/src/events.rs
+++ b/crates/gpui_macos/src/events.rs
@@ -36,10 +36,17 @@ const NUMPAD_ENTER_KEY: u32 = 0x03;
const TAB_KEY: u32 = 0x09;
const SHIFT_TAB_KEY: u32 = 0x19;
-pub fn key_to_native(key: &str) -> Cow<'_, str> {
+// Menu key equivalents use different character values than the ones NSEvent
+// reports for the same physical keys.
+//
+// See https://developer.apple.com/documentation/appkit/nsmenuitem/keyequivalent#Discussion
+const BACKSPACE_KEY_EQUIVALENT: u32 = 0x08;
+const DELETE_KEY_EQUIVALENT: u32 = 0x7f;
+
+pub(crate) fn key_to_menu_equivalent(key: &str) -> Cow<'_, str> {
let code = match key {
"space" => SPACE_KEY,
- "backspace" => BACKSPACE_KEY,
+ "backspace" => BACKSPACE_KEY_EQUIVALENT,
"escape" => u32::from(ESCAPE_KEY),
"up" => NSUpArrowFunctionKey,
"down" => NSDownArrowFunctionKey,
@@ -49,7 +56,7 @@ pub fn key_to_native(key: &str) -> Cow<'_, str> {
"pagedown" => NSPageDownFunctionKey,
"home" => NSHomeFunctionKey,
"end" => NSEndFunctionKey,
- "delete" => NSDeleteFunctionKey,
+ "delete" => DELETE_KEY_EQUIVALENT,
"insert" => NSHelpFunctionKey,
"f1" => NSF1FunctionKey,
"f2" => NSF2FunctionKey,
diff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rs
index aa0bc2d9ac2..f22510f8ebd 100644
--- a/crates/gpui_macos/src/platform.rs
+++ b/crates/gpui_macos/src/platform.rs
@@ -1,7 +1,7 @@
use crate::{
AppleActivity, AppleDispatcher, BoolExt, MacDisplay, MacKeyboardLayout, MacKeyboardMapper,
- MacWindow, events::key_to_native, main_thread_marker, ns_string, pasteboard::Pasteboard,
- renderer, set_active_window_cursor_style,
+ MacWindow, events::key_to_menu_equivalent, main_thread_marker, ns_string,
+ pasteboard::Pasteboard, renderer, set_active_window_cursor_style,
};
use anyhow::{Context as _, anyhow};
use block2::RcBlock;
@@ -401,7 +401,9 @@ impl MacPlatform {
NSMenuItem::alloc(self.1),
&NSString::from_str(name),
Some(selector),
- &NSString::from_str(key_to_native(keystroke.key()).as_ref()),
+ &NSString::from_str(
+ key_to_menu_equivalent(keystroke.key()).as_ref(),
+ ),
);
if Self::os_version() >= Version::new(12, 0, 0) {
item.setAllowsAutomaticKeyEquivalentLocalization(false);