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