forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB64a9351Ivan Molodetskikh2 years ago1319 commits

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