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_macos: Migrate keyboard and event bindings to objc2 (#65715)

Kirill Bulatov0a9eb81e6ced
diff --git a/Cargo.toml b/Cargo.toml
index 6db245e52cf..1b224fec377 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -744,6 +744,7 @@ objc2-app-kit = { version = "0.3.2", default-features = false, features = [
"NSButton",
"NSControl",
"NSDraggingItem",
+ "NSEvent",
"NSGraphics",
"NSImage",
"NSOpenPanel",
diff --git a/crates/gpui_macos/src/events.rs b/crates/gpui_macos/src/events.rs
index c15f7e4bb88..6ca05849a9b 100644
--- a/crates/gpui_macos/src/events.rs
+++ b/crates/gpui_macos/src/events.rs
@@ -9,29 +9,38 @@ use crate::{
LMGetKbdType, NSStringExt, TISCopyCurrentKeyboardLayoutInputSource, TISGetInputSourceProperty,
UCKeyTranslate, kTISPropertyUnicodeKeyLayoutData,
};
-use cocoa::{
- appkit::{NSEvent, NSEventModifierFlags, NSEventPhase, NSEventType},
- base::{YES, id},
-};
use core_foundation::data::{CFDataGetBytePtr, CFDataRef};
use core_graphics::event::CGKeyCode;
-use objc::{msg_send, sel, sel_impl};
+use objc2::{msg_send, runtime::AnyObject};
+use objc2_app_kit::{
+ NSDeleteFunctionKey, NSDownArrowFunctionKey, NSEndFunctionKey, NSEvent, NSEventModifierFlags,
+ NSEventPhase, NSEventType, NSF1FunctionKey, NSF2FunctionKey, NSF3FunctionKey, NSF4FunctionKey,
+ NSF5FunctionKey, NSF6FunctionKey, NSF7FunctionKey, NSF8FunctionKey, NSF9FunctionKey,
+ NSF10FunctionKey, NSF11FunctionKey, NSF12FunctionKey, NSF13FunctionKey, NSF14FunctionKey,
+ NSF15FunctionKey, NSF16FunctionKey, NSF17FunctionKey, NSF18FunctionKey, NSF19FunctionKey,
+ NSF20FunctionKey, NSF21FunctionKey, NSF22FunctionKey, NSF23FunctionKey, NSF24FunctionKey,
+ NSF25FunctionKey, NSF26FunctionKey, NSF27FunctionKey, NSF28FunctionKey, NSF29FunctionKey,
+ NSF30FunctionKey, NSF31FunctionKey, NSF32FunctionKey, NSF33FunctionKey, NSF34FunctionKey,
+ NSF35FunctionKey, NSHelpFunctionKey, NSHomeFunctionKey, NSLeftArrowFunctionKey,
+ NSModeSwitchFunctionKey, NSPageDownFunctionKey, NSPageUpFunctionKey, NSRightArrowFunctionKey,
+ NSUpArrowFunctionKey,
+};
use std::{borrow::Cow, ffi::c_void};
-const BACKSPACE_KEY: u16 = 0x7f;
-const SPACE_KEY: u16 = b' ' as u16;
-const ENTER_KEY: u16 = 0x0d;
-const NUMPAD_ENTER_KEY: u16 = 0x03;
pub(crate) const ESCAPE_KEY: u16 = 0x1b;
-const TAB_KEY: u16 = 0x09;
-const SHIFT_TAB_KEY: u16 = 0x19;
+
+const BACKSPACE_KEY: u32 = 0x7f;
+const SPACE_KEY: u32 = b' ' as u32;
+const ENTER_KEY: u32 = 0x0d;
+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> {
- use cocoa::appkit::*;
let code = match key {
"space" => SPACE_KEY,
"backspace" => BACKSPACE_KEY,
- "escape" => ESCAPE_KEY,
+ "escape" => u32::from(ESCAPE_KEY),
"up" => NSUpArrowFunctionKey,
"down" => NSDownArrowFunctionKey,
"left" => NSLeftArrowFunctionKey,
@@ -79,38 +88,37 @@ pub fn key_to_native(key: &str) -> Cow<'_, str> {
"f35" => NSF35FunctionKey,
_ => return Cow::Borrowed(key),
};
- Cow::Owned(String::from_utf16(&[code]).unwrap())
+ Cow::Owned(String::from_utf16(&[code as u16]).unwrap())
}
-unsafe fn read_modifiers(native_event: id) -> Modifiers {
- unsafe {
- let modifiers = native_event.modifierFlags();
- let control = modifiers.contains(NSEventModifierFlags::NSControlKeyMask);
- let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask);
- let shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask);
- let command = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask);
- let function = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask);
-
- Modifiers {
- control,
- alt,
- shift,
- platform: command,
- function,
- }
+fn read_modifiers(native_event: &NSEvent) -> Modifiers {
+ let modifiers = native_event.modifierFlags();
+ let control = modifiers.contains(NSEventModifierFlags::Control);
+ let alt = modifiers.contains(NSEventModifierFlags::Option);
+ let shift = modifiers.contains(NSEventModifierFlags::Shift);
+ let command = modifiers.contains(NSEventModifierFlags::Command);
+ let function = modifiers.contains(NSEventModifierFlags::Function);
+
+ Modifiers {
+ control,
+ alt,
+ shift,
+ platform: command,
+ function,
}
}
pub(crate) unsafe fn platform_input_from_native(
- native_event: id,
+ native_event: *mut AnyObject,
window_height: Option<Pixels>,
) -> Option<PlatformInput> {
unsafe {
- let event_type = native_event.eventType();
+ let native_event = native_event.cast::<NSEvent>().as_ref()?;
+ let event_type = native_event.r#type();
// Filter out event types that aren't in the NSEventType enum.
// See https://github.com/servo/cocoa-rs/issues/155#issuecomment-323482792 for details.
- match event_type as u64 {
+ match event_type.0 {
0 | 21 | 32 | 33 | 35 | 36 | 37 => {
return None;
}
@@ -118,27 +126,27 @@ pub(crate) unsafe fn platform_input_from_native(
}
match event_type {
- NSEventType::NSFlagsChanged => {
+ NSEventType::FlagsChanged => {
Some(PlatformInput::ModifiersChanged(ModifiersChangedEvent {
modifiers: read_modifiers(native_event),
capslock: Capslock {
on: native_event
.modifierFlags()
- .contains(NSEventModifierFlags::NSAlphaShiftKeyMask),
+ .contains(NSEventModifierFlags::CapsLock),
},
}))
}
- NSEventType::NSKeyDown => Some(PlatformInput::KeyDown(KeyDownEvent {
+ NSEventType::KeyDown => Some(PlatformInput::KeyDown(KeyDownEvent {
keystroke: parse_keystroke(native_event),
- is_held: native_event.isARepeat() == YES,
+ is_held: native_event.isARepeat(),
prefer_character_input: false,
})),
- NSEventType::NSKeyUp => Some(PlatformInput::KeyUp(KeyUpEvent {
+ NSEventType::KeyUp => Some(PlatformInput::KeyUp(KeyUpEvent {
keystroke: parse_keystroke(native_event),
})),
- NSEventType::NSLeftMouseDown
- | NSEventType::NSRightMouseDown
- | NSEventType::NSOtherMouseDown => {
+ NSEventType::LeftMouseDown
+ | NSEventType::RightMouseDown
+ | NSEventType::OtherMouseDown => {
let button = match native_event.buttonNumber() {
0 => MouseButton::Left,
1 => MouseButton::Right,
@@ -162,9 +170,7 @@ pub(crate) unsafe fn platform_input_from_native(
})
})
}
- NSEventType::NSLeftMouseUp
- | NSEventType::NSRightMouseUp
- | NSEventType::NSOtherMouseUp => {
+ NSEventType::LeftMouseUp | NSEventType::RightMouseUp | NSEventType::OtherMouseUp => {
let button = match native_event.buttonNumber() {
0 => MouseButton::Left,
1 => MouseButton::Right,
@@ -187,7 +193,7 @@ pub(crate) unsafe fn platform_input_from_native(
})
})
}
- NSEventType::NSEventTypePressure => {
+ NSEventType::Pressure => {
let stage = native_event.stage();
let pressure = native_event.pressure();
@@ -208,9 +214,9 @@ pub(crate) unsafe fn platform_input_from_native(
})
}
// Some mice (like Logitech MX Master) send navigation buttons as swipe events
- NSEventType::NSEventTypeSwipe => {
+ NSEventType::Swipe => {
let navigation_direction = match native_event.phase() {
- NSEventPhase::NSEventPhaseEnded => match native_event.deltaX() {
+ NSEventPhase::Ended => match native_event.deltaX() {
x if x > 0.0 => Some(NavigationDirection::Back),
x if x < 0.0 => Some(NavigationDirection::Forward),
_ => return None,
@@ -234,12 +240,10 @@ pub(crate) unsafe fn platform_input_from_native(
_ => None,
}
}
- NSEventType::NSEventTypeMagnify => window_height.map(|window_height| {
+ NSEventType::Magnify => window_height.map(|window_height| {
let phase = match native_event.phase() {
- NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => {
- TouchPhase::Started
- }
- NSEventPhase::NSEventPhaseEnded => TouchPhase::Ended,
+ NSEventPhase::MayBegin | NSEventPhase::Began => TouchPhase::Started,
+ NSEventPhase::Ended => TouchPhase::Ended,
_ => TouchPhase::Moved,
};
@@ -255,12 +259,10 @@ pub(crate) unsafe fn platform_input_from_native(
phase,
})
}),
- NSEventType::NSScrollWheel => window_height.map(|window_height| {
+ NSEventType::ScrollWheel => window_height.map(|window_height| {
let phase = match native_event.phase() {
- NSEventPhase::NSEventPhaseMayBegin | NSEventPhase::NSEventPhaseBegan => {
- TouchPhase::Started
- }
- NSEventPhase::NSEventPhaseEnded => TouchPhase::Ended,
+ NSEventPhase::MayBegin | NSEventPhase::Began => TouchPhase::Started,
+ NSEventPhase::Ended => TouchPhase::Ended,
_ => TouchPhase::Moved,
};
@@ -269,7 +271,7 @@ pub(crate) unsafe fn platform_input_from_native(
native_event.scrollingDeltaY() as f32,
);
- let delta = if native_event.hasPreciseScrollingDeltas() == YES {
+ let delta = if native_event.hasPreciseScrollingDeltas() {
ScrollDelta::Pixels(raw_data.map(px))
} else {
ScrollDelta::Lines(raw_data)
@@ -286,9 +288,9 @@ pub(crate) unsafe fn platform_input_from_native(
source: ScrollSource::Pointer,
})
}),
- NSEventType::NSLeftMouseDragged
- | NSEventType::NSRightMouseDragged
- | NSEventType::NSOtherMouseDragged => {
+ NSEventType::LeftMouseDragged
+ | NSEventType::RightMouseDragged
+ | NSEventType::OtherMouseDragged => {
let pressed_button = match native_event.buttonNumber() {
0 => MouseButton::Left,
1 => MouseButton::Right,
@@ -310,7 +312,7 @@ pub(crate) unsafe fn platform_input_from_native(
})
})
}
- NSEventType::NSMouseMoved => window_height.map(|window_height| {
+ NSEventType::MouseMoved => window_height.map(|window_height| {
PlatformInput::MouseMove(MouseMoveEvent {
position: point(
px(native_event.locationInWindow().x as f32),
@@ -320,7 +322,7 @@ pub(crate) unsafe fn platform_input_from_native(
modifiers: read_modifiers(native_event),
})
}),
- NSEventType::NSMouseExited => window_height.map(|window_height| {
+ NSEventType::MouseExited => window_height.map(|window_height| {
PlatformInput::MouseExited(MouseExitEvent {
position: point(
px(native_event.locationInWindow().x as f32),
@@ -336,164 +338,159 @@ pub(crate) unsafe fn platform_input_from_native(
}
}
-unsafe fn parse_keystroke(native_event: id) -> Keystroke {
- unsafe {
- use cocoa::appkit::*;
-
- let characters = native_event
- .charactersIgnoringModifiers()
- .to_str()
- .to_string();
- let mut key_char = None;
- let first_char = characters.chars().next().map(|ch| ch as u16);
- let modifiers = native_event.modifierFlags();
-
- let control = modifiers.contains(NSEventModifierFlags::NSControlKeyMask);
- let alt = modifiers.contains(NSEventModifierFlags::NSAlternateKeyMask);
- let mut shift = modifiers.contains(NSEventModifierFlags::NSShiftKeyMask);
- let command = modifiers.contains(NSEventModifierFlags::NSCommandKeyMask);
- let function = modifiers.contains(NSEventModifierFlags::NSFunctionKeyMask)
- && first_char
- .is_none_or(|ch| !(NSUpArrowFunctionKey..=NSModeSwitchFunctionKey).contains(&ch));
-
- #[allow(non_upper_case_globals)]
- let key = match first_char {
- Some(SPACE_KEY) => {
- key_char = Some(" ".to_string());
- "space".to_string()
- }
- Some(TAB_KEY) => {
- key_char = Some("\t".to_string());
- "tab".to_string()
- }
- Some(ENTER_KEY) | Some(NUMPAD_ENTER_KEY) => {
- key_char = Some("\n".to_string());
- "enter".to_string()
- }
- Some(BACKSPACE_KEY) => "backspace".to_string(),
- Some(ESCAPE_KEY) => "escape".to_string(),
- Some(SHIFT_TAB_KEY) => "tab".to_string(),
- Some(NSUpArrowFunctionKey) => "up".to_string(),
- Some(NSDownArrowFunctionKey) => "down".to_string(),
- Some(NSLeftArrowFunctionKey) => "left".to_string(),
- Some(NSRightArrowFunctionKey) => "right".to_string(),
- Some(NSPageUpFunctionKey) => "pageup".to_string(),
- Some(NSPageDownFunctionKey) => "pagedown".to_string(),
- Some(NSHomeFunctionKey) => "home".to_string(),
- Some(NSEndFunctionKey) => "end".to_string(),
- Some(NSDeleteFunctionKey) => "delete".to_string(),
- // Observed Insert==NSHelpFunctionKey not NSInsertFunctionKey.
- Some(NSHelpFunctionKey) => "insert".to_string(),
- Some(NSF1FunctionKey) => "f1".to_string(),
- Some(NSF2FunctionKey) => "f2".to_string(),
- Some(NSF3FunctionKey) => "f3".to_string(),
- Some(NSF4FunctionKey) => "f4".to_string(),
- Some(NSF5FunctionKey) => "f5".to_string(),
- Some(NSF6FunctionKey) => "f6".to_string(),
- Some(NSF7FunctionKey) => "f7".to_string(),
- Some(NSF8FunctionKey) => "f8".to_string(),
- Some(NSF9FunctionKey) => "f9".to_string(),
- Some(NSF10FunctionKey) => "f10".to_string(),
- Some(NSF11FunctionKey) => "f11".to_string(),
- Some(NSF12FunctionKey) => "f12".to_string(),
- Some(NSF13FunctionKey) => "f13".to_string(),
- Some(NSF14FunctionKey) => "f14".to_string(),
- Some(NSF15FunctionKey) => "f15".to_string(),
- Some(NSF16FunctionKey) => "f16".to_string(),
- Some(NSF17FunctionKey) => "f17".to_string(),
- Some(NSF18FunctionKey) => "f18".to_string(),
- Some(NSF19FunctionKey) => "f19".to_string(),
- Some(NSF20FunctionKey) => "f20".to_string(),
- Some(NSF21FunctionKey) => "f21".to_string(),
- Some(NSF22FunctionKey) => "f22".to_string(),
- Some(NSF23FunctionKey) => "f23".to_string(),
- Some(NSF24FunctionKey) => "f24".to_string(),
- Some(NSF25FunctionKey) => "f25".to_string(),
- Some(NSF26FunctionKey) => "f26".to_string(),
- Some(NSF27FunctionKey) => "f27".to_string(),
- Some(NSF28FunctionKey) => "f28".to_string(),
- Some(NSF29FunctionKey) => "f29".to_string(),
- Some(NSF30FunctionKey) => "f30".to_string(),
- Some(NSF31FunctionKey) => "f31".to_string(),
- Some(NSF32FunctionKey) => "f32".to_string(),
- Some(NSF33FunctionKey) => "f33".to_string(),
- Some(NSF34FunctionKey) => "f34".to_string(),
- Some(NSF35FunctionKey) => "f35".to_string(),
- _ => {
- // Cases to test when modifying this:
- //
- // qwerty key | none | cmd | cmd-shift
- // * Armenian s | ս | cmd-s | cmd-shift-s (layout is non-ASCII, so we use cmd layout)
- // * Dvorak+QWERTY s | o | cmd-s | cmd-shift-s (layout switches on cmd)
- // * Ukrainian+QWERTY s | с | cmd-s | cmd-shift-s (macOS reports cmd-s instead of cmd-S)
- // * Czech 7 | ý | cmd-ý | cmd-7 (layout has shifted numbers)
- // * Norwegian 7 | 7 | cmd-7 | cmd-/ (macOS reports cmd-shift-7 instead of cmd-/)
- // * Russian 7 | 7 | cmd-7 | cmd-& (shift-7 is . but when cmd is down, should use cmd layout)
- // * German QWERTZ ; | ö | cmd-ö | cmd-Ö (Zed's shift special case only applies to a-z)
- //
- let mut chars_ignoring_modifiers =
- chars_for_modified_key(native_event.keyCode(), NO_MOD);
- let mut chars_with_shift =
- chars_for_modified_key(native_event.keyCode(), SHIFT_MOD);
- let always_use_cmd_layout = always_use_command_layout();
-
- // Handle Dvorak+QWERTY / Russian / Armenian
- if command || always_use_cmd_layout {
- let chars_with_cmd = chars_for_modified_key(native_event.keyCode(), CMD_MOD);
- let chars_with_both =
- chars_for_modified_key(native_event.keyCode(), CMD_MOD | SHIFT_MOD);
-
- // We don't do this in the case that the shifted command key generates
- // the same character as the unshifted command key (Norwegian, e.g.)
- if chars_with_both != chars_with_cmd {
- chars_with_shift = chars_with_both;
-
- // Handle edge-case where cmd-shift-s reports cmd-s instead of
- // cmd-shift-s (Ukrainian, etc.)
- } else if chars_with_cmd.to_ascii_uppercase() != chars_with_cmd {
- chars_with_shift = chars_with_cmd.to_ascii_uppercase();
- }
- chars_ignoring_modifiers = chars_with_cmd;
- }
-
- if !control && !command && !function {
- let mut mods = NO_MOD;
- if shift {
- mods |= SHIFT_MOD;
- }
- if alt {
- mods |= OPTION_MOD;
- }
+fn parse_keystroke(native_event: &NSEvent) -> Keystroke {
+ let characters = unsafe {
+ let characters: *mut AnyObject = msg_send![native_event, charactersIgnoringModifiers];
+ characters.to_str().to_string()
+ };
+ let mut key_char = None;
+ let first_char = characters.chars().next().map(|ch| u32::from(ch as u16));
+ let modifiers = native_event.modifierFlags();
+
+ let control = modifiers.contains(NSEventModifierFlags::Control);
+ let alt = modifiers.contains(NSEventModifierFlags::Option);
+ let mut shift = modifiers.contains(NSEventModifierFlags::Shift);
+ let command = modifiers.contains(NSEventModifierFlags::Command);
+ let function = modifiers.contains(NSEventModifierFlags::Function)
+ && first_char
+ .is_none_or(|ch| !(NSUpArrowFunctionKey..=NSModeSwitchFunctionKey).contains(&ch));
- key_char = Some(chars_for_modified_key(native_event.keyCode(), mods));
+ #[allow(non_upper_case_globals)]
+ let key = match first_char {
+ Some(SPACE_KEY) => {
+ key_char = Some(" ".to_string());
+ "space".to_string()
+ }
+ Some(TAB_KEY) => {
+ key_char = Some("\t".to_string());
+ "tab".to_string()
+ }
+ Some(ENTER_KEY) | Some(NUMPAD_ENTER_KEY) => {
+ key_char = Some("\n".to_string());
+ "enter".to_string()
+ }
+ Some(BACKSPACE_KEY) => "backspace".to_string(),
+ Some(key) if key == u32::from(ESCAPE_KEY) => "escape".to_string(),
+ Some(SHIFT_TAB_KEY) => "tab".to_string(),
+ Some(NSUpArrowFunctionKey) => "up".to_string(),
+ Some(NSDownArrowFunctionKey) => "down".to_string(),
+ Some(NSLeftArrowFunctionKey) => "left".to_string(),
+ Some(NSRightArrowFunctionKey) => "right".to_string(),
+ Some(NSPageUpFunctionKey) => "pageup".to_string(),
+ Some(NSPageDownFunctionKey) => "pagedown".to_string(),
+ Some(NSHomeFunctionKey) => "home".to_string(),
+ Some(NSEndFunctionKey) => "end".to_string(),
+ Some(NSDeleteFunctionKey) => "delete".to_string(),
+ // Observed Insert==NSHelpFunctionKey not NSInsertFunctionKey.
+ Some(NSHelpFunctionKey) => "insert".to_string(),
+ Some(NSF1FunctionKey) => "f1".to_string(),
+ Some(NSF2FunctionKey) => "f2".to_string(),
+ Some(NSF3FunctionKey) => "f3".to_string(),
+ Some(NSF4FunctionKey) => "f4".to_string(),
+ Some(NSF5FunctionKey) => "f5".to_string(),
+ Some(NSF6FunctionKey) => "f6".to_string(),
+ Some(NSF7FunctionKey) => "f7".to_string(),
+ Some(NSF8FunctionKey) => "f8".to_string(),
+ Some(NSF9FunctionKey) => "f9".to_string(),
+ Some(NSF10FunctionKey) => "f10".to_string(),
+ Some(NSF11FunctionKey) => "f11".to_string(),
+ Some(NSF12FunctionKey) => "f12".to_string(),
+ Some(NSF13FunctionKey) => "f13".to_string(),
+ Some(NSF14FunctionKey) => "f14".to_string(),
+ Some(NSF15FunctionKey) => "f15".to_string(),
+ Some(NSF16FunctionKey) => "f16".to_string(),
+ Some(NSF17FunctionKey) => "f17".to_string(),
+ Some(NSF18FunctionKey) => "f18".to_string(),
+ Some(NSF19FunctionKey) => "f19".to_string(),
+ Some(NSF20FunctionKey) => "f20".to_string(),
+ Some(NSF21FunctionKey) => "f21".to_string(),
+ Some(NSF22FunctionKey) => "f22".to_string(),
+ Some(NSF23FunctionKey) => "f23".to_string(),
+ Some(NSF24FunctionKey) => "f24".to_string(),
+ Some(NSF25FunctionKey) => "f25".to_string(),
+ Some(NSF26FunctionKey) => "f26".to_string(),
+ Some(NSF27FunctionKey) => "f27".to_string(),
+ Some(NSF28FunctionKey) => "f28".to_string(),
+ Some(NSF29FunctionKey) => "f29".to_string(),
+ Some(NSF30FunctionKey) => "f30".to_string(),
+ Some(NSF31FunctionKey) => "f31".to_string(),
+ Some(NSF32FunctionKey) => "f32".to_string(),
+ Some(NSF33FunctionKey) => "f33".to_string(),
+ Some(NSF34FunctionKey) => "f34".to_string(),
+ Some(NSF35FunctionKey) => "f35".to_string(),
+ _ => {
+ // Cases to test when modifying this:
+ //
+ // qwerty key | none | cmd | cmd-shift
+ // * Armenian s | ս | cmd-s | cmd-shift-s (layout is non-ASCII, so we use cmd layout)
+ // * Dvorak+QWERTY s | o | cmd-s | cmd-shift-s (layout switches on cmd)
+ // * Ukrainian+QWERTY s | с | cmd-s | cmd-shift-s (macOS reports cmd-s instead of cmd-S)
+ // * Czech 7 | ý | cmd-ý | cmd-7 (layout has shifted numbers)
+ // * Norwegian 7 | 7 | cmd-7 | cmd-/ (macOS reports cmd-shift-7 instead of cmd-/)
+ // * Russian 7 | 7 | cmd-7 | cmd-& (shift-7 is . but when cmd is down, should use cmd layout)
+ // * German QWERTZ ; | ö | cmd-ö | cmd-Ö (Zed's shift special case only applies to a-z)
+ //
+ let mut chars_ignoring_modifiers =
+ chars_for_modified_key(native_event.keyCode(), NO_MOD);
+ let mut chars_with_shift = chars_for_modified_key(native_event.keyCode(), SHIFT_MOD);
+ let always_use_cmd_layout = always_use_command_layout();
+
+ // Handle Dvorak+QWERTY / Russian / Armenian
+ if command || always_use_cmd_layout {
+ let chars_with_cmd = chars_for_modified_key(native_event.keyCode(), CMD_MOD);
+ let chars_with_both =
+ chars_for_modified_key(native_event.keyCode(), CMD_MOD | SHIFT_MOD);
+
+ // We don't do this in the case that the shifted command key generates
+ // the same character as the unshifted command key (Norwegian, e.g.)
+ if chars_with_both != chars_with_cmd {
+ chars_with_shift = chars_with_both;
+
+ // Handle edge-case where cmd-shift-s reports cmd-s instead of
+ // cmd-shift-s (Ukrainian, etc.)
+ } else if chars_with_cmd.to_ascii_uppercase() != chars_with_cmd {
+ chars_with_shift = chars_with_cmd.to_ascii_uppercase();
}
+ chars_ignoring_modifiers = chars_with_cmd;
+ }
- if shift
- && chars_ignoring_modifiers
- .chars()
- .all(|c| c.is_ascii_lowercase())
- {
- chars_ignoring_modifiers
- } else if shift {
- shift = false;
- chars_with_shift
- } else {
- chars_ignoring_modifiers
+ if !control && !command && !function {
+ let mut mods = NO_MOD;
+ if shift {
+ mods |= SHIFT_MOD;
+ }
+ if alt {
+ mods |= OPTION_MOD;
}
+
+ key_char = Some(chars_for_modified_key(native_event.keyCode(), mods));
+ }
+
+ if shift
+ && chars_ignoring_modifiers
+ .chars()
+ .all(|c| c.is_ascii_lowercase())
+ {
+ chars_ignoring_modifiers
+ } else if shift {
+ shift = false;
+ chars_with_shift
+ } else {
+ chars_ignoring_modifiers
}
- };
-
- Keystroke {
- modifiers: Modifiers {
- control,
- alt,
- shift,
- platform: command,
- function,
- },
- key,
- key_char,
}
+ };
+
+ Keystroke {
+ modifiers: Modifiers {
+ control,
+ alt,
+ shift,
+ platform: command,
+ function,
+ },
+ key,
+ key_char,
}
}
diff --git a/crates/gpui_macos/src/gpui_macos.rs b/crates/gpui_macos/src/gpui_macos.rs
index a48dc8f098f..6314ca3da40 100644
--- a/crates/gpui_macos/src/gpui_macos.rs
+++ b/crates/gpui_macos/src/gpui_macos.rs
@@ -33,6 +33,8 @@ use cocoa::{
};
use objc::runtime::{BOOL, NO, YES};
+use objc2::runtime::AnyObject;
+use objc2_foundation::NSString as Objc2NSString;
use std::{
ffi::{CStr, c_char},
ops::Range,
@@ -74,6 +76,22 @@ impl NSStringExt for id {
}
}
+impl NSStringExt for *mut AnyObject {
+ unsafe fn to_str(&self) -> &str {
+ unsafe {
+ let Some(string) = self.cast::<Objc2NSString>().as_ref() else {
+ return "";
+ };
+ let cstr = string.UTF8String();
+ if cstr.is_null() {
+ ""
+ } else {
+ CStr::from_ptr(cstr).to_str().unwrap()
+ }
+ }
+ }
+}
+
#[repr(C)]
#[derive(Copy, Clone, Debug)]
struct NSRange {
diff --git a/crates/gpui_macos/src/keyboard.rs b/crates/gpui_macos/src/keyboard.rs
index 2b5b644a3ae..636346754e6 100644
--- a/crates/gpui_macos/src/keyboard.rs
+++ b/crates/gpui_macos/src/keyboard.rs
@@ -1,13 +1,13 @@
use collections::HashMap;
-use std::ffi::{CStr, c_void};
+use std::ffi::c_void;
-use objc::{msg_send, runtime::Object, sel, sel_impl};
+use objc2::msg_send;
use gpui::{KeybindingKeystroke, Keystroke, PlatformKeyboardLayout, PlatformKeyboardMapper};
use crate::{
- TISCopyCurrentKeyboardLayoutInputSource, TISGetInputSourceProperty, kTISPropertyInputSourceID,
- kTISPropertyLocalizedName,
+ NSStringExt, TISCopyCurrentKeyboardLayoutInputSource, TISGetInputSourceProperty,
+ kTISPropertyInputSourceID, kTISPropertyLocalizedName,
};
pub(crate) struct MacKeyboardLayout {
@@ -54,20 +54,26 @@ impl MacKeyboardLayout {
pub(crate) fn new() -> Self {
unsafe {
let current_keyboard = TISCopyCurrentKeyboardLayoutInputSource();
+ if current_keyboard.is_null() {
+ return Self {
+ id: String::new(),
+ name: String::new(),
+ };
+ }
- let id: *mut Object = TISGetInputSourceProperty(
+ let id = TISGetInputSourceProperty(
current_keyboard,
kTISPropertyInputSourceID as *const c_void,
- );
- let id: *const std::os::raw::c_char = msg_send![id, UTF8String];
- let id = CStr::from_ptr(id).to_str().unwrap().to_string();
+ )
+ .to_str()
+ .to_string();
- let name: *mut Object = TISGetInputSourceProperty(
+ let name = TISGetInputSourceProperty(
current_keyboard,
kTISPropertyLocalizedName as *const c_void,
- );
- let name: *const std::os::raw::c_char = msg_send![name, UTF8String];
- let name = CStr::from_ptr(name).to_str().unwrap().to_string();
+ )
+ .to_str()
+ .to_string();
let _: () = msg_send![current_keyboard, release];
diff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rs
index f0538566e6f..913c96fc2ea 100644
--- a/crates/gpui_macos/src/platform.rs
+++ b/crates/gpui_macos/src/platform.rs
@@ -1573,12 +1573,12 @@ unsafe fn ns_url_to_path(url: id) -> Result<PathBuf> {
#[link(name = "Carbon", kind = "framework")]
unsafe extern "C" {
- pub(super) fn TISCopyCurrentKeyboardLayoutInputSource() -> *mut Object;
- pub(super) fn TISCopyCurrentKeyboardInputSource() -> *mut Object;
+ pub(super) fn TISCopyCurrentKeyboardLayoutInputSource() -> *mut AnyObject;
+ pub(super) fn TISCopyCurrentKeyboardInputSource() -> *mut AnyObject;
pub(super) fn TISGetInputSourceProperty(
- inputSource: *mut Object,
+ inputSource: *mut AnyObject,
propertyKey: *const c_void,
- ) -> *mut Object;
+ ) -> *mut AnyObject;
pub(super) fn UCKeyTranslate(
keyLayoutPtr: *const ::std::os::raw::c_void,
diff --git a/crates/gpui_macos/src/window.rs b/crates/gpui_macos/src/window.rs
index 2ca0e202ffb..2e6fe9a953b 100644
--- a/crates/gpui_macos/src/window.rs
+++ b/crates/gpui_macos/src/window.rs
@@ -2652,7 +2652,7 @@ extern "C" fn handle_key_event(this: &Object, native_event: id, key_equivalent:
let mut lock = window_state.as_ref().lock();
let window_height = lock.content_size().height;
- let event = unsafe { platform_input_from_native(native_event, Some(window_height)) };
+ let event = unsafe { platform_input_from_native(native_event.cast(), Some(window_height)) };
let Some(event) = event else {
return NO;
@@ -2806,7 +2806,7 @@ extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) {
}
_ => {}
}
- let event = unsafe { platform_input_from_native(native_event, Some(window_height)) };
+ let event = unsafe { platform_input_from_native(native_event.cast(), Some(window_height)) };
if let Some(mut event) = event {
// AppKit unhides the cursor on the next mouse movement; mirror that here.