diff --git a/Cargo.lock b/Cargo.lock index 7aecdd8f33795b..506d8cb79e1cc6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5657,7 +5657,7 @@ dependencies = [ "httpdate", "itoa", "pin-project-lite", - "socket2 0.4.10", + "socket2 0.5.7", "tokio", "tower-service", "tracing", diff --git a/crates/gpui/src/platform.rs b/crates/gpui/src/platform.rs index 070f29842ec63e..ba0c19ee75a2b7 100644 --- a/crates/gpui/src/platform.rs +++ b/crates/gpui/src/platform.rs @@ -671,6 +671,10 @@ impl PlatformInputHandler { .flatten() } + fn apple_press_and_hold_enabled(&mut self) -> bool { + self.handler.apple_press_and_hold_enabled() + } + pub(crate) fn dispatch_input(&mut self, input: &str, cx: &mut WindowContext) { self.handler.replace_text_in_range(None, input, cx); } @@ -768,6 +772,14 @@ pub trait InputHandler: 'static { range_utf16: Range, cx: &mut WindowContext, ) -> Option>; + + /// Allows a given input context to opt into getting raw key repeats instead of + /// sending these to the platform. + /// TODO: Ideally we should be able to set ApplePressAndHoldEnabled in NSUserDefaults + /// (which is how iTerm does it) but it doesn't seem to work for me. + fn apple_press_and_hold_enabled(&mut self) -> bool { + true + } } /// The variables that can be configured when creating a new window diff --git a/crates/gpui/src/platform/mac/window.rs b/crates/gpui/src/platform/mac/window.rs index b1672f3fdb3b75..6427a13fcde25c 100644 --- a/crates/gpui/src/platform/mac/window.rs +++ b/crates/gpui/src/platform/mac/window.rs @@ -1212,7 +1212,7 @@ extern "C" fn handle_key_down(this: &Object, _: Sel, native_event: id) { // Brazilian layout: // - `" space` should create an unmarked quote // - `" backspace` should delete the marked quote -// - `" "`shoud create an unmarked quote and a second marked quote +// - `" "`should create an unmarked quote and a second marked quote // - `" up` should insert a quote, unmark it, and move up one line // - `" cmd-down` should insert a quote, unmark it, and move to the end of the file // - `cmd-ctrl-space` and clicking on an emoji should type it @@ -1273,7 +1273,7 @@ extern "C" fn handle_key_event(this: &Object, native_event: id, key_equivalent: let mut callback = window_state.as_ref().lock().event_callback.take(); let handled = if let Some(callback) = callback.as_mut() { - !callback(PlatformInput::KeyDown(event)).propagate + !callback(PlatformInput::KeyDown(event.clone())).propagate } else { false }; @@ -1282,10 +1282,27 @@ extern "C" fn handle_key_event(this: &Object, native_event: id, key_equivalent: return handled; } - unsafe { + if event.is_held { + let handled = with_input_handler(&this, |input_handler| { + if !input_handler.apple_press_and_hold_enabled() { + input_handler.replace_text_in_range( + None, + &event.keystroke.ime_key.unwrap_or(event.keystroke.key), + ); + return true; + } + false + }); + if handled == Some(true) { + return YES; + } + } + + let handled = unsafe { let input_context: id = msg_send![this, inputContext]; msg_send![input_context, handleEvent: native_event] - } + }; + handled } extern "C" fn handle_view_event(this: &Object, _: Sel, native_event: id) { diff --git a/crates/terminal_view/src/terminal_element.rs b/crates/terminal_view/src/terminal_element.rs index 5998caa0daaffb..bc4f58a5efb6f7 100644 --- a/crates/terminal_view/src/terminal_element.rs +++ b/crates/terminal_view/src/terminal_element.rs @@ -1044,6 +1044,10 @@ impl InputHandler for TerminalInputHandler { ) -> Option> { self.cursor_bounds } + + fn apple_press_and_hold_enabled(&mut self) -> bool { + false + } } pub fn is_blank(cell: &IndexedCell) -> bool {