From 180a991760c62f59cc740dcd255829f2eebe94bb Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 5 Jan 2020 22:54:59 -0800 Subject: [PATCH] macos: add use_ime option to disable the IME The IME stuff on macos tends to swallow repeats for some keys. Ugh. So this commit adds an option to disable the use of the IME. Switching away from it effectively inverts the meanging of backspace and delete (because our method is no longer called by the IME), so we need to check for that and remap it. Ugh. Ugh. --- src/config/mod.rs | 3 +++ src/main.rs | 5 +++++ window/src/os/macos/window.rs | 36 +++++++++++++++++++++++------------ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index b07efdff9..80796af06 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -467,6 +467,9 @@ pub struct Config { /// to the terminal. #[serde(default = "default_true")] pub scroll_to_bottom_on_input: bool, + + #[serde(default = "default_true")] + pub use_ime: bool, } #[derive(Deserialize, Clone, Copy, Debug)] diff --git a/src/main.rs b/src/main.rs index e57bd29f5..b70b9fad5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -705,6 +705,11 @@ fn run() -> anyhow::Result<()> { Ok(config) => config, }; + #[cfg(target_os = "macos")] + { + window::os::macos::use_ime(config.use_ime); + } + match opts .cmd .as_ref() diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index 16b74418f..39e28c4b2 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -28,6 +28,13 @@ use std::any::Any; use std::cell::RefCell; use std::ffi::c_void; use std::rc::Rc; +use std::sync::atomic::{AtomicBool, Ordering}; + +static USE_IME: AtomicBool = AtomicBool::new(false); + +pub fn use_ime(enable: bool) { + USE_IME.store(enable, Ordering::Relaxed); +} #[repr(C)] struct NSRange(cocoa::foundation::NSRange); @@ -590,9 +597,11 @@ impl WindowOpsMut for WindowInner { if let Some(window_view) = WindowView::get_this(unsafe { &**self.view }) { window_view.inner.borrow_mut().text_cursor_position = cursor; } - unsafe { - let input_context: id = msg_send![&**self.view, inputContext]; - let () = msg_send![input_context, invalidateCharacterCoordinates]; + if USE_IME.load(Ordering::Relaxed) { + unsafe { + let input_context: id = msg_send![&**self.view, inputContext]; + let () = msg_send![input_context, invalidateCharacterCoordinates]; + } } } } @@ -1016,16 +1025,19 @@ impl WindowView { // `Fn-Delete` emits DEL. // Alt-Delete is mapped by the IME to be equivalent to Fn-Delete. // We want to emit Alt-BS in that situation. - let unmod = - if virtual_key == super::keycodes::kVK_Delete && modifiers.contains(Modifiers::ALT) { - "\x08" - } else if virtual_key == super::keycodes::kVK_Tab { - "\t" - } else { - unmod - }; + let unmod = if virtual_key == super::keycodes::kVK_Delete + && modifiers.contains(Modifiers::ALT) + { + "\x08" + } else if virtual_key == super::keycodes::kVK_Tab { + "\t" + } else if !USE_IME.load(Ordering::Relaxed) && virtual_key == super::keycodes::kVK_Delete { + "\x08" + } else { + unmod + }; - if modifiers.is_empty() { + if USE_IME.load(Ordering::Relaxed) && modifiers.is_empty() { unsafe { let input_context: id = msg_send![this, inputContext]; let res: BOOL = msg_send![input_context, handleEvent: nsevent];