|
| 1 | +use std::ops::Deref; |
| 2 | +use std::sync::Mutex; |
| 3 | + |
| 4 | +use dpi::{LogicalPosition, PhysicalPosition}; |
| 5 | +use sctk::compositor::SurfaceData; |
| 6 | +use sctk::globals::GlobalData; |
| 7 | +use sctk::reexports::client::globals::{BindError, GlobalList}; |
| 8 | +use sctk::reexports::client::{delegate_dispatch, Connection, Dispatch, Proxy, QueueHandle}; |
| 9 | +use sctk::reexports::protocols::wp::pointer_gestures::zv1::client::zwp_pointer_gesture_pinch_v1::{ |
| 10 | + Event, ZwpPointerGesturePinchV1, |
| 11 | +}; |
| 12 | +use sctk::reexports::protocols::wp::pointer_gestures::zv1::client::zwp_pointer_gestures_v1::ZwpPointerGesturesV1; |
| 13 | +use winit_core::event::{TouchPhase, WindowEvent}; |
| 14 | +use winit_core::window::WindowId; |
| 15 | + |
| 16 | +use crate::state::WinitState; |
| 17 | + |
| 18 | +/// Wrapper around the pointer gesture. |
| 19 | +#[derive(Debug)] |
| 20 | +pub struct PointerGesturesState { |
| 21 | + pointer_gestures: ZwpPointerGesturesV1, |
| 22 | +} |
| 23 | + |
| 24 | +impl PointerGesturesState { |
| 25 | + /// Create a new pointer gesture |
| 26 | + pub fn new( |
| 27 | + globals: &GlobalList, |
| 28 | + queue_handle: &QueueHandle<WinitState>, |
| 29 | + ) -> Result<Self, BindError> { |
| 30 | + let pointer_gestures = globals.bind(queue_handle, 1..=1, GlobalData)?; |
| 31 | + Ok(Self { pointer_gestures }) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Debug, Default)] |
| 36 | +pub struct PointerGestureData { |
| 37 | + inner: Mutex<PointerGestureDataInner>, |
| 38 | +} |
| 39 | + |
| 40 | +#[derive(Debug)] |
| 41 | +pub struct PointerGestureDataInner { |
| 42 | + window_id: Option<WindowId>, |
| 43 | + previous_pinch: f64, |
| 44 | +} |
| 45 | + |
| 46 | +impl Default for PointerGestureDataInner { |
| 47 | + fn default() -> Self { |
| 48 | + Self { window_id: Default::default(), previous_pinch: 1.0 } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl Deref for PointerGesturesState { |
| 53 | + type Target = ZwpPointerGesturesV1; |
| 54 | + |
| 55 | + fn deref(&self) -> &Self::Target { |
| 56 | + &self.pointer_gestures |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl Dispatch<ZwpPointerGesturesV1, GlobalData, WinitState> for PointerGesturesState { |
| 61 | + fn event( |
| 62 | + _state: &mut WinitState, |
| 63 | + _proxy: &ZwpPointerGesturesV1, |
| 64 | + _event: <ZwpPointerGesturesV1 as wayland_client::Proxy>::Event, |
| 65 | + _data: &GlobalData, |
| 66 | + _conn: &Connection, |
| 67 | + _qhandle: &QueueHandle<WinitState>, |
| 68 | + ) { |
| 69 | + unreachable!("zwp_pointer_gestures_v1 has no events") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl Dispatch<ZwpPointerGesturePinchV1, PointerGestureData, WinitState> for PointerGesturesState { |
| 74 | + fn event( |
| 75 | + state: &mut WinitState, |
| 76 | + _proxy: &ZwpPointerGesturePinchV1, |
| 77 | + event: <ZwpPointerGesturePinchV1 as Proxy>::Event, |
| 78 | + data: &PointerGestureData, |
| 79 | + _conn: &Connection, |
| 80 | + _qhandle: &QueueHandle<WinitState>, |
| 81 | + ) { |
| 82 | + let mut pointer_gesture_data = data.inner.lock().unwrap(); |
| 83 | + let (window_id, phase, pan_delta, pinch_delta, rotation_delta) = match event { |
| 84 | + Event::Begin { surface, fingers, .. } => { |
| 85 | + // We only support two fingers for now. |
| 86 | + if fingers != 2 { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // Don't handle events from a subsurface. |
| 91 | + if !surface |
| 92 | + .data::<SurfaceData>() |
| 93 | + .is_some_and(|data| data.parent_surface().is_none()) |
| 94 | + { |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + let window_id = crate::make_wid(&surface); |
| 99 | + |
| 100 | + pointer_gesture_data.window_id = Some(window_id); |
| 101 | + pointer_gesture_data.previous_pinch = 1.; |
| 102 | + |
| 103 | + (window_id, TouchPhase::Started, PhysicalPosition::new(0., 0.), 0., 0.) |
| 104 | + }, |
| 105 | + Event::Update { dx, dy, scale: pinch, rotation, .. } => { |
| 106 | + let window_id = match pointer_gesture_data.window_id { |
| 107 | + Some(window_id) => window_id, |
| 108 | + _ => return, |
| 109 | + }; |
| 110 | + |
| 111 | + let scale_factor = match state.windows.get_mut().get_mut(&window_id) { |
| 112 | + Some(window) => window.lock().unwrap().scale_factor(), |
| 113 | + None => return, |
| 114 | + }; |
| 115 | + |
| 116 | + let pan_delta = |
| 117 | + LogicalPosition::new(dx as f32, dy as f32).to_physical(scale_factor); |
| 118 | + |
| 119 | + let pinch_delta = pinch - pointer_gesture_data.previous_pinch; |
| 120 | + pointer_gesture_data.previous_pinch = pinch; |
| 121 | + |
| 122 | + // Wayland provides rotation in degrees cw, opposite of winit's degrees ccw. |
| 123 | + let rotation_delta = -rotation as f32; |
| 124 | + (window_id, TouchPhase::Moved, pan_delta, pinch_delta, rotation_delta) |
| 125 | + }, |
| 126 | + Event::End { cancelled, .. } => { |
| 127 | + let window_id = match pointer_gesture_data.window_id { |
| 128 | + Some(window_id) => window_id, |
| 129 | + _ => return, |
| 130 | + }; |
| 131 | + |
| 132 | + // Reset the state. |
| 133 | + *pointer_gesture_data = Default::default(); |
| 134 | + |
| 135 | + let phase = if cancelled == 0 { TouchPhase::Ended } else { TouchPhase::Cancelled }; |
| 136 | + (window_id, phase, PhysicalPosition::new(0., 0.), 0., 0.) |
| 137 | + }, |
| 138 | + _ => unreachable!("Unknown event {event:?}"), |
| 139 | + }; |
| 140 | + |
| 141 | + // The chance of only one of these events being necessary is extremely small, |
| 142 | + // so it is easier to just send all three |
| 143 | + state.events_sink.push_window_event( |
| 144 | + WindowEvent::PanGesture { device_id: None, delta: pan_delta, phase }, |
| 145 | + window_id, |
| 146 | + ); |
| 147 | + state.events_sink.push_window_event( |
| 148 | + WindowEvent::PinchGesture { device_id: None, delta: pinch_delta, phase }, |
| 149 | + window_id, |
| 150 | + ); |
| 151 | + state.events_sink.push_window_event( |
| 152 | + WindowEvent::RotationGesture { device_id: None, delta: rotation_delta, phase }, |
| 153 | + window_id, |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +delegate_dispatch!(WinitState: [ZwpPointerGesturesV1: GlobalData] => PointerGesturesState); |
| 159 | +delegate_dispatch!(WinitState: [ZwpPointerGesturePinchV1: PointerGestureData] => PointerGesturesState); |
0 commit comments