Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add touchpad magnify and rotate gestures support for macOS #2157

Merged
merged 7 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ And please only add new entries to the top of this list, right below the `# Unre

- On Windows, added `WindowExtWindows::set_undecorated_shadow` and `WindowBuilderExtWindows::with_undecorated_shadow` to draw the drop shadow behind a borderless window.
- On Windows, fixed default window features (ie snap, animations, shake, etc.) when decorations are disabled.
- On macOS, add support for two-finger touchpad magnification and rotation gestures with new events `WindowEvent::TouchpadMagnify` and `WindowEvent::TouchpadRotate`.

# 0.27.2 (2022-8-12)

Expand Down
43 changes: 43 additions & 0 deletions examples/touchpad_gestures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};

fn main() {
SimpleLogger::new().init().unwrap();
let event_loop = EventLoop::new();

let _window = WindowBuilder::new()
.with_title("Touchpad gestures")
.build(&event_loop)
.unwrap();

println!("Only supported on macOS at the moment.");

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;

if let Event::WindowEvent { event, .. } = event {
match event {
WindowEvent::CloseRequested => *control_flow = ControlFlow::Exit,
WindowEvent::TouchpadMagnify { delta, .. } => {
if delta > 0.0 {
println!("Zoomed in {}", delta);
} else {
println!("Zoomed out {}", delta);
}
}
WindowEvent::TouchpadRotate { delta, .. } => {
if delta > 0.0 {
println!("Rotated counterclockwise {}", delta);
} else {
println!("Rotated clockwise {}", delta);
}
}
_ => (),
}
}
});
}
64 changes: 64 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,34 @@ pub enum WindowEvent<'a> {
modifiers: ModifiersState,
},

/// Touchpad magnification event with two-finger pinch gesture.
///
/// Positive delta values indicate magnification (zooming in) and
/// negative delta values indicate shrinking (zooming out).
///
/// ## Platform-specific
///
/// - Only available on **macOS**.
TouchpadMagnify {
device_id: DeviceId,
delta: f64,
phase: TouchPhase,
},

/// Touchpad rotation event with two-finger rotation gesture.
///
/// Positive delta values indicate rotation counterclockwise and
/// negative delta values indicate rotation clockwise.
///
/// ## Platform-specific
///
/// - Only available on **macOS**.
TouchpadRotate {
device_id: DeviceId,
delta: f32,
phase: TouchPhase,
},

/// Touchpad pressure event.
///
/// At the moment, only supported on Apple forcetouch-capable macbooks.
Expand Down Expand Up @@ -549,6 +577,24 @@ impl Clone for WindowEvent<'static> {
button: *button,
modifiers: *modifiers,
},
TouchpadMagnify {
device_id,
delta,
phase,
} => TouchpadMagnify {
device_id: *device_id,
delta: *delta,
phase: *phase,
},
TouchpadRotate {
device_id,
delta,
phase,
} => TouchpadRotate {
device_id: *device_id,
delta: *delta,
phase: *phase,
},
TouchpadPressure {
device_id,
pressure,
Expand Down Expand Up @@ -637,6 +683,24 @@ impl<'a> WindowEvent<'a> {
button,
modifiers,
}),
TouchpadMagnify {
device_id,
delta,
phase,
} => Some(TouchpadMagnify {
device_id,
delta,
phase,
}),
TouchpadRotate {
device_id,
delta,
phase,
} => Some(TouchpadRotate {
device_id,
delta,
phase,
}),
TouchpadPressure {
device_id,
pressure,
Expand Down
66 changes: 66 additions & 0 deletions src/platform_impl/macos/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ static VIEW_CLASS: Lazy<ViewClass> = Lazy::new(|| unsafe {
sel!(scrollWheel:),
scroll_wheel as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(magnifyWithEvent:),
magnify_with_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(rotateWithEvent:),
rotate_with_event as extern "C" fn(&Object, Sel, id),
);
decl.add_method(
sel!(pressureChangeWithEvent:),
pressure_change_with_event as extern "C" fn(&Object, Sel, id),
Expand Down Expand Up @@ -1196,6 +1204,64 @@ extern "C" fn scroll_wheel(this: &Object, _sel: Sel, event: id) {
}
}

extern "C" fn magnify_with_event(this: &Object, _sel: Sel, event: id) {
trace_scope!("magnifyWithEvent:");

unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);

let delta = event.magnification();
let phase = match event.phase() {
NSEventPhase::NSEventPhaseBegan => TouchPhase::Started,
NSEventPhase::NSEventPhaseChanged => TouchPhase::Moved,
NSEventPhase::NSEventPhaseCancelled => TouchPhase::Cancelled,
NSEventPhase::NSEventPhaseEnded => TouchPhase::Ended,
_ => return,
};

let window_event = Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::TouchpadMagnify {
device_id: DEVICE_ID,
delta,
phase,
},
};

AppState::queue_event(EventWrapper::StaticEvent(window_event));
}
}

extern "C" fn rotate_with_event(this: &Object, _sel: Sel, event: id) {
trace_scope!("rotateWithEvent:");

unsafe {
let state_ptr: *mut c_void = *this.get_ivar("winitState");
let state = &mut *(state_ptr as *mut ViewState);

let delta = event.rotation();
let phase = match event.phase() {
NSEventPhase::NSEventPhaseBegan => TouchPhase::Started,
NSEventPhase::NSEventPhaseChanged => TouchPhase::Moved,
NSEventPhase::NSEventPhaseCancelled => TouchPhase::Cancelled,
NSEventPhase::NSEventPhaseEnded => TouchPhase::Ended,
_ => return,
};

let window_event = Event::WindowEvent {
window_id: WindowId(get_window_id(state.ns_window)),
event: WindowEvent::TouchpadRotate {
device_id: DEVICE_ID,
delta,
phase,
},
};

AppState::queue_event(EventWrapper::StaticEvent(window_event));
}
}

extern "C" fn pressure_change_with_event(this: &Object, _sel: Sel, event: id) {
trace_scope!("pressureChangeWithEvent:");

Expand Down