From 912628030e926080c06e7597b1d6166ca65f8342 Mon Sep 17 00:00:00 2001 From: Ingo Budde Date: Mon, 17 Jan 2022 20:23:36 +0100 Subject: [PATCH] Add scrollwheel support for macos --- src/macos/view.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/macos/view.rs b/src/macos/view.rs index de0e618..62c04df 100644 --- a/src/macos/view.rs +++ b/src/macos/view.rs @@ -15,7 +15,7 @@ use uuid::Uuid; use crate::MouseEvent::{ButtonPressed, ButtonReleased}; use crate::{ - Event, EventStatus, MouseButton, MouseEvent, Point, Size, WindowEvent, WindowInfo, + Event, EventStatus, MouseButton, MouseEvent, Point, ScrollDelta, Size, WindowEvent, WindowInfo, WindowOpenOptions, }; @@ -121,6 +121,8 @@ unsafe fn create_view_class() -> &'static Class { class.add_method(sel!(rightMouseDragged:), mouse_moved as extern "C" fn(&Object, Sel, id)); class.add_method(sel!(otherMouseDragged:), mouse_moved as extern "C" fn(&Object, Sel, id)); + class.add_method(sel!(scrollWheel:), scroll_wheel as extern "C" fn(&Object, Sel, id)); + class.add_method( sel!(viewDidChangeBackingProperties:), view_did_change_backing_properties as extern "C" fn(&Object, Sel, id), @@ -313,3 +315,20 @@ extern "C" fn mouse_moved(this: &Object, _sel: Sel, event: id) { state.trigger_event(Event::Mouse(MouseEvent::CursorMoved { position })); } + +extern "C" fn scroll_wheel(this: &Object, _: Sel, event: id) { + let state: &mut WindowState = unsafe { WindowState::from_field(this) }; + + let delta = unsafe { + let x = NSEvent::scrollingDeltaX(event) as f32; + let y = NSEvent::scrollingDeltaY(event) as f32; + + if NSEvent::hasPreciseScrollingDeltas(event) != NO { + ScrollDelta::Pixels { x, y } + } else { + ScrollDelta::Lines { x, y } + } + }; + + state.trigger_event(Event::Mouse(MouseEvent::WheelScrolled(delta))); +}