Skip to content

Commit

Permalink
Enable key repeat in the terminal explicitly
Browse files Browse the repository at this point in the history
  • Loading branch information
ConradIrwin committed Nov 11, 2024
1 parent 4b09c3f commit 4cecede
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions crates/gpui/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -768,6 +772,14 @@ pub trait InputHandler: 'static {
range_utf16: Range<usize>,
cx: &mut WindowContext,
) -> Option<Bounds<Pixels>>;

/// 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
Expand Down
25 changes: 21 additions & 4 deletions crates/gpui/src/platform/mac/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
};
Expand All @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions crates/terminal_view/src/terminal_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,10 @@ impl InputHandler for TerminalInputHandler {
) -> Option<Bounds<Pixels>> {
self.cursor_bounds
}

fn apple_press_and_hold_enabled(&mut self) -> bool {
false
}
}

pub fn is_blank(cell: &IndexedCell) -> bool {
Expand Down

0 comments on commit 4cecede

Please sign in to comment.