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