Skip to content

Commit

Permalink
Merge pull request #229 from swiftcoder/master
Browse files Browse the repository at this point in the history
Implement raw mouse movement for Mac
  • Loading branch information
tomaka authored Aug 2, 2017
2 parents 718e0f8 + 15c4641 commit a582df4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/platform/macos/events_loop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use {ControlFlow, EventsLoopClosed};
use cocoa::{self, appkit, foundation};
use cocoa::appkit::{NSApplication, NSEvent, NSView, NSWindow};
use events::{self, ElementState, Event, MouseButton, TouchPhase, WindowEvent, ModifiersState, KeyboardInput};
use events::{self, ElementState, Event, MouseButton, TouchPhase, WindowEvent, DeviceEvent, ModifiersState, KeyboardInput};
use std::collections::VecDeque;
use std::sync::{Arc, Mutex, Weak};
use super::window::Window;
Expand Down Expand Up @@ -468,11 +468,33 @@ impl EventsLoop {
let view_rect = NSView::frame(*window.view);
let scale_factor = window.hidpi_factor();

let x = (scale_factor * view_point.x as f32) as f64;
let y = (scale_factor * (view_rect.size.height - view_point.y) as f32) as f64;
let window_event = WindowEvent::MouseMoved { device_id: DEVICE_ID, position: (x, y) };
let event = Event::WindowEvent { window_id: ::WindowId(window.id()), event: window_event };
Some(event)
let mut events = std::collections::VecDeque::new();

{
let x = (scale_factor * view_point.x as f32) as f64;
let y = (scale_factor * (view_rect.size.height - view_point.y) as f32) as f64;
let window_event = WindowEvent::MouseMoved { device_id: DEVICE_ID, position: (x, y) };
let event = Event::WindowEvent { window_id: ::WindowId(window.id()), event: window_event };
events.push_back(event);
}

let delta_x = (scale_factor * ns_event.deltaX() as f32) as f64;
if delta_x != 0.0 {
let motion_event = DeviceEvent::Motion { axis: 0, value: delta_x };
let event = Event::DeviceEvent{ device_id: DEVICE_ID, event: motion_event };
events.push_back(event);
}

let delta_y = (scale_factor * ns_event.deltaY() as f32) as f64;
if delta_y != 0.0 {
let motion_event = DeviceEvent::Motion { axis: 1, value: delta_y };
let event = Event::DeviceEvent{ device_id: DEVICE_ID, event: motion_event };
events.push_back(event);
}

let event = events.pop_front();
self.shared.pending_events.lock().unwrap().extend(events.into_iter());
event
},

appkit::NSScrollWheel => {
Expand Down
1 change: 1 addition & 0 deletions src/platform/macos/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ impl Window {
Ok(())
},
CursorState::Grab => {
let _: () = unsafe { msg_send![cls, hide] };
let _: i32 = unsafe { CGAssociateMouseAndMouseCursorPosition(false) };
Ok(())
}
Expand Down

0 comments on commit a582df4

Please sign in to comment.