forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB289ae36Ivan Molodetskikh2 years ago1428 commits

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