Implement DeviceEvent::Button on Mac (#967)

* Add deviceevent logging to cursor_grab example

* Implement DeviceEvent::Button on Mac
This commit is contained in:
Tristam MacDonald 2019-06-26 23:58:21 -07:00 committed by Osspial
parent 34db2d7d4c
commit ac08601b40
3 changed files with 41 additions and 5 deletions

View file

@ -1,5 +1,6 @@
# Unreleased
- On Mac, implement `DeviceEvent::Button`.
- Change `Event::Suspended(true / false)` to `Event::Suspended` and `Event::Resumed`.
- On X11, fix sanity check which checks that a monitor's reported width and height (in millimeters) are non-zero when calculating the DPI factor.
- On Windows, when a window is initially invisible, it won't take focus from the existing visible windows.

View file

@ -1,5 +1,5 @@
use winit::{
event::{ElementState, Event, KeyboardInput, WindowEvent},
event::{DeviceEvent, ElementState, Event, KeyboardInput, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
@ -14,8 +14,8 @@ fn main() {
event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
if let Event::WindowEvent { event, .. } = event {
match event {
Event::WindowEvent { event, .. } => match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::KeyboardInput {
input:
@ -36,7 +36,16 @@ fn main() {
}
}
_ => (),
}
},
Event::DeviceEvent { event, .. } => match event {
DeviceEvent::MouseMotion { delta } => println!("mouse moved: {:?}", delta),
DeviceEvent::Button { button, state } => match state {
ElementState::Pressed => println!("mouse button {} pressed", button),
ElementState::Released => println!("mouse button {} released", button),
},
_ => (),
},
_ => (),
}
});
}

View file

@ -10,7 +10,7 @@ use objc::{
};
use crate::{
event::{DeviceEvent, Event},
event::{DeviceEvent, ElementState, Event},
platform_impl::platform::{app_state::AppState, util, DEVICE_ID},
};
@ -101,6 +101,32 @@ unsafe fn maybe_dispatch_device_event(event: id) {
AppState::queue_events(events);
}
appkit::NSLeftMouseDown | appkit::NSRightMouseDown | appkit::NSOtherMouseDown => {
let mut events = VecDeque::with_capacity(1);
events.push_back(Event::DeviceEvent {
device_id: DEVICE_ID,
event: DeviceEvent::Button {
button: event.buttonNumber() as u32,
state: ElementState::Pressed,
},
});
AppState::queue_events(events);
}
appkit::NSLeftMouseUp | appkit::NSRightMouseUp | appkit::NSOtherMouseUp => {
let mut events = VecDeque::with_capacity(1);
events.push_back(Event::DeviceEvent {
device_id: DEVICE_ID,
event: DeviceEvent::Button {
button: event.buttonNumber() as u32,
state: ElementState::Released,
},
});
AppState::queue_events(events);
}
_ => (),
}
}