forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB9d8f4f8Ivan Molodetskikh1 month ago2997 commits

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