forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBa9fcbf8Ivan Molodetskikh2 years ago671 commits

monitor.rs682 lines, 21.6 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, Scale};
11
12use super::workspace::{
13 compute_working_area, Column, ColumnWidth, OutputId, Workspace, WorkspaceRenderElement,
14};
15use super::{LayoutElement, Options};
16use crate::animation::Animation;
17use crate::render_helpers::renderer::NiriRenderer;
18use crate::utils::output_size;
19
20#[derive(Debug)]
21pub struct Monitor<W: LayoutElement> {
22 /// Output for this monitor.
23 pub output: Output,
24 // Must always contain at least one.
25 pub workspaces: Vec<Workspace<W>>,
26 /// Index of the currently active workspace.
27 pub active_workspace_idx: usize,
28 /// In-progress switch between workspaces.
29 pub workspace_switch: Option<WorkspaceSwitch>,
30 /// Configurable properties of the layout.
31 pub options: Rc<Options>,
32}
33
34#[derive(Debug)]
35pub enum WorkspaceSwitch {
36 Animation(Animation),
37 Gesture(WorkspaceSwitchGesture),
38}
39
40#[derive(Debug)]
41pub struct WorkspaceSwitchGesture {
42 /// Index of the workspace where the gesture was started.
43 pub center_idx: usize,
44 /// Current, fractional workspace index.
45 pub current_idx: f64,
46}
47
48pub type MonitorRenderElement<R> =
49 RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;
50
51impl WorkspaceSwitch {
52 pub fn current_idx(&self) -> f64 {
53 match self {
54 WorkspaceSwitch::Animation(anim) => anim.value(),
55 WorkspaceSwitch::Gesture(gesture) => gesture.current_idx,
56 }
57 }
58
59 /// Returns `true` if the workspace switch is [`Animation`].
60 ///
61 /// [`Animation`]: WorkspaceSwitch::Animation
62 #[must_use]
63 fn is_animation(&self) -> bool {
64 matches!(self, Self::Animation(..))
65 }
66}
67
68impl<W: LayoutElement> Monitor<W> {
69 pub fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self {
70 Self {
71 output,
72 workspaces,
73 active_workspace_idx: 0,
74 workspace_switch: None,
75 options,
76 }
77 }
78
79 pub fn active_workspace(&mut self) -> &mut Workspace<W> {
80 &mut self.workspaces[self.active_workspace_idx]
81 }
82
83 fn activate_workspace(&mut self, idx: usize) {
84 if self.active_workspace_idx == idx {
85 return;
86 }
87
88 let current_idx = self
89 .workspace_switch
90 .as_ref()
91 .map(|s| s.current_idx())
92 .unwrap_or(self.active_workspace_idx as f64);
93
94 self.active_workspace_idx = idx;
95
96 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
97 current_idx,
98 idx as f64,
99 self.options.animations.workspace_switch,
100 niri_config::Animation::default_workspace_switch(),
101 )));
102 }
103
104 pub fn add_window(
105 &mut self,
106 workspace_idx: usize,
107 window: W,
108 activate: bool,
109 width: ColumnWidth,
110 is_full_width: bool,
111 ) {
112 let workspace = &mut self.workspaces[workspace_idx];
113
114 workspace.add_window(window, activate, width, is_full_width);
115
116 // After adding a new window, workspace becomes this output's own.
117 workspace.original_output = OutputId::new(&self.output);
118
119 if workspace_idx == self.workspaces.len() - 1 {
120 // Insert a new empty workspace.
121 let ws = Workspace::new(self.output.clone(), self.options.clone());
122 self.workspaces.push(ws);
123 }
124
125 if activate {
126 self.activate_workspace(workspace_idx);
127 }
128 }
129
130 pub fn add_window_right_of(
131 &mut self,
132 right_of: &W,
133 window: W,
134 width: ColumnWidth,
135 is_full_width: bool,
136 ) {
137 let workspace_idx = self
138 .workspaces
139 .iter_mut()
140 .position(|ws| ws.has_window(right_of))
141 .unwrap();
142 let workspace = &mut self.workspaces[workspace_idx];
143
144 workspace.add_window_right_of(right_of, window, width, is_full_width);
145
146 // After adding a new window, workspace becomes this output's own.
147 workspace.original_output = OutputId::new(&self.output);
148 }
149
150 pub fn add_column(&mut self, workspace_idx: usize, column: Column<W>, activate: bool) {
151 let workspace = &mut self.workspaces[workspace_idx];
152
153 workspace.add_column(column, activate);
154
155 // After adding a new window, workspace becomes this output's own.
156 workspace.original_output = OutputId::new(&self.output);
157
158 if workspace_idx == self.workspaces.len() - 1 {
159 // Insert a new empty workspace.
160 let ws = Workspace::new(self.output.clone(), self.options.clone());
161 self.workspaces.push(ws);
162 }
163
164 if activate {
165 self.activate_workspace(workspace_idx);
166 }
167 }
168
169 pub fn clean_up_workspaces(&mut self) {
170 assert!(self.workspace_switch.is_none());
171
172 for idx in (0..self.workspaces.len() - 1).rev() {
173 if self.active_workspace_idx == idx {
174 continue;
175 }
176
177 if !self.workspaces[idx].has_windows() {
178 self.workspaces.remove(idx);
179 if self.active_workspace_idx > idx {
180 self.active_workspace_idx -= 1;
181 }
182 }
183 }
184 }
185
186 pub fn move_left(&mut self) {
187 self.active_workspace().move_left();
188 }
189
190 pub fn move_right(&mut self) {
191 self.active_workspace().move_right();
192 }
193
194 pub fn move_column_to_first(&mut self) {
195 self.active_workspace().move_column_to_first();
196 }
197
198 pub fn move_column_to_last(&mut self) {
199 self.active_workspace().move_column_to_last();
200 }
201
202 pub fn move_down(&mut self) {
203 self.active_workspace().move_down();
204 }
205
206 pub fn move_up(&mut self) {
207 self.active_workspace().move_up();
208 }
209
210 pub fn move_down_or_to_workspace_down(&mut self) {
211 let workspace = self.active_workspace();
212 if workspace.columns.is_empty() {
213 return;
214 }
215 let column = &mut workspace.columns[workspace.active_column_idx];
216 let curr_idx = column.active_tile_idx;
217 let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1);
218 if curr_idx == new_idx {
219 self.move_to_workspace_down();
220 } else {
221 workspace.move_down();
222 }
223 }
224
225 pub fn move_up_or_to_workspace_up(&mut self) {
226 let workspace = self.active_workspace();
227 if workspace.columns.is_empty() {
228 return;
229 }
230 let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx;
231 let new_idx = curr_idx.saturating_sub(1);
232 if curr_idx == new_idx {
233 self.move_to_workspace_up();
234 } else {
235 workspace.move_up();
236 }
237 }
238
239 pub fn consume_or_expel_window_left(&mut self) {
240 self.active_workspace().consume_or_expel_window_left();
241 }
242
243 pub fn consume_or_expel_window_right(&mut self) {
244 self.active_workspace().consume_or_expel_window_right();
245 }
246
247 pub fn focus_left(&mut self) {
248 self.active_workspace().focus_left();
249 }
250
251 pub fn focus_right(&mut self) {
252 self.active_workspace().focus_right();
253 }
254
255 pub fn focus_column_first(&mut self) {
256 self.active_workspace().focus_column_first();
257 }
258
259 pub fn focus_column_last(&mut self) {
260 self.active_workspace().focus_column_last();
261 }
262
263 pub fn focus_down(&mut self) {
264 self.active_workspace().focus_down();
265 }
266
267 pub fn focus_up(&mut self) {
268 self.active_workspace().focus_up();
269 }
270
271 pub fn focus_window_or_workspace_down(&mut self) {
272 let workspace = self.active_workspace();
273 if workspace.columns.is_empty() {
274 self.switch_workspace_down();
275 } else {
276 let column = &workspace.columns[workspace.active_column_idx];
277 let curr_idx = column.active_tile_idx;
278 let new_idx = min(column.active_tile_idx + 1, column.tiles.len() - 1);
279 if curr_idx == new_idx {
280 self.switch_workspace_down();
281 } else {
282 workspace.focus_down();
283 }
284 }
285 }
286
287 pub fn focus_window_or_workspace_up(&mut self) {
288 let workspace = self.active_workspace();
289 if workspace.columns.is_empty() {
290 self.switch_workspace_up();
291 } else {
292 let curr_idx = workspace.columns[workspace.active_column_idx].active_tile_idx;
293 let new_idx = curr_idx.saturating_sub(1);
294 if curr_idx == new_idx {
295 self.switch_workspace_up();
296 } else {
297 workspace.focus_up();
298 }
299 }
300 }
301
302 pub fn move_to_workspace_up(&mut self) {
303 let source_workspace_idx = self.active_workspace_idx;
304
305 let new_idx = source_workspace_idx.saturating_sub(1);
306 if new_idx == source_workspace_idx {
307 return;
308 }
309
310 let workspace = &mut self.workspaces[source_workspace_idx];
311 if workspace.columns.is_empty() {
312 return;
313 }
314
315 let column = &workspace.columns[workspace.active_column_idx];
316 let width = column.width;
317 let is_full_width = column.is_full_width;
318 let window =
319 workspace.remove_window_by_idx(workspace.active_column_idx, column.active_tile_idx);
320
321 self.add_window(new_idx, window, true, width, is_full_width);
322 }
323
324 pub fn move_to_workspace_down(&mut self) {
325 let source_workspace_idx = self.active_workspace_idx;
326
327 let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1);
328 if new_idx == source_workspace_idx {
329 return;
330 }
331
332 let workspace = &mut self.workspaces[source_workspace_idx];
333 if workspace.columns.is_empty() {
334 return;
335 }
336
337 let column = &workspace.columns[workspace.active_column_idx];
338 let width = column.width;
339 let is_full_width = column.is_full_width;
340 let window =
341 workspace.remove_window_by_idx(workspace.active_column_idx, column.active_tile_idx);
342
343 self.add_window(new_idx, window, true, width, is_full_width);
344 }
345
346 pub fn move_to_workspace(&mut self, idx: usize) {
347 let source_workspace_idx = self.active_workspace_idx;
348
349 let new_idx = min(idx, self.workspaces.len() - 1);
350 if new_idx == source_workspace_idx {
351 return;
352 }
353
354 let workspace = &mut self.workspaces[source_workspace_idx];
355 if workspace.columns.is_empty() {
356 return;
357 }
358
359 let column = &workspace.columns[workspace.active_column_idx];
360 let width = column.width;
361 let is_full_width = column.is_full_width;
362 let window =
363 workspace.remove_window_by_idx(workspace.active_column_idx, column.active_tile_idx);
364
365 self.add_window(new_idx, window, true, width, is_full_width);
366
367 // Don't animate this action.
368 self.workspace_switch = None;
369
370 self.clean_up_workspaces();
371 }
372
373 pub fn move_column_to_workspace_up(&mut self) {
374 let source_workspace_idx = self.active_workspace_idx;
375
376 let new_idx = source_workspace_idx.saturating_sub(1);
377 if new_idx == source_workspace_idx {
378 return;
379 }
380
381 let workspace = &mut self.workspaces[source_workspace_idx];
382 if workspace.columns.is_empty() {
383 return;
384 }
385
386 let column = workspace.remove_column_by_idx(workspace.active_column_idx);
387 self.add_column(new_idx, column, true);
388 }
389
390 pub fn move_column_to_workspace_down(&mut self) {
391 let source_workspace_idx = self.active_workspace_idx;
392
393 let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1);
394 if new_idx == source_workspace_idx {
395 return;
396 }
397
398 let workspace = &mut self.workspaces[source_workspace_idx];
399 if workspace.columns.is_empty() {
400 return;
401 }
402
403 let column = workspace.remove_column_by_idx(workspace.active_column_idx);
404 self.add_column(new_idx, column, true);
405 }
406
407 pub fn move_column_to_workspace(&mut self, idx: usize) {
408 let source_workspace_idx = self.active_workspace_idx;
409
410 let new_idx = min(idx, self.workspaces.len() - 1);
411 if new_idx == source_workspace_idx {
412 return;
413 }
414
415 let workspace = &mut self.workspaces[source_workspace_idx];
416 if workspace.columns.is_empty() {
417 return;
418 }
419
420 let column = workspace.remove_column_by_idx(workspace.active_column_idx);
421 self.add_column(new_idx, column, true);
422
423 // Don't animate this action.
424 self.workspace_switch = None;
425
426 self.clean_up_workspaces();
427 }
428
429 pub fn switch_workspace_up(&mut self) {
430 self.activate_workspace(self.active_workspace_idx.saturating_sub(1));
431 }
432
433 pub fn switch_workspace_down(&mut self) {
434 self.activate_workspace(min(
435 self.active_workspace_idx + 1,
436 self.workspaces.len() - 1,
437 ));
438 }
439
440 pub fn switch_workspace(&mut self, idx: usize) {
441 self.activate_workspace(min(idx, self.workspaces.len() - 1));
442 // Don't animate this action.
443 self.workspace_switch = None;
444
445 self.clean_up_workspaces();
446 }
447
448 pub fn consume_into_column(&mut self) {
449 self.active_workspace().consume_into_column();
450 }
451
452 pub fn expel_from_column(&mut self) {
453 self.active_workspace().expel_from_column();
454 }
455
456 pub fn center_column(&mut self) {
457 self.active_workspace().center_column();
458 }
459
460 pub fn focus(&self) -> Option<&W> {
461 let workspace = &self.workspaces[self.active_workspace_idx];
462 if !workspace.has_windows() {
463 return None;
464 }
465
466 let column = &workspace.columns[workspace.active_column_idx];
467 Some(column.tiles[column.active_tile_idx].window())
468 }
469
470 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
471 if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch {
472 anim.set_current_time(current_time);
473 if anim.is_done() {
474 self.workspace_switch = None;
475 self.clean_up_workspaces();
476 }
477 }
478
479 for ws in &mut self.workspaces {
480 ws.advance_animations(current_time, is_active);
481 }
482 }
483
484 pub fn are_animations_ongoing(&self) -> bool {
485 self.workspace_switch
486 .as_ref()
487 .is_some_and(|s| s.is_animation())
488 || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
489 }
490
491 pub fn are_transitions_ongoing(&self) -> bool {
492 self.workspace_switch.is_some()
493 || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
494 }
495
496 pub fn update_config(&mut self, options: Rc<Options>) {
497 for ws in &mut self.workspaces {
498 ws.update_config(options.clone());
499 }
500
501 if self.options.struts != options.struts {
502 let view_size = output_size(&self.output);
503 let working_area = compute_working_area(&self.output, options.struts);
504
505 for ws in &mut self.workspaces {
506 ws.set_view_size(view_size, working_area);
507 }
508 }
509
510 self.options = options;
511 }
512
513 pub fn toggle_width(&mut self) {
514 self.active_workspace().toggle_width();
515 }
516
517 pub fn toggle_full_width(&mut self) {
518 self.active_workspace().toggle_full_width();
519 }
520
521 pub fn set_column_width(&mut self, change: SizeChange) {
522 self.active_workspace().set_column_width(change);
523 }
524
525 pub fn set_window_height(&mut self, change: SizeChange) {
526 self.active_workspace().set_window_height(change);
527 }
528
529 pub fn move_workspace_down(&mut self) {
530 let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);
531 if new_idx == self.active_workspace_idx {
532 return;
533 }
534
535 self.workspaces.swap(self.active_workspace_idx, new_idx);
536
537 if new_idx == self.workspaces.len() - 1 {
538 // Insert a new empty workspace.
539 let ws = Workspace::new(self.output.clone(), self.options.clone());
540 self.workspaces.push(ws);
541 }
542
543 self.activate_workspace(new_idx);
544 self.workspace_switch = None;
545
546 self.clean_up_workspaces();
547 }
548
549 pub fn move_workspace_up(&mut self) {
550 let new_idx = self.active_workspace_idx.saturating_sub(1);
551 if new_idx == self.active_workspace_idx {
552 return;
553 }
554
555 self.workspaces.swap(self.active_workspace_idx, new_idx);
556
557 if self.active_workspace_idx == self.workspaces.len() - 1 {
558 // Insert a new empty workspace.
559 let ws = Workspace::new(self.output.clone(), self.options.clone());
560 self.workspaces.push(ws);
561 }
562
563 self.activate_workspace(new_idx);
564 self.workspace_switch = None;
565
566 self.clean_up_workspaces();
567 }
568
569 pub fn window_under(
570 &self,
571 pos_within_output: Point<f64, Logical>,
572 ) -> Option<(&W, Option<Point<i32, Logical>>)> {
573 match &self.workspace_switch {
574 Some(switch) => {
575 let size = output_size(&self.output);
576
577 let render_idx = switch.current_idx();
578 let before_idx = render_idx.floor() as usize;
579 let after_idx = render_idx.ceil() as usize;
580
581 let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
582
583 let (idx, ws_offset) = if pos_within_output.y < (size.h - offset) as f64 {
584 (before_idx, Point::from((0, offset)))
585 } else {
586 (after_idx, Point::from((0, -size.h + offset)))
587 };
588
589 let ws = &self.workspaces[idx];
590 let (win, win_pos) = ws.window_under(pos_within_output + ws_offset.to_f64())?;
591 Some((win, win_pos.map(|p| p - ws_offset)))
592 }
593 None => {
594 let ws = &self.workspaces[self.active_workspace_idx];
595 ws.window_under(pos_within_output)
596 }
597 }
598 }
599
600 pub fn render_above_top_layer(&self) -> bool {
601 // Render above the top layer only if the view is stationary.
602 if self.workspace_switch.is_some() {
603 return false;
604 }
605
606 let ws = &self.workspaces[self.active_workspace_idx];
607 ws.render_above_top_layer()
608 }
609
610 pub fn render_elements<R: NiriRenderer>(
611 &self,
612 renderer: &mut R,
613 ) -> Vec<MonitorRenderElement<R>> {
614 let _span = tracy_client::span!("Monitor::render_elements");
615
616 let output_scale = Scale::from(self.output.current_scale().fractional_scale());
617 let output_transform = self.output.current_transform();
618 let output_mode = self.output.current_mode().unwrap();
619 let size = output_transform.transform_size(output_mode.size);
620
621 match &self.workspace_switch {
622 Some(switch) => {
623 let render_idx = switch.current_idx();
624 let before_idx = render_idx.floor() as usize;
625 let after_idx = render_idx.ceil() as usize;
626
627 let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
628
629 let before = self.workspaces[before_idx].render_elements(renderer);
630 let after = self.workspaces[after_idx].render_elements(renderer);
631
632 let before = before.into_iter().filter_map(|elem| {
633 Some(RelocateRenderElement::from_element(
634 CropRenderElement::from_element(
635 elem,
636 output_scale,
637 Rectangle::from_extemities((0, offset), (size.w, size.h)),
638 )?,
639 (0, -offset),
640 Relocate::Relative,
641 ))
642 });
643 let after = after.into_iter().filter_map(|elem| {
644 Some(RelocateRenderElement::from_element(
645 CropRenderElement::from_element(
646 elem,
647 output_scale,
648 Rectangle::from_extemities((0, 0), (size.w, offset)),
649 )?,
650 (0, -offset + size.h),
651 Relocate::Relative,
652 ))
653 });
654 before.chain(after).collect()
655 }
656 None => {
657 let elements = self.workspaces[self.active_workspace_idx].render_elements(renderer);
658 elements
659 .into_iter()
660 .filter_map(|elem| {
661 Some(RelocateRenderElement::from_element(
662 CropRenderElement::from_element(
663 elem,
664 output_scale,
665 // HACK: set infinite crop bounds due to a damage tracking bug
666 // which causes glitched rendering for maximized GTK windows.
667 // FIXME: use proper bounds after fixing the Crop element.
668 Rectangle::from_loc_and_size(
669 (-i32::MAX / 2, -i32::MAX / 2),
670 (i32::MAX, i32::MAX),
671 ),
672 // Rectangle::from_loc_and_size((0, 0), size),
673 )?,
674 (0, 0),
675 Relocate::Relative,
676 ))
677 })
678 .collect()
679 }
680 }
681 }
682}