niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::time::Duration; |
| 2 | |
| 3 | use smithay::desktop::Window; |
| 4 | use smithay::input::pointer::{CursorIcon, CursorImageStatus}; |
| 5 | use smithay::input::tablet::tool::{TabletToolGrab, TabletToolInnerHandle}; |
| 6 | use smithay::input::tablet::TabletSeatHandler; |
| 7 | use smithay::input::touch::{ |
| 8 | DownEvent, GrabStartData as TouchGrabStartData, MotionEvent, OrientationEvent, ShapeEvent, |
| 9 | TouchGrab, TouchInnerHandle, UpEvent, |
| 10 | }; |
| 11 | use smithay::input::{tablet, SeatHandler}; |
| 12 | use smithay::output::Output; |
| 13 | use smithay::utils::{IsAlive, Logical, Point, SERIAL_COUNTER}; |
| 14 | |
| 15 | use crate::input::AnyStartData; |
| 16 | use crate::layout::workspace::{Workspace, WorkspaceId}; |
| 17 | use crate::niri::State; |
| 18 | use crate::utils::get_monotonic_time; |
| 19 | use crate::window::Mapped; |
| 20 | |
| 21 | // When the touch is stationary for this much time, it becomes an interactive move. |
| 22 | const INTERACTIVE_MOVE_THRESHOLD: Duration = Duration::from_millis(500); |
| 23 | |
| 24 | pub struct TouchOverviewGrab { |
| 25 | start_data: AnyStartData<State>, |
| 26 | start_timestamp: Duration, |
| 27 | last_location: Point<f64, Logical>, |
| 28 | output: Output, |
| 29 | start_pos_within_output: Point<f64, Logical>, |
| 30 | workspace_id: Option<WorkspaceId>, |
| 31 | workspace_matched_narrow: bool, |
| 32 | window: Option<Window>, |
| 33 | gesture: GestureState, |
| 34 | |
| 35 | // Accumulated and applied in frame(). |
| 36 | new_location: Point<f64, Logical>, |
| 37 | event_timestamp: Option<Duration>, |
| 38 | } |
| 39 | |
| 40 | #[derive(Debug, Clone, Copy)] |
| 41 | enum GestureState { |
| 42 | Recognizing, |
| 43 | ViewOffset, |
| 44 | WorkspaceSwitch, |
| 45 | InteractiveMove, |
| 46 | } |
| 47 | |
| 48 | impl TouchOverviewGrab { |
| 49 | pub fn new( |
| 50 | start_data: AnyStartData<State>, |
| 51 | start_timestamp: Duration, |
| 52 | output: Output, |
| 53 | start_pos_within_output: Point<f64, Logical>, |
| 54 | workspace_id: Option<WorkspaceId>, |
| 55 | workspace_matched_narrow: bool, |
| 56 | window: Option<Window>, |
| 57 | ) -> Self { |
| 58 | let location = start_data.location(); |
| 59 | |
| 60 | Self { |
| 61 | last_location: location, |
| 62 | start_timestamp, |
| 63 | start_data, |
| 64 | output, |
| 65 | start_pos_within_output, |
| 66 | workspace_id, |
| 67 | workspace_matched_narrow, |
| 68 | window, |
| 69 | gesture: GestureState::Recognizing, |
| 70 | new_location: location, |
| 71 | event_timestamp: None, |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn on_frame(&mut self, data: &mut State) -> bool { |
| 76 | let Some(timestamp) = self.event_timestamp.take() else { |
| 77 | return true; |
| 78 | }; |
| 79 | |
| 80 | let layout = &mut data.niri.layout; |
| 81 | |
| 82 | // Check if we should become interactive move. |
| 83 | if matches!(self.gesture, GestureState::Recognizing) { |
| 84 | if let Some(window) = self.window.as_ref().filter(|win| win.alive()) { |
| 85 | let passed = timestamp.saturating_sub(self.start_timestamp); |
| 86 | if INTERACTIVE_MOVE_THRESHOLD <= passed |
| 87 | && layout.interactive_move_begin( |
| 88 | window.clone(), |
| 89 | &self.output, |
| 90 | self.start_pos_within_output, |
| 91 | ) |
| 92 | { |
| 93 | self.gesture = GestureState::InteractiveMove; |
| 94 | |
| 95 | if !self.start_data.is_touch() { |
| 96 | data.niri |
| 97 | .cursor_manager |
| 98 | .set_cursor_image(CursorImageStatus::Named(CursorIcon::Grabbing)); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | // Check if we should become a spatial scroll. |
| 105 | if matches!(self.gesture, GestureState::Recognizing) { |
| 106 | let c = self.new_location - self.start_data.location(); |
| 107 | |
| 108 | // Check if the gesture moved far enough to decide. Threshold copied from libadwaita. |
| 109 | if c.x * c.x + c.y * c.y >= 16. * 16. { |
| 110 | if let Some(ws_id) = self.workspace_id.filter(|_| c.x.abs() > c.y.abs()) { |
| 111 | if let Some((ws_idx, ws)) = layout.find_workspace_by_id(ws_id) { |
| 112 | if ws.current_output() == Some(&self.output) { |
| 113 | layout.view_offset_gesture_begin(&self.output, Some(ws_idx), false); |
| 114 | self.gesture = GestureState::ViewOffset; |
| 115 | |
| 116 | if !self.start_data.is_touch() { |
| 117 | data.niri.cursor_manager.set_cursor_image( |
| 118 | CursorImageStatus::Named(CursorIcon::AllScroll), |
| 119 | ); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if matches!(self.gesture, GestureState::Recognizing) { |
| 126 | layout.workspace_switch_gesture_begin(&self.output, false); |
| 127 | self.gesture = GestureState::WorkspaceSwitch; |
| 128 | |
| 129 | if !self.start_data.is_touch() { |
| 130 | data.niri |
| 131 | .cursor_manager |
| 132 | .set_cursor_image(CursorImageStatus::Named(CursorIcon::AllScroll)); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // Do nothing if still recognizing. |
| 139 | if matches!(self.gesture, GestureState::Recognizing) { |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | let delta = self.new_location - self.last_location; |
| 144 | self.last_location = self.new_location; |
| 145 | |
| 146 | let ongoing = match self.gesture { |
| 147 | GestureState::Recognizing => unreachable!(), |
| 148 | GestureState::ViewOffset => layout |
| 149 | .view_offset_gesture_update(-delta.x, timestamp, false) |
| 150 | .is_some(), |
| 151 | GestureState::WorkspaceSwitch => layout |
| 152 | .workspace_switch_gesture_update(-delta.y, timestamp, false) |
| 153 | .is_some(), |
| 154 | GestureState::InteractiveMove => { |
| 155 | let window = self.window.as_ref().unwrap(); |
| 156 | if let Some((output, pos_within_output)) = data.niri.output_under(self.new_location) |
| 157 | { |
| 158 | let output = output.clone(); |
| 159 | data.niri.layout.interactive_move_update( |
| 160 | window, |
| 161 | delta, |
| 162 | output, |
| 163 | pos_within_output, |
| 164 | ) |
| 165 | } else { |
| 166 | false |
| 167 | } |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | if ongoing { |
| 172 | data.niri.queue_redraw_all(); |
| 173 | } |
| 174 | |
| 175 | ongoing |
| 176 | } |
| 177 | |
| 178 | fn on_ungrab(&mut self, state: &mut State) { |
| 179 | let layout = &mut state.niri.layout; |
| 180 | match self.gesture { |
| 181 | GestureState::Recognizing => { |
| 182 | // Tap to activate. |
| 183 | layout.focus_output(&self.output); |
| 184 | |
| 185 | // Activate the workspace if necessary. |
| 186 | if self.window.is_some() || self.workspace_matched_narrow { |
| 187 | // When activating a window, we want to activate the window's current |
| 188 | // workspace. Otherwise, find the workspace that we tapped on. |
| 189 | let ws_matches = |ws: &Workspace<Mapped>| { |
| 190 | if let Some(window) = &self.window { |
| 191 | ws.has_window(window) |
| 192 | } else if let Some(ws_id) = self.workspace_id { |
| 193 | ws.id() == ws_id |
| 194 | } else { |
| 195 | false |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | let ws_idx = if let Some((Some(mon), ws_idx, _)) = |
| 200 | layout.workspaces().find(|(_, _, ws)| ws_matches(ws)) |
| 201 | { |
| 202 | // The workspace could've moved to a different output in the meantime. |
| 203 | (*mon.output() == self.output).then_some(ws_idx) |
| 204 | } else { |
| 205 | None |
| 206 | }; |
| 207 | |
| 208 | if let Some(ws_idx) = ws_idx { |
| 209 | layout.toggle_overview_to_workspace(ws_idx); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if let Some(window) = self.window.as_ref() { |
| 214 | layout.activate_window(window); |
| 215 | } |
| 216 | } |
| 217 | GestureState::ViewOffset => { |
| 218 | layout.view_offset_gesture_end(Some(false)); |
| 219 | } |
| 220 | GestureState::WorkspaceSwitch => { |
| 221 | layout.workspace_switch_gesture_end(Some(false)); |
| 222 | } |
| 223 | GestureState::InteractiveMove => { |
| 224 | layout.interactive_move_end(self.window.as_ref().unwrap()); |
| 225 | } |
| 226 | }; |
| 227 | |
| 228 | if !self.start_data.is_touch() { |
| 229 | state |
| 230 | .niri |
| 231 | .cursor_manager |
| 232 | .set_cursor_image(CursorImageStatus::default_named()); |
| 233 | } |
| 234 | |
| 235 | state.niri.queue_redraw_all(); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | impl TouchGrab<State> for TouchOverviewGrab { |
| 240 | fn down( |
| 241 | &mut self, |
| 242 | data: &mut State, |
| 243 | handle: &mut TouchInnerHandle<'_, State>, |
| 244 | _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, |
| 245 | event: &DownEvent, |
| 246 | ) { |
| 247 | handle.down(data, None, event); |
| 248 | |
| 249 | if event.slot == self.start_data.unwrap_touch().slot { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | if matches!(self.gesture, GestureState::InteractiveMove) { |
| 254 | if let Some(window) = &self.window.as_ref() { |
| 255 | data.niri.layout.toggle_window_floating(Some(window)); |
| 256 | data.niri.queue_redraw_all(); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | fn up(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>, event: &UpEvent) { |
| 262 | handle.up(data, event); |
| 263 | |
| 264 | if event.slot != self.start_data.unwrap_touch().slot { |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | handle.unset_grab(self, data); |
| 269 | } |
| 270 | |
| 271 | fn motion( |
| 272 | &mut self, |
| 273 | data: &mut State, |
| 274 | handle: &mut TouchInnerHandle<'_, State>, |
| 275 | _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>, |
| 276 | event: &MotionEvent, |
| 277 | ) { |
| 278 | handle.motion(data, None, event); |
| 279 | |
| 280 | if event.slot != self.start_data.unwrap_touch().slot { |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | self.new_location = event.location; |
| 285 | self.event_timestamp = Some(Duration::from_millis(u64::from(event.time))); |
| 286 | } |
| 287 | |
| 288 | fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { |
| 289 | handle.frame(data); |
| 290 | |
| 291 | if !self.on_frame(data) { |
| 292 | // The gesture is no longer ongoing. |
| 293 | handle.unset_grab(self, data); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) { |
| 298 | handle.cancel(data); |
| 299 | handle.unset_grab(self, data); |
| 300 | } |
| 301 | |
| 302 | fn shape( |
| 303 | &mut self, |
| 304 | data: &mut State, |
| 305 | handle: &mut TouchInnerHandle<'_, State>, |
| 306 | event: &ShapeEvent, |
| 307 | ) { |
| 308 | handle.shape(data, event); |
| 309 | } |
| 310 | |
| 311 | fn orientation( |
| 312 | &mut self, |
| 313 | data: &mut State, |
| 314 | handle: &mut TouchInnerHandle<'_, State>, |
| 315 | event: &OrientationEvent, |
| 316 | ) { |
| 317 | handle.orientation(data, event); |
| 318 | } |
| 319 | |
| 320 | fn start_data(&self) -> &TouchGrabStartData<State> { |
| 321 | self.start_data.unwrap_touch() |
| 322 | } |
| 323 | |
| 324 | fn unset(&mut self, data: &mut State) { |
| 325 | self.on_ungrab(data); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | impl TabletToolGrab<State> for TouchOverviewGrab { |
| 330 | fn start_data(&self) -> &tablet::tool::GrabStartData<State> { |
| 331 | self.start_data.unwrap_tablet_tool() |
| 332 | } |
| 333 | |
| 334 | fn proximity_out( |
| 335 | &mut self, |
| 336 | data: &mut State, |
| 337 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 338 | event: &tablet::tool::ProximityOutEvent, |
| 339 | ) { |
| 340 | handle.proximity_out(data, event); |
| 341 | handle.unset_grab(self, data, event.serial, event.time, false); |
| 342 | } |
| 343 | |
| 344 | fn motion( |
| 345 | &mut self, |
| 346 | data: &mut State, |
| 347 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 348 | _focus: Option<(<State as TabletSeatHandler>::ToolFocus, Point<f64, Logical>)>, |
| 349 | event: &tablet::tool::MotionEvent, |
| 350 | ) { |
| 351 | handle.motion(data, None, event); |
| 352 | |
| 353 | self.new_location = event.location; |
| 354 | self.event_timestamp = Some(Duration::from_millis(u64::from(event.time))); |
| 355 | } |
| 356 | |
| 357 | fn down( |
| 358 | &mut self, |
| 359 | data: &mut State, |
| 360 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 361 | event: &tablet::tool::DownEvent, |
| 362 | ) { |
| 363 | handle.down(data, event); |
| 364 | } |
| 365 | |
| 366 | fn up( |
| 367 | &mut self, |
| 368 | data: &mut State, |
| 369 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 370 | event: &tablet::tool::UpEvent, |
| 371 | ) { |
| 372 | handle.up(data, event); |
| 373 | handle.unset_grab(self, data, event.serial, event.time, true); |
| 374 | } |
| 375 | |
| 376 | fn button( |
| 377 | &mut self, |
| 378 | data: &mut State, |
| 379 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 380 | event: &tablet::tool::ButtonEvent, |
| 381 | ) { |
| 382 | handle.button(data, event); |
| 383 | } |
| 384 | |
| 385 | fn axis( |
| 386 | &mut self, |
| 387 | data: &mut State, |
| 388 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 389 | frame: tablet::tool::AxisFrame, |
| 390 | ) { |
| 391 | handle.axis(data, frame); |
| 392 | } |
| 393 | |
| 394 | fn frame( |
| 395 | &mut self, |
| 396 | data: &mut State, |
| 397 | handle: &mut TabletToolInnerHandle<'_, State>, |
| 398 | time: u32, |
| 399 | ) { |
| 400 | handle.frame(data, time); |
| 401 | |
| 402 | if !self.on_frame(data) { |
| 403 | // The gesture is no longer ongoing. |
| 404 | handle.unset_grab( |
| 405 | self, |
| 406 | data, |
| 407 | SERIAL_COUNTER.next_serial(), |
| 408 | get_monotonic_time().as_millis() as u32, |
| 409 | true, |
| 410 | ); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | fn unset(&mut self, data: &mut State) { |
| 415 | self.on_ungrab(data); |
| 416 | } |
| 417 | } |