forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiB90ef202Ivan Molodetskikh1 year ago2349 commits

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