forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBce9dd33sodiboo1 year ago2737 commits

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