1
0
Fork 0

Add scrollwheel support for macos

This commit is contained in:
Ingo Budde 2022-01-17 20:23:36 +01:00
parent 004065e9a4
commit 912628030e

View file

@ -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)));
}