forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBad3847fIvan Molodetskikh1 year ago2457 commits

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