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