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