diff --git a/PATCHES.md b/PATCHES.md index 28228bd0..199a50cf 100644 --- a/PATCHES.md +++ b/PATCHES.md @@ -58,7 +58,7 @@ Microsoft Korean IME (type 한) and the Microsoft Japanese IME (inside a composi arrow keys, Enter, Tab and Backspace still act. If they don't, limit the suppression to keys the composition consumes. -Not in this cut: W16 (Wayland pinch/rotation, #4778), the earlier tablet backport (replaced by `craft-tablet`). +Not in this cut: the earlier tablet backport (replaced by `craft-tablet`). ### W27 outgoing IME state (E21b) @@ -224,3 +224,68 @@ cross-compilation checks for Windows x86_64 GNU and macOS AArch64. Cross-compila execute native tests. The main-thread AppKit harness, Windows native message/IMM tests, live XIM/Wayland input methods, matching egui adapter and downstream custom editors remain separate qualification gates. Human review and merge are required. + +## W16: native Wayland pinch and rotation + +### Upstream reference and adaptation + +The upstream reference is rust-windowing/winit#4338, merged as +`3be30affe42c3d30c7bc9620e7d32635cfc47bac` (PR head +`78b07e5ae392c131a9f656b2e22f23b5930c6989`). Its gesture implementation is +`winit-wayland/src/seat/pointer/pointer_gesture.rs`. This is an adapted backport, +not a clean cherry-pick: upstream uses the split 0.31 window stack and also +delivers pan; this port targets Craft 0.30.13. + +The adaptation keeps the optional version-1 protocol binding, two-finger and +top-level-surface checks, and started/moved/ended/cancelled phases. It replaces +upstream scale subtraction with differences of logarithms because egui-winit +exponentiates the delta. A small production `PinchState` helper rejects invalid +scales and impossible jumps and makes cancellation/reset behavior testable. +Closed windows, poisoned locks and unknown events are handled without introducing +panic paths. Pointer and seat teardown preserve the Craft fork's newer drag/IME +handling. Swipe and pan are intentionally not backported. + +The production change is confined to the pointer gesture helper/policy, their +seat/state wiring and event documentation. No scroll or modifier handling is +added by the pinch dispatcher. Keep that scope when rebasing this patch. + +PR #20 initially ports the pinch-only candidate from storytold/pdfcraft#1580 onto the resolved +Craft fork. It optionally binds pointer-gestures-v1, converts cumulative compositor +scales to logarithmic incremental `PinchGesture` deltas and cancels gestures on pointer +or seat removal without replacing the newer drag/IME teardown. + +Leonid's rotation work from storytold/winit#21 (`fe3fb10d3df14f2d980c4760e7f69877429ee2bc`) +is layered onto #20's gesture lifecycle. Wayland supplies incremental clockwise degrees; +winit receives their counterclockwise equivalent. Zero and non-finite updates are ignored. +Rotation shares the optional binding, surface checks, and lifecycle phases, including +pointer/seat cancellation, without changing the pinch scale baseline. Rotation was not +exercised on physical hardware. Remove W16 when the suite uses a release with equivalent +Wayland pinch/rotation delivery, units, and cancellation. + +2026-10-31 automated validation: 207 library tests and four `wayland_pinch` regressions passed with +`--no-default-features --features wayland,rwh_06`; strict Clippy passed for the library +and that test target. Current PdfCraft 0.16.1 also passed four egui integration tests +with this port applied to its actual Git dependency. Live hardware validation was +not repeated for this update. + +### 2026-11-01 GNOME/Wayland validation + +The contributor exercised PdfCraft `c9470fb0` with the actual pinned winit dependency +`0bc1a9b7` on NixOS 26.05 and GNOME Shell 50.5, using a fresh isolated profile and a +synthetic one-page PDF. They confirmed pinch zoom in both directions, unchanged +application menu/toolbar size (only PDF content scales), and ordinary two-finger +scrolling after the pinch. + +Two completed `WAYLAND_DEBUG=client` captures contained 17 pinch begin/end pairs, +987 updates, and two compositor cancellations. Both captured scales below and above +1.0. The compositor advertised pointer-gestures version 3; the client bound version +1 and created its pinch object. No `wl_pointer.axis` event occurred between any +pinch begin/end pair; 284 axis events occurred outside those intervals. Together +with the pinch dispatcher's lack of scroll/modifier synthesis, this shows no +duplicate Ctrl-scroll zoom path in these GNOME samples. It does not establish that +behavior on every compositor or under every modifier combination. + +KDE and Sway were not tested. Physical pointer-capability/seat removal and a live +compositor without pointer-gestures remain untested; scale/reset/cancellation policy +is covered by the automated tests. Raw traces and the synthetic fixture are local +verification artifacts, not repository assets. diff --git a/src/changelog/unreleased.md b/src/changelog/unreleased.md index 5d6ebc26..c880041c 100644 --- a/src/changelog/unreleased.md +++ b/src/changelog/unreleased.md @@ -47,6 +47,8 @@ changelog entry. - On Wayland, add `ActiveEventLoopExtWayland::clipboard` and `platform::wayland::Clipboard`, which read and write text on the clipboard through winit's data device, so toolkits don't need to bind a second one. +- On Wayland, report two-finger touchpad `PinchGesture` and `RotationGesture` on compositors that + support the pointer gestures protocol. ### Changed diff --git a/src/event.rs b/src/event.rs index d7dd3159..89d14838 100644 --- a/src/event.rs +++ b/src/event.rs @@ -289,7 +289,8 @@ pub enum WindowEvent { /// /// ## Platform-specific /// - /// - Only available on **macOS** and **iOS**. + /// - Available on **macOS**, **iOS**, and **Wayland** compositors supporting + /// `zwp_pointer_gestures_v1` (PdfCraft backport of winit#4338). /// - On iOS, not recognized by default. It must be enabled when needed. PinchGesture { device_id: DeviceId, @@ -341,7 +342,8 @@ pub enum WindowEvent { /// /// ## Platform-specific /// - /// - Only available on **macOS** and **iOS**. + /// - Available on **macOS**, **iOS**, and **Wayland**. + /// - On Wayland, requires compositor pointer gesture support. /// - On iOS, not recognized by default. It must be enabled when needed. RotationGesture { device_id: DeviceId, diff --git a/src/platform_impl/linux/wayland/seat/mod.rs b/src/platform_impl/linux/wayland/seat/mod.rs index 1443a1c2..ff28de2a 100644 --- a/src/platform_impl/linux/wayland/seat/mod.rs +++ b/src/platform_impl/linux/wayland/seat/mod.rs @@ -11,6 +11,7 @@ use sctk::reexports::client::protocol::wl_touch::WlTouch; use sctk::reexports::client::{Connection, Proxy, QueueHandle}; use sctk::reexports::protocols::wp::relative_pointer::zv1::client::zwp_relative_pointer_v1::ZwpRelativePointerV1; use sctk::reexports::protocols::wp::text_input::zv3::client::zwp_text_input_v3::ZwpTextInputV3; +use wayland_protocols::wp::pointer_gestures::zv1::client::zwp_pointer_gesture_pinch_v1::ZwpPointerGesturePinchV1; use sctk::data_device_manager::data_device::DataDevice; use sctk::seat::pointer::{ThemeSpec, ThemedPointer}; @@ -28,6 +29,7 @@ mod pointer; mod text_input; mod touch; +pub use pointer::pointer_gesture::{PointerGestureData, PointerGesturesState}; pub use pointer::relative_pointer::RelativePointerState; pub use pointer::{PointerConstraintsState, WinitPointerData, WinitPointerDataExt}; pub use text_input::{ @@ -57,6 +59,9 @@ pub struct WinitSeatState { /// The relative pointer bound on the seat. relative_pointer: Option, + /// PdfCraft patch: the pinch gesture bound to this seat's pointer. + pointer_gesture_pinch: Option, + /// The keyboard bound on the seat. keyboard_state: Option, @@ -136,6 +141,14 @@ impl SeatHandler for WinitState { ) }); + seat_state.pointer_gesture_pinch = self.pointer_gestures.as_ref().map(|manager| { + manager.0.get_pinch_gesture( + themed_pointer.pointer(), + queue_handle, + PointerGestureData::default(), + ) + }); + let themed_pointer = Arc::new(themed_pointer); // Register cursor surface. @@ -164,6 +177,17 @@ impl SeatHandler for WinitState { seat: WlSeat, capability: SeatCapability, ) { + // Cancel before borrowing the seat, since cancellation queues a window event. + if matches!(capability, SeatCapability::Pointer) { + let gesture = + self.seats.get_mut(&seat.id()).and_then(|seat| seat.pointer_gesture_pinch.take()); + if let Some(gesture) = gesture { + if let Some(data) = gesture.data::() { + data.cancel(self); + } + gesture.destroy(); + } + } let seat_state = match self.seats.get_mut(&seat.id()) { Some(seat_state) => seat_state, None => { @@ -240,6 +264,14 @@ impl SeatHandler for WinitState { ) { // W15 (winit#4729): end a file drag in progress on this seat; W2 (winit#4747): the text // input is destroyed only here, when the seat itself goes away. + let removed_pointer_gesture = + self.seats.get_mut(&seat.id()).and_then(|seat| seat.pointer_gesture_pinch.take()); + if let Some(gesture) = removed_pointer_gesture { + if let Some(data) = gesture.data::() { + data.cancel(self); + } + gesture.destroy(); + } self.cancel_file_drag(&seat.id()); self.clipboard.remove_seat(&seat.id()); let removed_text_input = diff --git a/src/platform_impl/linux/wayland/seat/pointer/mod.rs b/src/platform_impl/linux/wayland/seat/pointer/mod.rs index 2e34ba83..27cf409b 100644 --- a/src/platform_impl/linux/wayland/seat/pointer/mod.rs +++ b/src/platform_impl/linux/wayland/seat/pointer/mod.rs @@ -33,6 +33,8 @@ use crate::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, Wind use crate::platform_impl::wayland::state::WinitState; use crate::platform_impl::wayland::{self, DeviceId, WindowId}; +mod pinch_state; +pub mod pointer_gesture; pub mod relative_pointer; mod scroll; diff --git a/src/platform_impl/linux/wayland/seat/pointer/pinch_state.rs b/src/platform_impl/linux/wayland/seat/pointer/pinch_state.rs new file mode 100644 index 00000000..e8b919be --- /dev/null +++ b/src/platform_impl/linux/wayland/seat/pointer/pinch_state.rs @@ -0,0 +1,79 @@ +//! Translate Wayland pinch scale and rotation into winit's gesture conventions. + +use crate::event::TouchPhase; + +#[derive(Debug)] +pub(crate) struct PinchState { + window: Option, + scale: f64, +} + +impl Default for PinchState { + fn default() -> Self { + Self { window: None, scale: 1.0 } + } +} + +#[derive(Debug)] +pub(crate) struct PinchEvent { + pub window: W, + pub phase: TouchPhase, + pub delta: f64, +} + +#[derive(Debug)] +pub(crate) struct RotationEvent { + pub window: W, + pub phase: TouchPhase, + pub delta: f32, +} + +impl PinchEvent { + pub fn rotation_boundary(&self) -> Option> { + let (window, phase) = (self.window, self.phase); + (phase != TouchPhase::Moved).then_some(RotationEvent { window, phase, delta: 0.0 }) + } +} + +impl PinchState { + pub fn begin(&mut self, window: Option, fingers: u32) -> Option> { + *self = Self::default(); + let window = window.filter(|_| fingers == 2)?; + self.window = Some(window); + Some(PinchEvent { window, phase: TouchPhase::Started, delta: 0.0 }) + } + + pub fn update(&mut self, scale: f64) -> Option> { + let window = self.window?; + if !scale.is_finite() || scale <= 0.0 { + return None; + } + // egui-winit exponentiates this delta. Use log differences, not subtraction of + // cumulative scales, so successive events multiply to the compositor's scale. + let delta = scale.ln() - self.scale.ln(); + // Reject impossible jumps before a consumer exponentiates them as f32. + if !delta.is_finite() || delta.abs() > 32.0 { + return None; + } + self.scale = scale; + Some(PinchEvent { window, phase: TouchPhase::Moved, delta }) + } + + pub fn rotate(&self, clockwise_degrees: f64) -> Option> { + let window = self.window?; + // The protocol already reports a change since the last update, not an absolute angle. + let delta = -(clockwise_degrees as f32); + // Narrowing can overflow, and a zero change is not a rotation update. + if !delta.is_finite() || delta == 0.0 { + return None; + } + Some(RotationEvent { window, phase: TouchPhase::Moved, delta }) + } + + pub fn end(&mut self, cancelled: bool) -> Option> { + let window = self.window.take()?; + self.scale = 1.0; + let phase = if cancelled { TouchPhase::Cancelled } else { TouchPhase::Ended }; + Some(PinchEvent { window, phase, delta: 0.0 }) + } +} diff --git a/src/platform_impl/linux/wayland/seat/pointer/pointer_gesture.rs b/src/platform_impl/linux/wayland/seat/pointer/pointer_gesture.rs new file mode 100644 index 00000000..a1c646dc --- /dev/null +++ b/src/platform_impl/linux/wayland/seat/pointer/pointer_gesture.rs @@ -0,0 +1,110 @@ +//! PdfCraft patch (#1095): pinch support adapted from rust-windowing/winit#4338. +//! Scale deltas use the logarithmic units egui-winit consumes. Rotation uses winit's +//! counterclockwise degrees, adapted from storytold/winit#21. + +use std::sync::Mutex; + +use sctk::compositor::SurfaceData; +use sctk::globals::GlobalData; +use wayland_client::globals::{BindError, GlobalList}; +use wayland_client::{delegate_dispatch, Connection, Dispatch, Proxy, QueueHandle}; +use wayland_protocols::wp::pointer_gestures::zv1::client::zwp_pointer_gesture_pinch_v1::{ + Event, ZwpPointerGesturePinchV1, +}; +use wayland_protocols::wp::pointer_gestures::zv1::client::zwp_pointer_gestures_v1::ZwpPointerGesturesV1; + +use crate::event::WindowEvent; +use crate::platform_impl::wayland::state::WinitState; +use crate::platform_impl::wayland::{make_wid, DeviceId, WindowId}; + +use super::pinch_state::{PinchEvent, PinchState, RotationEvent}; + +pub struct PointerGesturesState(pub ZwpPointerGesturesV1); + +impl PointerGesturesState { + pub fn new( + globals: &GlobalList, + queue_handle: &QueueHandle, + ) -> Result { + globals.bind(queue_handle, 1..=1, GlobalData).map(Self) + } +} + +#[derive(Debug, Default)] +pub struct PointerGestureData(Mutex>); + +fn emit( + state: &mut WinitState, + pinch: Option>, + rotation: Option>, +) { + let device_id = crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland(DeviceId)); + let rotation = rotation.or_else(|| pinch.as_ref().and_then(PinchEvent::rotation_boundary)); + // A window may have closed while the gesture was in progress. + let windows = state.windows.get_mut(); + let pinch = pinch.filter(|event| windows.contains_key(&event.window)); + let rotation = rotation.filter(|event| windows.contains_key(&event.window)); + if let Some(event) = pinch { + state.events_sink.push_window_event( + WindowEvent::PinchGesture { device_id, delta: event.delta, phase: event.phase }, + event.window, + ); + } + if let Some(event) = rotation { + state.events_sink.push_window_event( + WindowEvent::RotationGesture { device_id, delta: event.delta, phase: event.phase }, + event.window, + ); + } +} + +impl PointerGestureData { + pub fn cancel(&self, state: &mut WinitState) { + if let Ok(mut pinch) = self.0.lock() { + emit(state, pinch.end(true), None); + } + } +} + +impl Dispatch for PointerGesturesState { + fn event( + _: &mut WinitState, + _: &ZwpPointerGesturesV1, + _: ::Event, + _: &GlobalData, + _: &Connection, + _: &QueueHandle, + ) { + // This interface has no events. + } +} + +impl Dispatch for PointerGesturesState { + fn event( + state: &mut WinitState, + _: &ZwpPointerGesturePinchV1, + event: Event, + data: &PointerGestureData, + _: &Connection, + _: &QueueHandle, + ) { + let Ok(mut pinch) = data.0.lock() else { return }; + let (pinch, rotation) = match event { + Event::Begin { surface, fingers, .. } => { + // Decoration/subsurface gestures must not affect the document. + let window = surface + .data::() + .filter(|data| data.parent_surface().is_none()) + .map(|_| make_wid(&surface)); + (pinch.begin(window, fingers), None) + }, + Event::Update { scale, rotation, .. } => (pinch.update(scale), pinch.rotate(rotation)), + Event::End { cancelled, .. } => (pinch.end(cancelled != 0), None), + _ => (None, None), + }; + emit(state, pinch, rotation); + } +} + +delegate_dispatch!(WinitState: [ZwpPointerGesturesV1: GlobalData] => PointerGesturesState); +delegate_dispatch!(WinitState: [ZwpPointerGesturePinchV1: PointerGestureData] => PointerGesturesState); diff --git a/src/platform_impl/linux/wayland/state.rs b/src/platform_impl/linux/wayland/state.rs index 46f4fd73..b45c5b70 100644 --- a/src/platform_impl/linux/wayland/state.rs +++ b/src/platform_impl/linux/wayland/state.rs @@ -31,8 +31,8 @@ use crate::platform_impl::wayland::clipboard::ClipboardState; use crate::platform_impl::wayland::event_loop::sink::EventSink; use crate::platform_impl::wayland::output::MonitorHandle; use crate::platform_impl::wayland::seat::{ - PointerConstraintsState, RelativePointerState, TextInputState, WinitPointerData, - WinitPointerDataExt, WinitSeatState, + PointerConstraintsState, PointerGesturesState, RelativePointerState, TextInputState, + WinitPointerData, WinitPointerDataExt, WinitSeatState, }; #[cfg(feature = "wayland-kde-blur")] use crate::platform_impl::wayland::types::kwin_blur::KWinBlurManager; @@ -119,6 +119,9 @@ pub struct WinitState { /// Pointer constraints to handle pointer locking and confining. pub pointer_constraints: Option>, + /// PdfCraft patch: optional Wayland pinch protocol; other compositors keep working. + pub pointer_gestures: Option, + /// Viewporter state on the given window. pub viewporter_state: Option, @@ -254,6 +257,7 @@ impl WinitState { fractional_scaling_manager, #[cfg(feature = "wayland-kde-blur")] kwin_blur_manager: KWinBlurManager::new(globals, queue_handle).ok(), + pointer_gestures: PointerGesturesState::new(globals, queue_handle).ok(), seats, text_input_state: TextInputState::new(globals, queue_handle).ok(), diff --git a/tests/wayland_pinch.rs b/tests/wayland_pinch.rs new file mode 100644 index 00000000..a8a35d87 --- /dev/null +++ b/tests/wayland_pinch.rs @@ -0,0 +1,116 @@ +#![cfg(target_os = "linux")] + +mod event { + pub use winit::event::TouchPhase; +} + +#[path = "../src/platform_impl/linux/wayland/seat/pointer/pinch_state.rs"] +mod pinch_state; + +use event::TouchPhase; +use pinch_state::PinchState; + +#[test] +fn cumulative_scales_produce_multiplicative_zoom_in_both_directions() { + let mut pinch = PinchState::default(); + let begin = pinch.begin(Some(7), 2).unwrap(); + assert_eq!(begin.phase, TouchPhase::Started); + assert_eq!(begin.delta, 0.0); + let mut total = 1.0; + for scale in [1.25, 1.5, 2.0, 0.75, 1.0] { + let update = pinch.update(scale).unwrap(); + assert_eq!(update.window, 7); + assert_eq!(update.phase, TouchPhase::Moved); + total *= update.delta.exp(); + assert!((total - scale).abs() < 1e-12); + } +} + +#[test] +fn ending_or_cancelling_resets_the_next_gesture() { + for cancelled in [false, true] { + let mut pinch = PinchState::default(); + pinch.begin(Some(3), 2).unwrap(); + pinch.update(2.0).unwrap(); + let end = pinch.end(cancelled).unwrap(); + assert_eq!(end.phase, if cancelled { TouchPhase::Cancelled } else { TouchPhase::Ended }); + assert_eq!(end.delta, 0.0); + assert!(pinch.update(3.0).is_none()); + assert!(pinch.end(cancelled).is_none()); + pinch.begin(Some(4), 2).unwrap(); + let update = pinch.update(1.5).unwrap(); + assert_eq!(update.window, 4); + assert!((update.delta.exp() - 1.5).abs() < 1e-12); + } +} + +#[test] +fn invalid_scales_do_not_poison_the_baseline() { + let mut pinch = PinchState::default(); + pinch.begin(Some(1), 2).unwrap(); + for scale in [0.0, -1.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY, 1e300, 1e-300] { + assert!(pinch.update(scale).is_none()); + } + assert!((pinch.update(2.0).unwrap().delta.exp() - 2.0).abs() < 1e-12); +} + +#[test] +fn unsupported_fingers_or_surfaces_clear_the_active_gesture() { + let mut pinch = PinchState::default(); + assert!(pinch.update(1.5).is_none()); + assert!(pinch.end(false).is_none()); + for (window, fingers) in [(Some(2), 1), (Some(2), 3), (None, 2)] { + pinch.begin(Some(1), 2).unwrap(); + assert!(pinch.begin(window, fingers).is_none()); + assert!(pinch.update(1.5).is_none()); + assert!(pinch.end(false).is_none()); + } +} + +#[test] +fn rotation_is_incremental_counterclockwise_degrees() { + let mut pinch = PinchState::default(); + pinch.begin(Some(7), 2).unwrap(); + for (clockwise, counterclockwise) in [(10.0, -10.0), (10.0, -10.0), (-2.5, 2.5)] { + let event = pinch.rotate(clockwise).unwrap(); + assert_eq!(event.window, 7); + assert_eq!(event.phase, TouchPhase::Moved); + assert_eq!(event.delta, counterclockwise); + } +} + +#[test] +fn invalid_rotation_does_not_emit_or_change_pinch_scale() { + let mut pinch = PinchState::default(); + pinch.begin(Some(1), 2).unwrap(); + for degrees in [0.0, -0.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY, f64::MAX, -f64::MAX] { + assert!(pinch.rotate(degrees).is_none()); + } + let moved = pinch.update(2.0).unwrap(); + assert!((moved.delta.exp() - 2.0).abs() < 1e-12); + assert!(moved.rotation_boundary().is_none(), "a pinch update cannot synthesize rotation"); + assert!(pinch.update(0.0).is_none()); + assert_eq!(pinch.rotate(2.0).unwrap().delta, -2.0); +} + +#[test] +fn rotation_boundaries_follow_only_supported_active_gestures() { + let mut pinch = PinchState::default(); + assert!(pinch.rotate(8.0).is_none()); + for (window, fingers) in [(Some(1), 3), (None, 2)] { + assert!(pinch.begin(window, fingers).is_none()); + assert!(pinch.rotate(8.0).is_none()); + } + for cancelled in [false, true] { + let start = pinch.begin(Some(5), 2).unwrap().rotation_boundary().unwrap(); + assert_eq!(start.window, 5); + assert_eq!(start.phase, TouchPhase::Started); + assert_eq!(start.delta, 0.0); + let finish = pinch.end(cancelled).unwrap().rotation_boundary().unwrap(); + assert_eq!(finish.window, 5); + assert_eq!(finish.phase, if cancelled { TouchPhase::Cancelled } else { TouchPhase::Ended }); + assert_eq!(finish.delta, 0.0); + assert!(pinch.rotate(8.0).is_none()); + assert!(pinch.end(cancelled).is_none()); + } +}