forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBd397375Ivan Molodetskikh2 years ago409 commits

layout.rs3856 lines, 123.1 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::{max, min};
33use std::iter::zip;
34use std::mem;
35use std::rc::Rc;
36use std::time::Duration;
37
38use arrayvec::ArrayVec;
39use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement};
40use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
41use smithay::backend::renderer::element::utils::{
42 CropRenderElement, Relocate, RelocateRenderElement,
43};
44use smithay::backend::renderer::element::{AsRenderElements, Kind};
45use smithay::backend::renderer::gles::GlesRenderer;
46use smithay::backend::renderer::ImportAll;
47use smithay::desktop::space::SpaceElement;
48use smithay::desktop::{layer_map_for_output, Window};
49use smithay::output::Output;
50use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1;
51use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel;
52use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
53use smithay::render_elements;
54use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
55use smithay::wayland::compositor::{send_surface_state, with_states};
56use smithay::wayland::shell::xdg::SurfaceCachedState;
57
58use crate::animation::Animation;
59use crate::config::{self, Color, Config, PresetWidth, SizeChange};
60
61#[derive(Debug, Clone, PartialEq, Eq)]
62pub struct OutputId(String);
63
64render_elements! {
65 #[derive(Debug)]
66 pub WorkspaceRenderElement<R> where R: ImportAll;
67 Wayland = WaylandSurfaceRenderElement<R>,
68 FocusRing = SolidColorRenderElement,
69}
70pub type MonitorRenderElement<R> =
71 RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>;
72
73pub trait LayoutElement: SpaceElement + PartialEq + Clone {
74 fn request_size(&self, size: Size<i32, Logical>);
75 fn request_fullscreen(&self, size: Size<i32, Logical>);
76 fn min_size(&self) -> Size<i32, Logical>;
77 fn max_size(&self) -> Size<i32, Logical>;
78 fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool;
79 fn has_ssd(&self) -> bool;
80 fn set_preferred_scale_transform(&self, scale: i32, transform: Transform);
81}
82
83#[derive(Debug)]
84pub struct Layout<W: LayoutElement> {
85 /// Monitors and workspaes in the layout.
86 monitor_set: MonitorSet<W>,
87 /// Configurable properties of the layout.
88 options: Rc<Options>,
89}
90
91#[derive(Debug)]
92enum MonitorSet<W: LayoutElement> {
93 /// At least one output is connected.
94 Normal {
95 /// Connected monitors.
96 monitors: Vec<Monitor<W>>,
97 /// Index of the primary monitor.
98 primary_idx: usize,
99 /// Index of the active monitor.
100 active_monitor_idx: usize,
101 },
102 /// No outputs are connected, and these are the workspaces.
103 NoOutputs {
104 /// The workspaces.
105 workspaces: Vec<Workspace<W>>,
106 },
107}
108
109#[derive(Debug)]
110pub struct Monitor<W: LayoutElement> {
111 /// Output for this monitor.
112 output: Output,
113 // Must always contain at least one.
114 workspaces: Vec<Workspace<W>>,
115 /// Index of the currently active workspace.
116 active_workspace_idx: usize,
117 /// In-progress switch between workspaces.
118 workspace_switch: Option<WorkspaceSwitch>,
119 /// Configurable properties of the layout.
120 options: Rc<Options>,
121}
122
123#[derive(Debug)]
124enum WorkspaceSwitch {
125 Animation(Animation),
126 Gesture(WorkspaceSwitchGesture),
127}
128
129#[derive(Debug)]
130struct WorkspaceSwitchGesture {
131 /// Index of the workspace where the gesture was started.
132 center_idx: usize,
133 /// Current, fractional workspace index.
134 current_idx: f64,
135}
136
137#[derive(Debug)]
138pub struct Workspace<W: LayoutElement> {
139 /// The original output of this workspace.
140 ///
141 /// Most of the time this will be the workspace's current output, however, after an output
142 /// disconnection, it may remain pointing to the disconnected output.
143 original_output: OutputId,
144
145 /// Current output of this workspace.
146 output: Option<Output>,
147
148 /// Latest known view size for this workspace.
149 ///
150 /// This should be computed from the current workspace output size, or, if all outputs have
151 /// been disconnected, preserved until a new output is connected.
152 view_size: Size<i32, Logical>,
153
154 /// Latest known working area for this workspace.
155 ///
156 /// This is similar to view size, but takes into account things like layer shell exclusive
157 /// zones.
158 working_area: Rectangle<i32, Logical>,
159
160 /// Columns of windows on this workspace.
161 columns: Vec<Column<W>>,
162
163 /// Index of the currently active column, if any.
164 active_column_idx: usize,
165
166 /// Focus ring buffer and parameters.
167 focus_ring: FocusRing,
168
169 /// Offset of the view computed from the active column.
170 ///
171 /// Any gaps, including left padding from work area left exclusive zone, is handled
172 /// with this view offset (rather than added as a constant elsewhere in the code). This allows
173 /// for natural handling of fullscreen windows, which must ignore work area padding.
174 view_offset: i32,
175
176 /// Animation of the view offset, if one is currently ongoing.
177 view_offset_anim: Option<Animation>,
178
179 /// Whether to activate the previous, rather than the next, column upon column removal.
180 ///
181 /// When a new column is created and removed with no focus changes in-between, it is more
182 /// natural to activate the previously-focused column. This variable tracks that.
183 ///
184 /// Since we only create-and-activate columns immediately to the right of the active column (in
185 /// contrast to tabs in Firefox, for example), we can track this as a bool, rather than an
186 /// index of the previous column to activate.
187 activate_prev_column_on_removal: bool,
188
189 /// Configurable properties of the layout.
190 options: Rc<Options>,
191}
192
193#[derive(Debug)]
194struct FocusRing {
195 buffers: [SolidColorBuffer; 4],
196 locations: [Point<i32, Logical>; 4],
197 is_off: bool,
198 is_border: bool,
199 width: i32,
200 active_color: Color,
201 inactive_color: Color,
202}
203
204#[derive(Debug, PartialEq)]
205struct Options {
206 /// Padding around windows in logical pixels.
207 gaps: i32,
208 focus_ring: config::FocusRing,
209 /// Column widths that `toggle_width()` switches between.
210 preset_widths: Vec<ColumnWidth>,
211 /// Initial width for new windows.
212 default_width: Option<ColumnWidth>,
213}
214
215impl Default for Options {
216 fn default() -> Self {
217 Self {
218 gaps: 16,
219 focus_ring: Default::default(),
220 preset_widths: vec![
221 ColumnWidth::Proportion(1. / 3.),
222 ColumnWidth::Proportion(0.5),
223 ColumnWidth::Proportion(2. / 3.),
224 ],
225 default_width: None,
226 }
227 }
228}
229
230impl Options {
231 fn from_config(config: &Config) -> Self {
232 let preset_column_widths = &config.preset_column_widths;
233
234 let preset_widths = if preset_column_widths.is_empty() {
235 Options::default().preset_widths
236 } else {
237 preset_column_widths
238 .iter()
239 .copied()
240 .map(ColumnWidth::from)
241 .collect()
242 };
243
244 // Missing default_column_width maps to Some(ColumnWidth::Proportion(0.5)),
245 // while present, but empty, maps to None.
246 let default_width = config
247 .default_column_width
248 .as_ref()
249 .map(|w| w.0.first().copied().map(ColumnWidth::from))
250 .unwrap_or(Some(ColumnWidth::Proportion(0.5)));
251
252 Self {
253 gaps: config.gaps.into(),
254 focus_ring: config.focus_ring,
255 preset_widths,
256 default_width,
257 }
258 }
259}
260
261/// Width of a column.
262#[derive(Debug, Clone, Copy, PartialEq)]
263pub enum ColumnWidth {
264 /// Proportion of the current view width.
265 Proportion(f64),
266 /// One of the proportion presets.
267 ///
268 /// This is separate from Proportion in order to be able to reliably cycle between preset
269 /// proportions.
270 Preset(usize),
271 /// Fixed width in logical pixels.
272 Fixed(i32),
273}
274
275impl From<PresetWidth> for ColumnWidth {
276 fn from(value: PresetWidth) -> Self {
277 match value {
278 PresetWidth::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)),
279 PresetWidth::Fixed(f) => Self::Fixed(f.clamp(1, 100000)),
280 }
281 }
282}
283
284/// Height of a window in a column.
285///
286/// Proportional height is intentionally omitted. With column widths you frequently want e.g. two
287/// columns side-by-side with 50% width each, and you want them to remain this way when moving to a
288/// differently sized monitor. Windows in a column, however, already auto-size to fill the available
289/// height, giving you this behavior. The only reason to set a different window height, then, is
290/// when you want something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which
291/// corresponds to the `Fixed` variant.
292///
293/// This does not preclude the usual set of binds to set or resize a window proportionally. Just,
294/// they are converted to, and stored as fixed height right away, so that once you resize a window
295/// to fit the desired content, it can never become smaller than that when moving between monitors.
296#[derive(Debug, Clone, Copy, PartialEq, Eq)]
297pub enum WindowHeight {
298 /// Automatically computed height, evenly distributed across the column.
299 Auto,
300 /// Fixed height in logical pixels.
301 Fixed(i32),
302}
303
304#[derive(Debug)]
305struct Column<W: LayoutElement> {
306 /// Windows in this column.
307 ///
308 /// Must be non-empty.
309 windows: Vec<W>,
310
311 /// Heights of the windows.
312 ///
313 /// Must have the same number of elements as `windows`.
314 heights: Vec<WindowHeight>,
315
316 /// Index of the currently active window.
317 active_window_idx: usize,
318
319 /// Desired width of this column.
320 ///
321 /// If the column is full-width or full-screened, this is the width that should be restored
322 /// upon unfullscreening and untoggling full-width.
323 width: ColumnWidth,
324
325 /// Whether this column is full-width.
326 is_full_width: bool,
327
328 /// Whether this column contains a single full-screened window.
329 is_fullscreen: bool,
330
331 /// Latest known view size for this column's workspace.
332 view_size: Size<i32, Logical>,
333
334 /// Latest known working area for this column's workspace.
335 working_area: Rectangle<i32, Logical>,
336
337 /// Configurable properties of the layout.
338 options: Rc<Options>,
339}
340
341impl OutputId {
342 pub fn new(output: &Output) -> Self {
343 Self(output.name())
344 }
345}
346
347impl LayoutElement for Window {
348 fn request_size(&self, size: Size<i32, Logical>) {
349 self.toplevel().with_pending_state(|state| {
350 state.size = Some(size);
351 state.states.unset(xdg_toplevel::State::Fullscreen);
352 });
353 }
354
355 fn request_fullscreen(&self, size: Size<i32, Logical>) {
356 self.toplevel().with_pending_state(|state| {
357 state.size = Some(size);
358 state.states.set(xdg_toplevel::State::Fullscreen);
359 });
360 }
361
362 fn min_size(&self) -> Size<i32, Logical> {
363 with_states(self.toplevel().wl_surface(), |state| {
364 let curr = state.cached_state.current::<SurfaceCachedState>();
365 curr.min_size
366 })
367 }
368
369 fn max_size(&self) -> Size<i32, Logical> {
370 with_states(self.toplevel().wl_surface(), |state| {
371 let curr = state.cached_state.current::<SurfaceCachedState>();
372 curr.max_size
373 })
374 }
375
376 fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool {
377 self.toplevel().wl_surface() == wl_surface
378 }
379
380 fn set_preferred_scale_transform(&self, scale: i32, transform: Transform) {
381 self.with_surfaces(|surface, data| {
382 send_surface_state(surface, data, scale, transform);
383 });
384 }
385
386 fn has_ssd(&self) -> bool {
387 self.toplevel().current_state().decoration_mode
388 == Some(zxdg_toplevel_decoration_v1::Mode::ServerSide)
389 }
390}
391
392impl FocusRing {
393 fn update(
394 &mut self,
395 win_pos: Point<i32, Logical>,
396 win_size: Size<i32, Logical>,
397 is_border: bool,
398 ) {
399 if is_border {
400 self.buffers[0].resize((win_size.w + self.width * 2, self.width));
401 self.buffers[1].resize((win_size.w + self.width * 2, self.width));
402 self.buffers[2].resize((self.width, win_size.h));
403 self.buffers[3].resize((self.width, win_size.h));
404
405 self.locations[0] = win_pos + Point::from((-self.width, -self.width));
406 self.locations[1] = win_pos + Point::from((-self.width, win_size.h));
407 self.locations[2] = win_pos + Point::from((-self.width, 0));
408 self.locations[3] = win_pos + Point::from((win_size.w, 0));
409 } else {
410 let size = win_size + Size::from((self.width * 2, self.width * 2));
411 self.buffers[0].resize(size);
412 self.locations[0] = win_pos - Point::from((self.width, self.width));
413 }
414
415 self.is_border = is_border;
416 }
417
418 fn set_active(&mut self, is_active: bool) {
419 let color = if is_active {
420 self.active_color.into()
421 } else {
422 self.inactive_color.into()
423 };
424
425 for buf in &mut self.buffers {
426 buf.set_color(color);
427 }
428 }
429
430 fn render(&self, scale: Scale<f64>) -> impl Iterator<Item = SolidColorRenderElement> {
431 let mut rv = ArrayVec::<_, 4>::new();
432
433 if self.is_off {
434 return rv.into_iter();
435 }
436
437 let mut push = |buffer, location: Point<i32, Logical>| {
438 let elem = SolidColorRenderElement::from_buffer(
439 buffer,
440 location.to_physical_precise_round(scale),
441 scale,
442 1.,
443 Kind::Unspecified,
444 );
445 rv.push(elem);
446 };
447
448 if self.is_border {
449 for (buf, loc) in zip(&self.buffers, self.locations) {
450 push(buf, loc);
451 }
452 } else {
453 push(&self.buffers[0], self.locations[0]);
454 }
455
456 rv.into_iter()
457 }
458}
459
460impl FocusRing {
461 fn new(config: config::FocusRing) -> Self {
462 Self {
463 buffers: Default::default(),
464 locations: Default::default(),
465 is_off: config.off,
466 is_border: false,
467 width: config.width.into(),
468 active_color: config.active_color,
469 inactive_color: config.inactive_color,
470 }
471 }
472}
473
474impl WorkspaceSwitch {
475 fn current_idx(&self) -> f64 {
476 match self {
477 WorkspaceSwitch::Animation(anim) => anim.value(),
478 WorkspaceSwitch::Gesture(gesture) => gesture.current_idx,
479 }
480 }
481
482 /// Returns `true` if the workspace switch is [`Animation`].
483 ///
484 /// [`Animation`]: WorkspaceSwitch::Animation
485 #[must_use]
486 fn is_animation(&self) -> bool {
487 matches!(self, Self::Animation(..))
488 }
489}
490
491impl ColumnWidth {
492 fn resolve(self, options: &Options, view_width: i32) -> i32 {
493 match self {
494 ColumnWidth::Proportion(proportion) => {
495 ((view_width - options.gaps) as f64 * proportion).floor() as i32 - options.gaps
496 }
497 ColumnWidth::Preset(idx) => options.preset_widths[idx].resolve(options, view_width),
498 ColumnWidth::Fixed(width) => width,
499 }
500 }
501}
502
503impl<W: LayoutElement> Layout<W> {
504 pub fn new(config: &Config) -> Self {
505 Self {
506 monitor_set: MonitorSet::NoOutputs { workspaces: vec![] },
507 options: Rc::new(Options::from_config(config)),
508 }
509 }
510
511 pub fn add_output(&mut self, output: Output) {
512 let id = OutputId::new(&output);
513
514 self.monitor_set = match mem::take(&mut self.monitor_set) {
515 MonitorSet::Normal {
516 mut monitors,
517 primary_idx,
518 active_monitor_idx,
519 } => {
520 let primary = &mut monitors[primary_idx];
521
522 let mut workspaces = vec![];
523 for i in (0..primary.workspaces.len()).rev() {
524 if primary.workspaces[i].original_output == id {
525 let ws = primary.workspaces.remove(i);
526
527 // The user could've closed a window while remaining on this workspace, on
528 // another monitor. However, we will add an empty workspace in the end
529 // instead.
530 if ws.has_windows() {
531 workspaces.push(ws);
532 }
533
534 if i <= primary.active_workspace_idx {
535 primary.active_workspace_idx =
536 primary.active_workspace_idx.saturating_sub(1);
537 }
538 }
539 }
540 workspaces.reverse();
541
542 // Make sure there's always an empty workspace.
543 workspaces.push(Workspace::new(output.clone(), self.options.clone()));
544
545 for ws in &mut workspaces {
546 ws.set_output(Some(output.clone()));
547 }
548
549 monitors.push(Monitor::new(output, workspaces, self.options.clone()));
550 MonitorSet::Normal {
551 monitors,
552 primary_idx,
553 active_monitor_idx,
554 }
555 }
556 MonitorSet::NoOutputs { mut workspaces } => {
557 // We know there are no empty workspaces there, so add one.
558 workspaces.push(Workspace::new(output.clone(), self.options.clone()));
559
560 for workspace in &mut workspaces {
561 workspace.set_output(Some(output.clone()));
562 }
563
564 let monitor = Monitor::new(output, workspaces, self.options.clone());
565
566 MonitorSet::Normal {
567 monitors: vec![monitor],
568 primary_idx: 0,
569 active_monitor_idx: 0,
570 }
571 }
572 }
573 }
574
575 pub fn remove_output(&mut self, output: &Output) {
576 self.monitor_set = match mem::take(&mut self.monitor_set) {
577 MonitorSet::Normal {
578 mut monitors,
579 mut primary_idx,
580 mut active_monitor_idx,
581 } => {
582 let idx = monitors
583 .iter()
584 .position(|mon| &mon.output == output)
585 .expect("trying to remove non-existing output");
586 let monitor = monitors.remove(idx);
587 let mut workspaces = monitor.workspaces;
588
589 for ws in &mut workspaces {
590 ws.set_output(None);
591 }
592
593 // Get rid of empty workspaces.
594 workspaces.retain(|ws| ws.has_windows());
595
596 if monitors.is_empty() {
597 // Removed the last monitor.
598 MonitorSet::NoOutputs { workspaces }
599 } else {
600 if primary_idx >= idx {
601 // Update primary_idx to either still point at the same monitor, or at some
602 // other monitor if the primary has been removed.
603 primary_idx = primary_idx.saturating_sub(1);
604 }
605 if active_monitor_idx >= idx {
606 // Update active_monitor_idx to either still point at the same monitor, or
607 // at some other monitor if the active monitor has
608 // been removed.
609 active_monitor_idx = active_monitor_idx.saturating_sub(1);
610 }
611
612 let primary = &mut monitors[primary_idx];
613 for ws in &mut workspaces {
614 ws.set_output(Some(primary.output.clone()));
615 }
616
617 let empty_was_focused =
618 primary.active_workspace_idx == primary.workspaces.len() - 1;
619
620 // Push the workspaces from the removed monitor in the end, right before the
621 // last, empty, workspace.
622 let empty = primary.workspaces.remove(primary.workspaces.len() - 1);
623 primary.workspaces.extend(workspaces);
624 primary.workspaces.push(empty);
625
626 // If the empty workspace was focused on the primary monitor, keep it focused.
627 if empty_was_focused {
628 primary.active_workspace_idx = primary.workspaces.len() - 1;
629 }
630
631 MonitorSet::Normal {
632 monitors,
633 primary_idx,
634 active_monitor_idx,
635 }
636 }
637 }
638 MonitorSet::NoOutputs { .. } => {
639 panic!("tried to remove output when there were already none")
640 }
641 }
642 }
643
644 pub fn add_window_by_idx(
645 &mut self,
646 monitor_idx: usize,
647 workspace_idx: usize,
648 window: W,
649 activate: bool,
650 width: ColumnWidth,
651 is_full_width: bool,
652 ) {
653 let MonitorSet::Normal {
654 monitors,
655 active_monitor_idx,
656 ..
657 } = &mut self.monitor_set
658 else {
659 panic!()
660 };
661
662 monitors[monitor_idx].add_window(workspace_idx, window, activate, width, is_full_width);
663
664 if activate {
665 *active_monitor_idx = monitor_idx;
666 }
667 }
668
669 /// Adds a new window to the layout.
670 ///
671 /// Returns an output that the window was added to, if there were any outputs.
672 pub fn add_window(
673 &mut self,
674 window: W,
675 width: Option<ColumnWidth>,
676 is_full_width: bool,
677 ) -> Option<&Output> {
678 let width = width
679 .or(self.options.default_width)
680 .unwrap_or_else(|| ColumnWidth::Fixed(window.geometry().size.w));
681
682 match &mut self.monitor_set {
683 MonitorSet::Normal {
684 monitors,
685 active_monitor_idx,
686 ..
687 } => {
688 let mon = &mut monitors[*active_monitor_idx];
689
690 // Don't steal focus from an active fullscreen window.
691 let mut activate = true;
692 let ws = &mon.workspaces[mon.active_workspace_idx];
693 if !ws.columns.is_empty() && ws.columns[ws.active_column_idx].is_fullscreen {
694 activate = false;
695 }
696
697 mon.add_window(
698 mon.active_workspace_idx,
699 window,
700 activate,
701 width,
702 is_full_width,
703 );
704 Some(&mon.output)
705 }
706 MonitorSet::NoOutputs { workspaces } => {
707 let ws = if let Some(ws) = workspaces.get_mut(0) {
708 ws
709 } else {
710 workspaces.push(Workspace::new_no_outputs(self.options.clone()));
711 &mut workspaces[0]
712 };
713 ws.add_window(window, true, width, is_full_width);
714 None
715 }
716 }
717 }
718
719 pub fn remove_window(&mut self, window: &W) {
720 match &mut self.monitor_set {
721 MonitorSet::Normal { monitors, .. } => {
722 for mon in monitors {
723 for (idx, ws) in mon.workspaces.iter_mut().enumerate() {
724 if ws.has_window(window) {
725 ws.remove_window(window);
726
727 // Clean up empty workspaces that are not active and not last.
728 if !ws.has_windows()
729 && idx != mon.active_workspace_idx
730 && idx != mon.workspaces.len() - 1
731 {
732 mon.workspaces.remove(idx);
733
734 if idx < mon.active_workspace_idx {
735 mon.active_workspace_idx -= 1;
736 }
737 }
738
739 break;
740 }
741 }
742 }
743 }
744 MonitorSet::NoOutputs { workspaces, .. } => {
745 for (idx, ws) in workspaces.iter_mut().enumerate() {
746 if ws.has_window(window) {
747 ws.remove_window(window);
748
749 // Clean up empty workspaces.
750 if !ws.has_windows() {
751 workspaces.remove(idx);
752 }
753
754 break;
755 }
756 }
757 }
758 }
759 }
760
761 pub fn update_window(&mut self, window: &W) {
762 match &mut self.monitor_set {
763 MonitorSet::Normal { monitors, .. } => {
764 for mon in monitors {
765 for ws in &mut mon.workspaces {
766 if ws.has_window(window) {
767 ws.update_window(window);
768 return;
769 }
770 }
771 }
772 }
773 MonitorSet::NoOutputs { workspaces, .. } => {
774 for ws in workspaces {
775 if ws.has_window(window) {
776 ws.update_window(window);
777 return;
778 }
779 }
780 }
781 }
782 }
783
784 pub fn find_window_and_output(&self, wl_surface: &WlSurface) -> Option<(W, Output)> {
785 if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
786 for mon in monitors {
787 for ws in &mon.workspaces {
788 if let Some(window) = ws.find_wl_surface(wl_surface) {
789 return Some((window.clone(), mon.output.clone()));
790 }
791 }
792 }
793 }
794
795 None
796 }
797
798 pub fn update_output_size(&mut self, output: &Output) {
799 let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else {
800 panic!()
801 };
802
803 for mon in monitors {
804 if &mon.output == output {
805 let view_size = output_size(output);
806 let working_area = layer_map_for_output(output).non_exclusive_zone();
807
808 for ws in &mut mon.workspaces {
809 ws.set_view_size(view_size, working_area);
810 }
811
812 break;
813 }
814 }
815 }
816
817 pub fn activate_window(&mut self, window: &W) {
818 let MonitorSet::Normal {
819 monitors,
820 active_monitor_idx,
821 ..
822 } = &mut self.monitor_set
823 else {
824 todo!()
825 };
826
827 for (monitor_idx, mon) in monitors.iter_mut().enumerate() {
828 for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() {
829 if ws.has_window(window) {
830 *active_monitor_idx = monitor_idx;
831 ws.activate_window(window);
832
833 // Switch to that workspace if not already during a transition.
834 if mon.workspace_switch.is_none() {
835 mon.switch_workspace(workspace_idx);
836 }
837
838 break;
839 }
840 }
841 }
842 }
843
844 pub fn activate_output(&mut self, output: &Output) {
845 let MonitorSet::Normal {
846 monitors,
847 active_monitor_idx,
848 ..
849 } = &mut self.monitor_set
850 else {
851 return;
852 };
853
854 let idx = monitors
855 .iter()
856 .position(|mon| &mon.output == output)
857 .unwrap();
858 *active_monitor_idx = idx;
859 }
860
861 pub fn active_output(&self) -> Option<&Output> {
862 let MonitorSet::Normal {
863 monitors,
864 active_monitor_idx,
865 ..
866 } = &self.monitor_set
867 else {
868 return None;
869 };
870
871 Some(&monitors[*active_monitor_idx].output)
872 }
873
874 pub fn active_workspace(&self) -> Option<&Workspace<W>> {
875 let MonitorSet::Normal {
876 monitors,
877 active_monitor_idx,
878 ..
879 } = &self.monitor_set
880 else {
881 return None;
882 };
883
884 let mon = &monitors[*active_monitor_idx];
885 Some(&mon.workspaces[mon.active_workspace_idx])
886 }
887
888 pub fn active_window(&self) -> Option<(W, Output)> {
889 let MonitorSet::Normal {
890 monitors,
891 active_monitor_idx,
892 ..
893 } = &self.monitor_set
894 else {
895 return None;
896 };
897
898 let mon = &monitors[*active_monitor_idx];
899 let ws = &mon.workspaces[mon.active_workspace_idx];
900
901 if ws.columns.is_empty() {
902 return None;
903 }
904
905 let col = &ws.columns[ws.active_column_idx];
906 Some((
907 col.windows[col.active_window_idx].clone(),
908 mon.output.clone(),
909 ))
910 }
911
912 pub fn windows_for_output(&self, output: &Output) -> impl Iterator<Item = &W> + '_ {
913 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
914 panic!()
915 };
916
917 let mon = monitors.iter().find(|mon| &mon.output == output).unwrap();
918 mon.workspaces.iter().flat_map(|ws| ws.windows())
919 }
920
921 fn active_monitor(&mut self) -> Option<&mut Monitor<W>> {
922 let MonitorSet::Normal {
923 monitors,
924 active_monitor_idx,
925 ..
926 } = &mut self.monitor_set
927 else {
928 return None;
929 };
930
931 Some(&mut monitors[*active_monitor_idx])
932 }
933
934 pub fn monitor_for_output(&self, output: &Output) -> Option<&Monitor<W>> {
935 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
936 return None;
937 };
938
939 monitors.iter().find(|monitor| &monitor.output == output)
940 }
941
942 pub fn outputs(&self) -> impl Iterator<Item = &Output> + '_ {
943 let monitors = if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
944 &monitors[..]
945 } else {
946 &[][..]
947 };
948
949 monitors.iter().map(|mon| &mon.output)
950 }
951
952 pub fn move_left(&mut self) {
953 let Some(monitor) = self.active_monitor() else {
954 return;
955 };
956 monitor.move_left();
957 }
958
959 pub fn move_right(&mut self) {
960 let Some(monitor) = self.active_monitor() else {
961 return;
962 };
963 monitor.move_right();
964 }
965
966 pub fn move_down(&mut self) {
967 let Some(monitor) = self.active_monitor() else {
968 return;
969 };
970 monitor.move_down();
971 }
972
973 pub fn move_up(&mut self) {
974 let Some(monitor) = self.active_monitor() else {
975 return;
976 };
977 monitor.move_up();
978 }
979
980 pub fn focus_left(&mut self) {
981 let Some(monitor) = self.active_monitor() else {
982 return;
983 };
984 monitor.focus_left();
985 }
986
987 pub fn focus_right(&mut self) {
988 let Some(monitor) = self.active_monitor() else {
989 return;
990 };
991 monitor.focus_right();
992 }
993
994 pub fn focus_down(&mut self) {
995 let Some(monitor) = self.active_monitor() else {
996 return;
997 };
998 monitor.focus_down();
999 }
1000
1001 pub fn focus_up(&mut self) {
1002 let Some(monitor) = self.active_monitor() else {
1003 return;
1004 };
1005 monitor.focus_up();
1006 }
1007
1008 pub fn move_to_workspace_up(&mut self) {
1009 let Some(monitor) = self.active_monitor() else {
1010 return;
1011 };
1012 monitor.move_to_workspace_up();
1013 }
1014
1015 pub fn move_to_workspace_down(&mut self) {
1016 let Some(monitor) = self.active_monitor() else {
1017 return;
1018 };
1019 monitor.move_to_workspace_down();
1020 }
1021
1022 pub fn move_to_workspace(&mut self, idx: usize) {
1023 let Some(monitor) = self.active_monitor() else {
1024 return;
1025 };
1026 monitor.move_to_workspace(idx);
1027 }
1028
1029 pub fn switch_workspace_up(&mut self) {
1030 let Some(monitor) = self.active_monitor() else {
1031 return;
1032 };
1033 monitor.switch_workspace_up();
1034 }
1035
1036 pub fn switch_workspace_down(&mut self) {
1037 let Some(monitor) = self.active_monitor() else {
1038 return;
1039 };
1040 monitor.switch_workspace_down();
1041 }
1042
1043 pub fn switch_workspace(&mut self, idx: usize) {
1044 let Some(monitor) = self.active_monitor() else {
1045 return;
1046 };
1047 monitor.switch_workspace(idx);
1048 }
1049
1050 pub fn consume_into_column(&mut self) {
1051 let Some(monitor) = self.active_monitor() else {
1052 return;
1053 };
1054 monitor.consume_into_column();
1055 }
1056
1057 pub fn expel_from_column(&mut self) {
1058 let Some(monitor) = self.active_monitor() else {
1059 return;
1060 };
1061 monitor.expel_from_column();
1062 }
1063
1064 pub fn center_column(&mut self) {
1065 let Some(monitor) = self.active_monitor() else {
1066 return;
1067 };
1068 monitor.center_column();
1069 }
1070
1071 pub fn focus(&self) -> Option<&W> {
1072 let MonitorSet::Normal {
1073 monitors,
1074 active_monitor_idx,
1075 ..
1076 } = &self.monitor_set
1077 else {
1078 return None;
1079 };
1080
1081 monitors[*active_monitor_idx].focus()
1082 }
1083
1084 pub fn window_under(
1085 &self,
1086 output: &Output,
1087 pos_within_output: Point<f64, Logical>,
1088 ) -> Option<(&W, Point<i32, Logical>)> {
1089 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
1090 return None;
1091 };
1092
1093 let mon = monitors.iter().find(|mon| &mon.output == output)?;
1094 mon.window_under(pos_within_output)
1095 }
1096
1097 #[cfg(test)]
1098 fn verify_invariants(&self) {
1099 let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set {
1100 MonitorSet::Normal {
1101 monitors,
1102 primary_idx,
1103 active_monitor_idx,
1104 } => (monitors, primary_idx, active_monitor_idx),
1105 MonitorSet::NoOutputs { workspaces } => {
1106 for workspace in workspaces {
1107 assert!(
1108 workspace.has_windows(),
1109 "with no outputs there cannot be empty workspaces"
1110 );
1111
1112 assert_eq!(
1113 workspace.options, self.options,
1114 "workspace options must be synchronized with layout"
1115 );
1116
1117 workspace.verify_invariants();
1118 }
1119
1120 return;
1121 }
1122 };
1123
1124 assert!(primary_idx < monitors.len());
1125 assert!(active_monitor_idx < monitors.len());
1126
1127 for (idx, monitor) in monitors.iter().enumerate() {
1128 assert!(
1129 !monitor.workspaces.is_empty(),
1130 "monitor must have at least one workspace"
1131 );
1132 assert!(monitor.active_workspace_idx < monitor.workspaces.len());
1133
1134 assert_eq!(
1135 monitor.options, self.options,
1136 "monitor options must be synchronized with layout"
1137 );
1138
1139 let monitor_id = OutputId::new(&monitor.output);
1140
1141 if idx == primary_idx {
1142 for ws in &monitor.workspaces {
1143 if ws.original_output == monitor_id {
1144 // This is the primary monitor's own workspace.
1145 continue;
1146 }
1147
1148 let own_monitor_exists = monitors
1149 .iter()
1150 .any(|m| OutputId::new(&m.output) == ws.original_output);
1151 assert!(
1152 !own_monitor_exists,
1153 "primary monitor cannot have workspaces for which their own monitor exists"
1154 );
1155 }
1156 } else {
1157 assert!(
1158 monitor
1159 .workspaces
1160 .iter()
1161 .any(|workspace| workspace.original_output == monitor_id),
1162 "secondary monitor must not have any non-own workspaces"
1163 );
1164 }
1165
1166 assert!(
1167 monitor.workspaces.last().unwrap().columns.is_empty(),
1168 "monitor must have an empty workspace in the end"
1169 );
1170
1171 // If there's no workspace switch in progress, there can't be any non-last non-active
1172 // empty workspaces.
1173 if monitor.workspace_switch.is_none() {
1174 for (idx, ws) in monitor.workspaces.iter().enumerate().rev().skip(1) {
1175 if idx != monitor.active_workspace_idx {
1176 assert!(
1177 !ws.columns.is_empty(),
1178 "non-active workspace can't be empty except the last one"
1179 );
1180 }
1181 }
1182 }
1183
1184 // FIXME: verify that primary doesn't have any workspaces for which their own monitor
1185 // exists.
1186
1187 for workspace in &monitor.workspaces {
1188 assert_eq!(
1189 workspace.options, self.options,
1190 "workspace options must be synchronized with layout"
1191 );
1192
1193 workspace.verify_invariants();
1194 }
1195 }
1196 }
1197
1198 pub fn advance_animations(&mut self, current_time: Duration) {
1199 let _span = tracy_client::span!("Layout::advance_animations");
1200
1201 match &mut self.monitor_set {
1202 MonitorSet::Normal {
1203 monitors,
1204 active_monitor_idx,
1205 ..
1206 } => {
1207 for (idx, mon) in monitors.iter_mut().enumerate() {
1208 mon.advance_animations(current_time, idx == *active_monitor_idx);
1209 }
1210 }
1211 MonitorSet::NoOutputs { workspaces, .. } => {
1212 for ws in workspaces {
1213 ws.advance_animations(current_time, false);
1214 }
1215 }
1216 }
1217 }
1218
1219 pub fn update_config(&mut self, config: &Config) {
1220 let options = Rc::new(Options::from_config(config));
1221
1222 match &mut self.monitor_set {
1223 MonitorSet::Normal { monitors, .. } => {
1224 for mon in monitors {
1225 mon.update_config(options.clone());
1226 }
1227 }
1228 MonitorSet::NoOutputs { workspaces } => {
1229 for ws in workspaces {
1230 ws.update_config(options.clone());
1231 }
1232 }
1233 }
1234
1235 self.options = options;
1236 }
1237
1238 pub fn toggle_width(&mut self) {
1239 let Some(monitor) = self.active_monitor() else {
1240 return;
1241 };
1242 monitor.toggle_width();
1243 }
1244
1245 pub fn toggle_full_width(&mut self) {
1246 let Some(monitor) = self.active_monitor() else {
1247 return;
1248 };
1249 monitor.toggle_full_width();
1250 }
1251
1252 pub fn set_column_width(&mut self, change: SizeChange) {
1253 let Some(monitor) = self.active_monitor() else {
1254 return;
1255 };
1256 monitor.set_column_width(change);
1257 }
1258
1259 pub fn set_window_height(&mut self, change: SizeChange) {
1260 let Some(monitor) = self.active_monitor() else {
1261 return;
1262 };
1263 monitor.set_window_height(change);
1264 }
1265
1266 pub fn focus_output(&mut self, output: &Output) {
1267 if let MonitorSet::Normal {
1268 monitors,
1269 active_monitor_idx,
1270 ..
1271 } = &mut self.monitor_set
1272 {
1273 for (idx, mon) in monitors.iter().enumerate() {
1274 if &mon.output == output {
1275 *active_monitor_idx = idx;
1276 return;
1277 }
1278 }
1279 }
1280 }
1281
1282 pub fn move_to_output(&mut self, output: &Output) {
1283 if let MonitorSet::Normal {
1284 monitors,
1285 active_monitor_idx,
1286 ..
1287 } = &mut self.monitor_set
1288 {
1289 let new_idx = monitors
1290 .iter()
1291 .position(|mon| &mon.output == output)
1292 .unwrap();
1293
1294 let current = &mut monitors[*active_monitor_idx];
1295 let ws = current.active_workspace();
1296 if !ws.has_windows() {
1297 return;
1298 }
1299 let column = &ws.columns[ws.active_column_idx];
1300 let window = column.windows[column.active_window_idx].clone();
1301 let width = column.width;
1302 let is_full_width = column.is_full_width;
1303 ws.remove_window(&window);
1304
1305 let workspace_idx = monitors[new_idx].active_workspace_idx;
1306 self.add_window_by_idx(new_idx, workspace_idx, window, true, width, is_full_width);
1307 }
1308 }
1309
1310 pub fn move_window_to_output(&mut self, window: W, output: &Output) {
1311 if !matches!(&self.monitor_set, MonitorSet::Normal { .. }) {
1312 return;
1313 }
1314
1315 self.remove_window(&window);
1316
1317 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
1318 let mut width = None;
1319 let mut is_full_width = false;
1320 for mon in &*monitors {
1321 for ws in &mon.workspaces {
1322 for col in &ws.columns {
1323 if col.windows.contains(&window) {
1324 width = Some(col.width);
1325 is_full_width = col.is_full_width;
1326 break;
1327 }
1328 }
1329 }
1330 }
1331 let Some(width) = width else { return };
1332
1333 let new_idx = monitors
1334 .iter()
1335 .position(|mon| &mon.output == output)
1336 .unwrap();
1337
1338 let workspace_idx = monitors[new_idx].active_workspace_idx;
1339 // FIXME: activate only if it was already active and focused.
1340 self.add_window_by_idx(new_idx, workspace_idx, window, true, width, is_full_width);
1341 }
1342 }
1343
1344 pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) {
1345 match &mut self.monitor_set {
1346 MonitorSet::Normal { monitors, .. } => {
1347 for mon in monitors {
1348 for ws in &mut mon.workspaces {
1349 if ws.has_window(window) {
1350 ws.set_fullscreen(window, is_fullscreen);
1351 return;
1352 }
1353 }
1354 }
1355 }
1356 MonitorSet::NoOutputs { workspaces, .. } => {
1357 for ws in workspaces {
1358 if ws.has_window(window) {
1359 ws.set_fullscreen(window, is_fullscreen);
1360 return;
1361 }
1362 }
1363 }
1364 }
1365 }
1366
1367 pub fn toggle_fullscreen(&mut self, window: &W) {
1368 match &mut self.monitor_set {
1369 MonitorSet::Normal { monitors, .. } => {
1370 for mon in monitors {
1371 for ws in &mut mon.workspaces {
1372 if ws.has_window(window) {
1373 ws.toggle_fullscreen(window);
1374 return;
1375 }
1376 }
1377 }
1378 }
1379 MonitorSet::NoOutputs { workspaces, .. } => {
1380 for ws in workspaces {
1381 if ws.has_window(window) {
1382 ws.toggle_fullscreen(window);
1383 return;
1384 }
1385 }
1386 }
1387 }
1388 }
1389
1390 pub fn workspace_switch_gesture_begin(&mut self, output: &Output) {
1391 let monitors = match &mut self.monitor_set {
1392 MonitorSet::Normal { monitors, .. } => monitors,
1393 MonitorSet::NoOutputs { .. } => unreachable!(),
1394 };
1395
1396 for monitor in monitors {
1397 // Cancel the gesture on other outputs.
1398 if &monitor.output != output {
1399 if let Some(WorkspaceSwitch::Gesture(_)) = monitor.workspace_switch {
1400 monitor.workspace_switch = None;
1401 }
1402 continue;
1403 }
1404
1405 let center_idx = monitor.active_workspace_idx;
1406 let current_idx = monitor
1407 .workspace_switch
1408 .as_ref()
1409 .map(|s| s.current_idx())
1410 .unwrap_or(center_idx as f64);
1411
1412 let gesture = WorkspaceSwitchGesture {
1413 center_idx,
1414 current_idx,
1415 };
1416 monitor.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture));
1417 }
1418 }
1419
1420 pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<Option<Output>> {
1421 let monitors = match &mut self.monitor_set {
1422 MonitorSet::Normal { monitors, .. } => monitors,
1423 MonitorSet::NoOutputs { .. } => return None,
1424 };
1425
1426 for monitor in monitors {
1427 if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch {
1428 // Normalize like GNOME Shell's workspace switching.
1429 let delta_y = -delta_y / 400.;
1430
1431 let min = gesture.center_idx.saturating_sub(1) as f64;
1432 let max = (gesture.center_idx + 1).min(monitor.workspaces.len() - 1) as f64;
1433 let new_idx = (gesture.current_idx + delta_y).clamp(min, max);
1434
1435 if gesture.current_idx == new_idx {
1436 return Some(None);
1437 }
1438
1439 gesture.current_idx = new_idx;
1440 return Some(Some(monitor.output.clone()));
1441 }
1442 }
1443
1444 None
1445 }
1446
1447 pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> Option<Output> {
1448 let monitors = match &mut self.monitor_set {
1449 MonitorSet::Normal { monitors, .. } => monitors,
1450 MonitorSet::NoOutputs { .. } => return None,
1451 };
1452
1453 for monitor in monitors {
1454 if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch {
1455 if cancelled {
1456 monitor.workspace_switch = None;
1457 return Some(monitor.output.clone());
1458 }
1459
1460 // FIXME: keep track of gesture velocity and use it to compute the final point and
1461 // to animate to it.
1462 let current_idx = gesture.current_idx;
1463 let idx = current_idx.round() as usize;
1464
1465 monitor.active_workspace_idx = idx;
1466 monitor.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1467 current_idx,
1468 idx as f64,
1469 Duration::from_millis(250),
1470 )));
1471
1472 return Some(monitor.output.clone());
1473 }
1474 }
1475
1476 None
1477 }
1478
1479 pub fn move_workspace_down(&mut self) {
1480 let Some(monitor) = self.active_monitor() else {
1481 return;
1482 };
1483 monitor.move_workspace_down();
1484 }
1485
1486 pub fn move_workspace_up(&mut self) {
1487 let Some(monitor) = self.active_monitor() else {
1488 return;
1489 };
1490 monitor.move_workspace_up();
1491 }
1492}
1493
1494impl Layout<Window> {
1495 pub fn refresh(&self) {
1496 let _span = tracy_client::span!("MonitorSet::refresh");
1497
1498 match &self.monitor_set {
1499 MonitorSet::Normal { monitors, .. } => {
1500 for mon in monitors {
1501 for ws in &mon.workspaces {
1502 ws.refresh();
1503 }
1504 }
1505 }
1506 MonitorSet::NoOutputs { workspaces, .. } => {
1507 for ws in workspaces {
1508 ws.refresh();
1509 }
1510 }
1511 }
1512 }
1513}
1514
1515impl<W: LayoutElement> Default for MonitorSet<W> {
1516 fn default() -> Self {
1517 Self::NoOutputs { workspaces: vec![] }
1518 }
1519}
1520
1521impl<W: LayoutElement> Monitor<W> {
1522 fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self {
1523 Self {
1524 output,
1525 workspaces,
1526 active_workspace_idx: 0,
1527 workspace_switch: None,
1528 options,
1529 }
1530 }
1531
1532 fn active_workspace(&mut self) -> &mut Workspace<W> {
1533 &mut self.workspaces[self.active_workspace_idx]
1534 }
1535
1536 fn activate_workspace(&mut self, idx: usize) {
1537 if self.active_workspace_idx == idx {
1538 return;
1539 }
1540
1541 let current_idx = self
1542 .workspace_switch
1543 .as_ref()
1544 .map(|s| s.current_idx())
1545 .unwrap_or(self.active_workspace_idx as f64);
1546
1547 self.active_workspace_idx = idx;
1548
1549 self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new(
1550 current_idx,
1551 idx as f64,
1552 Duration::from_millis(250),
1553 )));
1554 }
1555
1556 pub fn add_window(
1557 &mut self,
1558 workspace_idx: usize,
1559 window: W,
1560 activate: bool,
1561 width: ColumnWidth,
1562 is_full_width: bool,
1563 ) {
1564 let workspace = &mut self.workspaces[workspace_idx];
1565
1566 workspace.add_window(window.clone(), activate, width, is_full_width);
1567
1568 // After adding a new window, workspace becomes this output's own.
1569 workspace.original_output = OutputId::new(&self.output);
1570
1571 if workspace_idx == self.workspaces.len() - 1 {
1572 // Insert a new empty workspace.
1573 let ws = Workspace::new(self.output.clone(), self.options.clone());
1574 self.workspaces.push(ws);
1575 }
1576
1577 if activate {
1578 self.activate_workspace(workspace_idx);
1579 }
1580 }
1581
1582 fn clean_up_workspaces(&mut self) {
1583 assert!(self.workspace_switch.is_none());
1584
1585 for idx in (0..self.workspaces.len() - 1).rev() {
1586 if self.active_workspace_idx == idx {
1587 continue;
1588 }
1589
1590 if !self.workspaces[idx].has_windows() {
1591 self.workspaces.remove(idx);
1592 if self.active_workspace_idx > idx {
1593 self.active_workspace_idx -= 1;
1594 }
1595 }
1596 }
1597 }
1598
1599 pub fn move_left(&mut self) {
1600 self.active_workspace().move_left();
1601 }
1602
1603 pub fn move_right(&mut self) {
1604 self.active_workspace().move_right();
1605 }
1606
1607 pub fn move_down(&mut self) {
1608 self.active_workspace().move_down();
1609 }
1610
1611 pub fn move_up(&mut self) {
1612 self.active_workspace().move_up();
1613 }
1614
1615 pub fn focus_left(&mut self) {
1616 self.active_workspace().focus_left();
1617 }
1618
1619 pub fn focus_right(&mut self) {
1620 self.active_workspace().focus_right();
1621 }
1622
1623 pub fn focus_down(&mut self) {
1624 self.active_workspace().focus_down();
1625 }
1626
1627 pub fn focus_up(&mut self) {
1628 self.active_workspace().focus_up();
1629 }
1630
1631 pub fn move_to_workspace_up(&mut self) {
1632 let source_workspace_idx = self.active_workspace_idx;
1633
1634 let new_idx = source_workspace_idx.saturating_sub(1);
1635 if new_idx == source_workspace_idx {
1636 return;
1637 }
1638
1639 let workspace = &mut self.workspaces[source_workspace_idx];
1640 if workspace.columns.is_empty() {
1641 return;
1642 }
1643
1644 let column = &mut workspace.columns[workspace.active_column_idx];
1645 let width = column.width;
1646 let is_full_width = column.is_full_width;
1647 let window = column.windows[column.active_window_idx].clone();
1648 workspace.remove_window(&window);
1649
1650 self.add_window(new_idx, window, true, width, is_full_width);
1651 }
1652
1653 pub fn move_to_workspace_down(&mut self) {
1654 let source_workspace_idx = self.active_workspace_idx;
1655
1656 let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1);
1657 if new_idx == source_workspace_idx {
1658 return;
1659 }
1660
1661 let workspace = &mut self.workspaces[source_workspace_idx];
1662 if workspace.columns.is_empty() {
1663 return;
1664 }
1665
1666 let column = &mut workspace.columns[workspace.active_column_idx];
1667 let width = column.width;
1668 let is_full_width = column.is_full_width;
1669 let window = column.windows[column.active_window_idx].clone();
1670 workspace.remove_window(&window);
1671
1672 self.add_window(new_idx, window, true, width, is_full_width);
1673 }
1674
1675 pub fn move_to_workspace(&mut self, idx: usize) {
1676 let source_workspace_idx = self.active_workspace_idx;
1677
1678 let new_idx = min(idx, self.workspaces.len() - 1);
1679 if new_idx == source_workspace_idx {
1680 return;
1681 }
1682
1683 let workspace = &mut self.workspaces[source_workspace_idx];
1684 if workspace.columns.is_empty() {
1685 return;
1686 }
1687
1688 let column = &mut workspace.columns[workspace.active_column_idx];
1689 let width = column.width;
1690 let is_full_width = column.is_full_width;
1691 let window = column.windows[column.active_window_idx].clone();
1692 workspace.remove_window(&window);
1693
1694 self.add_window(new_idx, window, true, width, is_full_width);
1695
1696 // Don't animate this action.
1697 self.workspace_switch = None;
1698
1699 self.clean_up_workspaces();
1700 }
1701
1702 pub fn switch_workspace_up(&mut self) {
1703 self.activate_workspace(self.active_workspace_idx.saturating_sub(1));
1704 }
1705
1706 pub fn switch_workspace_down(&mut self) {
1707 self.activate_workspace(min(
1708 self.active_workspace_idx + 1,
1709 self.workspaces.len() - 1,
1710 ));
1711 }
1712
1713 pub fn switch_workspace(&mut self, idx: usize) {
1714 self.activate_workspace(min(idx, self.workspaces.len() - 1));
1715 // Don't animate this action.
1716 self.workspace_switch = None;
1717
1718 self.clean_up_workspaces();
1719 }
1720
1721 pub fn consume_into_column(&mut self) {
1722 self.active_workspace().consume_into_column();
1723 }
1724
1725 pub fn expel_from_column(&mut self) {
1726 self.active_workspace().expel_from_column();
1727 }
1728
1729 pub fn center_column(&mut self) {
1730 self.active_workspace().center_column();
1731 }
1732
1733 pub fn focus(&self) -> Option<&W> {
1734 let workspace = &self.workspaces[self.active_workspace_idx];
1735 if !workspace.has_windows() {
1736 return None;
1737 }
1738
1739 let column = &workspace.columns[workspace.active_column_idx];
1740 Some(&column.windows[column.active_window_idx])
1741 }
1742
1743 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
1744 if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch {
1745 anim.set_current_time(current_time);
1746 if anim.is_done() {
1747 self.workspace_switch = None;
1748 self.clean_up_workspaces();
1749 }
1750 }
1751
1752 for ws in &mut self.workspaces {
1753 ws.advance_animations(current_time, is_active);
1754 }
1755 }
1756
1757 pub fn are_animations_ongoing(&self) -> bool {
1758 self.workspace_switch
1759 .as_ref()
1760 .is_some_and(|s| s.is_animation())
1761 || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
1762 }
1763
1764 pub fn are_transitions_ongoing(&self) -> bool {
1765 self.workspace_switch.is_some()
1766 || self.workspaces.iter().any(|ws| ws.are_animations_ongoing())
1767 }
1768
1769 fn update_config(&mut self, options: Rc<Options>) {
1770 for ws in &mut self.workspaces {
1771 ws.update_config(options.clone());
1772 }
1773
1774 self.options = options;
1775 }
1776
1777 fn toggle_width(&mut self) {
1778 self.active_workspace().toggle_width();
1779 }
1780
1781 fn toggle_full_width(&mut self) {
1782 self.active_workspace().toggle_full_width();
1783 }
1784
1785 fn set_column_width(&mut self, change: SizeChange) {
1786 self.active_workspace().set_column_width(change);
1787 }
1788
1789 fn set_window_height(&mut self, change: SizeChange) {
1790 self.active_workspace().set_window_height(change);
1791 }
1792
1793 fn move_workspace_down(&mut self) {
1794 let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1);
1795 if new_idx == self.active_workspace_idx {
1796 return;
1797 }
1798
1799 self.workspaces.swap(self.active_workspace_idx, new_idx);
1800
1801 if new_idx == self.workspaces.len() - 1 {
1802 // Insert a new empty workspace.
1803 let ws = Workspace::new(self.output.clone(), self.options.clone());
1804 self.workspaces.push(ws);
1805 }
1806
1807 self.activate_workspace(new_idx);
1808 self.workspace_switch = None;
1809
1810 self.clean_up_workspaces();
1811 }
1812
1813 fn move_workspace_up(&mut self) {
1814 let new_idx = self.active_workspace_idx.saturating_sub(1);
1815 if new_idx == self.active_workspace_idx {
1816 return;
1817 }
1818
1819 self.workspaces.swap(self.active_workspace_idx, new_idx);
1820
1821 if self.active_workspace_idx == self.workspaces.len() - 1 {
1822 // Insert a new empty workspace.
1823 let ws = Workspace::new(self.output.clone(), self.options.clone());
1824 self.workspaces.push(ws);
1825 }
1826
1827 self.activate_workspace(new_idx);
1828 self.workspace_switch = None;
1829
1830 self.clean_up_workspaces();
1831 }
1832
1833 pub fn window_under(
1834 &self,
1835 pos_within_output: Point<f64, Logical>,
1836 ) -> Option<(&W, Point<i32, Logical>)> {
1837 match &self.workspace_switch {
1838 Some(switch) => {
1839 let size = output_size(&self.output);
1840
1841 let render_idx = switch.current_idx();
1842 let before_idx = render_idx.floor() as usize;
1843 let after_idx = render_idx.ceil() as usize;
1844
1845 let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
1846
1847 let (idx, ws_offset) = if pos_within_output.y < (size.h - offset) as f64 {
1848 (before_idx, Point::from((0, offset)))
1849 } else {
1850 (after_idx, Point::from((0, -size.h + offset)))
1851 };
1852
1853 let ws = &self.workspaces[idx];
1854 let (win, win_pos) = ws.window_under(pos_within_output + ws_offset.to_f64())?;
1855 Some((win, win_pos - ws_offset))
1856 }
1857 None => {
1858 let ws = &self.workspaces[self.active_workspace_idx];
1859 ws.window_under(pos_within_output)
1860 }
1861 }
1862 }
1863
1864 pub fn render_above_top_layer(&self) -> bool {
1865 // Render above the top layer only if the view is stationary.
1866 if self.workspace_switch.is_some() {
1867 return false;
1868 }
1869
1870 let ws = &self.workspaces[self.active_workspace_idx];
1871 ws.render_above_top_layer()
1872 }
1873}
1874
1875impl Monitor<Window> {
1876 pub fn render_elements(
1877 &self,
1878 renderer: &mut GlesRenderer,
1879 ) -> Vec<MonitorRenderElement<GlesRenderer>> {
1880 let _span = tracy_client::span!("Monitor::render_elements");
1881
1882 let output_scale = Scale::from(self.output.current_scale().fractional_scale());
1883 let output_transform = self.output.current_transform();
1884 let output_mode = self.output.current_mode().unwrap();
1885 let size = output_transform.transform_size(output_mode.size);
1886
1887 match &self.workspace_switch {
1888 Some(switch) => {
1889 let render_idx = switch.current_idx();
1890 let before_idx = render_idx.floor() as usize;
1891 let after_idx = render_idx.ceil() as usize;
1892
1893 let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32;
1894
1895 let before = self.workspaces[before_idx].render_elements(renderer);
1896 let after = self.workspaces[after_idx].render_elements(renderer);
1897
1898 let before = before.into_iter().filter_map(|elem| {
1899 Some(RelocateRenderElement::from_element(
1900 CropRenderElement::from_element(
1901 elem,
1902 output_scale,
1903 Rectangle::from_extemities((0, offset), (size.w, size.h)),
1904 )?,
1905 (0, -offset),
1906 Relocate::Relative,
1907 ))
1908 });
1909 let after = after.into_iter().filter_map(|elem| {
1910 Some(RelocateRenderElement::from_element(
1911 CropRenderElement::from_element(
1912 elem,
1913 output_scale,
1914 Rectangle::from_extemities((0, 0), (size.w, offset)),
1915 )?,
1916 (0, -offset + size.h),
1917 Relocate::Relative,
1918 ))
1919 });
1920 before.chain(after).collect()
1921 }
1922 None => {
1923 let elements = self.workspaces[self.active_workspace_idx].render_elements(renderer);
1924 elements
1925 .into_iter()
1926 .filter_map(|elem| {
1927 Some(RelocateRenderElement::from_element(
1928 CropRenderElement::from_element(
1929 elem,
1930 output_scale,
1931 // HACK: set infinite crop bounds due to a damage tracking bug
1932 // which causes glitched rendering for maximized GTK windows.
1933 // FIXME: use proper bounds after fixing the Crop element.
1934 Rectangle::from_loc_and_size(
1935 (-i32::MAX / 2, -i32::MAX / 2),
1936 (i32::MAX, i32::MAX),
1937 ),
1938 // Rectangle::from_loc_and_size((0, 0), size),
1939 )?,
1940 (0, 0),
1941 Relocate::Relative,
1942 ))
1943 })
1944 .collect()
1945 }
1946 }
1947 }
1948}
1949
1950impl<W: LayoutElement> Workspace<W> {
1951 fn new(output: Output, options: Rc<Options>) -> Self {
1952 let working_area = layer_map_for_output(&output).non_exclusive_zone();
1953 Self {
1954 original_output: OutputId::new(&output),
1955 view_size: output_size(&output),
1956 working_area,
1957 output: Some(output),
1958 columns: vec![],
1959 active_column_idx: 0,
1960 focus_ring: FocusRing::new(options.focus_ring),
1961 view_offset: 0,
1962 view_offset_anim: None,
1963 activate_prev_column_on_removal: false,
1964 options,
1965 }
1966 }
1967
1968 fn new_no_outputs(options: Rc<Options>) -> Self {
1969 Self {
1970 output: None,
1971 original_output: OutputId(String::new()),
1972 view_size: Size::from((1280, 720)),
1973 working_area: Rectangle::from_loc_and_size((0, 0), (1280, 720)),
1974 columns: vec![],
1975 active_column_idx: 0,
1976 focus_ring: FocusRing::new(options.focus_ring),
1977 view_offset: 0,
1978 view_offset_anim: None,
1979 activate_prev_column_on_removal: false,
1980 options,
1981 }
1982 }
1983
1984 pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) {
1985 match &mut self.view_offset_anim {
1986 Some(anim) => {
1987 anim.set_current_time(current_time);
1988 self.view_offset = anim.value().round() as i32;
1989 if anim.is_done() {
1990 self.view_offset_anim = None;
1991 }
1992 }
1993 None => (),
1994 }
1995
1996 let view_pos = self.view_pos();
1997
1998 // This shall one day become a proper animation.
1999 if !self.columns.is_empty() {
2000 let col = &self.columns[self.active_column_idx];
2001 let active_win = &col.windows[col.active_window_idx];
2002 let geom = active_win.geometry();
2003 let has_ssd = active_win.has_ssd();
2004
2005 let win_pos = Point::from((
2006 self.column_x(self.active_column_idx) - view_pos,
2007 col.window_y(col.active_window_idx),
2008 ));
2009
2010 self.focus_ring.update(win_pos, geom.size, has_ssd);
2011 self.focus_ring.set_active(is_active);
2012 }
2013 }
2014
2015 pub fn are_animations_ongoing(&self) -> bool {
2016 self.view_offset_anim.is_some()
2017 }
2018
2019 fn update_config(&mut self, options: Rc<Options>) {
2020 let c = &options.focus_ring;
2021 self.focus_ring.is_off = c.off;
2022 self.focus_ring.width = c.width.into();
2023 self.focus_ring.active_color = c.active_color;
2024 self.focus_ring.inactive_color = c.inactive_color;
2025 // The focus ring buffer will be updated in a subsequent update_animations call.
2026
2027 for column in &mut self.columns {
2028 column.update_config(options.clone());
2029 }
2030
2031 self.options = options;
2032 }
2033
2034 fn windows(&self) -> impl Iterator<Item = &W> + '_ {
2035 self.columns.iter().flat_map(|col| col.windows.iter())
2036 }
2037
2038 fn set_output(&mut self, output: Option<Output>) {
2039 if self.output == output {
2040 return;
2041 }
2042
2043 if let Some(output) = self.output.take() {
2044 for win in self.windows() {
2045 win.output_leave(&output);
2046 }
2047 }
2048
2049 self.output = output;
2050
2051 if let Some(output) = &self.output {
2052 let working_area = layer_map_for_output(output).non_exclusive_zone();
2053 self.set_view_size(output_size(output), working_area);
2054
2055 for win in self.windows() {
2056 self.enter_output_for_window(win);
2057 }
2058 }
2059 }
2060
2061 fn enter_output_for_window(&self, window: &W) {
2062 if let Some(output) = &self.output {
2063 prepare_for_output(window, output);
2064
2065 // FIXME: proper overlap.
2066 window.output_enter(
2067 output,
2068 Rectangle::from_loc_and_size((0, 0), (i32::MAX, i32::MAX)),
2069 );
2070 }
2071 }
2072
2073 fn set_view_size(&mut self, size: Size<i32, Logical>, working_area: Rectangle<i32, Logical>) {
2074 if self.view_size == size && self.working_area == working_area {
2075 return;
2076 }
2077
2078 self.view_size = size;
2079 self.working_area = working_area;
2080
2081 for col in &mut self.columns {
2082 col.set_view_size(self.view_size, self.working_area);
2083 }
2084 }
2085
2086 fn toplevel_bounds(&self) -> Size<i32, Logical> {
2087 Size::from((
2088 max(self.working_area.size.w - self.options.gaps * 2, 1),
2089 max(self.working_area.size.h - self.options.gaps * 2, 1),
2090 ))
2091 }
2092
2093 pub fn configure_new_window(&self, window: &Window) {
2094 let width = if let Some(width) = self.options.default_width {
2095 max(1, width.resolve(&self.options, self.working_area.size.w))
2096 } else {
2097 0
2098 };
2099
2100 let height = self.working_area.size.h - self.options.gaps * 2;
2101 let size = Size::from((width, max(height, 1)));
2102
2103 let bounds = self.toplevel_bounds();
2104
2105 if let Some(output) = self.output.as_ref() {
2106 prepare_for_output(window, output);
2107 }
2108
2109 window.toplevel().with_pending_state(|state| {
2110 state.size = Some(size);
2111 state.bounds = Some(bounds);
2112 });
2113 }
2114
2115 fn compute_new_view_offset_for_column(&self, current_x: i32, idx: usize) -> i32 {
2116 if self.columns[idx].is_fullscreen {
2117 return 0;
2118 }
2119
2120 let new_col_x = self.column_x(idx);
2121
2122 let final_x = if let Some(anim) = &self.view_offset_anim {
2123 current_x - self.view_offset + anim.to().round() as i32
2124 } else {
2125 current_x
2126 };
2127
2128 let new_offset = compute_new_view_offset(
2129 final_x + self.working_area.loc.x,
2130 self.working_area.size.w,
2131 new_col_x,
2132 self.columns[idx].width(),
2133 self.options.gaps,
2134 );
2135
2136 // Non-fullscreen windows are always offset at least by the working area position.
2137 new_offset - self.working_area.loc.x
2138 }
2139
2140 fn animate_view_offset_to_column(&mut self, current_x: i32, idx: usize) {
2141 let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx);
2142
2143 let new_col_x = self.column_x(idx);
2144 let from_view_offset = current_x - new_col_x;
2145 self.view_offset = from_view_offset;
2146
2147 // If we're already animating towards that, don't restart it.
2148 if let Some(anim) = &self.view_offset_anim {
2149 if anim.value().round() as i32 == self.view_offset
2150 && anim.to().round() as i32 == new_view_offset
2151 {
2152 return;
2153 }
2154 }
2155
2156 // If our view offset is already this, we don't need to do anything.
2157 if self.view_offset == new_view_offset {
2158 self.view_offset_anim = None;
2159 return;
2160 }
2161
2162 self.view_offset_anim = Some(Animation::new(
2163 self.view_offset as f64,
2164 new_view_offset as f64,
2165 Duration::from_millis(250),
2166 ));
2167 }
2168
2169 fn activate_column(&mut self, idx: usize) {
2170 if self.active_column_idx == idx {
2171 return;
2172 }
2173
2174 let current_x = self.view_pos();
2175 self.animate_view_offset_to_column(current_x, idx);
2176
2177 self.active_column_idx = idx;
2178
2179 // A different column was activated; reset the flag.
2180 self.activate_prev_column_on_removal = false;
2181 }
2182
2183 fn has_windows(&self) -> bool {
2184 self.windows().next().is_some()
2185 }
2186
2187 fn has_window(&self, window: &W) -> bool {
2188 self.windows().any(|win| win == window)
2189 }
2190
2191 fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> {
2192 self.windows().find(|win| win.is_wl_surface(wl_surface))
2193 }
2194
2195 /// Computes the X position of the windows in the given column, in logical coordinates.
2196 fn column_x(&self, column_idx: usize) -> i32 {
2197 let mut x = 0;
2198
2199 for column in self.columns.iter().take(column_idx) {
2200 x += column.width() + self.options.gaps;
2201 }
2202
2203 x
2204 }
2205
2206 fn add_window(&mut self, window: W, activate: bool, width: ColumnWidth, is_full_width: bool) {
2207 self.enter_output_for_window(&window);
2208
2209 let was_empty = self.columns.is_empty();
2210
2211 let idx = if self.columns.is_empty() {
2212 0
2213 } else {
2214 self.active_column_idx + 1
2215 };
2216
2217 let column = Column::new(
2218 window,
2219 self.view_size,
2220 self.working_area,
2221 self.options.clone(),
2222 width,
2223 is_full_width,
2224 );
2225 self.columns.insert(idx, column);
2226
2227 if activate {
2228 // If this is the first window on an empty workspace, skip the animation from whatever
2229 // view_offset was left over.
2230 if was_empty {
2231 // Try to make the code produce a left-aligned offset, even in presence of left
2232 // exclusive zones.
2233 self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0);
2234 self.view_offset_anim = None;
2235 }
2236
2237 self.activate_column(idx);
2238 self.activate_prev_column_on_removal = true;
2239 }
2240 }
2241
2242 fn remove_window(&mut self, window: &W) {
2243 if let Some(output) = &self.output {
2244 window.output_leave(output);
2245 }
2246
2247 let column_idx = self
2248 .columns
2249 .iter()
2250 .position(|col| col.contains(window))
2251 .unwrap();
2252 let column = &mut self.columns[column_idx];
2253
2254 let window_idx = column.windows.iter().position(|win| win == window).unwrap();
2255 column.windows.remove(window_idx);
2256 column.heights.remove(window_idx);
2257 if column.windows.is_empty() {
2258 if column_idx + 1 == self.active_column_idx {
2259 // The previous column, that we were going to activate upon removal of the active
2260 // column, has just been itself removed.
2261 self.activate_prev_column_on_removal = false;
2262 }
2263
2264 // FIXME: activate_column below computes current view position to compute the new view
2265 // position, which can include the column we're removing here. This leads to unwanted
2266 // view jumps.
2267 self.columns.remove(column_idx);
2268 if self.columns.is_empty() {
2269 return;
2270 }
2271
2272 if self.active_column_idx > column_idx
2273 || (self.active_column_idx == column_idx && self.activate_prev_column_on_removal)
2274 {
2275 // A column to the left was removed; preserve the current position.
2276 // FIXME: preserve activate_prev_column_on_removal.
2277 // Or, the active column was removed, and we needed to activate the previous column.
2278 self.activate_column(self.active_column_idx.saturating_sub(1));
2279 } else {
2280 self.activate_column(min(self.active_column_idx, self.columns.len() - 1));
2281 }
2282
2283 return;
2284 }
2285
2286 column.active_window_idx = min(column.active_window_idx, column.windows.len() - 1);
2287 column.update_window_sizes();
2288 }
2289
2290 fn update_window(&mut self, window: &W) {
2291 let (idx, column) = self
2292 .columns
2293 .iter_mut()
2294 .enumerate()
2295 .find(|(_, col)| col.contains(window))
2296 .unwrap();
2297 column.update_window_sizes();
2298
2299 if idx == self.active_column_idx {
2300 // We might need to move the view to ensure the resized window is still visible.
2301 let current_x = self.view_pos();
2302 self.animate_view_offset_to_column(current_x, idx);
2303 }
2304 }
2305
2306 fn activate_window(&mut self, window: &W) {
2307 let column_idx = self
2308 .columns
2309 .iter()
2310 .position(|col| col.contains(window))
2311 .unwrap();
2312 let column = &mut self.columns[column_idx];
2313
2314 column.activate_window(window);
2315 self.activate_column(column_idx);
2316 }
2317
2318 #[cfg(test)]
2319 fn verify_invariants(&self) {
2320 assert!(self.view_size.w > 0);
2321 assert!(self.view_size.h > 0);
2322
2323 if !self.columns.is_empty() {
2324 assert!(self.active_column_idx < self.columns.len());
2325
2326 for column in &self.columns {
2327 column.verify_invariants();
2328 }
2329 }
2330 }
2331
2332 fn focus_left(&mut self) {
2333 self.activate_column(self.active_column_idx.saturating_sub(1));
2334 }
2335
2336 fn focus_right(&mut self) {
2337 if self.columns.is_empty() {
2338 return;
2339 }
2340
2341 self.activate_column(min(self.active_column_idx + 1, self.columns.len() - 1));
2342 }
2343
2344 fn focus_down(&mut self) {
2345 if self.columns.is_empty() {
2346 return;
2347 }
2348
2349 self.columns[self.active_column_idx].focus_down();
2350 }
2351
2352 fn focus_up(&mut self) {
2353 if self.columns.is_empty() {
2354 return;
2355 }
2356
2357 self.columns[self.active_column_idx].focus_up();
2358 }
2359
2360 fn move_left(&mut self) {
2361 let new_idx = self.active_column_idx.saturating_sub(1);
2362 if self.active_column_idx == new_idx {
2363 return;
2364 }
2365
2366 let current_x = self.view_pos();
2367
2368 self.columns.swap(self.active_column_idx, new_idx);
2369
2370 self.view_offset =
2371 self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
2372
2373 self.activate_column(new_idx);
2374 }
2375
2376 fn move_right(&mut self) {
2377 if self.columns.is_empty() {
2378 return;
2379 }
2380
2381 let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1);
2382 if self.active_column_idx == new_idx {
2383 return;
2384 }
2385
2386 let current_x = self.view_pos();
2387
2388 self.columns.swap(self.active_column_idx, new_idx);
2389
2390 self.view_offset =
2391 self.compute_new_view_offset_for_column(current_x, self.active_column_idx);
2392
2393 self.activate_column(new_idx);
2394 }
2395
2396 fn move_down(&mut self) {
2397 if self.columns.is_empty() {
2398 return;
2399 }
2400
2401 self.columns[self.active_column_idx].move_down();
2402 }
2403
2404 fn move_up(&mut self) {
2405 if self.columns.is_empty() {
2406 return;
2407 }
2408
2409 self.columns[self.active_column_idx].move_up();
2410 }
2411
2412 fn consume_into_column(&mut self) {
2413 if self.columns.len() < 2 {
2414 return;
2415 }
2416
2417 if self.active_column_idx == self.columns.len() - 1 {
2418 return;
2419 }
2420
2421 let source_column_idx = self.active_column_idx + 1;
2422
2423 let source_column = &mut self.columns[source_column_idx];
2424 let window = source_column.windows[0].clone();
2425 self.remove_window(&window);
2426
2427 let target_column = &mut self.columns[self.active_column_idx];
2428 target_column.add_window(window);
2429 }
2430
2431 fn expel_from_column(&mut self) {
2432 if self.columns.is_empty() {
2433 return;
2434 }
2435
2436 let source_column = &mut self.columns[self.active_column_idx];
2437 if source_column.windows.len() == 1 {
2438 return;
2439 }
2440
2441 let width = source_column.width;
2442 let is_full_width = source_column.is_full_width;
2443 let window = source_column.windows[source_column.active_window_idx].clone();
2444 self.remove_window(&window);
2445
2446 self.add_window(window, true, width, is_full_width);
2447 }
2448
2449 fn center_column(&mut self) {
2450 if self.columns.is_empty() {
2451 return;
2452 }
2453
2454 let col = &self.columns[self.active_column_idx];
2455 if col.is_fullscreen {
2456 return;
2457 }
2458
2459 let width = col.width();
2460
2461 // If the column is wider than the working area, then on commit it will be shifted to left
2462 // edge alignment by the usual positioning code, so there's no use in doing anything here.
2463 if self.working_area.size.w <= width {
2464 return;
2465 }
2466
2467 let new_view_offset = -(self.working_area.size.w - width) / 2 - self.working_area.loc.x;
2468
2469 // If we're already animating towards that, don't restart it.
2470 if let Some(anim) = &self.view_offset_anim {
2471 if anim.to().round() as i32 == new_view_offset {
2472 return;
2473 }
2474 }
2475
2476 // If our view offset is already this, we don't need to do anything.
2477 if self.view_offset == new_view_offset {
2478 return;
2479 }
2480
2481 self.view_offset_anim = Some(Animation::new(
2482 self.view_offset as f64,
2483 new_view_offset as f64,
2484 Duration::from_millis(250),
2485 ));
2486 }
2487
2488 fn view_pos(&self) -> i32 {
2489 self.column_x(self.active_column_idx) + self.view_offset
2490 }
2491
2492 fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, Point<i32, Logical>)> {
2493 if self.columns.is_empty() {
2494 return None;
2495 }
2496
2497 let view_pos = self.view_pos();
2498
2499 // Prefer the active window since it's drawn on top.
2500 let col = &self.columns[self.active_column_idx];
2501 let active_win = &col.windows[col.active_window_idx];
2502 let geom = active_win.geometry();
2503 let buf_pos = Point::from((
2504 self.column_x(self.active_column_idx) - view_pos,
2505 col.window_y(col.active_window_idx),
2506 )) - geom.loc;
2507 if active_win.is_in_input_region(&(pos - buf_pos.to_f64())) {
2508 return Some((active_win, buf_pos));
2509 }
2510
2511 let mut x = -view_pos;
2512 for col in &self.columns {
2513 for (win, y) in zip(&col.windows, col.window_ys()) {
2514 if win == active_win {
2515 // Already handled it above.
2516 continue;
2517 }
2518
2519 let geom = win.geometry();
2520 let buf_pos = Point::from((x, y)) - geom.loc;
2521 if win.is_in_input_region(&(pos - buf_pos.to_f64())) {
2522 return Some((win, buf_pos));
2523 }
2524 }
2525
2526 x += col.width() + self.options.gaps;
2527 }
2528
2529 None
2530 }
2531
2532 fn toggle_width(&mut self) {
2533 if self.columns.is_empty() {
2534 return;
2535 }
2536
2537 self.columns[self.active_column_idx].toggle_width();
2538 }
2539
2540 fn toggle_full_width(&mut self) {
2541 if self.columns.is_empty() {
2542 return;
2543 }
2544
2545 self.columns[self.active_column_idx].toggle_full_width();
2546 }
2547
2548 fn set_column_width(&mut self, change: SizeChange) {
2549 if self.columns.is_empty() {
2550 return;
2551 }
2552
2553 self.columns[self.active_column_idx].set_column_width(change);
2554 }
2555
2556 fn set_window_height(&mut self, change: SizeChange) {
2557 if self.columns.is_empty() {
2558 return;
2559 }
2560
2561 self.columns[self.active_column_idx].set_window_height(change);
2562 }
2563
2564 pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) {
2565 let (mut col_idx, win_idx) = self
2566 .columns
2567 .iter()
2568 .enumerate()
2569 .find_map(|(col_idx, col)| {
2570 col.windows
2571 .iter()
2572 .position(|w| w == window)
2573 .map(|win_idx| (col_idx, win_idx))
2574 })
2575 .unwrap();
2576
2577 let mut col = &mut self.columns[col_idx];
2578
2579 if is_fullscreen && col.windows.len() > 1 {
2580 // This wasn't the only window in its column; extract it into a separate column.
2581 let target_window_was_focused =
2582 self.active_column_idx == col_idx && col.active_window_idx == win_idx;
2583 let window = col.windows.remove(win_idx);
2584 col.heights.remove(win_idx);
2585 col.active_window_idx = min(col.active_window_idx, col.windows.len() - 1);
2586 col.update_window_sizes();
2587 let width = col.width;
2588 let is_full_width = col.is_full_width;
2589
2590 col_idx += 1;
2591 self.columns.insert(
2592 col_idx,
2593 Column::new(
2594 window,
2595 self.view_size,
2596 self.working_area,
2597 self.options.clone(),
2598 width,
2599 is_full_width,
2600 ),
2601 );
2602 if self.active_column_idx >= col_idx || target_window_was_focused {
2603 self.active_column_idx += 1;
2604 }
2605 col = &mut self.columns[col_idx];
2606 }
2607
2608 col.set_fullscreen(is_fullscreen);
2609 }
2610
2611 pub fn toggle_fullscreen(&mut self, window: &W) {
2612 let col = self
2613 .columns
2614 .iter_mut()
2615 .find(|col| col.windows.contains(window))
2616 .unwrap();
2617 let value = !col.is_fullscreen;
2618 self.set_fullscreen(window, value);
2619 }
2620
2621 pub fn render_above_top_layer(&self) -> bool {
2622 // Render above the top layer if we're on a fullscreen window and the view is stationary.
2623 if self.columns.is_empty() {
2624 return false;
2625 }
2626
2627 if self.view_offset_anim.is_some() {
2628 return false;
2629 }
2630
2631 self.columns[self.active_column_idx].is_fullscreen
2632 }
2633}
2634
2635impl Workspace<Window> {
2636 fn refresh(&self) {
2637 let bounds = self.toplevel_bounds();
2638
2639 for (col_idx, col) in self.columns.iter().enumerate() {
2640 for (win_idx, win) in col.windows.iter().enumerate() {
2641 let active = self.active_column_idx == col_idx && col.active_window_idx == win_idx;
2642 win.set_activated(active);
2643
2644 win.toplevel().with_pending_state(|state| {
2645 state.bounds = Some(bounds);
2646 });
2647
2648 win.toplevel().send_pending_configure();
2649 win.refresh();
2650 }
2651 }
2652 }
2653
2654 pub fn render_elements(
2655 &self,
2656 renderer: &mut GlesRenderer,
2657 ) -> Vec<WorkspaceRenderElement<GlesRenderer>> {
2658 if self.columns.is_empty() {
2659 return vec![];
2660 }
2661
2662 // FIXME: workspaces should probably cache their last used scale so they can be correctly
2663 // rendered even with no outputs connected.
2664 let output_scale = self
2665 .output
2666 .as_ref()
2667 .map(|o| Scale::from(o.current_scale().fractional_scale()))
2668 .unwrap_or(Scale::from(1.));
2669
2670 let mut rv = vec![];
2671 let view_pos = self.view_pos();
2672
2673 // Draw the active window on top.
2674 let col = &self.columns[self.active_column_idx];
2675 let active_win = &col.windows[col.active_window_idx];
2676 let win_pos = Point::from((
2677 self.column_x(self.active_column_idx) - view_pos,
2678 col.window_y(col.active_window_idx),
2679 ));
2680
2681 // Draw the window itself.
2682 let geom = active_win.geometry();
2683 let buf_pos = win_pos - geom.loc;
2684 rv.extend(active_win.render_elements(
2685 renderer,
2686 buf_pos.to_physical_precise_round(output_scale),
2687 output_scale,
2688 1.,
2689 ));
2690
2691 // Draw the focus ring.
2692 rv.extend(self.focus_ring.render(output_scale).map(Into::into));
2693
2694 let mut x = -view_pos;
2695 for col in &self.columns {
2696 for (win, y) in zip(&col.windows, col.window_ys()) {
2697 if win == active_win {
2698 // Already handled it above.
2699 continue;
2700 }
2701
2702 let geom = win.geometry();
2703 let buf_pos = Point::from((x, y)) - geom.loc;
2704 rv.extend(win.render_elements(
2705 renderer,
2706 buf_pos.to_physical_precise_round(output_scale),
2707 output_scale,
2708 1.,
2709 ));
2710 }
2711
2712 x += col.width() + self.options.gaps;
2713 }
2714
2715 rv
2716 }
2717}
2718
2719impl<W: LayoutElement> Column<W> {
2720 fn new(
2721 window: W,
2722 view_size: Size<i32, Logical>,
2723 working_area: Rectangle<i32, Logical>,
2724 options: Rc<Options>,
2725 width: ColumnWidth,
2726 is_full_width: bool,
2727 ) -> Self {
2728 let mut rv = Self {
2729 windows: vec![],
2730 heights: vec![],
2731 active_window_idx: 0,
2732 width,
2733 is_full_width,
2734 is_fullscreen: false,
2735 view_size,
2736 working_area,
2737 options,
2738 };
2739
2740 rv.add_window(window);
2741
2742 rv
2743 }
2744
2745 fn set_view_size(&mut self, size: Size<i32, Logical>, working_area: Rectangle<i32, Logical>) {
2746 if self.view_size == size && self.working_area == working_area {
2747 return;
2748 }
2749
2750 self.view_size = size;
2751 self.working_area = working_area;
2752
2753 self.update_window_sizes();
2754 }
2755
2756 fn update_config(&mut self, options: Rc<Options>) {
2757 let mut update_sizes = false;
2758
2759 // If preset widths changed, make our width non-preset.
2760 if self.options.preset_widths != options.preset_widths {
2761 if let ColumnWidth::Preset(idx) = self.width {
2762 self.width = self.options.preset_widths[idx];
2763 }
2764 }
2765
2766 if self.options.gaps != options.gaps {
2767 update_sizes = true;
2768 }
2769
2770 self.options = options;
2771
2772 if update_sizes {
2773 self.update_window_sizes();
2774 }
2775 }
2776
2777 fn set_width(&mut self, width: ColumnWidth) {
2778 self.width = width;
2779 self.is_full_width = false;
2780 self.update_window_sizes();
2781 }
2782
2783 fn contains(&self, window: &W) -> bool {
2784 self.windows.iter().any(|win| win == window)
2785 }
2786
2787 fn activate_window(&mut self, window: &W) {
2788 let idx = self.windows.iter().position(|win| win == window).unwrap();
2789 self.active_window_idx = idx;
2790 }
2791
2792 fn add_window(&mut self, window: W) {
2793 self.is_fullscreen = false;
2794 self.windows.push(window);
2795 self.heights.push(WindowHeight::Auto);
2796 self.update_window_sizes();
2797 }
2798
2799 fn update_window_sizes(&mut self) {
2800 if self.is_fullscreen {
2801 self.windows[0].request_fullscreen(self.view_size);
2802 return;
2803 }
2804
2805 let min_size: Vec<_> = self.windows.iter().map(LayoutElement::min_size).collect();
2806 let max_size: Vec<_> = self.windows.iter().map(LayoutElement::max_size).collect();
2807
2808 // Compute the column width.
2809 let min_width = min_size
2810 .iter()
2811 .filter_map(|size| {
2812 let w = size.w;
2813 if w == 0 {
2814 None
2815 } else {
2816 Some(w)
2817 }
2818 })
2819 .max()
2820 .unwrap_or(1);
2821 let max_width = max_size
2822 .iter()
2823 .filter_map(|size| {
2824 let w = size.w;
2825 if w == 0 {
2826 None
2827 } else {
2828 Some(w)
2829 }
2830 })
2831 .min()
2832 .unwrap_or(i32::MAX);
2833 let max_width = max(max_width, min_width);
2834
2835 let width = if self.is_full_width {
2836 ColumnWidth::Proportion(1.)
2837 } else {
2838 self.width
2839 };
2840
2841 let width = width.resolve(&self.options, self.working_area.size.w);
2842 let width = max(min(width, max_width), min_width);
2843
2844 // Compute the window heights.
2845 let mut heights = self.heights.clone();
2846 let mut height_left = self.working_area.size.h - self.options.gaps;
2847 let mut auto_windows_left = self.windows.len();
2848
2849 // Subtract all fixed-height windows.
2850 for (h, (min_size, max_size)) in zip(&mut heights, zip(&min_size, &max_size)) {
2851 // Check if the window has an exact height constraint.
2852 if min_size.h > 0 && min_size.h == max_size.h {
2853 *h = WindowHeight::Fixed(min_size.h);
2854 }
2855
2856 if let WindowHeight::Fixed(h) = h {
2857 if max_size.h > 0 {
2858 *h = min(*h, max_size.h);
2859 }
2860 if min_size.h > 0 {
2861 *h = max(*h, min_size.h);
2862 }
2863 *h = max(*h, 1);
2864
2865 height_left -= *h + self.options.gaps;
2866 auto_windows_left -= 1;
2867 }
2868 }
2869
2870 // Iteratively try to distribute the remaining height, checking against window min heights.
2871 // Pick an auto height according to the current sizes, then check if it satisfies all
2872 // remaining min heights. If not, allocate fixed height to those windows and repeat the
2873 // loop. On each iteration the auto height will get smaller.
2874 //
2875 // NOTE: we do not respect max height here. Doing so would complicate things: if the current
2876 // auto height is above some window's max height, then the auto height can become larger.
2877 // Combining this with the min height loop is where the complexity appears.
2878 //
2879 // However, most max height uses are for fixed-size dialogs, where min height == max_height.
2880 // This case is separately handled above.
2881 while auto_windows_left > 0 {
2882 // Compute the current auto height.
2883 let auto_height = height_left / auto_windows_left as i32 - self.options.gaps;
2884 let auto_height = max(auto_height, 1);
2885
2886 // Integer division above can result in imperfect height distribution. We will make some
2887 // windows 1 px taller to account for this.
2888 let mut ones_left = height_left
2889 .saturating_sub((auto_height + self.options.gaps) * auto_windows_left as i32);
2890
2891 let mut unsatisfied_min = false;
2892 let mut ones_left_2 = ones_left;
2893 for (h, min_size) in zip(&mut heights, &min_size) {
2894 if matches!(h, WindowHeight::Fixed(_)) {
2895 continue;
2896 }
2897
2898 let mut auto = auto_height;
2899 if ones_left_2 > 0 {
2900 auto += 1;
2901 ones_left_2 -= 1;
2902 }
2903
2904 // Check if the auto height satisfies the min height.
2905 if min_size.h > 0 && min_size.h > auto {
2906 *h = WindowHeight::Fixed(min_size.h);
2907 height_left -= min_size.h + self.options.gaps;
2908 auto_windows_left -= 1;
2909 unsatisfied_min = true;
2910 }
2911 }
2912
2913 // If some min height was unsatisfied, then we allocated the window more than the auto
2914 // height, which means that the remaining auto windows now have less height to work
2915 // with, and the loop must run again.
2916 if unsatisfied_min {
2917 continue;
2918 }
2919
2920 // All min heights were satisfied, fill them in.
2921 for h in &mut heights {
2922 if matches!(h, WindowHeight::Fixed(_)) {
2923 continue;
2924 }
2925
2926 let mut auto = auto_height;
2927 if ones_left > 0 {
2928 auto += 1;
2929 ones_left -= 1;
2930 }
2931
2932 *h = WindowHeight::Fixed(auto);
2933 auto_windows_left -= 1;
2934 }
2935
2936 assert_eq!(auto_windows_left, 0);
2937 }
2938
2939 for (win, h) in zip(&self.windows, heights) {
2940 let WindowHeight::Fixed(height) = h else {
2941 unreachable!()
2942 };
2943
2944 let size = Size::from((width, height));
2945 win.request_size(size);
2946 }
2947 }
2948
2949 fn width(&self) -> i32 {
2950 self.windows
2951 .iter()
2952 .map(|win| win.geometry().size.w)
2953 .max()
2954 .unwrap()
2955 }
2956
2957 fn focus_up(&mut self) {
2958 self.active_window_idx = self.active_window_idx.saturating_sub(1);
2959 }
2960
2961 fn focus_down(&mut self) {
2962 self.active_window_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
2963 }
2964
2965 fn move_up(&mut self) {
2966 let new_idx = self.active_window_idx.saturating_sub(1);
2967 if self.active_window_idx == new_idx {
2968 return;
2969 }
2970
2971 self.windows.swap(self.active_window_idx, new_idx);
2972 self.heights.swap(self.active_window_idx, new_idx);
2973 self.active_window_idx = new_idx;
2974 }
2975
2976 fn move_down(&mut self) {
2977 let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1);
2978 if self.active_window_idx == new_idx {
2979 return;
2980 }
2981
2982 self.windows.swap(self.active_window_idx, new_idx);
2983 self.heights.swap(self.active_window_idx, new_idx);
2984 self.active_window_idx = new_idx;
2985 }
2986
2987 #[cfg(test)]
2988 fn verify_invariants(&self) {
2989 assert!(!self.windows.is_empty(), "columns can't be empty");
2990 assert!(self.active_window_idx < self.windows.len());
2991 assert_eq!(self.windows.len(), self.heights.len());
2992
2993 if self.is_fullscreen {
2994 assert_eq!(self.windows.len(), 1);
2995 }
2996 }
2997
2998 fn toggle_width(&mut self) {
2999 let width = if self.is_full_width {
3000 ColumnWidth::Proportion(1.)
3001 } else {
3002 self.width
3003 };
3004
3005 let idx = match width {
3006 ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(),
3007 _ => {
3008 let current = self.width();
3009 self.options
3010 .preset_widths
3011 .iter()
3012 .position(|prop| {
3013 prop.resolve(&self.options, self.working_area.size.w) > current
3014 })
3015 .unwrap_or(0)
3016 }
3017 };
3018 let width = ColumnWidth::Preset(idx);
3019 self.set_width(width);
3020 }
3021
3022 fn toggle_full_width(&mut self) {
3023 self.is_full_width = !self.is_full_width;
3024 self.update_window_sizes();
3025 }
3026
3027 fn set_column_width(&mut self, change: SizeChange) {
3028 let width = if self.is_full_width {
3029 ColumnWidth::Proportion(1.)
3030 } else {
3031 self.width
3032 };
3033
3034 let current_px = width.resolve(&self.options, self.working_area.size.w);
3035
3036 let current = match width {
3037 ColumnWidth::Preset(idx) => self.options.preset_widths[idx],
3038 current => current,
3039 };
3040
3041 // FIXME: fix overflows then remove limits.
3042 const MAX_PX: i32 = 100000;
3043 const MAX_F: f64 = 10000.;
3044
3045 let width = match (current, change) {
3046 (_, SizeChange::SetFixed(fixed)) => ColumnWidth::Fixed(fixed.clamp(1, MAX_PX)),
3047 (_, SizeChange::SetProportion(proportion)) => {
3048 ColumnWidth::Proportion((proportion / 100.).clamp(0., MAX_F))
3049 }
3050 (_, SizeChange::AdjustFixed(delta)) => {
3051 let width = current_px.saturating_add(delta).clamp(1, MAX_PX);
3052 ColumnWidth::Fixed(width)
3053 }
3054 (ColumnWidth::Proportion(current), SizeChange::AdjustProportion(delta)) => {
3055 let proportion = (current + delta / 100.).clamp(0., MAX_F);
3056 ColumnWidth::Proportion(proportion)
3057 }
3058 (ColumnWidth::Fixed(_), SizeChange::AdjustProportion(delta)) => {
3059 let current = (current_px + self.options.gaps) as f64
3060 / (self.working_area.size.w - self.options.gaps) as f64;
3061 let proportion = (current + delta / 100.).clamp(0., MAX_F);
3062 ColumnWidth::Proportion(proportion)
3063 }
3064 (ColumnWidth::Preset(_), _) => unreachable!(),
3065 };
3066
3067 self.set_width(width);
3068 }
3069
3070 fn set_window_height(&mut self, change: SizeChange) {
3071 let current = self.heights[self.active_window_idx];
3072 let current_px = match current {
3073 WindowHeight::Auto => self.windows[self.active_window_idx].geometry().size.h,
3074 WindowHeight::Fixed(height) => height,
3075 };
3076 let current_prop = (current_px + self.options.gaps) as f64
3077 / (self.working_area.size.h - self.options.gaps) as f64;
3078
3079 // FIXME: fix overflows then remove limits.
3080 const MAX_PX: i32 = 100000;
3081
3082 let mut height = match change {
3083 SizeChange::SetFixed(fixed) => fixed,
3084 SizeChange::SetProportion(proportion) => {
3085 ((self.working_area.size.h - self.options.gaps) as f64 * proportion
3086 - self.options.gaps as f64)
3087 .round() as i32
3088 }
3089 SizeChange::AdjustFixed(delta) => current_px.saturating_add(delta),
3090 SizeChange::AdjustProportion(delta) => {
3091 let proportion = current_prop + delta / 100.;
3092 ((self.working_area.size.h - self.options.gaps) as f64 * proportion
3093 - self.options.gaps as f64)
3094 .round() as i32
3095 }
3096 };
3097
3098 // Clamp it against the window height constraints.
3099 let win = &self.windows[self.active_window_idx];
3100 let min_h = win.min_size().h;
3101 let max_h = win.max_size().h;
3102
3103 if max_h > 0 {
3104 height = height.min(max_h);
3105 }
3106 if min_h > 0 {
3107 height = height.max(min_h);
3108 }
3109
3110 self.heights[self.active_window_idx] = WindowHeight::Fixed(height.clamp(1, MAX_PX));
3111 self.update_window_sizes();
3112 }
3113
3114 fn set_fullscreen(&mut self, is_fullscreen: bool) {
3115 assert_eq!(self.windows.len(), 1);
3116 self.is_fullscreen = is_fullscreen;
3117 self.update_window_sizes();
3118 }
3119
3120 fn window_y(&self, window_idx: usize) -> i32 {
3121 self.window_ys().nth(window_idx).unwrap()
3122 }
3123
3124 fn window_ys(&self) -> impl Iterator<Item = i32> + '_ {
3125 let mut y = 0;
3126
3127 if !self.is_fullscreen {
3128 y = self.working_area.loc.y + self.options.gaps;
3129 }
3130
3131 self.windows.iter().map(move |win| {
3132 let pos = y;
3133 y += win.geometry().size.h + self.options.gaps;
3134 pos
3135 })
3136 }
3137}
3138
3139pub fn output_size(output: &Output) -> Size<i32, Logical> {
3140 let output_scale = output.current_scale().integer_scale();
3141 let output_transform = output.current_transform();
3142 let output_mode = output.current_mode().unwrap();
3143
3144 output_transform
3145 .transform_size(output_mode.size)
3146 .to_logical(output_scale)
3147}
3148
3149fn compute_new_view_offset(
3150 cur_x: i32,
3151 view_width: i32,
3152 new_col_x: i32,
3153 new_col_width: i32,
3154 gaps: i32,
3155) -> i32 {
3156 // If the column is wider than the view, always left-align it.
3157 if view_width <= new_col_width {
3158 return 0;
3159 }
3160
3161 // Compute the padding in case it needs to be smaller due to large window width.
3162 let padding = ((view_width - new_col_width) / 2).clamp(0, gaps);
3163
3164 // Compute the desired new X with padding.
3165 let new_x = new_col_x - padding;
3166 let new_right_x = new_col_x + new_col_width + padding;
3167
3168 // If the column is already fully visible, leave the view as is.
3169 if cur_x <= new_x && new_right_x <= cur_x + view_width {
3170 return -(new_col_x - cur_x);
3171 }
3172
3173 // Otherwise, prefer the aligment that results in less motion from the current position.
3174 let dist_to_left = cur_x.abs_diff(new_x);
3175 let dist_to_right = (cur_x + view_width).abs_diff(new_right_x);
3176 if dist_to_left <= dist_to_right {
3177 -padding
3178 } else {
3179 -(view_width - padding - new_col_width)
3180 }
3181}
3182
3183fn prepare_for_output(window: &impl LayoutElement, output: &Output) {
3184 let scale = output.current_scale().integer_scale();
3185 let transform = output.current_transform();
3186 window.set_preferred_scale_transform(scale, transform);
3187}
3188
3189#[cfg(test)]
3190mod tests {
3191 use std::cell::Cell;
3192 use std::rc::Rc;
3193
3194 use proptest::prelude::*;
3195 use proptest_derive::Arbitrary;
3196 use smithay::output::{Mode, PhysicalProperties, Subpixel};
3197 use smithay::utils::IsAlive;
3198
3199 use super::*;
3200
3201 impl<W: LayoutElement> Default for Layout<W> {
3202 fn default() -> Self {
3203 Self {
3204 monitor_set: MonitorSet::NoOutputs { workspaces: vec![] },
3205 options: Rc::new(Options::default()),
3206 }
3207 }
3208 }
3209
3210 #[derive(Debug)]
3211 struct TestWindowInner {
3212 id: usize,
3213 bbox: Cell<Rectangle<i32, Logical>>,
3214 initial_bbox: Rectangle<i32, Logical>,
3215 requested_size: Cell<Option<Size<i32, Logical>>>,
3216 }
3217
3218 #[derive(Debug, Clone)]
3219 struct TestWindow(Rc<TestWindowInner>);
3220
3221 impl TestWindow {
3222 fn new(id: usize, bbox: Rectangle<i32, Logical>) -> Self {
3223 Self(Rc::new(TestWindowInner {
3224 id,
3225 bbox: Cell::new(bbox),
3226 initial_bbox: bbox,
3227 requested_size: Cell::new(None),
3228 }))
3229 }
3230
3231 fn communicate(&self) -> bool {
3232 if let Some(size) = self.0.requested_size.take() {
3233 assert!(size.w >= 0);
3234 assert!(size.h >= 0);
3235
3236 let mut new_bbox = self.0.initial_bbox;
3237 if size.w != 0 {
3238 new_bbox.size.w = size.w;
3239 }
3240 if size.h != 0 {
3241 new_bbox.size.h = size.h;
3242 }
3243
3244 if self.0.bbox.get() != new_bbox {
3245 self.0.bbox.set(new_bbox);
3246 return true;
3247 }
3248 }
3249
3250 false
3251 }
3252 }
3253
3254 impl PartialEq for TestWindow {
3255 fn eq(&self, other: &Self) -> bool {
3256 self.0.id == other.0.id
3257 }
3258 }
3259
3260 impl IsAlive for TestWindow {
3261 fn alive(&self) -> bool {
3262 true
3263 }
3264 }
3265
3266 impl SpaceElement for TestWindow {
3267 fn bbox(&self) -> Rectangle<i32, Logical> {
3268 self.0.bbox.get()
3269 }
3270
3271 fn is_in_input_region(&self, _point: &Point<f64, Logical>) -> bool {
3272 false
3273 }
3274
3275 fn set_activate(&self, _activated: bool) {}
3276 fn output_enter(&self, _output: &Output, _overlap: Rectangle<i32, Logical>) {}
3277 fn output_leave(&self, _output: &Output) {}
3278 }
3279
3280 impl LayoutElement for TestWindow {
3281 fn request_size(&self, size: Size<i32, Logical>) {
3282 self.0.requested_size.set(Some(size));
3283 }
3284
3285 fn request_fullscreen(&self, _size: Size<i32, Logical>) {}
3286
3287 fn min_size(&self) -> Size<i32, Logical> {
3288 Size::from((0, 0))
3289 }
3290
3291 fn max_size(&self) -> Size<i32, Logical> {
3292 Size::from((0, 0))
3293 }
3294
3295 fn is_wl_surface(&self, _wl_surface: &WlSurface) -> bool {
3296 false
3297 }
3298
3299 fn set_preferred_scale_transform(&self, _scale: i32, _transform: Transform) {}
3300
3301 fn has_ssd(&self) -> bool {
3302 false
3303 }
3304 }
3305
3306 fn arbitrary_bbox() -> impl Strategy<Value = Rectangle<i32, Logical>> {
3307 any::<(i16, i16, u16, u16)>().prop_map(|(x, y, w, h)| {
3308 let loc: Point<i32, _> = Point::from((x.into(), y.into()));
3309 let size: Size<i32, _> = Size::from((w.into(), h.into()));
3310 Rectangle::from_loc_and_size(loc, size)
3311 })
3312 }
3313
3314 fn arbitrary_size_change() -> impl Strategy<Value = SizeChange> {
3315 prop_oneof![
3316 (0..).prop_map(SizeChange::SetFixed),
3317 (0f64..).prop_map(SizeChange::SetProportion),
3318 any::<i32>().prop_map(SizeChange::AdjustFixed),
3319 any::<f64>().prop_map(SizeChange::AdjustProportion),
3320 ]
3321 }
3322
3323 #[derive(Debug, Clone, Copy, Arbitrary)]
3324 enum Op {
3325 AddOutput(#[proptest(strategy = "1..=5usize")] usize),
3326 RemoveOutput(#[proptest(strategy = "1..=5usize")] usize),
3327 FocusOutput(#[proptest(strategy = "1..=5usize")] usize),
3328 AddWindow {
3329 #[proptest(strategy = "1..=5usize")]
3330 id: usize,
3331 #[proptest(strategy = "arbitrary_bbox()")]
3332 bbox: Rectangle<i32, Logical>,
3333 },
3334 CloseWindow(#[proptest(strategy = "1..=5usize")] usize),
3335 FullscreenWindow(#[proptest(strategy = "1..=5usize")] usize),
3336 FocusColumnLeft,
3337 FocusColumnRight,
3338 FocusWindowDown,
3339 FocusWindowUp,
3340 MoveColumnLeft,
3341 MoveColumnRight,
3342 MoveWindowDown,
3343 MoveWindowUp,
3344 ConsumeWindowIntoColumn,
3345 ExpelWindowFromColumn,
3346 CenterColumn,
3347 FocusWorkspaceDown,
3348 FocusWorkspaceUp,
3349 FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize),
3350 MoveWindowToWorkspaceDown,
3351 MoveWindowToWorkspaceUp,
3352 MoveWindowToWorkspace(#[proptest(strategy = "0..=4usize")] usize),
3353 MoveWorkspaceDown,
3354 MoveWorkspaceUp,
3355 MoveWindowToOutput(#[proptest(strategy = "1..=5u8")] u8),
3356 SwitchPresetColumnWidth,
3357 MaximizeColumn,
3358 SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
3359 SetWindowHeight(#[proptest(strategy = "arbitrary_size_change()")] SizeChange),
3360 Communicate(#[proptest(strategy = "1..=5usize")] usize),
3361 }
3362
3363 impl Op {
3364 fn apply(self, layout: &mut Layout<TestWindow>) {
3365 match self {
3366 Op::AddOutput(id) => {
3367 let name = format!("output{id}");
3368 if layout.outputs().any(|o| o.name() == name) {
3369 return;
3370 }
3371
3372 let output = Output::new(
3373 name,
3374 PhysicalProperties {
3375 size: Size::from((1280, 720)),
3376 subpixel: Subpixel::Unknown,
3377 make: String::new(),
3378 model: String::new(),
3379 },
3380 );
3381 output.change_current_state(
3382 Some(Mode {
3383 size: Size::from((1280, 720)),
3384 refresh: 60,
3385 }),
3386 None,
3387 None,
3388 None,
3389 );
3390 layout.add_output(output.clone());
3391 }
3392 Op::RemoveOutput(id) => {
3393 let name = format!("output{id}");
3394 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
3395 return;
3396 };
3397
3398 layout.remove_output(&output);
3399 }
3400 Op::FocusOutput(id) => {
3401 let name = format!("output{id}");
3402 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
3403 return;
3404 };
3405
3406 layout.focus_output(&output);
3407 }
3408 Op::AddWindow { id, bbox } => {
3409 match &mut layout.monitor_set {
3410 MonitorSet::Normal { monitors, .. } => {
3411 for mon in monitors {
3412 for ws in &mut mon.workspaces {
3413 for win in ws.windows() {
3414 if win.0.id == id {
3415 return;
3416 }
3417 }
3418 }
3419 }
3420 }
3421 MonitorSet::NoOutputs { workspaces, .. } => {
3422 for ws in workspaces {
3423 for win in ws.windows() {
3424 if win.0.id == id {
3425 return;
3426 }
3427 }
3428 }
3429 }
3430 }
3431
3432 let win = TestWindow::new(id, bbox);
3433 layout.add_window(win, None, false);
3434 }
3435 Op::CloseWindow(id) => {
3436 let dummy = TestWindow::new(id, Rectangle::default());
3437 layout.remove_window(&dummy);
3438 }
3439 Op::FullscreenWindow(id) => {
3440 let dummy = TestWindow::new(id, Rectangle::default());
3441 layout.toggle_fullscreen(&dummy);
3442 }
3443 Op::FocusColumnLeft => layout.focus_left(),
3444 Op::FocusColumnRight => layout.focus_right(),
3445 Op::FocusWindowDown => layout.focus_down(),
3446 Op::FocusWindowUp => layout.focus_up(),
3447 Op::MoveColumnLeft => layout.move_left(),
3448 Op::MoveColumnRight => layout.move_right(),
3449 Op::MoveWindowDown => layout.move_down(),
3450 Op::MoveWindowUp => layout.move_up(),
3451 Op::ConsumeWindowIntoColumn => layout.consume_into_column(),
3452 Op::ExpelWindowFromColumn => layout.expel_from_column(),
3453 Op::CenterColumn => layout.center_column(),
3454 Op::FocusWorkspaceDown => layout.switch_workspace_down(),
3455 Op::FocusWorkspaceUp => layout.switch_workspace_up(),
3456 Op::FocusWorkspace(idx) => layout.switch_workspace(idx),
3457 Op::MoveWindowToWorkspaceDown => layout.move_to_workspace_down(),
3458 Op::MoveWindowToWorkspaceUp => layout.move_to_workspace_up(),
3459 Op::MoveWindowToWorkspace(idx) => layout.move_to_workspace(idx),
3460 Op::MoveWindowToOutput(id) => {
3461 let name = format!("output{id}");
3462 let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else {
3463 return;
3464 };
3465
3466 layout.move_to_output(&output);
3467 }
3468 Op::MoveWorkspaceDown => layout.move_workspace_down(),
3469 Op::MoveWorkspaceUp => layout.move_workspace_up(),
3470 Op::SwitchPresetColumnWidth => layout.toggle_width(),
3471 Op::MaximizeColumn => layout.toggle_full_width(),
3472 Op::SetColumnWidth(change) => layout.set_column_width(change),
3473 Op::SetWindowHeight(change) => layout.set_window_height(change),
3474 Op::Communicate(id) => {
3475 let mut window = None;
3476 match &mut layout.monitor_set {
3477 MonitorSet::Normal { monitors, .. } => {
3478 'outer: for mon in monitors {
3479 for ws in &mut mon.workspaces {
3480 for win in ws.windows() {
3481 if win.0.id == id {
3482 if win.communicate() {
3483 window = Some(win.clone());
3484 }
3485 break 'outer;
3486 }
3487 }
3488 }
3489 }
3490 }
3491 MonitorSet::NoOutputs { workspaces, .. } => {
3492 'outer: for ws in workspaces {
3493 for win in ws.windows() {
3494 if win.0.id == id {
3495 if win.communicate() {
3496 window = Some(win.clone());
3497 }
3498 break 'outer;
3499 }
3500 }
3501 }
3502 }
3503 }
3504
3505 if let Some(win) = window {
3506 layout.update_window(&win);
3507 }
3508 }
3509 }
3510 }
3511 }
3512
3513 #[track_caller]
3514 fn check_ops(ops: &[Op]) {
3515 let mut layout = Layout::default();
3516 for op in ops {
3517 op.apply(&mut layout);
3518 layout.verify_invariants();
3519 }
3520 }
3521
3522 #[test]
3523 fn operations_dont_panic() {
3524 let every_op = [
3525 Op::AddOutput(0),
3526 Op::AddOutput(1),
3527 Op::AddOutput(2),
3528 Op::RemoveOutput(0),
3529 Op::RemoveOutput(1),
3530 Op::RemoveOutput(2),
3531 Op::FocusOutput(0),
3532 Op::FocusOutput(1),
3533 Op::FocusOutput(2),
3534 Op::AddWindow {
3535 id: 0,
3536 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3537 },
3538 Op::AddWindow {
3539 id: 1,
3540 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3541 },
3542 Op::AddWindow {
3543 id: 2,
3544 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3545 },
3546 Op::CloseWindow(0),
3547 Op::CloseWindow(1),
3548 Op::CloseWindow(2),
3549 Op::FocusColumnLeft,
3550 Op::FocusColumnRight,
3551 Op::MoveColumnLeft,
3552 Op::MoveColumnRight,
3553 Op::ConsumeWindowIntoColumn,
3554 Op::ExpelWindowFromColumn,
3555 Op::CenterColumn,
3556 Op::FocusWorkspaceDown,
3557 Op::FocusWorkspaceUp,
3558 Op::FocusWorkspace(1),
3559 Op::FocusWorkspace(2),
3560 Op::FocusWorkspace(3),
3561 Op::MoveWindowToWorkspaceDown,
3562 Op::MoveWindowToWorkspaceUp,
3563 Op::MoveWindowToWorkspace(1),
3564 Op::MoveWindowToWorkspace(2),
3565 Op::MoveWindowToWorkspace(3),
3566 Op::MoveWindowDown,
3567 Op::MoveWindowUp,
3568 ];
3569
3570 for third in every_op {
3571 for second in every_op {
3572 for first in every_op {
3573 // eprintln!("{first:?}, {second:?}, {third:?}");
3574
3575 let mut layout = Layout::default();
3576 first.apply(&mut layout);
3577 layout.verify_invariants();
3578 second.apply(&mut layout);
3579 layout.verify_invariants();
3580 third.apply(&mut layout);
3581 layout.verify_invariants();
3582 }
3583 }
3584 }
3585 }
3586
3587 #[test]
3588 fn operations_from_starting_state_dont_panic() {
3589 if std::env::var_os("RUN_SLOW_TESTS").is_none() {
3590 eprintln!("ignoring slow test");
3591 return;
3592 }
3593
3594 // Running every op from an empty state doesn't get us to all the interesting states. So,
3595 // also run it from a manually-created starting state with more things going on to exercise
3596 // more code paths.
3597 let setup_ops = [
3598 Op::AddOutput(1),
3599 Op::AddWindow {
3600 id: 1,
3601 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3602 },
3603 Op::MoveWindowToWorkspaceDown,
3604 Op::AddWindow {
3605 id: 2,
3606 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3607 },
3608 Op::AddWindow {
3609 id: 3,
3610 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3611 },
3612 Op::FocusColumnLeft,
3613 Op::ConsumeWindowIntoColumn,
3614 Op::AddWindow {
3615 id: 4,
3616 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3617 },
3618 Op::AddOutput(2),
3619 Op::AddWindow {
3620 id: 5,
3621 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3622 },
3623 Op::MoveWindowToOutput(2),
3624 Op::FocusOutput(1),
3625 Op::Communicate(1),
3626 Op::Communicate(2),
3627 Op::Communicate(3),
3628 Op::Communicate(4),
3629 Op::Communicate(5),
3630 ];
3631
3632 let every_op = [
3633 Op::AddOutput(0),
3634 Op::AddOutput(1),
3635 Op::AddOutput(2),
3636 Op::RemoveOutput(0),
3637 Op::RemoveOutput(1),
3638 Op::RemoveOutput(2),
3639 Op::FocusOutput(0),
3640 Op::FocusOutput(1),
3641 Op::FocusOutput(2),
3642 Op::AddWindow {
3643 id: 0,
3644 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3645 },
3646 Op::AddWindow {
3647 id: 1,
3648 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3649 },
3650 Op::AddWindow {
3651 id: 2,
3652 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3653 },
3654 Op::CloseWindow(0),
3655 Op::CloseWindow(1),
3656 Op::CloseWindow(2),
3657 Op::FocusColumnLeft,
3658 Op::FocusColumnRight,
3659 Op::MoveColumnLeft,
3660 Op::MoveColumnRight,
3661 Op::ConsumeWindowIntoColumn,
3662 Op::ExpelWindowFromColumn,
3663 Op::CenterColumn,
3664 Op::FocusWorkspaceDown,
3665 Op::FocusWorkspaceUp,
3666 Op::FocusWorkspace(1),
3667 Op::FocusWorkspace(2),
3668 Op::FocusWorkspace(3),
3669 Op::MoveWindowToWorkspaceDown,
3670 Op::MoveWindowToWorkspaceUp,
3671 Op::MoveWindowToWorkspace(1),
3672 Op::MoveWindowToWorkspace(2),
3673 Op::MoveWindowToWorkspace(3),
3674 Op::MoveWindowDown,
3675 Op::MoveWindowUp,
3676 ];
3677
3678 for third in every_op {
3679 for second in every_op {
3680 for first in every_op {
3681 // eprintln!("{first:?}, {second:?}, {third:?}");
3682
3683 let mut layout = Layout::default();
3684 for op in setup_ops {
3685 op.apply(&mut layout);
3686 }
3687
3688 first.apply(&mut layout);
3689 layout.verify_invariants();
3690 second.apply(&mut layout);
3691 layout.verify_invariants();
3692 third.apply(&mut layout);
3693 layout.verify_invariants();
3694 }
3695 }
3696 }
3697 }
3698
3699 #[test]
3700 fn primary_active_workspace_idx_not_updated_on_output_add() {
3701 let ops = [
3702 Op::AddOutput(1),
3703 Op::AddOutput(2),
3704 Op::FocusOutput(1),
3705 Op::AddWindow {
3706 id: 0,
3707 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3708 },
3709 Op::FocusOutput(2),
3710 Op::AddWindow {
3711 id: 1,
3712 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3713 },
3714 Op::RemoveOutput(2),
3715 Op::FocusWorkspace(3),
3716 Op::AddOutput(2),
3717 ];
3718
3719 check_ops(&ops);
3720 }
3721
3722 #[test]
3723 fn window_closed_on_previous_workspace() {
3724 let ops = [
3725 Op::AddOutput(1),
3726 Op::AddWindow {
3727 id: 0,
3728 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3729 },
3730 Op::FocusWorkspaceDown,
3731 Op::CloseWindow(0),
3732 ];
3733
3734 check_ops(&ops);
3735 }
3736
3737 #[test]
3738 fn removing_output_must_keep_empty_focus_on_primary() {
3739 let ops = [
3740 Op::AddOutput(1),
3741 Op::AddWindow {
3742 id: 0,
3743 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3744 },
3745 Op::AddOutput(2),
3746 Op::RemoveOutput(1),
3747 ];
3748
3749 let mut layout = Layout::default();
3750 for op in ops {
3751 op.apply(&mut layout);
3752 }
3753
3754 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
3755 unreachable!()
3756 };
3757
3758 // The workspace from the removed output was inserted at position 0, so the active workspace
3759 // must change to 1 to keep the focus on the empty workspace.
3760 assert_eq!(monitors[0].active_workspace_idx, 1);
3761 }
3762
3763 #[test]
3764 fn move_to_workspace_by_idx_does_not_leave_empty_workspaces() {
3765 let ops = [
3766 Op::AddOutput(1),
3767 Op::AddWindow {
3768 id: 0,
3769 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3770 },
3771 Op::MoveWindowToWorkspace(2),
3772 ];
3773
3774 let mut layout = Layout::default();
3775 for op in ops {
3776 op.apply(&mut layout);
3777 }
3778
3779 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
3780 unreachable!()
3781 };
3782
3783 assert!(monitors[0].workspaces[0].has_windows());
3784 }
3785
3786 #[test]
3787 fn focus_workspace_by_idx_does_not_leave_empty_workspaces() {
3788 let ops = [
3789 Op::AddOutput(1),
3790 Op::AddWindow {
3791 id: 0,
3792 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3793 },
3794 Op::FocusWorkspaceDown,
3795 Op::AddWindow {
3796 id: 1,
3797 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3798 },
3799 Op::FocusWorkspaceUp,
3800 Op::CloseWindow(0),
3801 Op::FocusWorkspace(3),
3802 ];
3803
3804 let mut layout = Layout::default();
3805 for op in ops {
3806 op.apply(&mut layout);
3807 }
3808
3809 let MonitorSet::Normal { monitors, .. } = layout.monitor_set else {
3810 unreachable!()
3811 };
3812
3813 assert!(monitors[0].workspaces[0].has_windows());
3814 }
3815
3816 #[test]
3817 fn empty_workspaces_dont_move_back_to_original_output() {
3818 let ops = [
3819 Op::AddOutput(1),
3820 Op::AddWindow {
3821 id: 1,
3822 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3823 },
3824 Op::FocusWorkspaceDown,
3825 Op::AddWindow {
3826 id: 2,
3827 bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
3828 },
3829 Op::AddOutput(2),
3830 Op::RemoveOutput(1),
3831 Op::FocusWorkspace(1),
3832 Op::CloseWindow(1),
3833 Op::AddOutput(1),
3834 ];
3835
3836 check_ops(&ops);
3837 }
3838
3839 proptest! {
3840 #![proptest_config(ProptestConfig {
3841 cases: if std::env::var_os("RUN_SLOW_TESTS").is_none() {
3842 eprintln!("ignoring slow test");
3843 0
3844 } else {
3845 ProptestConfig::default().cases
3846 },
3847 ..ProptestConfig::default()
3848 })]
3849
3850 #[test]
3851 fn random_operations_dont_panic(ops: Vec<Op>) {
3852 // eprintln!("{ops:?}");
3853 check_ops(&ops);
3854 }
3855 }
3856}