forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB055a94dIvan Molodetskikh1 year ago2247 commits

mod.rs5276 lines, 183.3 KiBHistoryRawDownload
1//! Window layout logic.
2//!
3//! Niri implements scrollable tiling with dynamic workspaces. The scrollable tiling is mostly
4//! orthogonal to any particular workspace system, though outputs living in separate coordinate
5//! spaces suggest per-output workspaces.
6//!
7//! I chose a dynamic workspace system because I think it works very well. In particular, it works
8//! naturally across outputs getting added and removed, since workspaces can move between outputs
9//! as necessary.
10//!
11//! In the layout, one output (the first one to be added) is designated as *primary*. This is where
12//! workspaces from disconnected outputs will move. Currently, the primary output has no other
13//! distinction from other outputs.
14//!
15//! Where possible, niri tries to follow these principles with regards to outputs:
16//!
17//! 1. Disconnecting and reconnecting the same output must not change the layout.
18//! * This includes both secondary outputs and the primary output.
19//! 2. Connecting an output must not change the layout for any workspaces that were never on that
20//! output.
21//!
22//! Therefore, we implement the following logic: every workspace keeps track of which output it
23//! originated on—its *original output*. When an output disconnects, its workspaces are appended to
24//! the (potentially new) primary output, but remember their original output. Then, if the original
25//! output connects again, all workspaces originally from there move back to that output.
26//!
27//! In order to avoid surprising behavior, if the user creates or moves any new windows onto a
28//! workspace, it forgets its original output, and its current output becomes its original output.
29//! Imagine a scenario: the user works with a laptop and a monitor at home, then takes their laptop
30//! with them, disconnecting the monitor, and keeps working as normal, using the second monitor's
31//! workspace just like any other. Then they come back, reconnect the second monitor, and now we
32//! don't want an unassuming workspace to end up on it.
33
34use std::cmp::min;
35use std::collections::HashMap;
36use std::mem;
37use std::rc::Rc;
38use std::time::Duration;
39
40use monitor::{InsertHint, InsertPosition, InsertWorkspace, MonitorAddWindowTarget};
41use niri_config::{
42 CenterFocusedColumn, Config, CornerRadius, FloatOrInt, PresetSize, Struts,
43 Workspace as WorkspaceConfig, WorkspaceReference,
44};
45use niri_ipc::{ColumnDisplay, PositionChange, SizeChange};
46use scrolling::{Column, ColumnWidth};
47use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement;
48use smithay::backend::renderer::element::utils::RescaleRenderElement;
49use smithay::backend::renderer::gles::{GlesRenderer, GlesTexture};
50use smithay::output::{self, Output};
51use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface;
52use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size, Transform};
53use tile::{Tile, TileRenderElement};
54use workspace::{WorkspaceAddWindowTarget, WorkspaceId};
55
56pub use self::monitor::MonitorRenderElement;
57use self::monitor::{Monitor, WorkspaceSwitch};
58use self::workspace::{OutputId, Workspace};
59use crate::animation::{Animation, Clock};
60use crate::input::swipe_tracker::SwipeTracker;
61use crate::layout::scrolling::ScrollDirection;
62use crate::niri_render_elements;
63use crate::render_helpers::offscreen::OffscreenData;
64use crate::render_helpers::renderer::NiriRenderer;
65use crate::render_helpers::snapshot::RenderSnapshot;
66use crate::render_helpers::solid_color::{SolidColorBuffer, SolidColorRenderElement};
67use crate::render_helpers::texture::TextureBuffer;
68use crate::render_helpers::{BakedBuffer, RenderTarget, SplitElements};
69use crate::rubber_band::RubberBand;
70use crate::utils::transaction::{Transaction, TransactionBlocker};
71use crate::utils::{
72 ensure_min_max_size_maybe_zero, output_matches_name, output_size,
73 round_logical_in_physical_max1, ResizeEdge,
74};
75use crate::window::ResolvedWindowRules;
76
77pub mod closing_window;
78pub mod floating;
79pub mod focus_ring;
80pub mod insert_hint_element;
81pub mod monitor;
82pub mod opening_window;
83pub mod scrolling;
84pub mod shadow;
85pub mod tab_indicator;
86pub mod tile;
87pub mod workspace;
88
89#[cfg(test)]
90mod tests;
91
92/// Size changes up to this many pixels don't animate.
93pub const RESIZE_ANIMATION_THRESHOLD: f64 = 10.;
94
95/// Pointer needs to move this far to pull a window from the layout.
96const INTERACTIVE_MOVE_START_THRESHOLD: f64 = 256. * 256.;
97
98/// Opacity of interactively moved tiles targeting the scrolling layout.
99const INTERACTIVE_MOVE_ALPHA: f64 = 0.75;
100
101/// Amount of touchpad movement to toggle the overview.
102const OVERVIEW_GESTURE_MOVEMENT: f64 = 300.;
103
104const OVERVIEW_GESTURE_RUBBER_BAND: RubberBand = RubberBand {
105 stiffness: 0.5,
106 limit: 0.05,
107};
108
109/// Size-relative units.
110pub struct SizeFrac;
111
112niri_render_elements! {
113 LayoutElementRenderElement<R> => {
114 Wayland = WaylandSurfaceRenderElement<R>,
115 SolidColor = SolidColorRenderElement,
116 }
117}
118
119pub type LayoutElementRenderSnapshot =
120 RenderSnapshot<BakedBuffer<TextureBuffer<GlesTexture>>, BakedBuffer<SolidColorBuffer>>;
121
122pub trait LayoutElement {
123 /// Type that can be used as a unique ID of this element.
124 type Id: PartialEq + std::fmt::Debug + Clone;
125
126 /// Unique ID of this element.
127 fn id(&self) -> &Self::Id;
128
129 /// Visual size of the element.
130 ///
131 /// This is what the user would consider the size, i.e. excluding CSD shadows and whatnot.
132 /// Corresponds to the Wayland window geometry size.
133 fn size(&self) -> Size<i32, Logical>;
134
135 /// Returns the location of the element's buffer relative to the element's visual geometry.
136 ///
137 /// I.e. if the element has CSD shadows, its buffer location will have negative coordinates.
138 fn buf_loc(&self) -> Point<i32, Logical>;
139
140 /// Checks whether a point is in the element's input region.
141 ///
142 /// The point is relative to the element's visual geometry.
143 fn is_in_input_region(&self, point: Point<f64, Logical>) -> bool;
144
145 /// Renders the element at the given visual location.
146 ///
147 /// The element should be rendered in such a way that its visual geometry ends up at the given
148 /// location.
149 fn render<R: NiriRenderer>(
150 &self,
151 renderer: &mut R,
152 location: Point<f64, Logical>,
153 scale: Scale<f64>,
154 alpha: f32,
155 target: RenderTarget,
156 ) -> SplitElements<LayoutElementRenderElement<R>>;
157
158 /// Renders the non-popup parts of the element.
159 fn render_normal<R: NiriRenderer>(
160 &self,
161 renderer: &mut R,
162 location: Point<f64, Logical>,
163 scale: Scale<f64>,
164 alpha: f32,
165 target: RenderTarget,
166 ) -> Vec<LayoutElementRenderElement<R>> {
167 self.render(renderer, location, scale, alpha, target).normal
168 }
169
170 /// Renders the popups of the element.
171 fn render_popups<R: NiriRenderer>(
172 &self,
173 renderer: &mut R,
174 location: Point<f64, Logical>,
175 scale: Scale<f64>,
176 alpha: f32,
177 target: RenderTarget,
178 ) -> Vec<LayoutElementRenderElement<R>> {
179 self.render(renderer, location, scale, alpha, target).popups
180 }
181
182 /// Requests the element to change its size.
183 ///
184 /// The size request is stored and will be continuously sent to the element on any further
185 /// state changes.
186 fn request_size(
187 &mut self,
188 size: Size<i32, Logical>,
189 is_fullscreen: bool,
190 animate: bool,
191 transaction: Option<Transaction>,
192 );
193
194 /// Requests the element to change size once, clearing the request afterwards.
195 fn request_size_once(&mut self, size: Size<i32, Logical>, animate: bool) {
196 self.request_size(size, false, animate, None);
197 }
198
199 fn min_size(&self) -> Size<i32, Logical>;
200 fn max_size(&self) -> Size<i32, Logical>;
201 fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool;
202 fn has_ssd(&self) -> bool;
203 fn set_preferred_scale_transform(&self, scale: output::Scale, transform: Transform);
204 fn output_enter(&self, output: &Output);
205 fn output_leave(&self, output: &Output);
206 fn set_offscreen_data(&self, data: Option<OffscreenData>);
207 fn set_activated(&mut self, active: bool);
208 fn set_active_in_column(&mut self, active: bool);
209 fn set_floating(&mut self, floating: bool);
210 fn set_bounds(&self, bounds: Size<i32, Logical>);
211 fn is_ignoring_opacity_window_rule(&self) -> bool;
212
213 fn is_urgent(&self) -> bool;
214
215 fn configure_intent(&self) -> ConfigureIntent;
216 fn send_pending_configure(&mut self);
217
218 /// Whether the element is currently fullscreen.
219 ///
220 /// This will *not* switch immediately after a [`LayoutElement::request_size()`] call.
221 fn is_fullscreen(&self) -> bool;
222
223 /// Whether we're requesting the element to be fullscreen.
224 ///
225 /// This *will* switch immediately after a [`LayoutElement::request_size()`] call.
226 fn is_pending_fullscreen(&self) -> bool;
227
228 /// Size previously requested through [`LayoutElement::request_size()`].
229 fn requested_size(&self) -> Option<Size<i32, Logical>>;
230
231 /// Non-fullscreen size that we expect this window has or will shortly have.
232 ///
233 /// This can be different from [`requested_size()`](LayoutElement::requested_size()). For
234 /// example, for floating windows this will generally return the current window size, rather
235 /// than the last size that we requested, since we want floating windows to be able to change
236 /// size freely. But not always: if we just requested a floating window to resize and it hasn't
237 /// responded to it yet, this will return the newly requested size.
238 ///
239 /// This function should never return a 0 size component. `None` means there's no known
240 /// expected size (for example, the window is fullscreen).
241 ///
242 /// The default impl is for testing only, it will not preserve the window's own size changes.
243 fn expected_size(&self) -> Option<Size<i32, Logical>> {
244 if self.is_fullscreen() {
245 return None;
246 }
247
248 let mut requested = self.requested_size().unwrap_or_default();
249 let current = self.size();
250 if requested.w == 0 {
251 requested.w = current.w;
252 }
253 if requested.h == 0 {
254 requested.h = current.h;
255 }
256 Some(requested)
257 }
258
259 fn is_pending_windowed_fullscreen(&self) -> bool {
260 false
261 }
262 fn request_windowed_fullscreen(&mut self, value: bool) {
263 let _ = value;
264 }
265
266 fn is_child_of(&self, parent: &Self) -> bool;
267
268 fn rules(&self) -> &ResolvedWindowRules;
269
270 /// Runs periodic clean-up tasks.
271 fn refresh(&self);
272
273 fn animation_snapshot(&self) -> Option<&LayoutElementRenderSnapshot>;
274 fn take_animation_snapshot(&mut self) -> Option<LayoutElementRenderSnapshot>;
275
276 fn set_interactive_resize(&mut self, data: Option<InteractiveResizeData>);
277 fn cancel_interactive_resize(&mut self);
278 fn interactive_resize_data(&self) -> Option<InteractiveResizeData>;
279
280 fn on_commit(&mut self, serial: Serial);
281}
282
283#[derive(Debug)]
284pub struct Layout<W: LayoutElement> {
285 /// Monitors and workspaes in the layout.
286 monitor_set: MonitorSet<W>,
287 /// Whether the layout should draw as active.
288 ///
289 /// This normally indicates that the layout has keyboard focus, but not always. E.g. when the
290 /// screenshot UI is open, it keeps the layout drawing as active.
291 is_active: bool,
292 /// Map from monitor name to id of its last active workspace.
293 ///
294 /// This data is stored upon monitor removal and is used to restore the active workspace when
295 /// the monitor is reconnected.
296 ///
297 /// The workspace id does not necessarily point to a valid workspace. If it doesn't, then it is
298 /// simply ignored.
299 last_active_workspace_id: HashMap<String, WorkspaceId>,
300 /// Ongoing interactive move.
301 interactive_move: Option<InteractiveMoveState<W>>,
302 /// Ongoing drag-and-drop operation.
303 dnd: Option<DndData<W>>,
304 /// Clock for driving animations.
305 clock: Clock,
306 /// Time that we last updated render elements for.
307 update_render_elements_time: Duration,
308 /// Whether the overview is open.
309 ///
310 /// This is a boolean flag that controls things like where input goes to. The actual animation
311 /// is controlled by overview_progress.
312 overview_open: bool,
313 /// The overview zoom progress.
314 overview_progress: Option<OverviewProgress>,
315 /// Configurable properties of the layout.
316 options: Rc<Options>,
317}
318
319#[derive(Debug)]
320enum MonitorSet<W: LayoutElement> {
321 /// At least one output is connected.
322 Normal {
323 /// Connected monitors.
324 monitors: Vec<Monitor<W>>,
325 /// Index of the primary monitor.
326 primary_idx: usize,
327 /// Index of the active monitor.
328 active_monitor_idx: usize,
329 },
330 /// No outputs are connected, and these are the workspaces.
331 NoOutputs {
332 /// The workspaces.
333 workspaces: Vec<Workspace<W>>,
334 },
335}
336
337#[derive(Debug, Clone, PartialEq)]
338pub struct Options {
339 /// Padding around windows in logical pixels.
340 pub gaps: f64,
341 /// Extra padding around the working area in logical pixels.
342 pub struts: Struts,
343 pub focus_ring: niri_config::FocusRing,
344 pub border: niri_config::Border,
345 pub shadow: niri_config::Shadow,
346 pub tab_indicator: niri_config::TabIndicator,
347 pub insert_hint: niri_config::InsertHint,
348 pub center_focused_column: CenterFocusedColumn,
349 pub always_center_single_column: bool,
350 pub empty_workspace_above_first: bool,
351 pub default_column_display: ColumnDisplay,
352 /// Column or window widths that `toggle_width()` switches between.
353 pub preset_column_widths: Vec<PresetSize>,
354 /// Initial width for new columns.
355 pub default_column_width: Option<PresetSize>,
356 /// Window height that `toggle_window_height()` switches between.
357 pub preset_window_heights: Vec<PresetSize>,
358 pub animations: niri_config::Animations,
359 pub gestures: niri_config::Gestures,
360 pub overview: niri_config::Overview,
361 // Debug flags.
362 pub disable_resize_throttling: bool,
363 pub disable_transactions: bool,
364 pub deactivate_unfocused_windows: bool,
365}
366
367impl Default for Options {
368 fn default() -> Self {
369 Self {
370 gaps: 16.,
371 struts: Default::default(),
372 focus_ring: Default::default(),
373 border: Default::default(),
374 shadow: Default::default(),
375 tab_indicator: Default::default(),
376 insert_hint: Default::default(),
377 center_focused_column: Default::default(),
378 always_center_single_column: false,
379 empty_workspace_above_first: false,
380 default_column_display: ColumnDisplay::Normal,
381 preset_column_widths: vec![
382 PresetSize::Proportion(1. / 3.),
383 PresetSize::Proportion(0.5),
384 PresetSize::Proportion(2. / 3.),
385 ],
386 default_column_width: None,
387 animations: Default::default(),
388 gestures: Default::default(),
389 overview: Default::default(),
390 disable_resize_throttling: false,
391 disable_transactions: false,
392 preset_window_heights: vec![
393 PresetSize::Proportion(1. / 3.),
394 PresetSize::Proportion(0.5),
395 PresetSize::Proportion(2. / 3.),
396 ],
397 deactivate_unfocused_windows: false,
398 }
399 }
400}
401
402#[allow(clippy::large_enum_variant)]
403#[derive(Debug)]
404enum InteractiveMoveState<W: LayoutElement> {
405 /// Initial rubberbanding; the window remains in the layout.
406 Starting {
407 /// The window we're moving.
408 window_id: W::Id,
409 /// Current pointer delta from the starting location.
410 pointer_delta: Point<f64, Logical>,
411 /// Pointer location within the visual window geometry as ratio from geometry size.
412 ///
413 /// This helps the pointer remain inside the window as it resizes.
414 pointer_ratio_within_window: (f64, f64),
415 },
416 /// Moving; the window is no longer in the layout.
417 Moving(InteractiveMoveData<W>),
418}
419
420#[derive(Debug)]
421struct InteractiveMoveData<W: LayoutElement> {
422 /// The window being moved.
423 pub(self) tile: Tile<W>,
424 /// Output where the window is currently located/rendered.
425 pub(self) output: Output,
426 /// Current pointer position within output.
427 pub(self) pointer_pos_within_output: Point<f64, Logical>,
428 /// Window column width.
429 pub(self) width: ColumnWidth,
430 /// Whether the window column was full-width.
431 pub(self) is_full_width: bool,
432 /// Whether the window targets the floating layout.
433 pub(self) is_floating: bool,
434 /// Pointer location within the visual window geometry as ratio from geometry size.
435 ///
436 /// This helps the pointer remain inside the window as it resizes.
437 pub(self) pointer_ratio_within_window: (f64, f64),
438}
439
440#[derive(Debug)]
441pub struct DndData<W: LayoutElement> {
442 /// Output where the pointer is currently located.
443 output: Output,
444 /// Current pointer position within output.
445 pointer_pos_within_output: Point<f64, Logical>,
446 /// Ongoing DnD hold to activate something.
447 hold: Option<DndHold<W>>,
448}
449
450#[derive(Debug)]
451struct DndHold<W: LayoutElement> {
452 /// Time when we started holding on the target.
453 start_time: Duration,
454 target: DndHoldTarget<W::Id>,
455}
456
457#[derive(Debug, PartialEq, Eq)]
458enum DndHoldTarget<WindowId> {
459 Window(WindowId),
460 Workspace(WorkspaceId),
461}
462
463#[derive(Debug, Clone, Copy)]
464pub struct InteractiveResizeData {
465 pub(self) edges: ResizeEdge,
466}
467
468#[derive(Debug, Clone, Copy)]
469pub enum ConfigureIntent {
470 /// A configure is not needed (no changes to server pending state).
471 NotNeeded,
472 /// A configure is throttled (due to resizing too fast for example).
473 Throttled,
474 /// Can send the configure if it isn't throttled externally (only size changed).
475 CanSend,
476 /// Should send the configure regardless of external throttling (something other than size
477 /// changed).
478 ShouldSend,
479}
480
481/// Tile that was just removed from the layout.
482pub struct RemovedTile<W: LayoutElement> {
483 tile: Tile<W>,
484 /// Width of the column the tile was in.
485 width: ColumnWidth,
486 /// Whether the column the tile was in was full-width.
487 is_full_width: bool,
488 /// Whether the tile was floating.
489 is_floating: bool,
490}
491
492/// Whether to activate a newly added window.
493#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
494pub enum ActivateWindow {
495 /// Activate unconditionally.
496 Yes,
497 /// Activate based on heuristics.
498 #[default]
499 Smart,
500 /// Do not activate.
501 No,
502}
503
504/// Where to put a newly added window.
505#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
506pub enum AddWindowTarget<'a, W: LayoutElement> {
507 /// No particular preference.
508 #[default]
509 Auto,
510 /// On this output.
511 Output(&'a Output),
512 /// On this workspace.
513 Workspace(WorkspaceId),
514 /// Next to this existing window.
515 NextTo(&'a W::Id),
516}
517
518/// Type of the window hit from `window_under()`.
519#[derive(Debug, Clone, Copy, PartialEq)]
520pub enum HitType {
521 /// The hit is within a window's input region and can be used for sending events to it.
522 Input {
523 /// Position of the window's buffer.
524 win_pos: Point<f64, Logical>,
525 },
526 /// The hit can activate a window, but it is not in the input region so cannot send events.
527 ///
528 /// For example, this could be clicking on a tile border outside the window.
529 Activate {
530 /// Whether the hit was on the tab indicator.
531 is_tab_indicator: bool,
532 },
533}
534
535#[derive(Debug)]
536enum OverviewProgress {
537 Animation(Animation),
538 Gesture(OverviewGesture),
539}
540
541#[derive(Debug)]
542struct OverviewGesture {
543 tracker: SwipeTracker,
544 /// Start point.
545 start: f64,
546 /// Current progress.
547 value: f64,
548}
549
550impl<W: LayoutElement> InteractiveMoveState<W> {
551 fn moving(&self) -> Option<&InteractiveMoveData<W>> {
552 match self {
553 InteractiveMoveState::Moving(move_) => Some(move_),
554 _ => None,
555 }
556 }
557
558 fn moving_mut(&mut self) -> Option<&mut InteractiveMoveData<W>> {
559 match self {
560 InteractiveMoveState::Moving(move_) => Some(move_),
561 _ => None,
562 }
563 }
564}
565
566impl<W: LayoutElement> InteractiveMoveData<W> {
567 fn tile_render_location(&self, zoom: f64) -> Point<f64, Logical> {
568 let scale = Scale::from(self.output.current_scale().fractional_scale());
569 let window_size = self.tile.window_size();
570 let pointer_offset_within_window = Point::from((
571 window_size.w * self.pointer_ratio_within_window.0,
572 window_size.h * self.pointer_ratio_within_window.1,
573 ));
574 let pos = self.pointer_pos_within_output
575 - (pointer_offset_within_window + self.tile.window_loc() - self.tile.render_offset())
576 .upscale(zoom);
577 // Round to physical pixels.
578 pos.to_physical_precise_round(scale).to_logical(scale)
579 }
580}
581
582impl ActivateWindow {
583 pub fn map_smart(self, f: impl FnOnce() -> bool) -> bool {
584 match self {
585 ActivateWindow::Yes => true,
586 ActivateWindow::Smart => f(),
587 ActivateWindow::No => false,
588 }
589 }
590}
591
592impl HitType {
593 pub fn offset_win_pos(mut self, offset: Point<f64, Logical>) -> Self {
594 match &mut self {
595 HitType::Input { win_pos } => *win_pos += offset,
596 HitType::Activate { .. } => (),
597 }
598 self
599 }
600
601 pub fn hit_tile<W: LayoutElement>(
602 tile: &Tile<W>,
603 tile_pos: Point<f64, Logical>,
604 point: Point<f64, Logical>,
605 ) -> Option<(&W, Self)> {
606 let pos_within_tile = point - tile_pos;
607 tile.hit(pos_within_tile)
608 .map(|hit| (tile.window(), hit.offset_win_pos(tile_pos)))
609 }
610
611 pub fn to_activate(self) -> Self {
612 match self {
613 HitType::Input { .. } => HitType::Activate {
614 is_tab_indicator: false,
615 },
616 HitType::Activate { .. } => self,
617 }
618 }
619}
620
621impl Options {
622 fn from_config(config: &Config) -> Self {
623 let layout = &config.layout;
624
625 let preset_column_widths = if layout.preset_column_widths.is_empty() {
626 Options::default().preset_column_widths
627 } else {
628 layout.preset_column_widths.clone()
629 };
630 let preset_window_heights = if layout.preset_window_heights.is_empty() {
631 Options::default().preset_window_heights
632 } else {
633 layout.preset_window_heights.clone()
634 };
635
636 // Missing default_column_width maps to Some(PresetSize::Proportion(0.5)),
637 // while present, but empty, maps to None.
638 let default_column_width = layout
639 .default_column_width
640 .as_ref()
641 .map(|w| w.0)
642 .unwrap_or(Some(PresetSize::Proportion(0.5)));
643
644 Self {
645 gaps: layout.gaps.0,
646 struts: layout.struts,
647 focus_ring: layout.focus_ring,
648 border: layout.border,
649 shadow: layout.shadow,
650 tab_indicator: layout.tab_indicator,
651 insert_hint: layout.insert_hint,
652 center_focused_column: layout.center_focused_column,
653 always_center_single_column: layout.always_center_single_column,
654 empty_workspace_above_first: layout.empty_workspace_above_first,
655 default_column_display: layout.default_column_display,
656 preset_column_widths,
657 default_column_width,
658 animations: config.animations.clone(),
659 gestures: config.gestures,
660 overview: config.overview,
661 disable_resize_throttling: config.debug.disable_resize_throttling,
662 disable_transactions: config.debug.disable_transactions,
663 deactivate_unfocused_windows: config.debug.deactivate_unfocused_windows,
664 preset_window_heights,
665 }
666 }
667
668 fn adjusted_for_scale(mut self, scale: f64) -> Self {
669 let round = |logical: f64| round_logical_in_physical_max1(scale, logical);
670
671 self.gaps = round(self.gaps);
672 self.focus_ring.width = FloatOrInt(round(self.focus_ring.width.0));
673 self.border.width = FloatOrInt(round(self.border.width.0));
674
675 self
676 }
677}
678
679impl OverviewProgress {
680 fn value(&self) -> f64 {
681 match self {
682 OverviewProgress::Animation(anim) => anim.value(),
683 OverviewProgress::Gesture(gesture) => gesture.value,
684 }
685 }
686
687 fn is_animation(&self) -> bool {
688 matches!(self, OverviewProgress::Animation(_))
689 }
690}
691
692impl<W: LayoutElement> Layout<W> {
693 pub fn new(clock: Clock, config: &Config) -> Self {
694 Self::with_options_and_workspaces(clock, config, Options::from_config(config))
695 }
696
697 pub fn with_options(clock: Clock, options: Options) -> Self {
698 Self {
699 monitor_set: MonitorSet::NoOutputs { workspaces: vec![] },
700 is_active: true,
701 last_active_workspace_id: HashMap::new(),
702 interactive_move: None,
703 dnd: None,
704 clock,
705 update_render_elements_time: Duration::ZERO,
706 overview_open: false,
707 overview_progress: None,
708 options: Rc::new(options),
709 }
710 }
711
712 fn with_options_and_workspaces(clock: Clock, config: &Config, options: Options) -> Self {
713 let opts = Rc::new(options);
714
715 let workspaces = config
716 .workspaces
717 .iter()
718 .map(|ws| {
719 Workspace::new_with_config_no_outputs(Some(ws.clone()), clock.clone(), opts.clone())
720 })
721 .collect();
722
723 Self {
724 monitor_set: MonitorSet::NoOutputs { workspaces },
725 is_active: true,
726 last_active_workspace_id: HashMap::new(),
727 interactive_move: None,
728 dnd: None,
729 clock,
730 update_render_elements_time: Duration::ZERO,
731 overview_open: false,
732 overview_progress: None,
733 options: opts,
734 }
735 }
736
737 pub fn add_output(&mut self, output: Output) {
738 self.monitor_set = match mem::take(&mut self.monitor_set) {
739 MonitorSet::Normal {
740 mut monitors,
741 primary_idx,
742 active_monitor_idx,
743 } => {
744 let primary = &mut monitors[primary_idx];
745
746 let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
747 let mut active_workspace_idx = None;
748
749 let mut stopped_primary_ws_switch = false;
750
751 let mut workspaces = vec![];
752 for i in (0..primary.workspaces.len()).rev() {
753 if primary.workspaces[i].original_output.matches(&output) {
754 let ws = primary.workspaces.remove(i);
755
756 // FIXME: this can be coded in a way that the workspace switch won't be
757 // affected if the removed workspace is invisible. But this is good enough
758 // for now.
759 if primary.workspace_switch.is_some() {
760 primary.workspace_switch = None;
761 stopped_primary_ws_switch = true;
762 }
763
764 // The user could've closed a window while remaining on this workspace, on
765 // another monitor. However, we will add an empty workspace in the end
766 // instead.
767 if ws.has_windows_or_name() {
768 if Some(ws.id()) == ws_id_to_activate {
769 active_workspace_idx = Some(workspaces.len());
770 }
771
772 workspaces.push(ws);
773 }
774
775 if i <= primary.active_workspace_idx
776 // Generally when moving the currently active workspace, we want to
777 // fall back to the workspace above, so as not to end up on the last
778 // empty workspace. However, with empty workspace above first, when
779 // moving the workspace at index 1 (first non-empty), we want to stay
780 // at index 1, so as once again not to end up on an empty workspace.
781 //
782 // This comes into play at compositor startup when having named
783 // workspaces set up across multiple monitors. Without this check, the
784 // first monitor to connect can end up with the first empty workspace
785 // focused instead of the first named workspace.
786 && !(self.options.empty_workspace_above_first
787 && primary.active_workspace_idx == 1)
788 {
789 primary.active_workspace_idx =
790 primary.active_workspace_idx.saturating_sub(1);
791 }
792 }
793 }
794
795 // If we stopped a workspace switch, then we might need to clean up workspaces.
796 // Also if empty_workspace_above_first is set and there are only 2 workspaces left,
797 // both will be empty and one of them needs to be removed. clean_up_workspaces
798 // takes care of this.
799
800 if stopped_primary_ws_switch
801 || (primary.options.empty_workspace_above_first
802 && primary.workspaces.len() == 2)
803 {
804 primary.clean_up_workspaces();
805 }
806
807 workspaces.reverse();
808
809 if let Some(idx) = &mut active_workspace_idx {
810 *idx = workspaces.len() - *idx - 1;
811 }
812 let mut active_workspace_idx = active_workspace_idx.unwrap_or(0);
813
814 // Make sure there's always an empty workspace.
815 workspaces.push(Workspace::new(
816 output.clone(),
817 self.clock.clone(),
818 self.options.clone(),
819 ));
820
821 if self.options.empty_workspace_above_first && workspaces.len() > 1 {
822 workspaces.insert(
823 0,
824 Workspace::new(output.clone(), self.clock.clone(), self.options.clone()),
825 );
826 active_workspace_idx += 1;
827 }
828
829 for ws in &mut workspaces {
830 ws.set_output(Some(output.clone()));
831 }
832
833 let mut monitor =
834 Monitor::new(output, workspaces, self.clock.clone(), self.options.clone());
835 monitor.active_workspace_idx = active_workspace_idx;
836 monitor.overview_open = self.overview_open;
837 monitor.set_overview_progress(self.overview_progress.as_ref());
838 monitors.push(monitor);
839
840 MonitorSet::Normal {
841 monitors,
842 primary_idx,
843 active_monitor_idx,
844 }
845 }
846 MonitorSet::NoOutputs { mut workspaces } => {
847 // We know there are no empty workspaces there, so add one.
848 workspaces.push(Workspace::new(
849 output.clone(),
850 self.clock.clone(),
851 self.options.clone(),
852 ));
853
854 let mut active_workspace_idx = 0;
855 if self.options.empty_workspace_above_first && workspaces.len() > 1 {
856 workspaces.insert(
857 0,
858 Workspace::new(output.clone(), self.clock.clone(), self.options.clone()),
859 );
860 active_workspace_idx += 1;
861 }
862
863 let ws_id_to_activate = self.last_active_workspace_id.remove(&output.name());
864
865 for (i, workspace) in workspaces.iter_mut().enumerate() {
866 workspace.set_output(Some(output.clone()));
867
868 if Some(workspace.id()) == ws_id_to_activate {
869 active_workspace_idx = i;
870 }
871 }
872
873 let mut monitor =
874 Monitor::new(output, workspaces, self.clock.clone(), self.options.clone());
875 monitor.active_workspace_idx = active_workspace_idx;
876 monitor.overview_open = self.overview_open;
877 monitor.set_overview_progress(self.overview_progress.as_ref());
878
879 MonitorSet::Normal {
880 monitors: vec![monitor],
881 primary_idx: 0,
882 active_monitor_idx: 0,
883 }
884 }
885 }
886 }
887
888 pub fn remove_output(&mut self, output: &Output) {
889 self.monitor_set = match mem::take(&mut self.monitor_set) {
890 MonitorSet::Normal {
891 mut monitors,
892 mut primary_idx,
893 mut active_monitor_idx,
894 } => {
895 let idx = monitors
896 .iter()
897 .position(|mon| &mon.output == output)
898 .expect("trying to remove non-existing output");
899 let monitor = monitors.remove(idx);
900
901 self.last_active_workspace_id.insert(
902 monitor.output_name().clone(),
903 monitor.workspaces[monitor.active_workspace_idx].id(),
904 );
905
906 let mut workspaces = monitor.workspaces;
907
908 for ws in &mut workspaces {
909 ws.set_output(None);
910 }
911
912 // Get rid of empty workspaces.
913 workspaces.retain(|ws| ws.has_windows_or_name());
914
915 if monitors.is_empty() {
916 // Removed the last monitor.
917 MonitorSet::NoOutputs { workspaces }
918 } else {
919 if primary_idx >= idx {
920 // Update primary_idx to either still point at the same monitor, or at some
921 // other monitor if the primary has been removed.
922 primary_idx = primary_idx.saturating_sub(1);
923 }
924 if active_monitor_idx >= idx {
925 // Update active_monitor_idx to either still point at the same monitor, or
926 // at some other monitor if the active monitor has
927 // been removed.
928 active_monitor_idx = active_monitor_idx.saturating_sub(1);
929 }
930
931 let primary = &mut monitors[primary_idx];
932 for ws in &mut workspaces {
933 ws.set_output(Some(primary.output.clone()));
934 }
935
936 let mut stopped_primary_ws_switch = false;
937 if !workspaces.is_empty() && primary.workspace_switch.is_some() {
938 // FIXME: if we're adding workspaces to currently invisible positions
939 // (outside the workspace switch), we don't need to cancel it.
940 primary.workspace_switch = None;
941 stopped_primary_ws_switch = true;
942 }
943
944 let empty_was_focused =
945 primary.active_workspace_idx == primary.workspaces.len() - 1;
946
947 // Push the workspaces from the removed monitor in the end, right before the
948 // last, empty, workspace.
949 let empty = primary.workspaces.remove(primary.workspaces.len() - 1);
950 primary.workspaces.extend(workspaces);
951 primary.workspaces.push(empty);
952
953 // If empty_workspace_above_first is set and the first workspace is now no
954 // longer empty, add a new empty workspace on top.
955 if primary.options.empty_workspace_above_first
956 && primary.workspaces[0].has_windows_or_name()
957 {
958 primary.add_workspace_top();
959 }
960
961 // If the empty workspace was focused on the primary monitor, keep it focused.
962 if empty_was_focused {
963 primary.active_workspace_idx = primary.workspaces.len() - 1;
964 }
965
966 if stopped_primary_ws_switch {
967 primary.clean_up_workspaces();
968 }
969
970 MonitorSet::Normal {
971 monitors,
972 primary_idx,
973 active_monitor_idx,
974 }
975 }
976 }
977 MonitorSet::NoOutputs { .. } => {
978 panic!("tried to remove output when there were already none")
979 }
980 }
981 }
982
983 pub fn add_column_by_idx(
984 &mut self,
985 monitor_idx: usize,
986 workspace_idx: usize,
987 column: Column<W>,
988 activate: bool,
989 ) {
990 let MonitorSet::Normal {
991 monitors,
992 active_monitor_idx,
993 ..
994 } = &mut self.monitor_set
995 else {
996 panic!()
997 };
998
999 monitors[monitor_idx].add_column(workspace_idx, column, activate);
1000
1001 if activate {
1002 *active_monitor_idx = monitor_idx;
1003 }
1004 }
1005
1006 /// Adds a new window to the layout.
1007 ///
1008 /// Returns an output that the window was added to, if there were any outputs.
1009 #[allow(clippy::too_many_arguments)]
1010 pub fn add_window(
1011 &mut self,
1012 window: W,
1013 target: AddWindowTarget<W>,
1014 width: Option<PresetSize>,
1015 height: Option<PresetSize>,
1016 is_full_width: bool,
1017 is_floating: bool,
1018 activate: ActivateWindow,
1019 ) -> Option<&Output> {
1020 let scrolling_width = self.resolve_scrolling_width(&window, width);
1021 let scrolling_height = height.map(SizeChange::from);
1022 let id = window.id().clone();
1023
1024 match &mut self.monitor_set {
1025 MonitorSet::Normal {
1026 monitors,
1027 active_monitor_idx,
1028 ..
1029 } => {
1030 let (mon_idx, target) = match target {
1031 AddWindowTarget::Auto => (*active_monitor_idx, MonitorAddWindowTarget::Auto),
1032 AddWindowTarget::Output(output) => {
1033 let mon_idx = monitors
1034 .iter()
1035 .position(|mon| mon.output == *output)
1036 .unwrap();
1037
1038 (mon_idx, MonitorAddWindowTarget::Auto)
1039 }
1040 AddWindowTarget::Workspace(ws_id) => {
1041 let mon_idx = monitors
1042 .iter()
1043 .position(|mon| mon.workspaces.iter().any(|ws| ws.id() == ws_id))
1044 .unwrap();
1045
1046 (
1047 mon_idx,
1048 MonitorAddWindowTarget::Workspace {
1049 id: ws_id,
1050 column_idx: None,
1051 },
1052 )
1053 }
1054 AddWindowTarget::NextTo(next_to) => {
1055 if let Some(output) = self
1056 .interactive_move
1057 .as_ref()
1058 .and_then(|move_| {
1059 if let InteractiveMoveState::Moving(move_) = move_ {
1060 Some(move_)
1061 } else {
1062 None
1063 }
1064 })
1065 .filter(|move_| next_to == move_.tile.window().id())
1066 .map(|move_| move_.output.clone())
1067 {
1068 // The next_to window is being interactively moved.
1069 let mon_idx = monitors
1070 .iter()
1071 .position(|mon| mon.output == output)
1072 .unwrap_or(*active_monitor_idx);
1073
1074 (mon_idx, MonitorAddWindowTarget::Auto)
1075 } else {
1076 let mon_idx = monitors
1077 .iter()
1078 .position(|mon| {
1079 mon.workspaces.iter().any(|ws| ws.has_window(next_to))
1080 })
1081 .unwrap();
1082 (mon_idx, MonitorAddWindowTarget::NextTo(next_to))
1083 }
1084 }
1085 };
1086 let mon = &mut monitors[mon_idx];
1087
1088 mon.add_window(
1089 window,
1090 target,
1091 activate,
1092 scrolling_width,
1093 is_full_width,
1094 is_floating,
1095 );
1096
1097 if activate.map_smart(|| false) {
1098 *active_monitor_idx = mon_idx;
1099 }
1100
1101 // Set the default height for scrolling windows.
1102 if !is_floating {
1103 if let Some(change) = scrolling_height {
1104 let ws = mon
1105 .workspaces
1106 .iter_mut()
1107 .find(|ws| ws.has_window(&id))
1108 .unwrap();
1109 ws.set_window_height(Some(&id), change);
1110 }
1111 }
1112
1113 Some(&mon.output)
1114 }
1115 MonitorSet::NoOutputs { workspaces } => {
1116 let (ws_idx, target) = match target {
1117 AddWindowTarget::Auto => {
1118 if workspaces.is_empty() {
1119 workspaces.push(Workspace::new_no_outputs(
1120 self.clock.clone(),
1121 self.options.clone(),
1122 ));
1123 }
1124
1125 (0, WorkspaceAddWindowTarget::Auto)
1126 }
1127 AddWindowTarget::Output(_) => panic!(),
1128 AddWindowTarget::Workspace(ws_id) => {
1129 let ws_idx = workspaces.iter().position(|ws| ws.id() == ws_id).unwrap();
1130 (ws_idx, WorkspaceAddWindowTarget::Auto)
1131 }
1132 AddWindowTarget::NextTo(next_to) => {
1133 if self
1134 .interactive_move
1135 .as_ref()
1136 .and_then(|move_| {
1137 if let InteractiveMoveState::Moving(move_) = move_ {
1138 Some(move_)
1139 } else {
1140 None
1141 }
1142 })
1143 .filter(|move_| next_to == move_.tile.window().id())
1144 .is_some()
1145 {
1146 // The next_to window is being interactively moved.
1147 (0, WorkspaceAddWindowTarget::Auto)
1148 } else {
1149 let ws_idx = workspaces
1150 .iter()
1151 .position(|ws| ws.has_window(next_to))
1152 .unwrap();
1153 (ws_idx, WorkspaceAddWindowTarget::NextTo(next_to))
1154 }
1155 }
1156 };
1157 let ws = &mut workspaces[ws_idx];
1158
1159 let tile = ws.make_tile(window);
1160 ws.add_tile(
1161 tile,
1162 target,
1163 activate,
1164 scrolling_width,
1165 is_full_width,
1166 is_floating,
1167 );
1168
1169 // Set the default height for scrolling windows.
1170 if !is_floating {
1171 if let Some(change) = scrolling_height {
1172 ws.set_window_height(Some(&id), change);
1173 }
1174 }
1175
1176 None
1177 }
1178 }
1179 }
1180
1181 pub fn remove_window(
1182 &mut self,
1183 window: &W::Id,
1184 transaction: Transaction,
1185 ) -> Option<RemovedTile<W>> {
1186 if let Some(state) = &self.interactive_move {
1187 match state {
1188 InteractiveMoveState::Starting { window_id, .. } => {
1189 if window_id == window {
1190 self.interactive_move_end(window);
1191 }
1192 }
1193 InteractiveMoveState::Moving(move_) => {
1194 if move_.tile.window().id() == window {
1195 let Some(InteractiveMoveState::Moving(move_)) =
1196 self.interactive_move.take()
1197 else {
1198 unreachable!()
1199 };
1200
1201 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
1202 for mon in monitors {
1203 mon.dnd_scroll_gesture_end();
1204 }
1205 }
1206
1207 // Unlock the view on the workspaces.
1208 for ws in self.workspaces_mut() {
1209 ws.dnd_scroll_gesture_end();
1210 }
1211
1212 return Some(RemovedTile {
1213 tile: move_.tile,
1214 width: move_.width,
1215 is_full_width: move_.is_full_width,
1216 is_floating: false,
1217 });
1218 }
1219 }
1220 }
1221 }
1222
1223 match &mut self.monitor_set {
1224 MonitorSet::Normal { monitors, .. } => {
1225 for mon in monitors {
1226 for (idx, ws) in mon.workspaces.iter_mut().enumerate() {
1227 if ws.has_window(window) {
1228 let removed = ws.remove_tile(window, transaction);
1229
1230 // Clean up empty workspaces that are not active and not last.
1231 if !ws.has_windows_or_name()
1232 && idx != mon.active_workspace_idx
1233 && idx != mon.workspaces.len() - 1
1234 && mon.workspace_switch.is_none()
1235 {
1236 mon.workspaces.remove(idx);
1237
1238 if idx < mon.active_workspace_idx {
1239 mon.active_workspace_idx -= 1;
1240 }
1241 }
1242
1243 // Special case handling when empty_workspace_above_first is set and all
1244 // workspaces are empty.
1245 if mon.options.empty_workspace_above_first
1246 && mon.workspaces.len() == 2
1247 && mon.workspace_switch.is_none()
1248 {
1249 assert!(!mon.workspaces[0].has_windows_or_name());
1250 assert!(!mon.workspaces[1].has_windows_or_name());
1251 mon.workspaces.remove(1);
1252 mon.active_workspace_idx = 0;
1253 }
1254 return Some(removed);
1255 }
1256 }
1257 }
1258 }
1259 MonitorSet::NoOutputs { workspaces, .. } => {
1260 for (idx, ws) in workspaces.iter_mut().enumerate() {
1261 if ws.has_window(window) {
1262 let removed = ws.remove_tile(window, transaction);
1263
1264 // Clean up empty workspaces.
1265 if !ws.has_windows_or_name() {
1266 workspaces.remove(idx);
1267 }
1268
1269 return Some(removed);
1270 }
1271 }
1272 }
1273 }
1274
1275 None
1276 }
1277
1278 pub fn descendants_added(&mut self, id: &W::Id) -> bool {
1279 for ws in self.workspaces_mut() {
1280 if ws.descendants_added(id) {
1281 return true;
1282 }
1283 }
1284
1285 false
1286 }
1287
1288 pub fn update_window(&mut self, window: &W::Id, serial: Option<Serial>) {
1289 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
1290 if move_.tile.window().id() == window {
1291 // Do this before calling update_window() so it can get up-to-date info.
1292 if let Some(serial) = serial {
1293 move_.tile.window_mut().on_commit(serial);
1294 }
1295
1296 move_.tile.update_window();
1297 return;
1298 }
1299 }
1300
1301 match &mut self.monitor_set {
1302 MonitorSet::Normal { monitors, .. } => {
1303 for mon in monitors {
1304 for ws in &mut mon.workspaces {
1305 if ws.has_window(window) {
1306 ws.update_window(window, serial);
1307 return;
1308 }
1309 }
1310 }
1311 }
1312 MonitorSet::NoOutputs { workspaces, .. } => {
1313 for ws in workspaces {
1314 if ws.has_window(window) {
1315 ws.update_window(window, serial);
1316 return;
1317 }
1318 }
1319 }
1320 }
1321 }
1322
1323 pub fn find_workspace_by_id(&self, id: WorkspaceId) -> Option<(usize, &Workspace<W>)> {
1324 match &self.monitor_set {
1325 MonitorSet::Normal { ref monitors, .. } => {
1326 for mon in monitors {
1327 if let Some((index, workspace)) = mon
1328 .workspaces
1329 .iter()
1330 .enumerate()
1331 .find(|(_, w)| w.id() == id)
1332 {
1333 return Some((index, workspace));
1334 }
1335 }
1336 }
1337 MonitorSet::NoOutputs { workspaces } => {
1338 if let Some((index, workspace)) =
1339 workspaces.iter().enumerate().find(|(_, w)| w.id() == id)
1340 {
1341 return Some((index, workspace));
1342 }
1343 }
1344 }
1345
1346 None
1347 }
1348
1349 pub fn find_workspace_by_name(&self, workspace_name: &str) -> Option<(usize, &Workspace<W>)> {
1350 match &self.monitor_set {
1351 MonitorSet::Normal { ref monitors, .. } => {
1352 for mon in monitors {
1353 if let Some((index, workspace)) =
1354 mon.workspaces.iter().enumerate().find(|(_, w)| {
1355 w.name
1356 .as_ref()
1357 .is_some_and(|name| name.eq_ignore_ascii_case(workspace_name))
1358 })
1359 {
1360 return Some((index, workspace));
1361 }
1362 }
1363 }
1364 MonitorSet::NoOutputs { workspaces } => {
1365 if let Some((index, workspace)) = workspaces.iter().enumerate().find(|(_, w)| {
1366 w.name
1367 .as_ref()
1368 .is_some_and(|name| name.eq_ignore_ascii_case(workspace_name))
1369 }) {
1370 return Some((index, workspace));
1371 }
1372 }
1373 }
1374
1375 None
1376 }
1377
1378 pub fn find_workspace_by_ref(
1379 &mut self,
1380 reference: WorkspaceReference,
1381 ) -> Option<&mut Workspace<W>> {
1382 if let WorkspaceReference::Index(index) = reference {
1383 self.active_monitor().and_then(|m| {
1384 let index = index.saturating_sub(1) as usize;
1385 m.workspaces.get_mut(index)
1386 })
1387 } else {
1388 self.workspaces_mut().find(|ws| match &reference {
1389 WorkspaceReference::Name(ref_name) => ws
1390 .name
1391 .as_ref()
1392 .is_some_and(|name| name.eq_ignore_ascii_case(ref_name)),
1393 WorkspaceReference::Id(id) => ws.id().get() == *id,
1394 WorkspaceReference::Index(_) => unreachable!(),
1395 })
1396 }
1397 }
1398
1399 pub fn unname_workspace(&mut self, workspace_name: &str) {
1400 self.unname_workspace_by_ref(WorkspaceReference::Name(workspace_name.into()));
1401 }
1402
1403 pub fn unname_workspace_by_ref(&mut self, reference: WorkspaceReference) {
1404 let id = self.find_workspace_by_ref(reference).map(|ws| ws.id());
1405 if let Some(id) = id {
1406 self.unname_workspace_by_id(id);
1407 }
1408 }
1409
1410 pub fn unname_workspace_by_id(&mut self, id: WorkspaceId) {
1411 match &mut self.monitor_set {
1412 MonitorSet::Normal { monitors, .. } => {
1413 for mon in monitors {
1414 if mon.unname_workspace(id) {
1415 if mon.workspace_switch.is_none() {
1416 mon.clean_up_workspaces();
1417 }
1418 return;
1419 }
1420 }
1421 }
1422 MonitorSet::NoOutputs { workspaces } => {
1423 for (idx, ws) in workspaces.iter_mut().enumerate() {
1424 if ws.id() == id {
1425 ws.unname();
1426
1427 // Clean up empty workspaces.
1428 if !ws.has_windows() {
1429 workspaces.remove(idx);
1430 }
1431
1432 return;
1433 }
1434 }
1435 }
1436 }
1437 }
1438
1439 pub fn find_window_and_output(&self, wl_surface: &WlSurface) -> Option<(&W, Option<&Output>)> {
1440 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
1441 if move_.tile.window().is_wl_surface(wl_surface) {
1442 return Some((move_.tile.window(), Some(&move_.output)));
1443 }
1444 }
1445
1446 match &self.monitor_set {
1447 MonitorSet::Normal { monitors, .. } => {
1448 for mon in monitors {
1449 for ws in &mon.workspaces {
1450 if let Some(window) = ws.find_wl_surface(wl_surface) {
1451 return Some((window, Some(&mon.output)));
1452 }
1453 }
1454 }
1455 }
1456 MonitorSet::NoOutputs { workspaces } => {
1457 for ws in workspaces {
1458 if let Some(window) = ws.find_wl_surface(wl_surface) {
1459 return Some((window, None));
1460 }
1461 }
1462 }
1463 }
1464
1465 None
1466 }
1467
1468 pub fn find_window_and_output_mut(
1469 &mut self,
1470 wl_surface: &WlSurface,
1471 ) -> Option<(&mut W, Option<&Output>)> {
1472 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
1473 if move_.tile.window().is_wl_surface(wl_surface) {
1474 return Some((move_.tile.window_mut(), Some(&move_.output)));
1475 }
1476 }
1477
1478 match &mut self.monitor_set {
1479 MonitorSet::Normal { monitors, .. } => {
1480 for mon in monitors {
1481 for ws in &mut mon.workspaces {
1482 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {
1483 return Some((window, Some(&mon.output)));
1484 }
1485 }
1486 }
1487 }
1488 MonitorSet::NoOutputs { workspaces } => {
1489 for ws in workspaces {
1490 if let Some(window) = ws.find_wl_surface_mut(wl_surface) {
1491 return Some((window, None));
1492 }
1493 }
1494 }
1495 }
1496
1497 None
1498 }
1499
1500 /// Computes the window-geometry-relative target rect for popup unconstraining.
1501 ///
1502 /// We will try to fit popups inside this rect.
1503 pub fn popup_target_rect(&self, window: &W::Id) -> Rectangle<f64, Logical> {
1504 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
1505 if move_.tile.window().id() == window {
1506 // Follow the scrolling layout logic and fit the popup horizontally within the
1507 // window geometry.
1508 let width = move_.tile.window_size().w;
1509 let height = output_size(&move_.output).h;
1510 let mut target = Rectangle::from_size(Size::from((width, height)));
1511 // FIXME: ideally this shouldn't include the tile render offset, but the code
1512 // duplication would be a bit annoying for this edge case.
1513 target.loc.y -= move_.tile_render_location(1.).y;
1514 target.loc.y -= move_.tile.window_loc().y;
1515 return target;
1516 }
1517 }
1518
1519 self.workspaces()
1520 .find_map(|(_, _, ws)| ws.popup_target_rect(window))
1521 .unwrap()
1522 }
1523
1524 pub fn update_output_size(&mut self, output: &Output) {
1525 let _span = tracy_client::span!("Layout::update_output_size");
1526
1527 let Some(mon) = self.monitor_for_output_mut(output) else {
1528 error!("monitor missing in update_output_size()");
1529 return;
1530 };
1531
1532 mon.update_output_size();
1533 }
1534
1535 pub fn scroll_amount_to_activate(&self, window: &W::Id) -> f64 {
1536 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
1537 if move_.tile.window().id() == window {
1538 return 0.;
1539 }
1540 }
1541
1542 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
1543 return 0.;
1544 };
1545
1546 for mon in monitors {
1547 for ws in &mon.workspaces {
1548 if ws.has_window(window) {
1549 return ws.scroll_amount_to_activate(window);
1550 }
1551 }
1552 }
1553
1554 0.
1555 }
1556
1557 pub fn should_trigger_focus_follows_mouse_on(&self, window: &W::Id) -> bool {
1558 // During an animation, it's easy to trigger focus-follows-mouse on the previous workspace,
1559 // especially when clicking to switch workspace on a bar of some kind. This cancels the
1560 // workspace switch, which is annoying and not intended.
1561 //
1562 // This function allows focus-follows-mouse to trigger only on the animation target
1563 // workspace.
1564 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
1565 if move_.tile.window().id() == window {
1566 return true;
1567 }
1568 }
1569
1570 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
1571 return true;
1572 };
1573
1574 let (mon, ws_idx) = monitors
1575 .iter()
1576 .find_map(|mon| {
1577 mon.workspaces
1578 .iter()
1579 .position(|ws| ws.has_window(window))
1580 .map(|ws_idx| (mon, ws_idx))
1581 })
1582 .unwrap();
1583
1584 // During a gesture, focus-follows-mouse does not cause any unintended workspace switches.
1585 if let Some(WorkspaceSwitch::Gesture(_)) = mon.workspace_switch {
1586 return true;
1587 }
1588
1589 ws_idx == mon.active_workspace_idx
1590 }
1591
1592 pub fn activate_window(&mut self, window: &W::Id) {
1593 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
1594 if move_.tile.window().id() == window {
1595 return;
1596 }
1597 }
1598
1599 let MonitorSet::Normal {
1600 monitors,
1601 active_monitor_idx,
1602 ..
1603 } = &mut self.monitor_set
1604 else {
1605 return;
1606 };
1607
1608 for (monitor_idx, mon) in monitors.iter_mut().enumerate() {
1609 for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() {
1610 if ws.activate_window(window) {
1611 *active_monitor_idx = monitor_idx;
1612
1613 // If currently in the middle of a vertical swipe between the target workspace
1614 // and some other, don't switch the workspace.
1615 match &mon.workspace_switch {
1616 Some(WorkspaceSwitch::Gesture(gesture))
1617 if gesture.current_idx.floor() == workspace_idx as f64
1618 || gesture.current_idx.ceil() == workspace_idx as f64 => {}
1619 _ => mon.switch_workspace(workspace_idx),
1620 }
1621
1622 return;
1623 }
1624 }
1625 }
1626 }
1627
1628 pub fn activate_window_without_raising(&mut self, window: &W::Id) {
1629 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
1630 if move_.tile.window().id() == window {
1631 return;
1632 }
1633 }
1634
1635 let MonitorSet::Normal {
1636 monitors,
1637 active_monitor_idx,
1638 ..
1639 } = &mut self.monitor_set
1640 else {
1641 return;
1642 };
1643
1644 for (monitor_idx, mon) in monitors.iter_mut().enumerate() {
1645 for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() {
1646 if ws.activate_window_without_raising(window) {
1647 *active_monitor_idx = monitor_idx;
1648
1649 // If currently in the middle of a vertical swipe between the target workspace
1650 // and some other, don't switch the workspace.
1651 match &mon.workspace_switch {
1652 Some(WorkspaceSwitch::Gesture(gesture))
1653 if gesture.current_idx.floor() == workspace_idx as f64
1654 || gesture.current_idx.ceil() == workspace_idx as f64 => {}
1655 _ => mon.switch_workspace(workspace_idx),
1656 }
1657
1658 return;
1659 }
1660 }
1661 }
1662 }
1663
1664 pub fn active_output(&self) -> Option<&Output> {
1665 let MonitorSet::Normal {
1666 monitors,
1667 active_monitor_idx,
1668 ..
1669 } = &self.monitor_set
1670 else {
1671 return None;
1672 };
1673
1674 Some(&monitors[*active_monitor_idx].output)
1675 }
1676
1677 pub fn active_workspace(&self) -> Option<&Workspace<W>> {
1678 let MonitorSet::Normal {
1679 monitors,
1680 active_monitor_idx,
1681 ..
1682 } = &self.monitor_set
1683 else {
1684 return None;
1685 };
1686
1687 let mon = &monitors[*active_monitor_idx];
1688 Some(&mon.workspaces[mon.active_workspace_idx])
1689 }
1690
1691 pub fn active_workspace_mut(&mut self) -> Option<&mut Workspace<W>> {
1692 let MonitorSet::Normal {
1693 monitors,
1694 active_monitor_idx,
1695 ..
1696 } = &mut self.monitor_set
1697 else {
1698 return None;
1699 };
1700
1701 let mon = &mut monitors[*active_monitor_idx];
1702 Some(&mut mon.workspaces[mon.active_workspace_idx])
1703 }
1704
1705 pub fn windows_for_output(&self, output: &Output) -> impl Iterator<Item = &W> + '_ {
1706 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
1707 panic!()
1708 };
1709
1710 let moving_window = self
1711 .interactive_move
1712 .as_ref()
1713 .and_then(|x| x.moving())
1714 .filter(|move_| move_.output == *output)
1715 .map(|move_| move_.tile.window())
1716 .into_iter();
1717
1718 let mon = monitors.iter().find(|mon| &mon.output == output).unwrap();
1719 let mon_windows = mon.workspaces.iter().flat_map(|ws| ws.windows());
1720
1721 moving_window.chain(mon_windows)
1722 }
1723
1724 pub fn windows_for_output_mut(&mut self, output: &Output) -> impl Iterator<Item = &mut W> + '_ {
1725 let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else {
1726 panic!()
1727 };
1728
1729 let moving_window = self
1730 .interactive_move
1731 .as_mut()
1732 .and_then(|x| x.moving_mut())
1733 .filter(|move_| move_.output == *output)
1734 .map(|move_| move_.tile.window_mut())
1735 .into_iter();
1736
1737 let mon = monitors
1738 .iter_mut()
1739 .find(|mon| &mon.output == output)
1740 .unwrap();
1741 let mon_windows = mon.workspaces.iter_mut().flat_map(|ws| ws.windows_mut());
1742
1743 moving_window.chain(mon_windows)
1744 }
1745
1746 pub fn with_windows(&self, mut f: impl FnMut(&W, Option<&Output>, Option<WorkspaceId>)) {
1747 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
1748 f(move_.tile.window(), Some(&move_.output), None);
1749 }
1750
1751 match &self.monitor_set {
1752 MonitorSet::Normal { monitors, .. } => {
1753 for mon in monitors {
1754 for ws in &mon.workspaces {
1755 for win in ws.windows() {
1756 f(win, Some(&mon.output), Some(ws.id()));
1757 }
1758 }
1759 }
1760 }
1761 MonitorSet::NoOutputs { workspaces } => {
1762 for ws in workspaces {
1763 for win in ws.windows() {
1764 f(win, None, Some(ws.id()));
1765 }
1766 }
1767 }
1768 }
1769 }
1770
1771 pub fn with_windows_mut(&mut self, mut f: impl FnMut(&mut W, Option<&Output>)) {
1772 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
1773 f(move_.tile.window_mut(), Some(&move_.output));
1774 }
1775
1776 match &mut self.monitor_set {
1777 MonitorSet::Normal { monitors, .. } => {
1778 for mon in monitors {
1779 for ws in &mut mon.workspaces {
1780 for win in ws.windows_mut() {
1781 f(win, Some(&mon.output));
1782 }
1783 }
1784 }
1785 }
1786 MonitorSet::NoOutputs { workspaces } => {
1787 for ws in workspaces {
1788 for win in ws.windows_mut() {
1789 f(win, None);
1790 }
1791 }
1792 }
1793 }
1794 }
1795
1796 fn active_monitor(&mut self) -> Option<&mut Monitor<W>> {
1797 let MonitorSet::Normal {
1798 monitors,
1799 active_monitor_idx,
1800 ..
1801 } = &mut self.monitor_set
1802 else {
1803 return None;
1804 };
1805
1806 Some(&mut monitors[*active_monitor_idx])
1807 }
1808
1809 pub fn active_monitor_ref(&self) -> Option<&Monitor<W>> {
1810 let MonitorSet::Normal {
1811 monitors,
1812 active_monitor_idx,
1813 ..
1814 } = &self.monitor_set
1815 else {
1816 return None;
1817 };
1818
1819 Some(&monitors[*active_monitor_idx])
1820 }
1821
1822 pub fn monitor_for_output(&self, output: &Output) -> Option<&Monitor<W>> {
1823 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
1824 return None;
1825 };
1826
1827 monitors.iter().find(|mon| &mon.output == output)
1828 }
1829
1830 pub fn monitor_for_output_mut(&mut self, output: &Output) -> Option<&mut Monitor<W>> {
1831 let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else {
1832 return None;
1833 };
1834
1835 monitors.iter_mut().find(|mon| &mon.output == output)
1836 }
1837
1838 pub fn monitor_for_workspace(&self, workspace_name: &str) -> Option<&Monitor<W>> {
1839 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
1840 return None;
1841 };
1842
1843 monitors.iter().find(|monitor| {
1844 monitor.workspaces.iter().any(|ws| {
1845 ws.name
1846 .as_ref()
1847 .is_some_and(|name| name.eq_ignore_ascii_case(workspace_name))
1848 })
1849 })
1850 }
1851
1852 pub fn outputs(&self) -> impl Iterator<Item = &Output> + '_ {
1853 let monitors = if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
1854 &monitors[..]
1855 } else {
1856 &[][..]
1857 };
1858
1859 monitors.iter().map(|mon| &mon.output)
1860 }
1861
1862 pub fn move_left(&mut self) {
1863 let Some(workspace) = self.active_workspace_mut() else {
1864 return;
1865 };
1866 workspace.move_left();
1867 }
1868
1869 pub fn move_right(&mut self) {
1870 let Some(workspace) = self.active_workspace_mut() else {
1871 return;
1872 };
1873 workspace.move_right();
1874 }
1875
1876 pub fn move_column_to_first(&mut self) {
1877 let Some(workspace) = self.active_workspace_mut() else {
1878 return;
1879 };
1880 workspace.move_column_to_first();
1881 }
1882
1883 pub fn move_column_to_last(&mut self) {
1884 let Some(workspace) = self.active_workspace_mut() else {
1885 return;
1886 };
1887 workspace.move_column_to_last();
1888 }
1889
1890 pub fn move_column_left_or_to_output(&mut self, output: &Output) -> bool {
1891 if let Some(workspace) = self.active_workspace_mut() {
1892 if workspace.move_left() {
1893 return false;
1894 }
1895 }
1896
1897 self.move_column_to_output(output, None, true);
1898 true
1899 }
1900
1901 pub fn move_column_right_or_to_output(&mut self, output: &Output) -> bool {
1902 if let Some(workspace) = self.active_workspace_mut() {
1903 if workspace.move_right() {
1904 return false;
1905 }
1906 }
1907
1908 self.move_column_to_output(output, None, true);
1909 true
1910 }
1911
1912 pub fn move_column_to_index(&mut self, index: usize) {
1913 let Some(workspace) = self.active_workspace_mut() else {
1914 return;
1915 };
1916 workspace.move_column_to_index(index);
1917 }
1918
1919 pub fn move_down(&mut self) {
1920 let Some(workspace) = self.active_workspace_mut() else {
1921 return;
1922 };
1923 workspace.move_down();
1924 }
1925
1926 pub fn move_up(&mut self) {
1927 let Some(workspace) = self.active_workspace_mut() else {
1928 return;
1929 };
1930 workspace.move_up();
1931 }
1932
1933 pub fn move_down_or_to_workspace_down(&mut self) {
1934 let Some(monitor) = self.active_monitor() else {
1935 return;
1936 };
1937 monitor.move_down_or_to_workspace_down();
1938 }
1939
1940 pub fn move_up_or_to_workspace_up(&mut self) {
1941 let Some(monitor) = self.active_monitor() else {
1942 return;
1943 };
1944 monitor.move_up_or_to_workspace_up();
1945 }
1946
1947 pub fn consume_or_expel_window_left(&mut self, window: Option<&W::Id>) {
1948 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
1949 if window.is_none() || window == Some(move_.tile.window().id()) {
1950 return;
1951 }
1952 }
1953
1954 let workspace = if let Some(window) = window {
1955 Some(
1956 self.workspaces_mut()
1957 .find(|ws| ws.has_window(window))
1958 .unwrap(),
1959 )
1960 } else {
1961 self.active_workspace_mut()
1962 };
1963
1964 let Some(workspace) = workspace else {
1965 return;
1966 };
1967 workspace.consume_or_expel_window_left(window);
1968 }
1969
1970 pub fn consume_or_expel_window_right(&mut self, window: Option<&W::Id>) {
1971 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
1972 if window.is_none() || window == Some(move_.tile.window().id()) {
1973 return;
1974 }
1975 }
1976
1977 let workspace = if let Some(window) = window {
1978 Some(
1979 self.workspaces_mut()
1980 .find(|ws| ws.has_window(window))
1981 .unwrap(),
1982 )
1983 } else {
1984 self.active_workspace_mut()
1985 };
1986
1987 let Some(workspace) = workspace else {
1988 return;
1989 };
1990 workspace.consume_or_expel_window_right(window);
1991 }
1992
1993 pub fn focus_left(&mut self) {
1994 let Some(workspace) = self.active_workspace_mut() else {
1995 return;
1996 };
1997 workspace.focus_left();
1998 }
1999
2000 pub fn focus_right(&mut self) {
2001 let Some(workspace) = self.active_workspace_mut() else {
2002 return;
2003 };
2004 workspace.focus_right();
2005 }
2006
2007 pub fn focus_column_first(&mut self) {
2008 let Some(workspace) = self.active_workspace_mut() else {
2009 return;
2010 };
2011 workspace.focus_column_first();
2012 }
2013
2014 pub fn focus_column_last(&mut self) {
2015 let Some(workspace) = self.active_workspace_mut() else {
2016 return;
2017 };
2018 workspace.focus_column_last();
2019 }
2020
2021 pub fn focus_column_right_or_first(&mut self) {
2022 let Some(workspace) = self.active_workspace_mut() else {
2023 return;
2024 };
2025 workspace.focus_column_right_or_first();
2026 }
2027
2028 pub fn focus_column_left_or_last(&mut self) {
2029 let Some(workspace) = self.active_workspace_mut() else {
2030 return;
2031 };
2032 workspace.focus_column_left_or_last();
2033 }
2034
2035 pub fn focus_column(&mut self, index: usize) {
2036 let Some(workspace) = self.active_workspace_mut() else {
2037 return;
2038 };
2039 workspace.focus_column(index);
2040 }
2041
2042 pub fn focus_window_up_or_output(&mut self, output: &Output) -> bool {
2043 if let Some(workspace) = self.active_workspace_mut() {
2044 if workspace.focus_up() {
2045 return false;
2046 }
2047 }
2048
2049 self.focus_output(output);
2050 true
2051 }
2052
2053 pub fn focus_window_down_or_output(&mut self, output: &Output) -> bool {
2054 if let Some(workspace) = self.active_workspace_mut() {
2055 if workspace.focus_down() {
2056 return false;
2057 }
2058 }
2059
2060 self.focus_output(output);
2061 true
2062 }
2063
2064 pub fn focus_column_left_or_output(&mut self, output: &Output) -> bool {
2065 if let Some(workspace) = self.active_workspace_mut() {
2066 if workspace.focus_left() {
2067 return false;
2068 }
2069 }
2070
2071 self.focus_output(output);
2072 true
2073 }
2074
2075 pub fn focus_column_right_or_output(&mut self, output: &Output) -> bool {
2076 if let Some(workspace) = self.active_workspace_mut() {
2077 if workspace.focus_right() {
2078 return false;
2079 }
2080 }
2081
2082 self.focus_output(output);
2083 true
2084 }
2085
2086 pub fn focus_window_in_column(&mut self, index: u8) {
2087 let Some(workspace) = self.active_workspace_mut() else {
2088 return;
2089 };
2090 workspace.focus_window_in_column(index);
2091 }
2092
2093 pub fn focus_down(&mut self) {
2094 let Some(workspace) = self.active_workspace_mut() else {
2095 return;
2096 };
2097 workspace.focus_down();
2098 }
2099
2100 pub fn focus_up(&mut self) {
2101 let Some(workspace) = self.active_workspace_mut() else {
2102 return;
2103 };
2104 workspace.focus_up();
2105 }
2106
2107 pub fn focus_down_or_left(&mut self) {
2108 let Some(workspace) = self.active_workspace_mut() else {
2109 return;
2110 };
2111 workspace.focus_down_or_left();
2112 }
2113
2114 pub fn focus_down_or_right(&mut self) {
2115 let Some(workspace) = self.active_workspace_mut() else {
2116 return;
2117 };
2118 workspace.focus_down_or_right();
2119 }
2120
2121 pub fn focus_up_or_left(&mut self) {
2122 let Some(workspace) = self.active_workspace_mut() else {
2123 return;
2124 };
2125 workspace.focus_up_or_left();
2126 }
2127
2128 pub fn focus_up_or_right(&mut self) {
2129 let Some(workspace) = self.active_workspace_mut() else {
2130 return;
2131 };
2132 workspace.focus_up_or_right();
2133 }
2134
2135 pub fn focus_window_or_workspace_down(&mut self) {
2136 let Some(monitor) = self.active_monitor() else {
2137 return;
2138 };
2139 monitor.focus_window_or_workspace_down();
2140 }
2141
2142 pub fn focus_window_or_workspace_up(&mut self) {
2143 let Some(monitor) = self.active_monitor() else {
2144 return;
2145 };
2146 monitor.focus_window_or_workspace_up();
2147 }
2148
2149 pub fn focus_window_top(&mut self) {
2150 let Some(workspace) = self.active_workspace_mut() else {
2151 return;
2152 };
2153 workspace.focus_window_top();
2154 }
2155
2156 pub fn focus_window_bottom(&mut self) {
2157 let Some(workspace) = self.active_workspace_mut() else {
2158 return;
2159 };
2160 workspace.focus_window_bottom();
2161 }
2162
2163 pub fn focus_window_down_or_top(&mut self) {
2164 let Some(workspace) = self.active_workspace_mut() else {
2165 return;
2166 };
2167 workspace.focus_window_down_or_top();
2168 }
2169
2170 pub fn focus_window_up_or_bottom(&mut self) {
2171 let Some(workspace) = self.active_workspace_mut() else {
2172 return;
2173 };
2174 workspace.focus_window_up_or_bottom();
2175 }
2176
2177 pub fn move_to_workspace_up(&mut self) {
2178 let Some(monitor) = self.active_monitor() else {
2179 return;
2180 };
2181 monitor.move_to_workspace_up();
2182 }
2183
2184 pub fn move_to_workspace_down(&mut self) {
2185 let Some(monitor) = self.active_monitor() else {
2186 return;
2187 };
2188 monitor.move_to_workspace_down();
2189 }
2190
2191 pub fn move_to_workspace(
2192 &mut self,
2193 window: Option<&W::Id>,
2194 idx: usize,
2195 activate: ActivateWindow,
2196 ) {
2197 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
2198 if window.is_none() || window == Some(move_.tile.window().id()) {
2199 return;
2200 }
2201 }
2202
2203 let monitor = if let Some(window) = window {
2204 match &mut self.monitor_set {
2205 MonitorSet::Normal { monitors, .. } => monitors
2206 .iter_mut()
2207 .find(|mon| mon.has_window(window))
2208 .unwrap(),
2209 MonitorSet::NoOutputs { .. } => {
2210 return;
2211 }
2212 }
2213 } else {
2214 let Some(monitor) = self.active_monitor() else {
2215 return;
2216 };
2217 monitor
2218 };
2219 monitor.move_to_workspace(window, idx, activate);
2220 }
2221
2222 pub fn move_column_to_workspace_up(&mut self, activate: bool) {
2223 let Some(monitor) = self.active_monitor() else {
2224 return;
2225 };
2226 monitor.move_column_to_workspace_up(activate);
2227 }
2228
2229 pub fn move_column_to_workspace_down(&mut self, activate: bool) {
2230 let Some(monitor) = self.active_monitor() else {
2231 return;
2232 };
2233 monitor.move_column_to_workspace_down(activate);
2234 }
2235
2236 pub fn move_column_to_workspace(&mut self, idx: usize, activate: bool) {
2237 let Some(monitor) = self.active_monitor() else {
2238 return;
2239 };
2240 monitor.move_column_to_workspace(idx, activate);
2241 }
2242
2243 pub fn switch_workspace_up(&mut self) {
2244 let Some(monitor) = self.active_monitor() else {
2245 return;
2246 };
2247 monitor.switch_workspace_up();
2248 }
2249
2250 pub fn switch_workspace_down(&mut self) {
2251 let Some(monitor) = self.active_monitor() else {
2252 return;
2253 };
2254 monitor.switch_workspace_down();
2255 }
2256
2257 pub fn switch_workspace(&mut self, idx: usize) {
2258 let Some(monitor) = self.active_monitor() else {
2259 return;
2260 };
2261 monitor.switch_workspace(idx);
2262 }
2263
2264 pub fn switch_workspace_auto_back_and_forth(&mut self, idx: usize) {
2265 let Some(monitor) = self.active_monitor() else {
2266 return;
2267 };
2268 monitor.switch_workspace_auto_back_and_forth(idx);
2269 }
2270
2271 pub fn switch_workspace_previous(&mut self) {
2272 let Some(monitor) = self.active_monitor() else {
2273 return;
2274 };
2275 monitor.switch_workspace_previous();
2276 }
2277
2278 pub fn consume_into_column(&mut self) {
2279 let Some(workspace) = self.active_workspace_mut() else {
2280 return;
2281 };
2282 workspace.consume_into_column();
2283 }
2284
2285 pub fn expel_from_column(&mut self) {
2286 let Some(workspace) = self.active_workspace_mut() else {
2287 return;
2288 };
2289 workspace.expel_from_column();
2290 }
2291
2292 pub fn swap_window_in_direction(&mut self, direction: ScrollDirection) {
2293 let Some(workspace) = self.active_workspace_mut() else {
2294 return;
2295 };
2296 workspace.swap_window_in_direction(direction);
2297 }
2298
2299 pub fn toggle_column_tabbed_display(&mut self) {
2300 let Some(workspace) = self.active_workspace_mut() else {
2301 return;
2302 };
2303 workspace.toggle_column_tabbed_display();
2304 }
2305
2306 pub fn set_column_display(&mut self, display: ColumnDisplay) {
2307 let Some(workspace) = self.active_workspace_mut() else {
2308 return;
2309 };
2310 workspace.set_column_display(display);
2311 }
2312
2313 pub fn center_column(&mut self) {
2314 let Some(workspace) = self.active_workspace_mut() else {
2315 return;
2316 };
2317 workspace.center_column();
2318 }
2319
2320 pub fn center_window(&mut self, id: Option<&W::Id>) {
2321 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
2322 if id.is_none() || id == Some(move_.tile.window().id()) {
2323 return;
2324 }
2325 }
2326
2327 let workspace = if let Some(id) = id {
2328 Some(self.workspaces_mut().find(|ws| ws.has_window(id)).unwrap())
2329 } else {
2330 self.active_workspace_mut()
2331 };
2332
2333 let Some(workspace) = workspace else {
2334 return;
2335 };
2336 workspace.center_window(id);
2337 }
2338
2339 pub fn center_visible_columns(&mut self) {
2340 let Some(workspace) = self.active_workspace_mut() else {
2341 return;
2342 };
2343 workspace.center_visible_columns();
2344 }
2345
2346 pub fn focus(&self) -> Option<&W> {
2347 self.focus_with_output().map(|(win, _out)| win)
2348 }
2349
2350 pub fn focus_with_output(&self) -> Option<(&W, &Output)> {
2351 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
2352 return Some((move_.tile.window(), &move_.output));
2353 }
2354
2355 let MonitorSet::Normal {
2356 monitors,
2357 active_monitor_idx,
2358 ..
2359 } = &self.monitor_set
2360 else {
2361 return None;
2362 };
2363
2364 let mon = &monitors[*active_monitor_idx];
2365 mon.active_window().map(|win| (win, &mon.output))
2366 }
2367
2368 pub fn interactive_moved_window_under(
2369 &self,
2370 output: &Output,
2371 pos_within_output: Point<f64, Logical>,
2372 ) -> Option<(&W, HitType)> {
2373 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
2374 if move_.output == *output {
2375 if self.overview_progress.is_some() {
2376 let zoom = self.overview_zoom();
2377 let tile_pos = move_.tile_render_location(zoom);
2378 let pos_within_tile = (pos_within_output - tile_pos).downscale(zoom);
2379 // During the overview animation, we cannot do input hits because we cannot
2380 // really represent scaled windows properly.
2381 let (win, hit) =
2382 HitType::hit_tile(&move_.tile, Point::from((0., 0.)), pos_within_tile)?;
2383 Some((win, hit.to_activate()))
2384 } else {
2385 let tile_pos = move_.tile_render_location(1.);
2386 HitType::hit_tile(&move_.tile, tile_pos, pos_within_output)
2387 }
2388 } else {
2389 None
2390 }
2391 } else {
2392 None
2393 }
2394 }
2395
2396 /// Returns the window under the cursor and the hit type.
2397 pub fn window_under(
2398 &self,
2399 output: &Output,
2400 pos_within_output: Point<f64, Logical>,
2401 ) -> Option<(&W, HitType)> {
2402 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
2403 return None;
2404 };
2405
2406 let mon = monitors.iter().find(|mon| &mon.output == output)?;
2407 mon.window_under(pos_within_output)
2408 }
2409
2410 pub fn resize_edges_under(
2411 &self,
2412 output: &Output,
2413 pos_within_output: Point<f64, Logical>,
2414 ) -> Option<ResizeEdge> {
2415 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
2416 return None;
2417 };
2418
2419 let mon = monitors.iter().find(|mon| &mon.output == output)?;
2420 mon.resize_edges_under(pos_within_output)
2421 }
2422
2423 pub fn workspace_under(
2424 &self,
2425 extended_bounds: bool,
2426 output: &Output,
2427 pos_within_output: Point<f64, Logical>,
2428 ) -> Option<&Workspace<W>> {
2429 if self
2430 .interactive_moved_window_under(output, pos_within_output)
2431 .is_some()
2432 {
2433 return None;
2434 }
2435
2436 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
2437 return None;
2438 };
2439
2440 let mon = monitors.iter().find(|mon| &mon.output == output)?;
2441 if extended_bounds {
2442 mon.workspace_under(pos_within_output).map(|(ws, _)| ws)
2443 } else {
2444 mon.workspace_under_narrow(pos_within_output)
2445 }
2446 }
2447
2448 pub fn overview_zoom(&self) -> f64 {
2449 let progress = self.overview_progress.as_ref().map(|p| p.value());
2450 compute_overview_zoom(&self.options, progress)
2451 }
2452
2453 #[cfg(test)]
2454 fn verify_invariants(&self) {
2455 use std::collections::HashSet;
2456
2457 use approx::assert_abs_diff_eq;
2458
2459 use crate::layout::monitor::WorkspaceSwitch;
2460
2461 let zoom = self.overview_zoom();
2462
2463 let mut move_win_id = None;
2464 if let Some(state) = &self.interactive_move {
2465 match state {
2466 InteractiveMoveState::Starting {
2467 window_id,
2468 pointer_delta: _,
2469 pointer_ratio_within_window: _,
2470 } => {
2471 assert!(
2472 self.has_window(window_id),
2473 "interactive move must be on an existing window"
2474 );
2475 move_win_id = Some(window_id.clone());
2476 }
2477 InteractiveMoveState::Moving(move_) => {
2478 assert_eq!(self.clock, move_.tile.clock);
2479 assert!(!move_.tile.window().is_pending_fullscreen());
2480
2481 move_.tile.verify_invariants();
2482
2483 let scale = move_.output.current_scale().fractional_scale();
2484 let options = Options::clone(&self.options).adjusted_for_scale(scale);
2485 assert_eq!(
2486 &*move_.tile.options, &options,
2487 "interactive moved tile options must be \
2488 base options adjusted for output scale"
2489 );
2490
2491 let tile_pos = move_.tile_render_location(zoom);
2492 let rounded_pos = tile_pos.to_physical_precise_round(scale).to_logical(scale);
2493
2494 // Tile position must be rounded to physical pixels.
2495 assert_abs_diff_eq!(tile_pos.x, rounded_pos.x, epsilon = 1e-5);
2496 assert_abs_diff_eq!(tile_pos.y, rounded_pos.y, epsilon = 1e-5);
2497
2498 if let Some(alpha) = &move_.tile.alpha_animation {
2499 if move_.is_floating {
2500 assert_eq!(
2501 alpha.anim.to(),
2502 1.,
2503 "interactively moved floating tile can animate alpha only to 1"
2504 );
2505
2506 assert!(
2507 !alpha.hold_after_done,
2508 "interactively moved floating tile \
2509 cannot have held alpha animation"
2510 );
2511 } else {
2512 assert_ne!(
2513 alpha.anim.to(),
2514 1.,
2515 "interactively moved scrolling tile must animate alpha to not 1"
2516 );
2517
2518 assert!(
2519 alpha.hold_after_done,
2520 "interactively moved scrolling tile \
2521 must have held alpha animation"
2522 );
2523 }
2524 }
2525 }
2526 }
2527 }
2528
2529 let mut seen_workspace_id = HashSet::new();
2530 let mut seen_workspace_name = Vec::<String>::new();
2531
2532 let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set {
2533 MonitorSet::Normal {
2534 monitors,
2535 primary_idx,
2536 active_monitor_idx,
2537 } => (monitors, primary_idx, active_monitor_idx),
2538 MonitorSet::NoOutputs { workspaces } => {
2539 for workspace in workspaces {
2540 assert!(
2541 workspace.has_windows_or_name(),
2542 "with no outputs there cannot be empty unnamed workspaces"
2543 );
2544
2545 assert_eq!(self.clock, workspace.clock);
2546
2547 assert_eq!(
2548 workspace.base_options, self.options,
2549 "workspace base options must be synchronized with layout"
2550 );
2551
2552 let options = Options::clone(&workspace.base_options)
2553 .adjusted_for_scale(workspace.scale().fractional_scale());
2554 assert_eq!(
2555 &*workspace.options, &options,
2556 "workspace options must be base options adjusted for workspace scale"
2557 );
2558
2559 assert!(
2560 seen_workspace_id.insert(workspace.id()),
2561 "workspace id must be unique"
2562 );
2563
2564 if let Some(name) = &workspace.name {
2565 assert!(
2566 !seen_workspace_name
2567 .iter()
2568 .any(|n| n.eq_ignore_ascii_case(name)),
2569 "workspace name must be unique"
2570 );
2571 seen_workspace_name.push(name.clone());
2572 }
2573
2574 workspace.verify_invariants(move_win_id.as_ref());
2575 }
2576
2577 return;
2578 }
2579 };
2580
2581 assert!(primary_idx < monitors.len());
2582 assert!(active_monitor_idx < monitors.len());
2583
2584 let mut saw_view_offset_gesture = false;
2585
2586 for (idx, monitor) in monitors.iter().enumerate() {
2587 assert!(
2588 !monitor.workspaces.is_empty(),
2589 "monitor must have at least one workspace"
2590 );
2591 assert!(monitor.active_workspace_idx < monitor.workspaces.len());
2592
2593 assert_eq!(self.clock, monitor.clock);
2594 assert_eq!(
2595 monitor.options, self.options,
2596 "monitor options must be synchronized with layout"
2597 );
2598
2599 assert_eq!(self.overview_open, monitor.overview_open);
2600 assert_eq!(
2601 self.overview_progress.as_ref().map(|p| p.value()),
2602 monitor.overview_progress_value()
2603 );
2604
2605 if let Some(WorkspaceSwitch::Animation(anim)) = &monitor.workspace_switch {
2606 let before_idx = anim.from() as usize;
2607 let after_idx = anim.to() as usize;
2608
2609 assert!(before_idx < monitor.workspaces.len());
2610 assert!(after_idx < monitor.workspaces.len());
2611 }
2612
2613 if idx == primary_idx {
2614 for ws in &monitor.workspaces {
2615 if ws.original_output.matches(&monitor.output) {
2616 // This is the primary monitor's own workspace.
2617 continue;
2618 }
2619
2620 let own_monitor_exists = monitors
2621 .iter()
2622 .any(|m| ws.original_output.matches(&m.output));
2623 assert!(
2624 !own_monitor_exists,
2625 "primary monitor cannot have workspaces for which their own monitor exists"
2626 );
2627 }
2628 } else {
2629 assert!(
2630 monitor
2631 .workspaces
2632 .iter()
2633 .any(|workspace| workspace.original_output.matches(&monitor.output)),
2634 "secondary monitor must not have any non-own workspaces"
2635 );
2636 }
2637
2638 assert!(
2639 !monitor.workspaces.last().unwrap().has_windows(),
2640 "monitor must have an empty workspace in the end"
2641 );
2642 if monitor.options.empty_workspace_above_first {
2643 assert!(
2644 !monitor.workspaces.first().unwrap().has_windows(),
2645 "first workspace must be empty when empty_workspace_above_first is set"
2646 )
2647 }
2648
2649 assert!(
2650 monitor.workspaces.last().unwrap().name.is_none(),
2651 "monitor must have an unnamed workspace in the end"
2652 );
2653 if monitor.options.empty_workspace_above_first {
2654 assert!(
2655 monitor.workspaces.first().unwrap().name.is_none(),
2656 "first workspace must be unnamed when empty_workspace_above_first is set"
2657 )
2658 }
2659
2660 if monitor.options.empty_workspace_above_first {
2661 assert!(
2662 monitor.workspaces.len() != 2,
2663 "if empty_workspace_above_first is set there must be just 1 or 3+ workspaces"
2664 )
2665 }
2666
2667 // If there's no workspace switch in progress, there can't be any non-last non-active
2668 // empty workspaces. If empty_workspace_above_first is set then the first workspace
2669 // will be empty too.
2670 let pre_skip = if monitor.options.empty_workspace_above_first {
2671 1
2672 } else {
2673 0
2674 };
2675 if monitor.workspace_switch.is_none() {
2676 for (idx, ws) in monitor
2677 .workspaces
2678 .iter()
2679 .enumerate()
2680 .skip(pre_skip)
2681 .rev()
2682 // skip last
2683 .skip(1)
2684 {
2685 if idx != monitor.active_workspace_idx {
2686 assert!(
2687 ws.has_windows_or_name(),
2688 "non-active workspace can't be empty and unnamed except the last one"
2689 );
2690 }
2691 }
2692 }
2693
2694 // FIXME: verify that primary doesn't have any workspaces for which their own monitor
2695 // exists.
2696
2697 for workspace in &monitor.workspaces {
2698 assert_eq!(self.clock, workspace.clock);
2699
2700 assert_eq!(
2701 monitor.scale().integer_scale(),
2702 workspace.scale().integer_scale()
2703 );
2704 assert_eq!(
2705 monitor.scale().fractional_scale(),
2706 workspace.scale().fractional_scale()
2707 );
2708 assert_eq!(monitor.view_size(), workspace.view_size());
2709 assert_eq!(monitor.working_area(), workspace.working_area());
2710
2711 assert_eq!(
2712 workspace.base_options, self.options,
2713 "workspace options must be synchronized with layout"
2714 );
2715
2716 let options = Options::clone(&workspace.base_options)
2717 .adjusted_for_scale(workspace.scale().fractional_scale());
2718 assert_eq!(
2719 &*workspace.options, &options,
2720 "workspace options must be base options adjusted for workspace scale"
2721 );
2722
2723 assert!(
2724 seen_workspace_id.insert(workspace.id()),
2725 "workspace id must be unique"
2726 );
2727
2728 if let Some(name) = &workspace.name {
2729 assert!(
2730 !seen_workspace_name
2731 .iter()
2732 .any(|n| n.eq_ignore_ascii_case(name)),
2733 "workspace name must be unique"
2734 );
2735 seen_workspace_name.push(name.clone());
2736 }
2737
2738 workspace.verify_invariants(move_win_id.as_ref());
2739
2740 let has_view_offset_gesture = workspace.scrolling().view_offset().is_gesture();
2741 if self.dnd.is_some() || self.interactive_move.is_some() {
2742 // We'd like to check that all workspaces have the gesture here, furthermore we
2743 // want to check that they have the gesture only if the interactive move
2744 // targets the scrolling layout. However, we cannot do that because we start
2745 // and stop the gesture lazily. Otherwise the gesture code would pollute a lot
2746 // of places like adding new workspaces, implicitly moving windows between
2747 // floating and tiling on fullscreen, etc.
2748 //
2749 // assert!(
2750 // has_view_offset_gesture,
2751 // "during an interactive move in the scrolling layout, \
2752 // all workspaces should be in a view offset gesture"
2753 // );
2754 } else if saw_view_offset_gesture {
2755 assert!(
2756 !has_view_offset_gesture,
2757 "only one workspace can have an ongoing view offset gesture"
2758 );
2759 }
2760 saw_view_offset_gesture = has_view_offset_gesture;
2761 }
2762
2763 let scale = monitor.scale().fractional_scale();
2764 let iter = monitor.workspaces_with_render_geo();
2765 for (_ws, ws_geo) in iter {
2766 let pos = ws_geo.loc;
2767 let rounded_pos = pos.to_physical_precise_round(scale).to_logical(scale);
2768
2769 // Workspace positions must be rounded to physical pixels.
2770 assert_abs_diff_eq!(pos.x, rounded_pos.x, epsilon = 1e-5);
2771 assert_abs_diff_eq!(pos.y, rounded_pos.y, epsilon = 1e-5);
2772 }
2773 }
2774 }
2775
2776 pub fn advance_animations(&mut self) {
2777 let _span = tracy_client::span!("Layout::advance_animations");
2778
2779 let mut dnd_scroll = None;
2780 let mut is_dnd = false;
2781 if let Some(dnd) = &self.dnd {
2782 dnd_scroll = Some((dnd.output.clone(), dnd.pointer_pos_within_output, true));
2783 is_dnd = true;
2784 }
2785
2786 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
2787 move_.tile.advance_animations();
2788
2789 if dnd_scroll.is_none() {
2790 dnd_scroll = Some((
2791 move_.output.clone(),
2792 move_.pointer_pos_within_output,
2793 !move_.is_floating,
2794 ));
2795 }
2796 }
2797
2798 let is_overview_open = self.overview_open;
2799
2800 // Scroll the view if needed.
2801 if let Some((output, pos_within_output, is_scrolling)) = dnd_scroll {
2802 if let Some(mon) = self.monitor_for_output_mut(&output) {
2803 let mut scrolled = false;
2804
2805 let zoom = mon.overview_zoom();
2806 scrolled |= mon.dnd_scroll_gesture_scroll(pos_within_output, 1. / zoom);
2807
2808 if is_scrolling {
2809 if let Some((ws, geo)) = mon.workspace_under(pos_within_output) {
2810 let ws_id = ws.id();
2811 let ws = mon
2812 .workspaces
2813 .iter_mut()
2814 .find(|ws| ws.id() == ws_id)
2815 .unwrap();
2816 // As far as the DnD scroll gesture is concerned, the workspace spans across
2817 // the whole monitor horizontally.
2818 let ws_pos = Point::from((0., geo.loc.y));
2819 scrolled |=
2820 ws.dnd_scroll_gesture_scroll(pos_within_output - ws_pos, 1. / zoom);
2821 }
2822 }
2823
2824 if scrolled {
2825 // Don't trigger DnD hold while scrolling.
2826 if let Some(dnd) = &mut self.dnd {
2827 dnd.hold = None;
2828 }
2829 } else if is_dnd {
2830 let target = mon
2831 .window_under(pos_within_output)
2832 .map(|(win, _)| DndHoldTarget::Window(win.id().clone()))
2833 .or_else(|| {
2834 mon.workspace_under_narrow(pos_within_output)
2835 .map(|ws| DndHoldTarget::Workspace(ws.id()))
2836 });
2837
2838 let dnd = self.dnd.as_mut().unwrap();
2839 if let Some(target) = target {
2840 let now = self.clock.now_unadjusted();
2841 let start_time = if let Some(hold) = &mut dnd.hold {
2842 if hold.target != target {
2843 hold.start_time = now;
2844 }
2845 hold.target = target;
2846 hold.start_time
2847 } else {
2848 let hold = dnd.hold.insert(DndHold {
2849 start_time: now,
2850 target,
2851 });
2852 hold.start_time
2853 };
2854
2855 // Delay copied from gnome-shell.
2856 let delay = Duration::from_millis(750);
2857 if delay <= now.saturating_sub(start_time) {
2858 let hold = dnd.hold.take().unwrap();
2859
2860 // Synchronize workspace switch to overview close to get a monotonic
2861 // animation.
2862 let config = is_overview_open
2863 .then_some(self.options.animations.overview_open_close.0);
2864
2865 let mon = self.monitor_for_output_mut(&output).unwrap();
2866
2867 let ws_idx = match hold.target {
2868 DndHoldTarget::Window(id) => mon
2869 .workspaces
2870 .iter_mut()
2871 .position(|ws| ws.activate_window(&id))
2872 .unwrap(),
2873 DndHoldTarget::Workspace(id) => {
2874 mon.workspaces.iter().position(|ws| ws.id() == id).unwrap()
2875 }
2876 };
2877
2878 mon.dnd_scroll_gesture_end();
2879 mon.activate_workspace_with_anim_config(ws_idx, config);
2880
2881 self.focus_output(&output);
2882
2883 if is_overview_open {
2884 self.close_overview();
2885 }
2886 }
2887 } else {
2888 // No target, reset the hold timer.
2889 dnd.hold = None;
2890 }
2891 }
2892 }
2893 }
2894
2895 if !self.overview_open {
2896 if let Some(OverviewProgress::Animation(anim)) = &mut self.overview_progress {
2897 if anim.is_done() {
2898 self.overview_progress = None;
2899 }
2900 }
2901 }
2902
2903 match &mut self.monitor_set {
2904 MonitorSet::Normal { monitors, .. } => {
2905 for mon in monitors {
2906 mon.set_overview_progress(self.overview_progress.as_ref());
2907 mon.advance_animations();
2908 }
2909 }
2910 MonitorSet::NoOutputs { workspaces, .. } => {
2911 for ws in workspaces {
2912 ws.advance_animations();
2913 }
2914 }
2915 }
2916 }
2917
2918 pub fn are_animations_ongoing(&self, output: Option<&Output>) -> bool {
2919 // Keep advancing animations if we might need to scroll the view.
2920 if let Some(dnd) = &self.dnd {
2921 if output.map_or(true, |output| *output == dnd.output) {
2922 return true;
2923 }
2924 }
2925
2926 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
2927 if output.map_or(true, |output| *output == move_.output) {
2928 if move_.tile.are_animations_ongoing() {
2929 return true;
2930 }
2931
2932 // Keep advancing animations if we might need to scroll the view.
2933 if !move_.is_floating {
2934 return true;
2935 }
2936 }
2937 }
2938
2939 if self
2940 .overview_progress
2941 .as_ref()
2942 .is_some_and(|p| p.is_animation())
2943 {
2944 return true;
2945 }
2946
2947 let MonitorSet::Normal { monitors, .. } = &self.monitor_set else {
2948 return false;
2949 };
2950
2951 for mon in monitors {
2952 if output.is_some_and(|output| mon.output != *output) {
2953 continue;
2954 }
2955
2956 if mon.are_animations_ongoing() {
2957 return true;
2958 }
2959 }
2960
2961 false
2962 }
2963
2964 pub fn update_render_elements(&mut self, output: Option<&Output>) {
2965 let _span = tracy_client::span!("Layout::update_render_elements");
2966
2967 self.update_render_elements_time = self.clock.now();
2968
2969 let zoom = self.overview_zoom();
2970 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
2971 if output.map_or(true, |output| move_.output == *output) {
2972 let pos_within_output = move_.tile_render_location(zoom);
2973 let view_rect =
2974 Rectangle::new(pos_within_output.upscale(-1.), output_size(&move_.output));
2975 move_.tile.update_render_elements(true, view_rect);
2976 }
2977 }
2978
2979 self.update_insert_hint(output);
2980
2981 let MonitorSet::Normal {
2982 monitors,
2983 active_monitor_idx,
2984 ..
2985 } = &mut self.monitor_set
2986 else {
2987 error!("update_render_elements called with no monitors");
2988 return;
2989 };
2990
2991 for (idx, mon) in monitors.iter_mut().enumerate() {
2992 if output.map_or(true, |output| mon.output == *output) {
2993 let is_active = self.is_active
2994 && idx == *active_monitor_idx
2995 && !matches!(self.interactive_move, Some(InteractiveMoveState::Moving(_)));
2996 mon.set_overview_progress(self.overview_progress.as_ref());
2997 mon.update_render_elements(is_active);
2998 }
2999 }
3000 }
3001
3002 pub fn update_shaders(&mut self) {
3003 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3004 move_.tile.update_shaders();
3005 }
3006
3007 match &mut self.monitor_set {
3008 MonitorSet::Normal { monitors, .. } => {
3009 for mon in monitors {
3010 mon.update_shaders();
3011 }
3012 }
3013 MonitorSet::NoOutputs { workspaces, .. } => {
3014 for ws in workspaces {
3015 ws.update_shaders();
3016 }
3017 }
3018 }
3019 }
3020
3021 fn update_insert_hint(&mut self, output: Option<&Output>) {
3022 let _span = tracy_client::span!("Layout::update_insert_hint");
3023
3024 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
3025 for mon in monitors {
3026 mon.insert_hint = None;
3027 }
3028 }
3029
3030 if !matches!(self.interactive_move, Some(InteractiveMoveState::Moving(_))) {
3031 return;
3032 }
3033 let Some(InteractiveMoveState::Moving(move_)) = self.interactive_move.take() else {
3034 unreachable!()
3035 };
3036 if output.is_some_and(|out| &move_.output != out) {
3037 self.interactive_move = Some(InteractiveMoveState::Moving(move_));
3038 return;
3039 }
3040
3041 let _span = tracy_client::span!("Layout::update_insert_hint::update");
3042
3043 if let Some(mon) = self.monitor_for_output_mut(&move_.output) {
3044 let zoom = mon.overview_zoom();
3045 let (insert_ws, geo) = mon.insert_position(move_.pointer_pos_within_output);
3046 match insert_ws {
3047 InsertWorkspace::Existing(ws_id) => {
3048 let ws = mon
3049 .workspaces
3050 .iter_mut()
3051 .find(|ws| ws.id() == ws_id)
3052 .unwrap();
3053 let pos_within_workspace =
3054 (move_.pointer_pos_within_output - geo.loc).downscale(zoom);
3055 let position = if move_.is_floating {
3056 InsertPosition::Floating
3057 } else {
3058 ws.scrolling_insert_position(pos_within_workspace)
3059 };
3060
3061 let rules = move_.tile.window().rules();
3062 let border_width = move_.tile.effective_border_width().unwrap_or(0.);
3063 let corner_radius = rules
3064 .geometry_corner_radius
3065 .map_or(CornerRadius::default(), |radius| {
3066 radius.expanded_by(border_width as f32)
3067 });
3068 mon.insert_hint = Some(InsertHint {
3069 workspace: insert_ws,
3070 position,
3071 corner_radius,
3072 });
3073 }
3074 InsertWorkspace::NewAt(_) => {
3075 let position = if move_.is_floating {
3076 InsertPosition::Floating
3077 } else {
3078 InsertPosition::NewColumn(0)
3079 };
3080 mon.insert_hint = Some(InsertHint {
3081 workspace: insert_ws,
3082 position,
3083 corner_radius: CornerRadius::default(),
3084 });
3085 }
3086 }
3087 }
3088
3089 self.interactive_move = Some(InteractiveMoveState::Moving(move_));
3090 }
3091
3092 pub fn ensure_named_workspace(&mut self, ws_config: &WorkspaceConfig) {
3093 if self.find_workspace_by_name(&ws_config.name.0).is_some() {
3094 return;
3095 }
3096
3097 let clock = self.clock.clone();
3098 let options = self.options.clone();
3099
3100 match &mut self.monitor_set {
3101 MonitorSet::Normal {
3102 monitors,
3103 primary_idx,
3104 active_monitor_idx,
3105 } => {
3106 let mon_idx = ws_config
3107 .open_on_output
3108 .as_deref()
3109 .map(|name| {
3110 monitors
3111 .iter_mut()
3112 .position(|monitor| output_matches_name(&monitor.output, name))
3113 .unwrap_or(*primary_idx)
3114 })
3115 .unwrap_or(*active_monitor_idx);
3116 let mon = &mut monitors[mon_idx];
3117
3118 let mut insert_idx = 0;
3119 if mon.options.empty_workspace_above_first {
3120 // need to insert new empty workspace on top
3121 mon.add_workspace_top();
3122 insert_idx += 1;
3123 }
3124
3125 let ws = Workspace::new_with_config(
3126 mon.output.clone(),
3127 Some(ws_config.clone()),
3128 clock,
3129 options,
3130 );
3131 mon.workspaces.insert(insert_idx, ws);
3132 mon.active_workspace_idx += 1;
3133
3134 mon.workspace_switch = None;
3135 mon.clean_up_workspaces();
3136 }
3137 MonitorSet::NoOutputs { workspaces } => {
3138 let ws =
3139 Workspace::new_with_config_no_outputs(Some(ws_config.clone()), clock, options);
3140 workspaces.insert(0, ws);
3141 }
3142 }
3143 }
3144
3145 pub fn update_config(&mut self, config: &Config) {
3146 self.update_options(Options::from_config(config));
3147 }
3148
3149 fn update_options(&mut self, options: Options) {
3150 let options = Rc::new(options);
3151
3152 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3153 let view_size = output_size(&move_.output);
3154 let scale = move_.output.current_scale().fractional_scale();
3155 move_.tile.update_config(
3156 view_size,
3157 scale,
3158 Rc::new(Options::clone(&options).adjusted_for_scale(scale)),
3159 );
3160 }
3161
3162 match &mut self.monitor_set {
3163 MonitorSet::Normal { monitors, .. } => {
3164 for mon in monitors {
3165 mon.update_config(options.clone());
3166 }
3167 }
3168 MonitorSet::NoOutputs { workspaces } => {
3169 for ws in workspaces {
3170 ws.update_config(options.clone());
3171 }
3172 }
3173 }
3174
3175 self.options = options;
3176 }
3177
3178 pub fn toggle_width(&mut self) {
3179 let Some(workspace) = self.active_workspace_mut() else {
3180 return;
3181 };
3182 workspace.toggle_width();
3183 }
3184
3185 pub fn toggle_window_width(&mut self, window: Option<&W::Id>) {
3186 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3187 if window.is_none() || window == Some(move_.tile.window().id()) {
3188 return;
3189 }
3190 }
3191
3192 let workspace = if let Some(window) = window {
3193 Some(
3194 self.workspaces_mut()
3195 .find(|ws| ws.has_window(window))
3196 .unwrap(),
3197 )
3198 } else {
3199 self.active_workspace_mut()
3200 };
3201
3202 let Some(workspace) = workspace else {
3203 return;
3204 };
3205 workspace.toggle_window_width(window);
3206 }
3207
3208 pub fn toggle_window_height(&mut self, window: Option<&W::Id>) {
3209 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3210 if window.is_none() || window == Some(move_.tile.window().id()) {
3211 return;
3212 }
3213 }
3214
3215 let workspace = if let Some(window) = window {
3216 Some(
3217 self.workspaces_mut()
3218 .find(|ws| ws.has_window(window))
3219 .unwrap(),
3220 )
3221 } else {
3222 self.active_workspace_mut()
3223 };
3224
3225 let Some(workspace) = workspace else {
3226 return;
3227 };
3228 workspace.toggle_window_height(window);
3229 }
3230
3231 pub fn toggle_full_width(&mut self) {
3232 let Some(workspace) = self.active_workspace_mut() else {
3233 return;
3234 };
3235 workspace.toggle_full_width();
3236 }
3237
3238 pub fn set_column_width(&mut self, change: SizeChange) {
3239 let Some(workspace) = self.active_workspace_mut() else {
3240 return;
3241 };
3242 workspace.set_column_width(change);
3243 }
3244
3245 pub fn set_window_width(&mut self, window: Option<&W::Id>, change: SizeChange) {
3246 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3247 if window.is_none() || window == Some(move_.tile.window().id()) {
3248 return;
3249 }
3250 }
3251
3252 let workspace = if let Some(window) = window {
3253 Some(
3254 self.workspaces_mut()
3255 .find(|ws| ws.has_window(window))
3256 .unwrap(),
3257 )
3258 } else {
3259 self.active_workspace_mut()
3260 };
3261
3262 let Some(workspace) = workspace else {
3263 return;
3264 };
3265 workspace.set_window_width(window, change);
3266 }
3267
3268 pub fn set_window_height(&mut self, window: Option<&W::Id>, change: SizeChange) {
3269 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3270 if window.is_none() || window == Some(move_.tile.window().id()) {
3271 return;
3272 }
3273 }
3274
3275 let workspace = if let Some(window) = window {
3276 Some(
3277 self.workspaces_mut()
3278 .find(|ws| ws.has_window(window))
3279 .unwrap(),
3280 )
3281 } else {
3282 self.active_workspace_mut()
3283 };
3284
3285 let Some(workspace) = workspace else {
3286 return;
3287 };
3288 workspace.set_window_height(window, change);
3289 }
3290
3291 pub fn reset_window_height(&mut self, window: Option<&W::Id>) {
3292 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3293 if window.is_none() || window == Some(move_.tile.window().id()) {
3294 return;
3295 }
3296 }
3297
3298 let workspace = if let Some(window) = window {
3299 Some(
3300 self.workspaces_mut()
3301 .find(|ws| ws.has_window(window))
3302 .unwrap(),
3303 )
3304 } else {
3305 self.active_workspace_mut()
3306 };
3307
3308 let Some(workspace) = workspace else {
3309 return;
3310 };
3311 workspace.reset_window_height(window);
3312 }
3313
3314 pub fn expand_column_to_available_width(&mut self) {
3315 let Some(workspace) = self.active_workspace_mut() else {
3316 return;
3317 };
3318 workspace.expand_column_to_available_width();
3319 }
3320
3321 pub fn toggle_window_floating(&mut self, window: Option<&W::Id>) {
3322 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3323 if window.is_none() || window == Some(move_.tile.window().id()) {
3324 move_.is_floating = !move_.is_floating;
3325
3326 // When going to floating, restore the floating window size.
3327 if move_.is_floating {
3328 let floating_size = move_.tile.floating_window_size;
3329 let win = move_.tile.window_mut();
3330 let mut size =
3331 floating_size.unwrap_or_else(|| win.expected_size().unwrap_or_default());
3332
3333 // Apply min/max size window rules. If requesting a concrete size, apply
3334 // completely; if requesting (0, 0), apply only when min/max results in a fixed
3335 // size.
3336 let min_size = win.min_size();
3337 let max_size = win.max_size();
3338 size.w = ensure_min_max_size_maybe_zero(size.w, min_size.w, max_size.w);
3339 size.h = ensure_min_max_size_maybe_zero(size.h, min_size.h, max_size.h);
3340
3341 win.request_size_once(size, true);
3342
3343 // Animate the tile back to opaque.
3344 move_.tile.animate_alpha(
3345 INTERACTIVE_MOVE_ALPHA,
3346 1.,
3347 self.options.animations.window_movement.0,
3348 );
3349 } else {
3350 // Animate the tile back to semitransparent.
3351 move_.tile.animate_alpha(
3352 1.,
3353 INTERACTIVE_MOVE_ALPHA,
3354 self.options.animations.window_movement.0,
3355 );
3356 move_.tile.hold_alpha_animation_after_done();
3357 }
3358
3359 return;
3360 }
3361 }
3362
3363 let workspace = if let Some(window) = window {
3364 Some(
3365 self.workspaces_mut()
3366 .find(|ws| ws.has_window(window))
3367 .unwrap(),
3368 )
3369 } else {
3370 self.active_workspace_mut()
3371 };
3372
3373 let Some(workspace) = workspace else {
3374 return;
3375 };
3376 workspace.toggle_window_floating(window);
3377 }
3378
3379 pub fn set_window_floating(&mut self, window: Option<&W::Id>, floating: bool) {
3380 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3381 if window.is_none() || window == Some(move_.tile.window().id()) {
3382 if move_.is_floating != floating {
3383 self.toggle_window_floating(window);
3384 }
3385 return;
3386 }
3387 }
3388
3389 let workspace = if let Some(window) = window {
3390 Some(
3391 self.workspaces_mut()
3392 .find(|ws| ws.has_window(window))
3393 .unwrap(),
3394 )
3395 } else {
3396 self.active_workspace_mut()
3397 };
3398
3399 let Some(workspace) = workspace else {
3400 return;
3401 };
3402 workspace.set_window_floating(window, floating);
3403 }
3404
3405 pub fn focus_floating(&mut self) {
3406 let Some(workspace) = self.active_workspace_mut() else {
3407 return;
3408 };
3409 workspace.focus_floating();
3410 }
3411
3412 pub fn focus_tiling(&mut self) {
3413 let Some(workspace) = self.active_workspace_mut() else {
3414 return;
3415 };
3416 workspace.focus_tiling();
3417 }
3418
3419 pub fn switch_focus_floating_tiling(&mut self) {
3420 let Some(workspace) = self.active_workspace_mut() else {
3421 return;
3422 };
3423 workspace.switch_focus_floating_tiling();
3424 }
3425
3426 pub fn move_floating_window(
3427 &mut self,
3428 id: Option<&W::Id>,
3429 x: PositionChange,
3430 y: PositionChange,
3431 animate: bool,
3432 ) {
3433 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3434 if id.is_none() || id == Some(move_.tile.window().id()) {
3435 return;
3436 }
3437 }
3438
3439 let workspace = if let Some(id) = id {
3440 Some(self.workspaces_mut().find(|ws| ws.has_window(id)).unwrap())
3441 } else {
3442 self.active_workspace_mut()
3443 };
3444
3445 let Some(workspace) = workspace else {
3446 return;
3447 };
3448 workspace.move_floating_window(id, x, y, animate);
3449 }
3450
3451 pub fn focus_output(&mut self, output: &Output) {
3452 if let MonitorSet::Normal {
3453 monitors,
3454 active_monitor_idx,
3455 ..
3456 } = &mut self.monitor_set
3457 {
3458 for (idx, mon) in monitors.iter().enumerate() {
3459 if &mon.output == output {
3460 *active_monitor_idx = idx;
3461 return;
3462 }
3463 }
3464 }
3465 }
3466
3467 pub fn move_to_output(
3468 &mut self,
3469 window: Option<&W::Id>,
3470 output: &Output,
3471 target_ws_idx: Option<usize>,
3472 activate: ActivateWindow,
3473 ) {
3474 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
3475 if window.is_none() || window == Some(move_.tile.window().id()) {
3476 return;
3477 }
3478 }
3479
3480 if let MonitorSet::Normal {
3481 monitors,
3482 active_monitor_idx,
3483 ..
3484 } = &mut self.monitor_set
3485 {
3486 let new_idx = monitors
3487 .iter()
3488 .position(|mon| &mon.output == output)
3489 .unwrap();
3490
3491 let (mon_idx, ws_idx) = if let Some(window) = window {
3492 monitors
3493 .iter()
3494 .enumerate()
3495 .find_map(|(mon_idx, mon)| {
3496 mon.workspaces
3497 .iter()
3498 .position(|ws| ws.has_window(window))
3499 .map(|ws_idx| (mon_idx, ws_idx))
3500 })
3501 .unwrap()
3502 } else {
3503 let mon_idx = *active_monitor_idx;
3504 let mon = &monitors[mon_idx];
3505 (mon_idx, mon.active_workspace_idx)
3506 };
3507
3508 let workspace_idx = target_ws_idx.unwrap_or(monitors[new_idx].active_workspace_idx);
3509 if mon_idx == new_idx && ws_idx == workspace_idx {
3510 return;
3511 }
3512
3513 let mon = &monitors[new_idx];
3514 if mon.workspaces.len() <= workspace_idx {
3515 return;
3516 }
3517
3518 let ws_id = mon.workspaces[workspace_idx].id();
3519
3520 let mon = &mut monitors[mon_idx];
3521 let activate = activate.map_smart(|| {
3522 window.map_or(true, |win| {
3523 mon_idx == *active_monitor_idx
3524 && mon.active_window().map(|win| win.id()) == Some(win)
3525 })
3526 });
3527 let activate = if activate {
3528 ActivateWindow::Yes
3529 } else {
3530 ActivateWindow::No
3531 };
3532
3533 let ws = &mut mon.workspaces[ws_idx];
3534 let transaction = Transaction::new();
3535 let mut removed = if let Some(window) = window {
3536 ws.remove_tile(window, transaction)
3537 } else if let Some(removed) = ws.remove_active_tile(transaction) {
3538 removed
3539 } else {
3540 return;
3541 };
3542
3543 removed.tile.stop_move_animations();
3544
3545 let mon = &mut monitors[new_idx];
3546 mon.add_tile(
3547 removed.tile,
3548 MonitorAddWindowTarget::Workspace {
3549 id: ws_id,
3550 column_idx: None,
3551 },
3552 activate,
3553 true,
3554 removed.width,
3555 removed.is_full_width,
3556 removed.is_floating,
3557 );
3558 if activate.map_smart(|| false) {
3559 *active_monitor_idx = new_idx;
3560 }
3561
3562 let mon = &mut monitors[mon_idx];
3563 if mon.workspace_switch.is_none() {
3564 monitors[mon_idx].clean_up_workspaces();
3565 }
3566 }
3567 }
3568
3569 pub fn move_column_to_output(
3570 &mut self,
3571 output: &Output,
3572 target_ws_idx: Option<usize>,
3573 activate: bool,
3574 ) {
3575 if let MonitorSet::Normal {
3576 monitors,
3577 active_monitor_idx,
3578 ..
3579 } = &mut self.monitor_set
3580 {
3581 let new_idx = monitors
3582 .iter()
3583 .position(|mon| &mon.output == output)
3584 .unwrap();
3585
3586 let current = &mut monitors[*active_monitor_idx];
3587 let ws = current.active_workspace();
3588
3589 if ws.floating_is_active() {
3590 self.move_to_output(None, output, None, ActivateWindow::Smart);
3591 return;
3592 }
3593
3594 let Some(column) = ws.remove_active_column() else {
3595 return;
3596 };
3597
3598 let workspace_idx = target_ws_idx
3599 .unwrap_or(monitors[new_idx].active_workspace_idx)
3600 .min(monitors[new_idx].workspaces.len() - 1);
3601 self.add_column_by_idx(new_idx, workspace_idx, column, activate);
3602 }
3603 }
3604
3605 pub fn move_workspace_to_output(&mut self, output: &Output) -> bool {
3606 let MonitorSet::Normal {
3607 monitors,
3608 active_monitor_idx,
3609 ..
3610 } = &mut self.monitor_set
3611 else {
3612 return false;
3613 };
3614
3615 let current = &mut monitors[*active_monitor_idx];
3616
3617 // Do not do anything if the output is already correct
3618 if &current.output == output {
3619 // Just update the original output since this is an explicit movement action.
3620 current.active_workspace().original_output = OutputId::new(output);
3621
3622 return false;
3623 }
3624
3625 if current.active_workspace_idx == current.workspaces.len() - 1 {
3626 // Insert a new empty workspace.
3627 current.add_workspace_bottom();
3628 }
3629 if current.options.empty_workspace_above_first && current.active_workspace_idx == 0 {
3630 current.add_workspace_top();
3631 }
3632
3633 let mut ws = current.workspaces.remove(current.active_workspace_idx);
3634 current.active_workspace_idx = current.active_workspace_idx.saturating_sub(1);
3635 current.workspace_switch = None;
3636 current.clean_up_workspaces();
3637
3638 ws.set_output(Some(output.clone()));
3639 ws.original_output = OutputId::new(output);
3640
3641 let target_idx = monitors
3642 .iter()
3643 .position(|mon| &mon.output == output)
3644 .unwrap();
3645 let target = &mut monitors[target_idx];
3646
3647 target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
3648
3649 if target.options.empty_workspace_above_first && target.workspaces.len() == 1 {
3650 // Insert a new empty workspace on top to prepare for insertion of new workspace.
3651 target.add_workspace_top();
3652 }
3653 // Insert the workspace after the currently active one. Unless the currently active one is
3654 // the last empty workspace, then insert before.
3655 let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
3656 target.workspaces.insert(target_ws_idx, ws);
3657 target.active_workspace_idx = target_ws_idx;
3658 target.workspace_switch = None;
3659 target.clean_up_workspaces();
3660
3661 *active_monitor_idx = target_idx;
3662
3663 true
3664 }
3665
3666 // FIXME: accept workspace by id and deduplicate logic with move_workspace_to_output()
3667 pub fn move_workspace_to_output_by_id(
3668 &mut self,
3669 old_idx: usize,
3670 old_output: Option<Output>,
3671 new_output: Output,
3672 ) -> bool {
3673 let MonitorSet::Normal {
3674 monitors,
3675 active_monitor_idx,
3676 ..
3677 } = &mut self.monitor_set
3678 else {
3679 return false;
3680 };
3681
3682 let current_idx = if let Some(old_output) = old_output {
3683 monitors
3684 .iter()
3685 .position(|mon| mon.output == old_output)
3686 .unwrap()
3687 } else {
3688 *active_monitor_idx
3689 };
3690 let target_idx = monitors
3691 .iter()
3692 .position(|mon| mon.output == new_output)
3693 .unwrap();
3694
3695 let current = &mut monitors[current_idx];
3696
3697 if current.workspaces.len() <= old_idx {
3698 return false;
3699 }
3700
3701 // Do not do anything if the output is already correct
3702 if current_idx == target_idx {
3703 // Just update the original output since this is an explicit movement action.
3704 current.workspaces[old_idx].original_output = OutputId::new(&current.output);
3705
3706 return false;
3707 }
3708
3709 let current_active_ws_idx = current.active_workspace_idx;
3710
3711 if old_idx == current.workspaces.len() - 1 {
3712 // Insert a new empty workspace.
3713 current.add_workspace_bottom();
3714 }
3715
3716 let mut ws = current.workspaces.remove(old_idx);
3717
3718 if current.options.empty_workspace_above_first && old_idx == 0 {
3719 current.add_workspace_top();
3720 }
3721
3722 if old_idx < current.active_workspace_idx {
3723 current.active_workspace_idx -= 1;
3724 }
3725 current.workspace_switch = None;
3726 current.clean_up_workspaces();
3727
3728 ws.set_output(Some(new_output.clone()));
3729 ws.original_output = OutputId::new(&new_output);
3730
3731 let target = &mut monitors[target_idx];
3732
3733 target.previous_workspace_id = Some(target.workspaces[target.active_workspace_idx].id());
3734
3735 if target.options.empty_workspace_above_first && target.workspaces.len() == 1 {
3736 // Insert a new empty workspace on top to prepare for insertion of new workspace.
3737 target.add_workspace_top();
3738 }
3739 // Insert the workspace after the currently active one. Unless the currently active one is
3740 // the last empty workspace, then insert before.
3741 let target_ws_idx = min(target.active_workspace_idx + 1, target.workspaces.len() - 1);
3742 target.workspaces.insert(target_ws_idx, ws);
3743
3744 // Only switch active monitor if the workspace moved was the currently focused one on the
3745 // current monitor
3746 let res = if current_idx == *active_monitor_idx && old_idx == current_active_ws_idx {
3747 *active_monitor_idx = target_idx;
3748 target.active_workspace_idx = target_ws_idx;
3749 true
3750 } else {
3751 if target_ws_idx <= target.active_workspace_idx {
3752 target.active_workspace_idx += 1;
3753 }
3754 false
3755 };
3756
3757 target.workspace_switch = None;
3758 target.clean_up_workspaces();
3759
3760 res
3761 }
3762
3763 pub fn set_fullscreen(&mut self, id: &W::Id, is_fullscreen: bool) {
3764 // Check if this is a request to unset the windowed fullscreen state.
3765 if !is_fullscreen {
3766 let mut handled = false;
3767 self.with_windows_mut(|window, _| {
3768 if window.id() == id && window.is_pending_windowed_fullscreen() {
3769 window.request_windowed_fullscreen(false);
3770 handled = true;
3771 }
3772 });
3773 if handled {
3774 return;
3775 }
3776 }
3777
3778 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
3779 if move_.tile.window().id() == id {
3780 return;
3781 }
3782 }
3783
3784 for ws in self.workspaces_mut() {
3785 if ws.has_window(id) {
3786 ws.set_fullscreen(id, is_fullscreen);
3787 return;
3788 }
3789 }
3790 }
3791
3792 pub fn toggle_fullscreen(&mut self, id: &W::Id) {
3793 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
3794 if move_.tile.window().id() == id {
3795 return;
3796 }
3797 }
3798
3799 for ws in self.workspaces_mut() {
3800 if ws.has_window(id) {
3801 ws.toggle_fullscreen(id);
3802 return;
3803 }
3804 }
3805 }
3806
3807 pub fn toggle_windowed_fullscreen(&mut self, id: &W::Id) {
3808 let (_, window) = self.windows().find(|(_, win)| win.id() == id).unwrap();
3809 if window.is_pending_fullscreen() {
3810 // Remove the real fullscreen.
3811 for ws in self.workspaces_mut() {
3812 if ws.has_window(id) {
3813 ws.set_fullscreen(id, false);
3814 break;
3815 }
3816 }
3817 }
3818
3819 // This will switch is_pending_fullscreen() to false right away.
3820 self.with_windows_mut(|window, _| {
3821 if window.id() == id {
3822 window.request_windowed_fullscreen(!window.is_pending_windowed_fullscreen());
3823 }
3824 });
3825 }
3826
3827 pub fn workspace_switch_gesture_begin(&mut self, output: &Output, is_touchpad: bool) {
3828 let monitors = match &mut self.monitor_set {
3829 MonitorSet::Normal { monitors, .. } => monitors,
3830 MonitorSet::NoOutputs { .. } => unreachable!(),
3831 };
3832
3833 for monitor in monitors {
3834 // Cancel the gesture on other outputs.
3835 if &monitor.output != output {
3836 monitor.workspace_switch_gesture_end(None);
3837 continue;
3838 }
3839
3840 monitor.workspace_switch_gesture_begin(is_touchpad);
3841 }
3842 }
3843
3844 pub fn workspace_switch_gesture_update(
3845 &mut self,
3846 delta_y: f64,
3847 timestamp: Duration,
3848 is_touchpad: bool,
3849 ) -> Option<Option<Output>> {
3850 let monitors = match &mut self.monitor_set {
3851 MonitorSet::Normal { monitors, .. } => monitors,
3852 MonitorSet::NoOutputs { .. } => return None,
3853 };
3854
3855 for monitor in monitors {
3856 if let Some(refresh) =
3857 monitor.workspace_switch_gesture_update(delta_y, timestamp, is_touchpad)
3858 {
3859 if refresh {
3860 return Some(Some(monitor.output.clone()));
3861 } else {
3862 return Some(None);
3863 }
3864 }
3865 }
3866
3867 None
3868 }
3869
3870 pub fn workspace_switch_gesture_end(&mut self, is_touchpad: Option<bool>) -> Option<Output> {
3871 let monitors = match &mut self.monitor_set {
3872 MonitorSet::Normal { monitors, .. } => monitors,
3873 MonitorSet::NoOutputs { .. } => return None,
3874 };
3875
3876 for monitor in monitors {
3877 if monitor.workspace_switch_gesture_end(is_touchpad) {
3878 return Some(monitor.output.clone());
3879 }
3880 }
3881
3882 None
3883 }
3884
3885 pub fn view_offset_gesture_begin(
3886 &mut self,
3887 output: &Output,
3888 workspace_idx: Option<usize>,
3889 is_touchpad: bool,
3890 ) {
3891 let monitors = match &mut self.monitor_set {
3892 MonitorSet::Normal { monitors, .. } => monitors,
3893 MonitorSet::NoOutputs { .. } => unreachable!(),
3894 };
3895
3896 for monitor in monitors {
3897 for (idx, ws) in monitor.workspaces.iter_mut().enumerate() {
3898 // Cancel the gesture on other workspaces.
3899 if &monitor.output != output
3900 || idx != workspace_idx.unwrap_or(monitor.active_workspace_idx)
3901 {
3902 ws.view_offset_gesture_end(None);
3903 continue;
3904 }
3905
3906 ws.view_offset_gesture_begin(is_touchpad);
3907 }
3908 }
3909 }
3910
3911 pub fn view_offset_gesture_update(
3912 &mut self,
3913 delta_x: f64,
3914 timestamp: Duration,
3915 is_touchpad: bool,
3916 ) -> Option<Option<Output>> {
3917 let zoom = self.overview_zoom();
3918 let delta_x = delta_x / zoom;
3919
3920 let monitors = match &mut self.monitor_set {
3921 MonitorSet::Normal { monitors, .. } => monitors,
3922 MonitorSet::NoOutputs { .. } => return None,
3923 };
3924
3925 for monitor in monitors {
3926 for ws in &mut monitor.workspaces {
3927 if let Some(refresh) =
3928 ws.view_offset_gesture_update(delta_x, timestamp, is_touchpad)
3929 {
3930 if refresh {
3931 return Some(Some(monitor.output.clone()));
3932 } else {
3933 return Some(None);
3934 }
3935 }
3936 }
3937 }
3938
3939 None
3940 }
3941
3942 pub fn view_offset_gesture_end(&mut self, is_touchpad: Option<bool>) -> Option<Output> {
3943 let monitors = match &mut self.monitor_set {
3944 MonitorSet::Normal { monitors, .. } => monitors,
3945 MonitorSet::NoOutputs { .. } => return None,
3946 };
3947
3948 for monitor in monitors {
3949 for ws in &mut monitor.workspaces {
3950 if ws.view_offset_gesture_end(is_touchpad) {
3951 return Some(monitor.output.clone());
3952 }
3953 }
3954 }
3955
3956 None
3957 }
3958
3959 pub fn overview_gesture_begin(&mut self) {
3960 self.overview_open = true;
3961
3962 let value = self.overview_progress.take().map_or(0., |p| p.value());
3963 let gesture = OverviewGesture {
3964 tracker: SwipeTracker::new(),
3965 start: value,
3966 value,
3967 };
3968 self.overview_progress = Some(OverviewProgress::Gesture(gesture));
3969
3970 self.set_monitors_overview_state();
3971 }
3972
3973 pub fn overview_gesture_update(&mut self, delta_y: f64, timestamp: Duration) -> Option<bool> {
3974 let Some(OverviewProgress::Gesture(gesture)) = &mut self.overview_progress else {
3975 return None;
3976 };
3977
3978 gesture.tracker.push(delta_y, timestamp);
3979
3980 let total_height = OVERVIEW_GESTURE_MOVEMENT;
3981 let pos = gesture.tracker.pos() / total_height;
3982 let new_value = gesture.start + pos;
3983 let new_value = OVERVIEW_GESTURE_RUBBER_BAND.clamp(0., 1., new_value);
3984
3985 if gesture.value == new_value {
3986 return Some(false);
3987 }
3988
3989 gesture.value = new_value;
3990 self.set_monitors_overview_state();
3991
3992 Some(true)
3993 }
3994
3995 pub fn overview_gesture_end(&mut self) -> bool {
3996 let Some(OverviewProgress::Gesture(gesture)) = &mut self.overview_progress else {
3997 return false;
3998 };
3999
4000 // Take into account any idle time between the last event and now.
4001 let now = self.clock.now_unadjusted();
4002 gesture.tracker.push(0., now);
4003
4004 let total_height = OVERVIEW_GESTURE_MOVEMENT;
4005
4006 let mut velocity = gesture.tracker.velocity() / total_height;
4007 let current_pos = gesture.tracker.pos() / total_height;
4008 let pos = gesture.tracker.projected_end_pos() / total_height;
4009
4010 let new_value = gesture.start + pos;
4011 let new_value = new_value.clamp(0., 1.).round();
4012
4013 velocity *=
4014 OVERVIEW_GESTURE_RUBBER_BAND.clamp_derivative(0., 1., gesture.start + current_pos);
4015
4016 self.overview_open = new_value == 1.;
4017 self.overview_progress = Some(OverviewProgress::Animation(Animation::new(
4018 self.clock.clone(),
4019 gesture.value,
4020 new_value,
4021 velocity,
4022 self.options.animations.overview_open_close.0,
4023 )));
4024
4025 self.set_monitors_overview_state();
4026
4027 true
4028 }
4029
4030 pub fn interactive_move_begin(
4031 &mut self,
4032 window_id: W::Id,
4033 output: &Output,
4034 start_pos_within_output: Point<f64, Logical>,
4035 ) -> bool {
4036 if self.interactive_move.is_some() {
4037 return false;
4038 }
4039
4040 let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else {
4041 return false;
4042 };
4043
4044 let Some((mon, (ws, ws_geo))) = monitors.iter().find_map(|mon| {
4045 mon.workspaces_with_render_geo()
4046 .find(|(ws, _)| ws.has_window(&window_id))
4047 .map(|rv| (mon, rv))
4048 }) else {
4049 return false;
4050 };
4051
4052 if mon.output() != output {
4053 return false;
4054 }
4055
4056 let zoom = mon.overview_zoom();
4057
4058 let is_floating = ws.is_floating(&window_id);
4059 let (tile, tile_offset, _visible) = ws
4060 .tiles_with_render_positions()
4061 .find(|(tile, _, _)| tile.window().id() == &window_id)
4062 .unwrap();
4063 let window_offset = tile.window_loc();
4064
4065 let tile_pos = ws_geo.loc + tile_offset.upscale(zoom);
4066
4067 let pointer_offset_within_window =
4068 start_pos_within_output - tile_pos - window_offset.upscale(zoom);
4069 let window_size = tile.window_size().upscale(zoom);
4070 let pointer_ratio_within_window = (
4071 f64::clamp(pointer_offset_within_window.x / window_size.w, 0., 1.),
4072 f64::clamp(pointer_offset_within_window.y / window_size.h, 0., 1.),
4073 );
4074
4075 self.interactive_move = Some(InteractiveMoveState::Starting {
4076 window_id,
4077 pointer_delta: Point::from((0., 0.)),
4078 pointer_ratio_within_window,
4079 });
4080
4081 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
4082 for mon in monitors {
4083 mon.dnd_scroll_gesture_begin();
4084 }
4085 }
4086
4087 // Lock the view for scrolling interactive move.
4088 if !is_floating {
4089 for ws in self.workspaces_mut() {
4090 ws.dnd_scroll_gesture_begin();
4091 }
4092 }
4093
4094 true
4095 }
4096
4097 pub fn interactive_move_update(
4098 &mut self,
4099 window: &W::Id,
4100 delta: Point<f64, Logical>,
4101 output: Output,
4102 pointer_pos_within_output: Point<f64, Logical>,
4103 ) -> bool {
4104 let Some(state) = self.interactive_move.take() else {
4105 return false;
4106 };
4107
4108 match state {
4109 InteractiveMoveState::Starting {
4110 window_id,
4111 mut pointer_delta,
4112 pointer_ratio_within_window,
4113 } => {
4114 if window_id != *window {
4115 self.interactive_move = Some(InteractiveMoveState::Starting {
4116 window_id,
4117 pointer_delta,
4118 pointer_ratio_within_window,
4119 });
4120 return false;
4121 }
4122
4123 let zoom = self.overview_zoom();
4124 let delta = delta.downscale(zoom);
4125
4126 pointer_delta += delta;
4127
4128 let (cx, cy) = (pointer_delta.x, pointer_delta.y);
4129 let sq_dist = cx * cx + cy * cy;
4130
4131 let factor = RubberBand {
4132 stiffness: 1.0,
4133 limit: 0.5,
4134 }
4135 .band(sq_dist / INTERACTIVE_MOVE_START_THRESHOLD);
4136
4137 let (is_floating, tile) = self
4138 .workspaces_mut()
4139 .find(|ws| ws.has_window(&window_id))
4140 .map(|ws| {
4141 (
4142 ws.is_floating(&window_id),
4143 ws.tiles_mut()
4144 .find(|tile| *tile.window().id() == window_id)
4145 .unwrap(),
4146 )
4147 })
4148 .unwrap();
4149 tile.interactive_move_offset = pointer_delta.upscale(factor);
4150
4151 // Put it back to be able to easily return.
4152 self.interactive_move = Some(InteractiveMoveState::Starting {
4153 window_id: window_id.clone(),
4154 pointer_delta,
4155 pointer_ratio_within_window,
4156 });
4157
4158 if !is_floating && sq_dist < INTERACTIVE_MOVE_START_THRESHOLD {
4159 return true;
4160 }
4161
4162 // If the pointer is currently on the window's own output, then we can animate the
4163 // window movement from its current (rubberbanded and possibly moved away) position
4164 // to the pointer. Otherwise, we just teleport it as the layout code is not aware
4165 // of monitor positions.
4166 //
4167 // FIXME: when and if the layout code knows about monitor positions, this will be
4168 // potentially animatable.
4169 let mut tile_pos = None;
4170 if let MonitorSet::Normal { monitors, .. } = &self.monitor_set {
4171 if let Some((mon, (ws, ws_geo))) = monitors.iter().find_map(|mon| {
4172 mon.workspaces_with_render_geo()
4173 .find(|(ws, _)| ws.has_window(window))
4174 .map(|rv| (mon, rv))
4175 }) {
4176 if mon.output() == &output {
4177 let (_, tile_offset, _) = ws
4178 .tiles_with_render_positions()
4179 .find(|(tile, _, _)| tile.window().id() == window)
4180 .unwrap();
4181
4182 let zoom = mon.overview_zoom();
4183 tile_pos = Some((ws_geo.loc + tile_offset.upscale(zoom), zoom));
4184 }
4185 }
4186 }
4187
4188 // Clear it before calling remove_window() to avoid running interactive_move_end()
4189 // in the middle of interactive_move_update() and the confusion that causes.
4190 self.interactive_move = None;
4191
4192 let RemovedTile {
4193 mut tile,
4194 width,
4195 is_full_width,
4196 mut is_floating,
4197 } = self.remove_window(window, Transaction::new()).unwrap();
4198
4199 tile.stop_move_animations();
4200 tile.interactive_move_offset = Point::from((0., 0.));
4201 tile.window().output_enter(&output);
4202 tile.window().set_preferred_scale_transform(
4203 output.current_scale(),
4204 output.current_transform(),
4205 );
4206
4207 let view_size = output_size(&output);
4208 let scale = output.current_scale().fractional_scale();
4209 tile.update_config(
4210 view_size,
4211 scale,
4212 Rc::new(Options::clone(&self.options).adjusted_for_scale(scale)),
4213 );
4214
4215 // Unfullscreen.
4216 let floating_size = tile.floating_window_size;
4217 let unfullscreen_to_floating = tile.unfullscreen_to_floating;
4218 let win = tile.window_mut();
4219 if win.is_pending_fullscreen() {
4220 // If we're unfullscreening to floating, use the stored floating size,
4221 // otherwise use (0, 0).
4222 let mut size = if unfullscreen_to_floating {
4223 floating_size.unwrap_or_default()
4224 } else {
4225 Size::from((0, 0))
4226 };
4227
4228 // Apply min/max size window rules. If requesting a concrete size, apply
4229 // completely; if requesting (0, 0), apply only when min/max results in a fixed
4230 // size.
4231 let min_size = win.min_size();
4232 let max_size = win.max_size();
4233 size.w = ensure_min_max_size_maybe_zero(size.w, min_size.w, max_size.w);
4234 size.h = ensure_min_max_size_maybe_zero(size.h, min_size.h, max_size.h);
4235
4236 win.request_size_once(size, true);
4237
4238 // If we're unfullscreening to floating, default to the floating layout.
4239 is_floating = unfullscreen_to_floating;
4240 }
4241
4242 if is_floating {
4243 // Unlock the view in case we locked it moving a fullscreen window that is
4244 // going to unfullscreen to floating.
4245 for ws in self.workspaces_mut() {
4246 ws.dnd_scroll_gesture_end();
4247 }
4248 } else {
4249 // Animate to semitransparent.
4250 tile.animate_alpha(
4251 1.,
4252 INTERACTIVE_MOVE_ALPHA,
4253 self.options.animations.window_movement.0,
4254 );
4255 tile.hold_alpha_animation_after_done();
4256 }
4257
4258 let mut data = InteractiveMoveData {
4259 tile,
4260 output,
4261 pointer_pos_within_output,
4262 width,
4263 is_full_width,
4264 is_floating,
4265 pointer_ratio_within_window,
4266 };
4267
4268 if let Some((tile_pos, zoom)) = tile_pos {
4269 let new_tile_pos = data.tile_render_location(zoom);
4270 data.tile
4271 .animate_move_from((tile_pos - new_tile_pos).downscale(zoom));
4272 }
4273
4274 self.interactive_move = Some(InteractiveMoveState::Moving(data));
4275 }
4276 InteractiveMoveState::Moving(mut move_) => {
4277 if window != move_.tile.window().id() {
4278 self.interactive_move = Some(InteractiveMoveState::Moving(move_));
4279 return false;
4280 }
4281
4282 if output != move_.output {
4283 move_.tile.window().output_leave(&move_.output);
4284 move_.tile.window().output_enter(&output);
4285 move_.tile.window().set_preferred_scale_transform(
4286 output.current_scale(),
4287 output.current_transform(),
4288 );
4289 let view_size = output_size(&output);
4290 let scale = output.current_scale().fractional_scale();
4291 move_.tile.update_config(
4292 view_size,
4293 scale,
4294 Rc::new(Options::clone(&self.options).adjusted_for_scale(scale)),
4295 );
4296 move_.output = output.clone();
4297 self.focus_output(&output);
4298 }
4299
4300 move_.pointer_pos_within_output = pointer_pos_within_output;
4301
4302 self.interactive_move = Some(InteractiveMoveState::Moving(move_));
4303 }
4304 }
4305
4306 true
4307 }
4308
4309 pub fn interactive_move_end(&mut self, window: &W::Id) {
4310 let Some(move_) = &self.interactive_move else {
4311 return;
4312 };
4313
4314 let move_ = match move_ {
4315 InteractiveMoveState::Starting { window_id, .. } => {
4316 if window_id != window {
4317 return;
4318 }
4319
4320 let Some(InteractiveMoveState::Starting { window_id, .. }) =
4321 self.interactive_move.take()
4322 else {
4323 unreachable!()
4324 };
4325
4326 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
4327 for mon in monitors {
4328 mon.dnd_scroll_gesture_end();
4329 }
4330 }
4331
4332 let mut ws_id = None;
4333 for ws in self.workspaces_mut() {
4334 let id = ws.id();
4335 if let Some(tile) = ws.tiles_mut().find(|tile| *tile.window().id() == window_id)
4336 {
4337 let offset = tile.interactive_move_offset;
4338 tile.interactive_move_offset = Point::from((0., 0.));
4339 tile.animate_move_from(offset);
4340
4341 ws_id = Some(id);
4342 }
4343
4344 // Unlock the view on the workspaces, but if the moved window was active,
4345 // preserve that.
4346 let moved_tile_was_active =
4347 ws.active_window().is_some_and(|win| *win.id() == window_id);
4348
4349 ws.dnd_scroll_gesture_end();
4350
4351 if moved_tile_was_active {
4352 ws.activate_window(&window_id);
4353 }
4354 }
4355
4356 // In the overview, we want to click on a window to focus it, and also to
4357 // click-and-drag to move the window. The way we handle this is by always starting
4358 // the interactive move (to get frozen view), then, when in the overview, *not*
4359 // calling interactive_move_update() until the cursor moves far enough. This means
4360 // that if we "just click" then we end up in this branch with state == Starting.
4361 // Close the overview in this case.
4362 if self.overview_open {
4363 let ws_id = ws_id.unwrap();
4364 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
4365 for mon in monitors {
4366 if let Some(ws_idx) =
4367 mon.workspaces.iter().position(|ws| ws.id() == ws_id)
4368 {
4369 mon.activate_workspace_with_anim_config(
4370 ws_idx,
4371 Some(self.options.animations.overview_open_close.0),
4372 );
4373 break;
4374 }
4375 }
4376 }
4377
4378 self.activate_window(&window_id);
4379 self.close_overview();
4380 }
4381
4382 return;
4383 }
4384 InteractiveMoveState::Moving(move_) => move_,
4385 };
4386
4387 if window != move_.tile.window().id() {
4388 return;
4389 }
4390
4391 let Some(InteractiveMoveState::Moving(mut move_)) = self.interactive_move.take() else {
4392 unreachable!()
4393 };
4394
4395 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
4396 for mon in monitors {
4397 mon.dnd_scroll_gesture_end();
4398 }
4399 }
4400
4401 // Unlock the view on the workspaces.
4402 if !move_.is_floating {
4403 for ws in self.workspaces_mut() {
4404 ws.dnd_scroll_gesture_end();
4405 }
4406
4407 // Also animate the tile back to opaque.
4408 move_.tile.animate_alpha(
4409 INTERACTIVE_MOVE_ALPHA,
4410 1.,
4411 self.options.animations.window_movement.0,
4412 );
4413 }
4414
4415 // Dragging in the overview shouldn't switch the workspace and so on.
4416 let allow_to_activate_workspace = !self.overview_open;
4417
4418 match &mut self.monitor_set {
4419 MonitorSet::Normal {
4420 monitors,
4421 active_monitor_idx,
4422 ..
4423 } => {
4424 let (mon, insert_ws, position, offset, zoom) =
4425 if let Some(mon) = monitors.iter_mut().find(|mon| mon.output == move_.output) {
4426 let zoom = mon.overview_zoom();
4427
4428 let (insert_ws, geo) = mon.insert_position(move_.pointer_pos_within_output);
4429 let (position, offset) = match insert_ws {
4430 InsertWorkspace::Existing(ws_id) => {
4431 let ws_idx = mon
4432 .workspaces
4433 .iter_mut()
4434 .position(|ws| ws.id() == ws_id)
4435 .unwrap();
4436
4437 let position = if move_.is_floating {
4438 InsertPosition::Floating
4439 } else {
4440 let pos_within_workspace =
4441 (move_.pointer_pos_within_output - geo.loc).downscale(zoom);
4442 let ws = &mut mon.workspaces[ws_idx];
4443 ws.scrolling_insert_position(pos_within_workspace)
4444 };
4445
4446 (position, Some(geo.loc))
4447 }
4448 InsertWorkspace::NewAt(_) => {
4449 let position = if move_.is_floating {
4450 InsertPosition::Floating
4451 } else {
4452 InsertPosition::NewColumn(0)
4453 };
4454
4455 (position, None)
4456 }
4457 };
4458
4459 (mon, insert_ws, position, offset, zoom)
4460 } else {
4461 let mon = &mut monitors[*active_monitor_idx];
4462 let zoom = mon.overview_zoom();
4463 // No point in trying to use the pointer position on the wrong output.
4464 let ws = &mon.workspaces[0];
4465 let ws_geo = mon.workspaces_render_geo().next().unwrap();
4466
4467 let position = if move_.is_floating {
4468 InsertPosition::Floating
4469 } else {
4470 ws.scrolling_insert_position(Point::from((0., 0.)))
4471 };
4472
4473 let insert_ws = InsertWorkspace::Existing(ws.id());
4474 (mon, insert_ws, position, Some(ws_geo.loc), zoom)
4475 };
4476
4477 let win_id = move_.tile.window().id().clone();
4478 let window_render_loc = move_.tile_render_location(zoom) + move_.tile.window_loc();
4479
4480 let ws_idx = match insert_ws {
4481 InsertWorkspace::Existing(ws_id) => mon
4482 .workspaces
4483 .iter()
4484 .position(|ws| ws.id() == ws_id)
4485 .unwrap(),
4486 InsertWorkspace::NewAt(ws_idx) => {
4487 if self.options.empty_workspace_above_first && ws_idx == 0 {
4488 // Reuse the top empty workspace.
4489 0
4490 } else if mon.workspaces.len() - 1 <= ws_idx {
4491 // Reuse the bottom empty workspace.
4492 mon.workspaces.len() - 1
4493 } else {
4494 mon.add_workspace_at(ws_idx);
4495 ws_idx
4496 }
4497 }
4498 };
4499
4500 match position {
4501 InsertPosition::NewColumn(column_idx) => {
4502 let ws_id = mon.workspaces[ws_idx].id();
4503 mon.add_tile(
4504 move_.tile,
4505 MonitorAddWindowTarget::Workspace {
4506 id: ws_id,
4507 column_idx: Some(column_idx),
4508 },
4509 ActivateWindow::Yes,
4510 allow_to_activate_workspace,
4511 move_.width,
4512 move_.is_full_width,
4513 false,
4514 );
4515 }
4516 InsertPosition::InColumn(column_idx, tile_idx) => {
4517 mon.add_tile_to_column(
4518 ws_idx,
4519 column_idx,
4520 Some(tile_idx),
4521 move_.tile,
4522 true,
4523 allow_to_activate_workspace,
4524 );
4525 }
4526 InsertPosition::Floating => {
4527 let tile_render_loc = move_.tile_render_location(zoom);
4528
4529 let mut tile = move_.tile;
4530 tile.floating_pos = None;
4531
4532 match insert_ws {
4533 InsertWorkspace::Existing(_) => {
4534 if let Some(offset) = offset {
4535 let pos = (tile_render_loc - offset).downscale(zoom);
4536 let pos =
4537 mon.workspaces[ws_idx].floating_logical_to_size_frac(pos);
4538 tile.floating_pos = Some(pos);
4539 } else {
4540 error!(
4541 "offset unset for inserting a floating tile \
4542 to existing workspace"
4543 );
4544 }
4545 }
4546 InsertWorkspace::NewAt(_) => {
4547 // When putting a floating tile on a new workspace, we don't really
4548 // have a good pre-existing position.
4549 }
4550 }
4551
4552 // Set the floating size so it takes into account any window resizing that
4553 // took place during the move.
4554 if let Some(size) = tile.window().expected_size() {
4555 tile.floating_window_size = Some(size);
4556 }
4557
4558 let ws_id = mon.workspaces[ws_idx].id();
4559 mon.add_tile(
4560 tile,
4561 MonitorAddWindowTarget::Workspace {
4562 id: ws_id,
4563 column_idx: None,
4564 },
4565 ActivateWindow::Yes,
4566 allow_to_activate_workspace,
4567 move_.width,
4568 move_.is_full_width,
4569 true,
4570 );
4571 }
4572 }
4573
4574 // needed because empty_workspace_above_first could have modified the idx
4575 let (tile, tile_render_loc, ws_geo) = mon
4576 .workspaces_with_render_geo_mut(false)
4577 .find_map(|(ws, geo)| {
4578 ws.tiles_with_render_positions_mut(false)
4579 .find(|(tile, _)| tile.window().id() == &win_id)
4580 .map(|(tile, tile_render_loc)| (tile, tile_render_loc, geo))
4581 })
4582 .unwrap();
4583 let new_window_render_loc =
4584 ws_geo.loc + (tile_render_loc + tile.window_loc()).upscale(zoom);
4585
4586 tile.animate_move_from((window_render_loc - new_window_render_loc).downscale(zoom));
4587 }
4588 MonitorSet::NoOutputs { workspaces, .. } => {
4589 if workspaces.is_empty() {
4590 workspaces.push(Workspace::new_no_outputs(
4591 self.clock.clone(),
4592 self.options.clone(),
4593 ));
4594 }
4595 let ws = &mut workspaces[0];
4596
4597 // No point in trying to use the pointer position without outputs.
4598 ws.add_tile(
4599 move_.tile,
4600 WorkspaceAddWindowTarget::Auto,
4601 ActivateWindow::Yes,
4602 move_.width,
4603 move_.is_full_width,
4604 move_.is_floating,
4605 );
4606 }
4607 }
4608 }
4609
4610 pub fn interactive_move_is_moving_above_output(&self, output: &Output) -> bool {
4611 let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move else {
4612 return false;
4613 };
4614
4615 move_.output == *output
4616 }
4617
4618 pub fn dnd_update(&mut self, output: Output, pointer_pos_within_output: Point<f64, Logical>) {
4619 let begin_gesture = self.dnd.is_none();
4620
4621 self.dnd = Some(DndData {
4622 output,
4623 pointer_pos_within_output,
4624 hold: None,
4625 });
4626
4627 if begin_gesture {
4628 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
4629 for mon in monitors {
4630 mon.dnd_scroll_gesture_begin();
4631 }
4632 }
4633
4634 for ws in self.workspaces_mut() {
4635 ws.dnd_scroll_gesture_begin();
4636 }
4637 }
4638 }
4639
4640 pub fn dnd_end(&mut self) {
4641 if self.dnd.is_none() {
4642 return;
4643 }
4644
4645 self.dnd = None;
4646
4647 if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set {
4648 for mon in monitors {
4649 mon.dnd_scroll_gesture_end();
4650 }
4651 }
4652
4653 for ws in self.workspaces_mut() {
4654 ws.dnd_scroll_gesture_end();
4655 }
4656 }
4657
4658 pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool {
4659 match &mut self.monitor_set {
4660 MonitorSet::Normal { monitors, .. } => {
4661 for mon in monitors {
4662 for ws in &mut mon.workspaces {
4663 if ws.has_window(&window) {
4664 return ws.interactive_resize_begin(window, edges);
4665 }
4666 }
4667 }
4668 }
4669 MonitorSet::NoOutputs { workspaces, .. } => {
4670 for ws in workspaces {
4671 if ws.has_window(&window) {
4672 return ws.interactive_resize_begin(window, edges);
4673 }
4674 }
4675 }
4676 }
4677
4678 false
4679 }
4680
4681 pub fn interactive_resize_update(
4682 &mut self,
4683 window: &W::Id,
4684 delta: Point<f64, Logical>,
4685 ) -> bool {
4686 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
4687 if move_.tile.window().id() == window {
4688 return false;
4689 }
4690 }
4691
4692 match &mut self.monitor_set {
4693 MonitorSet::Normal { monitors, .. } => {
4694 for mon in monitors {
4695 for ws in &mut mon.workspaces {
4696 if ws.has_window(window) {
4697 return ws.interactive_resize_update(window, delta);
4698 }
4699 }
4700 }
4701 }
4702 MonitorSet::NoOutputs { workspaces, .. } => {
4703 for ws in workspaces {
4704 if ws.has_window(window) {
4705 return ws.interactive_resize_update(window, delta);
4706 }
4707 }
4708 }
4709 }
4710
4711 false
4712 }
4713
4714 pub fn interactive_resize_end(&mut self, window: &W::Id) {
4715 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
4716 if move_.tile.window().id() == window {
4717 return;
4718 }
4719 }
4720
4721 match &mut self.monitor_set {
4722 MonitorSet::Normal { monitors, .. } => {
4723 for mon in monitors {
4724 for ws in &mut mon.workspaces {
4725 if ws.has_window(window) {
4726 ws.interactive_resize_end(Some(window));
4727 return;
4728 }
4729 }
4730 }
4731 }
4732 MonitorSet::NoOutputs { workspaces, .. } => {
4733 for ws in workspaces {
4734 if ws.has_window(window) {
4735 ws.interactive_resize_end(Some(window));
4736 return;
4737 }
4738 }
4739 }
4740 }
4741 }
4742
4743 pub fn move_workspace_down(&mut self) {
4744 let Some(monitor) = self.active_monitor() else {
4745 return;
4746 };
4747 monitor.move_workspace_down();
4748 }
4749
4750 pub fn move_workspace_up(&mut self) {
4751 let Some(monitor) = self.active_monitor() else {
4752 return;
4753 };
4754 monitor.move_workspace_up();
4755 }
4756
4757 pub fn move_workspace_to_idx(
4758 &mut self,
4759 reference: Option<(Option<Output>, usize)>,
4760 new_idx: usize,
4761 ) {
4762 let (monitor, old_idx) = if let Some((output, old_idx)) = reference {
4763 let monitor = if let Some(output) = output {
4764 let Some(monitor) = self.monitor_for_output_mut(&output) else {
4765 return;
4766 };
4767 monitor
4768 } else {
4769 // In case a numbered workspace reference is used, assume the active monitor
4770 let Some(monitor) = self.active_monitor() else {
4771 return;
4772 };
4773 monitor
4774 };
4775
4776 (monitor, old_idx)
4777 } else {
4778 let Some(monitor) = self.active_monitor() else {
4779 return;
4780 };
4781 let index = monitor.active_workspace_idx;
4782 (monitor, index)
4783 };
4784
4785 monitor.move_workspace_to_idx(old_idx, new_idx);
4786 }
4787
4788 pub fn set_workspace_name(&mut self, name: String, reference: Option<WorkspaceReference>) {
4789 // ignore the request if the name is already used by another workspace
4790 if self.find_workspace_by_name(&name).is_some() {
4791 return;
4792 }
4793
4794 let ws = if let Some(reference) = reference {
4795 self.find_workspace_by_ref(reference)
4796 } else {
4797 self.active_workspace_mut()
4798 };
4799 let Some(ws) = ws else {
4800 return;
4801 };
4802
4803 ws.name.replace(name);
4804
4805 let wsid = ws.id();
4806
4807 // if `empty_workspace_above_first` is set and `ws` is the first
4808 // worskpace on a monitor, another empty workspace needs to
4809 // be added before.
4810 // Conversely, if `ws` was the last workspace on a monitor, an
4811 // empty workspace needs to be added after.
4812
4813 if let MonitorSet::Normal {
4814 monitors,
4815 active_monitor_idx,
4816 ..
4817 } = &mut self.monitor_set
4818 {
4819 let monitor = &mut monitors[*active_monitor_idx];
4820 if self.options.empty_workspace_above_first
4821 && monitor
4822 .workspaces
4823 .first()
4824 .is_some_and(|first| first.id() == wsid)
4825 {
4826 monitor.add_workspace_top();
4827 }
4828 if monitor
4829 .workspaces
4830 .last()
4831 .is_some_and(|last| last.id() == wsid)
4832 {
4833 monitor.add_workspace_bottom();
4834 }
4835 }
4836 }
4837
4838 pub fn unset_workspace_name(&mut self, reference: Option<WorkspaceReference>) {
4839 let ws = if let Some(reference) = reference {
4840 self.find_workspace_by_ref(reference)
4841 } else {
4842 self.active_workspace_mut()
4843 };
4844 let Some(ws) = ws else {
4845 return;
4846 };
4847 let id = ws.id();
4848
4849 self.unname_workspace_by_id(id);
4850 }
4851
4852 pub fn set_monitors_overview_state(&mut self) {
4853 let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else {
4854 return;
4855 };
4856
4857 for mon in monitors {
4858 mon.overview_open = self.overview_open;
4859 mon.set_overview_progress(self.overview_progress.as_ref());
4860 }
4861 }
4862
4863 pub fn toggle_overview(&mut self) {
4864 self.overview_open = !self.overview_open;
4865
4866 let from = self.overview_progress.take().map_or(0., |p| p.value());
4867 let to = if self.overview_open { 1. } else { 0. };
4868
4869 self.overview_progress = Some(OverviewProgress::Animation(Animation::new(
4870 self.clock.clone(),
4871 from,
4872 to,
4873 0.,
4874 self.options.animations.overview_open_close.0,
4875 )));
4876
4877 self.set_monitors_overview_state();
4878 }
4879
4880 pub fn open_overview(&mut self) -> bool {
4881 if self.overview_open {
4882 return false;
4883 }
4884
4885 self.toggle_overview();
4886 true
4887 }
4888
4889 pub fn close_overview(&mut self) -> bool {
4890 if !self.overview_open {
4891 return false;
4892 }
4893
4894 self.toggle_overview();
4895 true
4896 }
4897
4898 pub fn toggle_overview_to_workspace(&mut self, ws_idx: usize) {
4899 let config = self.options.animations.overview_open_close.0;
4900 if let Some(mon) = self.active_monitor() {
4901 mon.activate_workspace_with_anim_config(ws_idx, Some(config));
4902 }
4903 self.toggle_overview();
4904 }
4905
4906 pub fn start_open_animation_for_window(&mut self, window: &W::Id) {
4907 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
4908 if move_.tile.window().id() == window {
4909 return;
4910 }
4911 }
4912
4913 for ws in self.workspaces_mut() {
4914 if ws.start_open_animation(window) {
4915 return;
4916 }
4917 }
4918 }
4919
4920 pub fn store_unmap_snapshot(&mut self, renderer: &mut GlesRenderer, window: &W::Id) {
4921 let _span = tracy_client::span!("Layout::store_unmap_snapshot");
4922
4923 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
4924 if move_.tile.window().id() == window {
4925 move_.tile.store_unmap_snapshot_if_empty(renderer);
4926 return;
4927 }
4928 }
4929
4930 match &mut self.monitor_set {
4931 MonitorSet::Normal { monitors, .. } => {
4932 for mon in monitors {
4933 for ws in &mut mon.workspaces {
4934 if ws.has_window(window) {
4935 ws.store_unmap_snapshot_if_empty(renderer, window);
4936 return;
4937 }
4938 }
4939 }
4940 }
4941 MonitorSet::NoOutputs { workspaces, .. } => {
4942 for ws in workspaces {
4943 if ws.has_window(window) {
4944 ws.store_unmap_snapshot_if_empty(renderer, window);
4945 return;
4946 }
4947 }
4948 }
4949 }
4950 }
4951
4952 pub fn clear_unmap_snapshot(&mut self, window: &W::Id) {
4953 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
4954 if move_.tile.window().id() == window {
4955 let _ = move_.tile.take_unmap_snapshot();
4956 return;
4957 }
4958 }
4959
4960 match &mut self.monitor_set {
4961 MonitorSet::Normal { monitors, .. } => {
4962 for mon in monitors {
4963 for ws in &mut mon.workspaces {
4964 if ws.has_window(window) {
4965 ws.clear_unmap_snapshot(window);
4966 return;
4967 }
4968 }
4969 }
4970 }
4971 MonitorSet::NoOutputs { workspaces, .. } => {
4972 for ws in workspaces {
4973 if ws.has_window(window) {
4974 ws.clear_unmap_snapshot(window);
4975 return;
4976 }
4977 }
4978 }
4979 }
4980 }
4981
4982 pub fn start_close_animation_for_window(
4983 &mut self,
4984 renderer: &mut GlesRenderer,
4985 window: &W::Id,
4986 blocker: TransactionBlocker,
4987 ) {
4988 let _span = tracy_client::span!("Layout::start_close_animation_for_window");
4989
4990 let zoom = self.overview_zoom();
4991
4992 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
4993 if move_.tile.window().id() == window {
4994 let Some(snapshot) = move_.tile.take_unmap_snapshot() else {
4995 return;
4996 };
4997 let tile_pos = move_.tile_render_location(zoom);
4998 let tile_size = move_.tile.tile_size();
4999
5000 let output = move_.output.clone();
5001 let pointer_pos_within_output = move_.pointer_pos_within_output;
5002 let Some(mon) = self.monitor_for_output_mut(&output) else {
5003 return;
5004 };
5005 let Some((ws, ws_geo)) = mon.workspace_under(pointer_pos_within_output) else {
5006 return;
5007 };
5008 let ws_id = ws.id();
5009 let ws = mon
5010 .workspaces
5011 .iter_mut()
5012 .find(|ws| ws.id() == ws_id)
5013 .unwrap();
5014
5015 let tile_pos = tile_pos - ws_geo.loc;
5016 ws.start_close_animation_for_tile(renderer, snapshot, tile_size, tile_pos, blocker);
5017 return;
5018 }
5019 }
5020
5021 match &mut self.monitor_set {
5022 MonitorSet::Normal { monitors, .. } => {
5023 for mon in monitors {
5024 for ws in &mut mon.workspaces {
5025 if ws.has_window(window) {
5026 ws.start_close_animation_for_window(renderer, window, blocker);
5027 return;
5028 }
5029 }
5030 }
5031 }
5032 MonitorSet::NoOutputs { workspaces, .. } => {
5033 for ws in workspaces {
5034 if ws.has_window(window) {
5035 ws.start_close_animation_for_window(renderer, window, blocker);
5036 return;
5037 }
5038 }
5039 }
5040 }
5041 }
5042
5043 pub fn render_interactive_move_for_output<'a, R: NiriRenderer + 'a>(
5044 &'a self,
5045 renderer: &mut R,
5046 output: &Output,
5047 target: RenderTarget,
5048 ) -> impl Iterator<Item = RescaleRenderElement<TileRenderElement<R>>> + 'a {
5049 if self.update_render_elements_time != self.clock.now() {
5050 error!("clock moved between updating render elements and rendering");
5051 }
5052
5053 let mut rv = None;
5054
5055 if let Some(InteractiveMoveState::Moving(move_)) = &self.interactive_move {
5056 if &move_.output == output {
5057 let scale = Scale::from(move_.output.current_scale().fractional_scale());
5058 let zoom = self.overview_zoom();
5059 let location = move_.tile_render_location(zoom);
5060 let iter = move_
5061 .tile
5062 .render(renderer, location, true, target)
5063 .map(move |elem| {
5064 RescaleRenderElement::from_element(
5065 elem,
5066 location.to_physical_precise_round(scale),
5067 zoom,
5068 )
5069 });
5070 rv = Some(iter);
5071 }
5072 }
5073
5074 rv.into_iter().flatten()
5075 }
5076
5077 pub fn refresh(&mut self, is_active: bool) {
5078 let _span = tracy_client::span!("Layout::refresh");
5079
5080 self.is_active = is_active;
5081
5082 let mut ongoing_scrolling_dnd = self.dnd.is_some().then_some(true);
5083
5084 if let Some(InteractiveMoveState::Moving(move_)) = &mut self.interactive_move {
5085 let win = move_.tile.window_mut();
5086
5087 win.set_active_in_column(true);
5088 win.set_floating(move_.is_floating);
5089 win.set_activated(true);
5090
5091 win.set_interactive_resize(None);
5092
5093 win.set_bounds(output_size(&move_.output).to_i32_round());
5094
5095 win.send_pending_configure();
5096 win.refresh();
5097
5098 ongoing_scrolling_dnd.get_or_insert(!move_.is_floating);
5099 } else if let Some(InteractiveMoveState::Starting { window_id, .. }) =
5100 &self.interactive_move
5101 {
5102 ongoing_scrolling_dnd.get_or_insert_with(|| {
5103 let (_, _, ws) = self
5104 .workspaces()
5105 .find(|(_, _, ws)| ws.has_window(window_id))
5106 .unwrap();
5107 !ws.is_floating(window_id)
5108 });
5109 }
5110
5111 match &mut self.monitor_set {
5112 MonitorSet::Normal {
5113 monitors,
5114 active_monitor_idx,
5115 ..
5116 } => {
5117 for (idx, mon) in monitors.iter_mut().enumerate() {
5118 let is_active = self.is_active
5119 && idx == *active_monitor_idx
5120 && !matches!(self.interactive_move, Some(InteractiveMoveState::Moving(_)));
5121
5122 if ongoing_scrolling_dnd.is_some() && self.overview_open {
5123 // Begin the scroll on new monitors and when opening the overview.
5124 mon.dnd_scroll_gesture_begin();
5125 } else if !self.overview_open {
5126 mon.dnd_scroll_gesture_end();
5127 }
5128
5129 for (ws_idx, ws) in mon.workspaces.iter_mut().enumerate() {
5130 let is_focused = is_active && ws_idx == mon.active_workspace_idx;
5131 ws.refresh(is_active, is_focused);
5132
5133 if let Some(is_scrolling) = ongoing_scrolling_dnd {
5134 // Lock or unlock the view for scrolling interactive move.
5135 if is_scrolling {
5136 ws.dnd_scroll_gesture_begin();
5137 } else {
5138 ws.dnd_scroll_gesture_end();
5139 }
5140 } else {
5141 // Cancel the view offset gesture after workspace switches, moves, etc.
5142 if !self.overview_open && ws_idx != mon.active_workspace_idx {
5143 ws.view_offset_gesture_end(None);
5144 }
5145 }
5146 }
5147 }
5148 }
5149 MonitorSet::NoOutputs { workspaces, .. } => {
5150 for ws in workspaces {
5151 ws.refresh(false, false);
5152 ws.view_offset_gesture_end(None);
5153 }
5154 }
5155 }
5156 }
5157
5158 pub fn workspaces(
5159 &self,
5160 ) -> impl Iterator<Item = (Option<&Monitor<W>>, usize, &Workspace<W>)> + '_ {
5161 let iter_normal;
5162 let iter_no_outputs;
5163
5164 match &self.monitor_set {
5165 MonitorSet::Normal { monitors, .. } => {
5166 let it = monitors.iter().flat_map(|mon| {
5167 mon.workspaces
5168 .iter()
5169 .enumerate()
5170 .map(move |(idx, ws)| (Some(mon), idx, ws))
5171 });
5172
5173 iter_normal = Some(it);
5174 iter_no_outputs = None;
5175 }
5176 MonitorSet::NoOutputs { workspaces } => {
5177 let it = workspaces
5178 .iter()
5179 .enumerate()
5180 .map(|(idx, ws)| (None, idx, ws));
5181
5182 iter_normal = None;
5183 iter_no_outputs = Some(it);
5184 }
5185 }
5186
5187 let iter_normal = iter_normal.into_iter().flatten();
5188 let iter_no_outputs = iter_no_outputs.into_iter().flatten();
5189 iter_normal.chain(iter_no_outputs)
5190 }
5191
5192 pub fn workspaces_mut(&mut self) -> impl Iterator<Item = &mut Workspace<W>> + '_ {
5193 let iter_normal;
5194 let iter_no_outputs;
5195
5196 match &mut self.monitor_set {
5197 MonitorSet::Normal { monitors, .. } => {
5198 let it = monitors
5199 .iter_mut()
5200 .flat_map(|mon| mon.workspaces.iter_mut());
5201
5202 iter_normal = Some(it);
5203 iter_no_outputs = None;
5204 }
5205 MonitorSet::NoOutputs { workspaces } => {
5206 let it = workspaces.iter_mut();
5207
5208 iter_normal = None;
5209 iter_no_outputs = Some(it);
5210 }
5211 }
5212
5213 let iter_normal = iter_normal.into_iter().flatten();
5214 let iter_no_outputs = iter_no_outputs.into_iter().flatten();
5215 iter_normal.chain(iter_no_outputs)
5216 }
5217
5218 pub fn windows(&self) -> impl Iterator<Item = (Option<&Monitor<W>>, &W)> {
5219 let moving_window = self
5220 .interactive_move
5221 .as_ref()
5222 .and_then(|x| x.moving())
5223 .map(|move_| (self.monitor_for_output(&move_.output), move_.tile.window()))
5224 .into_iter();
5225
5226 let rest = self
5227 .workspaces()
5228 .flat_map(|(mon, _, ws)| ws.windows().map(move |win| (mon, win)));
5229
5230 moving_window.chain(rest)
5231 }
5232
5233 pub fn has_window(&self, window: &W::Id) -> bool {
5234 self.windows().any(|(_, win)| win.id() == window)
5235 }
5236
5237 pub fn is_overview_open(&self) -> bool {
5238 self.overview_open
5239 }
5240
5241 fn resolve_scrolling_width(&self, window: &W, width: Option<PresetSize>) -> ColumnWidth {
5242 let width = width.unwrap_or_else(|| PresetSize::Fixed(window.size().w));
5243 match width {
5244 PresetSize::Fixed(fixed) => {
5245 let mut fixed = f64::from(fixed);
5246
5247 // Add border width since ColumnWidth includes borders.
5248 let rules = window.rules();
5249 let border = rules.border.resolve_against(self.options.border);
5250 if !border.off {
5251 fixed += border.width.0 * 2.;
5252 }
5253
5254 ColumnWidth::Fixed(fixed)
5255 }
5256 PresetSize::Proportion(prop) => ColumnWidth::Proportion(prop),
5257 }
5258 }
5259}
5260
5261impl<W: LayoutElement> Default for MonitorSet<W> {
5262 fn default() -> Self {
5263 Self::NoOutputs { workspaces: vec![] }
5264 }
5265}
5266
5267fn compute_overview_zoom(options: &Options, overview_progress: Option<f64>) -> f64 {
5268 // Clamp to some sane values.
5269 let zoom = options.overview.zoom.0.clamp(0.0001, 0.75);
5270
5271 if let Some(p) = overview_progress {
5272 (1. - p * (1. - zoom)).max(0.0001)
5273 } else {
5274 1.
5275 }
5276}