From 6c23e53b2288b3ed73900fec86882f0064ddbc24 Mon Sep 17 00:00:00 2001 From: tomasmark79 Date: Sat, 31 Oct 2026 08:56:47 +0100 Subject: [PATCH 1/3] wayland: deliver native touchpad pinch gestures on the Craft 0.30 branch Port the pinch input path from storytold/pdfcraft#1580 to the resolved winit fork, preserving newer drag and IME seat teardown. Test cumulative scales, cancellation, invalid input and unsupported surfaces. --- src/event.rs | 3 +- src/platform_impl/linux/wayland/seat/mod.rs | 32 ++++++ .../linux/wayland/seat/pointer/mod.rs | 2 + .../linux/wayland/seat/pointer/pinch_state.rs | 54 ++++++++++ .../wayland/seat/pointer/pointer_gesture.rs | 102 ++++++++++++++++++ src/platform_impl/linux/wayland/state.rs | 8 +- tests/wayland_pinch.rs | 68 ++++++++++++ 7 files changed, 266 insertions(+), 3 deletions(-) create mode 100644 src/platform_impl/linux/wayland/seat/pointer/pinch_state.rs create mode 100644 src/platform_impl/linux/wayland/seat/pointer/pointer_gesture.rs create mode 100644 tests/wayland_pinch.rs diff --git a/src/event.rs b/src/event.rs index d7dd3159..48e6a6b1 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, 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..6ab552d5 --- /dev/null +++ b/src/platform_impl/linux/wayland/seat/pointer/pinch_state.rs @@ -0,0 +1,54 @@ +//! PdfCraft patch: translate Wayland's cumulative pinch scale into winit's log deltas. + +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, +} + +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 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..80441e1d --- /dev/null +++ b/src/platform_impl/linux/wayland/seat/pointer/pointer_gesture.rs @@ -0,0 +1,102 @@ +//! PdfCraft patch (#1095): pinch support adapted from rust-windowing/winit#4338. +//! Only pinch is backported; scale deltas use the logarithmic units egui-winit consumes. + +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}; + +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, event: Option>) { + if let Some(event) = event { + // The window may have closed while a gesture was in progress. + if state.windows.get_mut().contains_key(&event.window) { + state.events_sink.push_window_event( + WindowEvent::PinchGesture { + device_id: crate::event::DeviceId(crate::platform_impl::DeviceId::Wayland( + DeviceId, + )), + 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)); + } + } +} + +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 event = match event { + Event::Begin { surface, fingers, .. } => { + // Decoration/subsurface gestures must not zoom the document. + let window = surface + .data::() + .filter(|data| data.parent_surface().is_none()) + .map(|_| make_wid(&surface)); + pinch.begin(window, fingers) + }, + Event::Update { scale, .. } => pinch.update(scale), + Event::End { cancelled, .. } => pinch.end(cancelled != 0), + _ => None, + }; + emit(state, event); + } +} + +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 74c44574..c59fd1cb 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, }; use crate::platform_impl::wayland::types::kwin_blur::KWinBlurManager; use crate::platform_impl::wayland::types::wp_fractional_scaling::FractionalScalingManager; @@ -118,6 +118,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, @@ -251,6 +254,7 @@ impl WinitState { viewporter_state, fractional_scaling_manager, 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..c3a2acef --- /dev/null +++ b/tests/wayland_pinch.rs @@ -0,0 +1,68 @@ +#![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()); + } +} From 0bc1a9b7b239d62ee3cb4ce498aa6bf3af208b5a Mon Sep 17 00:00:00 2001 From: tomasmark79 Date: Sat, 31 Oct 2026 08:58:13 +0100 Subject: [PATCH 2/3] docs: record the tested pinch-only W16 port and removal condition --- PATCHES.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/PATCHES.md b/PATCHES.md index c100c83d..833c33f4 100644 --- a/PATCHES.md +++ b/PATCHES.md @@ -221,3 +221,18 @@ 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 subset: native Wayland pinch + +PR #20 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. Rotation and swipe +remain outside this cut. Remove the port when an upstream 0.30-compatible release +provides equivalent pinch delivery and scale/lifecycle handling. + +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. From 934cb6a6406c794772c66654742f10445f32d0d9 Mon Sep 17 00:00:00 2001 From: tomasmark79 Date: Sun, 1 Nov 2026 08:50:49 +0100 Subject: [PATCH 3/3] docs: document pinch backport provenance and GNOME hardware validation Record the immutable upstream reference and 0.30 adaptation, protocol version and phase handling, and current-head contributor checks with no axis events during captured gestures. Keep KDE/Sway and physical teardown explicitly unverified. --- PATCHES.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/PATCHES.md b/PATCHES.md index 833c33f4..97c192bd 100644 --- a/PATCHES.md +++ b/PATCHES.md @@ -224,6 +224,28 @@ qualification gates. Human review and merge are required. ## W16 subset: native Wayland pinch +### 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 pinch-only +backport, not a clean cherry-pick: upstream uses the split 0.31 window stack and +also delivers pan and rotation; 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. Rotation, 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 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 @@ -231,8 +253,30 @@ or seat removal without replacing the newer drag/IME teardown. Rotation and swip remain outside this cut. Remove the port when an upstream 0.30-compatible release provides equivalent pinch delivery and scale/lifecycle handling. -Validation: 207 library tests and four `wayland_pinch` regressions passed with +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.