forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBdde9214Ivan Molodetskikh1 year ago1835 commits

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