overview: Add touchscreen gestures
Browse files at this commitb8a9be542fe6c6e99b1eac159188166f2bf2e82eParent: 59de6912 changed files +312 -3
- src/input/mod.rs +38 -3
- src/input/touch_overview_grab.rs +274 -0
src/input/mod.rsmodified+38 -3
| @@ -35,6 +35,7 @@ use smithay::wayland::pointer_constraints::{with_pointer_constraint, PointerCons | |||
| 35 | use smithay::wayland::selection::data_device::DnDGrab; | 35 | use smithay::wayland::selection::data_device::DnDGrab; |
| 36 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; | 36 | use smithay::wayland::tablet_manager::{TabletDescriptor, TabletSeatTrait}; |
| 37 | use touch_move_grab::TouchMoveGrab; | 37 | use touch_move_grab::TouchMoveGrab; |
| 38 | +use touch_overview_grab::TouchOverviewGrab; | ||
| 38 | 39 | ||
| 39 | use self::move_grab::MoveGrab; | 40 | use self::move_grab::MoveGrab; |
| 40 | use self::resize_grab::ResizeGrab; | 41 | use self::resize_grab::ResizeGrab; |
| @@ -56,6 +57,7 @@ pub mod scroll_tracker; | |||
| 56 | pub mod spatial_movement_grab; | 57 | pub mod spatial_movement_grab; |
| 57 | pub mod swipe_tracker; | 58 | pub mod swipe_tracker; |
| 58 | pub mod touch_move_grab; | 59 | pub mod touch_move_grab; |
| 60 | +pub mod touch_overview_grab; | ||
| 59 | pub mod touch_resize_grab; | 61 | pub mod touch_resize_grab; |
| 60 | 62 | ||
| 61 | use backend_ext::{NiriInputBackend as InputBackend, NiriInputDevice as _}; | 63 | use backend_ext::{NiriInputBackend as InputBackend, NiriInputDevice as _}; |
| @@ -3467,12 +3469,45 @@ impl State { | |||
| 3467 | let mod_key = self.backend.mod_key(&self.niri.config.borrow()); | 3469 | let mod_key = self.backend.mod_key(&self.niri.config.borrow()); |
| 3468 | 3470 | ||
| 3469 | if !handle.is_grabbed() { | 3471 | if !handle.is_grabbed() { |
| 3470 | - if let Some((window, _)) = under.window { | 3472 | + let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); |
| 3473 | + let mods = modifiers_from_state(mods); | ||
| 3474 | + let mod_down = mods.contains(mod_key.to_modifiers()); | ||
| 3475 | + | ||
| 3476 | + if self.niri.layout.is_overview_open() && !mod_down && under.layer.is_none() { | ||
| 3477 | + let (output, pos_within_output) = self.niri.output_under(touch_location).unwrap(); | ||
| 3478 | + let output = output.clone(); | ||
| 3479 | + | ||
| 3480 | + let mut matched_narrow = true; | ||
| 3481 | + let mut ws = self.niri.workspace_under(false, touch_location); | ||
| 3482 | + if ws.is_none() { | ||
| 3483 | + matched_narrow = false; | ||
| 3484 | + ws = self.niri.workspace_under(true, touch_location); | ||
| 3485 | + } | ||
| 3486 | + let ws_id = ws.map(|(_, ws)| ws.id()); | ||
| 3487 | + | ||
| 3488 | + let mapped = self.niri.window_under(touch_location); | ||
| 3489 | + let window = mapped.map(|mapped| mapped.window.clone()); | ||
| 3490 | + | ||
| 3491 | + let start_data = TouchGrabStartData { | ||
| 3492 | + focus: None, | ||
| 3493 | + slot: evt.slot(), | ||
| 3494 | + location: touch_location, | ||
| 3495 | + }; | ||
| 3496 | + let start_timestamp = Duration::from_micros(evt.time()); | ||
| 3497 | + let grab = TouchOverviewGrab::new( | ||
| 3498 | + start_data, | ||
| 3499 | + start_timestamp, | ||
| 3500 | + output, | ||
| 3501 | + pos_within_output, | ||
| 3502 | + ws_id, | ||
| 3503 | + matched_narrow, | ||
| 3504 | + window, | ||
| 3505 | + ); | ||
| 3506 | + handle.set_grab(self, grab, serial); | ||
| 3507 | + } else if let Some((window, _)) = under.window { | ||
| 3471 | self.niri.layout.activate_window(&window); | 3508 | self.niri.layout.activate_window(&window); |
| 3472 | 3509 | ||
| 3473 | // Check if we need to start an interactive move. | 3510 | // Check if we need to start an interactive move. |
| 3474 | - let mods = self.niri.seat.get_keyboard().unwrap().modifier_state(); | ||
| 3475 | - let mod_down = modifiers_from_state(mods).contains(mod_key.to_modifiers()); | ||
| 3476 | if mod_down { | 3511 | if mod_down { |
| 3477 | let (output, pos_within_output) = | 3512 | let (output, pos_within_output) = |
| 3478 | self.niri.output_under(touch_location).unwrap(); | 3513 | self.niri.output_under(touch_location).unwrap(); |
src/input/touch_overview_grab.rsadded+274 -0
| @@ -0,0 +1,274 @@ | |||
| 1 | +use std::time::Duration; | ||
| 2 | + | ||
| 3 | +use smithay::desktop::Window; | ||
| 4 | +use smithay::input::touch::{ | ||
| 5 | + DownEvent, GrabStartData as TouchGrabStartData, MotionEvent, OrientationEvent, ShapeEvent, | ||
| 6 | + TouchGrab, TouchInnerHandle, UpEvent, | ||
| 7 | +}; | ||
| 8 | +use smithay::input::SeatHandler; | ||
| 9 | +use smithay::output::Output; | ||
| 10 | +use smithay::utils::{IsAlive, Logical, Point, Serial}; | ||
| 11 | + | ||
| 12 | +use crate::layout::workspace::{Workspace, WorkspaceId}; | ||
| 13 | +use crate::niri::State; | ||
| 14 | +use crate::window::Mapped; | ||
| 15 | + | ||
| 16 | +// When the touch is stationary for this much time, it becomes an interactive move. | ||
| 17 | +const INTERACTIVE_MOVE_THRESHOLD: Duration = Duration::from_millis(500); | ||
| 18 | + | ||
| 19 | +pub struct TouchOverviewGrab { | ||
| 20 | + start_data: TouchGrabStartData<State>, | ||
| 21 | + start_timestamp: Duration, | ||
| 22 | + last_location: Point<f64, Logical>, | ||
| 23 | + output: Output, | ||
| 24 | + start_pos_within_output: Point<f64, Logical>, | ||
| 25 | + workspace_id: Option<WorkspaceId>, | ||
| 26 | + workspace_matched_narrow: bool, | ||
| 27 | + window: Option<Window>, | ||
| 28 | + gesture: GestureState, | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +#[derive(Debug, Clone, Copy)] | ||
| 32 | +enum GestureState { | ||
| 33 | + Recognizing, | ||
| 34 | + ViewOffset, | ||
| 35 | + WorkspaceSwitch, | ||
| 36 | + InteractiveMove, | ||
| 37 | +} | ||
| 38 | + | ||
| 39 | +impl TouchOverviewGrab { | ||
| 40 | + pub fn new( | ||
| 41 | + start_data: TouchGrabStartData<State>, | ||
| 42 | + start_timestamp: Duration, | ||
| 43 | + output: Output, | ||
| 44 | + start_pos_within_output: Point<f64, Logical>, | ||
| 45 | + workspace_id: Option<WorkspaceId>, | ||
| 46 | + workspace_matched_narrow: bool, | ||
| 47 | + window: Option<Window>, | ||
| 48 | + ) -> Self { | ||
| 49 | + Self { | ||
| 50 | + last_location: start_data.location, | ||
| 51 | + start_timestamp, | ||
| 52 | + start_data, | ||
| 53 | + output, | ||
| 54 | + start_pos_within_output, | ||
| 55 | + workspace_id, | ||
| 56 | + workspace_matched_narrow, | ||
| 57 | + window, | ||
| 58 | + gesture: GestureState::Recognizing, | ||
| 59 | + } | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + fn on_ungrab(&mut self, state: &mut State) { | ||
| 63 | + let layout = &mut state.niri.layout; | ||
| 64 | + match self.gesture { | ||
| 65 | + GestureState::Recognizing => { | ||
| 66 | + // Tap to activate. | ||
| 67 | + layout.focus_output(&self.output); | ||
| 68 | + | ||
| 69 | + // Activate the workspace if necessary. | ||
| 70 | + if self.window.is_some() || self.workspace_matched_narrow { | ||
| 71 | + // When activating a window, we want to activate the window's current | ||
| 72 | + // workspace. Otherwise, find the workspace that we tapped on. | ||
| 73 | + let ws_matches = |ws: &Workspace<Mapped>| { | ||
| 74 | + if let Some(window) = &self.window { | ||
| 75 | + ws.has_window(window) | ||
| 76 | + } else if let Some(ws_id) = self.workspace_id { | ||
| 77 | + ws.id() == ws_id | ||
| 78 | + } else { | ||
| 79 | + false | ||
| 80 | + } | ||
| 81 | + }; | ||
| 82 | + | ||
| 83 | + let ws_idx = if let Some((Some(mon), ws_idx, _)) = | ||
| 84 | + layout.workspaces().find(|(_, _, ws)| ws_matches(ws)) | ||
| 85 | + { | ||
| 86 | + // The workspace could've moved to a different output in the meantime. | ||
| 87 | + (*mon.output() == self.output).then_some(ws_idx) | ||
| 88 | + } else { | ||
| 89 | + None | ||
| 90 | + }; | ||
| 91 | + | ||
| 92 | + if let Some(ws_idx) = ws_idx { | ||
| 93 | + layout.toggle_overview_to_workspace(ws_idx); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + if let Some(window) = self.window.as_ref() { | ||
| 98 | + layout.activate_window(window); | ||
| 99 | + } | ||
| 100 | + } | ||
| 101 | + GestureState::ViewOffset => { | ||
| 102 | + layout.view_offset_gesture_end(Some(false)); | ||
| 103 | + } | ||
| 104 | + GestureState::WorkspaceSwitch => { | ||
| 105 | + layout.workspace_switch_gesture_end(Some(false)); | ||
| 106 | + } | ||
| 107 | + GestureState::InteractiveMove => { | ||
| 108 | + layout.interactive_move_end(self.window.as_ref().unwrap()); | ||
| 109 | + } | ||
| 110 | + }; | ||
| 111 | + | ||
| 112 | + state.niri.queue_redraw_all(); | ||
| 113 | + } | ||
| 114 | +} | ||
| 115 | + | ||
| 116 | +impl TouchGrab<State> for TouchOverviewGrab { | ||
| 117 | + fn down( | ||
| 118 | + &mut self, | ||
| 119 | + data: &mut State, | ||
| 120 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 121 | + _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, | ||
| 122 | + event: &DownEvent, | ||
| 123 | + seq: Serial, | ||
| 124 | + ) { | ||
| 125 | + handle.down(data, None, event, seq); | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + fn up( | ||
| 129 | + &mut self, | ||
| 130 | + data: &mut State, | ||
| 131 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 132 | + event: &UpEvent, | ||
| 133 | + seq: Serial, | ||
| 134 | + ) { | ||
| 135 | + handle.up(data, event, seq); | ||
| 136 | + | ||
| 137 | + if event.slot != self.start_data.slot { | ||
| 138 | + return; | ||
| 139 | + } | ||
| 140 | + | ||
| 141 | + handle.unset_grab(self, data); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + fn motion( | ||
| 145 | + &mut self, | ||
| 146 | + data: &mut State, | ||
| 147 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 148 | + _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, | ||
| 149 | + event: &MotionEvent, | ||
| 150 | + seq: Serial, | ||
| 151 | + ) { | ||
| 152 | + handle.motion(data, None, event, seq); | ||
| 153 | + | ||
| 154 | + if event.slot != self.start_data.slot { | ||
| 155 | + return; | ||
| 156 | + } | ||
| 157 | + | ||
| 158 | + let timestamp = Duration::from_millis(u64::from(event.time)); | ||
| 159 | + let layout = &mut data.niri.layout; | ||
| 160 | + | ||
| 161 | + // Check if we should become interactive move. | ||
| 162 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 163 | + if let Some(window) = self.window.as_ref().filter(|win| win.alive()) { | ||
| 164 | + let passed = timestamp.saturating_sub(self.start_timestamp); | ||
| 165 | + if INTERACTIVE_MOVE_THRESHOLD <= passed | ||
| 166 | + && layout.interactive_move_begin( | ||
| 167 | + window.clone(), | ||
| 168 | + &self.output, | ||
| 169 | + self.start_pos_within_output, | ||
| 170 | + ) | ||
| 171 | + { | ||
| 172 | + self.gesture = GestureState::InteractiveMove; | ||
| 173 | + } | ||
| 174 | + } | ||
| 175 | + } | ||
| 176 | + | ||
| 177 | + // Check if we should become a spatial scroll. | ||
| 178 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 179 | + let c = event.location - self.start_data.location; | ||
| 180 | + | ||
| 181 | + // Check if the gesture moved far enough to decide. Threshold copied from libadwaita. | ||
| 182 | + if c.x * c.x + c.y * c.y >= 16. * 16. { | ||
| 183 | + if let Some(ws_id) = self.workspace_id.filter(|_| c.x.abs() > c.y.abs()) { | ||
| 184 | + if let Some((ws_idx, ws)) = layout.find_workspace_by_id(ws_id) { | ||
| 185 | + if ws.current_output() == Some(&self.output) { | ||
| 186 | + layout.view_offset_gesture_begin(&self.output, Some(ws_idx), false); | ||
| 187 | + self.gesture = GestureState::ViewOffset; | ||
| 188 | + } | ||
| 189 | + } | ||
| 190 | + } | ||
| 191 | + | ||
| 192 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 193 | + layout.workspace_switch_gesture_begin(&self.output, false); | ||
| 194 | + self.gesture = GestureState::WorkspaceSwitch; | ||
| 195 | + } | ||
| 196 | + } | ||
| 197 | + } | ||
| 198 | + | ||
| 199 | + // Do nothing if still recognizing. | ||
| 200 | + if matches!(self.gesture, GestureState::Recognizing) { | ||
| 201 | + return; | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + let delta = event.location - self.last_location; | ||
| 205 | + self.last_location = event.location; | ||
| 206 | + | ||
| 207 | + let ongoing = match self.gesture { | ||
| 208 | + GestureState::Recognizing => unreachable!(), | ||
| 209 | + GestureState::ViewOffset => layout | ||
| 210 | + .view_offset_gesture_update(-delta.x, timestamp, false) | ||
| 211 | + .is_some(), | ||
| 212 | + GestureState::WorkspaceSwitch => layout | ||
| 213 | + .workspace_switch_gesture_update(-delta.y, timestamp, false) | ||
| 214 | + .is_some(), | ||
| 215 | + GestureState::InteractiveMove => { | ||
| 216 | + let window = self.window.as_ref().unwrap(); | ||
| 217 | + if let Some((output, pos_within_output)) = data.niri.output_under(event.location) { | ||
| 218 | + let output = output.clone(); | ||
| 219 | + data.niri.layout.interactive_move_update( | ||
| 220 | + window, | ||
| 221 | + delta, | ||
| 222 | + output, | ||
| 223 | + pos_within_output, | ||
| 224 | + ) | ||
| 225 | + } else { | ||
| 226 | + false | ||
| 227 | + } | ||
| 228 | + } | ||
| 229 | + }; | ||
| 230 | + | ||
| 231 | + if ongoing { | ||
| 232 | + data.niri.queue_redraw_all(); | ||
| 233 | + } else { | ||
| 234 | + handle.unset_grab(self, data); | ||
| 235 | + } | ||
| 236 | + } | ||
| 237 | + | ||
| 238 | + fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>, seq: Serial) { | ||
| 239 | + handle.frame(data, seq); | ||
| 240 | + } | ||
| 241 | + | ||
| 242 | + fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>, seq: Serial) { | ||
| 243 | + handle.cancel(data, seq); | ||
| 244 | + handle.unset_grab(self, data); | ||
| 245 | + } | ||
| 246 | + | ||
| 247 | + fn shape( | ||
| 248 | + &mut self, | ||
| 249 | + data: &mut State, | ||
| 250 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 251 | + event: &ShapeEvent, | ||
| 252 | + seq: Serial, | ||
| 253 | + ) { | ||
| 254 | + handle.shape(data, event, seq); | ||
| 255 | + } | ||
| 256 | + | ||
| 257 | + fn orientation( | ||
| 258 | + &mut self, | ||
| 259 | + data: &mut State, | ||
| 260 | + handle: &mut TouchInnerHandle<'_, State>, | ||
| 261 | + event: &OrientationEvent, | ||
| 262 | + seq: Serial, | ||
| 263 | + ) { | ||
| 264 | + handle.orientation(data, event, seq); | ||
| 265 | + } | ||
| 266 | + | ||
| 267 | + fn start_data(&self) -> &TouchGrabStartData<State> { | ||
| 268 | + &self.start_data | ||
| 269 | + } | ||
| 270 | + | ||
| 271 | + fn unset(&mut self, data: &mut State) { | ||
| 272 | + self.on_ungrab(data); | ||
| 273 | + } | ||
| 274 | +} | ||