macOS: fix modifiers during key repeat (#666)

* macOS: fix modifiers during key repeat

* fix compile warnings
This commit is contained in:
Joe Moon 2018-10-17 19:03:26 -07:00 committed by Francesca Plebani
parent 26b70e457b
commit ea07ec1fda
3 changed files with 21 additions and 11 deletions

View file

@ -1,7 +1,7 @@
# Unreleased # Unreleased
- On Wayland, titles will now be displayed in the window header decoration - 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, 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 now use more stylish and modern client side decorations.
- On Wayland, windows will use server-side decorations when available. - On Wayland, windows will use server-side decorations when available.
- Added support for F16-F24 keys. - Added support for F16-F24 keys.
@ -11,8 +11,9 @@
- Added `WindowBuilderExt::with_gtk_theme_variant` to X11-specific `WindowBuilder` functions. - Added `WindowBuilderExt::with_gtk_theme_variant` to X11-specific `WindowBuilder` functions.
- Fixed UTF8 handling bug in X11 `set_title` function. - 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, `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 Windows, fix transparency (#260).
- on macOS, fix modifiers during key repeat.
# Version 0.17.2 (2018-08-19) # Version 0.17.2 (2018-08-19)

View file

@ -24,7 +24,7 @@ struct ViewState {
shared: Weak<Shared>, shared: Weak<Shared>,
ime_spot: Option<(f64, f64)>, ime_spot: Option<(f64, f64)>,
raw_characters: Option<String>, raw_characters: Option<String>,
last_insert: Option<String>, is_key_down: bool,
} }
pub fn new_view(window: id, shared: Weak<Shared>) -> IdRef { pub fn new_view(window: id, shared: Weak<Shared>) -> IdRef {
@ -33,7 +33,7 @@ pub fn new_view(window: id, shared: Weak<Shared>) -> IdRef {
shared, shared,
ime_spot: None, ime_spot: None,
raw_characters: None, raw_characters: None,
last_insert: None, is_key_down: false,
}; };
unsafe { unsafe {
// This is free'd in `dealloc` // This is free'd in `dealloc`
@ -280,7 +280,7 @@ extern fn insert_text(this: &Object, _sel: Sel, string: id, _replacement_range:
characters.len(), characters.len(),
); );
let string = str::from_utf8_unchecked(slice); 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. // We don't need this now, but it's here if that changes.
//let event: id = msg_send![class!(NSApp), currentEvent]; //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() { if let Some(shared) = state.shared.upgrade() {
shared.pending_events shared.pending_events
.lock() .lock()
.unwrap() .unwrap()
.push_back(window_event); .push_back(window_event);
// Emit `ReceivedCharacter` for key repeats // Emit `ReceivedCharacter` for key repeats
if is_repeat && state.last_insert.is_some() { if is_repeat && state.is_key_down{
let last_insert = state.last_insert.as_ref().unwrap(); for character in string.chars() {
for character in last_insert.chars() {
let window_event = Event::WindowEvent { let window_event = Event::WindowEvent {
window_id, window_id,
event: WindowEvent::ReceivedCharacter(character), 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_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState); 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 // We need characters here to check for additional keys such as
// F21-F24. // F21-F24.

View file

@ -5,7 +5,6 @@ use std::os::raw::c_void;
use std::sync::Weak; use std::sync::Weak;
use std::sync::atomic::{Ordering, AtomicBool}; use std::sync::atomic::{Ordering, AtomicBool};
use cocoa;
use cocoa::appkit::{ use cocoa::appkit::{
self, self,
CGFloat, CGFloat,