Implement touch interactive move
Browse files at this commit26ab4dfb87550b2f8ceed5d11be614a239703fd7Parent: e887ee94 changed files +200 -18
- src/handlers/xdg_shell.rs +43 -17
- src/input/mod.rs +19 -1
- src/input/move_grab.rs +2 -0
- src/input/touch_move_grab.rs +136 -0
src/handlers/xdg_shell.rsmodified+43 -17
| @@ -6,7 +6,7 @@ use smithay::desktop::{ | |||
| 6 | PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window, | 6 | PopupKeyboardGrab, PopupKind, PopupManager, PopupPointerGrab, PopupUngrabStrategy, Window, |
| 7 | WindowSurfaceType, | 7 | WindowSurfaceType, |
| 8 | }; | 8 | }; |
| 9 | -use smithay::input::pointer::{Focus, PointerGrab}; | 9 | +use smithay::input::pointer::Focus; |
| 10 | use smithay::output::Output; | 10 | use smithay::output::Output; |
| 11 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; | 11 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; |
| 12 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_positioner::ConstraintAdjustment; | 12 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_positioner::ConstraintAdjustment; |
| @@ -38,7 +38,8 @@ use tracing::field::Empty; | |||
| 38 | 38 | ||
| 39 | use crate::input::move_grab::MoveGrab; | 39 | use crate::input::move_grab::MoveGrab; |
| 40 | use crate::input::resize_grab::ResizeGrab; | 40 | use crate::input::resize_grab::ResizeGrab; |
| 41 | -use crate::input::DOUBLE_CLICK_TIME; | 41 | +use crate::input::touch_move_grab::TouchMoveGrab; |
| 42 | +use crate::input::{PointerOrTouchStartData, DOUBLE_CLICK_TIME}; | ||
| 42 | use crate::layout::workspace::ColumnWidth; | 43 | use crate::layout::workspace::ColumnWidth; |
| 43 | use crate::niri::{PopupGrabState, State}; | 44 | use crate::niri::{PopupGrabState, State}; |
| 44 | use crate::utils::transaction::Transaction; | 45 | use crate::utils::transaction::Transaction; |
| @@ -67,24 +68,39 @@ impl XdgShellHandler for State { | |||
| 67 | } | 68 | } |
| 68 | 69 | ||
| 69 | fn move_request(&mut self, surface: ToplevelSurface, _seat: WlSeat, serial: Serial) { | 70 | fn move_request(&mut self, surface: ToplevelSurface, _seat: WlSeat, serial: Serial) { |
| 71 | + let wl_surface = surface.wl_surface(); | ||
| 72 | + | ||
| 73 | + let mut grab_start_data = None; | ||
| 74 | + | ||
| 75 | + // See if this comes from a pointer grab. | ||
| 70 | let pointer = self.niri.seat.get_pointer().unwrap(); | 76 | let pointer = self.niri.seat.get_pointer().unwrap(); |
| 71 | - if !pointer.has_grab(serial) { | 77 | + if pointer.has_grab(serial) { |
| 72 | - return; | 78 | + if let Some(start_data) = pointer.grab_start_data() { |
| 79 | + if let Some((focus, _)) = &start_data.focus { | ||
| 80 | + if focus.id().same_client_as(&wl_surface.id()) { | ||
| 81 | + grab_start_data = Some(PointerOrTouchStartData::Pointer(start_data)); | ||
| 82 | + } | ||
| 83 | + } | ||
| 84 | + } | ||
| 73 | } | 85 | } |
| 74 | 86 | ||
| 75 | - let Some(start_data) = pointer.grab_start_data() else { | 87 | + // See if this comes from a touch grab. |
| 76 | - return; | 88 | + if let Some(touch) = self.niri.seat.get_touch() { |
| 77 | - }; | 89 | + if touch.has_grab(serial) { |
| 90 | + if let Some(start_data) = touch.grab_start_data() { | ||
| 91 | + if let Some((focus, _)) = &start_data.focus { | ||
| 92 | + if focus.id().same_client_as(&wl_surface.id()) { | ||
| 93 | + grab_start_data = Some(PointerOrTouchStartData::Touch(start_data)); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + } | ||
| 97 | + } | ||
| 98 | + } | ||
| 78 | 99 | ||
| 79 | - let Some((focus, _)) = &start_data.focus else { | 100 | + let Some(start_data) = grab_start_data else { |
| 80 | return; | 101 | return; |
| 81 | }; | 102 | }; |
| 82 | 103 | ||
| 83 | - let wl_surface = surface.wl_surface(); | ||
| 84 | - if !focus.id().same_client_as(&wl_surface.id()) { | ||
| 85 | - return; | ||
| 86 | - } | ||
| 87 | - | ||
| 88 | let Some((mapped, output)) = self.niri.layout.find_window_and_output(wl_surface) else { | 104 | let Some((mapped, output)) = self.niri.layout.find_window_and_output(wl_surface) else { |
| 89 | return; | 105 | return; |
| 90 | }; | 106 | }; |
| @@ -100,7 +116,7 @@ impl XdgShellHandler for State { | |||
| 100 | .loc | 116 | .loc |
| 101 | .to_f64(); | 117 | .to_f64(); |
| 102 | 118 | ||
| 103 | - let pos_within_output = start_data.location - output_pos; | 119 | + let pos_within_output = start_data.location() - output_pos; |
| 104 | 120 | ||
| 105 | if !self | 121 | if !self |
| 106 | .niri | 122 | .niri |
| @@ -110,10 +126,20 @@ impl XdgShellHandler for State { | |||
| 110 | return; | 126 | return; |
| 111 | } | 127 | } |
| 112 | 128 | ||
| 113 | - let grab = MoveGrab::new(start_data, window.clone()); | 129 | + match start_data { |
| 130 | + PointerOrTouchStartData::Pointer(start_data) => { | ||
| 131 | + let grab = MoveGrab::new(start_data, window); | ||
| 132 | + pointer.set_grab(self, grab, serial, Focus::Clear); | ||
| 133 | + self.niri.pointer_grab_ongoing = true; | ||
| 134 | + } | ||
| 135 | + PointerOrTouchStartData::Touch(start_data) => { | ||
| 136 | + let touch = self.niri.seat.get_touch().unwrap(); | ||
| 137 | + let grab = TouchMoveGrab::new(start_data, window); | ||
| 138 | + touch.set_grab(self, grab, serial); | ||
| 139 | + } | ||
| 140 | + } | ||
| 114 | 141 | ||
| 115 | - pointer.set_grab(self, grab, serial, Focus::Clear); | 142 | + self.niri.queue_redraw(&output); |
| 116 | - self.niri.pointer_grab_ongoing = true; | ||
| 117 | } | 143 | } |
| 118 | 144 | ||
| 119 | fn resize_request( | 145 | fn resize_request( |
src/input/mod.rsmodified+19 -1
| @@ -24,7 +24,10 @@ use smithay::input::pointer::{ | |||
| 24 | GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent, | 24 | GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent, |
| 25 | GrabStartData as PointerGrabStartData, MotionEvent, RelativeMotionEvent, | 25 | GrabStartData as PointerGrabStartData, MotionEvent, RelativeMotionEvent, |
| 26 | }; | 26 | }; |
| 27 | -use smithay::input::touch::{DownEvent, MotionEvent as TouchMotionEvent, UpEvent}; | 27 | +use smithay::input::touch::{ |
| 28 | + DownEvent, GrabStartData as TouchGrabStartData, MotionEvent as TouchMotionEvent, UpEvent, | ||
| 29 | +}; | ||
| 30 | +use smithay::input::SeatHandler; | ||
| 28 | use smithay::utils::{Logical, Point, Rectangle, Transform, SERIAL_COUNTER}; | 31 | use smithay::utils::{Logical, Point, Rectangle, Transform, SERIAL_COUNTER}; |
| 29 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint}; | 32 | use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerConstraint}; |
| 30 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; | 33 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| @@ -42,6 +45,7 @@ pub mod resize_grab; | |||
| 42 | pub mod scroll_tracker; | 45 | pub mod scroll_tracker; |
| 43 | pub mod spatial_movement_grab; | 46 | pub mod spatial_movement_grab; |
| 44 | pub mod swipe_tracker; | 47 | pub mod swipe_tracker; |
| 48 | +pub mod touch_move_grab; | ||
| 45 | 49 | ||
| 46 | pub const DOUBLE_CLICK_TIME: Duration = Duration::from_millis(400); | 50 | pub const DOUBLE_CLICK_TIME: Duration = Duration::from_millis(400); |
| 47 | 51 | ||
| @@ -56,6 +60,20 @@ pub struct TabletData { | |||
| 56 | pub aspect_ratio: f64, | 60 | pub aspect_ratio: f64, |
| 57 | } | 61 | } |
| 58 | 62 | ||
| 63 | +pub enum PointerOrTouchStartData<D: SeatHandler> { | ||
| 64 | + Pointer(PointerGrabStartData<D>), | ||
| 65 | + Touch(TouchGrabStartData<D>), | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +impl<D: SeatHandler> PointerOrTouchStartData<D> { | ||
| 69 | + pub fn location(&self) -> Point<f64, Logical> { | ||
| 70 | + match self { | ||
| 71 | + PointerOrTouchStartData::Pointer(x) => x.location, | ||
| 72 | + PointerOrTouchStartData::Touch(x) => x.location, | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | +} | ||
| 76 | + | ||
| 59 | impl State { | 77 | impl State { |
| 60 | pub fn process_input_event<I: InputBackend + 'static>(&mut self, event: InputEvent<I>) | 78 | pub fn process_input_event<I: InputBackend + 'static>(&mut self, event: InputEvent<I>) |
| 61 | where | 79 | where |
src/input/move_grab.rsmodified+2 -0
| @@ -73,6 +73,8 @@ impl PointerGrab<State> for MoveGrab { | |||
| 73 | false, | 73 | false, |
| 74 | ); | 74 | ); |
| 75 | } | 75 | } |
| 76 | + // FIXME: only redraw the previous and the new output. | ||
| 77 | + data.niri.queue_redraw_all(); | ||
| 76 | return; | 78 | return; |
| 77 | } | 79 | } |
| 78 | } else { | 80 | } else { |
src/input/touch_move_grab.rsadded+136 -0
| @@ -0,0 +1,136 @@ | |||
| 1 | +use smithay::desktop::Window; | ||
| 2 | +use smithay::input::touch::{ | ||
| 3 | + DownEvent, GrabStartData as TouchGrabStartData, MotionEvent, OrientationEvent, ShapeEvent, | ||
| 4 | + TouchGrab, TouchInnerHandle, UpEvent, | ||
| 5 | +}; | ||
| 6 | +use smithay::input::SeatHandler; | ||
| 7 | +use smithay::utils::{IsAlive, Logical, Point, Serial}; | ||
| 8 | + | ||
| 9 | +use crate::niri::State; | ||
| 10 | + | ||
| 11 | +pub struct TouchMoveGrab { | ||
| 12 | + start_data: TouchGrabStartData<State>, | ||
| 13 | + last_location: Point<f64, Logical>, | ||
| 14 | + window: Window, | ||
| 15 | +} | ||
| 16 | + | ||
| 17 | +impl TouchMoveGrab { | ||
| 18 | + pub fn new(start_data: TouchGrabStartData<State>, window: Window) -> Self { | ||
| 19 | + Self { | ||
| 20 | + last_location: start_data.location, | ||
| 21 | + start_data, | ||
| 22 | + window, | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + fn on_ungrab(&mut self, state: &mut State) { | ||
| 27 | + state.niri.layout.interactive_move_end(&self.window); | ||
| 28 | + // FIXME: only redraw the window output. | ||
| 29 | + state.niri.queue_redraw_all(); | ||
| 30 | + } | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +impl TouchGrab<State> for TouchMoveGrab { | ||
| 34 | + fn down( | ||
| 35 | + &mut self, | ||
| 36 | + data: &mut State, | ||
| 37 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 38 | + _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, | ||
| 39 | + event: &DownEvent, | ||
| 40 | + seq: Serial, | ||
| 41 | + ) { | ||
| 42 | + handle.down(data, None, event, seq); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + fn up( | ||
| 46 | + &mut self, | ||
| 47 | + data: &mut State, | ||
| 48 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 49 | + event: &UpEvent, | ||
| 50 | + seq: Serial, | ||
| 51 | + ) { | ||
| 52 | + handle.up(data, event, seq); | ||
| 53 | + | ||
| 54 | + if event.slot != self.start_data.slot { | ||
| 55 | + return; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + handle.unset_grab(self, data); | ||
| 59 | + } | ||
| 60 | + | ||
| 61 | + fn motion( | ||
| 62 | + &mut self, | ||
| 63 | + data: &mut State, | ||
| 64 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 65 | + _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, | ||
| 66 | + event: &MotionEvent, | ||
| 67 | + seq: Serial, | ||
| 68 | + ) { | ||
| 69 | + handle.motion(data, None, event, seq); | ||
| 70 | + | ||
| 71 | + if event.slot != self.start_data.slot { | ||
| 72 | + return; | ||
| 73 | + } | ||
| 74 | + | ||
| 75 | + if self.window.alive() { | ||
| 76 | + if let Some((output, pos_within_output)) = data.niri.output_under(event.location) { | ||
| 77 | + let output = output.clone(); | ||
| 78 | + let event_delta = event.location - self.last_location; | ||
| 79 | + self.last_location = event.location; | ||
| 80 | + let ongoing = data.niri.layout.interactive_move_update( | ||
| 81 | + &self.window, | ||
| 82 | + event_delta, | ||
| 83 | + output, | ||
| 84 | + pos_within_output, | ||
| 85 | + ); | ||
| 86 | + if ongoing { | ||
| 87 | + // FIXME: only redraw the previous and the new output. | ||
| 88 | + data.niri.queue_redraw_all(); | ||
| 89 | + return; | ||
| 90 | + } | ||
| 91 | + } else { | ||
| 92 | + return; | ||
| 93 | + } | ||
| 94 | + } | ||
| 95 | + | ||
| 96 | + // The move is no longer ongoing. | ||
| 97 | + handle.unset_grab(self, data); | ||
| 98 | + } | ||
| 99 | + | ||
| 100 | + fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>, seq: Serial) { | ||
| 101 | + handle.frame(data, seq); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>, seq: Serial) { | ||
| 105 | + handle.cancel(data, seq); | ||
| 106 | + handle.unset_grab(self, data); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + fn shape( | ||
| 110 | + &mut self, | ||
| 111 | + data: &mut State, | ||
| 112 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 113 | + event: &ShapeEvent, | ||
| 114 | + seq: Serial, | ||
| 115 | + ) { | ||
| 116 | + handle.shape(data, event, seq); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + fn orientation( | ||
| 120 | + &mut self, | ||
| 121 | + data: &mut State, | ||
| 122 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 123 | + event: &OrientationEvent, | ||
| 124 | + seq: Serial, | ||
| 125 | + ) { | ||
| 126 | + handle.orientation(data, event, seq); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + fn start_data(&self) -> &TouchGrabStartData<State> { | ||
| 130 | + &self.start_data | ||
| 131 | + } | ||
| 132 | + | ||
| 133 | + fn unset(&mut self, data: &mut State) { | ||
| 134 | + self.on_ungrab(data); | ||
| 135 | + } | ||
| 136 | +} | ||