Convert function keys to the correct macOS codes for menu items

Max Brunsfeld created

Change summary

crates/gpui/src/platform/mac/event.rs    | 30 +++++++++++++++++++++++++
crates/gpui/src/platform/mac/platform.rs |  4 +-
2 files changed, 31 insertions(+), 3 deletions(-)

Detailed changes

crates/gpui/src/platform/mac/event.rs 🔗

@@ -8,7 +8,35 @@ use cocoa::{
     base::{id, nil, YES},
     foundation::NSString as _,
 };
-use std::{ffi::CStr, os::raw::c_char};
+use std::{borrow::Cow, ffi::CStr, os::raw::c_char};
+
+pub fn key_to_native(key: &str) -> Cow<str> {
+    use cocoa::appkit::*;
+    let code = match key {
+        "backspace" => 0x7F,
+        "up" => NSUpArrowFunctionKey,
+        "down" => NSDownArrowFunctionKey,
+        "left" => NSLeftArrowFunctionKey,
+        "right" => NSRightArrowFunctionKey,
+        "pageup" => NSPageUpFunctionKey,
+        "pagedown" => NSPageDownFunctionKey,
+        "delete" => NSDeleteFunctionKey,
+        "f1" => NSF1FunctionKey,
+        "f2" => NSF2FunctionKey,
+        "f3" => NSF3FunctionKey,
+        "f4" => NSF4FunctionKey,
+        "f5" => NSF5FunctionKey,
+        "f6" => NSF6FunctionKey,
+        "f7" => NSF7FunctionKey,
+        "f8" => NSF8FunctionKey,
+        "f9" => NSF9FunctionKey,
+        "f10" => NSF10FunctionKey,
+        "f11" => NSF11FunctionKey,
+        "f12" => NSF12FunctionKey,
+        _ => return Cow::Borrowed(key),
+    };
+    Cow::Owned(String::from_utf16(&[code]).unwrap())
+}
 
 impl Event {
     pub unsafe fn from_native(native_event: id, window_height: Option<f32>) -> Option<Self> {

crates/gpui/src/platform/mac/platform.rs 🔗

@@ -1,4 +1,4 @@
-use super::{BoolExt as _, Dispatcher, FontSystem, Window};
+use super::{event::key_to_native, BoolExt as _, Dispatcher, FontSystem, Window};
 use crate::{
     executor, keymap,
     platform::{self, CursorStyle},
@@ -165,7 +165,7 @@ impl MacForegroundPlatform {
                                 .initWithTitle_action_keyEquivalent_(
                                     ns_string(name),
                                     selector("handleGPUIMenuItem:"),
-                                    ns_string(&keystroke.key),
+                                    ns_string(key_to_native(&keystroke.key).as_ref()),
                                 )
                                 .autorelease();
                             item.setKeyEquivalentModifierMask_(mask);