niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·e259061·Ivan Molodetskikh·2 years ago·424 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::{max, min}; |
| 33 | use std::iter::zip; |
| 34 | use std::mem; |
| 35 | use std::rc::Rc; |
| 36 | use std::time::Duration; |
| 37 | |
| 38 | use arrayvec::ArrayVec; |
| 39 | use smithay::backend::renderer::element::solid::{SolidColorBuffer, SolidColorRenderElement}; |
| 40 | use smithay::backend::renderer::element::surface::WaylandSurfaceRenderElement; |
| 41 | use smithay::backend::renderer::element::utils::{ |
| 42 | CropRenderElement, Relocate, RelocateRenderElement, |
| 43 | }; |
| 44 | use smithay::backend::renderer::element::{AsRenderElements, Kind}; |
| 45 | use smithay::backend::renderer::gles::GlesRenderer; |
| 46 | use smithay::backend::renderer::ImportAll; |
| 47 | use smithay::desktop::space::SpaceElement; |
| 48 | use smithay::desktop::{layer_map_for_output, Window}; |
| 49 | use smithay::output::Output; |
| 50 | use smithay::reexports::wayland_protocols::xdg::decoration::zv1::server::zxdg_toplevel_decoration_v1; |
| 51 | use smithay::reexports::wayland_protocols::xdg::shell::server::xdg_toplevel; |
| 52 | use smithay::reexports::wayland_server::protocol::wl_surface::WlSurface; |
| 53 | use smithay::render_elements; |
| 54 | use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform}; |
| 55 | use smithay::wayland::compositor::{send_surface_state, with_states}; |
| 56 | use smithay::wayland::shell::xdg::SurfaceCachedState; |
| 57 | |
| 58 | use crate::animation::Animation; |
| 59 | use crate::config::{self, Color, Config, PresetWidth, SizeChange}; |
| 60 | |
| 61 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 62 | pub struct OutputId(String); |
| 63 | |
| 64 | render_elements! { |
| 65 | #[derive(Debug)] |
| 66 | pub WorkspaceRenderElement<R> where R: ImportAll; |
| 67 | Wayland = WaylandSurfaceRenderElement<R>, |
| 68 | FocusRing = SolidColorRenderElement, |
| 69 | } |
| 70 | pub type MonitorRenderElement<R> = |
| 71 | RelocateRenderElement<CropRenderElement<WorkspaceRenderElement<R>>>; |
| 72 | |
| 73 | pub trait LayoutElement: SpaceElement + PartialEq + Clone { |
| 74 | fn request_size(&self, size: Size<i32, Logical>); |
| 75 | fn request_fullscreen(&self, size: Size<i32, Logical>); |
| 76 | fn min_size(&self) -> Size<i32, Logical>; |
| 77 | fn max_size(&self) -> Size<i32, Logical>; |
| 78 | fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool; |
| 79 | fn has_ssd(&self) -> bool; |
| 80 | fn set_preferred_scale_transform(&self, scale: i32, transform: Transform); |
| 81 | } |
| 82 | |
| 83 | #[derive(Debug)] |
| 84 | pub struct Layout<W: LayoutElement> { |
| 85 | /// Monitors and workspaes in the layout. |
| 86 | monitor_set: MonitorSet<W>, |
| 87 | /// Configurable properties of the layout. |
| 88 | options: Rc<Options>, |
| 89 | } |
| 90 | |
| 91 | #[derive(Debug)] |
| 92 | enum MonitorSet<W: LayoutElement> { |
| 93 | /// At least one output is connected. |
| 94 | Normal { |
| 95 | /// Connected monitors. |
| 96 | monitors: Vec<Monitor<W>>, |
| 97 | /// Index of the primary monitor. |
| 98 | primary_idx: usize, |
| 99 | /// Index of the active monitor. |
| 100 | active_monitor_idx: usize, |
| 101 | }, |
| 102 | /// No outputs are connected, and these are the workspaces. |
| 103 | NoOutputs { |
| 104 | /// The workspaces. |
| 105 | workspaces: Vec<Workspace<W>>, |
| 106 | }, |
| 107 | } |
| 108 | |
| 109 | #[derive(Debug)] |
| 110 | pub struct Monitor<W: LayoutElement> { |
| 111 | /// Output for this monitor. |
| 112 | output: Output, |
| 113 | // Must always contain at least one. |
| 114 | workspaces: Vec<Workspace<W>>, |
| 115 | /// Index of the currently active workspace. |
| 116 | active_workspace_idx: usize, |
| 117 | /// In-progress switch between workspaces. |
| 118 | workspace_switch: Option<WorkspaceSwitch>, |
| 119 | /// Configurable properties of the layout. |
| 120 | options: Rc<Options>, |
| 121 | } |
| 122 | |
| 123 | #[derive(Debug)] |
| 124 | enum WorkspaceSwitch { |
| 125 | Animation(Animation), |
| 126 | Gesture(WorkspaceSwitchGesture), |
| 127 | } |
| 128 | |
| 129 | #[derive(Debug)] |
| 130 | struct WorkspaceSwitchGesture { |
| 131 | /// Index of the workspace where the gesture was started. |
| 132 | center_idx: usize, |
| 133 | /// Current, fractional workspace index. |
| 134 | current_idx: f64, |
| 135 | } |
| 136 | |
| 137 | #[derive(Debug)] |
| 138 | pub struct Workspace<W: LayoutElement> { |
| 139 | /// The original output of this workspace. |
| 140 | /// |
| 141 | /// Most of the time this will be the workspace's current output, however, after an output |
| 142 | /// disconnection, it may remain pointing to the disconnected output. |
| 143 | original_output: OutputId, |
| 144 | |
| 145 | /// Current output of this workspace. |
| 146 | output: Option<Output>, |
| 147 | |
| 148 | /// Latest known view size for this workspace. |
| 149 | /// |
| 150 | /// This should be computed from the current workspace output size, or, if all outputs have |
| 151 | /// been disconnected, preserved until a new output is connected. |
| 152 | view_size: Size<i32, Logical>, |
| 153 | |
| 154 | /// Latest known working area for this workspace. |
| 155 | /// |
| 156 | /// This is similar to view size, but takes into account things like layer shell exclusive |
| 157 | /// zones. |
| 158 | working_area: Rectangle<i32, Logical>, |
| 159 | |
| 160 | /// Columns of windows on this workspace. |
| 161 | columns: Vec<Column<W>>, |
| 162 | |
| 163 | /// Index of the currently active column, if any. |
| 164 | active_column_idx: usize, |
| 165 | |
| 166 | /// Focus ring buffer and parameters. |
| 167 | focus_ring: FocusRing, |
| 168 | |
| 169 | /// Offset of the view computed from the active column. |
| 170 | /// |
| 171 | /// Any gaps, including left padding from work area left exclusive zone, is handled |
| 172 | /// with this view offset (rather than added as a constant elsewhere in the code). This allows |
| 173 | /// for natural handling of fullscreen windows, which must ignore work area padding. |
| 174 | view_offset: i32, |
| 175 | |
| 176 | /// Animation of the view offset, if one is currently ongoing. |
| 177 | view_offset_anim: Option<Animation>, |
| 178 | |
| 179 | /// Whether to activate the previous, rather than the next, column upon column removal. |
| 180 | /// |
| 181 | /// When a new column is created and removed with no focus changes in-between, it is more |
| 182 | /// natural to activate the previously-focused column. This variable tracks that. |
| 183 | /// |
| 184 | /// Since we only create-and-activate columns immediately to the right of the active column (in |
| 185 | /// contrast to tabs in Firefox, for example), we can track this as a bool, rather than an |
| 186 | /// index of the previous column to activate. |
| 187 | activate_prev_column_on_removal: bool, |
| 188 | |
| 189 | /// Configurable properties of the layout. |
| 190 | options: Rc<Options>, |
| 191 | } |
| 192 | |
| 193 | #[derive(Debug)] |
| 194 | struct FocusRing { |
| 195 | buffers: [SolidColorBuffer; 4], |
| 196 | locations: [Point<i32, Logical>; 4], |
| 197 | is_off: bool, |
| 198 | is_border: bool, |
| 199 | width: i32, |
| 200 | active_color: Color, |
| 201 | inactive_color: Color, |
| 202 | } |
| 203 | |
| 204 | #[derive(Debug, PartialEq)] |
| 205 | struct Options { |
| 206 | /// Padding around windows in logical pixels. |
| 207 | gaps: i32, |
| 208 | focus_ring: config::FocusRing, |
| 209 | /// Column widths that `toggle_width()` switches between. |
| 210 | preset_widths: Vec<ColumnWidth>, |
| 211 | /// Initial width for new windows. |
| 212 | default_width: Option<ColumnWidth>, |
| 213 | } |
| 214 | |
| 215 | impl Default for Options { |
| 216 | fn default() -> Self { |
| 217 | Self { |
| 218 | gaps: 16, |
| 219 | focus_ring: Default::default(), |
| 220 | preset_widths: vec![ |
| 221 | ColumnWidth::Proportion(1. / 3.), |
| 222 | ColumnWidth::Proportion(0.5), |
| 223 | ColumnWidth::Proportion(2. / 3.), |
| 224 | ], |
| 225 | default_width: None, |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | impl Options { |
| 231 | fn from_config(config: &Config) -> Self { |
| 232 | let preset_column_widths = &config.preset_column_widths; |
| 233 | |
| 234 | let preset_widths = if preset_column_widths.is_empty() { |
| 235 | Options::default().preset_widths |
| 236 | } else { |
| 237 | preset_column_widths |
| 238 | .iter() |
| 239 | .copied() |
| 240 | .map(ColumnWidth::from) |
| 241 | .collect() |
| 242 | }; |
| 243 | |
| 244 | // Missing default_column_width maps to Some(ColumnWidth::Proportion(0.5)), |
| 245 | // while present, but empty, maps to None. |
| 246 | let default_width = config |
| 247 | .default_column_width |
| 248 | .as_ref() |
| 249 | .map(|w| w.0.first().copied().map(ColumnWidth::from)) |
| 250 | .unwrap_or(Some(ColumnWidth::Proportion(0.5))); |
| 251 | |
| 252 | Self { |
| 253 | gaps: config.gaps.into(), |
| 254 | focus_ring: config.focus_ring, |
| 255 | preset_widths, |
| 256 | default_width, |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /// Width of a column. |
| 262 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 263 | pub enum ColumnWidth { |
| 264 | /// Proportion of the current view width. |
| 265 | Proportion(f64), |
| 266 | /// One of the proportion presets. |
| 267 | /// |
| 268 | /// This is separate from Proportion in order to be able to reliably cycle between preset |
| 269 | /// proportions. |
| 270 | Preset(usize), |
| 271 | /// Fixed width in logical pixels. |
| 272 | Fixed(i32), |
| 273 | } |
| 274 | |
| 275 | impl From<PresetWidth> for ColumnWidth { |
| 276 | fn from(value: PresetWidth) -> Self { |
| 277 | match value { |
| 278 | PresetWidth::Proportion(p) => Self::Proportion(p.clamp(0., 10000.)), |
| 279 | PresetWidth::Fixed(f) => Self::Fixed(f.clamp(1, 100000)), |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /// Height of a window in a column. |
| 285 | /// |
| 286 | /// Proportional height is intentionally omitted. With column widths you frequently want e.g. two |
| 287 | /// columns side-by-side with 50% width each, and you want them to remain this way when moving to a |
| 288 | /// differently sized monitor. Windows in a column, however, already auto-size to fill the available |
| 289 | /// height, giving you this behavior. The only reason to set a different window height, then, is |
| 290 | /// when you want something in the window to fit exactly, e.g. to fit 30 lines in a terminal, which |
| 291 | /// corresponds to the `Fixed` variant. |
| 292 | /// |
| 293 | /// This does not preclude the usual set of binds to set or resize a window proportionally. Just, |
| 294 | /// they are converted to, and stored as fixed height right away, so that once you resize a window |
| 295 | /// to fit the desired content, it can never become smaller than that when moving between monitors. |
| 296 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 297 | pub enum WindowHeight { |
| 298 | /// Automatically computed height, evenly distributed across the column. |
| 299 | Auto, |
| 300 | /// Fixed height in logical pixels. |
| 301 | Fixed(i32), |
| 302 | } |
| 303 | |
| 304 | #[derive(Debug)] |
| 305 | struct Column<W: LayoutElement> { |
| 306 | /// Windows in this column. |
| 307 | /// |
| 308 | /// Must be non-empty. |
| 309 | windows: Vec<W>, |
| 310 | |
| 311 | /// Heights of the windows. |
| 312 | /// |
| 313 | /// Must have the same number of elements as `windows`. |
| 314 | heights: Vec<WindowHeight>, |
| 315 | |
| 316 | /// Index of the currently active window. |
| 317 | active_window_idx: usize, |
| 318 | |
| 319 | /// Desired width of this column. |
| 320 | /// |
| 321 | /// If the column is full-width or full-screened, this is the width that should be restored |
| 322 | /// upon unfullscreening and untoggling full-width. |
| 323 | width: ColumnWidth, |
| 324 | |
| 325 | /// Whether this column is full-width. |
| 326 | is_full_width: bool, |
| 327 | |
| 328 | /// Whether this column contains a single full-screened window. |
| 329 | is_fullscreen: bool, |
| 330 | |
| 331 | /// Latest known view size for this column's workspace. |
| 332 | view_size: Size<i32, Logical>, |
| 333 | |
| 334 | /// Latest known working area for this column's workspace. |
| 335 | working_area: Rectangle<i32, Logical>, |
| 336 | |
| 337 | /// Configurable properties of the layout. |
| 338 | options: Rc<Options>, |
| 339 | } |
| 340 | |
| 341 | impl OutputId { |
| 342 | pub fn new(output: &Output) -> Self { |
| 343 | Self(output.name()) |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | impl LayoutElement for Window { |
| 348 | fn request_size(&self, size: Size<i32, Logical>) { |
| 349 | self.toplevel().with_pending_state(|state| { |
| 350 | state.size = Some(size); |
| 351 | state.states.unset(xdg_toplevel::State::Fullscreen); |
| 352 | }); |
| 353 | } |
| 354 | |
| 355 | fn request_fullscreen(&self, size: Size<i32, Logical>) { |
| 356 | self.toplevel().with_pending_state(|state| { |
| 357 | state.size = Some(size); |
| 358 | state.states.set(xdg_toplevel::State::Fullscreen); |
| 359 | }); |
| 360 | } |
| 361 | |
| 362 | fn min_size(&self) -> Size<i32, Logical> { |
| 363 | with_states(self.toplevel().wl_surface(), |state| { |
| 364 | let curr = state.cached_state.current::<SurfaceCachedState>(); |
| 365 | curr.min_size |
| 366 | }) |
| 367 | } |
| 368 | |
| 369 | fn max_size(&self) -> Size<i32, Logical> { |
| 370 | with_states(self.toplevel().wl_surface(), |state| { |
| 371 | let curr = state.cached_state.current::<SurfaceCachedState>(); |
| 372 | curr.max_size |
| 373 | }) |
| 374 | } |
| 375 | |
| 376 | fn is_wl_surface(&self, wl_surface: &WlSurface) -> bool { |
| 377 | self.toplevel().wl_surface() == wl_surface |
| 378 | } |
| 379 | |
| 380 | fn set_preferred_scale_transform(&self, scale: i32, transform: Transform) { |
| 381 | self.with_surfaces(|surface, data| { |
| 382 | send_surface_state(surface, data, scale, transform); |
| 383 | }); |
| 384 | } |
| 385 | |
| 386 | fn has_ssd(&self) -> bool { |
| 387 | self.toplevel().current_state().decoration_mode |
| 388 | == Some(zxdg_toplevel_decoration_v1::Mode::ServerSide) |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | impl FocusRing { |
| 393 | fn update( |
| 394 | &mut self, |
| 395 | win_pos: Point<i32, Logical>, |
| 396 | win_size: Size<i32, Logical>, |
| 397 | is_border: bool, |
| 398 | ) { |
| 399 | if is_border { |
| 400 | self.buffers[0].resize((win_size.w + self.width * 2, self.width)); |
| 401 | self.buffers[1].resize((win_size.w + self.width * 2, self.width)); |
| 402 | self.buffers[2].resize((self.width, win_size.h)); |
| 403 | self.buffers[3].resize((self.width, win_size.h)); |
| 404 | |
| 405 | self.locations[0] = win_pos + Point::from((-self.width, -self.width)); |
| 406 | self.locations[1] = win_pos + Point::from((-self.width, win_size.h)); |
| 407 | self.locations[2] = win_pos + Point::from((-self.width, 0)); |
| 408 | self.locations[3] = win_pos + Point::from((win_size.w, 0)); |
| 409 | } else { |
| 410 | let size = win_size + Size::from((self.width * 2, self.width * 2)); |
| 411 | self.buffers[0].resize(size); |
| 412 | self.locations[0] = win_pos - Point::from((self.width, self.width)); |
| 413 | } |
| 414 | |
| 415 | self.is_border = is_border; |
| 416 | } |
| 417 | |
| 418 | fn set_active(&mut self, is_active: bool) { |
| 419 | let color = if is_active { |
| 420 | self.active_color.into() |
| 421 | } else { |
| 422 | self.inactive_color.into() |
| 423 | }; |
| 424 | |
| 425 | for buf in &mut self.buffers { |
| 426 | buf.set_color(color); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | fn render(&self, scale: Scale<f64>) -> impl Iterator<Item = SolidColorRenderElement> { |
| 431 | let mut rv = ArrayVec::<_, 4>::new(); |
| 432 | |
| 433 | if self.is_off { |
| 434 | return rv.into_iter(); |
| 435 | } |
| 436 | |
| 437 | let mut push = |buffer, location: Point<i32, Logical>| { |
| 438 | let elem = SolidColorRenderElement::from_buffer( |
| 439 | buffer, |
| 440 | location.to_physical_precise_round(scale), |
| 441 | scale, |
| 442 | 1., |
| 443 | Kind::Unspecified, |
| 444 | ); |
| 445 | rv.push(elem); |
| 446 | }; |
| 447 | |
| 448 | if self.is_border { |
| 449 | for (buf, loc) in zip(&self.buffers, self.locations) { |
| 450 | push(buf, loc); |
| 451 | } |
| 452 | } else { |
| 453 | push(&self.buffers[0], self.locations[0]); |
| 454 | } |
| 455 | |
| 456 | rv.into_iter() |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | impl FocusRing { |
| 461 | fn new(config: config::FocusRing) -> Self { |
| 462 | Self { |
| 463 | buffers: Default::default(), |
| 464 | locations: Default::default(), |
| 465 | is_off: config.off, |
| 466 | is_border: false, |
| 467 | width: config.width.into(), |
| 468 | active_color: config.active_color, |
| 469 | inactive_color: config.inactive_color, |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | impl WorkspaceSwitch { |
| 475 | fn current_idx(&self) -> f64 { |
| 476 | match self { |
| 477 | WorkspaceSwitch::Animation(anim) => anim.value(), |
| 478 | WorkspaceSwitch::Gesture(gesture) => gesture.current_idx, |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | /// Returns `true` if the workspace switch is [`Animation`]. |
| 483 | /// |
| 484 | /// [`Animation`]: WorkspaceSwitch::Animation |
| 485 | #[must_use] |
| 486 | fn is_animation(&self) -> bool { |
| 487 | matches!(self, Self::Animation(..)) |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | impl ColumnWidth { |
| 492 | fn resolve(self, options: &Options, view_width: i32) -> i32 { |
| 493 | match self { |
| 494 | ColumnWidth::Proportion(proportion) => { |
| 495 | ((view_width - options.gaps) as f64 * proportion).floor() as i32 - options.gaps |
| 496 | } |
| 497 | ColumnWidth::Preset(idx) => options.preset_widths[idx].resolve(options, view_width), |
| 498 | ColumnWidth::Fixed(width) => width, |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | impl<W: LayoutElement> Layout<W> { |
| 504 | pub fn new(config: &Config) -> Self { |
| 505 | Self { |
| 506 | monitor_set: MonitorSet::NoOutputs { workspaces: vec![] }, |
| 507 | options: Rc::new(Options::from_config(config)), |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | pub fn add_output(&mut self, output: Output) { |
| 512 | let id = OutputId::new(&output); |
| 513 | |
| 514 | self.monitor_set = match mem::take(&mut self.monitor_set) { |
| 515 | MonitorSet::Normal { |
| 516 | mut monitors, |
| 517 | primary_idx, |
| 518 | active_monitor_idx, |
| 519 | } => { |
| 520 | let primary = &mut monitors[primary_idx]; |
| 521 | |
| 522 | let mut workspaces = vec![]; |
| 523 | for i in (0..primary.workspaces.len()).rev() { |
| 524 | if primary.workspaces[i].original_output == id { |
| 525 | let ws = primary.workspaces.remove(i); |
| 526 | |
| 527 | // The user could've closed a window while remaining on this workspace, on |
| 528 | // another monitor. However, we will add an empty workspace in the end |
| 529 | // instead. |
| 530 | if ws.has_windows() { |
| 531 | workspaces.push(ws); |
| 532 | } |
| 533 | |
| 534 | if i <= primary.active_workspace_idx { |
| 535 | primary.active_workspace_idx = |
| 536 | primary.active_workspace_idx.saturating_sub(1); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | workspaces.reverse(); |
| 541 | |
| 542 | // Make sure there's always an empty workspace. |
| 543 | workspaces.push(Workspace::new(output.clone(), self.options.clone())); |
| 544 | |
| 545 | for ws in &mut workspaces { |
| 546 | ws.set_output(Some(output.clone())); |
| 547 | } |
| 548 | |
| 549 | monitors.push(Monitor::new(output, workspaces, self.options.clone())); |
| 550 | MonitorSet::Normal { |
| 551 | monitors, |
| 552 | primary_idx, |
| 553 | active_monitor_idx, |
| 554 | } |
| 555 | } |
| 556 | MonitorSet::NoOutputs { mut workspaces } => { |
| 557 | // We know there are no empty workspaces there, so add one. |
| 558 | workspaces.push(Workspace::new(output.clone(), self.options.clone())); |
| 559 | |
| 560 | for workspace in &mut workspaces { |
| 561 | workspace.set_output(Some(output.clone())); |
| 562 | } |
| 563 | |
| 564 | let monitor = Monitor::new(output, workspaces, self.options.clone()); |
| 565 | |
| 566 | MonitorSet::Normal { |
| 567 | monitors: vec![monitor], |
| 568 | primary_idx: 0, |
| 569 | active_monitor_idx: 0, |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | pub fn remove_output(&mut self, output: &Output) { |
| 576 | self.monitor_set = match mem::take(&mut self.monitor_set) { |
| 577 | MonitorSet::Normal { |
| 578 | mut monitors, |
| 579 | mut primary_idx, |
| 580 | mut active_monitor_idx, |
| 581 | } => { |
| 582 | let idx = monitors |
| 583 | .iter() |
| 584 | .position(|mon| &mon.output == output) |
| 585 | .expect("trying to remove non-existing output"); |
| 586 | let monitor = monitors.remove(idx); |
| 587 | let mut workspaces = monitor.workspaces; |
| 588 | |
| 589 | for ws in &mut workspaces { |
| 590 | ws.set_output(None); |
| 591 | } |
| 592 | |
| 593 | // Get rid of empty workspaces. |
| 594 | workspaces.retain(|ws| ws.has_windows()); |
| 595 | |
| 596 | if monitors.is_empty() { |
| 597 | // Removed the last monitor. |
| 598 | MonitorSet::NoOutputs { workspaces } |
| 599 | } else { |
| 600 | if primary_idx >= idx { |
| 601 | // Update primary_idx to either still point at the same monitor, or at some |
| 602 | // other monitor if the primary has been removed. |
| 603 | primary_idx = primary_idx.saturating_sub(1); |
| 604 | } |
| 605 | if active_monitor_idx >= idx { |
| 606 | // Update active_monitor_idx to either still point at the same monitor, or |
| 607 | // at some other monitor if the active monitor has |
| 608 | // been removed. |
| 609 | active_monitor_idx = active_monitor_idx.saturating_sub(1); |
| 610 | } |
| 611 | |
| 612 | let primary = &mut monitors[primary_idx]; |
| 613 | for ws in &mut workspaces { |
| 614 | ws.set_output(Some(primary.output.clone())); |
| 615 | } |
| 616 | |
| 617 | let empty_was_focused = |
| 618 | primary.active_workspace_idx == primary.workspaces.len() - 1; |
| 619 | |
| 620 | // Push the workspaces from the removed monitor in the end, right before the |
| 621 | // last, empty, workspace. |
| 622 | let empty = primary.workspaces.remove(primary.workspaces.len() - 1); |
| 623 | primary.workspaces.extend(workspaces); |
| 624 | primary.workspaces.push(empty); |
| 625 | |
| 626 | // If the empty workspace was focused on the primary monitor, keep it focused. |
| 627 | if empty_was_focused { |
| 628 | primary.active_workspace_idx = primary.workspaces.len() - 1; |
| 629 | } |
| 630 | |
| 631 | MonitorSet::Normal { |
| 632 | monitors, |
| 633 | primary_idx, |
| 634 | active_monitor_idx, |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | MonitorSet::NoOutputs { .. } => { |
| 639 | panic!("tried to remove output when there were already none") |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | pub fn add_window_by_idx( |
| 645 | &mut self, |
| 646 | monitor_idx: usize, |
| 647 | workspace_idx: usize, |
| 648 | window: W, |
| 649 | activate: bool, |
| 650 | width: ColumnWidth, |
| 651 | is_full_width: bool, |
| 652 | ) { |
| 653 | let MonitorSet::Normal { |
| 654 | monitors, |
| 655 | active_monitor_idx, |
| 656 | .. |
| 657 | } = &mut self.monitor_set |
| 658 | else { |
| 659 | panic!() |
| 660 | }; |
| 661 | |
| 662 | monitors[monitor_idx].add_window(workspace_idx, window, activate, width, is_full_width); |
| 663 | |
| 664 | if activate { |
| 665 | *active_monitor_idx = monitor_idx; |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | /// Adds a new window to the layout. |
| 670 | /// |
| 671 | /// Returns an output that the window was added to, if there were any outputs. |
| 672 | pub fn add_window( |
| 673 | &mut self, |
| 674 | window: W, |
| 675 | width: Option<ColumnWidth>, |
| 676 | is_full_width: bool, |
| 677 | ) -> Option<&Output> { |
| 678 | let width = width |
| 679 | .or(self.options.default_width) |
| 680 | .unwrap_or_else(|| ColumnWidth::Fixed(window.geometry().size.w)); |
| 681 | |
| 682 | match &mut self.monitor_set { |
| 683 | MonitorSet::Normal { |
| 684 | monitors, |
| 685 | active_monitor_idx, |
| 686 | .. |
| 687 | } => { |
| 688 | let mon = &mut monitors[*active_monitor_idx]; |
| 689 | |
| 690 | // Don't steal focus from an active fullscreen window. |
| 691 | let mut activate = true; |
| 692 | let ws = &mon.workspaces[mon.active_workspace_idx]; |
| 693 | if !ws.columns.is_empty() && ws.columns[ws.active_column_idx].is_fullscreen { |
| 694 | activate = false; |
| 695 | } |
| 696 | |
| 697 | mon.add_window( |
| 698 | mon.active_workspace_idx, |
| 699 | window, |
| 700 | activate, |
| 701 | width, |
| 702 | is_full_width, |
| 703 | ); |
| 704 | Some(&mon.output) |
| 705 | } |
| 706 | MonitorSet::NoOutputs { workspaces } => { |
| 707 | let ws = if let Some(ws) = workspaces.get_mut(0) { |
| 708 | ws |
| 709 | } else { |
| 710 | workspaces.push(Workspace::new_no_outputs(self.options.clone())); |
| 711 | &mut workspaces[0] |
| 712 | }; |
| 713 | ws.add_window(window, true, width, is_full_width); |
| 714 | None |
| 715 | } |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | pub fn remove_window(&mut self, window: &W) { |
| 720 | match &mut self.monitor_set { |
| 721 | MonitorSet::Normal { monitors, .. } => { |
| 722 | for mon in monitors { |
| 723 | for (idx, ws) in mon.workspaces.iter_mut().enumerate() { |
| 724 | if ws.has_window(window) { |
| 725 | ws.remove_window(window); |
| 726 | |
| 727 | // Clean up empty workspaces that are not active and not last. |
| 728 | if !ws.has_windows() |
| 729 | && idx != mon.active_workspace_idx |
| 730 | && idx != mon.workspaces.len() - 1 |
| 731 | { |
| 732 | mon.workspaces.remove(idx); |
| 733 | |
| 734 | if idx < mon.active_workspace_idx { |
| 735 | mon.active_workspace_idx -= 1; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | break; |
| 740 | } |
| 741 | } |
| 742 | } |
| 743 | } |
| 744 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 745 | for (idx, ws) in workspaces.iter_mut().enumerate() { |
| 746 | if ws.has_window(window) { |
| 747 | ws.remove_window(window); |
| 748 | |
| 749 | // Clean up empty workspaces. |
| 750 | if !ws.has_windows() { |
| 751 | workspaces.remove(idx); |
| 752 | } |
| 753 | |
| 754 | break; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | pub fn update_window(&mut self, window: &W) { |
| 762 | match &mut self.monitor_set { |
| 763 | MonitorSet::Normal { monitors, .. } => { |
| 764 | for mon in monitors { |
| 765 | for ws in &mut mon.workspaces { |
| 766 | if ws.has_window(window) { |
| 767 | ws.update_window(window); |
| 768 | return; |
| 769 | } |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 774 | for ws in workspaces { |
| 775 | if ws.has_window(window) { |
| 776 | ws.update_window(window); |
| 777 | return; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | pub fn find_window_and_output(&self, wl_surface: &WlSurface) -> Option<(W, Output)> { |
| 785 | if let MonitorSet::Normal { monitors, .. } = &self.monitor_set { |
| 786 | for mon in monitors { |
| 787 | for ws in &mon.workspaces { |
| 788 | if let Some(window) = ws.find_wl_surface(wl_surface) { |
| 789 | return Some((window.clone(), mon.output.clone())); |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | None |
| 796 | } |
| 797 | |
| 798 | pub fn window_y(&self, window: &W) -> Option<i32> { |
| 799 | match &self.monitor_set { |
| 800 | MonitorSet::Normal { monitors, .. } => { |
| 801 | for mon in monitors { |
| 802 | for ws in &mon.workspaces { |
| 803 | for col in &ws.columns { |
| 804 | if let Some(idx) = col.windows.iter().position(|w| w == window) { |
| 805 | return Some(col.window_y(idx)); |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 812 | for ws in workspaces { |
| 813 | for col in &ws.columns { |
| 814 | if let Some(idx) = col.windows.iter().position(|w| w == window) { |
| 815 | return Some(col.window_y(idx)); |
| 816 | } |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | None |
| 823 | } |
| 824 | |
| 825 | pub fn update_output_size(&mut self, output: &Output) { |
| 826 | let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set else { |
| 827 | panic!() |
| 828 | }; |
| 829 | |
| 830 | for mon in monitors { |
| 831 | if &mon.output == output { |
| 832 | let view_size = output_size(output); |
| 833 | let working_area = layer_map_for_output(output).non_exclusive_zone(); |
| 834 | |
| 835 | for ws in &mut mon.workspaces { |
| 836 | ws.set_view_size(view_size, working_area); |
| 837 | } |
| 838 | |
| 839 | break; |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | pub fn activate_window(&mut self, window: &W) { |
| 845 | let MonitorSet::Normal { |
| 846 | monitors, |
| 847 | active_monitor_idx, |
| 848 | .. |
| 849 | } = &mut self.monitor_set |
| 850 | else { |
| 851 | todo!() |
| 852 | }; |
| 853 | |
| 854 | for (monitor_idx, mon) in monitors.iter_mut().enumerate() { |
| 855 | for (workspace_idx, ws) in mon.workspaces.iter_mut().enumerate() { |
| 856 | if ws.has_window(window) { |
| 857 | *active_monitor_idx = monitor_idx; |
| 858 | ws.activate_window(window); |
| 859 | |
| 860 | // Switch to that workspace if not already during a transition. |
| 861 | if mon.workspace_switch.is_none() { |
| 862 | mon.switch_workspace(workspace_idx); |
| 863 | } |
| 864 | |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | pub fn activate_output(&mut self, output: &Output) { |
| 872 | let MonitorSet::Normal { |
| 873 | monitors, |
| 874 | active_monitor_idx, |
| 875 | .. |
| 876 | } = &mut self.monitor_set |
| 877 | else { |
| 878 | return; |
| 879 | }; |
| 880 | |
| 881 | let idx = monitors |
| 882 | .iter() |
| 883 | .position(|mon| &mon.output == output) |
| 884 | .unwrap(); |
| 885 | *active_monitor_idx = idx; |
| 886 | } |
| 887 | |
| 888 | pub fn active_output(&self) -> Option<&Output> { |
| 889 | let MonitorSet::Normal { |
| 890 | monitors, |
| 891 | active_monitor_idx, |
| 892 | .. |
| 893 | } = &self.monitor_set |
| 894 | else { |
| 895 | return None; |
| 896 | }; |
| 897 | |
| 898 | Some(&monitors[*active_monitor_idx].output) |
| 899 | } |
| 900 | |
| 901 | pub fn active_workspace(&self) -> Option<&Workspace<W>> { |
| 902 | let MonitorSet::Normal { |
| 903 | monitors, |
| 904 | active_monitor_idx, |
| 905 | .. |
| 906 | } = &self.monitor_set |
| 907 | else { |
| 908 | return None; |
| 909 | }; |
| 910 | |
| 911 | let mon = &monitors[*active_monitor_idx]; |
| 912 | Some(&mon.workspaces[mon.active_workspace_idx]) |
| 913 | } |
| 914 | |
| 915 | pub fn active_window(&self) -> Option<(W, Output)> { |
| 916 | let MonitorSet::Normal { |
| 917 | monitors, |
| 918 | active_monitor_idx, |
| 919 | .. |
| 920 | } = &self.monitor_set |
| 921 | else { |
| 922 | return None; |
| 923 | }; |
| 924 | |
| 925 | let mon = &monitors[*active_monitor_idx]; |
| 926 | let ws = &mon.workspaces[mon.active_workspace_idx]; |
| 927 | |
| 928 | if ws.columns.is_empty() { |
| 929 | return None; |
| 930 | } |
| 931 | |
| 932 | let col = &ws.columns[ws.active_column_idx]; |
| 933 | Some(( |
| 934 | col.windows[col.active_window_idx].clone(), |
| 935 | mon.output.clone(), |
| 936 | )) |
| 937 | } |
| 938 | |
| 939 | pub fn windows_for_output(&self, output: &Output) -> impl Iterator<Item = &W> + '_ { |
| 940 | let MonitorSet::Normal { monitors, .. } = &self.monitor_set else { |
| 941 | panic!() |
| 942 | }; |
| 943 | |
| 944 | let mon = monitors.iter().find(|mon| &mon.output == output).unwrap(); |
| 945 | mon.workspaces.iter().flat_map(|ws| ws.windows()) |
| 946 | } |
| 947 | |
| 948 | fn active_monitor(&mut self) -> Option<&mut Monitor<W>> { |
| 949 | let MonitorSet::Normal { |
| 950 | monitors, |
| 951 | active_monitor_idx, |
| 952 | .. |
| 953 | } = &mut self.monitor_set |
| 954 | else { |
| 955 | return None; |
| 956 | }; |
| 957 | |
| 958 | Some(&mut monitors[*active_monitor_idx]) |
| 959 | } |
| 960 | |
| 961 | pub fn monitor_for_output(&self, output: &Output) -> Option<&Monitor<W>> { |
| 962 | let MonitorSet::Normal { monitors, .. } = &self.monitor_set else { |
| 963 | return None; |
| 964 | }; |
| 965 | |
| 966 | monitors.iter().find(|monitor| &monitor.output == output) |
| 967 | } |
| 968 | |
| 969 | pub fn outputs(&self) -> impl Iterator<Item = &Output> + '_ { |
| 970 | let monitors = if let MonitorSet::Normal { monitors, .. } = &self.monitor_set { |
| 971 | &monitors[..] |
| 972 | } else { |
| 973 | &[][..] |
| 974 | }; |
| 975 | |
| 976 | monitors.iter().map(|mon| &mon.output) |
| 977 | } |
| 978 | |
| 979 | pub fn move_left(&mut self) { |
| 980 | let Some(monitor) = self.active_monitor() else { |
| 981 | return; |
| 982 | }; |
| 983 | monitor.move_left(); |
| 984 | } |
| 985 | |
| 986 | pub fn move_right(&mut self) { |
| 987 | let Some(monitor) = self.active_monitor() else { |
| 988 | return; |
| 989 | }; |
| 990 | monitor.move_right(); |
| 991 | } |
| 992 | |
| 993 | pub fn move_down(&mut self) { |
| 994 | let Some(monitor) = self.active_monitor() else { |
| 995 | return; |
| 996 | }; |
| 997 | monitor.move_down(); |
| 998 | } |
| 999 | |
| 1000 | pub fn move_up(&mut self) { |
| 1001 | let Some(monitor) = self.active_monitor() else { |
| 1002 | return; |
| 1003 | }; |
| 1004 | monitor.move_up(); |
| 1005 | } |
| 1006 | |
| 1007 | pub fn move_down_or_to_workspace_down(&mut self) { |
| 1008 | let Some(monitor) = self.active_monitor() else { |
| 1009 | return; |
| 1010 | }; |
| 1011 | monitor.move_down_or_to_workspace_down(); |
| 1012 | } |
| 1013 | |
| 1014 | pub fn move_up_or_to_workspace_up(&mut self) { |
| 1015 | let Some(monitor) = self.active_monitor() else { |
| 1016 | return; |
| 1017 | }; |
| 1018 | monitor.move_up_or_to_workspace_up(); |
| 1019 | } |
| 1020 | |
| 1021 | pub fn focus_left(&mut self) { |
| 1022 | let Some(monitor) = self.active_monitor() else { |
| 1023 | return; |
| 1024 | }; |
| 1025 | monitor.focus_left(); |
| 1026 | } |
| 1027 | |
| 1028 | pub fn focus_right(&mut self) { |
| 1029 | let Some(monitor) = self.active_monitor() else { |
| 1030 | return; |
| 1031 | }; |
| 1032 | monitor.focus_right(); |
| 1033 | } |
| 1034 | |
| 1035 | pub fn focus_down(&mut self) { |
| 1036 | let Some(monitor) = self.active_monitor() else { |
| 1037 | return; |
| 1038 | }; |
| 1039 | monitor.focus_down(); |
| 1040 | } |
| 1041 | |
| 1042 | pub fn focus_up(&mut self) { |
| 1043 | let Some(monitor) = self.active_monitor() else { |
| 1044 | return; |
| 1045 | }; |
| 1046 | monitor.focus_up(); |
| 1047 | } |
| 1048 | |
| 1049 | pub fn focus_window_or_workspace_down(&mut self) { |
| 1050 | let Some(monitor) = self.active_monitor() else { |
| 1051 | return; |
| 1052 | }; |
| 1053 | monitor.focus_window_or_workspace_down(); |
| 1054 | } |
| 1055 | |
| 1056 | pub fn focus_window_or_workspace_up(&mut self) { |
| 1057 | let Some(monitor) = self.active_monitor() else { |
| 1058 | return; |
| 1059 | }; |
| 1060 | monitor.focus_window_or_workspace_up(); |
| 1061 | } |
| 1062 | |
| 1063 | pub fn move_to_workspace_up(&mut self) { |
| 1064 | let Some(monitor) = self.active_monitor() else { |
| 1065 | return; |
| 1066 | }; |
| 1067 | monitor.move_to_workspace_up(); |
| 1068 | } |
| 1069 | |
| 1070 | pub fn move_to_workspace_down(&mut self) { |
| 1071 | let Some(monitor) = self.active_monitor() else { |
| 1072 | return; |
| 1073 | }; |
| 1074 | monitor.move_to_workspace_down(); |
| 1075 | } |
| 1076 | |
| 1077 | pub fn move_to_workspace(&mut self, idx: usize) { |
| 1078 | let Some(monitor) = self.active_monitor() else { |
| 1079 | return; |
| 1080 | }; |
| 1081 | monitor.move_to_workspace(idx); |
| 1082 | } |
| 1083 | |
| 1084 | pub fn switch_workspace_up(&mut self) { |
| 1085 | let Some(monitor) = self.active_monitor() else { |
| 1086 | return; |
| 1087 | }; |
| 1088 | monitor.switch_workspace_up(); |
| 1089 | } |
| 1090 | |
| 1091 | pub fn switch_workspace_down(&mut self) { |
| 1092 | let Some(monitor) = self.active_monitor() else { |
| 1093 | return; |
| 1094 | }; |
| 1095 | monitor.switch_workspace_down(); |
| 1096 | } |
| 1097 | |
| 1098 | pub fn switch_workspace(&mut self, idx: usize) { |
| 1099 | let Some(monitor) = self.active_monitor() else { |
| 1100 | return; |
| 1101 | }; |
| 1102 | monitor.switch_workspace(idx); |
| 1103 | } |
| 1104 | |
| 1105 | pub fn consume_into_column(&mut self) { |
| 1106 | let Some(monitor) = self.active_monitor() else { |
| 1107 | return; |
| 1108 | }; |
| 1109 | monitor.consume_into_column(); |
| 1110 | } |
| 1111 | |
| 1112 | pub fn expel_from_column(&mut self) { |
| 1113 | let Some(monitor) = self.active_monitor() else { |
| 1114 | return; |
| 1115 | }; |
| 1116 | monitor.expel_from_column(); |
| 1117 | } |
| 1118 | |
| 1119 | pub fn center_column(&mut self) { |
| 1120 | let Some(monitor) = self.active_monitor() else { |
| 1121 | return; |
| 1122 | }; |
| 1123 | monitor.center_column(); |
| 1124 | } |
| 1125 | |
| 1126 | pub fn focus(&self) -> Option<&W> { |
| 1127 | let MonitorSet::Normal { |
| 1128 | monitors, |
| 1129 | active_monitor_idx, |
| 1130 | .. |
| 1131 | } = &self.monitor_set |
| 1132 | else { |
| 1133 | return None; |
| 1134 | }; |
| 1135 | |
| 1136 | monitors[*active_monitor_idx].focus() |
| 1137 | } |
| 1138 | |
| 1139 | pub fn window_under( |
| 1140 | &self, |
| 1141 | output: &Output, |
| 1142 | pos_within_output: Point<f64, Logical>, |
| 1143 | ) -> Option<(&W, Point<i32, Logical>)> { |
| 1144 | let MonitorSet::Normal { monitors, .. } = &self.monitor_set else { |
| 1145 | return None; |
| 1146 | }; |
| 1147 | |
| 1148 | let mon = monitors.iter().find(|mon| &mon.output == output)?; |
| 1149 | mon.window_under(pos_within_output) |
| 1150 | } |
| 1151 | |
| 1152 | #[cfg(test)] |
| 1153 | fn verify_invariants(&self) { |
| 1154 | let (monitors, &primary_idx, &active_monitor_idx) = match &self.monitor_set { |
| 1155 | MonitorSet::Normal { |
| 1156 | monitors, |
| 1157 | primary_idx, |
| 1158 | active_monitor_idx, |
| 1159 | } => (monitors, primary_idx, active_monitor_idx), |
| 1160 | MonitorSet::NoOutputs { workspaces } => { |
| 1161 | for workspace in workspaces { |
| 1162 | assert!( |
| 1163 | workspace.has_windows(), |
| 1164 | "with no outputs there cannot be empty workspaces" |
| 1165 | ); |
| 1166 | |
| 1167 | assert_eq!( |
| 1168 | workspace.options, self.options, |
| 1169 | "workspace options must be synchronized with layout" |
| 1170 | ); |
| 1171 | |
| 1172 | workspace.verify_invariants(); |
| 1173 | } |
| 1174 | |
| 1175 | return; |
| 1176 | } |
| 1177 | }; |
| 1178 | |
| 1179 | assert!(primary_idx < monitors.len()); |
| 1180 | assert!(active_monitor_idx < monitors.len()); |
| 1181 | |
| 1182 | for (idx, monitor) in monitors.iter().enumerate() { |
| 1183 | assert!( |
| 1184 | !monitor.workspaces.is_empty(), |
| 1185 | "monitor must have at least one workspace" |
| 1186 | ); |
| 1187 | assert!(monitor.active_workspace_idx < monitor.workspaces.len()); |
| 1188 | |
| 1189 | assert_eq!( |
| 1190 | monitor.options, self.options, |
| 1191 | "monitor options must be synchronized with layout" |
| 1192 | ); |
| 1193 | |
| 1194 | let monitor_id = OutputId::new(&monitor.output); |
| 1195 | |
| 1196 | if idx == primary_idx { |
| 1197 | for ws in &monitor.workspaces { |
| 1198 | if ws.original_output == monitor_id { |
| 1199 | // This is the primary monitor's own workspace. |
| 1200 | continue; |
| 1201 | } |
| 1202 | |
| 1203 | let own_monitor_exists = monitors |
| 1204 | .iter() |
| 1205 | .any(|m| OutputId::new(&m.output) == ws.original_output); |
| 1206 | assert!( |
| 1207 | !own_monitor_exists, |
| 1208 | "primary monitor cannot have workspaces for which their own monitor exists" |
| 1209 | ); |
| 1210 | } |
| 1211 | } else { |
| 1212 | assert!( |
| 1213 | monitor |
| 1214 | .workspaces |
| 1215 | .iter() |
| 1216 | .any(|workspace| workspace.original_output == monitor_id), |
| 1217 | "secondary monitor must not have any non-own workspaces" |
| 1218 | ); |
| 1219 | } |
| 1220 | |
| 1221 | assert!( |
| 1222 | monitor.workspaces.last().unwrap().columns.is_empty(), |
| 1223 | "monitor must have an empty workspace in the end" |
| 1224 | ); |
| 1225 | |
| 1226 | // If there's no workspace switch in progress, there can't be any non-last non-active |
| 1227 | // empty workspaces. |
| 1228 | if monitor.workspace_switch.is_none() { |
| 1229 | for (idx, ws) in monitor.workspaces.iter().enumerate().rev().skip(1) { |
| 1230 | if idx != monitor.active_workspace_idx { |
| 1231 | assert!( |
| 1232 | !ws.columns.is_empty(), |
| 1233 | "non-active workspace can't be empty except the last one" |
| 1234 | ); |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | // FIXME: verify that primary doesn't have any workspaces for which their own monitor |
| 1240 | // exists. |
| 1241 | |
| 1242 | for workspace in &monitor.workspaces { |
| 1243 | assert_eq!( |
| 1244 | workspace.options, self.options, |
| 1245 | "workspace options must be synchronized with layout" |
| 1246 | ); |
| 1247 | |
| 1248 | workspace.verify_invariants(); |
| 1249 | } |
| 1250 | } |
| 1251 | } |
| 1252 | |
| 1253 | pub fn advance_animations(&mut self, current_time: Duration) { |
| 1254 | let _span = tracy_client::span!("Layout::advance_animations"); |
| 1255 | |
| 1256 | match &mut self.monitor_set { |
| 1257 | MonitorSet::Normal { |
| 1258 | monitors, |
| 1259 | active_monitor_idx, |
| 1260 | .. |
| 1261 | } => { |
| 1262 | for (idx, mon) in monitors.iter_mut().enumerate() { |
| 1263 | mon.advance_animations(current_time, idx == *active_monitor_idx); |
| 1264 | } |
| 1265 | } |
| 1266 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 1267 | for ws in workspaces { |
| 1268 | ws.advance_animations(current_time, false); |
| 1269 | } |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | pub fn update_config(&mut self, config: &Config) { |
| 1275 | let options = Rc::new(Options::from_config(config)); |
| 1276 | |
| 1277 | match &mut self.monitor_set { |
| 1278 | MonitorSet::Normal { monitors, .. } => { |
| 1279 | for mon in monitors { |
| 1280 | mon.update_config(options.clone()); |
| 1281 | } |
| 1282 | } |
| 1283 | MonitorSet::NoOutputs { workspaces } => { |
| 1284 | for ws in workspaces { |
| 1285 | ws.update_config(options.clone()); |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | self.options = options; |
| 1291 | } |
| 1292 | |
| 1293 | pub fn toggle_width(&mut self) { |
| 1294 | let Some(monitor) = self.active_monitor() else { |
| 1295 | return; |
| 1296 | }; |
| 1297 | monitor.toggle_width(); |
| 1298 | } |
| 1299 | |
| 1300 | pub fn toggle_full_width(&mut self) { |
| 1301 | let Some(monitor) = self.active_monitor() else { |
| 1302 | return; |
| 1303 | }; |
| 1304 | monitor.toggle_full_width(); |
| 1305 | } |
| 1306 | |
| 1307 | pub fn set_column_width(&mut self, change: SizeChange) { |
| 1308 | let Some(monitor) = self.active_monitor() else { |
| 1309 | return; |
| 1310 | }; |
| 1311 | monitor.set_column_width(change); |
| 1312 | } |
| 1313 | |
| 1314 | pub fn set_window_height(&mut self, change: SizeChange) { |
| 1315 | let Some(monitor) = self.active_monitor() else { |
| 1316 | return; |
| 1317 | }; |
| 1318 | monitor.set_window_height(change); |
| 1319 | } |
| 1320 | |
| 1321 | pub fn focus_output(&mut self, output: &Output) { |
| 1322 | if let MonitorSet::Normal { |
| 1323 | monitors, |
| 1324 | active_monitor_idx, |
| 1325 | .. |
| 1326 | } = &mut self.monitor_set |
| 1327 | { |
| 1328 | for (idx, mon) in monitors.iter().enumerate() { |
| 1329 | if &mon.output == output { |
| 1330 | *active_monitor_idx = idx; |
| 1331 | return; |
| 1332 | } |
| 1333 | } |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | pub fn move_to_output(&mut self, output: &Output) { |
| 1338 | if let MonitorSet::Normal { |
| 1339 | monitors, |
| 1340 | active_monitor_idx, |
| 1341 | .. |
| 1342 | } = &mut self.monitor_set |
| 1343 | { |
| 1344 | let new_idx = monitors |
| 1345 | .iter() |
| 1346 | .position(|mon| &mon.output == output) |
| 1347 | .unwrap(); |
| 1348 | |
| 1349 | let current = &mut monitors[*active_monitor_idx]; |
| 1350 | let ws = current.active_workspace(); |
| 1351 | if !ws.has_windows() { |
| 1352 | return; |
| 1353 | } |
| 1354 | let column = &ws.columns[ws.active_column_idx]; |
| 1355 | let window = column.windows[column.active_window_idx].clone(); |
| 1356 | let width = column.width; |
| 1357 | let is_full_width = column.is_full_width; |
| 1358 | ws.remove_window(&window); |
| 1359 | |
| 1360 | let workspace_idx = monitors[new_idx].active_workspace_idx; |
| 1361 | self.add_window_by_idx(new_idx, workspace_idx, window, true, width, is_full_width); |
| 1362 | } |
| 1363 | } |
| 1364 | |
| 1365 | pub fn move_window_to_output(&mut self, window: W, output: &Output) { |
| 1366 | if !matches!(&self.monitor_set, MonitorSet::Normal { .. }) { |
| 1367 | return; |
| 1368 | } |
| 1369 | |
| 1370 | self.remove_window(&window); |
| 1371 | |
| 1372 | if let MonitorSet::Normal { monitors, .. } = &mut self.monitor_set { |
| 1373 | let mut width = None; |
| 1374 | let mut is_full_width = false; |
| 1375 | for mon in &*monitors { |
| 1376 | for ws in &mon.workspaces { |
| 1377 | for col in &ws.columns { |
| 1378 | if col.windows.contains(&window) { |
| 1379 | width = Some(col.width); |
| 1380 | is_full_width = col.is_full_width; |
| 1381 | break; |
| 1382 | } |
| 1383 | } |
| 1384 | } |
| 1385 | } |
| 1386 | let Some(width) = width else { return }; |
| 1387 | |
| 1388 | let new_idx = monitors |
| 1389 | .iter() |
| 1390 | .position(|mon| &mon.output == output) |
| 1391 | .unwrap(); |
| 1392 | |
| 1393 | let workspace_idx = monitors[new_idx].active_workspace_idx; |
| 1394 | // FIXME: activate only if it was already active and focused. |
| 1395 | self.add_window_by_idx(new_idx, workspace_idx, window, true, width, is_full_width); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) { |
| 1400 | match &mut self.monitor_set { |
| 1401 | MonitorSet::Normal { monitors, .. } => { |
| 1402 | for mon in monitors { |
| 1403 | for ws in &mut mon.workspaces { |
| 1404 | if ws.has_window(window) { |
| 1405 | ws.set_fullscreen(window, is_fullscreen); |
| 1406 | return; |
| 1407 | } |
| 1408 | } |
| 1409 | } |
| 1410 | } |
| 1411 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 1412 | for ws in workspaces { |
| 1413 | if ws.has_window(window) { |
| 1414 | ws.set_fullscreen(window, is_fullscreen); |
| 1415 | return; |
| 1416 | } |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | pub fn toggle_fullscreen(&mut self, window: &W) { |
| 1423 | match &mut self.monitor_set { |
| 1424 | MonitorSet::Normal { monitors, .. } => { |
| 1425 | for mon in monitors { |
| 1426 | for ws in &mut mon.workspaces { |
| 1427 | if ws.has_window(window) { |
| 1428 | ws.toggle_fullscreen(window); |
| 1429 | return; |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | } |
| 1434 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 1435 | for ws in workspaces { |
| 1436 | if ws.has_window(window) { |
| 1437 | ws.toggle_fullscreen(window); |
| 1438 | return; |
| 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | pub fn workspace_switch_gesture_begin(&mut self, output: &Output) { |
| 1446 | let monitors = match &mut self.monitor_set { |
| 1447 | MonitorSet::Normal { monitors, .. } => monitors, |
| 1448 | MonitorSet::NoOutputs { .. } => unreachable!(), |
| 1449 | }; |
| 1450 | |
| 1451 | for monitor in monitors { |
| 1452 | // Cancel the gesture on other outputs. |
| 1453 | if &monitor.output != output { |
| 1454 | if let Some(WorkspaceSwitch::Gesture(_)) = monitor.workspace_switch { |
| 1455 | monitor.workspace_switch = None; |
| 1456 | } |
| 1457 | continue; |
| 1458 | } |
| 1459 | |
| 1460 | let center_idx = monitor.active_workspace_idx; |
| 1461 | let current_idx = monitor |
| 1462 | .workspace_switch |
| 1463 | .as_ref() |
| 1464 | .map(|s| s.current_idx()) |
| 1465 | .unwrap_or(center_idx as f64); |
| 1466 | |
| 1467 | let gesture = WorkspaceSwitchGesture { |
| 1468 | center_idx, |
| 1469 | current_idx, |
| 1470 | }; |
| 1471 | monitor.workspace_switch = Some(WorkspaceSwitch::Gesture(gesture)); |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | pub fn workspace_switch_gesture_update(&mut self, delta_y: f64) -> Option<Option<Output>> { |
| 1476 | let monitors = match &mut self.monitor_set { |
| 1477 | MonitorSet::Normal { monitors, .. } => monitors, |
| 1478 | MonitorSet::NoOutputs { .. } => return None, |
| 1479 | }; |
| 1480 | |
| 1481 | for monitor in monitors { |
| 1482 | if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch { |
| 1483 | // Normalize like GNOME Shell's workspace switching. |
| 1484 | let delta_y = -delta_y / 400.; |
| 1485 | |
| 1486 | let min = gesture.center_idx.saturating_sub(1) as f64; |
| 1487 | let max = (gesture.center_idx + 1).min(monitor.workspaces.len() - 1) as f64; |
| 1488 | let new_idx = (gesture.current_idx + delta_y).clamp(min, max); |
| 1489 | |
| 1490 | if gesture.current_idx == new_idx { |
| 1491 | return Some(None); |
| 1492 | } |
| 1493 | |
| 1494 | gesture.current_idx = new_idx; |
| 1495 | return Some(Some(monitor.output.clone())); |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | None |
| 1500 | } |
| 1501 | |
| 1502 | pub fn workspace_switch_gesture_end(&mut self, cancelled: bool) -> Option<Output> { |
| 1503 | let monitors = match &mut self.monitor_set { |
| 1504 | MonitorSet::Normal { monitors, .. } => monitors, |
| 1505 | MonitorSet::NoOutputs { .. } => return None, |
| 1506 | }; |
| 1507 | |
| 1508 | for monitor in monitors { |
| 1509 | if let Some(WorkspaceSwitch::Gesture(gesture)) = &mut monitor.workspace_switch { |
| 1510 | if cancelled { |
| 1511 | monitor.workspace_switch = None; |
| 1512 | return Some(monitor.output.clone()); |
| 1513 | } |
| 1514 | |
| 1515 | // FIXME: keep track of gesture velocity and use it to compute the final point and |
| 1516 | // to animate to it. |
| 1517 | let current_idx = gesture.current_idx; |
| 1518 | let idx = current_idx.round() as usize; |
| 1519 | |
| 1520 | monitor.active_workspace_idx = idx; |
| 1521 | monitor.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 1522 | current_idx, |
| 1523 | idx as f64, |
| 1524 | Duration::from_millis(250), |
| 1525 | ))); |
| 1526 | |
| 1527 | return Some(monitor.output.clone()); |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | None |
| 1532 | } |
| 1533 | |
| 1534 | pub fn move_workspace_down(&mut self) { |
| 1535 | let Some(monitor) = self.active_monitor() else { |
| 1536 | return; |
| 1537 | }; |
| 1538 | monitor.move_workspace_down(); |
| 1539 | } |
| 1540 | |
| 1541 | pub fn move_workspace_up(&mut self) { |
| 1542 | let Some(monitor) = self.active_monitor() else { |
| 1543 | return; |
| 1544 | }; |
| 1545 | monitor.move_workspace_up(); |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | impl Layout<Window> { |
| 1550 | pub fn refresh(&self) { |
| 1551 | let _span = tracy_client::span!("MonitorSet::refresh"); |
| 1552 | |
| 1553 | match &self.monitor_set { |
| 1554 | MonitorSet::Normal { monitors, .. } => { |
| 1555 | for mon in monitors { |
| 1556 | for ws in &mon.workspaces { |
| 1557 | ws.refresh(); |
| 1558 | } |
| 1559 | } |
| 1560 | } |
| 1561 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 1562 | for ws in workspaces { |
| 1563 | ws.refresh(); |
| 1564 | } |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | } |
| 1569 | |
| 1570 | impl<W: LayoutElement> Default for MonitorSet<W> { |
| 1571 | fn default() -> Self { |
| 1572 | Self::NoOutputs { workspaces: vec![] } |
| 1573 | } |
| 1574 | } |
| 1575 | |
| 1576 | impl<W: LayoutElement> Monitor<W> { |
| 1577 | fn new(output: Output, workspaces: Vec<Workspace<W>>, options: Rc<Options>) -> Self { |
| 1578 | Self { |
| 1579 | output, |
| 1580 | workspaces, |
| 1581 | active_workspace_idx: 0, |
| 1582 | workspace_switch: None, |
| 1583 | options, |
| 1584 | } |
| 1585 | } |
| 1586 | |
| 1587 | fn active_workspace(&mut self) -> &mut Workspace<W> { |
| 1588 | &mut self.workspaces[self.active_workspace_idx] |
| 1589 | } |
| 1590 | |
| 1591 | fn activate_workspace(&mut self, idx: usize) { |
| 1592 | if self.active_workspace_idx == idx { |
| 1593 | return; |
| 1594 | } |
| 1595 | |
| 1596 | let current_idx = self |
| 1597 | .workspace_switch |
| 1598 | .as_ref() |
| 1599 | .map(|s| s.current_idx()) |
| 1600 | .unwrap_or(self.active_workspace_idx as f64); |
| 1601 | |
| 1602 | self.active_workspace_idx = idx; |
| 1603 | |
| 1604 | self.workspace_switch = Some(WorkspaceSwitch::Animation(Animation::new( |
| 1605 | current_idx, |
| 1606 | idx as f64, |
| 1607 | Duration::from_millis(250), |
| 1608 | ))); |
| 1609 | } |
| 1610 | |
| 1611 | pub fn add_window( |
| 1612 | &mut self, |
| 1613 | workspace_idx: usize, |
| 1614 | window: W, |
| 1615 | activate: bool, |
| 1616 | width: ColumnWidth, |
| 1617 | is_full_width: bool, |
| 1618 | ) { |
| 1619 | let workspace = &mut self.workspaces[workspace_idx]; |
| 1620 | |
| 1621 | workspace.add_window(window.clone(), activate, width, is_full_width); |
| 1622 | |
| 1623 | // After adding a new window, workspace becomes this output's own. |
| 1624 | workspace.original_output = OutputId::new(&self.output); |
| 1625 | |
| 1626 | if workspace_idx == self.workspaces.len() - 1 { |
| 1627 | // Insert a new empty workspace. |
| 1628 | let ws = Workspace::new(self.output.clone(), self.options.clone()); |
| 1629 | self.workspaces.push(ws); |
| 1630 | } |
| 1631 | |
| 1632 | if activate { |
| 1633 | self.activate_workspace(workspace_idx); |
| 1634 | } |
| 1635 | } |
| 1636 | |
| 1637 | fn clean_up_workspaces(&mut self) { |
| 1638 | assert!(self.workspace_switch.is_none()); |
| 1639 | |
| 1640 | for idx in (0..self.workspaces.len() - 1).rev() { |
| 1641 | if self.active_workspace_idx == idx { |
| 1642 | continue; |
| 1643 | } |
| 1644 | |
| 1645 | if !self.workspaces[idx].has_windows() { |
| 1646 | self.workspaces.remove(idx); |
| 1647 | if self.active_workspace_idx > idx { |
| 1648 | self.active_workspace_idx -= 1; |
| 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | } |
| 1653 | |
| 1654 | pub fn move_left(&mut self) { |
| 1655 | self.active_workspace().move_left(); |
| 1656 | } |
| 1657 | |
| 1658 | pub fn move_right(&mut self) { |
| 1659 | self.active_workspace().move_right(); |
| 1660 | } |
| 1661 | |
| 1662 | pub fn move_down(&mut self) { |
| 1663 | self.active_workspace().move_down(); |
| 1664 | } |
| 1665 | |
| 1666 | pub fn move_up(&mut self) { |
| 1667 | self.active_workspace().move_up(); |
| 1668 | } |
| 1669 | |
| 1670 | pub fn move_down_or_to_workspace_down(&mut self) { |
| 1671 | let workspace = self.active_workspace(); |
| 1672 | if workspace.columns.is_empty() { |
| 1673 | return; |
| 1674 | } |
| 1675 | let column = &mut workspace.columns[workspace.active_column_idx]; |
| 1676 | let curr_idx = column.active_window_idx; |
| 1677 | let new_idx = min(column.active_window_idx + 1, column.windows.len() - 1); |
| 1678 | if curr_idx == new_idx { |
| 1679 | self.move_to_workspace_down(); |
| 1680 | } else { |
| 1681 | workspace.move_down(); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | pub fn move_up_or_to_workspace_up(&mut self) { |
| 1686 | let workspace = self.active_workspace(); |
| 1687 | if workspace.columns.is_empty() { |
| 1688 | return; |
| 1689 | } |
| 1690 | let curr_idx = workspace.columns[workspace.active_column_idx].active_window_idx; |
| 1691 | let new_idx = curr_idx.saturating_sub(1); |
| 1692 | if curr_idx == new_idx { |
| 1693 | self.move_to_workspace_up(); |
| 1694 | } else { |
| 1695 | workspace.move_up(); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | pub fn focus_left(&mut self) { |
| 1700 | self.active_workspace().focus_left(); |
| 1701 | } |
| 1702 | |
| 1703 | pub fn focus_right(&mut self) { |
| 1704 | self.active_workspace().focus_right(); |
| 1705 | } |
| 1706 | |
| 1707 | pub fn focus_down(&mut self) { |
| 1708 | self.active_workspace().focus_down(); |
| 1709 | } |
| 1710 | |
| 1711 | pub fn focus_up(&mut self) { |
| 1712 | self.active_workspace().focus_up(); |
| 1713 | } |
| 1714 | |
| 1715 | pub fn focus_window_or_workspace_down(&mut self) { |
| 1716 | let workspace = self.active_workspace(); |
| 1717 | if workspace.columns.is_empty() { |
| 1718 | self.switch_workspace_down(); |
| 1719 | } else { |
| 1720 | let column = &workspace.columns[workspace.active_column_idx]; |
| 1721 | let curr_idx = column.active_window_idx; |
| 1722 | let new_idx = min(column.active_window_idx + 1, column.windows.len() - 1); |
| 1723 | if curr_idx == new_idx { |
| 1724 | self.switch_workspace_down(); |
| 1725 | } else { |
| 1726 | workspace.focus_down(); |
| 1727 | } |
| 1728 | } |
| 1729 | } |
| 1730 | |
| 1731 | pub fn focus_window_or_workspace_up(&mut self) { |
| 1732 | let workspace = self.active_workspace(); |
| 1733 | if workspace.columns.is_empty() { |
| 1734 | self.switch_workspace_up(); |
| 1735 | } else { |
| 1736 | let curr_idx = workspace.columns[workspace.active_column_idx].active_window_idx; |
| 1737 | let new_idx = curr_idx.saturating_sub(1); |
| 1738 | if curr_idx == new_idx { |
| 1739 | self.switch_workspace_up(); |
| 1740 | } else { |
| 1741 | workspace.focus_up(); |
| 1742 | } |
| 1743 | } |
| 1744 | } |
| 1745 | |
| 1746 | pub fn move_to_workspace_up(&mut self) { |
| 1747 | let source_workspace_idx = self.active_workspace_idx; |
| 1748 | |
| 1749 | let new_idx = source_workspace_idx.saturating_sub(1); |
| 1750 | if new_idx == source_workspace_idx { |
| 1751 | return; |
| 1752 | } |
| 1753 | |
| 1754 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 1755 | if workspace.columns.is_empty() { |
| 1756 | return; |
| 1757 | } |
| 1758 | |
| 1759 | let column = &mut workspace.columns[workspace.active_column_idx]; |
| 1760 | let width = column.width; |
| 1761 | let is_full_width = column.is_full_width; |
| 1762 | let window = column.windows[column.active_window_idx].clone(); |
| 1763 | workspace.remove_window(&window); |
| 1764 | |
| 1765 | self.add_window(new_idx, window, true, width, is_full_width); |
| 1766 | } |
| 1767 | |
| 1768 | pub fn move_to_workspace_down(&mut self) { |
| 1769 | let source_workspace_idx = self.active_workspace_idx; |
| 1770 | |
| 1771 | let new_idx = min(source_workspace_idx + 1, self.workspaces.len() - 1); |
| 1772 | if new_idx == source_workspace_idx { |
| 1773 | return; |
| 1774 | } |
| 1775 | |
| 1776 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 1777 | if workspace.columns.is_empty() { |
| 1778 | return; |
| 1779 | } |
| 1780 | |
| 1781 | let column = &mut workspace.columns[workspace.active_column_idx]; |
| 1782 | let width = column.width; |
| 1783 | let is_full_width = column.is_full_width; |
| 1784 | let window = column.windows[column.active_window_idx].clone(); |
| 1785 | workspace.remove_window(&window); |
| 1786 | |
| 1787 | self.add_window(new_idx, window, true, width, is_full_width); |
| 1788 | } |
| 1789 | |
| 1790 | pub fn move_to_workspace(&mut self, idx: usize) { |
| 1791 | let source_workspace_idx = self.active_workspace_idx; |
| 1792 | |
| 1793 | let new_idx = min(idx, self.workspaces.len() - 1); |
| 1794 | if new_idx == source_workspace_idx { |
| 1795 | return; |
| 1796 | } |
| 1797 | |
| 1798 | let workspace = &mut self.workspaces[source_workspace_idx]; |
| 1799 | if workspace.columns.is_empty() { |
| 1800 | return; |
| 1801 | } |
| 1802 | |
| 1803 | let column = &mut workspace.columns[workspace.active_column_idx]; |
| 1804 | let width = column.width; |
| 1805 | let is_full_width = column.is_full_width; |
| 1806 | let window = column.windows[column.active_window_idx].clone(); |
| 1807 | workspace.remove_window(&window); |
| 1808 | |
| 1809 | self.add_window(new_idx, window, true, width, is_full_width); |
| 1810 | |
| 1811 | // Don't animate this action. |
| 1812 | self.workspace_switch = None; |
| 1813 | |
| 1814 | self.clean_up_workspaces(); |
| 1815 | } |
| 1816 | |
| 1817 | pub fn switch_workspace_up(&mut self) { |
| 1818 | self.activate_workspace(self.active_workspace_idx.saturating_sub(1)); |
| 1819 | } |
| 1820 | |
| 1821 | pub fn switch_workspace_down(&mut self) { |
| 1822 | self.activate_workspace(min( |
| 1823 | self.active_workspace_idx + 1, |
| 1824 | self.workspaces.len() - 1, |
| 1825 | )); |
| 1826 | } |
| 1827 | |
| 1828 | pub fn switch_workspace(&mut self, idx: usize) { |
| 1829 | self.activate_workspace(min(idx, self.workspaces.len() - 1)); |
| 1830 | // Don't animate this action. |
| 1831 | self.workspace_switch = None; |
| 1832 | |
| 1833 | self.clean_up_workspaces(); |
| 1834 | } |
| 1835 | |
| 1836 | pub fn consume_into_column(&mut self) { |
| 1837 | self.active_workspace().consume_into_column(); |
| 1838 | } |
| 1839 | |
| 1840 | pub fn expel_from_column(&mut self) { |
| 1841 | self.active_workspace().expel_from_column(); |
| 1842 | } |
| 1843 | |
| 1844 | pub fn center_column(&mut self) { |
| 1845 | self.active_workspace().center_column(); |
| 1846 | } |
| 1847 | |
| 1848 | pub fn focus(&self) -> Option<&W> { |
| 1849 | let workspace = &self.workspaces[self.active_workspace_idx]; |
| 1850 | if !workspace.has_windows() { |
| 1851 | return None; |
| 1852 | } |
| 1853 | |
| 1854 | let column = &workspace.columns[workspace.active_column_idx]; |
| 1855 | Some(&column.windows[column.active_window_idx]) |
| 1856 | } |
| 1857 | |
| 1858 | pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) { |
| 1859 | if let Some(WorkspaceSwitch::Animation(anim)) = &mut self.workspace_switch { |
| 1860 | anim.set_current_time(current_time); |
| 1861 | if anim.is_done() { |
| 1862 | self.workspace_switch = None; |
| 1863 | self.clean_up_workspaces(); |
| 1864 | } |
| 1865 | } |
| 1866 | |
| 1867 | for ws in &mut self.workspaces { |
| 1868 | ws.advance_animations(current_time, is_active); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | pub fn are_animations_ongoing(&self) -> bool { |
| 1873 | self.workspace_switch |
| 1874 | .as_ref() |
| 1875 | .is_some_and(|s| s.is_animation()) |
| 1876 | || self.workspaces.iter().any(|ws| ws.are_animations_ongoing()) |
| 1877 | } |
| 1878 | |
| 1879 | pub fn are_transitions_ongoing(&self) -> bool { |
| 1880 | self.workspace_switch.is_some() |
| 1881 | || self.workspaces.iter().any(|ws| ws.are_animations_ongoing()) |
| 1882 | } |
| 1883 | |
| 1884 | fn update_config(&mut self, options: Rc<Options>) { |
| 1885 | for ws in &mut self.workspaces { |
| 1886 | ws.update_config(options.clone()); |
| 1887 | } |
| 1888 | |
| 1889 | self.options = options; |
| 1890 | } |
| 1891 | |
| 1892 | fn toggle_width(&mut self) { |
| 1893 | self.active_workspace().toggle_width(); |
| 1894 | } |
| 1895 | |
| 1896 | fn toggle_full_width(&mut self) { |
| 1897 | self.active_workspace().toggle_full_width(); |
| 1898 | } |
| 1899 | |
| 1900 | fn set_column_width(&mut self, change: SizeChange) { |
| 1901 | self.active_workspace().set_column_width(change); |
| 1902 | } |
| 1903 | |
| 1904 | fn set_window_height(&mut self, change: SizeChange) { |
| 1905 | self.active_workspace().set_window_height(change); |
| 1906 | } |
| 1907 | |
| 1908 | fn move_workspace_down(&mut self) { |
| 1909 | let new_idx = min(self.active_workspace_idx + 1, self.workspaces.len() - 1); |
| 1910 | if new_idx == self.active_workspace_idx { |
| 1911 | return; |
| 1912 | } |
| 1913 | |
| 1914 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 1915 | |
| 1916 | if new_idx == self.workspaces.len() - 1 { |
| 1917 | // Insert a new empty workspace. |
| 1918 | let ws = Workspace::new(self.output.clone(), self.options.clone()); |
| 1919 | self.workspaces.push(ws); |
| 1920 | } |
| 1921 | |
| 1922 | self.activate_workspace(new_idx); |
| 1923 | self.workspace_switch = None; |
| 1924 | |
| 1925 | self.clean_up_workspaces(); |
| 1926 | } |
| 1927 | |
| 1928 | fn move_workspace_up(&mut self) { |
| 1929 | let new_idx = self.active_workspace_idx.saturating_sub(1); |
| 1930 | if new_idx == self.active_workspace_idx { |
| 1931 | return; |
| 1932 | } |
| 1933 | |
| 1934 | self.workspaces.swap(self.active_workspace_idx, new_idx); |
| 1935 | |
| 1936 | if self.active_workspace_idx == self.workspaces.len() - 1 { |
| 1937 | // Insert a new empty workspace. |
| 1938 | let ws = Workspace::new(self.output.clone(), self.options.clone()); |
| 1939 | self.workspaces.push(ws); |
| 1940 | } |
| 1941 | |
| 1942 | self.activate_workspace(new_idx); |
| 1943 | self.workspace_switch = None; |
| 1944 | |
| 1945 | self.clean_up_workspaces(); |
| 1946 | } |
| 1947 | |
| 1948 | pub fn window_under( |
| 1949 | &self, |
| 1950 | pos_within_output: Point<f64, Logical>, |
| 1951 | ) -> Option<(&W, Point<i32, Logical>)> { |
| 1952 | match &self.workspace_switch { |
| 1953 | Some(switch) => { |
| 1954 | let size = output_size(&self.output); |
| 1955 | |
| 1956 | let render_idx = switch.current_idx(); |
| 1957 | let before_idx = render_idx.floor() as usize; |
| 1958 | let after_idx = render_idx.ceil() as usize; |
| 1959 | |
| 1960 | let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32; |
| 1961 | |
| 1962 | let (idx, ws_offset) = if pos_within_output.y < (size.h - offset) as f64 { |
| 1963 | (before_idx, Point::from((0, offset))) |
| 1964 | } else { |
| 1965 | (after_idx, Point::from((0, -size.h + offset))) |
| 1966 | }; |
| 1967 | |
| 1968 | let ws = &self.workspaces[idx]; |
| 1969 | let (win, win_pos) = ws.window_under(pos_within_output + ws_offset.to_f64())?; |
| 1970 | Some((win, win_pos - ws_offset)) |
| 1971 | } |
| 1972 | None => { |
| 1973 | let ws = &self.workspaces[self.active_workspace_idx]; |
| 1974 | ws.window_under(pos_within_output) |
| 1975 | } |
| 1976 | } |
| 1977 | } |
| 1978 | |
| 1979 | pub fn render_above_top_layer(&self) -> bool { |
| 1980 | // Render above the top layer only if the view is stationary. |
| 1981 | if self.workspace_switch.is_some() { |
| 1982 | return false; |
| 1983 | } |
| 1984 | |
| 1985 | let ws = &self.workspaces[self.active_workspace_idx]; |
| 1986 | ws.render_above_top_layer() |
| 1987 | } |
| 1988 | } |
| 1989 | |
| 1990 | impl Monitor<Window> { |
| 1991 | pub fn render_elements( |
| 1992 | &self, |
| 1993 | renderer: &mut GlesRenderer, |
| 1994 | ) -> Vec<MonitorRenderElement<GlesRenderer>> { |
| 1995 | let _span = tracy_client::span!("Monitor::render_elements"); |
| 1996 | |
| 1997 | let output_scale = Scale::from(self.output.current_scale().fractional_scale()); |
| 1998 | let output_transform = self.output.current_transform(); |
| 1999 | let output_mode = self.output.current_mode().unwrap(); |
| 2000 | let size = output_transform.transform_size(output_mode.size); |
| 2001 | |
| 2002 | match &self.workspace_switch { |
| 2003 | Some(switch) => { |
| 2004 | let render_idx = switch.current_idx(); |
| 2005 | let before_idx = render_idx.floor() as usize; |
| 2006 | let after_idx = render_idx.ceil() as usize; |
| 2007 | |
| 2008 | let offset = ((render_idx - before_idx as f64) * size.h as f64).round() as i32; |
| 2009 | |
| 2010 | let before = self.workspaces[before_idx].render_elements(renderer); |
| 2011 | let after = self.workspaces[after_idx].render_elements(renderer); |
| 2012 | |
| 2013 | let before = before.into_iter().filter_map(|elem| { |
| 2014 | Some(RelocateRenderElement::from_element( |
| 2015 | CropRenderElement::from_element( |
| 2016 | elem, |
| 2017 | output_scale, |
| 2018 | Rectangle::from_extemities((0, offset), (size.w, size.h)), |
| 2019 | )?, |
| 2020 | (0, -offset), |
| 2021 | Relocate::Relative, |
| 2022 | )) |
| 2023 | }); |
| 2024 | let after = after.into_iter().filter_map(|elem| { |
| 2025 | Some(RelocateRenderElement::from_element( |
| 2026 | CropRenderElement::from_element( |
| 2027 | elem, |
| 2028 | output_scale, |
| 2029 | Rectangle::from_extemities((0, 0), (size.w, offset)), |
| 2030 | )?, |
| 2031 | (0, -offset + size.h), |
| 2032 | Relocate::Relative, |
| 2033 | )) |
| 2034 | }); |
| 2035 | before.chain(after).collect() |
| 2036 | } |
| 2037 | None => { |
| 2038 | let elements = self.workspaces[self.active_workspace_idx].render_elements(renderer); |
| 2039 | elements |
| 2040 | .into_iter() |
| 2041 | .filter_map(|elem| { |
| 2042 | Some(RelocateRenderElement::from_element( |
| 2043 | CropRenderElement::from_element( |
| 2044 | elem, |
| 2045 | output_scale, |
| 2046 | // HACK: set infinite crop bounds due to a damage tracking bug |
| 2047 | // which causes glitched rendering for maximized GTK windows. |
| 2048 | // FIXME: use proper bounds after fixing the Crop element. |
| 2049 | Rectangle::from_loc_and_size( |
| 2050 | (-i32::MAX / 2, -i32::MAX / 2), |
| 2051 | (i32::MAX, i32::MAX), |
| 2052 | ), |
| 2053 | // Rectangle::from_loc_and_size((0, 0), size), |
| 2054 | )?, |
| 2055 | (0, 0), |
| 2056 | Relocate::Relative, |
| 2057 | )) |
| 2058 | }) |
| 2059 | .collect() |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | |
| 2065 | impl<W: LayoutElement> Workspace<W> { |
| 2066 | fn new(output: Output, options: Rc<Options>) -> Self { |
| 2067 | let working_area = layer_map_for_output(&output).non_exclusive_zone(); |
| 2068 | Self { |
| 2069 | original_output: OutputId::new(&output), |
| 2070 | view_size: output_size(&output), |
| 2071 | working_area, |
| 2072 | output: Some(output), |
| 2073 | columns: vec![], |
| 2074 | active_column_idx: 0, |
| 2075 | focus_ring: FocusRing::new(options.focus_ring), |
| 2076 | view_offset: 0, |
| 2077 | view_offset_anim: None, |
| 2078 | activate_prev_column_on_removal: false, |
| 2079 | options, |
| 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | fn new_no_outputs(options: Rc<Options>) -> Self { |
| 2084 | Self { |
| 2085 | output: None, |
| 2086 | original_output: OutputId(String::new()), |
| 2087 | view_size: Size::from((1280, 720)), |
| 2088 | working_area: Rectangle::from_loc_and_size((0, 0), (1280, 720)), |
| 2089 | columns: vec![], |
| 2090 | active_column_idx: 0, |
| 2091 | focus_ring: FocusRing::new(options.focus_ring), |
| 2092 | view_offset: 0, |
| 2093 | view_offset_anim: None, |
| 2094 | activate_prev_column_on_removal: false, |
| 2095 | options, |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | pub fn advance_animations(&mut self, current_time: Duration, is_active: bool) { |
| 2100 | match &mut self.view_offset_anim { |
| 2101 | Some(anim) => { |
| 2102 | anim.set_current_time(current_time); |
| 2103 | self.view_offset = anim.value().round() as i32; |
| 2104 | if anim.is_done() { |
| 2105 | self.view_offset_anim = None; |
| 2106 | } |
| 2107 | } |
| 2108 | None => (), |
| 2109 | } |
| 2110 | |
| 2111 | let view_pos = self.view_pos(); |
| 2112 | |
| 2113 | // This shall one day become a proper animation. |
| 2114 | if !self.columns.is_empty() { |
| 2115 | let col = &self.columns[self.active_column_idx]; |
| 2116 | let active_win = &col.windows[col.active_window_idx]; |
| 2117 | let geom = active_win.geometry(); |
| 2118 | let has_ssd = active_win.has_ssd(); |
| 2119 | |
| 2120 | let win_pos = Point::from(( |
| 2121 | self.column_x(self.active_column_idx) - view_pos, |
| 2122 | col.window_y(col.active_window_idx), |
| 2123 | )); |
| 2124 | |
| 2125 | self.focus_ring.update(win_pos, geom.size, has_ssd); |
| 2126 | self.focus_ring.set_active(is_active); |
| 2127 | } |
| 2128 | } |
| 2129 | |
| 2130 | pub fn are_animations_ongoing(&self) -> bool { |
| 2131 | self.view_offset_anim.is_some() |
| 2132 | } |
| 2133 | |
| 2134 | fn update_config(&mut self, options: Rc<Options>) { |
| 2135 | let c = &options.focus_ring; |
| 2136 | self.focus_ring.is_off = c.off; |
| 2137 | self.focus_ring.width = c.width.into(); |
| 2138 | self.focus_ring.active_color = c.active_color; |
| 2139 | self.focus_ring.inactive_color = c.inactive_color; |
| 2140 | // The focus ring buffer will be updated in a subsequent update_animations call. |
| 2141 | |
| 2142 | for column in &mut self.columns { |
| 2143 | column.update_config(options.clone()); |
| 2144 | } |
| 2145 | |
| 2146 | self.options = options; |
| 2147 | } |
| 2148 | |
| 2149 | fn windows(&self) -> impl Iterator<Item = &W> + '_ { |
| 2150 | self.columns.iter().flat_map(|col| col.windows.iter()) |
| 2151 | } |
| 2152 | |
| 2153 | fn set_output(&mut self, output: Option<Output>) { |
| 2154 | if self.output == output { |
| 2155 | return; |
| 2156 | } |
| 2157 | |
| 2158 | if let Some(output) = self.output.take() { |
| 2159 | for win in self.windows() { |
| 2160 | win.output_leave(&output); |
| 2161 | } |
| 2162 | } |
| 2163 | |
| 2164 | self.output = output; |
| 2165 | |
| 2166 | if let Some(output) = &self.output { |
| 2167 | let working_area = layer_map_for_output(output).non_exclusive_zone(); |
| 2168 | self.set_view_size(output_size(output), working_area); |
| 2169 | |
| 2170 | for win in self.windows() { |
| 2171 | self.enter_output_for_window(win); |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | fn enter_output_for_window(&self, window: &W) { |
| 2177 | if let Some(output) = &self.output { |
| 2178 | prepare_for_output(window, output); |
| 2179 | |
| 2180 | // FIXME: proper overlap. |
| 2181 | window.output_enter( |
| 2182 | output, |
| 2183 | Rectangle::from_loc_and_size((0, 0), (i32::MAX, i32::MAX)), |
| 2184 | ); |
| 2185 | } |
| 2186 | } |
| 2187 | |
| 2188 | fn set_view_size(&mut self, size: Size<i32, Logical>, working_area: Rectangle<i32, Logical>) { |
| 2189 | if self.view_size == size && self.working_area == working_area { |
| 2190 | return; |
| 2191 | } |
| 2192 | |
| 2193 | self.view_size = size; |
| 2194 | self.working_area = working_area; |
| 2195 | |
| 2196 | for col in &mut self.columns { |
| 2197 | col.set_view_size(self.view_size, self.working_area); |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | fn toplevel_bounds(&self) -> Size<i32, Logical> { |
| 2202 | Size::from(( |
| 2203 | max(self.working_area.size.w - self.options.gaps * 2, 1), |
| 2204 | max(self.working_area.size.h - self.options.gaps * 2, 1), |
| 2205 | )) |
| 2206 | } |
| 2207 | |
| 2208 | pub fn configure_new_window(&self, window: &Window) { |
| 2209 | let width = if let Some(width) = self.options.default_width { |
| 2210 | max(1, width.resolve(&self.options, self.working_area.size.w)) |
| 2211 | } else { |
| 2212 | 0 |
| 2213 | }; |
| 2214 | |
| 2215 | let height = self.working_area.size.h - self.options.gaps * 2; |
| 2216 | let size = Size::from((width, max(height, 1))); |
| 2217 | |
| 2218 | let bounds = self.toplevel_bounds(); |
| 2219 | |
| 2220 | if let Some(output) = self.output.as_ref() { |
| 2221 | prepare_for_output(window, output); |
| 2222 | } |
| 2223 | |
| 2224 | window.toplevel().with_pending_state(|state| { |
| 2225 | state.size = Some(size); |
| 2226 | state.bounds = Some(bounds); |
| 2227 | }); |
| 2228 | } |
| 2229 | |
| 2230 | fn compute_new_view_offset_for_column(&self, current_x: i32, idx: usize) -> i32 { |
| 2231 | if self.columns[idx].is_fullscreen { |
| 2232 | return 0; |
| 2233 | } |
| 2234 | |
| 2235 | let new_col_x = self.column_x(idx); |
| 2236 | |
| 2237 | let final_x = if let Some(anim) = &self.view_offset_anim { |
| 2238 | current_x - self.view_offset + anim.to().round() as i32 |
| 2239 | } else { |
| 2240 | current_x |
| 2241 | }; |
| 2242 | |
| 2243 | let new_offset = compute_new_view_offset( |
| 2244 | final_x + self.working_area.loc.x, |
| 2245 | self.working_area.size.w, |
| 2246 | new_col_x, |
| 2247 | self.columns[idx].width(), |
| 2248 | self.options.gaps, |
| 2249 | ); |
| 2250 | |
| 2251 | // Non-fullscreen windows are always offset at least by the working area position. |
| 2252 | new_offset - self.working_area.loc.x |
| 2253 | } |
| 2254 | |
| 2255 | fn animate_view_offset_to_column(&mut self, current_x: i32, idx: usize) { |
| 2256 | let new_view_offset = self.compute_new_view_offset_for_column(current_x, idx); |
| 2257 | |
| 2258 | let new_col_x = self.column_x(idx); |
| 2259 | let from_view_offset = current_x - new_col_x; |
| 2260 | self.view_offset = from_view_offset; |
| 2261 | |
| 2262 | // If we're already animating towards that, don't restart it. |
| 2263 | if let Some(anim) = &self.view_offset_anim { |
| 2264 | if anim.value().round() as i32 == self.view_offset |
| 2265 | && anim.to().round() as i32 == new_view_offset |
| 2266 | { |
| 2267 | return; |
| 2268 | } |
| 2269 | } |
| 2270 | |
| 2271 | // If our view offset is already this, we don't need to do anything. |
| 2272 | if self.view_offset == new_view_offset { |
| 2273 | self.view_offset_anim = None; |
| 2274 | return; |
| 2275 | } |
| 2276 | |
| 2277 | self.view_offset_anim = Some(Animation::new( |
| 2278 | self.view_offset as f64, |
| 2279 | new_view_offset as f64, |
| 2280 | Duration::from_millis(250), |
| 2281 | )); |
| 2282 | } |
| 2283 | |
| 2284 | fn activate_column(&mut self, idx: usize) { |
| 2285 | if self.active_column_idx == idx { |
| 2286 | return; |
| 2287 | } |
| 2288 | |
| 2289 | let current_x = self.view_pos(); |
| 2290 | self.animate_view_offset_to_column(current_x, idx); |
| 2291 | |
| 2292 | self.active_column_idx = idx; |
| 2293 | |
| 2294 | // A different column was activated; reset the flag. |
| 2295 | self.activate_prev_column_on_removal = false; |
| 2296 | } |
| 2297 | |
| 2298 | fn has_windows(&self) -> bool { |
| 2299 | self.windows().next().is_some() |
| 2300 | } |
| 2301 | |
| 2302 | fn has_window(&self, window: &W) -> bool { |
| 2303 | self.windows().any(|win| win == window) |
| 2304 | } |
| 2305 | |
| 2306 | fn find_wl_surface(&self, wl_surface: &WlSurface) -> Option<&W> { |
| 2307 | self.windows().find(|win| win.is_wl_surface(wl_surface)) |
| 2308 | } |
| 2309 | |
| 2310 | /// Computes the X position of the windows in the given column, in logical coordinates. |
| 2311 | fn column_x(&self, column_idx: usize) -> i32 { |
| 2312 | let mut x = 0; |
| 2313 | |
| 2314 | for column in self.columns.iter().take(column_idx) { |
| 2315 | x += column.width() + self.options.gaps; |
| 2316 | } |
| 2317 | |
| 2318 | x |
| 2319 | } |
| 2320 | |
| 2321 | fn add_window(&mut self, window: W, activate: bool, width: ColumnWidth, is_full_width: bool) { |
| 2322 | self.enter_output_for_window(&window); |
| 2323 | |
| 2324 | let was_empty = self.columns.is_empty(); |
| 2325 | |
| 2326 | let idx = if self.columns.is_empty() { |
| 2327 | 0 |
| 2328 | } else { |
| 2329 | self.active_column_idx + 1 |
| 2330 | }; |
| 2331 | |
| 2332 | let column = Column::new( |
| 2333 | window, |
| 2334 | self.view_size, |
| 2335 | self.working_area, |
| 2336 | self.options.clone(), |
| 2337 | width, |
| 2338 | is_full_width, |
| 2339 | ); |
| 2340 | self.columns.insert(idx, column); |
| 2341 | |
| 2342 | if activate { |
| 2343 | // If this is the first window on an empty workspace, skip the animation from whatever |
| 2344 | // view_offset was left over. |
| 2345 | if was_empty { |
| 2346 | // Try to make the code produce a left-aligned offset, even in presence of left |
| 2347 | // exclusive zones. |
| 2348 | self.view_offset = self.compute_new_view_offset_for_column(self.column_x(0), 0); |
| 2349 | self.view_offset_anim = None; |
| 2350 | } |
| 2351 | |
| 2352 | self.activate_column(idx); |
| 2353 | self.activate_prev_column_on_removal = true; |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | fn remove_window(&mut self, window: &W) { |
| 2358 | if let Some(output) = &self.output { |
| 2359 | window.output_leave(output); |
| 2360 | } |
| 2361 | |
| 2362 | let column_idx = self |
| 2363 | .columns |
| 2364 | .iter() |
| 2365 | .position(|col| col.contains(window)) |
| 2366 | .unwrap(); |
| 2367 | let column = &mut self.columns[column_idx]; |
| 2368 | |
| 2369 | let window_idx = column.windows.iter().position(|win| win == window).unwrap(); |
| 2370 | column.windows.remove(window_idx); |
| 2371 | column.heights.remove(window_idx); |
| 2372 | if column.windows.is_empty() { |
| 2373 | if column_idx + 1 == self.active_column_idx { |
| 2374 | // The previous column, that we were going to activate upon removal of the active |
| 2375 | // column, has just been itself removed. |
| 2376 | self.activate_prev_column_on_removal = false; |
| 2377 | } |
| 2378 | |
| 2379 | // FIXME: activate_column below computes current view position to compute the new view |
| 2380 | // position, which can include the column we're removing here. This leads to unwanted |
| 2381 | // view jumps. |
| 2382 | self.columns.remove(column_idx); |
| 2383 | if self.columns.is_empty() { |
| 2384 | return; |
| 2385 | } |
| 2386 | |
| 2387 | if self.active_column_idx > column_idx |
| 2388 | || (self.active_column_idx == column_idx && self.activate_prev_column_on_removal) |
| 2389 | { |
| 2390 | // A column to the left was removed; preserve the current position. |
| 2391 | // FIXME: preserve activate_prev_column_on_removal. |
| 2392 | // Or, the active column was removed, and we needed to activate the previous column. |
| 2393 | self.activate_column(self.active_column_idx.saturating_sub(1)); |
| 2394 | } else { |
| 2395 | self.activate_column(min(self.active_column_idx, self.columns.len() - 1)); |
| 2396 | } |
| 2397 | |
| 2398 | return; |
| 2399 | } |
| 2400 | |
| 2401 | column.active_window_idx = min(column.active_window_idx, column.windows.len() - 1); |
| 2402 | column.update_window_sizes(); |
| 2403 | } |
| 2404 | |
| 2405 | fn update_window(&mut self, window: &W) { |
| 2406 | let (idx, column) = self |
| 2407 | .columns |
| 2408 | .iter_mut() |
| 2409 | .enumerate() |
| 2410 | .find(|(_, col)| col.contains(window)) |
| 2411 | .unwrap(); |
| 2412 | column.update_window_sizes(); |
| 2413 | |
| 2414 | if idx == self.active_column_idx { |
| 2415 | // We might need to move the view to ensure the resized window is still visible. |
| 2416 | let current_x = self.view_pos(); |
| 2417 | self.animate_view_offset_to_column(current_x, idx); |
| 2418 | } |
| 2419 | } |
| 2420 | |
| 2421 | fn activate_window(&mut self, window: &W) { |
| 2422 | let column_idx = self |
| 2423 | .columns |
| 2424 | .iter() |
| 2425 | .position(|col| col.contains(window)) |
| 2426 | .unwrap(); |
| 2427 | let column = &mut self.columns[column_idx]; |
| 2428 | |
| 2429 | column.activate_window(window); |
| 2430 | self.activate_column(column_idx); |
| 2431 | } |
| 2432 | |
| 2433 | #[cfg(test)] |
| 2434 | fn verify_invariants(&self) { |
| 2435 | assert!(self.view_size.w > 0); |
| 2436 | assert!(self.view_size.h > 0); |
| 2437 | |
| 2438 | if !self.columns.is_empty() { |
| 2439 | assert!(self.active_column_idx < self.columns.len()); |
| 2440 | |
| 2441 | for column in &self.columns { |
| 2442 | column.verify_invariants(); |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | |
| 2447 | fn focus_left(&mut self) { |
| 2448 | self.activate_column(self.active_column_idx.saturating_sub(1)); |
| 2449 | } |
| 2450 | |
| 2451 | fn focus_right(&mut self) { |
| 2452 | if self.columns.is_empty() { |
| 2453 | return; |
| 2454 | } |
| 2455 | |
| 2456 | self.activate_column(min(self.active_column_idx + 1, self.columns.len() - 1)); |
| 2457 | } |
| 2458 | |
| 2459 | fn focus_down(&mut self) { |
| 2460 | if self.columns.is_empty() { |
| 2461 | return; |
| 2462 | } |
| 2463 | |
| 2464 | self.columns[self.active_column_idx].focus_down(); |
| 2465 | } |
| 2466 | |
| 2467 | fn focus_up(&mut self) { |
| 2468 | if self.columns.is_empty() { |
| 2469 | return; |
| 2470 | } |
| 2471 | |
| 2472 | self.columns[self.active_column_idx].focus_up(); |
| 2473 | } |
| 2474 | |
| 2475 | fn move_left(&mut self) { |
| 2476 | let new_idx = self.active_column_idx.saturating_sub(1); |
| 2477 | if self.active_column_idx == new_idx { |
| 2478 | return; |
| 2479 | } |
| 2480 | |
| 2481 | let current_x = self.view_pos(); |
| 2482 | |
| 2483 | self.columns.swap(self.active_column_idx, new_idx); |
| 2484 | |
| 2485 | self.view_offset = |
| 2486 | self.compute_new_view_offset_for_column(current_x, self.active_column_idx); |
| 2487 | |
| 2488 | self.activate_column(new_idx); |
| 2489 | } |
| 2490 | |
| 2491 | fn move_right(&mut self) { |
| 2492 | if self.columns.is_empty() { |
| 2493 | return; |
| 2494 | } |
| 2495 | |
| 2496 | let new_idx = min(self.active_column_idx + 1, self.columns.len() - 1); |
| 2497 | if self.active_column_idx == new_idx { |
| 2498 | return; |
| 2499 | } |
| 2500 | |
| 2501 | let current_x = self.view_pos(); |
| 2502 | |
| 2503 | self.columns.swap(self.active_column_idx, new_idx); |
| 2504 | |
| 2505 | self.view_offset = |
| 2506 | self.compute_new_view_offset_for_column(current_x, self.active_column_idx); |
| 2507 | |
| 2508 | self.activate_column(new_idx); |
| 2509 | } |
| 2510 | |
| 2511 | fn move_down(&mut self) { |
| 2512 | if self.columns.is_empty() { |
| 2513 | return; |
| 2514 | } |
| 2515 | |
| 2516 | self.columns[self.active_column_idx].move_down(); |
| 2517 | } |
| 2518 | |
| 2519 | fn move_up(&mut self) { |
| 2520 | if self.columns.is_empty() { |
| 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | self.columns[self.active_column_idx].move_up(); |
| 2525 | } |
| 2526 | |
| 2527 | fn consume_into_column(&mut self) { |
| 2528 | if self.columns.len() < 2 { |
| 2529 | return; |
| 2530 | } |
| 2531 | |
| 2532 | if self.active_column_idx == self.columns.len() - 1 { |
| 2533 | return; |
| 2534 | } |
| 2535 | |
| 2536 | let source_column_idx = self.active_column_idx + 1; |
| 2537 | |
| 2538 | let source_column = &mut self.columns[source_column_idx]; |
| 2539 | let window = source_column.windows[0].clone(); |
| 2540 | self.remove_window(&window); |
| 2541 | |
| 2542 | let target_column = &mut self.columns[self.active_column_idx]; |
| 2543 | target_column.add_window(window); |
| 2544 | } |
| 2545 | |
| 2546 | fn expel_from_column(&mut self) { |
| 2547 | if self.columns.is_empty() { |
| 2548 | return; |
| 2549 | } |
| 2550 | |
| 2551 | let source_column = &mut self.columns[self.active_column_idx]; |
| 2552 | if source_column.windows.len() == 1 { |
| 2553 | return; |
| 2554 | } |
| 2555 | |
| 2556 | let width = source_column.width; |
| 2557 | let is_full_width = source_column.is_full_width; |
| 2558 | let window = source_column.windows[source_column.active_window_idx].clone(); |
| 2559 | self.remove_window(&window); |
| 2560 | |
| 2561 | self.add_window(window, true, width, is_full_width); |
| 2562 | } |
| 2563 | |
| 2564 | fn center_column(&mut self) { |
| 2565 | if self.columns.is_empty() { |
| 2566 | return; |
| 2567 | } |
| 2568 | |
| 2569 | let col = &self.columns[self.active_column_idx]; |
| 2570 | if col.is_fullscreen { |
| 2571 | return; |
| 2572 | } |
| 2573 | |
| 2574 | let width = col.width(); |
| 2575 | |
| 2576 | // If the column is wider than the working area, then on commit it will be shifted to left |
| 2577 | // edge alignment by the usual positioning code, so there's no use in doing anything here. |
| 2578 | if self.working_area.size.w <= width { |
| 2579 | return; |
| 2580 | } |
| 2581 | |
| 2582 | let new_view_offset = -(self.working_area.size.w - width) / 2 - self.working_area.loc.x; |
| 2583 | |
| 2584 | // If we're already animating towards that, don't restart it. |
| 2585 | if let Some(anim) = &self.view_offset_anim { |
| 2586 | if anim.to().round() as i32 == new_view_offset { |
| 2587 | return; |
| 2588 | } |
| 2589 | } |
| 2590 | |
| 2591 | // If our view offset is already this, we don't need to do anything. |
| 2592 | if self.view_offset == new_view_offset { |
| 2593 | return; |
| 2594 | } |
| 2595 | |
| 2596 | self.view_offset_anim = Some(Animation::new( |
| 2597 | self.view_offset as f64, |
| 2598 | new_view_offset as f64, |
| 2599 | Duration::from_millis(250), |
| 2600 | )); |
| 2601 | } |
| 2602 | |
| 2603 | fn view_pos(&self) -> i32 { |
| 2604 | self.column_x(self.active_column_idx) + self.view_offset |
| 2605 | } |
| 2606 | |
| 2607 | fn window_under(&self, pos: Point<f64, Logical>) -> Option<(&W, Point<i32, Logical>)> { |
| 2608 | if self.columns.is_empty() { |
| 2609 | return None; |
| 2610 | } |
| 2611 | |
| 2612 | let view_pos = self.view_pos(); |
| 2613 | |
| 2614 | // Prefer the active window since it's drawn on top. |
| 2615 | let col = &self.columns[self.active_column_idx]; |
| 2616 | let active_win = &col.windows[col.active_window_idx]; |
| 2617 | let geom = active_win.geometry(); |
| 2618 | let buf_pos = Point::from(( |
| 2619 | self.column_x(self.active_column_idx) - view_pos, |
| 2620 | col.window_y(col.active_window_idx), |
| 2621 | )) - geom.loc; |
| 2622 | if active_win.is_in_input_region(&(pos - buf_pos.to_f64())) { |
| 2623 | return Some((active_win, buf_pos)); |
| 2624 | } |
| 2625 | |
| 2626 | let mut x = -view_pos; |
| 2627 | for col in &self.columns { |
| 2628 | for (win, y) in zip(&col.windows, col.window_ys()) { |
| 2629 | if win == active_win { |
| 2630 | // Already handled it above. |
| 2631 | continue; |
| 2632 | } |
| 2633 | |
| 2634 | let geom = win.geometry(); |
| 2635 | let buf_pos = Point::from((x, y)) - geom.loc; |
| 2636 | if win.is_in_input_region(&(pos - buf_pos.to_f64())) { |
| 2637 | return Some((win, buf_pos)); |
| 2638 | } |
| 2639 | } |
| 2640 | |
| 2641 | x += col.width() + self.options.gaps; |
| 2642 | } |
| 2643 | |
| 2644 | None |
| 2645 | } |
| 2646 | |
| 2647 | fn toggle_width(&mut self) { |
| 2648 | if self.columns.is_empty() { |
| 2649 | return; |
| 2650 | } |
| 2651 | |
| 2652 | self.columns[self.active_column_idx].toggle_width(); |
| 2653 | } |
| 2654 | |
| 2655 | fn toggle_full_width(&mut self) { |
| 2656 | if self.columns.is_empty() { |
| 2657 | return; |
| 2658 | } |
| 2659 | |
| 2660 | self.columns[self.active_column_idx].toggle_full_width(); |
| 2661 | } |
| 2662 | |
| 2663 | fn set_column_width(&mut self, change: SizeChange) { |
| 2664 | if self.columns.is_empty() { |
| 2665 | return; |
| 2666 | } |
| 2667 | |
| 2668 | self.columns[self.active_column_idx].set_column_width(change); |
| 2669 | } |
| 2670 | |
| 2671 | fn set_window_height(&mut self, change: SizeChange) { |
| 2672 | if self.columns.is_empty() { |
| 2673 | return; |
| 2674 | } |
| 2675 | |
| 2676 | self.columns[self.active_column_idx].set_window_height(change); |
| 2677 | } |
| 2678 | |
| 2679 | pub fn set_fullscreen(&mut self, window: &W, is_fullscreen: bool) { |
| 2680 | let (mut col_idx, win_idx) = self |
| 2681 | .columns |
| 2682 | .iter() |
| 2683 | .enumerate() |
| 2684 | .find_map(|(col_idx, col)| { |
| 2685 | col.windows |
| 2686 | .iter() |
| 2687 | .position(|w| w == window) |
| 2688 | .map(|win_idx| (col_idx, win_idx)) |
| 2689 | }) |
| 2690 | .unwrap(); |
| 2691 | |
| 2692 | let mut col = &mut self.columns[col_idx]; |
| 2693 | |
| 2694 | if is_fullscreen && col.windows.len() > 1 { |
| 2695 | // This wasn't the only window in its column; extract it into a separate column. |
| 2696 | let target_window_was_focused = |
| 2697 | self.active_column_idx == col_idx && col.active_window_idx == win_idx; |
| 2698 | let window = col.windows.remove(win_idx); |
| 2699 | col.heights.remove(win_idx); |
| 2700 | col.active_window_idx = min(col.active_window_idx, col.windows.len() - 1); |
| 2701 | col.update_window_sizes(); |
| 2702 | let width = col.width; |
| 2703 | let is_full_width = col.is_full_width; |
| 2704 | |
| 2705 | col_idx += 1; |
| 2706 | self.columns.insert( |
| 2707 | col_idx, |
| 2708 | Column::new( |
| 2709 | window, |
| 2710 | self.view_size, |
| 2711 | self.working_area, |
| 2712 | self.options.clone(), |
| 2713 | width, |
| 2714 | is_full_width, |
| 2715 | ), |
| 2716 | ); |
| 2717 | if self.active_column_idx >= col_idx || target_window_was_focused { |
| 2718 | self.active_column_idx += 1; |
| 2719 | } |
| 2720 | col = &mut self.columns[col_idx]; |
| 2721 | } |
| 2722 | |
| 2723 | col.set_fullscreen(is_fullscreen); |
| 2724 | } |
| 2725 | |
| 2726 | pub fn toggle_fullscreen(&mut self, window: &W) { |
| 2727 | let col = self |
| 2728 | .columns |
| 2729 | .iter_mut() |
| 2730 | .find(|col| col.windows.contains(window)) |
| 2731 | .unwrap(); |
| 2732 | let value = !col.is_fullscreen; |
| 2733 | self.set_fullscreen(window, value); |
| 2734 | } |
| 2735 | |
| 2736 | pub fn render_above_top_layer(&self) -> bool { |
| 2737 | // Render above the top layer if we're on a fullscreen window and the view is stationary. |
| 2738 | if self.columns.is_empty() { |
| 2739 | return false; |
| 2740 | } |
| 2741 | |
| 2742 | if self.view_offset_anim.is_some() { |
| 2743 | return false; |
| 2744 | } |
| 2745 | |
| 2746 | self.columns[self.active_column_idx].is_fullscreen |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | impl Workspace<Window> { |
| 2751 | fn refresh(&self) { |
| 2752 | let bounds = self.toplevel_bounds(); |
| 2753 | |
| 2754 | for (col_idx, col) in self.columns.iter().enumerate() { |
| 2755 | for (win_idx, win) in col.windows.iter().enumerate() { |
| 2756 | let active = self.active_column_idx == col_idx && col.active_window_idx == win_idx; |
| 2757 | win.set_activated(active); |
| 2758 | |
| 2759 | win.toplevel().with_pending_state(|state| { |
| 2760 | state.bounds = Some(bounds); |
| 2761 | }); |
| 2762 | |
| 2763 | win.toplevel().send_pending_configure(); |
| 2764 | win.refresh(); |
| 2765 | } |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | pub fn render_elements( |
| 2770 | &self, |
| 2771 | renderer: &mut GlesRenderer, |
| 2772 | ) -> Vec<WorkspaceRenderElement<GlesRenderer>> { |
| 2773 | if self.columns.is_empty() { |
| 2774 | return vec![]; |
| 2775 | } |
| 2776 | |
| 2777 | // FIXME: workspaces should probably cache their last used scale so they can be correctly |
| 2778 | // rendered even with no outputs connected. |
| 2779 | let output_scale = self |
| 2780 | .output |
| 2781 | .as_ref() |
| 2782 | .map(|o| Scale::from(o.current_scale().fractional_scale())) |
| 2783 | .unwrap_or(Scale::from(1.)); |
| 2784 | |
| 2785 | let mut rv = vec![]; |
| 2786 | let view_pos = self.view_pos(); |
| 2787 | |
| 2788 | // Draw the active window on top. |
| 2789 | let col = &self.columns[self.active_column_idx]; |
| 2790 | let active_win = &col.windows[col.active_window_idx]; |
| 2791 | let win_pos = Point::from(( |
| 2792 | self.column_x(self.active_column_idx) - view_pos, |
| 2793 | col.window_y(col.active_window_idx), |
| 2794 | )); |
| 2795 | |
| 2796 | // Draw the window itself. |
| 2797 | let geom = active_win.geometry(); |
| 2798 | let buf_pos = win_pos - geom.loc; |
| 2799 | rv.extend(active_win.render_elements( |
| 2800 | renderer, |
| 2801 | buf_pos.to_physical_precise_round(output_scale), |
| 2802 | output_scale, |
| 2803 | 1., |
| 2804 | )); |
| 2805 | |
| 2806 | // Draw the focus ring. |
| 2807 | rv.extend(self.focus_ring.render(output_scale).map(Into::into)); |
| 2808 | |
| 2809 | let mut x = -view_pos; |
| 2810 | for col in &self.columns { |
| 2811 | for (win, y) in zip(&col.windows, col.window_ys()) { |
| 2812 | if win == active_win { |
| 2813 | // Already handled it above. |
| 2814 | continue; |
| 2815 | } |
| 2816 | |
| 2817 | let geom = win.geometry(); |
| 2818 | let buf_pos = Point::from((x, y)) - geom.loc; |
| 2819 | rv.extend(win.render_elements( |
| 2820 | renderer, |
| 2821 | buf_pos.to_physical_precise_round(output_scale), |
| 2822 | output_scale, |
| 2823 | 1., |
| 2824 | )); |
| 2825 | } |
| 2826 | |
| 2827 | x += col.width() + self.options.gaps; |
| 2828 | } |
| 2829 | |
| 2830 | rv |
| 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | impl<W: LayoutElement> Column<W> { |
| 2835 | fn new( |
| 2836 | window: W, |
| 2837 | view_size: Size<i32, Logical>, |
| 2838 | working_area: Rectangle<i32, Logical>, |
| 2839 | options: Rc<Options>, |
| 2840 | width: ColumnWidth, |
| 2841 | is_full_width: bool, |
| 2842 | ) -> Self { |
| 2843 | let mut rv = Self { |
| 2844 | windows: vec![], |
| 2845 | heights: vec![], |
| 2846 | active_window_idx: 0, |
| 2847 | width, |
| 2848 | is_full_width, |
| 2849 | is_fullscreen: false, |
| 2850 | view_size, |
| 2851 | working_area, |
| 2852 | options, |
| 2853 | }; |
| 2854 | |
| 2855 | rv.add_window(window); |
| 2856 | |
| 2857 | rv |
| 2858 | } |
| 2859 | |
| 2860 | fn set_view_size(&mut self, size: Size<i32, Logical>, working_area: Rectangle<i32, Logical>) { |
| 2861 | if self.view_size == size && self.working_area == working_area { |
| 2862 | return; |
| 2863 | } |
| 2864 | |
| 2865 | self.view_size = size; |
| 2866 | self.working_area = working_area; |
| 2867 | |
| 2868 | self.update_window_sizes(); |
| 2869 | } |
| 2870 | |
| 2871 | fn update_config(&mut self, options: Rc<Options>) { |
| 2872 | let mut update_sizes = false; |
| 2873 | |
| 2874 | // If preset widths changed, make our width non-preset. |
| 2875 | if self.options.preset_widths != options.preset_widths { |
| 2876 | if let ColumnWidth::Preset(idx) = self.width { |
| 2877 | self.width = self.options.preset_widths[idx]; |
| 2878 | } |
| 2879 | } |
| 2880 | |
| 2881 | if self.options.gaps != options.gaps { |
| 2882 | update_sizes = true; |
| 2883 | } |
| 2884 | |
| 2885 | self.options = options; |
| 2886 | |
| 2887 | if update_sizes { |
| 2888 | self.update_window_sizes(); |
| 2889 | } |
| 2890 | } |
| 2891 | |
| 2892 | fn set_width(&mut self, width: ColumnWidth) { |
| 2893 | self.width = width; |
| 2894 | self.is_full_width = false; |
| 2895 | self.update_window_sizes(); |
| 2896 | } |
| 2897 | |
| 2898 | fn contains(&self, window: &W) -> bool { |
| 2899 | self.windows.iter().any(|win| win == window) |
| 2900 | } |
| 2901 | |
| 2902 | fn activate_window(&mut self, window: &W) { |
| 2903 | let idx = self.windows.iter().position(|win| win == window).unwrap(); |
| 2904 | self.active_window_idx = idx; |
| 2905 | } |
| 2906 | |
| 2907 | fn add_window(&mut self, window: W) { |
| 2908 | self.is_fullscreen = false; |
| 2909 | self.windows.push(window); |
| 2910 | self.heights.push(WindowHeight::Auto); |
| 2911 | self.update_window_sizes(); |
| 2912 | } |
| 2913 | |
| 2914 | fn update_window_sizes(&mut self) { |
| 2915 | if self.is_fullscreen { |
| 2916 | self.windows[0].request_fullscreen(self.view_size); |
| 2917 | return; |
| 2918 | } |
| 2919 | |
| 2920 | let min_size: Vec<_> = self.windows.iter().map(LayoutElement::min_size).collect(); |
| 2921 | let max_size: Vec<_> = self.windows.iter().map(LayoutElement::max_size).collect(); |
| 2922 | |
| 2923 | // Compute the column width. |
| 2924 | let min_width = min_size |
| 2925 | .iter() |
| 2926 | .filter_map(|size| { |
| 2927 | let w = size.w; |
| 2928 | if w == 0 { |
| 2929 | None |
| 2930 | } else { |
| 2931 | Some(w) |
| 2932 | } |
| 2933 | }) |
| 2934 | .max() |
| 2935 | .unwrap_or(1); |
| 2936 | let max_width = max_size |
| 2937 | .iter() |
| 2938 | .filter_map(|size| { |
| 2939 | let w = size.w; |
| 2940 | if w == 0 { |
| 2941 | None |
| 2942 | } else { |
| 2943 | Some(w) |
| 2944 | } |
| 2945 | }) |
| 2946 | .min() |
| 2947 | .unwrap_or(i32::MAX); |
| 2948 | let max_width = max(max_width, min_width); |
| 2949 | |
| 2950 | let width = if self.is_full_width { |
| 2951 | ColumnWidth::Proportion(1.) |
| 2952 | } else { |
| 2953 | self.width |
| 2954 | }; |
| 2955 | |
| 2956 | let width = width.resolve(&self.options, self.working_area.size.w); |
| 2957 | let width = max(min(width, max_width), min_width); |
| 2958 | |
| 2959 | // Compute the window heights. |
| 2960 | let mut heights = self.heights.clone(); |
| 2961 | let mut height_left = self.working_area.size.h - self.options.gaps; |
| 2962 | let mut auto_windows_left = self.windows.len(); |
| 2963 | |
| 2964 | // Subtract all fixed-height windows. |
| 2965 | for (h, (min_size, max_size)) in zip(&mut heights, zip(&min_size, &max_size)) { |
| 2966 | // Check if the window has an exact height constraint. |
| 2967 | if min_size.h > 0 && min_size.h == max_size.h { |
| 2968 | *h = WindowHeight::Fixed(min_size.h); |
| 2969 | } |
| 2970 | |
| 2971 | if let WindowHeight::Fixed(h) = h { |
| 2972 | if max_size.h > 0 { |
| 2973 | *h = min(*h, max_size.h); |
| 2974 | } |
| 2975 | if min_size.h > 0 { |
| 2976 | *h = max(*h, min_size.h); |
| 2977 | } |
| 2978 | *h = max(*h, 1); |
| 2979 | |
| 2980 | height_left -= *h + self.options.gaps; |
| 2981 | auto_windows_left -= 1; |
| 2982 | } |
| 2983 | } |
| 2984 | |
| 2985 | // Iteratively try to distribute the remaining height, checking against window min heights. |
| 2986 | // Pick an auto height according to the current sizes, then check if it satisfies all |
| 2987 | // remaining min heights. If not, allocate fixed height to those windows and repeat the |
| 2988 | // loop. On each iteration the auto height will get smaller. |
| 2989 | // |
| 2990 | // NOTE: we do not respect max height here. Doing so would complicate things: if the current |
| 2991 | // auto height is above some window's max height, then the auto height can become larger. |
| 2992 | // Combining this with the min height loop is where the complexity appears. |
| 2993 | // |
| 2994 | // However, most max height uses are for fixed-size dialogs, where min height == max_height. |
| 2995 | // This case is separately handled above. |
| 2996 | while auto_windows_left > 0 { |
| 2997 | // Compute the current auto height. |
| 2998 | let auto_height = height_left / auto_windows_left as i32 - self.options.gaps; |
| 2999 | let auto_height = max(auto_height, 1); |
| 3000 | |
| 3001 | // Integer division above can result in imperfect height distribution. We will make some |
| 3002 | // windows 1 px taller to account for this. |
| 3003 | let mut ones_left = height_left |
| 3004 | .saturating_sub((auto_height + self.options.gaps) * auto_windows_left as i32); |
| 3005 | |
| 3006 | let mut unsatisfied_min = false; |
| 3007 | let mut ones_left_2 = ones_left; |
| 3008 | for (h, min_size) in zip(&mut heights, &min_size) { |
| 3009 | if matches!(h, WindowHeight::Fixed(_)) { |
| 3010 | continue; |
| 3011 | } |
| 3012 | |
| 3013 | let mut auto = auto_height; |
| 3014 | if ones_left_2 > 0 { |
| 3015 | auto += 1; |
| 3016 | ones_left_2 -= 1; |
| 3017 | } |
| 3018 | |
| 3019 | // Check if the auto height satisfies the min height. |
| 3020 | if min_size.h > 0 && min_size.h > auto { |
| 3021 | *h = WindowHeight::Fixed(min_size.h); |
| 3022 | height_left -= min_size.h + self.options.gaps; |
| 3023 | auto_windows_left -= 1; |
| 3024 | unsatisfied_min = true; |
| 3025 | } |
| 3026 | } |
| 3027 | |
| 3028 | // If some min height was unsatisfied, then we allocated the window more than the auto |
| 3029 | // height, which means that the remaining auto windows now have less height to work |
| 3030 | // with, and the loop must run again. |
| 3031 | if unsatisfied_min { |
| 3032 | continue; |
| 3033 | } |
| 3034 | |
| 3035 | // All min heights were satisfied, fill them in. |
| 3036 | for h in &mut heights { |
| 3037 | if matches!(h, WindowHeight::Fixed(_)) { |
| 3038 | continue; |
| 3039 | } |
| 3040 | |
| 3041 | let mut auto = auto_height; |
| 3042 | if ones_left > 0 { |
| 3043 | auto += 1; |
| 3044 | ones_left -= 1; |
| 3045 | } |
| 3046 | |
| 3047 | *h = WindowHeight::Fixed(auto); |
| 3048 | auto_windows_left -= 1; |
| 3049 | } |
| 3050 | |
| 3051 | assert_eq!(auto_windows_left, 0); |
| 3052 | } |
| 3053 | |
| 3054 | for (win, h) in zip(&self.windows, heights) { |
| 3055 | let WindowHeight::Fixed(height) = h else { |
| 3056 | unreachable!() |
| 3057 | }; |
| 3058 | |
| 3059 | let size = Size::from((width, height)); |
| 3060 | win.request_size(size); |
| 3061 | } |
| 3062 | } |
| 3063 | |
| 3064 | fn width(&self) -> i32 { |
| 3065 | self.windows |
| 3066 | .iter() |
| 3067 | .map(|win| win.geometry().size.w) |
| 3068 | .max() |
| 3069 | .unwrap() |
| 3070 | } |
| 3071 | |
| 3072 | fn focus_up(&mut self) { |
| 3073 | self.active_window_idx = self.active_window_idx.saturating_sub(1); |
| 3074 | } |
| 3075 | |
| 3076 | fn focus_down(&mut self) { |
| 3077 | self.active_window_idx = min(self.active_window_idx + 1, self.windows.len() - 1); |
| 3078 | } |
| 3079 | |
| 3080 | fn move_up(&mut self) { |
| 3081 | let new_idx = self.active_window_idx.saturating_sub(1); |
| 3082 | if self.active_window_idx == new_idx { |
| 3083 | return; |
| 3084 | } |
| 3085 | |
| 3086 | self.windows.swap(self.active_window_idx, new_idx); |
| 3087 | self.heights.swap(self.active_window_idx, new_idx); |
| 3088 | self.active_window_idx = new_idx; |
| 3089 | } |
| 3090 | |
| 3091 | fn move_down(&mut self) { |
| 3092 | let new_idx = min(self.active_window_idx + 1, self.windows.len() - 1); |
| 3093 | if self.active_window_idx == new_idx { |
| 3094 | return; |
| 3095 | } |
| 3096 | |
| 3097 | self.windows.swap(self.active_window_idx, new_idx); |
| 3098 | self.heights.swap(self.active_window_idx, new_idx); |
| 3099 | self.active_window_idx = new_idx; |
| 3100 | } |
| 3101 | |
| 3102 | #[cfg(test)] |
| 3103 | fn verify_invariants(&self) { |
| 3104 | assert!(!self.windows.is_empty(), "columns can't be empty"); |
| 3105 | assert!(self.active_window_idx < self.windows.len()); |
| 3106 | assert_eq!(self.windows.len(), self.heights.len()); |
| 3107 | |
| 3108 | if self.is_fullscreen { |
| 3109 | assert_eq!(self.windows.len(), 1); |
| 3110 | } |
| 3111 | } |
| 3112 | |
| 3113 | fn toggle_width(&mut self) { |
| 3114 | let width = if self.is_full_width { |
| 3115 | ColumnWidth::Proportion(1.) |
| 3116 | } else { |
| 3117 | self.width |
| 3118 | }; |
| 3119 | |
| 3120 | let idx = match width { |
| 3121 | ColumnWidth::Preset(idx) => (idx + 1) % self.options.preset_widths.len(), |
| 3122 | _ => { |
| 3123 | let current = self.width(); |
| 3124 | self.options |
| 3125 | .preset_widths |
| 3126 | .iter() |
| 3127 | .position(|prop| { |
| 3128 | prop.resolve(&self.options, self.working_area.size.w) > current |
| 3129 | }) |
| 3130 | .unwrap_or(0) |
| 3131 | } |
| 3132 | }; |
| 3133 | let width = ColumnWidth::Preset(idx); |
| 3134 | self.set_width(width); |
| 3135 | } |
| 3136 | |
| 3137 | fn toggle_full_width(&mut self) { |
| 3138 | self.is_full_width = !self.is_full_width; |
| 3139 | self.update_window_sizes(); |
| 3140 | } |
| 3141 | |
| 3142 | fn set_column_width(&mut self, change: SizeChange) { |
| 3143 | let width = if self.is_full_width { |
| 3144 | ColumnWidth::Proportion(1.) |
| 3145 | } else { |
| 3146 | self.width |
| 3147 | }; |
| 3148 | |
| 3149 | let current_px = width.resolve(&self.options, self.working_area.size.w); |
| 3150 | |
| 3151 | let current = match width { |
| 3152 | ColumnWidth::Preset(idx) => self.options.preset_widths[idx], |
| 3153 | current => current, |
| 3154 | }; |
| 3155 | |
| 3156 | // FIXME: fix overflows then remove limits. |
| 3157 | const MAX_PX: i32 = 100000; |
| 3158 | const MAX_F: f64 = 10000.; |
| 3159 | |
| 3160 | let width = match (current, change) { |
| 3161 | (_, SizeChange::SetFixed(fixed)) => ColumnWidth::Fixed(fixed.clamp(1, MAX_PX)), |
| 3162 | (_, SizeChange::SetProportion(proportion)) => { |
| 3163 | ColumnWidth::Proportion((proportion / 100.).clamp(0., MAX_F)) |
| 3164 | } |
| 3165 | (_, SizeChange::AdjustFixed(delta)) => { |
| 3166 | let width = current_px.saturating_add(delta).clamp(1, MAX_PX); |
| 3167 | ColumnWidth::Fixed(width) |
| 3168 | } |
| 3169 | (ColumnWidth::Proportion(current), SizeChange::AdjustProportion(delta)) => { |
| 3170 | let proportion = (current + delta / 100.).clamp(0., MAX_F); |
| 3171 | ColumnWidth::Proportion(proportion) |
| 3172 | } |
| 3173 | (ColumnWidth::Fixed(_), SizeChange::AdjustProportion(delta)) => { |
| 3174 | let current = (current_px + self.options.gaps) as f64 |
| 3175 | / (self.working_area.size.w - self.options.gaps) as f64; |
| 3176 | let proportion = (current + delta / 100.).clamp(0., MAX_F); |
| 3177 | ColumnWidth::Proportion(proportion) |
| 3178 | } |
| 3179 | (ColumnWidth::Preset(_), _) => unreachable!(), |
| 3180 | }; |
| 3181 | |
| 3182 | self.set_width(width); |
| 3183 | } |
| 3184 | |
| 3185 | fn set_window_height(&mut self, change: SizeChange) { |
| 3186 | let current = self.heights[self.active_window_idx]; |
| 3187 | let current_px = match current { |
| 3188 | WindowHeight::Auto => self.windows[self.active_window_idx].geometry().size.h, |
| 3189 | WindowHeight::Fixed(height) => height, |
| 3190 | }; |
| 3191 | let current_prop = (current_px + self.options.gaps) as f64 |
| 3192 | / (self.working_area.size.h - self.options.gaps) as f64; |
| 3193 | |
| 3194 | // FIXME: fix overflows then remove limits. |
| 3195 | const MAX_PX: i32 = 100000; |
| 3196 | |
| 3197 | let mut height = match change { |
| 3198 | SizeChange::SetFixed(fixed) => fixed, |
| 3199 | SizeChange::SetProportion(proportion) => { |
| 3200 | ((self.working_area.size.h - self.options.gaps) as f64 * proportion |
| 3201 | - self.options.gaps as f64) |
| 3202 | .round() as i32 |
| 3203 | } |
| 3204 | SizeChange::AdjustFixed(delta) => current_px.saturating_add(delta), |
| 3205 | SizeChange::AdjustProportion(delta) => { |
| 3206 | let proportion = current_prop + delta / 100.; |
| 3207 | ((self.working_area.size.h - self.options.gaps) as f64 * proportion |
| 3208 | - self.options.gaps as f64) |
| 3209 | .round() as i32 |
| 3210 | } |
| 3211 | }; |
| 3212 | |
| 3213 | // Clamp it against the window height constraints. |
| 3214 | let win = &self.windows[self.active_window_idx]; |
| 3215 | let min_h = win.min_size().h; |
| 3216 | let max_h = win.max_size().h; |
| 3217 | |
| 3218 | if max_h > 0 { |
| 3219 | height = height.min(max_h); |
| 3220 | } |
| 3221 | if min_h > 0 { |
| 3222 | height = height.max(min_h); |
| 3223 | } |
| 3224 | |
| 3225 | self.heights[self.active_window_idx] = WindowHeight::Fixed(height.clamp(1, MAX_PX)); |
| 3226 | self.update_window_sizes(); |
| 3227 | } |
| 3228 | |
| 3229 | fn set_fullscreen(&mut self, is_fullscreen: bool) { |
| 3230 | assert_eq!(self.windows.len(), 1); |
| 3231 | self.is_fullscreen = is_fullscreen; |
| 3232 | self.update_window_sizes(); |
| 3233 | } |
| 3234 | |
| 3235 | fn window_y(&self, window_idx: usize) -> i32 { |
| 3236 | self.window_ys().nth(window_idx).unwrap() |
| 3237 | } |
| 3238 | |
| 3239 | fn window_ys(&self) -> impl Iterator<Item = i32> + '_ { |
| 3240 | let mut y = 0; |
| 3241 | |
| 3242 | if !self.is_fullscreen { |
| 3243 | y = self.working_area.loc.y + self.options.gaps; |
| 3244 | } |
| 3245 | |
| 3246 | self.windows.iter().map(move |win| { |
| 3247 | let pos = y; |
| 3248 | y += win.geometry().size.h + self.options.gaps; |
| 3249 | pos |
| 3250 | }) |
| 3251 | } |
| 3252 | } |
| 3253 | |
| 3254 | pub fn output_size(output: &Output) -> Size<i32, Logical> { |
| 3255 | let output_scale = output.current_scale().integer_scale(); |
| 3256 | let output_transform = output.current_transform(); |
| 3257 | let output_mode = output.current_mode().unwrap(); |
| 3258 | |
| 3259 | output_transform |
| 3260 | .transform_size(output_mode.size) |
| 3261 | .to_logical(output_scale) |
| 3262 | } |
| 3263 | |
| 3264 | fn compute_new_view_offset( |
| 3265 | cur_x: i32, |
| 3266 | view_width: i32, |
| 3267 | new_col_x: i32, |
| 3268 | new_col_width: i32, |
| 3269 | gaps: i32, |
| 3270 | ) -> i32 { |
| 3271 | // If the column is wider than the view, always left-align it. |
| 3272 | if view_width <= new_col_width { |
| 3273 | return 0; |
| 3274 | } |
| 3275 | |
| 3276 | // Compute the padding in case it needs to be smaller due to large window width. |
| 3277 | let padding = ((view_width - new_col_width) / 2).clamp(0, gaps); |
| 3278 | |
| 3279 | // Compute the desired new X with padding. |
| 3280 | let new_x = new_col_x - padding; |
| 3281 | let new_right_x = new_col_x + new_col_width + padding; |
| 3282 | |
| 3283 | // If the column is already fully visible, leave the view as is. |
| 3284 | if cur_x <= new_x && new_right_x <= cur_x + view_width { |
| 3285 | return -(new_col_x - cur_x); |
| 3286 | } |
| 3287 | |
| 3288 | // Otherwise, prefer the aligment that results in less motion from the current position. |
| 3289 | let dist_to_left = cur_x.abs_diff(new_x); |
| 3290 | let dist_to_right = (cur_x + view_width).abs_diff(new_right_x); |
| 3291 | if dist_to_left <= dist_to_right { |
| 3292 | -padding |
| 3293 | } else { |
| 3294 | -(view_width - padding - new_col_width) |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | fn prepare_for_output(window: &impl LayoutElement, output: &Output) { |
| 3299 | let scale = output.current_scale().integer_scale(); |
| 3300 | let transform = output.current_transform(); |
| 3301 | window.set_preferred_scale_transform(scale, transform); |
| 3302 | } |
| 3303 | |
| 3304 | #[cfg(test)] |
| 3305 | mod tests { |
| 3306 | use std::cell::Cell; |
| 3307 | use std::rc::Rc; |
| 3308 | |
| 3309 | use proptest::prelude::*; |
| 3310 | use proptest_derive::Arbitrary; |
| 3311 | use smithay::output::{Mode, PhysicalProperties, Subpixel}; |
| 3312 | use smithay::utils::IsAlive; |
| 3313 | |
| 3314 | use super::*; |
| 3315 | |
| 3316 | impl<W: LayoutElement> Default for Layout<W> { |
| 3317 | fn default() -> Self { |
| 3318 | Self { |
| 3319 | monitor_set: MonitorSet::NoOutputs { workspaces: vec![] }, |
| 3320 | options: Rc::new(Options::default()), |
| 3321 | } |
| 3322 | } |
| 3323 | } |
| 3324 | |
| 3325 | #[derive(Debug)] |
| 3326 | struct TestWindowInner { |
| 3327 | id: usize, |
| 3328 | bbox: Cell<Rectangle<i32, Logical>>, |
| 3329 | initial_bbox: Rectangle<i32, Logical>, |
| 3330 | requested_size: Cell<Option<Size<i32, Logical>>>, |
| 3331 | } |
| 3332 | |
| 3333 | #[derive(Debug, Clone)] |
| 3334 | struct TestWindow(Rc<TestWindowInner>); |
| 3335 | |
| 3336 | impl TestWindow { |
| 3337 | fn new(id: usize, bbox: Rectangle<i32, Logical>) -> Self { |
| 3338 | Self(Rc::new(TestWindowInner { |
| 3339 | id, |
| 3340 | bbox: Cell::new(bbox), |
| 3341 | initial_bbox: bbox, |
| 3342 | requested_size: Cell::new(None), |
| 3343 | })) |
| 3344 | } |
| 3345 | |
| 3346 | fn communicate(&self) -> bool { |
| 3347 | if let Some(size) = self.0.requested_size.take() { |
| 3348 | assert!(size.w >= 0); |
| 3349 | assert!(size.h >= 0); |
| 3350 | |
| 3351 | let mut new_bbox = self.0.initial_bbox; |
| 3352 | if size.w != 0 { |
| 3353 | new_bbox.size.w = size.w; |
| 3354 | } |
| 3355 | if size.h != 0 { |
| 3356 | new_bbox.size.h = size.h; |
| 3357 | } |
| 3358 | |
| 3359 | if self.0.bbox.get() != new_bbox { |
| 3360 | self.0.bbox.set(new_bbox); |
| 3361 | return true; |
| 3362 | } |
| 3363 | } |
| 3364 | |
| 3365 | false |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | impl PartialEq for TestWindow { |
| 3370 | fn eq(&self, other: &Self) -> bool { |
| 3371 | self.0.id == other.0.id |
| 3372 | } |
| 3373 | } |
| 3374 | |
| 3375 | impl IsAlive for TestWindow { |
| 3376 | fn alive(&self) -> bool { |
| 3377 | true |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | impl SpaceElement for TestWindow { |
| 3382 | fn bbox(&self) -> Rectangle<i32, Logical> { |
| 3383 | self.0.bbox.get() |
| 3384 | } |
| 3385 | |
| 3386 | fn is_in_input_region(&self, _point: &Point<f64, Logical>) -> bool { |
| 3387 | false |
| 3388 | } |
| 3389 | |
| 3390 | fn set_activate(&self, _activated: bool) {} |
| 3391 | fn output_enter(&self, _output: &Output, _overlap: Rectangle<i32, Logical>) {} |
| 3392 | fn output_leave(&self, _output: &Output) {} |
| 3393 | } |
| 3394 | |
| 3395 | impl LayoutElement for TestWindow { |
| 3396 | fn request_size(&self, size: Size<i32, Logical>) { |
| 3397 | self.0.requested_size.set(Some(size)); |
| 3398 | } |
| 3399 | |
| 3400 | fn request_fullscreen(&self, _size: Size<i32, Logical>) {} |
| 3401 | |
| 3402 | fn min_size(&self) -> Size<i32, Logical> { |
| 3403 | Size::from((0, 0)) |
| 3404 | } |
| 3405 | |
| 3406 | fn max_size(&self) -> Size<i32, Logical> { |
| 3407 | Size::from((0, 0)) |
| 3408 | } |
| 3409 | |
| 3410 | fn is_wl_surface(&self, _wl_surface: &WlSurface) -> bool { |
| 3411 | false |
| 3412 | } |
| 3413 | |
| 3414 | fn set_preferred_scale_transform(&self, _scale: i32, _transform: Transform) {} |
| 3415 | |
| 3416 | fn has_ssd(&self) -> bool { |
| 3417 | false |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | fn arbitrary_bbox() -> impl Strategy<Value = Rectangle<i32, Logical>> { |
| 3422 | any::<(i16, i16, u16, u16)>().prop_map(|(x, y, w, h)| { |
| 3423 | let loc: Point<i32, _> = Point::from((x.into(), y.into())); |
| 3424 | let size: Size<i32, _> = Size::from((w.into(), h.into())); |
| 3425 | Rectangle::from_loc_and_size(loc, size) |
| 3426 | }) |
| 3427 | } |
| 3428 | |
| 3429 | fn arbitrary_size_change() -> impl Strategy<Value = SizeChange> { |
| 3430 | prop_oneof![ |
| 3431 | (0..).prop_map(SizeChange::SetFixed), |
| 3432 | (0f64..).prop_map(SizeChange::SetProportion), |
| 3433 | any::<i32>().prop_map(SizeChange::AdjustFixed), |
| 3434 | any::<f64>().prop_map(SizeChange::AdjustProportion), |
| 3435 | ] |
| 3436 | } |
| 3437 | |
| 3438 | #[derive(Debug, Clone, Copy, Arbitrary)] |
| 3439 | enum Op { |
| 3440 | AddOutput(#[proptest(strategy = "1..=5usize")] usize), |
| 3441 | RemoveOutput(#[proptest(strategy = "1..=5usize")] usize), |
| 3442 | FocusOutput(#[proptest(strategy = "1..=5usize")] usize), |
| 3443 | AddWindow { |
| 3444 | #[proptest(strategy = "1..=5usize")] |
| 3445 | id: usize, |
| 3446 | #[proptest(strategy = "arbitrary_bbox()")] |
| 3447 | bbox: Rectangle<i32, Logical>, |
| 3448 | }, |
| 3449 | CloseWindow(#[proptest(strategy = "1..=5usize")] usize), |
| 3450 | FullscreenWindow(#[proptest(strategy = "1..=5usize")] usize), |
| 3451 | FocusColumnLeft, |
| 3452 | FocusColumnRight, |
| 3453 | FocusWindowDown, |
| 3454 | FocusWindowUp, |
| 3455 | FocusWindowOrWorkspaceDown, |
| 3456 | FocusWindowOrWorkspaceUp, |
| 3457 | MoveColumnLeft, |
| 3458 | MoveColumnRight, |
| 3459 | MoveWindowDown, |
| 3460 | MoveWindowUp, |
| 3461 | MoveWindowDownOrToWorkspaceDown, |
| 3462 | MoveWindowUpOrToWorkspaceUp, |
| 3463 | ConsumeWindowIntoColumn, |
| 3464 | ExpelWindowFromColumn, |
| 3465 | CenterColumn, |
| 3466 | FocusWorkspaceDown, |
| 3467 | FocusWorkspaceUp, |
| 3468 | FocusWorkspace(#[proptest(strategy = "0..=4usize")] usize), |
| 3469 | MoveWindowToWorkspaceDown, |
| 3470 | MoveWindowToWorkspaceUp, |
| 3471 | MoveWindowToWorkspace(#[proptest(strategy = "0..=4usize")] usize), |
| 3472 | MoveWorkspaceDown, |
| 3473 | MoveWorkspaceUp, |
| 3474 | MoveWindowToOutput(#[proptest(strategy = "1..=5u8")] u8), |
| 3475 | SwitchPresetColumnWidth, |
| 3476 | MaximizeColumn, |
| 3477 | SetColumnWidth(#[proptest(strategy = "arbitrary_size_change()")] SizeChange), |
| 3478 | SetWindowHeight(#[proptest(strategy = "arbitrary_size_change()")] SizeChange), |
| 3479 | Communicate(#[proptest(strategy = "1..=5usize")] usize), |
| 3480 | } |
| 3481 | |
| 3482 | impl Op { |
| 3483 | fn apply(self, layout: &mut Layout<TestWindow>) { |
| 3484 | match self { |
| 3485 | Op::AddOutput(id) => { |
| 3486 | let name = format!("output{id}"); |
| 3487 | if layout.outputs().any(|o| o.name() == name) { |
| 3488 | return; |
| 3489 | } |
| 3490 | |
| 3491 | let output = Output::new( |
| 3492 | name, |
| 3493 | PhysicalProperties { |
| 3494 | size: Size::from((1280, 720)), |
| 3495 | subpixel: Subpixel::Unknown, |
| 3496 | make: String::new(), |
| 3497 | model: String::new(), |
| 3498 | }, |
| 3499 | ); |
| 3500 | output.change_current_state( |
| 3501 | Some(Mode { |
| 3502 | size: Size::from((1280, 720)), |
| 3503 | refresh: 60, |
| 3504 | }), |
| 3505 | None, |
| 3506 | None, |
| 3507 | None, |
| 3508 | ); |
| 3509 | layout.add_output(output.clone()); |
| 3510 | } |
| 3511 | Op::RemoveOutput(id) => { |
| 3512 | let name = format!("output{id}"); |
| 3513 | let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else { |
| 3514 | return; |
| 3515 | }; |
| 3516 | |
| 3517 | layout.remove_output(&output); |
| 3518 | } |
| 3519 | Op::FocusOutput(id) => { |
| 3520 | let name = format!("output{id}"); |
| 3521 | let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else { |
| 3522 | return; |
| 3523 | }; |
| 3524 | |
| 3525 | layout.focus_output(&output); |
| 3526 | } |
| 3527 | Op::AddWindow { id, bbox } => { |
| 3528 | match &mut layout.monitor_set { |
| 3529 | MonitorSet::Normal { monitors, .. } => { |
| 3530 | for mon in monitors { |
| 3531 | for ws in &mut mon.workspaces { |
| 3532 | for win in ws.windows() { |
| 3533 | if win.0.id == id { |
| 3534 | return; |
| 3535 | } |
| 3536 | } |
| 3537 | } |
| 3538 | } |
| 3539 | } |
| 3540 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 3541 | for ws in workspaces { |
| 3542 | for win in ws.windows() { |
| 3543 | if win.0.id == id { |
| 3544 | return; |
| 3545 | } |
| 3546 | } |
| 3547 | } |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | let win = TestWindow::new(id, bbox); |
| 3552 | layout.add_window(win, None, false); |
| 3553 | } |
| 3554 | Op::CloseWindow(id) => { |
| 3555 | let dummy = TestWindow::new(id, Rectangle::default()); |
| 3556 | layout.remove_window(&dummy); |
| 3557 | } |
| 3558 | Op::FullscreenWindow(id) => { |
| 3559 | let dummy = TestWindow::new(id, Rectangle::default()); |
| 3560 | layout.toggle_fullscreen(&dummy); |
| 3561 | } |
| 3562 | Op::FocusColumnLeft => layout.focus_left(), |
| 3563 | Op::FocusColumnRight => layout.focus_right(), |
| 3564 | Op::FocusWindowDown => layout.focus_down(), |
| 3565 | Op::FocusWindowUp => layout.focus_up(), |
| 3566 | Op::FocusWindowOrWorkspaceDown => layout.focus_window_or_workspace_down(), |
| 3567 | Op::FocusWindowOrWorkspaceUp => layout.focus_window_or_workspace_up(), |
| 3568 | Op::MoveColumnLeft => layout.move_left(), |
| 3569 | Op::MoveColumnRight => layout.move_right(), |
| 3570 | Op::MoveWindowDown => layout.move_down(), |
| 3571 | Op::MoveWindowUp => layout.move_up(), |
| 3572 | Op::MoveWindowDownOrToWorkspaceDown => layout.move_down_or_to_workspace_down(), |
| 3573 | Op::MoveWindowUpOrToWorkspaceUp => layout.move_up_or_to_workspace_up(), |
| 3574 | Op::ConsumeWindowIntoColumn => layout.consume_into_column(), |
| 3575 | Op::ExpelWindowFromColumn => layout.expel_from_column(), |
| 3576 | Op::CenterColumn => layout.center_column(), |
| 3577 | Op::FocusWorkspaceDown => layout.switch_workspace_down(), |
| 3578 | Op::FocusWorkspaceUp => layout.switch_workspace_up(), |
| 3579 | Op::FocusWorkspace(idx) => layout.switch_workspace(idx), |
| 3580 | Op::MoveWindowToWorkspaceDown => layout.move_to_workspace_down(), |
| 3581 | Op::MoveWindowToWorkspaceUp => layout.move_to_workspace_up(), |
| 3582 | Op::MoveWindowToWorkspace(idx) => layout.move_to_workspace(idx), |
| 3583 | Op::MoveWindowToOutput(id) => { |
| 3584 | let name = format!("output{id}"); |
| 3585 | let Some(output) = layout.outputs().find(|o| o.name() == name).cloned() else { |
| 3586 | return; |
| 3587 | }; |
| 3588 | |
| 3589 | layout.move_to_output(&output); |
| 3590 | } |
| 3591 | Op::MoveWorkspaceDown => layout.move_workspace_down(), |
| 3592 | Op::MoveWorkspaceUp => layout.move_workspace_up(), |
| 3593 | Op::SwitchPresetColumnWidth => layout.toggle_width(), |
| 3594 | Op::MaximizeColumn => layout.toggle_full_width(), |
| 3595 | Op::SetColumnWidth(change) => layout.set_column_width(change), |
| 3596 | Op::SetWindowHeight(change) => layout.set_window_height(change), |
| 3597 | Op::Communicate(id) => { |
| 3598 | let mut window = None; |
| 3599 | match &mut layout.monitor_set { |
| 3600 | MonitorSet::Normal { monitors, .. } => { |
| 3601 | 'outer: for mon in monitors { |
| 3602 | for ws in &mut mon.workspaces { |
| 3603 | for win in ws.windows() { |
| 3604 | if win.0.id == id { |
| 3605 | if win.communicate() { |
| 3606 | window = Some(win.clone()); |
| 3607 | } |
| 3608 | break 'outer; |
| 3609 | } |
| 3610 | } |
| 3611 | } |
| 3612 | } |
| 3613 | } |
| 3614 | MonitorSet::NoOutputs { workspaces, .. } => { |
| 3615 | 'outer: for ws in workspaces { |
| 3616 | for win in ws.windows() { |
| 3617 | if win.0.id == id { |
| 3618 | if win.communicate() { |
| 3619 | window = Some(win.clone()); |
| 3620 | } |
| 3621 | break 'outer; |
| 3622 | } |
| 3623 | } |
| 3624 | } |
| 3625 | } |
| 3626 | } |
| 3627 | |
| 3628 | if let Some(win) = window { |
| 3629 | layout.update_window(&win); |
| 3630 | } |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | #[track_caller] |
| 3637 | fn check_ops(ops: &[Op]) { |
| 3638 | let mut layout = Layout::default(); |
| 3639 | for op in ops { |
| 3640 | op.apply(&mut layout); |
| 3641 | layout.verify_invariants(); |
| 3642 | } |
| 3643 | } |
| 3644 | |
| 3645 | #[test] |
| 3646 | fn operations_dont_panic() { |
| 3647 | let every_op = [ |
| 3648 | Op::AddOutput(0), |
| 3649 | Op::AddOutput(1), |
| 3650 | Op::AddOutput(2), |
| 3651 | Op::RemoveOutput(0), |
| 3652 | Op::RemoveOutput(1), |
| 3653 | Op::RemoveOutput(2), |
| 3654 | Op::FocusOutput(0), |
| 3655 | Op::FocusOutput(1), |
| 3656 | Op::FocusOutput(2), |
| 3657 | Op::AddWindow { |
| 3658 | id: 0, |
| 3659 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3660 | }, |
| 3661 | Op::AddWindow { |
| 3662 | id: 1, |
| 3663 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3664 | }, |
| 3665 | Op::AddWindow { |
| 3666 | id: 2, |
| 3667 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3668 | }, |
| 3669 | Op::CloseWindow(0), |
| 3670 | Op::CloseWindow(1), |
| 3671 | Op::CloseWindow(2), |
| 3672 | Op::FocusColumnLeft, |
| 3673 | Op::FocusColumnRight, |
| 3674 | Op::FocusWindowUp, |
| 3675 | Op::FocusWindowOrWorkspaceUp, |
| 3676 | Op::FocusWindowDown, |
| 3677 | Op::FocusWindowOrWorkspaceDown, |
| 3678 | Op::MoveColumnLeft, |
| 3679 | Op::MoveColumnRight, |
| 3680 | Op::ConsumeWindowIntoColumn, |
| 3681 | Op::ExpelWindowFromColumn, |
| 3682 | Op::CenterColumn, |
| 3683 | Op::FocusWorkspaceDown, |
| 3684 | Op::FocusWorkspaceUp, |
| 3685 | Op::FocusWorkspace(1), |
| 3686 | Op::FocusWorkspace(2), |
| 3687 | Op::FocusWorkspace(3), |
| 3688 | Op::MoveWindowToWorkspaceDown, |
| 3689 | Op::MoveWindowToWorkspaceUp, |
| 3690 | Op::MoveWindowToWorkspace(1), |
| 3691 | Op::MoveWindowToWorkspace(2), |
| 3692 | Op::MoveWindowToWorkspace(3), |
| 3693 | Op::MoveWindowDown, |
| 3694 | Op::MoveWindowDownOrToWorkspaceDown, |
| 3695 | Op::MoveWindowUp, |
| 3696 | Op::MoveWindowUpOrToWorkspaceUp, |
| 3697 | ]; |
| 3698 | |
| 3699 | for third in every_op { |
| 3700 | for second in every_op { |
| 3701 | for first in every_op { |
| 3702 | // eprintln!("{first:?}, {second:?}, {third:?}"); |
| 3703 | |
| 3704 | let mut layout = Layout::default(); |
| 3705 | first.apply(&mut layout); |
| 3706 | layout.verify_invariants(); |
| 3707 | second.apply(&mut layout); |
| 3708 | layout.verify_invariants(); |
| 3709 | third.apply(&mut layout); |
| 3710 | layout.verify_invariants(); |
| 3711 | } |
| 3712 | } |
| 3713 | } |
| 3714 | } |
| 3715 | |
| 3716 | #[test] |
| 3717 | fn operations_from_starting_state_dont_panic() { |
| 3718 | if std::env::var_os("RUN_SLOW_TESTS").is_none() { |
| 3719 | eprintln!("ignoring slow test"); |
| 3720 | return; |
| 3721 | } |
| 3722 | |
| 3723 | // Running every op from an empty state doesn't get us to all the interesting states. So, |
| 3724 | // also run it from a manually-created starting state with more things going on to exercise |
| 3725 | // more code paths. |
| 3726 | let setup_ops = [ |
| 3727 | Op::AddOutput(1), |
| 3728 | Op::AddWindow { |
| 3729 | id: 1, |
| 3730 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3731 | }, |
| 3732 | Op::MoveWindowToWorkspaceDown, |
| 3733 | Op::AddWindow { |
| 3734 | id: 2, |
| 3735 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3736 | }, |
| 3737 | Op::AddWindow { |
| 3738 | id: 3, |
| 3739 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3740 | }, |
| 3741 | Op::FocusColumnLeft, |
| 3742 | Op::ConsumeWindowIntoColumn, |
| 3743 | Op::AddWindow { |
| 3744 | id: 4, |
| 3745 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3746 | }, |
| 3747 | Op::AddOutput(2), |
| 3748 | Op::AddWindow { |
| 3749 | id: 5, |
| 3750 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3751 | }, |
| 3752 | Op::MoveWindowToOutput(2), |
| 3753 | Op::FocusOutput(1), |
| 3754 | Op::Communicate(1), |
| 3755 | Op::Communicate(2), |
| 3756 | Op::Communicate(3), |
| 3757 | Op::Communicate(4), |
| 3758 | Op::Communicate(5), |
| 3759 | ]; |
| 3760 | |
| 3761 | let every_op = [ |
| 3762 | Op::AddOutput(0), |
| 3763 | Op::AddOutput(1), |
| 3764 | Op::AddOutput(2), |
| 3765 | Op::RemoveOutput(0), |
| 3766 | Op::RemoveOutput(1), |
| 3767 | Op::RemoveOutput(2), |
| 3768 | Op::FocusOutput(0), |
| 3769 | Op::FocusOutput(1), |
| 3770 | Op::FocusOutput(2), |
| 3771 | Op::AddWindow { |
| 3772 | id: 0, |
| 3773 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3774 | }, |
| 3775 | Op::AddWindow { |
| 3776 | id: 1, |
| 3777 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3778 | }, |
| 3779 | Op::AddWindow { |
| 3780 | id: 2, |
| 3781 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3782 | }, |
| 3783 | Op::CloseWindow(0), |
| 3784 | Op::CloseWindow(1), |
| 3785 | Op::CloseWindow(2), |
| 3786 | Op::FocusColumnLeft, |
| 3787 | Op::FocusColumnRight, |
| 3788 | Op::FocusWindowUp, |
| 3789 | Op::FocusWindowOrWorkspaceUp, |
| 3790 | Op::FocusWindowDown, |
| 3791 | Op::FocusWindowOrWorkspaceDown, |
| 3792 | Op::MoveColumnLeft, |
| 3793 | Op::MoveColumnRight, |
| 3794 | Op::ConsumeWindowIntoColumn, |
| 3795 | Op::ExpelWindowFromColumn, |
| 3796 | Op::CenterColumn, |
| 3797 | Op::FocusWorkspaceDown, |
| 3798 | Op::FocusWorkspaceUp, |
| 3799 | Op::FocusWorkspace(1), |
| 3800 | Op::FocusWorkspace(2), |
| 3801 | Op::FocusWorkspace(3), |
| 3802 | Op::MoveWindowToWorkspaceDown, |
| 3803 | Op::MoveWindowToWorkspaceUp, |
| 3804 | Op::MoveWindowToWorkspace(1), |
| 3805 | Op::MoveWindowToWorkspace(2), |
| 3806 | Op::MoveWindowToWorkspace(3), |
| 3807 | Op::MoveWindowDown, |
| 3808 | Op::MoveWindowDownOrToWorkspaceDown, |
| 3809 | Op::MoveWindowUp, |
| 3810 | Op::MoveWindowUpOrToWorkspaceUp, |
| 3811 | ]; |
| 3812 | |
| 3813 | for third in every_op { |
| 3814 | for second in every_op { |
| 3815 | for first in every_op { |
| 3816 | // eprintln!("{first:?}, {second:?}, {third:?}"); |
| 3817 | |
| 3818 | let mut layout = Layout::default(); |
| 3819 | for op in setup_ops { |
| 3820 | op.apply(&mut layout); |
| 3821 | } |
| 3822 | |
| 3823 | first.apply(&mut layout); |
| 3824 | layout.verify_invariants(); |
| 3825 | second.apply(&mut layout); |
| 3826 | layout.verify_invariants(); |
| 3827 | third.apply(&mut layout); |
| 3828 | layout.verify_invariants(); |
| 3829 | } |
| 3830 | } |
| 3831 | } |
| 3832 | } |
| 3833 | |
| 3834 | #[test] |
| 3835 | fn primary_active_workspace_idx_not_updated_on_output_add() { |
| 3836 | let ops = [ |
| 3837 | Op::AddOutput(1), |
| 3838 | Op::AddOutput(2), |
| 3839 | Op::FocusOutput(1), |
| 3840 | Op::AddWindow { |
| 3841 | id: 0, |
| 3842 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3843 | }, |
| 3844 | Op::FocusOutput(2), |
| 3845 | Op::AddWindow { |
| 3846 | id: 1, |
| 3847 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3848 | }, |
| 3849 | Op::RemoveOutput(2), |
| 3850 | Op::FocusWorkspace(3), |
| 3851 | Op::AddOutput(2), |
| 3852 | ]; |
| 3853 | |
| 3854 | check_ops(&ops); |
| 3855 | } |
| 3856 | |
| 3857 | #[test] |
| 3858 | fn window_closed_on_previous_workspace() { |
| 3859 | let ops = [ |
| 3860 | Op::AddOutput(1), |
| 3861 | Op::AddWindow { |
| 3862 | id: 0, |
| 3863 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3864 | }, |
| 3865 | Op::FocusWorkspaceDown, |
| 3866 | Op::CloseWindow(0), |
| 3867 | ]; |
| 3868 | |
| 3869 | check_ops(&ops); |
| 3870 | } |
| 3871 | |
| 3872 | #[test] |
| 3873 | fn removing_output_must_keep_empty_focus_on_primary() { |
| 3874 | let ops = [ |
| 3875 | Op::AddOutput(1), |
| 3876 | Op::AddWindow { |
| 3877 | id: 0, |
| 3878 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3879 | }, |
| 3880 | Op::AddOutput(2), |
| 3881 | Op::RemoveOutput(1), |
| 3882 | ]; |
| 3883 | |
| 3884 | let mut layout = Layout::default(); |
| 3885 | for op in ops { |
| 3886 | op.apply(&mut layout); |
| 3887 | } |
| 3888 | |
| 3889 | let MonitorSet::Normal { monitors, .. } = layout.monitor_set else { |
| 3890 | unreachable!() |
| 3891 | }; |
| 3892 | |
| 3893 | // The workspace from the removed output was inserted at position 0, so the active workspace |
| 3894 | // must change to 1 to keep the focus on the empty workspace. |
| 3895 | assert_eq!(monitors[0].active_workspace_idx, 1); |
| 3896 | } |
| 3897 | |
| 3898 | #[test] |
| 3899 | fn move_to_workspace_by_idx_does_not_leave_empty_workspaces() { |
| 3900 | let ops = [ |
| 3901 | Op::AddOutput(1), |
| 3902 | Op::AddWindow { |
| 3903 | id: 0, |
| 3904 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3905 | }, |
| 3906 | Op::MoveWindowToWorkspace(2), |
| 3907 | ]; |
| 3908 | |
| 3909 | let mut layout = Layout::default(); |
| 3910 | for op in ops { |
| 3911 | op.apply(&mut layout); |
| 3912 | } |
| 3913 | |
| 3914 | let MonitorSet::Normal { monitors, .. } = layout.monitor_set else { |
| 3915 | unreachable!() |
| 3916 | }; |
| 3917 | |
| 3918 | assert!(monitors[0].workspaces[0].has_windows()); |
| 3919 | } |
| 3920 | |
| 3921 | #[test] |
| 3922 | fn focus_workspace_by_idx_does_not_leave_empty_workspaces() { |
| 3923 | let ops = [ |
| 3924 | Op::AddOutput(1), |
| 3925 | Op::AddWindow { |
| 3926 | id: 0, |
| 3927 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3928 | }, |
| 3929 | Op::FocusWorkspaceDown, |
| 3930 | Op::AddWindow { |
| 3931 | id: 1, |
| 3932 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3933 | }, |
| 3934 | Op::FocusWorkspaceUp, |
| 3935 | Op::CloseWindow(0), |
| 3936 | Op::FocusWorkspace(3), |
| 3937 | ]; |
| 3938 | |
| 3939 | let mut layout = Layout::default(); |
| 3940 | for op in ops { |
| 3941 | op.apply(&mut layout); |
| 3942 | } |
| 3943 | |
| 3944 | let MonitorSet::Normal { monitors, .. } = layout.monitor_set else { |
| 3945 | unreachable!() |
| 3946 | }; |
| 3947 | |
| 3948 | assert!(monitors[0].workspaces[0].has_windows()); |
| 3949 | } |
| 3950 | |
| 3951 | #[test] |
| 3952 | fn empty_workspaces_dont_move_back_to_original_output() { |
| 3953 | let ops = [ |
| 3954 | Op::AddOutput(1), |
| 3955 | Op::AddWindow { |
| 3956 | id: 1, |
| 3957 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3958 | }, |
| 3959 | Op::FocusWorkspaceDown, |
| 3960 | Op::AddWindow { |
| 3961 | id: 2, |
| 3962 | bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)), |
| 3963 | }, |
| 3964 | Op::AddOutput(2), |
| 3965 | Op::RemoveOutput(1), |
| 3966 | Op::FocusWorkspace(1), |
| 3967 | Op::CloseWindow(1), |
| 3968 | Op::AddOutput(1), |
| 3969 | ]; |
| 3970 | |
| 3971 | check_ops(&ops); |
| 3972 | } |
| 3973 | |
| 3974 | proptest! { |
| 3975 | #![proptest_config(ProptestConfig { |
| 3976 | cases: if std::env::var_os("RUN_SLOW_TESTS").is_none() { |
| 3977 | eprintln!("ignoring slow test"); |
| 3978 | 0 |
| 3979 | } else { |
| 3980 | ProptestConfig::default().cases |
| 3981 | }, |
| 3982 | ..ProptestConfig::default() |
| 3983 | })] |
| 3984 | |
| 3985 | #[test] |
| 3986 | fn random_operations_dont_panic(ops: Vec<Op>) { |
| 3987 | // eprintln!("{ops:?}"); |
| 3988 | check_ops(&ops); |
| 3989 | } |
| 3990 | } |
| 3991 | } |