forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBc658424Ivan Molodetskikh2 years ago1277 commits

monitor.rs1085 lines, 34.9 KiBHistoryRawDownload
1use std::cmp::min;
2use std::rc::Rc;
3use std::time::Duration;
4
5use niri_ipc::SizeChange;
6use smithay::backend::renderer::element::utils::{
7 CropRenderElement, Relocate, RelocateRenderElement,
8};
9use smithay::output::Output;
10use smithay::utils::{Logical, Point, Rectangle};
11
12use super::workspace::{
13 compute_working_area, Column, ColumnWidth, OutputId, Workspace, WorkspaceId,
14 WorkspaceRenderElement,
15};
16use super::{LayoutElement, Options};
17use crate::animation::Animation;
18use crate::input::swipe_tracker::SwipeTracker;
19use crate::render_helpers::renderer::NiriRenderer;
20use crate::render_helpers::RenderTarget;
21use crate::rubber_band::RubberBand;
22use crate::utils::{output_size, to_physical_precise_round, ResizeEdge};
23
24/// Amount of touchpad movement to scroll the height of one workspace.
25const WORKSPACE_GESTURE_MOVEMENT: f64 = 300.;
26
27const WORKSPACE_GESTURE_RUBBER_BAND: RubberBand = RubberBand {
28 stiffness: 0.5,
29 limit: 0.05,
30};
31
32#[derive(Debug)]
33pub struct Monitor<W: LayoutElement> {
34 /// Output for this monitor.
35 pub output: Output,
36 // Must always contain at least one.
37 pub workspaces: Vec<Workspace<W>>,
38 /// Index of the currently active workspace.
39 pub active_workspace_idx: usize,
40 /// ID of the previously active workspace.
41 pub previous_workspace_id: Option<WorkspaceId>,
42 /// In-progress switch between workspaces.
43 pub workspace_switch: Option<WorkspaceSwitch>,
44 /// Configurable properties of the layout.
45 pub options: Rc<Options>,
46}
47
48#[derive(Debug)]
49pub enum WorkspaceSwitch {
50 Animation(Animation),
51 Gesture(WorkspaceSwitchGesture),
52}
53
54#[derive(Debug)]
55pub struct WorkspaceSwitchGesture {
56 /// Index of the workspace where the gesture was started.
57 center_idx: usize,
58 /// Current, fractional workspace index.
59 pub current_idx: f64,
60 tracker: SwipeTracker,
61 /// Whether the gesture is controlled by the touchpad.
62 is_touchpad: bool,
63}
64
65pub type MonitorRenderElement<R> =
66 RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;
67
68impl WorkspaceSwitch {
69 pub fn current_idx(&self) -> f64 {
70 match self {
71 WorkspaceSwitch::Animation(anim) => anim.value(),
72 WorkspaceSwitch::Gesture(gesture) => gesture.current_idx,
73 }
74 }
75
76 pub fn target_idx(&self) -> f64 {
77 match self {
78 WorkspaceSwitch::Animation(anim) => anim.to(),
79 WorkspaceSwitch::Gesture(gesture) => gesture.current_idx,
80 }
81 }
82
83 /// Returns `true` if the workspace switch is [`Animation`].
84 ///
85 /// [`Animation`]: WorkspaceSwitch::Animation
86 #[must_use]
87 fn is_animation(&self) -> bool {
88 matches!(self, Self::Animation(..))
89 }
90}
91
92impl<W: LayoutElement> Monitor<W> {
93 pub fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self {
94 Self {
95 output,
96 workspaces,
97 active_workspace_idx: 0,
98 previous_workspace_id: None,
99 workspace_switch: None,
100 options,
101 }
102 }
103
104 pub fn active_workspace_ref(&self) -> &Workspace<W> {
105 &self.workspaces[self.active_workspace_idx]
106 }
107
108 pub fn find_named_workspace(&self, workspace_name: &str) -> Option<&Workspace<W>> {
109 self.workspaces.iter().find(|ws| {
110 ws.name
111 .as_ref()
112 .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name))
113 })
114 }
115
116 pub fn find_named_workspace_index(&self, workspace_name: &str) -> Option<usize> {
117 self.workspaces.iter().position(|ws| {
118 ws.name
119 .as_ref()
120 .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name))
121 })
122 }
123
124 pub fn active_workspace(&mut self) -> &mut Workspace<W> {
125 &mut self.workspaces[self.active_workspace_idx]
126 }
127
128 fn activate_workspace(&mut self, idx: usize) {
129 if self.active_workspace_idx == idx {
130 return;
131 }
132
133 // FIXME: also compute and use current velocity.
134 let current_idx = self
135 .workspace_switch
136 .as_ref()
137 .map(|s| s.current_idx())
138 .unwrap_or(self.active_workspace_idx as f64);
139
140 self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id());
141
142 self.active_workspace_idx = idx;
143
144 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
145 current_idx,
146 idx as f64,
147 0.,
148 self.options.animations.workspace_switch.0,
149 )));
150 }
151
152 pub fn add_window(
153 &mut self,
154 workspace_idx: usize,
155 window: W,
156 activate: bool,
157 width: ColumnWidth,
158 is_full_width: bool,
159 ) {
160 let workspace = &mut self.workspaces[workspace_idx];
161
162 workspace.add_window(window, activate, width, is_full_width);
163
164 // After adding a new window, workspace becomes this output's own.
165 workspace.original_output = OutputId::new(&self.output);
166
167 if workspace_idx == self.workspaces.len() - 1 {
168 // Insert a new empty workspace.
169 let ws = Workspace::new(self.output.clone(), self.options.clone());
170 self.workspaces.push(ws);
171 }
172
173 if activate {
174 self.activate_workspace(workspace_idx);
175 }
176 }
177
178 pub fn add_window_right_of(
179 &mut self,
180 right_of: &W::Id,
181 window: W,
182 width: ColumnWidth,
183 is_full_width: bool,
184 ) {
185 let workspace_idx = self
186 .workspaces
187 .iter_mut()
188 .position(|ws| ws.has_window(right_of))
189 .unwrap();
190 let workspace = &mut self.workspaces[workspace_idx];
191
192 workspace.add_window_right_of(right_of, window, width, is_full_width);
193
194 // After adding a new window, workspace becomes this output's own.
195 workspace.original_output = OutputId::new(&self.output);
196 }
197
198 pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) {
199 let workspace = &mut self.workspaces[workspace_idx];
200
201 workspace.add_column(column, activate);
202
203 // After adding a new window, workspace becomes this output's own.
204 workspace.original_output = OutputId::new(&self.output);
205
206 if workspace_idx == self.workspaces.len() - 1 {
207 // Insert a new empty workspace.
208 let ws = Workspace::new(self.output.clone(), self.options.clone());
209 self.workspaces.push(ws);
210 }
211
212 if activate {
213 self.activate_workspace(workspace_idx);
214 }
215 }
216
217 pub fn clean_up_workspaces(&mut self) {
218 assert!(self.workspace_switch.is_none());
219
220 for idx in (0..self.workspaces.len() - 1).rev() {
221 if self.active_workspace_idx == idx {
222 continue;
223 }
224
225 if !self.workspaces[idx].has_windows() && self.workspaces[idx].name.is_none() {
226 self.workspaces.remove(idx);
227 if self.active_workspace_idx > idx {
228 self.active_workspace_idx -= 1;
229 }
230 }
231 }
232 }
233
234 pub fn unname_workspace(&mut self, workspace_name: &str) -> bool {
235 for ws in &mut self.workspaces {
236 if ws
237 .name
238 .as_ref()
239 .map_or(false, |name| name.eq_ignore_ascii_case(workspace_name))
240 {
241 ws.unname();
242 return true;
243 }
244 }
245 false
246 }
247
248 pub fn move_left(&mut self) {
249 self.active_workspace().move_left();
250 }
251
252 pub fn move_right(&mut self) {
253 self.active_workspace().move_right();
254 }
255
256 pub fn move_column_to_first(&mut self) {
257 self.active_workspace().move_column_to_first();
258 }
259
260 pub fn move_column_to_last(&mut self) {
261 self.active_workspace().move_column_to_last();
262 }
263
264 pub fn move_down(&mut self) {
265 self.active_workspace().move_down();
266 }
267
268 pub fn move_up(&mut self) {
269 self.active_workspace().move_up();
270 }
271
272 pub fn move_down_or_to_workspace_down(&mut self) {
273 let workspace = self.active_workspace();
274 if workspace.columns.is_empty() {
275 return;
276 }
277 let column = &mut workspace.columns[workspace.active_column_idx];
278 let curr_idx = column.active_tile_idx;
279 let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1);
280 if curr_idx == new_idx {
281 self.move_to_workspace_down();
282 } else {
283 workspace.move_down();
284 }
285 }
286
287 pub fn move_up_or_to_workspace_up(&mut self) {
288 let workspace = self.active_workspace();
289 if workspace.columns.is_empty() {
290 return;
291 }
292 let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx;
293 let new_idx = curr_idx.saturating_sub(1);
294 if curr_idx == new_idx {
295 self.move_to_workspace_up();
296 } else {
297 workspace.move_up();
298 }
299 }
300
301 pub fn consume_or_expel_window_left(&mut self) {
302 self.active_workspace().consume_or_expel_window_left();
303 }
304
305 pub fn consume_or_expel_window_right(&mut self) {
306 self.active_workspace().consume_or_expel_window_right();
307 }
308
309 pub fn focus_left(&mut self) {
310 self.active_workspace().focus_left();
311 }
312
313 pub fn focus_right(&mut self) {
314 self.active_workspace().focus_right();
315 }
316
317 pub fn focus_column_first(&mut self) {
318 self.active_workspace().focus_column_first();
319 }
320
321 pub fn focus_column_last(&mut self) {
322 self.active_workspace().focus_column_last();
323 }
324
325 pub fn focus_column_right_or_first(&mut self) {
326 self.active_workspace().focus_column_right_or_first();
327 }
328
329 pub fn focus_column_left_or_last(&mut self) {
330 self.active_workspace().focus_column_left_or_last();
331 }
332
333 pub fn focus_down(&mut self) {
334 self.active_workspace().focus_down();
335 }
336
337 pub fn focus_up(&mut self) {
338 self.active_workspace().focus_up();
339 }
340
341 pub fn focus_down_or_left(&mut self) {
342 let workspace = self.active_workspace();
343 if !workspace.columns.is_empty() {
344 let column = &workspace.columns[workspace.active_column_idx];
345 let curr_idx = column.active_tile_idx;
346 let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1);
347 if curr_idx == new_idx {
348 self.focus_left();
349 } else {
350 workspace.focus_down();
351 }
352 }
353 }
354
355 pub fn focus_down_or_right(&mut self) {
356 let workspace = self.active_workspace();
357 if !workspace.columns.is_empty() {
358 let column = &workspace.columns[workspace.active_column_idx];
359 let curr_idx = column.active_tile_idx;
360 let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1);
361 if curr_idx == new_idx {
362 self.focus_right();
363 } else {
364 workspace.focus_down();
365 }
366 }
367 }
368
369 pub fn focus_up_or_left(&mut self) {
370 let workspace = self.active_workspace();
371 if !workspace.columns.is_empty() {
372 let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx;
373 let new_idx = curr_idx.saturating_sub(1);
374 if curr_idx == new_idx {
375 self.focus_left();
376 } else {
377 workspace.focus_up();
378 }
379 }
380 }
381
382 pub fn focus_up_or_right(&mut self) {
383 let workspace = self.active_workspace();
384 if workspace.columns.is_empty() {
385 self.switch_workspace_up();
386 } else {
387 let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx;
388 let new_idx = curr_idx.saturating_sub(1);
389 if curr_idx == new_idx {
390 self.focus_left();
391 } else {
392 workspace.focus_up();
393 }
394 }
395 }
396
397 pub fn focus_window_or_workspace_down(&mut self) {
398 let workspace = self.active_workspace();
399 if workspace.columns.is_empty() {
400 self.switch_workspace_down();
401 } else {
402 let column = &workspace.columns[workspace.active_column_idx];
403 let curr_idx = column.active_tile_idx;
404 let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1);
405 if curr_idx == new_idx {
406 self.switch_workspace_down();
407 } else {
408 workspace.focus_down();
409 }
410 }
411 }
412
413 pub fn focus_window_or_workspace_up(&mut self) {
414 let workspace = self.active_workspace();
415 if workspace.columns.is_empty() {
416 self.switch_workspace_up();
417 } else {
418 let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx;
419 let new_idx = curr_idx.saturating_sub(1);
420 if curr_idx == new_idx {
421 self.switch_workspace_up();
422 } else {
423 workspace.focus_up();
424 }
425 }
426 }
427
428 pub fn move_to_workspace_up(&mut self) {
429 let source_workspace_idx = self.active_workspace_idx;
430
431 let new_idx = source_workspace_idx.saturating_sub(1);
432 if new_idx == source_workspace_idx {
433 return;
434 }
435
436 let workspace = &mut self.workspaces[source_workspace_idx];
437 if workspace.columns.is_empty() {
438 return;
439 }
440
441 let column = &workspace.columns[workspace.active_column_idx];
442 let width = column.width;
443 let is_full_width = column.is_full_width;
444 let window = workspace
445 .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx, None)
446 .into_window();
447
448 self.add_window(new_idx, window, true, width, is_full_width);
449 }
450
451 pub fn move_to_workspace_down(&mut self) {
452 let source_workspace_idx = self.active_workspace_idx;
453
454 let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1);
455 if new_idx == source_workspace_idx {
456 return;
457 }
458
459 let workspace = &mut self.workspaces[source_workspace_idx];
460 if workspace.columns.is_empty() {
461 return;
462 }
463
464 let column = &workspace.columns[workspace.active_column_idx];
465 let width = column.width;
466 let is_full_width = column.is_full_width;
467 let window = workspace
468 .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx, None)
469 .into_window();
470
471 self.add_window(new_idx, window, true, width, is_full_width);
472 }
473
474 pub fn move_to_workspace(&mut self, idx: usize) {
475 let source_workspace_idx = self.active_workspace_idx;
476
477 let new_idx = min(idx, self.workspaces.len() - 1);
478 if new_idx == source_workspace_idx {
479 return;
480 }
481
482 let workspace = &mut self.workspaces[source_workspace_idx];
483 if workspace.columns.is_empty() {
484 return;
485 }
486
487 let column = &workspace.columns[workspace.active_column_idx];
488 let width = column.width;
489 let is_full_width = column.is_full_width;
490 let window = workspace
491 .remove_tile_by_idx(workspace.active_column_idx, column.active_tile_idx, None)
492 .into_window();
493
494 self.add_window(new_idx, window, true, width, is_full_width);
495
496 // Don't animate this action.
497 self.workspace_switch = None;
498
499 self.clean_up_workspaces();
500 }
501
502 pub fn move_column_to_workspace_up(&mut self) {
503 let source_workspace_idx = self.active_workspace_idx;
504
505 let new_idx = source_workspace_idx.saturating_sub(1);
506 if new_idx == source_workspace_idx {
507 return;
508 }
509
510 let workspace = &mut self.workspaces[source_workspace_idx];
511 if workspace.columns.is_empty() {
512 return;
513 }
514
515 let column = workspace.remove_column_by_idx(workspace.active_column_idx);
516 self.add_column(new_idx, column, true);
517 }
518
519 pub fn move_column_to_workspace_down(&mut self) {
520 let source_workspace_idx = self.active_workspace_idx;
521
522 let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1);
523 if new_idx == source_workspace_idx {
524 return;
525 }
526
527 let workspace = &mut self.workspaces[source_workspace_idx];
528 if workspace.columns.is_empty() {
529 return;
530 }
531
532 let column = workspace.remove_column_by_idx(workspace.active_column_idx);
533 self.add_column(new_idx, column, true);
534 }
535
536 pub fn move_column_to_workspace(&mut self, idx: usize) {
537 let source_workspace_idx = self.active_workspace_idx;
538
539 let new_idx = min(idx, self.workspaces.len() - 1);
540 if new_idx == source_workspace_idx {
541 return;
542 }
543
544 let workspace = &mut self.workspaces[source_workspace_idx];
545 if workspace.columns.is_empty() {
546 return;
547 }
548
549 let column = workspace.remove_column_by_idx(workspace.active_column_idx);
550 self.add_column(new_idx, column, true);
551
552 // Don't animate this action.
553 self.workspace_switch = None;
554
555 self.clean_up_workspaces();
556 }
557
558 pub fn switch_workspace_up(&mut self) {
559 self.activate_workspace(self.active_workspace_idx.saturating_sub(1));
560 }
561
562 pub fn switch_workspace_down(&mut self) {
563 self.activate_workspace(min(
564 self.active_workspace_idx + 1,
565 self.workspaces.len() - 1,
566 ));
567 }
568
569 fn previous_workspace_idx(&self) -> Option<usize> {
570 let id = self.previous_workspace_id?;
571 self.workspaces.iter().position(|w| w.id() == id)
572 }
573
574 pub fn switch_workspace(&mut self, idx: usize, animate: bool) {
575 self.activate_workspace(min(idx, self.workspaces.len() - 1));
576
577 if !animate {
578 self.workspace_switch = None;
579 self.clean_up_workspaces();
580 }
581 }
582
583 pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) {
584 let idx = min(idx, self.workspaces.len() - 1);
585
586 if idx == self.active_workspace_idx {
587 if let Some(prev_idx) = self.previous_workspace_idx() {
588 self.switch_workspace(prev_idx, false);
589 }
590 } else {
591 self.switch_workspace(idx, false);
592 }
593 }
594
595 pub fn switch_workspace_previous(&mut self) {
596 if let Some(idx) = self.previous_workspace_idx() {
597 self.switch_workspace(idx, false);
598 }
599 }
600
601 pub fn consume_into_column(&mut self) {
602 self.active_workspace().consume_into_column();
603 }
604
605 pub fn expel_from_column(&mut self) {
606 self.active_workspace().expel_from_column();
607 }
608
609 pub fn center_column(&mut self) {
610 self.active_workspace().center_column();
611 }
612
613 pub fn focus(&self) -> Option<&W> {
614 let workspace = &self.workspaces[self.active_workspace_idx];
615 if !workspace.has_windows() {
616 return None;
617 }
618
619 let column = &workspace.columns[workspace.active_column_idx];
620 Some(column.tiles[column.active_tile_idx].window())
621 }
622
623 pub fn advance_animations(&mut self, current_time: Duration) {
624 if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch {
625 anim.set_current_time(current_time);
626 if anim.is_done() {
627 self.workspace_switch = None;
628 self.clean_up_workspaces();
629 }
630 }
631
632 for ws in &mut self.workspaces {
633 ws.advance_animations(current_time);
634 }
635 }
636
637 pub fn are_animations_ongoing(&self) -> bool {
638 self.workspace_switch
639 .as_ref()
640 .is_some_and(|s| s.is_animation())
641 || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
642 }
643
644 pub fn are_transitions_ongoing(&self) -> bool {
645 self.workspace_switch.is_some()
646 || self
647 .workspaces
648 .iter()
649 .any(|ws| ws.are_transitions_ongoing())
650 }
651
652 pub fn update_render_elements(&mut self, is_active: bool) {
653 match &self.workspace_switch {
654 Some(switch) => {
655 let render_idx = switch.current_idx();
656 let before_idx = render_idx.floor();
657 let after_idx = render_idx.ceil();
658
659 if after_idx < 0. || before_idx as usize >= self.workspaces.len() {
660 return;
661 }
662
663 let after_idx = after_idx as usize;
664 if after_idx < self.workspaces.len() {
665 self.workspaces[after_idx].update_render_elements(is_active);
666
667 if before_idx < 0. {
668 return;
669 }
670 }
671
672 let before_idx = before_idx as usize;
673 self.workspaces[before_idx].update_render_elements(is_active);
674 }
675 None => {
676 self.workspaces[self.active_workspace_idx].update_render_elements(is_active);
677 }
678 }
679 }
680
681 pub fn update_config(&mut self, options: Rc<Options>) {
682 for ws in &mut self.workspaces {
683 ws.update_config(options.clone());
684 }
685
686 if self.options.struts != options.struts {
687 let scale = self.output.current_scale();
688 let transform = self.output.current_transform();
689 let view_size = output_size(&self.output);
690 let working_area = compute_working_area(&self.output, options.struts);
691
692 for ws in &mut self.workspaces {
693 ws.set_view_size(scale, transform, view_size, working_area);
694 }
695 }
696
697 self.options = options;
698 }
699
700 pub fn toggle_width(&mut self) {
701 self.active_workspace().toggle_width();
702 }
703
704 pub fn toggle_full_width(&mut self) {
705 self.active_workspace().toggle_full_width();
706 }
707
708 pub fn set_column_width(&mut self, change: SizeChange) {
709 self.active_workspace().set_column_width(change);
710 }
711
712 pub fn set_window_height(&mut self, change: SizeChange) {
713 self.active_workspace().set_window_height(change);
714 }
715
716 pub fn reset_window_height(&mut self) {
717 self.active_workspace().reset_window_height();
718 }
719
720 pub fn move_workspace_down(&mut self) {
721 let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);
722 if new_idx == self.active_workspace_idx {
723 return;
724 }
725
726 self.workspaces.swap(self.active_workspace_idx, new_idx);
727
728 if new_idx == self.workspaces.len() - 1 {
729 // Insert a new empty workspace.
730 let ws = Workspace::new(self.output.clone(), self.options.clone());
731 self.workspaces.push(ws);
732 }
733
734 let previous_workspace_id = self.previous_workspace_id;
735 self.activate_workspace(new_idx);
736 self.workspace_switch = None;
737 self.previous_workspace_id = previous_workspace_id;
738
739 self.clean_up_workspaces();
740 }
741
742 pub fn move_workspace_up(&mut self) {
743 let new_idx = self.active_workspace_idx.saturating_sub(1);
744 if new_idx == self.active_workspace_idx {
745 return;
746 }
747
748 self.workspaces.swap(self.active_workspace_idx, new_idx);
749
750 if self.active_workspace_idx == self.workspaces.len() - 1 {
751 // Insert a new empty workspace.
752 let ws = Workspace::new(self.output.clone(), self.options.clone());
753 self.workspaces.push(ws);
754 }
755
756 let previous_workspace_id = self.previous_workspace_id;
757 self.activate_workspace(new_idx);
758 self.workspace_switch = None;
759 self.previous_workspace_id = previous_workspace_id;
760
761 self.clean_up_workspaces();
762 }
763
764 /// Returns the geometry of the active tile relative to and clamped to the output.
765 ///
766 /// During animations, assumes the final view position.
767 pub fn active_tile_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> {
768 let mut rect = self.active_workspace_ref().active_tile_visual_rectangle()?;
769
770 if let Some(switch) = &self.workspace_switch {
771 let size = output_size(&self.output).to_f64();
772
773 let offset = switch.target_idx() - self.active_workspace_idx as f64;
774 let offset = offset * size.h;
775
776 let clip_rect = Rectangle::from_loc_and_size((0., -offset), size);
777 rect = rect.intersection(clip_rect)?;
778 }
779
780 Some(rect)
781 }
782
783 pub fn window_under(
784 &self,
785 pos_within_output: Point<f64, Logical>,
786 ) -> Option<(&W, Option<Point<f64, Logical>>)> {
787 match &self.workspace_switch {
788 Some(switch) => {
789 let size = output_size(&self.output).to_f64();
790
791 let render_idx = switch.current_idx();
792 let before_idx = render_idx.floor();
793 let after_idx = render_idx.ceil();
794
795 let offset = (render_idx - before_idx) * size.h;
796
797 if after_idx < 0. || before_idx as usize >= self.workspaces.len() {
798 return None;
799 }
800
801 let after_idx = after_idx as usize;
802
803 let (idx, ws_offset) = if pos_within_output.y < size.h - offset {
804 if before_idx < 0. {
805 return None;
806 }
807
808 (before_idx as usize, Point::from((0., offset)))
809 } else {
810 if after_idx >= self.workspaces.len() {
811 return None;
812 }
813
814 (after_idx, Point::from((0., -size.h + offset)))
815 };
816
817 let ws = &self.workspaces[idx];
818 let (win, win_pos) = ws.window_under(pos_within_output + ws_offset)?;
819 Some((win, win_pos.map(|p| p - ws_offset)))
820 }
821 None => {
822 let ws = &self.workspaces[self.active_workspace_idx];
823 ws.window_under(pos_within_output)
824 }
825 }
826 }
827
828 pub fn resize_edges_under(&self, pos_within_output: Point<f64, Logical>) -> Option<ResizeEdge> {
829 match &self.workspace_switch {
830 Some(switch) => {
831 let size = output_size(&self.output);
832
833 let render_idx = switch.current_idx();
834 let before_idx = render_idx.floor();
835 let after_idx = render_idx.ceil();
836
837 let offset = (render_idx - before_idx) * size.h;
838
839 if after_idx < 0. || before_idx as usize >= self.workspaces.len() {
840 return None;
841 }
842
843 let after_idx = after_idx as usize;
844
845 let (idx, ws_offset) = if pos_within_output.y < size.h - offset {
846 if before_idx < 0. {
847 return None;
848 }
849
850 (before_idx as usize, Point::from((0., offset)))
851 } else {
852 if after_idx >= self.workspaces.len() {
853 return None;
854 }
855
856 (after_idx, Point::from((0., -size.h + offset)))
857 };
858
859 let ws = &self.workspaces[idx];
860 ws.resize_edges_under(pos_within_output + ws_offset)
861 }
862 None => {
863 let ws = &self.workspaces[self.active_workspace_idx];
864 ws.resize_edges_under(pos_within_output)
865 }
866 }
867 }
868
869 pub fn render_above_top_layer(&self) -> bool {
870 // Render above the top layer only if the view is stationary.
871 if self.workspace_switch.is_some() {
872 return false;
873 }
874
875 let ws = &self.workspaces[self.active_workspace_idx];
876 ws.render_above_top_layer()
877 }
878
879 pub fn render_elements<R: NiriRenderer>(
880 &self,
881 renderer: &mut R,
882 target: RenderTarget,
883 ) -> Vec<MonitorRenderElement<R>> {
884 let _span = tracy_client::span!("Monitor::render_elements");
885
886 let scale = self.output.current_scale().fractional_scale();
887 let size = output_size(&self.output);
888
889 match &self.workspace_switch {
890 Some(switch) => {
891 let render_idx = switch.current_idx();
892 let before_idx = render_idx.floor();
893 let after_idx = render_idx.ceil();
894
895 let offset = (render_idx - before_idx) * size.h;
896
897 if after_idx < 0. || before_idx as usize >= self.workspaces.len() {
898 return vec![];
899 }
900
901 let after_idx = after_idx as usize;
902 let after = if after_idx < self.workspaces.len() {
903 let after = self.workspaces[after_idx].render_elements(renderer, target);
904 let after = after.into_iter().filter_map(|elem| {
905 Some(RelocateRenderElement::from_element(
906 CropRenderElement::from_element(
907 elem,
908 scale,
909 // HACK: crop to infinite bounds for all sides except the side
910 // where the workspaces join,
911 // otherwise it will cut pixel shaders and mess up
912 // the coordinate space.
913 Rectangle::from_extemities(
914 (-i32::MAX / 2, 0),
915 (i32::MAX / 2, i32::MAX / 2),
916 ),
917 )?,
918 Point::from((0., -offset + size.h)).to_physical_precise_round(scale),
919 Relocate::Relative,
920 ))
921 });
922
923 if before_idx < 0. {
924 return after.collect();
925 }
926
927 Some(after)
928 } else {
929 None
930 };
931
932 let before_idx = before_idx as usize;
933 let before = self.workspaces[before_idx].render_elements(renderer, target);
934 let before = before.into_iter().filter_map(|elem| {
935 Some(RelocateRenderElement::from_element(
936 CropRenderElement::from_element(
937 elem,
938 scale,
939 Rectangle::from_extemities(
940 (-i32::MAX / 2, -i32::MAX / 2),
941 (i32::MAX / 2, to_physical_precise_round(scale, size.h)),
942 ),
943 )?,
944 Point::from((0., -offset)).to_physical_precise_round(scale),
945 Relocate::Relative,
946 ))
947 });
948 before.chain(after.into_iter().flatten()).collect()
949 }
950 None => {
951 let elements =
952 self.workspaces[self.active_workspace_idx].render_elements(renderer, target);
953 elements
954 .into_iter()
955 .filter_map(|elem| {
956 Some(RelocateRenderElement::from_element(
957 CropRenderElement::from_element(
958 elem,
959 scale,
960 // HACK: set infinite crop bounds due to a damage tracking bug
961 // which causes glitched rendering for maximized GTK windows.
962 // FIXME: use proper bounds after fixing the Crop element.
963 Rectangle::from_loc_and_size(
964 (-i32::MAX / 2, -i32::MAX / 2),
965 (i32::MAX, i32::MAX),
966 ),
967 // Rectangle::from_loc_and_size((0, 0), size),
968 )?,
969 (0, 0),
970 Relocate::Relative,
971 ))
972 })
973 .collect()
974 }
975 }
976 }
977
978 pub fn workspace_switch_gesture_begin(&mut self, is_touchpad: bool) {
979 let center_idx = self.active_workspace_idx;
980 let current_idx = self
981 .workspace_switch
982 .as_ref()
983 .map(|s| s.current_idx())
984 .unwrap_or(center_idx as f64);
985
986 let gesture = WorkspaceSwitchGesture {
987 center_idx,
988 current_idx,
989 tracker: SwipeTracker::new(),
990 is_touchpad,
991 };
992 self.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
993 }
994
995 pub fn workspace_switch_gesture_update(
996 &mut self,
997 delta_y: f64,
998 timestamp: Duration,
999 is_touchpad: bool,
1000 ) -> Option<bool> {
1001 let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
1002 return None;
1003 };
1004
1005 if gesture.is_touchpad != is_touchpad {
1006 return None;
1007 }
1008
1009 gesture.tracker.push(delta_y, timestamp);
1010
1011 let total_height = if gesture.is_touchpad {
1012 WORKSPACE_GESTURE_MOVEMENT
1013 } else {
1014 self.workspaces[0].view_size().h
1015 };
1016 let pos = gesture.tracker.pos() / total_height;
1017
1018 let min = gesture.center_idx.saturating_sub(1) as f64;
1019 let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
1020 let new_idx = gesture.center_idx as f64 + pos;
1021 let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx);
1022
1023 if gesture.current_idx == new_idx {
1024 return Some(false);
1025 }
1026
1027 gesture.current_idx = new_idx;
1028 Some(true)
1029 }
1030
1031 pub fn workspace_switch_gesture_end(
1032 &mut self,
1033 cancelled: bool,
1034 is_touchpad: Option<bool>,
1035 ) -> bool {
1036 let Some(WorkspaceSwitch::Gesture(gesture)) = &mut self.workspace_switch else {
1037 return false;
1038 };
1039
1040 if is_touchpad.map_or(false, |x| gesture.is_touchpad != x) {
1041 return false;
1042 }
1043
1044 if cancelled {
1045 self.workspace_switch = None;
1046 self.clean_up_workspaces();
1047 return true;
1048 }
1049
1050 let total_height = if gesture.is_touchpad {
1051 WORKSPACE_GESTURE_MOVEMENT
1052 } else {
1053 self.workspaces[0].view_size().h
1054 };
1055
1056 let mut velocity = gesture.tracker.velocity() / total_height;
1057 let current_pos = gesture.tracker.pos() / total_height;
1058 let pos = gesture.tracker.projected_end_pos() / total_height;
1059
1060 let min = gesture.center_idx.saturating_sub(1) as f64;
1061 let max = (gesture.center_idx + 1).min(self.workspaces.len() - 1) as f64;
1062 let new_idx = gesture.center_idx as f64 + pos;
1063
1064 let new_idx = WORKSPACE_GESTURE_RUBBER_BAND.clamp(min, max, new_idx);
1065 let new_idx = new_idx.round() as usize;
1066
1067 velocity *= WORKSPACE_GESTURE_RUBBER_BAND.clamp_derivative(
1068 min,
1069 max,
1070 gesture.center_idx as f64 + current_pos,
1071 );
1072
1073 self.previous_workspace_id = Some(self.workspaces[self.active_workspace_idx].id());
1074
1075 self.active_workspace_idx = new_idx;
1076 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1077 gesture.current_idx,
1078 new_idx as f64,
1079 velocity,
1080 self.options.animations.workspace_switch.0,
1081 )));
1082
1083 true
1084 }
1085}