Skip to content

Commit

Permalink
macos: Add a hook to update stale modifiers
Browse files Browse the repository at this point in the history
Sometimes, `ViewState` and `event` might have different values for their
stored `modifiers` flags.  These are internally stored as a bitmask in
the latter and an enum in the former.

We can check to see if they differ, and if they do, automatically
dispatch an event to update consumers of modifier state as well as the
stored `state.modifiers`.  That's what the hook does.

This hook is then called in the key_down, mouse_entered, mouse_exited,
mouse_click, scroll_wheel, and pressure_change_with_event callbacks,
which each will contain updated modifiers.

Signed-off-by: Kristofer Rye <[email protected]>
  • Loading branch information
rye committed Jan 22, 2020
1 parent b28fd6b commit ac78bab
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,18 @@ fn retrieve_keycode(event: id) -> Option<VirtualKeyCode> {
})
}

// Update `state.modifiers` if `event` has something different
fn update_potentially_stale_modifiers(state: &mut ViewState, event: id) {
if state.modifiers != event_mods(event) {
state.modifiers = event_mods(event);
trace!("Modifiers are stale; updating modifiers");
AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::ModifiersChanged(state.modifiers),
}));
}
}

extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {
trace!("Triggered `keyDown`");
unsafe {
Expand All @@ -638,6 +650,8 @@ extern "C" fn key_down(this: &Object, _sel: Sel, event: id) {

let is_repeat = msg_send![event, isARepeat];

update_potentially_stale_modifiers(state, event);

#[allow(deprecated)]
let window_event = Event::WindowEvent {
window_id,
Expand Down Expand Up @@ -762,8 +776,6 @@ extern "C" fn flags_changed(this: &Object, _sel: Sel, event: id) {
}));
}

trace!("Queueing event with modifiers {:?}", state.modifiers);

AppState::queue_event(EventWrapper::StaticEvent(Event::WindowEvent {
// TODO Maybe memoize get_window_id if it's safe to reuse?
window_id: WindowId(get_window_id(state.ns_window)),
Expand Down Expand Up @@ -834,6 +846,8 @@ fn mouse_click(this: &Object, event: id, button: MouseButton, button_state: Elem
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);

update_potentially_stale_modifiers(state, event);

let window_event = Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::MouseInput {
Expand Down Expand Up @@ -932,6 +946,8 @@ extern "C" fn mouse_entered(this: &Object, _sel: Sel, event: id) {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);

update_potentially_stale_modifiers(state, event);

let enter_event = Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::CursorEntered {
Expand All @@ -945,12 +961,14 @@ extern "C" fn mouse_entered(this: &Object, _sel: Sel, event: id) {
trace!("Completed `mouseEntered`");
}

extern "C" fn mouse_exited(this: &Object, _sel: Sel, _event: id) {
extern "C" fn mouse_exited(this: &Object, _sel: Sel, event: id) {
trace!("Triggered `mouseExited`");
unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);

update_potentially_stale_modifiers(state, event);

let window_event = Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::CursorLeft {
Expand Down Expand Up @@ -990,6 +1008,8 @@ extern "C" fn scroll_wheel(this: &Object, _sel: Sel, event: id) {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);

update_potentially_stale_modifiers(state, event);

let window_event = Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::MouseWheel {
Expand All @@ -1012,6 +1032,8 @@ extern "C" fn pressure_change_with_event(this: &Object, _sel: Sel, event: id) {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);

update_potentially_stale_modifiers(state, event);

let pressure = event.pressure();
let stage = event.stage();

Expand Down

0 comments on commit ac78bab

Please sign in to comment.