forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBa6baef7Anton Kesy10 months ago2623 commits

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