diff --git a/CHANGELOG.md b/CHANGELOG.md index d4469498..a81c8ba4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/examples/cursor_grab.rs b/examples/cursor_grab.rs index 40613d70..726aba56 100644 --- a/examples/cursor_grab.rs +++ b/examples/cursor_grab.rs @@ -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 { + 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), + }, + _ => (), + }, + _ => (), } }); } diff --git a/src/platform_impl/macos/app.rs b/src/platform_impl/macos/app.rs index 7aad8d9d..3278cb13 100644 --- a/src/platform_impl/macos/app.rs +++ b/src/platform_impl/macos/app.rs @@ -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); + } _ => (), } }