forge
Explore
Log in

Don't you dare go Hollow

niri

Clone
★ 0

0 watching0 forksRustGPL-3.012.1 MiBea3bbeaIvan Molodetskikh1 year ago2422 commits

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