Skip to content

Commit

Permalink
Merge pull request rust-windowing#5 from rye/macos-update-stale-modif…
Browse files Browse the repository at this point in the history
…iers

macos: Add a hook to update stale modifiers
  • Loading branch information
murarth authored Jan 22, 2020
2 parents b28fd6b + ac78bab commit 4a8d78f
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 4a8d78f

Please sign in to comment.