forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBb0eb8adIvan Molodetskikh3 months ago2857 commits

move_grab.rs646 lines, 18.9 KiBHistoryRawDownload
1use std::time::Duration;
2
3use smithay::backend::input::ButtonState;
4use smithay::desktop::Window;
5use smithay::input::pointer::{
6 AxisFrame, ButtonEvent, CursorIcon, CursorImageStatus, GestureHoldBeginEvent,
7 GestureHoldEndEvent, GesturePinchBeginEvent, GesturePinchEndEvent, GesturePinchUpdateEvent,
8 GestureSwipeBeginEvent, GestureSwipeEndEvent, GestureSwipeUpdateEvent,
9 GrabStartData as PointerGrabStartData, MotionEvent, PointerGrab, PointerInnerHandle,
10 RelativeMotionEvent,
11};
12use smithay::input::tablet::tool::{TabletToolGrab, TabletToolInnerHandle};
13use smithay::input::tablet::TabletSeatHandler;
14use smithay::input::touch::{
15 self, GrabStartData as TouchGrabStartData, TouchGrab, TouchInnerHandle,
16};
17use smithay::input::{tablet, SeatHandler};
18use smithay::output::Output;
19use smithay::utils::{IsAlive, Logical, Point, SERIAL_COUNTER};
20
21use crate::input::AnyStartData;
22use crate::niri::State;
23use crate::utils::get_monotonic_time;
24
25pub struct MoveGrab {
26 start_data: AnyStartData<State>,
27 start_output: Output,
28 start_pos_within_output: Point<f64, Logical>,
29 last_location: Point<f64, Logical>,
30 window: Window,
31 gesture: GestureState,
32 enable_view_offset: bool,
33 move_icon: CursorIcon,
34
35 // Accumulated and applied in frame().
36 new_location: Point<f64, Logical>,
37 event_timestamp: Option<Duration>,
38 relative_delta: Option<Point<f64, Logical>>,
39}
40
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42enum GestureState {
43 Recognizing,
44 Move,
45 ViewOffset,
46}
47
48impl MoveGrab {
49 pub fn new(
50 state: &mut State,
51 start_data: AnyStartData<State>,
52 window: Window,
53 enable_view_offset: bool,
54 move_icon: Option<CursorIcon>,
55 ) -> Option<Self> {
56 let location = start_data.location();
57 let (output, pos_within_output) = state.niri.output_under(location)?;
58
59 Some(Self {
60 last_location: location,
61 start_data,
62 start_output: output.clone(),
63 start_pos_within_output: pos_within_output,
64 window,
65 gesture: GestureState::Recognizing,
66 enable_view_offset,
67 // Moving windows by their titlebars uses the default cursor by default.
68 move_icon: move_icon.unwrap_or(CursorIcon::Default),
69 new_location: location,
70 event_timestamp: None,
71 relative_delta: None,
72 })
73 }
74
75 pub fn is_move(&self) -> bool {
76 self.gesture == GestureState::Move
77 }
78
79 pub fn view_offset_output(&self) -> Option<&Output> {
80 (self.gesture == GestureState::ViewOffset).then_some(&self.start_output)
81 }
82
83 fn on_ungrab(&mut self, data: &mut State) {
84 let layout = &mut data.niri.layout;
85 match self.gesture {
86 GestureState::Recognizing => {
87 // Activate the window on release. This is most prominent in the overview where
88 // windows are not activated on click. In the overview, we also try to do a nice
89 // synchronized workspace animation.
90 if layout.is_overview_open() {
91 let res = layout.workspaces().find_map(|(mon, ws_idx, ws)| {
92 ws.windows()
93 .any(|w| w.window == self.window)
94 .then(|| (mon.map(|mon| mon.output().clone()), ws_idx))
95 });
96 if let Some((Some(output), ws_idx)) = res {
97 layout.focus_output(&output);
98 layout.toggle_overview_to_workspace(ws_idx);
99 }
100 }
101
102 layout.activate_window(&self.window);
103 }
104 GestureState::Move => layout.interactive_move_end(&self.window),
105 GestureState::ViewOffset => {
106 layout.view_offset_gesture_end(Some(false));
107 }
108 }
109
110 if !self.start_data.is_touch() {
111 data.niri
112 .cursor_manager
113 .set_cursor_image(CursorImageStatus::default_named());
114 }
115
116 // FIXME: only redraw the window output.
117 data.niri.queue_redraw_all();
118 }
119
120 fn begin_move(&mut self, data: &mut State) -> bool {
121 if !data.niri.layout.interactive_move_begin(
122 self.window.clone(),
123 &self.start_output,
124 self.start_pos_within_output,
125 ) {
126 // Can no longer start the move.
127 return false;
128 }
129
130 self.gesture = GestureState::Move;
131
132 if !self.start_data.is_touch() {
133 data.niri
134 .cursor_manager
135 .set_cursor_image(CursorImageStatus::Named(self.move_icon));
136 }
137
138 true
139 }
140
141 fn begin_view_offset(&mut self, data: &mut State) -> bool {
142 let layout = &mut data.niri.layout;
143 let Some(ws_idx) = layout.workspaces().find_map(|(mon, ws_idx, ws)| {
144 let ws_idx = ws
145 .windows()
146 .any(|w| w.window == self.window)
147 .then_some(ws_idx)?;
148 let output = mon?.output();
149
150 // If the window moved to a different output, don't start the gesture.
151 if *output != self.start_output {
152 return None;
153 }
154
155 Some(ws_idx)
156 }) else {
157 // Can no longer start the gesture.
158 return false;
159 };
160
161 layout.view_offset_gesture_begin(&self.start_output, Some(ws_idx), false);
162
163 self.gesture = GestureState::ViewOffset;
164
165 if !self.start_data.is_touch() {
166 data.niri
167 .cursor_manager
168 .set_cursor_image(CursorImageStatus::Named(CursorIcon::AllScroll));
169 }
170
171 true
172 }
173
174 fn on_frame(&mut self, data: &mut State) -> bool {
175 let Some(timestamp) = self.event_timestamp.take() else {
176 return true;
177 };
178
179 let mut delta = self.new_location - self.last_location;
180 let mut relative_delta = self.relative_delta.take().unwrap_or(delta);
181 self.last_location = self.new_location;
182
183 // Try to recognize the gesture.
184 if self.gesture == GestureState::Recognizing {
185 // Check if the window has closed.
186 if !self.window.alive() {
187 return false;
188 }
189
190 // Check if the gesture moved far enough to decide.
191 let c = self.new_location - self.start_data.location();
192 if c.x * c.x + c.y * c.y >= 8. * 8. {
193 let is_floating = data
194 .niri
195 .layout
196 .workspaces()
197 .find_map(|(_, _, ws)| {
198 ws.windows()
199 .any(|w| w.window == self.window)
200 .then(|| ws.is_floating(&self.window))
201 })
202 .unwrap_or(false);
203
204 let is_view_offset =
205 self.enable_view_offset && !is_floating && c.x.abs() > c.y.abs();
206
207 let started = if is_view_offset {
208 self.begin_view_offset(data)
209 } else {
210 self.begin_move(data)
211 };
212 if !started {
213 return false;
214 }
215
216 // Apply the whole delta that accumulated during recognizing.
217 delta = c;
218 relative_delta = c;
219 }
220 }
221
222 match self.gesture {
223 GestureState::Recognizing => return true,
224 GestureState::Move => {
225 let Some((output, pos_within_output)) = data.niri.output_under(self.last_location)
226 else {
227 return true;
228 };
229 let output = output.clone();
230
231 // Interactive move always uses absolute delta since the window must remain pinned
232 // to the cursor even when it's clamped to monitor bounds.
233 let ongoing = data.niri.layout.interactive_move_update(
234 &self.window,
235 delta,
236 output,
237 pos_within_output,
238 );
239 if ongoing {
240 // FIXME: only redraw the previous and the new output.
241 data.niri.queue_redraw_all();
242 return true;
243 }
244 }
245 GestureState::ViewOffset => {
246 let res = data.niri.layout.view_offset_gesture_update(
247 -relative_delta.x,
248 timestamp,
249 false,
250 );
251 if let Some(output) = res {
252 if let Some(output) = output {
253 data.niri.queue_redraw(&output);
254 }
255 return true;
256 }
257 }
258 }
259
260 false
261 }
262
263 fn on_toggle_floating(&mut self, data: &mut State) -> bool {
264 if self.gesture == GestureState::ViewOffset {
265 return true;
266 }
267
268 // Start move if still recognizing.
269 if self.gesture == GestureState::Recognizing {
270 let Some((output, pos_within_output)) = data.niri.output_under(self.last_location)
271 else {
272 return false;
273 };
274 let output = output.clone();
275
276 if !self.begin_move(data) {
277 return false;
278 }
279
280 // Apply the delta accumulated during recognizing.
281 let ongoing = data.niri.layout.interactive_move_update(
282 &self.window,
283 self.last_location - self.start_data.location(),
284 output,
285 pos_within_output,
286 );
287 if !ongoing {
288 return false;
289 }
290 }
291
292 data.niri.layout.toggle_window_floating(Some(&self.window));
293 data.niri.queue_redraw_all();
294
295 true
296 }
297}
298
299impl PointerGrab<State> for MoveGrab {
300 fn motion(
301 &mut self,
302 data: &mut State,
303 handle: &mut PointerInnerHandle<'_, State>,
304 _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>,
305 event: &MotionEvent,
306 ) {
307 // While the grab is active, no client has pointer focus.
308 handle.motion(data, None, event);
309
310 self.new_location = event.location;
311
312 // Relative motion takes precedence over normal motion.
313 if self.relative_delta.is_none() {
314 self.event_timestamp = Some(Duration::from_millis(u64::from(event.time)));
315 }
316 }
317
318 fn relative_motion(
319 &mut self,
320 data: &mut State,
321 handle: &mut PointerInnerHandle<'_, State>,
322 _focus: Option<(<State as SeatHandler>::PointerFocus, Point<f64, Logical>)>,
323 event: &RelativeMotionEvent,
324 ) {
325 // While the grab is active, no client has pointer focus.
326 handle.relative_motion(data, None, event);
327
328 *self.relative_delta.get_or_insert_default() += event.delta;
329 self.event_timestamp = Some(Duration::from_micros(event.utime));
330 }
331
332 fn button(
333 &mut self,
334 data: &mut State,
335 handle: &mut PointerInnerHandle<'_, State>,
336 event: &ButtonEvent,
337 ) {
338 handle.button(data, event);
339
340 let start_data = self.start_data.unwrap_pointer();
341
342 if !handle.current_pressed().contains(&start_data.button) {
343 // The button that initiated the grab was released.
344 handle.unset_grab(self, data, event.serial, event.time, true);
345 return;
346 }
347
348 // When moving with the left button, right toggles floating, and vice versa.
349 let toggle_floating_button = if start_data.button == 0x110 {
350 0x111
351 } else {
352 0x110
353 };
354 if event.state != ButtonState::Pressed || event.button != toggle_floating_button {
355 return;
356 }
357
358 if !self.on_toggle_floating(data) {
359 handle.unset_grab(self, data, event.serial, event.time, true);
360 }
361 }
362
363 fn axis(
364 &mut self,
365 data: &mut State,
366 handle: &mut PointerInnerHandle<'_, State>,
367 details: AxisFrame,
368 ) {
369 handle.axis(data, details);
370 }
371
372 fn frame(&mut self, data: &mut State, handle: &mut PointerInnerHandle<'_, State>) {
373 handle.frame(data);
374
375 if !self.on_frame(data) {
376 // The gesture is no longer ongoing.
377 handle.unset_grab(
378 self,
379 data,
380 SERIAL_COUNTER.next_serial(),
381 get_monotonic_time().as_millis() as u32,
382 true,
383 );
384 }
385 }
386
387 fn gesture_swipe_begin(
388 &mut self,
389 data: &mut State,
390 handle: &mut PointerInnerHandle<'_, State>,
391 event: &GestureSwipeBeginEvent,
392 ) {
393 handle.gesture_swipe_begin(data, event);
394 }
395
396 fn gesture_swipe_update(
397 &mut self,
398 data: &mut State,
399 handle: &mut PointerInnerHandle<'_, State>,
400 event: &GestureSwipeUpdateEvent,
401 ) {
402 handle.gesture_swipe_update(data, event);
403 }
404
405 fn gesture_swipe_end(
406 &mut self,
407 data: &mut State,
408 handle: &mut PointerInnerHandle<'_, State>,
409 event: &GestureSwipeEndEvent,
410 ) {
411 handle.gesture_swipe_end(data, event);
412 }
413
414 fn gesture_pinch_begin(
415 &mut self,
416 data: &mut State,
417 handle: &mut PointerInnerHandle<'_, State>,
418 event: &GesturePinchBeginEvent,
419 ) {
420 handle.gesture_pinch_begin(data, event);
421 }
422
423 fn gesture_pinch_update(
424 &mut self,
425 data: &mut State,
426 handle: &mut PointerInnerHandle<'_, State>,
427 event: &GesturePinchUpdateEvent,
428 ) {
429 handle.gesture_pinch_update(data, event);
430 }
431
432 fn gesture_pinch_end(
433 &mut self,
434 data: &mut State,
435 handle: &mut PointerInnerHandle<'_, State>,
436 event: &GesturePinchEndEvent,
437 ) {
438 handle.gesture_pinch_end(data, event);
439 }
440
441 fn gesture_hold_begin(
442 &mut self,
443 data: &mut State,
444 handle: &mut PointerInnerHandle<'_, State>,
445 event: &GestureHoldBeginEvent,
446 ) {
447 handle.gesture_hold_begin(data, event);
448 }
449
450 fn gesture_hold_end(
451 &mut self,
452 data: &mut State,
453 handle: &mut PointerInnerHandle<'_, State>,
454 event: &GestureHoldEndEvent,
455 ) {
456 handle.gesture_hold_end(data, event);
457 }
458
459 fn start_data(&self) -> &PointerGrabStartData<State> {
460 self.start_data.unwrap_pointer()
461 }
462
463 fn unset(&mut self, data: &mut State) {
464 self.on_ungrab(data);
465 }
466}
467
468impl TouchGrab<State> for MoveGrab {
469 fn down(
470 &mut self,
471 data: &mut State,
472 handle: &mut TouchInnerHandle<'_, State>,
473 _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>,
474 event: &touch::DownEvent,
475 ) {
476 handle.down(data, None, event);
477
478 if event.slot == self.start_data.unwrap_touch().slot {
479 return;
480 }
481
482 if !self.on_toggle_floating(data) {
483 handle.unset_grab(self, data);
484 }
485 }
486
487 fn up(
488 &mut self,
489 data: &mut State,
490 handle: &mut TouchInnerHandle<'_, State>,
491 event: &touch::UpEvent,
492 ) {
493 handle.up(data, event);
494
495 if event.slot == self.start_data.unwrap_touch().slot {
496 handle.unset_grab(self, data);
497 }
498 }
499
500 fn motion(
501 &mut self,
502 data: &mut State,
503 handle: &mut TouchInnerHandle<'_, State>,
504 _focus: Option<(<State as SeatHandler>::TouchFocus, Point<f64, Logical>)>,
505 event: &touch::MotionEvent,
506 ) {
507 handle.motion(data, None, event);
508
509 if event.slot != self.start_data.unwrap_touch().slot {
510 return;
511 }
512
513 self.new_location = event.location;
514 self.event_timestamp = Some(Duration::from_millis(u64::from(event.time)));
515 }
516
517 fn frame(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) {
518 handle.frame(data);
519
520 if !self.on_frame(data) {
521 // The gesture is no longer ongoing.
522 handle.unset_grab(self, data);
523 }
524 }
525
526 fn cancel(&mut self, data: &mut State, handle: &mut TouchInnerHandle<'_, State>) {
527 handle.cancel(data);
528 handle.unset_grab(self, data);
529 }
530
531 fn shape(
532 &mut self,
533 data: &mut State,
534 handle: &mut TouchInnerHandle<'_, State>,
535 event: &touch::ShapeEvent,
536 ) {
537 handle.shape(data, event);
538 }
539
540 fn orientation(
541 &mut self,
542 data: &mut State,
543 handle: &mut TouchInnerHandle<'_, State>,
544 event: &touch::OrientationEvent,
545 ) {
546 handle.orientation(data, event);
547 }
548
549 fn start_data(&self) -> &TouchGrabStartData<State> {
550 self.start_data.unwrap_touch()
551 }
552
553 fn unset(&mut self, data: &mut State) {
554 self.on_ungrab(data);
555 }
556}
557
558impl TabletToolGrab<State> for MoveGrab {
559 fn start_data(&self) -> &tablet::tool::GrabStartData<State> {
560 self.start_data.unwrap_tablet_tool()
561 }
562
563 fn proximity_out(
564 &mut self,
565 data: &mut State,
566 handle: &mut TabletToolInnerHandle<'_, State>,
567 event: &tablet::tool::ProximityOutEvent,
568 ) {
569 handle.proximity_out(data, event);
570 handle.unset_grab(self, data, event.serial, event.time, false);
571 }
572
573 fn motion(
574 &mut self,
575 data: &mut State,
576 handle: &mut TabletToolInnerHandle<'_, State>,
577 _focus: Option<(<State as TabletSeatHandler>::ToolFocus, Point<f64, Logical>)>,
578 event: &tablet::tool::MotionEvent,
579 ) {
580 handle.motion(data, None, event);
581
582 self.new_location = event.location;
583 self.event_timestamp = Some(Duration::from_millis(u64::from(event.time)));
584 }
585
586 fn down(
587 &mut self,
588 data: &mut State,
589 handle: &mut TabletToolInnerHandle<'_, State>,
590 event: &tablet::tool::DownEvent,
591 ) {
592 handle.down(data, event);
593 }
594
595 fn up(
596 &mut self,
597 data: &mut State,
598 handle: &mut TabletToolInnerHandle<'_, State>,
599 event: &tablet::tool::UpEvent,
600 ) {
601 handle.up(data, event);
602 handle.unset_grab(self, data, event.serial, event.time, true);
603 }
604
605 fn button(
606 &mut self,
607 data: &mut State,
608 handle: &mut TabletToolInnerHandle<'_, State>,
609 event: &tablet::tool::ButtonEvent,
610 ) {
611 handle.button(data, event);
612 }
613
614 fn axis(
615 &mut self,
616 data: &mut State,
617 handle: &mut TabletToolInnerHandle<'_, State>,
618 frame: tablet::tool::AxisFrame,
619 ) {
620 handle.axis(data, frame);
621 }
622
623 fn frame(
624 &mut self,
625 data: &mut State,
626 handle: &mut TabletToolInnerHandle<'_, State>,
627 time: u32,
628 ) {
629 handle.frame(data, time);
630
631 if !self.on_frame(data) {
632 // The gesture is no longer ongoing.
633 handle.unset_grab(
634 self,
635 data,
636 SERIAL_COUNTER.next_serial(),
637 get_monotonic_time().as_millis() as u32,
638 true,
639 );
640 }
641 }
642
643 fn unset(&mut self, data: &mut State) {
644 self.on_ungrab(data);
645 }
646}