From ea07ec1fda49d9be14918552039b72f56d8f4215 Mon Sep 17 00:00:00 2001 From: Joe Moon Date: Wed, 17 Oct 2018 19:03:26 -0700 Subject: [PATCH] macOS: fix modifiers during key repeat (#666) * macOS: fix modifiers during key repeat * fix compile warnings --- CHANGELOG.md | 7 ++++--- src/platform/macos/view.rs | 24 +++++++++++++++++------- src/platform/macos/window.rs | 1 - 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d14021f..0762449e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # Unreleased -- On Wayland, titles will now be displayed in the window header decoration -- On Wayland, key repetition is now ended when keyboard loses focus +- On Wayland, titles will now be displayed in the window header decoration. +- On Wayland, key repetition is now ended when keyboard loses focus. - On Wayland, windows will now use more stylish and modern client side decorations. - On Wayland, windows will use server-side decorations when available. - Added support for F16-F24 keys. @@ -11,8 +11,9 @@ - Added `WindowBuilderExt::with_gtk_theme_variant` to X11-specific `WindowBuilder` functions. - Fixed UTF8 handling bug in X11 `set_title` function. - On Windows, `Window::set_cursor` now applies immediately instead of requiring specific events to occur first. -- On Windows, fix window.set_maximized(). +- On Windows, fix `Window::set_maximized`. - On Windows, fix transparency (#260). +- on macOS, fix modifiers during key repeat. # Version 0.17.2 (2018-08-19) diff --git a/src/platform/macos/view.rs b/src/platform/macos/view.rs index 1e70be99..64296aeb 100644 --- a/src/platform/macos/view.rs +++ b/src/platform/macos/view.rs @@ -24,7 +24,7 @@ struct ViewState { shared: Weak, ime_spot: Option<(f64, f64)>, raw_characters: Option, - last_insert: Option, + is_key_down: bool, } pub fn new_view(window: id, shared: Weak) -> IdRef { @@ -33,7 +33,7 @@ pub fn new_view(window: id, shared: Weak) -> IdRef { shared, ime_spot: None, raw_characters: None, - last_insert: None, + is_key_down: false, }; unsafe { // This is free'd in `dealloc` @@ -280,7 +280,7 @@ extern fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_range: characters.len(), ); let string = str::from_utf8_unchecked(slice); - state.last_insert = Some(string.to_owned()); + state.is_key_down = true; // We don't need this now, but it's here if that changes. //let event: id = msg_send![class!(NSApp), currentEvent]; @@ -389,15 +389,25 @@ extern fn key_down(this: &Object, _sel: Sel, event: id) { }, }; + let characters: id = msg_send![event, characters]; + let slice = slice::from_raw_parts( + characters.UTF8String() as *const c_uchar, + characters.len(), + ); + let string = str::from_utf8_unchecked(slice); + + state.raw_characters = { + Some(string.to_owned()) + }; + if let Some(shared) = state.shared.upgrade() { shared.pending_events .lock() .unwrap() .push_back(window_event); // Emit `ReceivedCharacter` for key repeats - if is_repeat && state.last_insert.is_some() { - let last_insert = state.last_insert.as_ref().unwrap(); - for character in last_insert.chars() { + if is_repeat && state.is_key_down{ + for character in string.chars() { let window_event = Event::WindowEvent { window_id, event: WindowEvent::ReceivedCharacter(character), @@ -424,7 +434,7 @@ extern fn key_up(this: &Object, _sel: Sel, event: id) { let state_ptr: *mut c_void = *this.get_ivar("winitState"); let state = &mut *(state_ptr as *mut ViewState); - state.last_insert = None; + state.is_key_down = false; // We need characters here to check for additional keys such as // F21-F24. diff --git a/src/platform/macos/window.rs b/src/platform/macos/window.rs index 8a307a93..59e8f05d 100644 --- a/src/platform/macos/window.rs +++ b/src/platform/macos/window.rs @@ -5,7 +5,6 @@ use std::os::raw::c_void; use std::sync::Weak; use std::sync::atomic::{Ordering, AtomicBool}; -use cocoa; use cocoa::appkit::{ self, CGFloat,