forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBf51dd67Ivan Molodetskikh2 years ago981 commits

mod.rs3212 lines, 104.7 KiBHistoryRawDownload
1//! Window layout logic.
2//!
3//! Niri implements scrollable tiling with workspaces. There's one primary output, and potentially
4//! multiple other outputs.
5//!
6//! Our layout has the following invariants:
7//!
8//! 1. Disconnecting and reconnecting the same output must not change the layout.
9//! * This includes both secondary outputs and the primary output.
10//! 2. Connecting an output must not change the layout for any workspaces that were never on that
11//! output.
12//!
13//! Therefore, we implement the following logic: every workspace keeps track of which output it
14//! originated on. When an output disconnects, its workspace (or workspaces, in case of the primary
15//! output disconnecting) are appended to the (potentially new) primary output, but remember their
16//! original output. Then, if the original output connects again, all workspaces originally from
17//! there move back to that output.
18//!
19//! In order to avoid surprising behavior, if the user creates or moves any new windows onto a
20//! workspace, it forgets its original output, and its current output becomes its original output.
21//! Imagine a scenario: the user works with a laptop and a monitor at home, then takes their laptop
22//! with them, disconnecting the monitor, and keeps working as normal, using the second monitor's
23//! workspace just like any other. Then they come back, reconnect the second monitor, and now we
24//! don't want an unassuming workspace to end up on it.
25//!
26//! ## Workspaces-only-on-primary considerations
27//!
28//! If this logic results in more than one workspace present on a secondary output, then as a
29//! compromise we only keep the first workspace there, and move the rest to the primary output,
30//! making the primary output their original output.
31
32use std::cmp::min;
33use std::mem;
34use std::rc::Rc;
35use std::time::Duration;
36
37use niri_config::{CenterFocusedColumn, Config, Struts};
38use niri_ipc::SizeChange;
39use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
40use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
41use smithay::backend::renderer::element::texture::TextureBuffer;
42use smithay::backend::renderer::element::Id;
43use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
44use smithay::output::Output;
45use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
46use smithay::utils::{Logical, Point, Scale, Size, Transform};
47
48use self::monitor::Monitor;
49pub use self::monitor::MonitorRenderElement;
50use self::workspace::{compute_working_area, Column, ColumnWidth, OutputId, Workspace};
51use crate::niri_render_elements;
52use crate::render_helpers::renderer::NiriRenderer;
53use crate::render_helpers::snapshot::RenderSnapshot;
54use crate::render_helpers::{BakedBuffer, RenderTarget};
55use crate::utils::output_size;
56use crate::window::ResolvedWindowRules;
57
58pub mod closing_window;
59pub mod focus_ring;
60pub mod monitor;
61pub mod tile;
62pub mod workspace;
63
64/// Size changes up to this many pixels don't animate.
65pub const RESIZE_ANIMATION_THRESHOLD: i32 = 10;
66
67niri_render_elements! {
68 LayoutElementRenderElement<R> => {
69 Wayland = WaylandSurfaceRenderElement<R>,
70 SolidColor = SolidColorRenderElement,
71 }
72}
73
74pub type LayoutElementRenderSnapshot =
75 RenderSnapshot<BakedBuffer<TextureBuffer<GlesTexture>>, BakedBuffer<SolidColorBuffer>>;
76
77pub trait LayoutElement {
78 /// Type that can be used as a unique ID of this element.
79 type Id: PartialEq;
80
81 /// Unique ID of this element.
82 fn id(&self) -> &Self::Id;
83
84 /// Visual size of the element.
85 ///
86 /// This is what the user would consider the size, i.e. excluding CSD shadows and whatnot.
87 /// Corresponds to the Wayland window geometry size.
88 fn size(&self) -> Size<i32, Logical>;
89
90 /// Returns the location of the element's buffer relative to the element's visual geometry.
91 ///
92 /// I.e. if the element has CSD shadows, its buffer location will have negative coordinates.
93 fn buf_loc(&self) -> Point<i32, Logical>;
94
95 /// Checks whether a point is in the element's input region.
96 ///
97 /// The point is relative to the element's visual geometry.
98 fn is_in_input_region(&self, point: Point<f64, Logical>) -> bool;
99
100 /// Renders the element at the given visual location.
101 ///
102 /// The element should be rendered in such a way that its visual geometry ends up at the given
103 /// location.
104 fn render<R: NiriRenderer>(
105 &self,
106 renderer: &mut R,
107 location: Point<i32, Logical>,
108 scale: Scale<f64>,
109 alpha: f32,
110 target: RenderTarget,
111 ) -> Vec<LayoutElementRenderElement<R>>;
112
113 fn request_size(&mut self, size: Size<i32, Logical>, animate: bool);
114 fn request_fullscreen(&self, size: Size<i32, Logical>);
115 fn min_size(&self) -> Size<i32, Logical>;
116 fn max_size(&self) -> Size<i32, Logical>;
117 fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool;
118 fn has_ssd(&self) -> bool;
119 fn set_preferred_scale_transform(&self, scale: i32, transform: Transform);
120 fn output_enter(&self, output: &Output);
121 fn output_leave(&self, output: &Output);
122 fn set_offscreen_element_id(&self, id: Option<Id>);
123 fn set_activated(&mut self, active: bool);
124 fn set_bounds(&self, bounds: Size<i32, Logical>);
125
126 fn send_pending_configure(&mut self);
127
128 /// Whether the element is currently fullscreen.
129 ///
130 /// This will *not* switch immediately after a [`LayoutElement::request_fullscreen()`] call.
131 fn is_fullscreen(&self) -> bool;
132
133 /// Whether we're requesting the element to be fullscreen.
134 ///
135 /// This *will* switch immediately after a [`LayoutElement::request_fullscreen()`] call.
136 fn is_pending_fullscreen(&self) -> bool;
137
138 fn rules(&self) -> &ResolvedWindowRules;
139
140 /// Runs periodic clean-up tasks.
141 fn refresh(&self);
142
143 fn take_unmap_snapshot(&self) -> Option<LayoutElementRenderSnapshot>;
144
145 fn animation_snapshot(&self) -> Option<&LayoutElementRenderSnapshot>;
146 fn take_animation_snapshot(&mut self) -> Option<LayoutElementRenderSnapshot>;
147}
148
149#[derive(Debug)]
150pub struct Layout<W: LayoutElement> {
151 /// Monitors and workspaes in the layout.
152 monitor_set: MonitorSet<W>,
153 /// Configurable properties of the layout.
154 options: Rc<Options>,
155}
156
157#[derive(Debug)]
158enum MonitorSet<W: LayoutElement> {
159 /// At least one output is connected.
160 Normal {
161 /// Connected monitors.
162 monitors: Vec<Monitor<W>>,
163 /// Index of the primary monitor.
164 primary_idx: usize,
165 /// Index of the active monitor.
166 active_monitor_idx: usize,
167 },
168 /// No outputs are connected, and these are the workspaces.
169 NoOutputs {
170 /// The workspaces.
171 workspaces: Vec<Workspace<W>>,
172 },
173}
174
175#[derive(Debug, PartialEq)]
176pub struct Options {
177 /// Padding around windows in logical pixels.
178 pub gaps: i32,
179 /// Extra padding around the working area in logical pixels.
180 pub struts: Struts,
181 pub focus_ring: niri_config::FocusRing,
182 pub border: niri_config::Border,
183 pub center_focused_column: CenterFocusedColumn,
184 /// Column widths that `toggle_width()` switches between.
185 pub preset_widths: Vec<ColumnWidth>,
186 /// Initial width for new columns.
187 pub default_width: Option<ColumnWidth>,
188 pub animations: niri_config::Animations,
189}
190
191impl Default for Options {
192 fn default() -> Self {
193 Self {
194 gaps: 16,
195 struts: Default::default(),
196 focus_ring: Default::default(),
197 border: Default::default(),
198 center_focused_column: Default::default(),
199 preset_widths: vec![
200 ColumnWidth::Proportion(1. / 3.),
201 ColumnWidth::Proportion(0.5),
202 ColumnWidth::Proportion(2. / 3.),
203 ],
204 default_width: None,
205 animations: Default::default(),
206 }
207 }
208}
209
210impl Options {
211 fn from_config(config: &Config) -> Self {
212 let layout = &config.layout;
213 let preset_column_widths = &layout.preset_column_widths;
214
215 let preset_widths = if preset_column_widths.is_empty() {
216 Options::default().preset_widths
217 } else {
218 preset_column_widths
219 .iter()
220 .copied()
221 .map(ColumnWidth::from)
222 .collect()
223 };
224
225 // Missing default_column_width maps to Some(ColumnWidth::Proportion(0.5)),
226 // while present, but empty, maps to None.
227 let default_width = layout
228 .default_column_width
229 .as_ref()
230 .map(|w| w.0.map(ColumnWidth::from))
231 .unwrap_or(Some(ColumnWidth::Proportion(0.5)));
232
233 Self {
234 gaps: layout.gaps.into(),
235 struts: layout.struts,
236 focus_ring: layout.focus_ring,
237 border: layout.border,
238 center_focused_column: layout.center_focused_column,
239 preset_widths,
240 default_width,
241 animations: config.animations,
242 }
243 }
244}
245
246impl<W: LayoutElement> Layout<W> {
247 pub fn new(config: &Config) -> Self {
248 Self::with_options(Options::from_config(config))
249 }
250
251 pub fn with_options(options: Options) -> Self {
252 Self {
253 monitor_set: MonitorSet::NoOutputs { workspaces: vec![] },
254 options: Rc::new(options),
255 }
256 }
257
258 pub fn add_output(&mut self, output: Output) {
259 let id = OutputId::new(&output);
260
261 self.monitor_set = match mem::take(&mut self.monitor_set) {
262 MonitorSet::Normal {
263 mut monitors,
264 primary_idx,
265 active_monitor_idx,
266 } => {
267 let primary = &mut monitors[primary_idx];
268
269 let mut stopped_primary_ws_switch = false;
270
271 let mut workspaces = vec![];
272 for i in (0..primary.workspaces.len()).rev() {
273 if primary.workspaces[i].original_output == id {
274 let ws = primary.workspaces.remove(i);
275
276 // FIXME: this can be coded in a way that the workspace switch won't be
277 // affected if the removed workspace is invisible. But this is good enough
278 // for now.
279 if primary.workspace_switch.is_some() {
280 primary.workspace_switch = None;
281 stopped_primary_ws_switch = true;
282 }
283
284 // The user could've closed a window while remaining on this workspace, on
285 // another monitor. However, we will add an empty workspace in the end
286 // instead.
287 if ws.has_windows() {
288 workspaces.push(ws);
289 }
290
291 if i <= primary.active_workspace_idx {
292 primary.active_workspace_idx =
293 primary.active_workspace_idx.saturating_sub(1);
294 }
295 }
296 }
297
298 // If we stopped a workspace switch, then we might need to clean up workspaces.
299 if stopped_primary_ws_switch {
300 primary.clean_up_workspaces();
301 }
302
303 workspaces.reverse();
304
305 // Make sure there's always an empty workspace.
306 workspaces.push(Workspace::new(output.clone(), self.options.clone()));
307
308 for ws in &mut workspaces {
309 ws.set_output(Some(output.clone()));
310 }
311
312 monitors.push(Monitor::new(output, workspaces, self.options.clone()));
313 MonitorSet::Normal {
314 monitors,
315 primary_idx,
316 active_monitor_idx,
317 }
318 }
319 MonitorSet::NoOutputs { mut workspaces } => {
320 // We know there are no empty workspaces there, so add one.
321 workspaces.push(Workspace::new(output.clone(), self.options.clone()));
322
323 for workspace in &mut workspaces {
324 workspace.set_output(Some(output.clone()));
325 }
326
327 let monitor = Monitor::new(output, workspaces, self.options.clone());
328
329 MonitorSet::Normal {
330 monitors: vec![monitor],
331 primary_idx: 0,
332 active_monitor_idx: 0,
333 }
334 }
335 }
336 }
337
338 pub fn remove_output(&mut self, output: &Output) {
339 self.monitor_set = match mem::take(&mut self.monitor_set) {
340 MonitorSet::Normal {
341 mut monitors,
342 mut primary_idx,
343 mut active_monitor_idx,
344 } => {
345 let idx = monitors
346 .iter()
347 .position(|mon| &mon.output == output)
348 .expect("trying to remove non-existing output");
349 let monitor = monitors.remove(idx);
350 let mut workspaces = monitor.workspaces;
351
352 for ws in &mut workspaces {
353 ws.set_output(None);
354 }
355
356 // Get rid of empty workspaces.
357 workspaces.retain(|ws| ws.has_windows());
358
359 if monitors.is_empty() {
360 // Removed the last monitor.
361 MonitorSet::NoOutputs { workspaces }
362 } else {
363 if primary_idx >= idx {
364 // Update primary_idx to either still point at the same monitor, or at some
365 // other monitor if the primary has been removed.
366 primary_idx = primary_idx.saturating_sub(1);
367 }
368 if active_monitor_idx >= idx {
369 // Update active_monitor_idx to either still point at the same monitor, or
370 // at some other monitor if the active monitor has
371 // been removed.
372 active_monitor_idx = active_monitor_idx.saturating_sub(1);
373 }
374
375 let primary = &mut monitors[primary_idx];
376 for ws in &mut workspaces {
377 ws.set_output(Some(primary.output.clone()));
378 }
379
380 let empty_was_focused =
381 primary.active_workspace_idx == primary.workspaces.len() - 1;
382
383 // Push the workspaces from the removed monitor in the end, right before the
384 // last, empty, workspace.
385 let empty = primary.workspaces.remove(primary.workspaces.len() - 1);
386 primary.workspaces.extend(workspaces);
387 primary.workspaces.push(empty);
388
389 // If the empty workspace was focused on the primary monitor, keep it focused.
390 if empty_was_focused {
391 primary.active_workspace_idx = primary.workspaces.len() - 1;
392 }
393
394 MonitorSet::Normal {
395 monitors,
396 primary_idx,
397 active_monitor_idx,
398 }
399 }
400 }
401 MonitorSet::NoOutputs { .. } => {
402 panic!("tried to remove output when there were already none")
403 }
404 }
405 }
406
407 pub fn add_window_by_idx(
408 &mut self,
409 monitor_idx: usize,
410 workspace_idx: usize,
411 window: W,
412 activate: bool,
413 width: ColumnWidth,
414 is_full_width: bool,
415 ) {
416 let MonitorSet::Normal {
417 monitors,
418 active_monitor_idx,
419 ..
420 } = &mut self.monitor_set
421 else {
422 panic!()
423 };
424
425 monitors[monitor_idx].add_window(workspace_idx, window, activate, width, is_full_width);
426
427 if activate {
428 *active_monitor_idx = monitor_idx;
429 }
430 }
431
432 pub fn add_column_by_idx(
433 &mut self,
434 monitor_idx: usize,
435 workspace_idx: usize,
436 column: Column<W>,
437 activate: bool,
438 ) {
439 let MonitorSet::Normal {
440 monitors,
441 active_monitor_idx,
442 ..
443 } = &mut self.monitor_set
444 else {
445 panic!()
446 };
447
448 monitors[monitor_idx].add_column(workspace_idx, column, activate);
449
450 if activate {
451 *active_monitor_idx = monitor_idx;
452 }
453 }
454
455 /// Adds a new window to the layout.
456 ///
457 /// Returns an output that the window was added to, if there were any outputs.
458 pub fn add_window(
459 &mut self,
460 window: W,
461 width: Option<ColumnWidth>,
462 is_full_width: bool,
463 ) -> Option<&Output> {
464 let mut width = width.unwrap_or_else(|| ColumnWidth::Fixed(window.size().w));
465 if let ColumnWidth::Fixed(w) = &mut width {
466 if !self.options.border.off {
467 *w += self.options.border.width as i32 * 2;
468 }
469 }
470
471 match &mut self.monitor_set {
472 MonitorSet::Normal {
473 monitors,
474 active_monitor_idx,
475 ..
476 } => {
477 let mon = &mut monitors[*active_monitor_idx];
478
479 // Don't steal focus from an active fullscreen window.
480 let mut activate = true;
481 let ws = &mon.workspaces[mon.active_workspace_idx];
482 if !ws.columns.is_empty() && ws.columns[ws.active_column_idx].is_fullscreen {
483 activate = false;
484 }
485
486 mon.add_window(
487 mon.active_workspace_idx,
488 window,
489 activate,
490 width,
491 is_full_width,
492 );
493 Some(&mon.output)
494 }
495 MonitorSet::NoOutputs { workspaces } => {
496 let ws = if let Some(ws) = workspaces.get_mut(0) {
497 ws
498 } else {
499 workspaces.push(Workspace::new_no_outputs(self.options.clone()));
500 &mut workspaces[0]
501 };
502 ws.add_window(window, true, width, is_full_width);
503 None
504 }
505 }
506 }
507
508 /// Adds a new window to the layout immediately to the right of another window.
509 ///
510 /// If that another window was active, activates the new window.
511 ///
512 /// Returns an output that the window was added to, if there were any outputs.
513 pub fn add_window_right_of(
514 &mut self,
515 right_of: &W::Id,
516 window: W,
517 width: Option<ColumnWidth>,
518 is_full_width: bool,
519 ) -> Option<&Output> {
520 let mut width = width.unwrap_or_else(|| ColumnWidth::Fixed(window.size().w));
521 if let ColumnWidth::Fixed(w) = &mut width {
522 if !self.options.border.off {
523 *w += self.options.border.width as i32 * 2;
524 }
525 }
526
527 match &mut self.monitor_set {
528 MonitorSet::Normal { monitors, .. } => {
529 let mon = monitors
530 .iter_mut()
531 .find(|mon| mon.workspaces.iter().any(|ws| ws.has_window(right_of)))
532 .unwrap();
533
534 mon.add_window_right_of(right_of, window, width, is_full_width);
535 Some(&mon.output)
536 }
537 MonitorSet::NoOutputs { workspaces } => {
538 let ws = workspaces
539 .iter_mut()
540 .find(|ws| ws.has_window(right_of))
541 .unwrap();
542 ws.add_window_right_of(right_of, window, width, is_full_width);
543 None
544 }
545 }
546 }
547
548 /// Adds a new window to the layout on a specific output.
549 pub fn add_window_on_output(
550 &mut self,
551 output: &Output,
552 window: W,
553 width: Option<ColumnWidth>,
554 is_full_width: bool,
555 ) {
556 let mut width = width.unwrap_or_else(|| ColumnWidth::Fixed(window.size().w));
557 if let ColumnWidth::Fixed(w) = &mut width {
558 if !self.options.border.off {
559 *w += self.options.border.width as i32 * 2;
560 }
561 }
562
563 let MonitorSet::Normal {
564 monitors,
565 active_monitor_idx,
566 ..
567 } = &mut self.monitor_set
568 else {
569 panic!()
570 };
571
572 let (mon_idx, mon) = monitors
573 .iter_mut()
574 .enumerate()
575 .find(|(_, mon)| mon.output == *output)
576 .unwrap();
577
578 // Don't steal focus from an active fullscreen window.
579 let mut activate = true;
580 let ws = &mon.workspaces[mon.active_workspace_idx];
581 if mon_idx == *active_monitor_idx
582 && !ws.columns.is_empty()
583 && ws.columns[ws.active_column_idx].is_fullscreen
584 {
585 activate = false;
586 }
587
588 mon.add_window(
589 mon.active_workspace_idx,
590 window,
591 activate,
592 width,
593 is_full_width,
594 );
595 }
596
597 pub fn remove_window(&mut self, window: &W::Id) -> Option<W> {
598 let mut rv = None;
599
600 match &mut self.monitor_set {
601 MonitorSet::Normal { monitors, .. } => {
602 for mon in monitors {
603 for (idx, ws) in mon.workspaces.iter_mut().enumerate() {
604 if ws.has_window(window) {
605 rv = Some(ws.remove_window(window));
606
607 // Clean up empty workspaces that are not active and not last.
608 if !ws.has_windows()
609 && idx != mon.active_workspace_idx
610 && idx != mon.workspaces.len() - 1
611 && mon.workspace_switch.is_none()
612 {
613 mon.workspaces.remove(idx);
614
615 if idx < mon.active_workspace_idx {
616 mon.active_workspace_idx -= 1;
617 }
618 }
619
620 break;
621 }
622 }
623 }
624 }
625 MonitorSet::NoOutputs { workspaces, .. } => {
626 for (idx, ws) in workspaces.iter_mut().enumerate() {
627 if ws.has_window(window) {
628 rv = Some(ws.remove_window(window));
629
630 // Clean up empty workspaces.
631 if !ws.has_windows() {
632 workspaces.remove(idx);
633 }
634
635 break;
636 }
637 }
638 }
639 }
640
641 rv
642 }
643
644 pub fn update_window(&mut self, window: &W::Id) {
645 match &mut self.monitor_set {
646 MonitorSet::Normal { monitors, .. } => {
647 for mon in monitors {
648 for ws in &mut mon.workspaces {
649 if ws.has_window(window) {
650 ws.update_window(window);
651 return;
652 }
653 }
654 }
655 }
656 MonitorSet::NoOutputs { workspaces, .. } => {
657 for ws in workspaces {
658 if ws.has_window(window) {
659 ws.update_window(window);
660 return;
661 }
662 }
663 }
664 }
665 }
666
667 pub fn find_window_and_output(&self, wl_surface: &WlSurface) -> Option<(&W, &Output)> {
668 if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
669 for mon in monitors {
670 for ws in &mon.workspaces {
671 if let Some(window) = ws.find_wl_surface(wl_surface) {
672 return Some((window, &mon.output));
673 }
674 }
675 }
676 }
677
678 None
679 }
680
681 pub fn find_window_and_output_mut(
682 &mut self,
683 wl_surface: &WlSurface,
684 ) -> Option<(&mut W, Option<&Output>)> {
685 match &mut self.monitor_set {
686 MonitorSet::Normal { monitors, .. } => {
687 for mon in monitors {
688 for ws in &mut mon.workspaces {
689 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {
690 return Some((window, Some(&mon.output)));
691 }
692 }
693 }
694 }
695 MonitorSet::NoOutputs { workspaces } => {
696 for ws in workspaces {
697 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {
698 return Some((window, None));
699 }
700 }
701 }
702 }
703
704 None
705 }
706
707 pub fn window_y(&self, window: &W::Id) -> Option<i32> {
708 match &self.monitor_set {
709 MonitorSet::Normal { monitors, .. } => {
710 for mon in monitors {
711 for ws in &mon.workspaces {
712 for col in &ws.columns {
713 if let Some(idx) = col.position(window) {
714 return Some(col.window_y(idx));
715 }
716 }
717 }
718 }
719 }
720 MonitorSet::NoOutputs { workspaces, .. } => {
721 for ws in workspaces {
722 for col in &ws.columns {
723 if let Some(idx) = col.position(window) {
724 return Some(col.window_y(idx));
725 }
726 }
727 }
728 }
729 }
730
731 None
732 }
733
734 pub fn update_output_size(&mut self, output: &Output) {
735 let _span = tracy_client::span!("Layout::update_output_size");
736
737 let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else {
738 panic!()
739 };
740
741 for mon in monitors {
742 if &mon.output == output {
743 let view_size = output_size(output);
744 let working_area = compute_working_area(output, self.options.struts);
745
746 for ws in &mut mon.workspaces {
747 ws.set_view_size(view_size, working_area);
748 ws.update_output_scale_transform();
749 }
750
751 break;
752 }
753 }
754 }
755
756 pub fn activate_window(&mut self, window: &W::Id) {
757 let MonitorSet::Normal {
758 monitors,
759 active_monitor_idx,
760 ..
761 } = &mut self.monitor_set
762 else {
763 todo!()
764 };
765
766 for (monitor_idx, mon) in monitors.iter_mut().enumerate() {
767 for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() {
768 if ws.has_window(window) {
769 *active_monitor_idx = monitor_idx;
770 ws.activate_window(window);
771
772 // Switch to that workspace if not already during a transition.
773 if mon.workspace_switch.is_none() {
774 mon.switch_workspace(workspace_idx);
775 }
776
777 break;
778 }
779 }
780 }
781 }
782
783 pub fn activate_output(&mut self, output: &Output) {
784 let MonitorSet::Normal {
785 monitors,
786 active_monitor_idx,
787 ..
788 } = &mut self.monitor_set
789 else {
790 return;
791 };
792
793 let idx = monitors
794 .iter()
795 .position(|mon| &mon.output == output)
796 .unwrap();
797 *active_monitor_idx = idx;
798 }
799
800 pub fn active_output(&self) -> Option<&Output> {
801 let MonitorSet::Normal {
802 monitors,
803 active_monitor_idx,
804 ..
805 } = &self.monitor_set
806 else {
807 return None;
808 };
809
810 Some(&monitors[*active_monitor_idx].output)
811 }
812
813 pub fn active_workspace(&self) -> Option<&Workspace<W>> {
814 let MonitorSet::Normal {
815 monitors,
816 active_monitor_idx,
817 ..
818 } = &self.monitor_set
819 else {
820 return None;
821 };
822
823 let mon = &monitors[*active_monitor_idx];
824 Some(&mon.workspaces[mon.active_workspace_idx])
825 }
826
827 pub fn active_window(&self) -> Option<(&W, &Output)> {
828 let MonitorSet::Normal {
829 monitors,
830 active_monitor_idx,
831 ..
832 } = &self.monitor_set
833 else {
834 return None;
835 };
836
837 let mon = &monitors[*active_monitor_idx];
838 let ws = &mon.workspaces[mon.active_workspace_idx];
839
840 if ws.columns.is_empty() {
841 return None;
842 }
843
844 let col = &ws.columns[ws.active_column_idx];
845 Some((col.tiles[col.active_tile_idx].window(), &mon.output))
846 }
847
848 pub fn windows_for_output(&self, output: &Output) -> impl Iterator<Item = &W> + '_ {
849 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
850 panic!()
851 };
852
853 let mon = monitors.iter().find(|mon| &mon.output == output).unwrap();
854 mon.workspaces.iter().flat_map(|ws| ws.windows())
855 }
856
857 pub fn with_windows(&self, mut f: impl FnMut(&W, Option<&Output>)) {
858 match &self.monitor_set {
859 MonitorSet::Normal { monitors, .. } => {
860 for mon in monitors {
861 for ws in &mon.workspaces {
862 for win in ws.windows() {
863 f(win, Some(&mon.output));
864 }
865 }
866 }
867 }
868 MonitorSet::NoOutputs { workspaces } => {
869 for ws in workspaces {
870 for win in ws.windows() {
871 f(win, None);
872 }
873 }
874 }
875 }
876 }
877
878 pub fn with_windows_mut(&mut self, mut f: impl FnMut(&mut W, Option<&Output>)) {
879 match &mut self.monitor_set {
880 MonitorSet::Normal { monitors, .. } => {
881 for mon in monitors {
882 for ws in &mut mon.workspaces {
883 for win in ws.windows_mut() {
884 f(win, Some(&mon.output));
885 }
886 }
887 }
888 }
889 MonitorSet::NoOutputs { workspaces } => {
890 for ws in workspaces {
891 for win in ws.windows_mut() {
892 f(win, None);
893 }
894 }
895 }
896 }
897 }
898
899 fn active_monitor(&mut self) -> Option<&mut Monitor<W>> {
900 let MonitorSet::Normal {
901 monitors,
902 active_monitor_idx,
903 ..
904 } = &mut self.monitor_set
905 else {
906 return None;
907 };
908
909 Some(&mut monitors[*active_monitor_idx])
910 }
911
912 pub fn active_monitor_ref(&self) -> Option<&Monitor<W>> {
913 let MonitorSet::Normal {
914 monitors,
915 active_monitor_idx,
916 ..
917 } = &self.monitor_set
918 else {
919 return None;
920 };
921
922 Some(&monitors[*active_monitor_idx])
923 }
924
925 pub fn monitor_for_output(&self, output: &Output) -> Option<&Monitor<W>> {
926 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
927 return None;
928 };
929
930 monitors.iter().find(|monitor| &monitor.output == output)
931 }
932
933 pub fn outputs(&self) -> impl Iterator<Item = &Output> + '_ {
934 let monitors = if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
935 &monitors[..]
936 } else {
937 &[][..]
938 };
939
940 monitors.iter().map(|mon| &mon.output)
941 }
942
943 pub fn move_left(&mut self) {
944 let Some(monitor) = self.active_monitor() else {
945 return;
946 };
947 monitor.move_left();
948 }
949
950 pub fn move_right(&mut self) {
951 let Some(monitor) = self.active_monitor() else {
952 return;
953 };
954 monitor.move_right();
955 }
956
957 pub fn move_column_to_first(&mut self) {
958 let Some(monitor) = self.active_monitor() else {
959 return;
960 };
961 monitor.move_column_to_first();
962 }
963
964 pub fn move_column_to_last(&mut self) {
965 let Some(monitor) = self.active_monitor() else {
966 return;
967 };
968 monitor.move_column_to_last();
969 }
970
971 pub fn move_down(&mut self) {
972 let Some(monitor) = self.active_monitor() else {
973 return;
974 };
975 monitor.move_down();
976 }
977
978 pub fn move_up(&mut self) {
979 let Some(monitor) = self.active_monitor() else {
980 return;
981 };
982 monitor.move_up();
983 }
984
985 pub fn move_down_or_to_workspace_down(&mut self) {
986 let Some(monitor) = self.active_monitor() else {
987 return;
988 };
989 monitor.move_down_or_to_workspace_down();
990 }
991
992 pub fn move_up_or_to_workspace_up(&mut self) {
993 let Some(monitor) = self.active_monitor() else {
994 return;
995 };
996 monitor.move_up_or_to_workspace_up();
997 }
998
999 pub fn consume_or_expel_window_left(&mut self) {
1000 let Some(monitor) = self.active_monitor() else {
1001 return;
1002 };
1003 monitor.consume_or_expel_window_left();
1004 }
1005
1006 pub fn consume_or_expel_window_right(&mut self) {
1007 let Some(monitor) = self.active_monitor() else {
1008 return;
1009 };
1010 monitor.consume_or_expel_window_right();
1011 }
1012
1013 pub fn focus_left(&mut self) {
1014 let Some(monitor) = self.active_monitor() else {
1015 return;
1016 };
1017 monitor.focus_left();
1018 }
1019
1020 pub fn focus_right(&mut self) {
1021 let Some(monitor) = self.active_monitor() else {
1022 return;
1023 };
1024 monitor.focus_right();
1025 }
1026
1027 pub fn focus_column_first(&mut self) {
1028 let Some(monitor) = self.active_monitor() else {
1029 return;
1030 };
1031 monitor.focus_column_first();
1032 }
1033
1034 pub fn focus_column_last(&mut self) {
1035 let Some(monitor) = self.active_monitor() else {
1036 return;
1037 };
1038 monitor.focus_column_last();
1039 }
1040
1041 pub fn focus_down(&mut self) {
1042 let Some(monitor) = self.active_monitor() else {
1043 return;
1044 };
1045 monitor.focus_down();
1046 }
1047
1048 pub fn focus_up(&mut self) {
1049 let Some(monitor) = self.active_monitor() else {
1050 return;
1051 };
1052 monitor.focus_up();
1053 }
1054
1055 pub fn focus_window_or_workspace_down(&mut self) {
1056 let Some(monitor) = self.active_monitor() else {
1057 return;
1058 };
1059 monitor.focus_window_or_workspace_down();
1060 }
1061
1062 pub fn focus_window_or_workspace_up(&mut self) {
1063 let Some(monitor) = self.active_monitor() else {
1064 return;
1065 };
1066 monitor.focus_window_or_workspace_up();
1067 }
1068
1069 pub fn move_to_workspace_up(&mut self) {
1070 let Some(monitor) = self.active_monitor() else {
1071 return;
1072 };
1073 monitor.move_to_workspace_up();
1074 }
1075
1076 pub fn move_to_workspace_down(&mut self) {
1077 let Some(monitor) = self.active_monitor() else {
1078 return;
1079 };
1080 monitor.move_to_workspace_down();
1081 }
1082
1083 pub fn move_to_workspace(&mut self, idx: usize) {
1084 let Some(monitor) = self.active_monitor() else {
1085 return;
1086 };
1087 monitor.move_to_workspace(idx);
1088 }
1089
1090 pub fn move_column_to_workspace_up(&mut self) {
1091 let Some(monitor) = self.active_monitor() else {
1092 return;
1093 };
1094 monitor.move_column_to_workspace_up();
1095 }
1096
1097 pub fn move_column_to_workspace_down(&mut self) {
1098 let Some(monitor) = self.active_monitor() else {
1099 return;
1100 };
1101 monitor.move_column_to_workspace_down();
1102 }
1103
1104 pub fn move_column_to_workspace(&mut self, idx: usize) {
1105 let Some(monitor) = self.active_monitor() else {
1106 return;
1107 };
1108 monitor.move_column_to_workspace(idx);
1109 }
1110
1111 pub fn switch_workspace_up(&mut self) {
1112 let Some(monitor) = self.active_monitor() else {
1113 return;
1114 };
1115 monitor.switch_workspace_up();
1116 }
1117
1118 pub fn switch_workspace_down(&mut self) {
1119 let Some(monitor) = self.active_monitor() else {
1120 return;
1121 };
1122 monitor.switch_workspace_down();
1123 }
1124
1125 pub fn switch_workspace(&mut self, idx: usize) {
1126 let Some(monitor) = self.active_monitor() else {
1127 return;
1128 };
1129 monitor.switch_workspace(idx);
1130 }
1131
1132 pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) {
1133 let Some(monitor) = self.active_monitor() else {
1134 return;
1135 };
1136 monitor.switch_workspace_auto_back_and_forth(idx);
1137 }
1138
1139 pub fn switch_workspace_previous(&mut self) {
1140 let Some(monitor) = self.active_monitor() else {
1141 return;
1142 };
1143 monitor.switch_workspace_previous();
1144 }
1145
1146 pub fn consume_into_column(&mut self) {
1147 let Some(monitor) = self.active_monitor() else {
1148 return;
1149 };
1150 monitor.consume_into_column();
1151 }
1152
1153 pub fn expel_from_column(&mut self) {
1154 let Some(monitor) = self.active_monitor() else {
1155 return;
1156 };
1157 monitor.expel_from_column();
1158 }
1159
1160 pub fn center_column(&mut self) {
1161 let Some(monitor) = self.active_monitor() else {
1162 return;
1163 };
1164 monitor.center_column();
1165 }
1166
1167 pub fn focus(&self) -> Option<&W> {
1168 let MonitorSet::Normal {
1169 monitors,
1170 active_monitor_idx,
1171 ..
1172 } = &self.monitor_set
1173 else {
1174 return None;
1175 };
1176
1177 monitors[*active_monitor_idx].focus()
1178 }
1179
1180 /// Returns the window under the cursor and the position of its toplevel surface within the
1181 /// output.
1182 ///
1183 /// `Some((w, Some(p)))` means that the cursor is within the window's input region and can be
1184 /// used for delivering events to the window. `Some((w, None))` means that the cursor is within
1185 /// the window's activation region, but not within the window's input region. For example, the
1186 /// cursor may be on the window's server-side border.
1187 pub fn window_under(
1188 &self,
1189 output: &Output,
1190 pos_within_output: Point<f64, Logical>,
1191 ) -> Option<(&W, Option<Point<i32, Logical>>)> {
1192 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
1193 return None;
1194 };
1195
1196 let mon = monitors.iter().find(|mon| &mon.output == output)?;
1197 mon.window_under(pos_within_output)
1198 }
1199
1200 #[cfg(test)]
1201 fn verify_invariants(&self) {
1202 use std::collections::HashSet;
1203
1204 use crate::layout::monitor::WorkspaceSwitch;
1205
1206 let mut seen_workspace_id = HashSet::new();
1207
1208 let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set {
1209 MonitorSet::Normal {
1210 monitors,
1211 primary_idx,
1212 active_monitor_idx,
1213 } => (monitors, primary_idx, active_monitor_idx),
1214 MonitorSet::NoOutputs { workspaces } => {
1215 for workspace in workspaces {
1216 assert!(
1217 workspace.has_windows(),
1218 "with no outputs there cannot be empty workspaces"
1219 );
1220
1221 assert_eq!(
1222 workspace.options, self.options,
1223 "workspace options must be synchronized with layout"
1224 );
1225
1226 assert!(
1227 seen_workspace_id.insert(workspace.id()),
1228 "workspace id must be unique"
1229 );
1230
1231 workspace.verify_invariants();
1232 }
1233
1234 return;
1235 }
1236 };
1237
1238 assert!(primary_idx < monitors.len());
1239 assert!(active_monitor_idx < monitors.len());
1240
1241 for (idx, monitor) in monitors.iter().enumerate() {
1242 assert!(
1243 !monitor.workspaces.is_empty(),
1244 "monitor must have at least one workspace"
1245 );
1246 assert!(monitor.active_workspace_idx < monitor.workspaces.len());
1247
1248 assert_eq!(
1249 monitor.options, self.options,
1250 "monitor options must be synchronized with layout"
1251 );
1252
1253 if let Some(WorkspaceSwitch::Animation(anim)) = &monitor.workspace_switch {
1254 let before_idx = anim.from() as usize;
1255 let after_idx = anim.to() as usize;
1256
1257 assert!(before_idx < monitor.workspaces.len());
1258 assert!(after_idx < monitor.workspaces.len());
1259 }
1260
1261 let monitor_id = OutputId::new(&monitor.output);
1262
1263 if idx == primary_idx {
1264 for ws in &monitor.workspaces {
1265 if ws.original_output == monitor_id {
1266 // This is the primary monitor's own workspace.
1267 continue;
1268 }
1269
1270 let own_monitor_exists = monitors
1271 .iter()
1272 .any(|m| OutputId::new(&m.output) == ws.original_output);
1273 assert!(
1274 !own_monitor_exists,
1275 "primary monitor cannot have workspaces for which their own monitor exists"
1276 );
1277 }
1278 } else {
1279 assert!(
1280 monitor
1281 .workspaces
1282 .iter()
1283 .any(|workspace| workspace.original_output == monitor_id),
1284 "secondary monitor must not have any non-own workspaces"
1285 );
1286 }
1287
1288 assert!(
1289 monitor.workspaces.last().unwrap().columns.is_empty(),
1290 "monitor must have an empty workspace in the end"
1291 );
1292
1293 // If there's no workspace switch in progress, there can't be any non-last non-active
1294 // empty workspaces.
1295 if monitor.workspace_switch.is_none() {
1296 for (idx, ws) in monitor.workspaces.iter().enumerate().rev().skip(1) {
1297 if idx != monitor.active_workspace_idx {
1298 assert!(
1299 !ws.columns.is_empty(),
1300 "non-active workspace can't be empty except the last one"
1301 );
1302 }
1303 }
1304 }
1305
1306 // FIXME: verify that primary doesn't have any workspaces for which their own monitor
1307 // exists.
1308
1309 for workspace in &monitor.workspaces {
1310 assert_eq!(
1311 workspace.options, self.options,
1312 "workspace options must be synchronized with layout"
1313 );
1314
1315 assert!(
1316 seen_workspace_id.insert(workspace.id()),
1317 "workspace id must be unique"
1318 );
1319
1320 workspace.verify_invariants();
1321 }
1322 }
1323 }
1324
1325 pub fn advance_animations(&mut self, current_time: Duration) {
1326 let _span = tracy_client::span!("Layout::advance_animations");
1327
1328 match &mut self.monitor_set {
1329 MonitorSet::Normal {
1330 monitors,
1331 active_monitor_idx,
1332 ..
1333 } => {
1334 for (idx, mon) in monitors.iter_mut().enumerate() {
1335 mon.advance_animations(current_time, idx == *active_monitor_idx);
1336 }
1337 }
1338 MonitorSet::NoOutputs { workspaces, .. } => {
1339 for ws in workspaces {
1340 ws.advance_animations(current_time, false);
1341 }
1342 }
1343 }
1344 }
1345
1346 pub fn update_config(&mut self, config: &Config) {
1347 let options = Rc::new(Options::from_config(config));
1348
1349 match &mut self.monitor_set {
1350 MonitorSet::Normal { monitors, .. } => {
1351 for mon in monitors {
1352 mon.update_config(options.clone());
1353 }
1354 }
1355 MonitorSet::NoOutputs { workspaces } => {
1356 for ws in workspaces {
1357 ws.update_config(options.clone());
1358 }
1359 }
1360 }
1361
1362 self.options = options;
1363 }
1364
1365 pub fn toggle_width(&mut self) {
1366 let Some(monitor) = self.active_monitor() else {
1367 return;
1368 };
1369 monitor.toggle_width();
1370 }
1371
1372 pub fn toggle_full_width(&mut self) {
1373 let Some(monitor) = self.active_monitor() else {
1374 return;
1375 };
1376 monitor.toggle_full_width();
1377 }
1378
1379 pub fn set_column_width(&mut self, change: SizeChange) {
1380 let Some(monitor) = self.active_monitor() else {
1381 return;
1382 };
1383 monitor.set_column_width(change);
1384 }
1385
1386 pub fn set_window_height(&mut self, change: SizeChange) {
1387 let Some(monitor) = self.active_monitor() else {
1388 return;
1389 };
1390 monitor.set_window_height(change);
1391 }
1392
1393 pub fn focus_output(&mut self, output: &Output) {
1394 if let MonitorSet::Normal {
1395 monitors,
1396 active_monitor_idx,
1397 ..
1398 } = &mut self.monitor_set
1399 {
1400 for (idx, mon) in monitors.iter().enumerate() {
1401 if &mon.output == output {
1402 *active_monitor_idx = idx;
1403 return;
1404 }
1405 }
1406 }
1407 }
1408
1409 pub fn move_to_output(&mut self, output: &Output) {
1410 if let MonitorSet::Normal {
1411 monitors,
1412 active_monitor_idx,
1413 ..
1414 } = &mut self.monitor_set
1415 {
1416 let new_idx = monitors
1417 .iter()
1418 .position(|mon| &mon.output == output)
1419 .unwrap();
1420
1421 let current = &mut monitors[*active_monitor_idx];
1422 let ws = current.active_workspace();
1423 if !ws.has_windows() {
1424 return;
1425 }
1426 let column = &ws.columns[ws.active_column_idx];
1427 let width = column.width;
1428 let is_full_width = column.is_full_width;
1429 let window = ws
1430 .remove_tile_by_idx(ws.active_column_idx, column.active_tile_idx)
1431 .into_window();
1432
1433 let workspace_idx = monitors[new_idx].active_workspace_idx;
1434 self.add_window_by_idx(new_idx, workspace_idx, window, true, width, is_full_width);
1435 }
1436 }
1437
1438 pub fn move_column_to_output(&mut self, output: &Output) {
1439 if let MonitorSet::Normal {
1440 monitors,
1441 active_monitor_idx,
1442 ..
1443 } = &mut self.monitor_set
1444 {
1445 let new_idx = monitors
1446 .iter()
1447 .position(|mon| &mon.output == output)
1448 .unwrap();
1449
1450 let current = &mut monitors[*active_monitor_idx];
1451 let ws = current.active_workspace();
1452 if !ws.has_windows() {
1453 return;
1454 }
1455 let column = ws.remove_column_by_idx(ws.active_column_idx);
1456
1457 let workspace_idx = monitors[new_idx].active_workspace_idx;
1458 self.add_column_by_idx(new_idx, workspace_idx, column, true);
1459 }
1460 }
1461
1462 pub fn move_window_to_output(&mut self, window: &W::Id, output: &Output) {
1463 let mut width = None;
1464 let mut is_full_width = false;
1465
1466 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
1467 for mon in &*monitors {
1468 for ws in &mon.workspaces {
1469 for col in &ws.columns {
1470 if col.contains(window) {
1471 width = Some(col.width);
1472 is_full_width = col.is_full_width;
1473 break;
1474 }
1475 }
1476 }
1477 }
1478 }
1479
1480 let Some(width) = width else { return };
1481
1482 let window = self.remove_window(window).unwrap();
1483
1484 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
1485 let new_idx = monitors
1486 .iter()
1487 .position(|mon| &mon.output == output)
1488 .unwrap();
1489
1490 let workspace_idx = monitors[new_idx].active_workspace_idx;
1491 // FIXME: activate only if it was already active and focused.
1492 self.add_window_by_idx(new_idx, workspace_idx, window, true, width, is_full_width);
1493 }
1494 }
1495
1496 pub fn move_workspace_to_output(&mut self, output: &Output) {
1497 let MonitorSet::Normal {
1498 monitors,
1499 active_monitor_idx,
1500 ..
1501 } = &mut self.monitor_set
1502 else {
1503 return;
1504 };
1505
1506 let current = &mut monitors[*active_monitor_idx];
1507 if current.active_workspace_idx == current.workspaces.len() - 1 {
1508 // Insert a new empty workspace.
1509 let ws = Workspace::new(current.output.clone(), current.options.clone());
1510 current.workspaces.push(ws);
1511 }
1512 let mut ws = current.workspaces.remove(current.active_workspace_idx);
1513 current.active_workspace_idx = current.active_workspace_idx.saturating_sub(1);
1514 current.workspace_switch = None;
1515 current.clean_up_workspaces();
1516
1517 ws.set_output(Some(output.clone()));
1518 ws.original_output = OutputId::new(output);
1519
1520 let target_idx = monitors
1521 .iter()
1522 .position(|mon| &mon.output == output)
1523 .unwrap();
1524 let target = &mut monitors[target_idx];
1525
1526 target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
1527
1528 // Insert the workspace after the currently active one. Unless the currently active one is
1529 // the last empty workspace, then insert before.
1530 let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
1531 target.workspaces.insert(target_ws_idx, ws);
1532 target.active_workspace_idx = target_ws_idx;
1533 target.workspace_switch = None;
1534 target.clean_up_workspaces();
1535
1536 *active_monitor_idx = target_idx;
1537 }
1538
1539 pub fn set_fullscreen(&mut self, window: &W::Id, is_fullscreen: bool) {
1540 match &mut self.monitor_set {
1541 MonitorSet::Normal { monitors, .. } => {
1542 for mon in monitors {
1543 for ws in &mut mon.workspaces {
1544 if ws.has_window(window) {
1545 ws.set_fullscreen(window, is_fullscreen);
1546 return;
1547 }
1548 }
1549 }
1550 }
1551 MonitorSet::NoOutputs { workspaces, .. } => {
1552 for ws in workspaces {
1553 if ws.has_window(window) {
1554 ws.set_fullscreen(window, is_fullscreen);
1555 return;
1556 }
1557 }
1558 }
1559 }
1560 }
1561
1562 pub fn toggle_fullscreen(&mut self, window: &W::Id) {
1563 match &mut self.monitor_set {
1564 MonitorSet::Normal { monitors, .. } => {
1565 for mon in monitors {
1566 for ws in &mut mon.workspaces {
1567 if ws.has_window(window) {
1568 ws.toggle_fullscreen(window);
1569 return;
1570 }
1571 }
1572 }
1573 }
1574 MonitorSet::NoOutputs { workspaces, .. } => {
1575 for ws in workspaces {
1576 if ws.has_window(window) {
1577 ws.toggle_fullscreen(window);
1578 return;
1579 }
1580 }
1581 }
1582 }
1583 }
1584
1585 pub fn workspace_switch_gesture_begin(&mut self, output: &Output) {
1586 let monitors = match &mut self.monitor_set {
1587 MonitorSet::Normal { monitors, .. } => monitors,
1588 MonitorSet::NoOutputs { .. } => unreachable!(),
1589 };
1590
1591 for monitor in monitors {
1592 // Cancel the gesture on other outputs.
1593 if &monitor.output != output {
1594 monitor.workspace_switch_gesture_end(true);
1595 continue;
1596 }
1597
1598 monitor.workspace_switch_gesture_begin();
1599 }
1600 }
1601
1602 pub fn workspace_switch_gesture_update(
1603 &mut self,
1604 delta_y: f64,
1605 timestamp: Duration,
1606 ) -> Option<Option<Output>> {
1607 let monitors = match &mut self.monitor_set {
1608 MonitorSet::Normal { monitors, .. } => monitors,
1609 MonitorSet::NoOutputs { .. } => return None,
1610 };
1611
1612 for monitor in monitors {
1613 if let Some(refresh) = monitor.workspace_switch_gesture_update(delta_y, timestamp) {
1614 if refresh {
1615 return Some(Some(monitor.output.clone()));
1616 } else {
1617 return Some(None);
1618 }
1619 }
1620 }
1621
1622 None
1623 }
1624
1625 pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> Option<Output> {
1626 let monitors = match &mut self.monitor_set {
1627 MonitorSet::Normal { monitors, .. } => monitors,
1628 MonitorSet::NoOutputs { .. } => return None,
1629 };
1630
1631 for monitor in monitors {
1632 if monitor.workspace_switch_gesture_end(cancelled) {
1633 return Some(monitor.output.clone());
1634 }
1635 }
1636
1637 None
1638 }
1639
1640 pub fn view_offset_gesture_begin(&mut self, output: &Output) {
1641 let monitors = match &mut self.monitor_set {
1642 MonitorSet::Normal { monitors, .. } => monitors,
1643 MonitorSet::NoOutputs { .. } => unreachable!(),
1644 };
1645
1646 for monitor in monitors {
1647 for (idx, ws) in monitor.workspaces.iter_mut().enumerate() {
1648 // Cancel the gesture on other workspaces.
1649 if &monitor.output != output || idx != monitor.active_workspace_idx {
1650 ws.view_offset_gesture_end(true);
1651 continue;
1652 }
1653
1654 ws.view_offset_gesture_begin();
1655 }
1656 }
1657 }
1658
1659 pub fn view_offset_gesture_update(
1660 &mut self,
1661 delta_x: f64,
1662 timestamp: Duration,
1663 ) -> Option<Option<Output>> {
1664 let monitors = match &mut self.monitor_set {
1665 MonitorSet::Normal { monitors, .. } => monitors,
1666 MonitorSet::NoOutputs { .. } => return None,
1667 };
1668
1669 for monitor in monitors {
1670 for ws in &mut monitor.workspaces {
1671 if let Some(refresh) = ws.view_offset_gesture_update(delta_x, timestamp) {
1672 if refresh {
1673 return Some(Some(monitor.output.clone()));
1674 } else {
1675 return Some(None);
1676 }
1677 }
1678 }
1679 }
1680
1681 None
1682 }
1683
1684 pub fn view_offset_gesture_end(&mut self, cancelled: bool) -> Option<Output> {
1685 let monitors = match &mut self.monitor_set {
1686 MonitorSet::Normal { monitors, .. } => monitors,
1687 MonitorSet::NoOutputs { .. } => return None,
1688 };
1689
1690 for monitor in monitors {
1691 for ws in &mut monitor.workspaces {
1692 if ws.view_offset_gesture_end(cancelled) {
1693 return Some(monitor.output.clone());
1694 }
1695 }
1696 }
1697
1698 None
1699 }
1700
1701 pub fn move_workspace_down(&mut self) {
1702 let Some(monitor) = self.active_monitor() else {
1703 return;
1704 };
1705 monitor.move_workspace_down();
1706 }
1707
1708 pub fn move_workspace_up(&mut self) {
1709 let Some(monitor) = self.active_monitor() else {
1710 return;
1711 };
1712 monitor.move_workspace_up();
1713 }
1714
1715 pub fn start_open_animation_for_window(&mut self, window: &W::Id) {
1716 match &mut self.monitor_set {
1717 MonitorSet::Normal { monitors, .. } => {
1718 for mon in monitors {
1719 for ws in &mut mon.workspaces {
1720 for col in &mut ws.columns {
1721 for tile in &mut col.tiles {
1722 if tile.window().id() == window {
1723 tile.start_open_animation();
1724 return;
1725 }
1726 }
1727 }
1728 }
1729 }
1730 }
1731 MonitorSet::NoOutputs { workspaces, .. } => {
1732 for ws in workspaces {
1733 for col in &mut ws.columns {
1734 for tile in &mut col.tiles {
1735 if tile.window().id() == window {
1736 tile.start_open_animation();
1737 return;
1738 }
1739 }
1740 }
1741 }
1742 }
1743 }
1744 }
1745
1746 pub fn start_close_animation_for_window(
1747 &mut self,
1748 renderer: &mut GlesRenderer,
1749 window: &W::Id,
1750 ) {
1751 let _span = tracy_client::span!("Layout::start_close_animation_for_window");
1752
1753 match &mut self.monitor_set {
1754 MonitorSet::Normal { monitors, .. } => {
1755 for mon in monitors {
1756 for ws in &mut mon.workspaces {
1757 if ws.has_window(window) {
1758 ws.start_close_animation_for_window(renderer, window);
1759 return;
1760 }
1761 }
1762 }
1763 }
1764 MonitorSet::NoOutputs { workspaces, .. } => {
1765 for ws in workspaces {
1766 if ws.has_window(window) {
1767 ws.start_close_animation_for_window(renderer, window);
1768 return;
1769 }
1770 }
1771 }
1772 }
1773 }
1774
1775 pub fn prepare_for_resize_animation(&mut self, window: &W::Id) {
1776 let _span = tracy_client::span!("Layout::prepare_for_resize_animation");
1777
1778 match &mut self.monitor_set {
1779 MonitorSet::Normal { monitors, .. } => {
1780 for mon in monitors {
1781 for ws in &mut mon.workspaces {
1782 if ws.has_window(window) {
1783 ws.prepare_for_resize_animation(window);
1784 return;
1785 }
1786 }
1787 }
1788 }
1789 MonitorSet::NoOutputs { workspaces, .. } => {
1790 for ws in workspaces {
1791 if ws.has_window(window) {
1792 ws.prepare_for_resize_animation(window);
1793 return;
1794 }
1795 }
1796 }
1797 }
1798 }
1799
1800 pub fn refresh(&mut self) {
1801 let _span = tracy_client::span!("Layout::refresh");
1802
1803 match &mut self.monitor_set {
1804 MonitorSet::Normal {
1805 monitors,
1806 active_monitor_idx,
1807 ..
1808 } => {
1809 for (idx, mon) in monitors.iter_mut().enumerate() {
1810 let is_active = idx == *active_monitor_idx;
1811 for (ws_idx, ws) in mon.workspaces.iter_mut().enumerate() {
1812 ws.refresh(is_active);
1813
1814 // Cancel the view offset gesture after workspace switches, moves, etc.
1815 if ws_idx != mon.active_workspace_idx {
1816 ws.view_offset_gesture_end(false);
1817 }
1818 }
1819 }
1820 }
1821 MonitorSet::NoOutputs { workspaces, .. } => {
1822 for ws in workspaces {
1823 ws.refresh(false);
1824 ws.view_offset_gesture_end(false);
1825 }
1826 }
1827 }
1828 }
1829}
1830
1831impl<W: LayoutElement> Default for MonitorSet<W> {
1832 fn default() -> Self {
1833 Self::NoOutputs { workspaces: vec![] }
1834 }
1835}
1836
1837#[cfg(test)]
1838mod tests {
1839 use std::cell::Cell;
1840
1841 use proptest::prelude::*;
1842 use proptest_derive::Arbitrary;
1843 use smithay::output::{Mode, PhysicalProperties, Subpixel};
1844 use smithay::utils::Rectangle;
1845
1846 use super::*;
1847
1848 impl<W: LayoutElement> Default for Layout<W> {
1849 fn default() -> Self {
1850 Self::with_options(Default::default())
1851 }
1852 }
1853
1854 #[derive(Debug)]
1855 struct TestWindowInner {
1856 id: usize,
1857 bbox: Cell<Rectangle<i32, Logical>>,
1858 initial_bbox: Rectangle<i32, Logical>,
1859 requested_size: Cell<Option<Size<i32, Logical>>>,
1860 min_size: Size<i32, Logical>,
1861 max_size: Size<i32, Logical>,
1862 pending_fullscreen: Cell<bool>,
1863 }
1864
1865 #[derive(Debug, Clone)]
1866 struct TestWindow(Rc<TestWindowInner>);
1867
1868 impl TestWindow {
1869 fn new(
1870 id: usize,
1871 bbox: Rectangle<i32, Logical>,
1872 min_size: Size<i32, Logical>,
1873 max_size: Size<i32, Logical>,
1874 ) -> Self {
1875 Self(Rc::new(TestWindowInner {
1876 id,
1877 bbox: Cell::new(bbox),
1878 initial_bbox: bbox,
1879 requested_size: Cell::new(None),
1880 min_size,
1881 max_size,
1882 pending_fullscreen: Cell::new(false),
1883 }))
1884 }
1885
1886 fn communicate(&self) -> bool {
1887 if let Some(size) = self.0.requested_size.take() {
1888 assert!(size.w >= 0);
1889 assert!(size.h >= 0);
1890
1891 let mut new_bbox = self.0.initial_bbox;
1892 if size.w != 0 {
1893 new_bbox.size.w = size.w;
1894 }
1895 if size.h != 0 {
1896 new_bbox.size.h = size.h;
1897 }
1898
1899 if self.0.bbox.get() != new_bbox {
1900 self.0.bbox.set(new_bbox);
1901 return true;
1902 }
1903 }
1904
1905 false
1906 }
1907 }
1908
1909 impl LayoutElement for TestWindow {
1910 type Id = usize;
1911
1912 fn id(&self) -> &Self::Id {
1913 &self.0.id
1914 }
1915
1916 fn size(&self) -> Size<i32, Logical> {
1917 self.0.bbox.get().size
1918 }
1919
1920 fn buf_loc(&self) -> Point<i32, Logical> {
1921 (0, 0).into()
1922 }
1923
1924 fn is_in_input_region(&self, _point: Point<f64, Logical>) -> bool {
1925 false
1926 }
1927
1928 fn render<R: NiriRenderer>(
1929 &self,
1930 _renderer: &mut R,
1931 _location: Point<i32, Logical>,
1932 _scale: Scale<f64>,
1933 _alpha: f32,
1934 _target: RenderTarget,
1935 ) -> Vec<LayoutElementRenderElement<R>> {
1936 vec![]
1937 }
1938
1939 fn request_size(&mut self, size: Size<i32, Logical>, _animate: bool) {
1940 self.0.requested_size.set(Some(size));
1941 self.0.pending_fullscreen.set(false);
1942 }
1943
1944 fn request_fullscreen(&self, _size: Size<i32, Logical>) {
1945 self.0.pending_fullscreen.set(true);
1946 }
1947
1948 fn min_size(&self) -> Size<i32, Logical> {
1949 self.0.min_size
1950 }
1951
1952 fn max_size(&self) -> Size<i32, Logical> {
1953 self.0.max_size
1954 }
1955
1956 fn is_wl_surface(&self, _wl_surface: &WlSurface) -> bool {
1957 false
1958 }
1959
1960 fn set_preferred_scale_transform(&self, _scale: i32, _transform: Transform) {}
1961
1962 fn has_ssd(&self) -> bool {
1963 false
1964 }
1965
1966 fn output_enter(&self, _output: &Output) {}
1967
1968 fn output_leave(&self, _output: &Output) {}
1969
1970 fn set_offscreen_element_id(&self, _id: Option<Id>) {}
1971
1972 fn set_activated(&mut self, _active: bool) {}
1973
1974 fn set_bounds(&self, _bounds: Size<i32, Logical>) {}
1975
1976 fn send_pending_configure(&mut self) {}
1977
1978 fn is_fullscreen(&self) -> bool {
1979 false
1980 }
1981
1982 fn is_pending_fullscreen(&self) -> bool {
1983 self.0.pending_fullscreen.get()
1984 }
1985
1986 fn refresh(&self) {}
1987
1988 fn rules(&self) -> &ResolvedWindowRules {
1989 static EMPTY: ResolvedWindowRules = ResolvedWindowRules::empty();
1990 &EMPTY
1991 }
1992
1993 fn take_unmap_snapshot(&self) -> Option<LayoutElementRenderSnapshot> {
1994 None
1995 }
1996
1997 fn animation_snapshot(&self) -> Option<&LayoutElementRenderSnapshot> {
1998 None
1999 }
2000
2001 fn take_animation_snapshot(&mut self) -> Option<LayoutElementRenderSnapshot> {
2002 None
2003 }
2004 }
2005
2006 fn arbitrary_bbox() -> impl Strategy<Value = Rectangle<i32, Logical>> {
2007 any::<(i16, i16, u16, u16)>().prop_map(|(x, y, w, h)| {
2008 let loc: Point<i32, _> = Point::from((x.into(), y.into()));
2009 let size: Size<i32, _> = Size::from((w.into(), h.into()));
2010 Rectangle::from_loc_and_size(loc, size)
2011 })
2012 }
2013
2014 fn arbitrary_size_change() -> impl Strategy<Value = SizeChange> {
2015 prop_oneof![
2016 (0..).prop_map(SizeChange::SetFixed),
2017 (0f64..).prop_map(SizeChange::SetProportion),
2018 any::<i32>().prop_map(SizeChange::AdjustFixed),
2019 any::<f64>().prop_map(SizeChange::AdjustProportion),
2020 ]
2021 }
2022
2023 fn arbitrary_min_max() -> impl Strategy<Value = (i32, i32)> {
2024 prop_oneof![
2025 Just((0, 0)),
2026 (1..65536).prop_map(|n| (n, n)),
2027 (1..65536).prop_map(|min| (min, 0)),
2028 (1..).prop_map(|max| (0, max)),
2029 (1..65536, 1..).prop_map(|(min, max): (i32, i32)| (min, max.max(min))),
2030 ]
2031 }
2032
2033 fn arbitrary_min_max_size() -> impl Strategy<Value = (Size<i32, Logical>, Size<i32, Logical>)> {
2034 (arbitrary_min_max(), arbitrary_min_max()).prop_map(|((min_w, max_w), (min_h, max_h))| {
2035 let min_size = Size::from((min_w, min_h));
2036 let max_size = Size::from((max_w, max_h));
2037 (min_size, max_size)
2038 })
2039 }
2040
2041 fn arbitrary_view_offset_gesture_delta() -> impl Strategy<Value = f64> {
2042 prop_oneof![(-10f64..10f64), (-50000f64..50000f64),]
2043 }
2044
2045 #[derive(Debug, Clone, Copy, Arbitrary)]
2046 enum Op {
2047 AddOutput(#[proptest(strategy = "1..=5usize")] usize),
2048 RemoveOutput(#[proptest(strategy = "1..=5usize")] usize),
2049 FocusOutput(#[proptest(strategy = "1..=5usize")] usize),
2050 AddWindow {
2051 #[proptest(strategy = "1..=5usize")]
2052 id: usize,
2053 #[proptest(strategy = "arbitrary_bbox()")]
2054 bbox: Rectangle<i32, Logical>,
2055 #[proptest(strategy = "arbitrary_min_max_size()")]
2056 min_max_size: (Size<i32, Logical>, Size<i32, Logical>),
2057 },
2058 AddWindowRightOf {
2059 #[proptest(strategy = "1..=5usize")]
2060 id: usize,
2061 #[proptest(strategy = "1..=5usize")]
2062 right_of_id: usize,
2063 #[proptest(strategy = "arbitrary_bbox()")]
2064 bbox: Rectangle<i32, Logical>,
2065 #[proptest(strategy = "arbitrary_min_max_size()")]
2066 min_max_size: (Size<i32, Logical>, Size<i32, Logical>),
2067 },
2068 CloseWindow(#[proptest(strategy = "1..=5usize")] usize),
2069 FullscreenWindow(#[proptest(strategy = "1..=5usize")] usize),
2070 FocusColumnLeft,
2071 FocusColumnRight,
2072 FocusColumnFirst,
2073 FocusColumnLast,
2074 FocusWindowDown,
2075 FocusWindowUp,
2076 FocusWindowOrWorkspaceDown,
2077 FocusWindowOrWorkspaceUp,
2078 MoveColumnLeft,
2079 MoveColumnRight,
2080 MoveColumnToFirst,
2081 MoveColumnToLast,
2082 MoveWindowDown,
2083 MoveWindowUp,
2084 MoveWindowDownOrToWorkspaceDown,
2085 MoveWindowUpOrToWorkspaceUp,
2086 ConsumeOrExpelWindowLeft,
2087 ConsumeOrExpelWindowRight,
2088 ConsumeWindowIntoColumn,
2089 ExpelWindowFromColumn,
2090 CenterColumn,
2091 FocusWorkspaceDown,
2092 FocusWorkspaceUp,
2093 FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize),
2094 FocusWorkspaceAutoBackAndForth(#[proptest(strategy = "0..=4usize")] usize),
2095 FocusWorkspacePrevious,
2096 MoveWindowToWorkspaceDown,
2097 MoveWindowToWorkspaceUp,
2098 MoveWindowToWorkspace(#[proptest(strategy = "0..=4usize")] usize),
2099 MoveColumnToWorkspaceDown,
2100 MoveColumnToWorkspaceUp,
2101 MoveColumnToWorkspace(#[proptest(strategy = "0..=4usize")] usize),
2102 MoveWorkspaceDown,
2103 MoveWorkspaceUp,
2104 MoveWindowToOutput(#[proptest(strategy = "1..=5u8")] u8),
2105 MoveColumnToOutput(#[proptest(strategy = "1..=5u8")] u8),
2106 SwitchPresetColumnWidth,
2107 MaximizeColumn,
2108 SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
2109 SetWindowHeight(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
2110 Communicate(#[proptest(strategy = "1..=5usize")] usize),
2111 MoveWorkspaceToOutput(#[proptest(strategy = "1..=5u8")] u8),
2112 ViewOffsetGestureBegin {
2113 #[proptest(strategy = "1..=5usize")]
2114 output_idx: usize,
2115 },
2116 ViewOffsetGestureUpdate {
2117 #[proptest(strategy = "arbitrary_view_offset_gesture_delta()")]
2118 delta: f64,
2119 timestamp: Duration,
2120 },
2121 ViewOffsetGestureEnd,
2122 WorkspaceSwitchGestureBegin {
2123 #[proptest(strategy = "1..=5usize")]
2124 output_idx: usize,
2125 },
2126 WorkspaceSwitchGestureUpdate {
2127 #[proptest(strategy = "-400f64..400f64")]
2128 delta: f64,
2129 timestamp: Duration,
2130 },
2131 WorkspaceSwitchGestureEnd {
2132 cancelled: bool,
2133 },
2134 }
2135
2136 impl Op {
2137 fn apply(self, layout: &mut Layout<TestWindow>) {
2138 match self {
2139 Op::AddOutput(id) => {
2140 let name = format!("output{id}");
2141 if layout.outputs().any(|o| o.name() == name) {
2142 return;
2143 }
2144
2145 let output = Output::new(
2146 name,
2147 PhysicalProperties {
2148 size: Size::from((1280, 720)),
2149 subpixel: Subpixel::Unknown,
2150 make: String::new(),
2151 model: String::new(),
2152 },
2153 );
2154 output.change_current_state(
2155 Some(Mode {
2156 size: Size::from((1280, 720)),
2157 refresh: 60000,
2158 }),
2159 None,
2160 None,
2161 None,
2162 );
2163 layout.add_output(output.clone());
2164 }
2165 Op::RemoveOutput(id) => {
2166 let name = format!("output{id}");
2167 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2168 return;
2169 };
2170
2171 layout.remove_output(&output);
2172 }
2173 Op::FocusOutput(id) => {
2174 let name = format!("output{id}");
2175 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2176 return;
2177 };
2178
2179 layout.focus_output(&output);
2180 }
2181 Op::AddWindow {
2182 id,
2183 bbox,
2184 min_max_size,
2185 } => {
2186 match &mut layout.monitor_set {
2187 MonitorSet::Normal { monitors, .. } => {
2188 for mon in monitors {
2189 for ws in &mut mon.workspaces {
2190 for win in ws.windows() {
2191 if win.0.id == id {
2192 return;
2193 }
2194 }
2195 }
2196 }
2197 }
2198 MonitorSet::NoOutputs { workspaces, .. } => {
2199 for ws in workspaces {
2200 for win in ws.windows() {
2201 if win.0.id == id {
2202 return;
2203 }
2204 }
2205 }
2206 }
2207 }
2208
2209 let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1);
2210 layout.add_window(win, None, false);
2211 }
2212 Op::AddWindowRightOf {
2213 id,
2214 right_of_id,
2215 bbox,
2216 min_max_size,
2217 } => {
2218 let mut found_right_of = false;
2219
2220 match &mut layout.monitor_set {
2221 MonitorSet::Normal { monitors, .. } => {
2222 for mon in monitors {
2223 for ws in &mut mon.workspaces {
2224 for win in ws.windows() {
2225 if win.0.id == id {
2226 return;
2227 }
2228
2229 if win.0.id == right_of_id {
2230 found_right_of = true;
2231 }
2232 }
2233 }
2234 }
2235 }
2236 MonitorSet::NoOutputs { workspaces, .. } => {
2237 for ws in workspaces {
2238 for win in ws.windows() {
2239 if win.0.id == id {
2240 return;
2241 }
2242
2243 if win.0.id == right_of_id {
2244 found_right_of = true;
2245 }
2246 }
2247 }
2248 }
2249 }
2250
2251 if !found_right_of {
2252 return;
2253 }
2254
2255 let win = TestWindow::new(id, bbox, min_max_size.0, min_max_size.1);
2256 layout.add_window_right_of(&right_of_id, win, None, false);
2257 }
2258 Op::CloseWindow(id) => {
2259 layout.remove_window(&id);
2260 }
2261 Op::FullscreenWindow(id) => {
2262 layout.toggle_fullscreen(&id);
2263 }
2264 Op::FocusColumnLeft => layout.focus_left(),
2265 Op::FocusColumnRight => layout.focus_right(),
2266 Op::FocusColumnFirst => layout.focus_column_first(),
2267 Op::FocusColumnLast => layout.focus_column_last(),
2268 Op::FocusWindowDown => layout.focus_down(),
2269 Op::FocusWindowUp => layout.focus_up(),
2270 Op::FocusWindowOrWorkspaceDown => layout.focus_window_or_workspace_down(),
2271 Op::FocusWindowOrWorkspaceUp => layout.focus_window_or_workspace_up(),
2272 Op::MoveColumnLeft => layout.move_left(),
2273 Op::MoveColumnRight => layout.move_right(),
2274 Op::MoveColumnToFirst => layout.move_column_to_first(),
2275 Op::MoveColumnToLast => layout.move_column_to_last(),
2276 Op::MoveWindowDown => layout.move_down(),
2277 Op::MoveWindowUp => layout.move_up(),
2278 Op::MoveWindowDownOrToWorkspaceDown => layout.move_down_or_to_workspace_down(),
2279 Op::MoveWindowUpOrToWorkspaceUp => layout.move_up_or_to_workspace_up(),
2280 Op::ConsumeOrExpelWindowLeft => layout.consume_or_expel_window_left(),
2281 Op::ConsumeOrExpelWindowRight => layout.consume_or_expel_window_right(),
2282 Op::ConsumeWindowIntoColumn => layout.consume_into_column(),
2283 Op::ExpelWindowFromColumn => layout.expel_from_column(),
2284 Op::CenterColumn => layout.center_column(),
2285 Op::FocusWorkspaceDown => layout.switch_workspace_down(),
2286 Op::FocusWorkspaceUp => layout.switch_workspace_up(),
2287 Op::FocusWorkspace(idx) => layout.switch_workspace(idx),
2288 Op::FocusWorkspaceAutoBackAndForth(idx) => {
2289 layout.switch_workspace_auto_back_and_forth(idx)
2290 }
2291 Op::FocusWorkspacePrevious => layout.switch_workspace_previous(),
2292 Op::MoveWindowToWorkspaceDown => layout.move_to_workspace_down(),
2293 Op::MoveWindowToWorkspaceUp => layout.move_to_workspace_up(),
2294 Op::MoveWindowToWorkspace(idx) => layout.move_to_workspace(idx),
2295 Op::MoveColumnToWorkspaceDown => layout.move_column_to_workspace_down(),
2296 Op::MoveColumnToWorkspaceUp => layout.move_column_to_workspace_up(),
2297 Op::MoveColumnToWorkspace(idx) => layout.move_column_to_workspace(idx),
2298 Op::MoveWindowToOutput(id) => {
2299 let name = format!("output{id}");
2300 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2301 return;
2302 };
2303
2304 layout.move_to_output(&output);
2305 }
2306 Op::MoveColumnToOutput(id) => {
2307 let name = format!("output{id}");
2308 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2309 return;
2310 };
2311
2312 layout.move_column_to_output(&output);
2313 }
2314 Op::MoveWorkspaceDown => layout.move_workspace_down(),
2315 Op::MoveWorkspaceUp => layout.move_workspace_up(),
2316 Op::SwitchPresetColumnWidth => layout.toggle_width(),
2317 Op::MaximizeColumn => layout.toggle_full_width(),
2318 Op::SetColumnWidth(change) => layout.set_column_width(change),
2319 Op::SetWindowHeight(change) => layout.set_window_height(change),
2320 Op::Communicate(id) => {
2321 let mut update = false;
2322 match &mut layout.monitor_set {
2323 MonitorSet::Normal { monitors, .. } => {
2324 'outer: for mon in monitors {
2325 for ws in &mut mon.workspaces {
2326 for win in ws.windows() {
2327 if win.0.id == id {
2328 if win.communicate() {
2329 update = true;
2330 }
2331 break 'outer;
2332 }
2333 }
2334 }
2335 }
2336 }
2337 MonitorSet::NoOutputs { workspaces, .. } => {
2338 'outer: for ws in workspaces {
2339 for win in ws.windows() {
2340 if win.0.id == id {
2341 if win.communicate() {
2342 update = true;
2343 }
2344 break 'outer;
2345 }
2346 }
2347 }
2348 }
2349 }
2350
2351 if update {
2352 layout.update_window(&id);
2353 }
2354 }
2355 Op::MoveWorkspaceToOutput(id) => {
2356 let name = format!("output{id}");
2357 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2358 return;
2359 };
2360
2361 layout.move_workspace_to_output(&output);
2362 }
2363 Op::ViewOffsetGestureBegin { output_idx: id } => {
2364 let name = format!("output{id}");
2365 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2366 return;
2367 };
2368
2369 layout.view_offset_gesture_begin(&output);
2370 }
2371 Op::ViewOffsetGestureUpdate { delta, timestamp } => {
2372 layout.view_offset_gesture_update(delta, timestamp);
2373 }
2374 Op::ViewOffsetGestureEnd => {
2375 // We don't handle cancels in this gesture.
2376 layout.view_offset_gesture_end(false);
2377 }
2378 Op::WorkspaceSwitchGestureBegin { output_idx: id } => {
2379 let name = format!("output{id}");
2380 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
2381 return;
2382 };
2383
2384 layout.workspace_switch_gesture_begin(&output);
2385 }
2386 Op::WorkspaceSwitchGestureUpdate { delta, timestamp } => {
2387 layout.workspace_switch_gesture_update(delta, timestamp);
2388 }
2389 Op::WorkspaceSwitchGestureEnd { cancelled } => {
2390 layout.workspace_switch_gesture_end(cancelled);
2391 }
2392 }
2393 }
2394 }
2395
2396 #[track_caller]
2397 fn check_ops(ops: &[Op]) {
2398 let mut layout = Layout::default();
2399 for op in ops {
2400 op.apply(&mut layout);
2401 layout.verify_invariants();
2402 }
2403 }
2404
2405 #[track_caller]
2406 fn check_ops_with_options(options: Options, ops: &[Op]) {
2407 let mut layout = Layout::with_options(options);
2408
2409 for op in ops {
2410 op.apply(&mut layout);
2411 layout.verify_invariants();
2412 }
2413 }
2414
2415 #[test]
2416 fn operations_dont_panic() {
2417 let every_op = [
2418 Op::AddOutput(0),
2419 Op::AddOutput(1),
2420 Op::AddOutput(2),
2421 Op::RemoveOutput(0),
2422 Op::RemoveOutput(1),
2423 Op::RemoveOutput(2),
2424 Op::FocusOutput(0),
2425 Op::FocusOutput(1),
2426 Op::FocusOutput(2),
2427 Op::AddWindow {
2428 id: 0,
2429 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2430 min_max_size: Default::default(),
2431 },
2432 Op::AddWindow {
2433 id: 1,
2434 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2435 min_max_size: Default::default(),
2436 },
2437 Op::AddWindow {
2438 id: 2,
2439 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2440 min_max_size: Default::default(),
2441 },
2442 Op::AddWindowRightOf {
2443 id: 3,
2444 right_of_id: 0,
2445 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2446 min_max_size: Default::default(),
2447 },
2448 Op::AddWindowRightOf {
2449 id: 4,
2450 right_of_id: 1,
2451 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2452 min_max_size: Default::default(),
2453 },
2454 Op::CloseWindow(0),
2455 Op::CloseWindow(1),
2456 Op::CloseWindow(2),
2457 Op::FullscreenWindow(1),
2458 Op::FullscreenWindow(2),
2459 Op::FullscreenWindow(3),
2460 Op::FocusColumnLeft,
2461 Op::FocusColumnRight,
2462 Op::FocusWindowUp,
2463 Op::FocusWindowOrWorkspaceUp,
2464 Op::FocusWindowDown,
2465 Op::FocusWindowOrWorkspaceDown,
2466 Op::MoveColumnLeft,
2467 Op::MoveColumnRight,
2468 Op::ConsumeWindowIntoColumn,
2469 Op::ExpelWindowFromColumn,
2470 Op::CenterColumn,
2471 Op::FocusWorkspaceDown,
2472 Op::FocusWorkspaceUp,
2473 Op::FocusWorkspace(1),
2474 Op::FocusWorkspace(2),
2475 Op::FocusWorkspace(3),
2476 Op::MoveWindowToWorkspaceDown,
2477 Op::MoveWindowToWorkspaceUp,
2478 Op::MoveWindowToWorkspace(1),
2479 Op::MoveWindowToWorkspace(2),
2480 Op::MoveWindowToWorkspace(3),
2481 Op::MoveColumnToWorkspaceDown,
2482 Op::MoveColumnToWorkspaceUp,
2483 Op::MoveColumnToWorkspace(1),
2484 Op::MoveColumnToWorkspace(2),
2485 Op::MoveColumnToWorkspace(3),
2486 Op::MoveWindowDown,
2487 Op::MoveWindowDownOrToWorkspaceDown,
2488 Op::MoveWindowUp,
2489 Op::MoveWindowUpOrToWorkspaceUp,
2490 Op::ConsumeOrExpelWindowLeft,
2491 Op::ConsumeOrExpelWindowRight,
2492 Op::MoveWorkspaceToOutput(1),
2493 ];
2494
2495 for third in every_op {
2496 for second in every_op {
2497 for first in every_op {
2498 // eprintln!("{first:?}, {second:?}, {third:?}");
2499
2500 let mut layout = Layout::default();
2501 first.apply(&mut layout);
2502 layout.verify_invariants();
2503 second.apply(&mut layout);
2504 layout.verify_invariants();
2505 third.apply(&mut layout);
2506 layout.verify_invariants();
2507 }
2508 }
2509 }
2510 }
2511
2512 #[test]
2513 fn operations_from_starting_state_dont_panic() {
2514 if std::env::var_os("RUN_SLOW_TESTS").is_none() {
2515 eprintln!("ignoring slow test");
2516 return;
2517 }
2518
2519 // Running every op from an empty state doesn't get us to all the interesting states. So,
2520 // also run it from a manually-created starting state with more things going on to exercise
2521 // more code paths.
2522 let setup_ops = [
2523 Op::AddOutput(1),
2524 Op::AddWindow {
2525 id: 1,
2526 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2527 min_max_size: Default::default(),
2528 },
2529 Op::MoveWindowToWorkspaceDown,
2530 Op::AddWindow {
2531 id: 2,
2532 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2533 min_max_size: Default::default(),
2534 },
2535 Op::AddWindow {
2536 id: 3,
2537 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2538 min_max_size: Default::default(),
2539 },
2540 Op::FocusColumnLeft,
2541 Op::ConsumeWindowIntoColumn,
2542 Op::AddWindow {
2543 id: 4,
2544 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2545 min_max_size: Default::default(),
2546 },
2547 Op::AddOutput(2),
2548 Op::AddWindow {
2549 id: 5,
2550 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2551 min_max_size: Default::default(),
2552 },
2553 Op::MoveWindowToOutput(2),
2554 Op::FocusOutput(1),
2555 Op::Communicate(1),
2556 Op::Communicate(2),
2557 Op::Communicate(3),
2558 Op::Communicate(4),
2559 Op::Communicate(5),
2560 ];
2561
2562 let every_op = [
2563 Op::AddOutput(0),
2564 Op::AddOutput(1),
2565 Op::AddOutput(2),
2566 Op::RemoveOutput(0),
2567 Op::RemoveOutput(1),
2568 Op::RemoveOutput(2),
2569 Op::FocusOutput(0),
2570 Op::FocusOutput(1),
2571 Op::FocusOutput(2),
2572 Op::AddWindow {
2573 id: 0,
2574 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2575 min_max_size: Default::default(),
2576 },
2577 Op::AddWindow {
2578 id: 1,
2579 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2580 min_max_size: Default::default(),
2581 },
2582 Op::AddWindow {
2583 id: 2,
2584 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2585 min_max_size: Default::default(),
2586 },
2587 Op::AddWindowRightOf {
2588 id: 6,
2589 right_of_id: 0,
2590 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2591 min_max_size: Default::default(),
2592 },
2593 Op::AddWindowRightOf {
2594 id: 7,
2595 right_of_id: 1,
2596 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2597 min_max_size: Default::default(),
2598 },
2599 Op::CloseWindow(0),
2600 Op::CloseWindow(1),
2601 Op::CloseWindow(2),
2602 Op::FullscreenWindow(1),
2603 Op::FullscreenWindow(2),
2604 Op::FullscreenWindow(3),
2605 Op::FocusColumnLeft,
2606 Op::FocusColumnRight,
2607 Op::FocusWindowUp,
2608 Op::FocusWindowOrWorkspaceUp,
2609 Op::FocusWindowDown,
2610 Op::FocusWindowOrWorkspaceDown,
2611 Op::MoveColumnLeft,
2612 Op::MoveColumnRight,
2613 Op::ConsumeWindowIntoColumn,
2614 Op::ExpelWindowFromColumn,
2615 Op::CenterColumn,
2616 Op::FocusWorkspaceDown,
2617 Op::FocusWorkspaceUp,
2618 Op::FocusWorkspace(1),
2619 Op::FocusWorkspace(2),
2620 Op::FocusWorkspace(3),
2621 Op::MoveWindowToWorkspaceDown,
2622 Op::MoveWindowToWorkspaceUp,
2623 Op::MoveWindowToWorkspace(1),
2624 Op::MoveWindowToWorkspace(2),
2625 Op::MoveWindowToWorkspace(3),
2626 Op::MoveColumnToWorkspaceDown,
2627 Op::MoveColumnToWorkspaceUp,
2628 Op::MoveColumnToWorkspace(1),
2629 Op::MoveColumnToWorkspace(2),
2630 Op::MoveColumnToWorkspace(3),
2631 Op::MoveWindowDown,
2632 Op::MoveWindowDownOrToWorkspaceDown,
2633 Op::MoveWindowUp,
2634 Op::MoveWindowUpOrToWorkspaceUp,
2635 Op::ConsumeOrExpelWindowLeft,
2636 Op::ConsumeOrExpelWindowRight,
2637 ];
2638
2639 for third in every_op {
2640 for second in every_op {
2641 for first in every_op {
2642 // eprintln!("{first:?}, {second:?}, {third:?}");
2643
2644 let mut layout = Layout::default();
2645 for op in setup_ops {
2646 op.apply(&mut layout);
2647 }
2648
2649 first.apply(&mut layout);
2650 layout.verify_invariants();
2651 second.apply(&mut layout);
2652 layout.verify_invariants();
2653 third.apply(&mut layout);
2654 layout.verify_invariants();
2655 }
2656 }
2657 }
2658 }
2659
2660 #[test]
2661 fn primary_active_workspace_idx_not_updated_on_output_add() {
2662 let ops = [
2663 Op::AddOutput(1),
2664 Op::AddOutput(2),
2665 Op::FocusOutput(1),
2666 Op::AddWindow {
2667 id: 0,
2668 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2669 min_max_size: Default::default(),
2670 },
2671 Op::FocusOutput(2),
2672 Op::AddWindow {
2673 id: 1,
2674 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2675 min_max_size: Default::default(),
2676 },
2677 Op::RemoveOutput(2),
2678 Op::FocusWorkspace(3),
2679 Op::AddOutput(2),
2680 ];
2681
2682 check_ops(&ops);
2683 }
2684
2685 #[test]
2686 fn window_closed_on_previous_workspace() {
2687 let ops = [
2688 Op::AddOutput(1),
2689 Op::AddWindow {
2690 id: 0,
2691 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2692 min_max_size: Default::default(),
2693 },
2694 Op::FocusWorkspaceDown,
2695 Op::CloseWindow(0),
2696 ];
2697
2698 check_ops(&ops);
2699 }
2700
2701 #[test]
2702 fn removing_output_must_keep_empty_focus_on_primary() {
2703 let ops = [
2704 Op::AddOutput(1),
2705 Op::AddWindow {
2706 id: 0,
2707 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2708 min_max_size: Default::default(),
2709 },
2710 Op::AddOutput(2),
2711 Op::RemoveOutput(1),
2712 ];
2713
2714 let mut layout = Layout::default();
2715 for op in ops {
2716 op.apply(&mut layout);
2717 }
2718
2719 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2720 unreachable!()
2721 };
2722
2723 // The workspace from the removed output was inserted at position 0, so the active workspace
2724 // must change to 1 to keep the focus on the empty workspace.
2725 assert_eq!(monitors[0].active_workspace_idx, 1);
2726 }
2727
2728 #[test]
2729 fn move_to_workspace_by_idx_does_not_leave_empty_workspaces() {
2730 let ops = [
2731 Op::AddOutput(1),
2732 Op::AddWindow {
2733 id: 0,
2734 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2735 min_max_size: Default::default(),
2736 },
2737 Op::MoveWindowToWorkspace(2),
2738 ];
2739
2740 let mut layout = Layout::default();
2741 for op in ops {
2742 op.apply(&mut layout);
2743 }
2744
2745 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2746 unreachable!()
2747 };
2748
2749 assert!(monitors[0].workspaces[0].has_windows());
2750 }
2751
2752 #[test]
2753 fn focus_workspace_by_idx_does_not_leave_empty_workspaces() {
2754 let ops = [
2755 Op::AddOutput(1),
2756 Op::AddWindow {
2757 id: 0,
2758 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2759 min_max_size: Default::default(),
2760 },
2761 Op::FocusWorkspaceDown,
2762 Op::AddWindow {
2763 id: 1,
2764 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2765 min_max_size: Default::default(),
2766 },
2767 Op::FocusWorkspaceUp,
2768 Op::CloseWindow(0),
2769 Op::FocusWorkspace(3),
2770 ];
2771
2772 let mut layout = Layout::default();
2773 for op in ops {
2774 op.apply(&mut layout);
2775 }
2776
2777 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
2778 unreachable!()
2779 };
2780
2781 assert!(monitors[0].workspaces[0].has_windows());
2782 }
2783
2784 #[test]
2785 fn empty_workspaces_dont_move_back_to_original_output() {
2786 let ops = [
2787 Op::AddOutput(1),
2788 Op::AddWindow {
2789 id: 1,
2790 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2791 min_max_size: Default::default(),
2792 },
2793 Op::FocusWorkspaceDown,
2794 Op::AddWindow {
2795 id: 2,
2796 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2797 min_max_size: Default::default(),
2798 },
2799 Op::AddOutput(2),
2800 Op::RemoveOutput(1),
2801 Op::FocusWorkspace(1),
2802 Op::CloseWindow(1),
2803 Op::AddOutput(1),
2804 ];
2805
2806 check_ops(&ops);
2807 }
2808
2809 #[test]
2810 fn large_negative_height_change() {
2811 let ops = [
2812 Op::AddOutput(1),
2813 Op::AddWindow {
2814 id: 1,
2815 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2816 min_max_size: Default::default(),
2817 },
2818 Op::SetWindowHeight(SizeChange::AdjustProportion(-1e129)),
2819 ];
2820
2821 let mut options = Options::default();
2822 options.border.off = false;
2823 options.border.width = 1;
2824
2825 check_ops_with_options(options, &ops);
2826 }
2827
2828 #[test]
2829 fn large_max_size() {
2830 let ops = [
2831 Op::AddOutput(1),
2832 Op::AddWindow {
2833 id: 1,
2834 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2835 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2836 },
2837 ];
2838
2839 let mut options = Options::default();
2840 options.border.off = false;
2841 options.border.width = 1;
2842
2843 check_ops_with_options(options, &ops);
2844 }
2845
2846 #[test]
2847 fn workspace_cleanup_during_switch() {
2848 let ops = [
2849 Op::AddOutput(1),
2850 Op::AddWindow {
2851 id: 1,
2852 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2853 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2854 },
2855 Op::FocusWorkspaceDown,
2856 Op::CloseWindow(1),
2857 ];
2858
2859 check_ops(&ops);
2860 }
2861
2862 #[test]
2863 fn workspace_transfer_during_switch() {
2864 let ops = [
2865 Op::AddOutput(1),
2866 Op::AddWindow {
2867 id: 1,
2868 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2869 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2870 },
2871 Op::AddOutput(2),
2872 Op::FocusOutput(2),
2873 Op::AddWindow {
2874 id: 2,
2875 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2876 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2877 },
2878 Op::RemoveOutput(1),
2879 Op::FocusWorkspaceDown,
2880 Op::FocusWorkspaceDown,
2881 Op::AddOutput(1),
2882 ];
2883
2884 check_ops(&ops);
2885 }
2886
2887 #[test]
2888 fn workspace_transfer_during_switch_from_last() {
2889 let ops = [
2890 Op::AddOutput(1),
2891 Op::AddWindow {
2892 id: 1,
2893 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2894 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2895 },
2896 Op::AddOutput(2),
2897 Op::RemoveOutput(1),
2898 Op::FocusWorkspaceUp,
2899 Op::AddOutput(1),
2900 ];
2901
2902 check_ops(&ops);
2903 }
2904
2905 #[test]
2906 fn workspace_transfer_during_switch_gets_cleaned_up() {
2907 let ops = [
2908 Op::AddOutput(1),
2909 Op::AddWindow {
2910 id: 1,
2911 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2912 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2913 },
2914 Op::RemoveOutput(1),
2915 Op::AddOutput(2),
2916 Op::MoveColumnToWorkspaceDown,
2917 Op::MoveColumnToWorkspaceDown,
2918 Op::AddOutput(1),
2919 ];
2920
2921 check_ops(&ops);
2922 }
2923
2924 #[test]
2925 fn move_workspace_to_output() {
2926 let ops = [
2927 Op::AddOutput(1),
2928 Op::AddOutput(2),
2929 Op::FocusOutput(1),
2930 Op::AddWindow {
2931 id: 0,
2932 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2933 min_max_size: Default::default(),
2934 },
2935 Op::MoveWorkspaceToOutput(2),
2936 ];
2937
2938 let mut layout = Layout::default();
2939 for op in ops {
2940 op.apply(&mut layout);
2941 }
2942
2943 let MonitorSet::Normal {
2944 monitors,
2945 active_monitor_idx,
2946 ..
2947 } = layout.monitor_set
2948 else {
2949 unreachable!()
2950 };
2951
2952 assert_eq!(active_monitor_idx, 1);
2953 assert_eq!(monitors[0].workspaces.len(), 1);
2954 assert!(!monitors[0].workspaces[0].has_windows());
2955 assert_eq!(monitors[1].active_workspace_idx, 0);
2956 assert_eq!(monitors[1].workspaces.len(), 2);
2957 assert!(monitors[1].workspaces[0].has_windows());
2958 }
2959
2960 #[test]
2961 fn fullscreen() {
2962 let ops = [
2963 Op::AddOutput(1),
2964 Op::AddWindow {
2965 id: 1,
2966 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2967 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2968 },
2969 Op::FullscreenWindow(1),
2970 ];
2971
2972 check_ops(&ops);
2973 }
2974
2975 #[test]
2976 fn open_right_of_on_different_workspace() {
2977 let ops = [
2978 Op::AddOutput(1),
2979 Op::AddWindow {
2980 id: 1,
2981 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2982 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2983 },
2984 Op::FocusWorkspaceDown,
2985 Op::AddWindow {
2986 id: 2,
2987 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2988 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2989 },
2990 Op::AddWindowRightOf {
2991 id: 3,
2992 right_of_id: 1,
2993 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2994 min_max_size: (Size::from((0, 0)), Size::from((i32::MAX, i32::MAX))),
2995 },
2996 ];
2997
2998 let mut layout = Layout::default();
2999 for op in ops {
3000 op.apply(&mut layout);
3001 }
3002
3003 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
3004 unreachable!()
3005 };
3006
3007 let mon = monitors.into_iter().next().unwrap();
3008 assert_eq!(
3009 mon.active_workspace_idx, 1,
3010 "the second workspace must remain active"
3011 );
3012 assert_eq!(
3013 mon.workspaces[0].active_column_idx, 1,
3014 "the new window must become active"
3015 );
3016 }
3017
3018 #[test]
3019 fn unfullscreen_view_offset_not_reset_on_removal() {
3020 let ops = [
3021 Op::AddOutput(1),
3022 Op::AddWindow {
3023 id: 0,
3024 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3025 min_max_size: Default::default(),
3026 },
3027 Op::FullscreenWindow(0),
3028 Op::AddWindow {
3029 id: 1,
3030 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3031 min_max_size: Default::default(),
3032 },
3033 Op::ConsumeOrExpelWindowRight,
3034 ];
3035
3036 check_ops(&ops);
3037 }
3038
3039 #[test]
3040 fn unfullscreen_view_offset_not_reset_on_consume() {
3041 let ops = [
3042 Op::AddOutput(1),
3043 Op::AddWindow {
3044 id: 0,
3045 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3046 min_max_size: Default::default(),
3047 },
3048 Op::FullscreenWindow(0),
3049 Op::AddWindow {
3050 id: 1,
3051 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3052 min_max_size: Default::default(),
3053 },
3054 Op::ConsumeWindowIntoColumn,
3055 ];
3056
3057 check_ops(&ops);
3058 }
3059
3060 #[test]
3061 fn unfullscreen_view_offset_not_reset_on_quick_double_toggle() {
3062 let ops = [
3063 Op::AddOutput(1),
3064 Op::AddWindow {
3065 id: 0,
3066 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3067 min_max_size: Default::default(),
3068 },
3069 Op::FullscreenWindow(0),
3070 Op::FullscreenWindow(0),
3071 ];
3072
3073 check_ops(&ops);
3074 }
3075
3076 #[test]
3077 fn unfullscreen_view_offset_set_on_fullscreening_inactive_tile_in_column() {
3078 let ops = [
3079 Op::AddOutput(1),
3080 Op::AddWindow {
3081 id: 0,
3082 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3083 min_max_size: Default::default(),
3084 },
3085 Op::AddWindow {
3086 id: 1,
3087 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3088 min_max_size: Default::default(),
3089 },
3090 Op::ConsumeOrExpelWindowLeft,
3091 Op::FullscreenWindow(0),
3092 ];
3093
3094 check_ops(&ops);
3095 }
3096
3097 #[test]
3098 fn unfullscreen_view_offset_not_reset_on_gesture() {
3099 let ops = [
3100 Op::AddOutput(1),
3101 Op::AddWindow {
3102 id: 0,
3103 bbox: Rectangle::from_loc_and_size((0, 0), (200, 200)),
3104 min_max_size: Default::default(),
3105 },
3106 Op::AddWindow {
3107 id: 1,
3108 bbox: Rectangle::from_loc_and_size((0, 0), (1280, 200)),
3109 min_max_size: Default::default(),
3110 },
3111 Op::FullscreenWindow(1),
3112 Op::ViewOffsetGestureBegin { output_idx: 1 },
3113 Op::ViewOffsetGestureEnd,
3114 ];
3115
3116 check_ops(&ops);
3117 }
3118
3119 fn arbitrary_spacing() -> impl Strategy<Value = u16> {
3120 // Give equal weight to:
3121 // - 0: the element is disabled
3122 // - 4: some reasonable value
3123 // - random value, likely unreasonably big
3124 prop_oneof![Just(0), Just(4), (1..=u16::MAX)]
3125 }
3126
3127 fn arbitrary_struts() -> impl Strategy<Value = Struts> {
3128 (
3129 arbitrary_spacing(),
3130 arbitrary_spacing(),
3131 arbitrary_spacing(),
3132 arbitrary_spacing(),
3133 )
3134 .prop_map(|(left, right, top, bottom)| Struts {
3135 left,
3136 right,
3137 top,
3138 bottom,
3139 })
3140 }
3141
3142 fn arbitrary_center_focused_column() -> impl Strategy<Value = CenterFocusedColumn> {
3143 prop_oneof![
3144 Just(CenterFocusedColumn::Never),
3145 Just(CenterFocusedColumn::OnOverflow),
3146 Just(CenterFocusedColumn::Always),
3147 ]
3148 }
3149
3150 prop_compose! {
3151 fn arbitrary_focus_ring()(
3152 off in any::<bool>(),
3153 width in arbitrary_spacing(),
3154 ) -> niri_config::FocusRing {
3155 niri_config::FocusRing {
3156 off,
3157 width,
3158 ..Default::default()
3159 }
3160 }
3161 }
3162
3163 prop_compose! {
3164 fn arbitrary_border()(
3165 off in any::<bool>(),
3166 width in arbitrary_spacing(),
3167 ) -> niri_config::Border {
3168 niri_config::Border {
3169 off,
3170 width,
3171 ..Default::default()
3172 }
3173 }
3174 }
3175
3176 prop_compose! {
3177 fn arbitrary_options()(
3178 gaps in arbitrary_spacing(),
3179 struts in arbitrary_struts(),
3180 focus_ring in arbitrary_focus_ring(),
3181 border in arbitrary_border(),
3182 center_focused_column in arbitrary_center_focused_column(),
3183 ) -> Options {
3184 Options {
3185 gaps: gaps.into(),
3186 struts,
3187 center_focused_column,
3188 focus_ring,
3189 border,
3190 ..Default::default()
3191 }
3192 }
3193 }
3194
3195 proptest! {
3196 #![proptest_config(ProptestConfig {
3197 cases: if std::env::var_os("RUN_SLOW_TESTS").is_none() {
3198 eprintln!("ignoring slow test");
3199 0
3200 } else {
3201 ProptestConfig::default().cases
3202 },
3203 ..ProptestConfig::default()
3204 })]
3205
3206 #[test]
3207 fn random_operations_dont_panic(ops: Vec<Op>, options in arbitrary_options()) {
3208 // eprintln!("{ops:?}");
3209 check_ops_with_options(options, &ops);
3210 }
3211 }
3212}