niri
Clone
https://git.tahinli.com/Tahinli/niri.git
0 watching·0 forks·Rust·GPL-3.0·12.1 MiB·b0eb8ad·Ivan Molodetskikh·3 months ago·2857 commits
| 1 | use std::cmp::max; |
| 2 | use std::iter::zip; |
| 3 | use std::rc::Rc; |
| 4 | |
| 5 | use niri_config::utils::MergeWith as _; |
| 6 | use niri_config::{PresetSize, RelativeTo}; |
| 7 | use niri_ipc::{PositionChange, SizeChange, WindowLayout}; |
| 8 | use smithay::backend::renderer::gles::GlesRenderer; |
| 9 | use smithay::utils::{Logical, Point, Rectangle, Scale, Serial, Size}; |
| 10 | |
| 11 | use super::closing_window::{ClosingWindow, ClosingWindowRenderElement}; |
| 12 | use super::scrolling::ColumnWidth; |
| 13 | use super::tile::{Tile, TileRenderElement, TileRenderSnapshot}; |
| 14 | use super::workspace::{InteractiveResize, ResolvedSize}; |
| 15 | use super::{ |
| 16 | ConfigureIntent, InteractiveResizeData, LayoutElement, Options, RemovedTile, SizeFrac, |
| 17 | }; |
| 18 | use crate::animation::{Animation, Clock}; |
| 19 | use crate::layout::RenderLayer; |
| 20 | use crate::niri_render_elements; |
| 21 | use crate::render_helpers::renderer::NiriRenderer; |
| 22 | use crate::render_helpers::xray::XrayPos; |
| 23 | use crate::render_helpers::RenderCtx; |
| 24 | use crate::utils::transaction::TransactionBlocker; |
| 25 | use crate::utils::{ |
| 26 | center_preferring_top_left_in_area, clamp_preferring_top_left_in_area, ensure_min_max_size, |
| 27 | ensure_min_max_size_maybe_zero, ResizeEdge, |
| 28 | }; |
| 29 | use crate::window::ResolvedWindowRules; |
| 30 | |
| 31 | /// By how many logical pixels the directional move commands move floating windows. |
| 32 | pub const DIRECTIONAL_MOVE_PX: f64 = 50.; |
| 33 | |
| 34 | /// Space for floating windows. |
| 35 | #[derive(Debug)] |
| 36 | pub struct FloatingSpace<W: LayoutElement> { |
| 37 | /// Tiles in top-to-bottom order. |
| 38 | tiles: Vec<Tile<W>>, |
| 39 | |
| 40 | /// Extra per-tile data. |
| 41 | data: Vec<Data>, |
| 42 | |
| 43 | /// Id of the active window. |
| 44 | /// |
| 45 | /// The active window is not necessarily the topmost window. Focus-follows-mouse should |
| 46 | /// activate a window, but not bring it to the top, because that's very annoying. |
| 47 | /// |
| 48 | /// This is always set to `Some()` when `tiles` isn't empty. |
| 49 | active_window_id: Option<W::Id>, |
| 50 | |
| 51 | /// Ongoing interactive resize. |
| 52 | interactive_resize: Option<InteractiveResize<W>>, |
| 53 | |
| 54 | /// Windows in the closing animation. |
| 55 | closing_windows: Vec<ClosingWindow>, |
| 56 | |
| 57 | /// View size for this space. |
| 58 | view_size: Size<f64, Logical>, |
| 59 | |
| 60 | /// Working area for this space. |
| 61 | working_area: Rectangle<f64, Logical>, |
| 62 | |
| 63 | /// Scale of the output the space is on (and rounds its sizes to). |
| 64 | scale: f64, |
| 65 | |
| 66 | /// Clock for driving animations. |
| 67 | clock: Clock, |
| 68 | |
| 69 | /// Configurable properties of the layout. |
| 70 | options: Rc<Options>, |
| 71 | } |
| 72 | |
| 73 | niri_render_elements! { |
| 74 | FloatingSpaceRenderElement<R> => { |
| 75 | Tile = TileRenderElement<R>, |
| 76 | ClosingWindow = ClosingWindowRenderElement, |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /// Extra per-tile data. |
| 81 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 82 | struct Data { |
| 83 | /// Position relative to the working area. |
| 84 | pos: Point<f64, SizeFrac>, |
| 85 | |
| 86 | /// Cached position in logical coordinates. |
| 87 | /// |
| 88 | /// Not rounded to physical pixels. |
| 89 | logical_pos: Point<f64, Logical>, |
| 90 | |
| 91 | /// Cached actual size of the tile. |
| 92 | size: Size<f64, Logical>, |
| 93 | |
| 94 | /// Working area used for conversions. |
| 95 | working_area: Rectangle<f64, Logical>, |
| 96 | } |
| 97 | |
| 98 | impl Data { |
| 99 | pub fn new<W: LayoutElement>( |
| 100 | working_area: Rectangle<f64, Logical>, |
| 101 | tile: &Tile<W>, |
| 102 | logical_pos: Point<f64, Logical>, |
| 103 | ) -> Self { |
| 104 | let mut rv = Self { |
| 105 | pos: Point::default(), |
| 106 | logical_pos: Point::default(), |
| 107 | size: Size::default(), |
| 108 | working_area, |
| 109 | }; |
| 110 | rv.update(tile); |
| 111 | rv.set_logical_pos(logical_pos); |
| 112 | rv |
| 113 | } |
| 114 | |
| 115 | pub fn scale_by_working_area( |
| 116 | area: Rectangle<f64, Logical>, |
| 117 | pos: Point<f64, SizeFrac>, |
| 118 | ) -> Point<f64, Logical> { |
| 119 | let mut logical_pos = Point::from((pos.x, pos.y)); |
| 120 | logical_pos.x *= area.size.w; |
| 121 | logical_pos.y *= area.size.h; |
| 122 | logical_pos += area.loc; |
| 123 | logical_pos |
| 124 | } |
| 125 | |
| 126 | pub fn logical_to_size_frac_in_working_area( |
| 127 | area: Rectangle<f64, Logical>, |
| 128 | logical_pos: Point<f64, Logical>, |
| 129 | ) -> Point<f64, SizeFrac> { |
| 130 | let pos = logical_pos - area.loc; |
| 131 | let mut pos = Point::from((pos.x, pos.y)); |
| 132 | pos.x /= f64::max(area.size.w, 1.0); |
| 133 | pos.y /= f64::max(area.size.h, 1.0); |
| 134 | pos |
| 135 | } |
| 136 | |
| 137 | fn recompute_logical_pos(&mut self) { |
| 138 | let mut logical_pos = Self::scale_by_working_area(self.working_area, self.pos); |
| 139 | |
| 140 | // Make sure the window doesn't go too much off-screen. Numbers taken from Mutter. |
| 141 | let min_on_screen_hor = f64::clamp(self.size.w / 4., 10., 75.); |
| 142 | let min_on_screen_ver = f64::clamp(self.size.h / 4., 10., 75.); |
| 143 | let max_off_screen_hor = f64::max(0., self.size.w - min_on_screen_hor); |
| 144 | let max_off_screen_ver = f64::max(0., self.size.h - min_on_screen_ver); |
| 145 | |
| 146 | logical_pos -= self.working_area.loc; |
| 147 | logical_pos.x = f64::max(logical_pos.x, -max_off_screen_hor); |
| 148 | logical_pos.y = f64::max(logical_pos.y, -max_off_screen_ver); |
| 149 | logical_pos.x = f64::min( |
| 150 | logical_pos.x, |
| 151 | self.working_area.size.w - self.size.w + max_off_screen_hor, |
| 152 | ); |
| 153 | logical_pos.y = f64::min( |
| 154 | logical_pos.y, |
| 155 | self.working_area.size.h - self.size.h + max_off_screen_ver, |
| 156 | ); |
| 157 | logical_pos += self.working_area.loc; |
| 158 | |
| 159 | self.logical_pos = logical_pos; |
| 160 | } |
| 161 | |
| 162 | pub fn update_config(&mut self, working_area: Rectangle<f64, Logical>) { |
| 163 | if self.working_area == working_area { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | self.working_area = working_area; |
| 168 | self.recompute_logical_pos(); |
| 169 | } |
| 170 | |
| 171 | pub fn update<W: LayoutElement>(&mut self, tile: &Tile<W>) { |
| 172 | let size = tile.tile_size(); |
| 173 | if self.size == size { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | self.size = size; |
| 178 | self.recompute_logical_pos(); |
| 179 | } |
| 180 | |
| 181 | pub fn set_logical_pos(&mut self, logical_pos: Point<f64, Logical>) { |
| 182 | self.pos = Self::logical_to_size_frac_in_working_area(self.working_area, logical_pos); |
| 183 | |
| 184 | // This will clamp the logical position to the current working area. |
| 185 | self.recompute_logical_pos(); |
| 186 | } |
| 187 | |
| 188 | pub fn center(&self) -> Point<f64, Logical> { |
| 189 | self.logical_pos + self.size.downscale(2.) |
| 190 | } |
| 191 | |
| 192 | #[cfg(test)] |
| 193 | fn verify_invariants(&self) { |
| 194 | let mut temp = *self; |
| 195 | temp.recompute_logical_pos(); |
| 196 | assert_eq!( |
| 197 | self.logical_pos, temp.logical_pos, |
| 198 | "cached logical pos must be up to date" |
| 199 | ); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | impl<W: LayoutElement> FloatingSpace<W> { |
| 204 | pub fn new( |
| 205 | view_size: Size<f64, Logical>, |
| 206 | working_area: Rectangle<f64, Logical>, |
| 207 | scale: f64, |
| 208 | clock: Clock, |
| 209 | options: Rc<Options>, |
| 210 | ) -> Self { |
| 211 | Self { |
| 212 | tiles: Vec::new(), |
| 213 | data: Vec::new(), |
| 214 | active_window_id: None, |
| 215 | interactive_resize: None, |
| 216 | closing_windows: Vec::new(), |
| 217 | view_size, |
| 218 | working_area, |
| 219 | scale, |
| 220 | clock, |
| 221 | options, |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | pub fn update_config( |
| 226 | &mut self, |
| 227 | view_size: Size<f64, Logical>, |
| 228 | working_area: Rectangle<f64, Logical>, |
| 229 | scale: f64, |
| 230 | options: Rc<Options>, |
| 231 | ) { |
| 232 | for (tile, data) in zip(&mut self.tiles, &mut self.data) { |
| 233 | tile.update_config(view_size, scale, options.clone()); |
| 234 | data.update(tile); |
| 235 | data.update_config(working_area); |
| 236 | } |
| 237 | |
| 238 | self.view_size = view_size; |
| 239 | self.working_area = working_area; |
| 240 | self.scale = scale; |
| 241 | self.options = options; |
| 242 | } |
| 243 | |
| 244 | pub fn update_shaders(&mut self) { |
| 245 | for tile in &mut self.tiles { |
| 246 | tile.update_shaders(); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | pub fn advance_animations(&mut self) { |
| 251 | for tile in &mut self.tiles { |
| 252 | tile.advance_animations(); |
| 253 | } |
| 254 | |
| 255 | self.closing_windows.retain_mut(|closing| { |
| 256 | closing.advance_animations(); |
| 257 | closing.are_animations_ongoing() |
| 258 | }); |
| 259 | } |
| 260 | |
| 261 | pub fn are_animations_ongoing(&self) -> bool { |
| 262 | self.tiles.iter().any(Tile::are_animations_ongoing) || !self.closing_windows.is_empty() |
| 263 | } |
| 264 | |
| 265 | pub fn are_transitions_ongoing(&self) -> bool { |
| 266 | self.tiles.iter().any(Tile::are_transitions_ongoing) || !self.closing_windows.is_empty() |
| 267 | } |
| 268 | |
| 269 | pub fn update_render_elements( |
| 270 | &mut self, |
| 271 | is_active: bool, |
| 272 | view_rect: Rectangle<f64, Logical>, |
| 273 | layer: RenderLayer, |
| 274 | ) { |
| 275 | let active = self.active_window_id.clone(); |
| 276 | for (tile, offset) in self.tiles_with_offsets_mut() { |
| 277 | // Skip tiles belonging to a different render layer. |
| 278 | if layer.is_normal() == tile.is_moving_between_workspaces() { |
| 279 | continue; |
| 280 | } |
| 281 | |
| 282 | let id = tile.window().id(); |
| 283 | let is_active = is_active && Some(id) == active.as_ref(); |
| 284 | |
| 285 | let mut tile_view_rect = view_rect; |
| 286 | tile_view_rect.loc -= offset + tile.render_offset(); |
| 287 | tile.update_render_elements(is_active, tile_view_rect); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | pub fn tiles(&self) -> impl Iterator<Item = &Tile<W>> + '_ { |
| 292 | self.tiles.iter() |
| 293 | } |
| 294 | |
| 295 | pub fn tiles_mut(&mut self) -> impl Iterator<Item = &mut Tile<W>> + '_ { |
| 296 | self.tiles.iter_mut() |
| 297 | } |
| 298 | |
| 299 | pub fn tiles_with_offsets(&self) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>)> + '_ { |
| 300 | let offsets = self.data.iter().map(|d| d.logical_pos); |
| 301 | zip(&self.tiles, offsets) |
| 302 | } |
| 303 | |
| 304 | pub fn tiles_with_offsets_mut( |
| 305 | &mut self, |
| 306 | ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> + '_ { |
| 307 | let offsets = self.data.iter().map(|d| d.logical_pos); |
| 308 | zip(&mut self.tiles, offsets) |
| 309 | } |
| 310 | |
| 311 | pub fn tiles_with_render_positions( |
| 312 | &self, |
| 313 | ) -> impl Iterator<Item = (&Tile<W>, Point<f64, Logical>)> { |
| 314 | let scale = self.scale; |
| 315 | self.tiles_with_offsets().map(move |(tile, offset)| { |
| 316 | let pos = offset + tile.render_offset(); |
| 317 | // Round to physical pixels. |
| 318 | let pos = pos.to_physical_precise_round(scale).to_logical(scale); |
| 319 | (tile, pos) |
| 320 | }) |
| 321 | } |
| 322 | |
| 323 | pub fn tiles_with_render_positions_mut( |
| 324 | &mut self, |
| 325 | round: bool, |
| 326 | ) -> impl Iterator<Item = (&mut Tile<W>, Point<f64, Logical>)> { |
| 327 | let scale = self.scale; |
| 328 | self.tiles_with_offsets_mut().map(move |(tile, offset)| { |
| 329 | let mut pos = offset + tile.render_offset(); |
| 330 | // Round to physical pixels. |
| 331 | if round { |
| 332 | pos = pos.to_physical_precise_round(scale).to_logical(scale); |
| 333 | } |
| 334 | (tile, pos) |
| 335 | }) |
| 336 | } |
| 337 | |
| 338 | pub fn tiles_with_ipc_layouts(&self) -> impl Iterator<Item = (&Tile<W>, WindowLayout)> { |
| 339 | let scale = self.scale; |
| 340 | self.tiles_with_offsets().map(move |(tile, offset)| { |
| 341 | // Do not include animated render offset here to avoid IPC spam. |
| 342 | let pos = offset; |
| 343 | // Round to physical pixels. |
| 344 | let pos = pos.to_physical_precise_round(scale).to_logical(scale); |
| 345 | |
| 346 | let layout = WindowLayout { |
| 347 | tile_pos_in_workspace_view: Some(pos.into()), |
| 348 | ..tile.ipc_layout_template() |
| 349 | }; |
| 350 | (tile, layout) |
| 351 | }) |
| 352 | } |
| 353 | |
| 354 | pub fn new_window_toplevel_bounds(&self, rules: &ResolvedWindowRules) -> Size<i32, Logical> { |
| 355 | let border_config = self.options.layout.border.merged_with(&rules.border); |
| 356 | compute_toplevel_bounds(border_config, self.working_area.size) |
| 357 | } |
| 358 | |
| 359 | /// Returns the geometry of the active window relative to and clamped to the working area. |
| 360 | /// |
| 361 | /// During animations, assumes the final tile position. |
| 362 | pub fn active_window_visual_rectangle(&self) -> Option<Rectangle<f64, Logical>> { |
| 363 | let active_id = self.active_window_id.as_ref()?; |
| 364 | let (tile, offset) = self |
| 365 | .tiles_with_offsets() |
| 366 | .find(|(tile, _)| tile.window().id() == active_id)?; |
| 367 | |
| 368 | let window_pos = offset + tile.window_loc(); |
| 369 | let window_size = tile.window_size(); |
| 370 | let window_rect = Rectangle::new(window_pos, window_size); |
| 371 | |
| 372 | self.working_area.intersection(window_rect) |
| 373 | } |
| 374 | |
| 375 | pub fn popup_target_rect(&self, id: &W::Id) -> Option<Rectangle<f64, Logical>> { |
| 376 | for (tile, pos) in self.tiles_with_offsets() { |
| 377 | if tile.window().id() == id { |
| 378 | // Position within the working area. |
| 379 | let mut target = self.working_area; |
| 380 | target.loc -= pos; |
| 381 | target.loc -= tile.window_loc(); |
| 382 | |
| 383 | return Some(target); |
| 384 | } |
| 385 | } |
| 386 | None |
| 387 | } |
| 388 | |
| 389 | fn idx_of(&self, id: &W::Id) -> Option<usize> { |
| 390 | self.tiles.iter().position(|tile| tile.window().id() == id) |
| 391 | } |
| 392 | |
| 393 | fn contains(&self, id: &W::Id) -> bool { |
| 394 | self.idx_of(id).is_some() |
| 395 | } |
| 396 | |
| 397 | pub fn active_window(&self) -> Option<&W> { |
| 398 | let id = self.active_window_id.as_ref()?; |
| 399 | self.tiles |
| 400 | .iter() |
| 401 | .find(|tile| tile.window().id() == id) |
| 402 | .map(Tile::window) |
| 403 | } |
| 404 | |
| 405 | pub fn active_window_mut(&mut self) -> Option<&mut W> { |
| 406 | let id = self.active_window_id.as_ref()?; |
| 407 | self.tiles |
| 408 | .iter_mut() |
| 409 | .find(|tile| tile.window().id() == id) |
| 410 | .map(Tile::window_mut) |
| 411 | } |
| 412 | |
| 413 | pub fn has_window(&self, id: &W::Id) -> bool { |
| 414 | self.tiles.iter().any(|tile| tile.window().id() == id) |
| 415 | } |
| 416 | |
| 417 | pub fn is_empty(&self) -> bool { |
| 418 | self.tiles.is_empty() |
| 419 | } |
| 420 | |
| 421 | pub fn add_tile(&mut self, tile: Tile<W>, activate: bool) { |
| 422 | self.add_tile_at(0, tile, activate); |
| 423 | } |
| 424 | |
| 425 | fn add_tile_at(&mut self, mut idx: usize, mut tile: Tile<W>, activate: bool) { |
| 426 | tile.update_config(self.view_size, self.scale, self.options.clone()); |
| 427 | |
| 428 | // Restore the previous floating window size, and in case the tile is fullscreen, |
| 429 | // unfullscreen it. |
| 430 | let floating_size = tile.floating_window_size; |
| 431 | let win = tile.window_mut(); |
| 432 | let mut size = if !win.pending_sizing_mode().is_normal() { |
| 433 | // If the window was fullscreen or maximized without a floating size, ask for (0, 0). |
| 434 | floating_size.unwrap_or_default() |
| 435 | } else { |
| 436 | // If the window wasn't fullscreen without a floating size (e.g. it was tiled before), |
| 437 | // ask for the current size. If the current size is unknown (the window was only ever |
| 438 | // fullscreen until now), fall back to (0, 0). |
| 439 | floating_size.unwrap_or_else(|| win.expected_size().unwrap_or_default()) |
| 440 | }; |
| 441 | |
| 442 | // Apply min/max size window rules. If requesting a concrete size, apply completely; if |
| 443 | // requesting (0, 0), apply only when min/max results in a fixed size. |
| 444 | let min_size = win.min_size(); |
| 445 | let max_size = win.max_size(); |
| 446 | size.w = ensure_min_max_size_maybe_zero(size.w, min_size.w, max_size.w); |
| 447 | size.h = ensure_min_max_size_maybe_zero(size.h, min_size.h, max_size.h); |
| 448 | |
| 449 | win.request_size_once(size, true); |
| 450 | |
| 451 | if activate || self.tiles.is_empty() { |
| 452 | self.active_window_id = Some(win.id().clone()); |
| 453 | } |
| 454 | |
| 455 | // Make sure the tile isn't inserted below its parent. |
| 456 | for (i, tile_above) in self.tiles.iter().enumerate().take(idx) { |
| 457 | if win.is_child_of(tile_above.window()) { |
| 458 | idx = i; |
| 459 | break; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | let pos = self.stored_or_default_tile_pos(&tile).unwrap_or_else(|| { |
| 464 | center_preferring_top_left_in_area(self.working_area, tile.tile_size()) |
| 465 | }); |
| 466 | |
| 467 | let data = Data::new(self.working_area, &tile, pos); |
| 468 | self.data.insert(idx, data); |
| 469 | self.tiles.insert(idx, tile); |
| 470 | |
| 471 | self.bring_up_descendants_of(idx); |
| 472 | } |
| 473 | |
| 474 | pub fn add_tile_above(&mut self, above: &W::Id, mut tile: Tile<W>, activate: bool) { |
| 475 | let idx = self.idx_of(above).unwrap(); |
| 476 | |
| 477 | let above_pos = self.data[idx].logical_pos; |
| 478 | let above_size = self.data[idx].size; |
| 479 | let tile_size = tile.tile_size(); |
| 480 | let pos = above_pos + (above_size.to_point() - tile_size.to_point()).downscale(2.); |
| 481 | let pos = self.clamp_within_working_area(pos, tile_size); |
| 482 | tile.floating_pos = Some(self.logical_to_size_frac(pos)); |
| 483 | |
| 484 | self.add_tile_at(idx, tile, activate); |
| 485 | } |
| 486 | |
| 487 | fn bring_up_descendants_of(&mut self, idx: usize) { |
| 488 | let tile = &self.tiles[idx]; |
| 489 | let win = tile.window(); |
| 490 | |
| 491 | // We always maintain the correct stacking order, so walking descendants back to front |
| 492 | // should give us all of them. |
| 493 | let mut descendants: Vec<usize> = Vec::new(); |
| 494 | for (i, tile_below) in self.tiles.iter().enumerate().skip(idx + 1).rev() { |
| 495 | let win_below = tile_below.window(); |
| 496 | if win_below.is_child_of(win) |
| 497 | || descendants |
| 498 | .iter() |
| 499 | .any(|idx| win_below.is_child_of(self.tiles[*idx].window())) |
| 500 | { |
| 501 | descendants.push(i); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Now, descendants is in back-to-front order, and repositioning them in the front-to-back |
| 506 | // order will preserve the subsequent indices and work out right. |
| 507 | let mut idx = idx; |
| 508 | #[allow(clippy::explicit_counter_loop)] |
| 509 | for descendant_idx in descendants.into_iter().rev() { |
| 510 | self.raise_window(descendant_idx, idx); |
| 511 | idx += 1; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | pub fn remove_tile(&mut self, id: &W::Id) -> RemovedTile<W> { |
| 516 | let idx = self.idx_of(id).unwrap(); |
| 517 | self.remove_tile_by_idx(idx) |
| 518 | } |
| 519 | |
| 520 | fn remove_tile_by_idx(&mut self, idx: usize) -> RemovedTile<W> { |
| 521 | let mut tile = self.tiles.remove(idx); |
| 522 | let data = self.data.remove(idx); |
| 523 | |
| 524 | if self.tiles.is_empty() { |
| 525 | self.active_window_id = None; |
| 526 | } else if Some(tile.window().id()) == self.active_window_id.as_ref() { |
| 527 | // The active tile was removed, make the topmost tile active. |
| 528 | self.active_window_id = Some(self.tiles[0].window().id().clone()); |
| 529 | } |
| 530 | |
| 531 | // Stop interactive resize. |
| 532 | if let Some(resize) = &self.interactive_resize { |
| 533 | if tile.window().id() == &resize.window { |
| 534 | self.interactive_resize = None; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // Store the floating size if we have one. |
| 539 | if let Some(size) = tile.window().expected_size() { |
| 540 | tile.floating_window_size = Some(size); |
| 541 | } |
| 542 | // Store the floating position. |
| 543 | tile.floating_pos = Some(data.pos); |
| 544 | |
| 545 | let width = ColumnWidth::Fixed(tile.tile_expected_or_current_size().w); |
| 546 | RemovedTile { |
| 547 | tile, |
| 548 | width, |
| 549 | is_full_width: false, |
| 550 | is_floating: true, |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | pub fn start_close_animation_for_window( |
| 555 | &mut self, |
| 556 | renderer: &mut GlesRenderer, |
| 557 | id: &W::Id, |
| 558 | blocker: TransactionBlocker, |
| 559 | ) { |
| 560 | let (tile, tile_pos) = self |
| 561 | .tiles_with_render_positions_mut(false) |
| 562 | .find(|(tile, _)| tile.window().id() == id) |
| 563 | .unwrap(); |
| 564 | |
| 565 | let Some(snapshot) = tile.take_unmap_snapshot() else { |
| 566 | return; |
| 567 | }; |
| 568 | |
| 569 | let tile_size = tile.tile_size(); |
| 570 | |
| 571 | self.start_close_animation_for_tile(renderer, snapshot, tile_size, tile_pos, blocker); |
| 572 | } |
| 573 | |
| 574 | pub fn activate_window_without_raising(&mut self, id: &W::Id) -> bool { |
| 575 | if !self.contains(id) { |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | self.active_window_id = Some(id.clone()); |
| 580 | true |
| 581 | } |
| 582 | |
| 583 | pub fn activate_window(&mut self, id: &W::Id) -> bool { |
| 584 | let Some(idx) = self.idx_of(id) else { |
| 585 | return false; |
| 586 | }; |
| 587 | |
| 588 | self.raise_window(idx, 0); |
| 589 | self.active_window_id = Some(id.clone()); |
| 590 | self.bring_up_descendants_of(0); |
| 591 | |
| 592 | true |
| 593 | } |
| 594 | |
| 595 | fn raise_window(&mut self, from_idx: usize, to_idx: usize) { |
| 596 | assert!(to_idx <= from_idx); |
| 597 | |
| 598 | let tile = self.tiles.remove(from_idx); |
| 599 | let data = self.data.remove(from_idx); |
| 600 | self.tiles.insert(to_idx, tile); |
| 601 | self.data.insert(to_idx, data); |
| 602 | } |
| 603 | |
| 604 | pub fn start_close_animation_for_tile( |
| 605 | &mut self, |
| 606 | renderer: &mut GlesRenderer, |
| 607 | snapshot: TileRenderSnapshot, |
| 608 | tile_size: Size<f64, Logical>, |
| 609 | tile_pos: Point<f64, Logical>, |
| 610 | blocker: TransactionBlocker, |
| 611 | ) { |
| 612 | let anim = Animation::new( |
| 613 | self.clock.clone(), |
| 614 | 0., |
| 615 | 1., |
| 616 | 0., |
| 617 | self.options.animations.window_close.anim, |
| 618 | ); |
| 619 | |
| 620 | let blocker = if self.options.disable_transactions { |
| 621 | TransactionBlocker::completed() |
| 622 | } else { |
| 623 | blocker |
| 624 | }; |
| 625 | |
| 626 | let scale = Scale::from(self.scale); |
| 627 | let res = ClosingWindow::new( |
| 628 | renderer, snapshot, scale, tile_size, tile_pos, blocker, anim, |
| 629 | ); |
| 630 | match res { |
| 631 | Ok(closing) => { |
| 632 | self.closing_windows.push(closing); |
| 633 | } |
| 634 | Err(err) => { |
| 635 | warn!("error creating a closing window animation: {err:?}"); |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | pub fn toggle_window_width(&mut self, id: Option<&W::Id>, forwards: bool) { |
| 641 | let Some(id) = id.or(self.active_window_id.as_ref()).cloned() else { |
| 642 | return; |
| 643 | }; |
| 644 | let idx = self.idx_of(&id).unwrap(); |
| 645 | |
| 646 | let available_size = self.working_area.size.w; |
| 647 | |
| 648 | let len = self.options.layout.preset_column_widths.len(); |
| 649 | let tile = &mut self.tiles[idx]; |
| 650 | let preset_idx = if let Some(idx) = tile.floating_preset_width_idx { |
| 651 | (idx + if forwards { 1 } else { len - 1 }) % len |
| 652 | } else { |
| 653 | let current_window = tile.window_expected_or_current_size().w; |
| 654 | let current_tile = tile.tile_expected_or_current_size().w; |
| 655 | |
| 656 | let mut it = self |
| 657 | .options |
| 658 | .layout |
| 659 | .preset_column_widths |
| 660 | .iter() |
| 661 | .map(|preset| resolve_preset_size(*preset, available_size)); |
| 662 | |
| 663 | if forwards { |
| 664 | it.position(|resolved| { |
| 665 | match resolved { |
| 666 | // Some allowance for fractional scaling purposes. |
| 667 | ResolvedSize::Tile(resolved) => current_tile + 1. < resolved, |
| 668 | ResolvedSize::Window(resolved) => current_window + 1. < resolved, |
| 669 | } |
| 670 | }) |
| 671 | .unwrap_or(0) |
| 672 | } else { |
| 673 | it.rposition(|resolved| { |
| 674 | match resolved { |
| 675 | // Some allowance for fractional scaling purposes. |
| 676 | ResolvedSize::Tile(resolved) => resolved + 1. < current_tile, |
| 677 | ResolvedSize::Window(resolved) => resolved + 1. < current_window, |
| 678 | } |
| 679 | }) |
| 680 | .unwrap_or(len - 1) |
| 681 | } |
| 682 | }; |
| 683 | |
| 684 | let preset = self.options.layout.preset_column_widths[preset_idx]; |
| 685 | self.set_window_width(Some(&id), SizeChange::from(preset), true); |
| 686 | |
| 687 | self.tiles[idx].floating_preset_width_idx = Some(preset_idx); |
| 688 | |
| 689 | self.interactive_resize_end(Some(&id)); |
| 690 | } |
| 691 | |
| 692 | pub fn start_open_animation(&mut self, id: &W::Id) -> bool { |
| 693 | let Some(idx) = self.idx_of(id) else { |
| 694 | return false; |
| 695 | }; |
| 696 | |
| 697 | self.tiles[idx].start_open_animation(); |
| 698 | true |
| 699 | } |
| 700 | |
| 701 | pub fn toggle_window_height(&mut self, id: Option<&W::Id>, forwards: bool) { |
| 702 | let Some(id) = id.or(self.active_window_id.as_ref()).cloned() else { |
| 703 | return; |
| 704 | }; |
| 705 | let idx = self.idx_of(&id).unwrap(); |
| 706 | |
| 707 | let available_size = self.working_area.size.h; |
| 708 | |
| 709 | let len = self.options.layout.preset_window_heights.len(); |
| 710 | let tile = &mut self.tiles[idx]; |
| 711 | let preset_idx = if let Some(idx) = tile.floating_preset_height_idx { |
| 712 | (idx + if forwards { 1 } else { len - 1 }) % len |
| 713 | } else { |
| 714 | let current_window = tile.window_expected_or_current_size().h; |
| 715 | let current_tile = tile.tile_expected_or_current_size().h; |
| 716 | |
| 717 | let mut it = self |
| 718 | .options |
| 719 | .layout |
| 720 | .preset_window_heights |
| 721 | .iter() |
| 722 | .map(|preset| resolve_preset_size(*preset, available_size)); |
| 723 | |
| 724 | if forwards { |
| 725 | it.position(|resolved| { |
| 726 | match resolved { |
| 727 | // Some allowance for fractional scaling purposes. |
| 728 | ResolvedSize::Tile(resolved) => current_tile + 1. < resolved, |
| 729 | ResolvedSize::Window(resolved) => current_window + 1. < resolved, |
| 730 | } |
| 731 | }) |
| 732 | .unwrap_or(0) |
| 733 | } else { |
| 734 | it.rposition(|resolved| { |
| 735 | match resolved { |
| 736 | // Some allowance for fractional scaling purposes. |
| 737 | ResolvedSize::Tile(resolved) => resolved + 1. < current_tile, |
| 738 | ResolvedSize::Window(resolved) => resolved + 1. < current_window, |
| 739 | } |
| 740 | }) |
| 741 | .unwrap_or(len - 1) |
| 742 | } |
| 743 | }; |
| 744 | |
| 745 | let preset = self.options.layout.preset_window_heights[preset_idx]; |
| 746 | self.set_window_height(Some(&id), SizeChange::from(preset), true); |
| 747 | |
| 748 | let tile = &mut self.tiles[idx]; |
| 749 | tile.floating_preset_height_idx = Some(preset_idx); |
| 750 | |
| 751 | self.interactive_resize_end(Some(&id)); |
| 752 | } |
| 753 | |
| 754 | pub fn set_window_width(&mut self, id: Option<&W::Id>, change: SizeChange, animate: bool) { |
| 755 | let Some(id) = id.or(self.active_window_id.as_ref()) else { |
| 756 | return; |
| 757 | }; |
| 758 | let idx = self.idx_of(id).unwrap(); |
| 759 | |
| 760 | let tile = &mut self.tiles[idx]; |
| 761 | tile.floating_preset_width_idx = None; |
| 762 | |
| 763 | let available_size = self.working_area.size.w; |
| 764 | let win = tile.window(); |
| 765 | let current_window = win.expected_size().unwrap_or_else(|| win.size()).w; |
| 766 | let current_tile = tile.tile_expected_or_current_size().w; |
| 767 | |
| 768 | const MAX_PX: f64 = 100000.; |
| 769 | const MAX_F: f64 = 10000.; |
| 770 | |
| 771 | let win_width = match change { |
| 772 | SizeChange::SetFixed(win_width) => f64::from(win_width), |
| 773 | SizeChange::SetProportion(prop) => { |
| 774 | let prop = (prop / 100.).clamp(0., MAX_F); |
| 775 | let tile_width = available_size * prop; |
| 776 | tile.window_width_for_tile_width(tile_width) |
| 777 | } |
| 778 | SizeChange::AdjustFixed(delta) => f64::from(current_window.saturating_add(delta)), |
| 779 | SizeChange::AdjustProportion(delta) => { |
| 780 | let current_prop = current_tile / available_size; |
| 781 | let prop = (current_prop + delta / 100.).clamp(0., MAX_F); |
| 782 | let tile_width = available_size * prop; |
| 783 | tile.window_width_for_tile_width(tile_width) |
| 784 | } |
| 785 | }; |
| 786 | let win_width = win_width.round().clamp(1., MAX_PX) as i32; |
| 787 | |
| 788 | let win = tile.window_mut(); |
| 789 | let min_size = win.min_size(); |
| 790 | let max_size = win.max_size(); |
| 791 | |
| 792 | let win_width = ensure_min_max_size(win_width, min_size.w, max_size.w); |
| 793 | |
| 794 | let win_height = win.expected_size().unwrap_or_default().h; |
| 795 | let win_height = ensure_min_max_size(win_height, min_size.h, max_size.h); |
| 796 | |
| 797 | let win_size = Size::from((win_width, win_height)); |
| 798 | win.request_size_once(win_size, animate); |
| 799 | } |
| 800 | |
| 801 | pub fn set_window_height(&mut self, id: Option<&W::Id>, change: SizeChange, animate: bool) { |
| 802 | let Some(id) = id.or(self.active_window_id.as_ref()) else { |
| 803 | return; |
| 804 | }; |
| 805 | let idx = self.idx_of(id).unwrap(); |
| 806 | |
| 807 | let tile = &mut self.tiles[idx]; |
| 808 | tile.floating_preset_height_idx = None; |
| 809 | |
| 810 | let available_size = self.working_area.size.h; |
| 811 | let win = tile.window(); |
| 812 | let current_window = win.expected_size().unwrap_or_else(|| win.size()).h; |
| 813 | let current_tile = tile.tile_expected_or_current_size().h; |
| 814 | |
| 815 | const MAX_PX: f64 = 100000.; |
| 816 | const MAX_F: f64 = 10000.; |
| 817 | |
| 818 | let win_height = match change { |
| 819 | SizeChange::SetFixed(win_height) => f64::from(win_height), |
| 820 | SizeChange::SetProportion(prop) => { |
| 821 | let prop = (prop / 100.).clamp(0., MAX_F); |
| 822 | let tile_height = available_size * prop; |
| 823 | tile.window_height_for_tile_height(tile_height) |
| 824 | } |
| 825 | SizeChange::AdjustFixed(delta) => f64::from(current_window.saturating_add(delta)), |
| 826 | SizeChange::AdjustProportion(delta) => { |
| 827 | let current_prop = current_tile / available_size; |
| 828 | let prop = (current_prop + delta / 100.).clamp(0., MAX_F); |
| 829 | let tile_height = available_size * prop; |
| 830 | tile.window_height_for_tile_height(tile_height) |
| 831 | } |
| 832 | }; |
| 833 | let win_height = win_height.round().clamp(1., MAX_PX) as i32; |
| 834 | |
| 835 | let win = tile.window_mut(); |
| 836 | let min_size = win.min_size(); |
| 837 | let max_size = win.max_size(); |
| 838 | |
| 839 | let win_height = ensure_min_max_size(win_height, min_size.h, max_size.h); |
| 840 | |
| 841 | let win_width = win.expected_size().unwrap_or_default().w; |
| 842 | let win_width = ensure_min_max_size(win_width, min_size.w, max_size.w); |
| 843 | |
| 844 | let win_size = Size::from((win_width, win_height)); |
| 845 | win.request_size_once(win_size, animate); |
| 846 | } |
| 847 | |
| 848 | fn focus_directional( |
| 849 | &mut self, |
| 850 | distance: impl Fn(Point<f64, Logical>, Point<f64, Logical>) -> f64, |
| 851 | ) -> bool { |
| 852 | let Some(active_id) = &self.active_window_id else { |
| 853 | return false; |
| 854 | }; |
| 855 | let active_idx = self.idx_of(active_id).unwrap(); |
| 856 | let center = self.data[active_idx].center(); |
| 857 | |
| 858 | let result = zip(&self.tiles, &self.data) |
| 859 | .filter(|(tile, _)| tile.window().id() != active_id) |
| 860 | .map(|(tile, data)| (tile, distance(center, data.center()))) |
| 861 | .filter(|(_, dist)| *dist > 0.) |
| 862 | .min_by(|(_, dist_a), (_, dist_b)| f64::total_cmp(dist_a, dist_b)); |
| 863 | if let Some((tile, _)) = result { |
| 864 | let id = tile.window().id().clone(); |
| 865 | self.activate_window(&id); |
| 866 | true |
| 867 | } else { |
| 868 | false |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | pub fn focus_left(&mut self) -> bool { |
| 873 | self.focus_directional(|focus, other| focus.x - other.x) |
| 874 | } |
| 875 | |
| 876 | pub fn focus_right(&mut self) -> bool { |
| 877 | self.focus_directional(|focus, other| other.x - focus.x) |
| 878 | } |
| 879 | |
| 880 | pub fn focus_up(&mut self) -> bool { |
| 881 | self.focus_directional(|focus, other| focus.y - other.y) |
| 882 | } |
| 883 | |
| 884 | pub fn focus_down(&mut self) -> bool { |
| 885 | self.focus_directional(|focus, other| other.y - focus.y) |
| 886 | } |
| 887 | |
| 888 | pub fn focus_leftmost(&mut self) { |
| 889 | let result = self |
| 890 | .tiles_with_offsets() |
| 891 | .min_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.x, &pos_b.x)); |
| 892 | if let Some((tile, _)) = result { |
| 893 | let id = tile.window().id().clone(); |
| 894 | self.activate_window(&id); |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | pub fn focus_rightmost(&mut self) { |
| 899 | let result = self |
| 900 | .tiles_with_offsets() |
| 901 | .max_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.x, &pos_b.x)); |
| 902 | if let Some((tile, _)) = result { |
| 903 | let id = tile.window().id().clone(); |
| 904 | self.activate_window(&id); |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | pub fn focus_topmost(&mut self) { |
| 909 | let result = self |
| 910 | .tiles_with_offsets() |
| 911 | .min_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.y, &pos_b.y)); |
| 912 | if let Some((tile, _)) = result { |
| 913 | let id = tile.window().id().clone(); |
| 914 | self.activate_window(&id); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | pub fn focus_bottommost(&mut self) { |
| 919 | let result = self |
| 920 | .tiles_with_offsets() |
| 921 | .max_by(|(_, pos_a), (_, pos_b)| f64::total_cmp(&pos_a.y, &pos_b.y)); |
| 922 | if let Some((tile, _)) = result { |
| 923 | let id = tile.window().id().clone(); |
| 924 | self.activate_window(&id); |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | fn move_to(&mut self, idx: usize, new_pos: Point<f64, Logical>, animate: bool) { |
| 929 | if animate { |
| 930 | self.move_and_animate(idx, new_pos); |
| 931 | } else { |
| 932 | self.data[idx].set_logical_pos(new_pos); |
| 933 | } |
| 934 | |
| 935 | self.interactive_resize_end(None); |
| 936 | } |
| 937 | |
| 938 | fn move_by(&mut self, amount: Point<f64, Logical>) { |
| 939 | let Some(active_id) = &self.active_window_id else { |
| 940 | return; |
| 941 | }; |
| 942 | let idx = self.idx_of(active_id).unwrap(); |
| 943 | |
| 944 | let new_pos = self.data[idx].logical_pos + amount; |
| 945 | self.move_to(idx, new_pos, true) |
| 946 | } |
| 947 | |
| 948 | pub fn move_left(&mut self) { |
| 949 | self.move_by(Point::from((-DIRECTIONAL_MOVE_PX, 0.))); |
| 950 | } |
| 951 | |
| 952 | pub fn move_right(&mut self) { |
| 953 | self.move_by(Point::from((DIRECTIONAL_MOVE_PX, 0.))); |
| 954 | } |
| 955 | |
| 956 | pub fn move_up(&mut self) { |
| 957 | self.move_by(Point::from((0., -DIRECTIONAL_MOVE_PX))); |
| 958 | } |
| 959 | |
| 960 | pub fn move_down(&mut self) { |
| 961 | self.move_by(Point::from((0., DIRECTIONAL_MOVE_PX))); |
| 962 | } |
| 963 | |
| 964 | pub fn move_window( |
| 965 | &mut self, |
| 966 | id: Option<&W::Id>, |
| 967 | x: PositionChange, |
| 968 | y: PositionChange, |
| 969 | animate: bool, |
| 970 | ) { |
| 971 | let Some(id) = id.or(self.active_window_id.as_ref()) else { |
| 972 | return; |
| 973 | }; |
| 974 | let idx = self.idx_of(id).unwrap(); |
| 975 | |
| 976 | let mut pos = self.data[idx].logical_pos; |
| 977 | |
| 978 | let available_width = self.working_area.size.w; |
| 979 | let available_height = self.working_area.size.h; |
| 980 | let working_area_loc = self.working_area.loc; |
| 981 | |
| 982 | const MAX_F: f64 = 10000.; |
| 983 | |
| 984 | match x { |
| 985 | PositionChange::SetFixed(x) => pos.x = x + working_area_loc.x, |
| 986 | PositionChange::SetProportion(prop) => { |
| 987 | let prop = (prop / 100.).clamp(0., MAX_F); |
| 988 | pos.x = available_width * prop + working_area_loc.x; |
| 989 | } |
| 990 | PositionChange::AdjustFixed(x) => pos.x += x, |
| 991 | PositionChange::AdjustProportion(prop) => { |
| 992 | let current_prop = (pos.x - working_area_loc.x) / available_width.max(1.); |
| 993 | let prop = (current_prop + prop / 100.).clamp(0., MAX_F); |
| 994 | pos.x = available_width * prop + working_area_loc.x; |
| 995 | } |
| 996 | } |
| 997 | match y { |
| 998 | PositionChange::SetFixed(y) => pos.y = y + working_area_loc.y, |
| 999 | PositionChange::SetProportion(prop) => { |
| 1000 | let prop = (prop / 100.).clamp(0., MAX_F); |
| 1001 | pos.y = available_height * prop + working_area_loc.y; |
| 1002 | } |
| 1003 | PositionChange::AdjustFixed(y) => pos.y += y, |
| 1004 | PositionChange::AdjustProportion(prop) => { |
| 1005 | let current_prop = (pos.y - working_area_loc.y) / available_height.max(1.); |
| 1006 | let prop = (current_prop + prop / 100.).clamp(0., MAX_F); |
| 1007 | pos.y = available_height * prop + working_area_loc.y; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | self.move_to(idx, pos, animate); |
| 1012 | } |
| 1013 | |
| 1014 | pub fn center_window(&mut self, id: Option<&W::Id>) { |
| 1015 | let Some(id) = id.or(self.active_window_id.as_ref()).cloned() else { |
| 1016 | return; |
| 1017 | }; |
| 1018 | let idx = self.idx_of(&id).unwrap(); |
| 1019 | |
| 1020 | let new_pos = center_preferring_top_left_in_area(self.working_area, self.data[idx].size); |
| 1021 | self.move_to(idx, new_pos, true); |
| 1022 | } |
| 1023 | |
| 1024 | pub fn descendants_added(&mut self, id: &W::Id) -> bool { |
| 1025 | let Some(idx) = self.idx_of(id) else { |
| 1026 | return false; |
| 1027 | }; |
| 1028 | |
| 1029 | self.bring_up_descendants_of(idx); |
| 1030 | true |
| 1031 | } |
| 1032 | |
| 1033 | pub fn update_window(&mut self, id: &W::Id, serial: Option<Serial>) -> bool { |
| 1034 | let Some(tile_idx) = self.idx_of(id) else { |
| 1035 | return false; |
| 1036 | }; |
| 1037 | |
| 1038 | let tile = &mut self.tiles[tile_idx]; |
| 1039 | let data = &mut self.data[tile_idx]; |
| 1040 | |
| 1041 | let resize = tile.window_mut().interactive_resize_data(); |
| 1042 | |
| 1043 | // Do this before calling update_window() so it can get up-to-date info. |
| 1044 | if let Some(serial) = serial { |
| 1045 | tile.window_mut().on_commit(serial); |
| 1046 | } |
| 1047 | |
| 1048 | let prev_size = data.size; |
| 1049 | |
| 1050 | tile.update_window(); |
| 1051 | data.update(tile); |
| 1052 | |
| 1053 | // When resizing by top/left edge, update the position accordingly. |
| 1054 | if let Some(resize) = resize { |
| 1055 | let mut offset = Point::from((0., 0.)); |
| 1056 | if resize.edges.contains(ResizeEdge::LEFT) { |
| 1057 | offset.x += prev_size.w - data.size.w; |
| 1058 | } |
| 1059 | if resize.edges.contains(ResizeEdge::TOP) { |
| 1060 | offset.y += prev_size.h - data.size.h; |
| 1061 | } |
| 1062 | data.set_logical_pos(data.logical_pos + offset); |
| 1063 | } |
| 1064 | |
| 1065 | true |
| 1066 | } |
| 1067 | |
| 1068 | pub fn render<R: NiriRenderer>( |
| 1069 | &self, |
| 1070 | mut ctx: RenderCtx<R>, |
| 1071 | xray_pos: XrayPos, |
| 1072 | view_rect: Rectangle<f64, Logical>, |
| 1073 | focus_ring: bool, |
| 1074 | layer: RenderLayer, |
| 1075 | push: &mut dyn FnMut(FloatingSpaceRenderElement<R>), |
| 1076 | ) { |
| 1077 | let scale = Scale::from(self.scale); |
| 1078 | |
| 1079 | // Draw the closing windows on top of the other windows. |
| 1080 | // |
| 1081 | // FIXME: I guess this should rather preserve the stacking order when the window is closed. |
| 1082 | if layer.is_normal() { |
| 1083 | for closing in self.closing_windows.iter().rev() { |
| 1084 | let elem = closing.render(ctx.as_gles(), view_rect, scale); |
| 1085 | push(elem.into()); |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | let active = self.active_window_id.clone(); |
| 1090 | for (tile, tile_pos) in self.tiles_with_render_positions() { |
| 1091 | // Skip tiles belonging to a different render layer. |
| 1092 | if layer.is_normal() == tile.is_moving_between_workspaces() { |
| 1093 | continue; |
| 1094 | } |
| 1095 | |
| 1096 | // For the active tile, draw the focus ring. |
| 1097 | let focus_ring = focus_ring && Some(tile.window().id()) == active.as_ref(); |
| 1098 | |
| 1099 | let xray_pos = xray_pos.offset(tile_pos); |
| 1100 | tile.render(ctx.r(), tile_pos, xray_pos, focus_ring, &mut |elem| { |
| 1101 | push(elem.into()) |
| 1102 | }); |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | pub fn interactive_resize_begin(&mut self, window: W::Id, edges: ResizeEdge) -> bool { |
| 1107 | if self.interactive_resize.is_some() { |
| 1108 | return false; |
| 1109 | } |
| 1110 | |
| 1111 | let tile = self |
| 1112 | .tiles |
| 1113 | .iter_mut() |
| 1114 | .find(|tile| tile.window().id() == &window) |
| 1115 | .unwrap(); |
| 1116 | |
| 1117 | let original_window_size = tile.window_size(); |
| 1118 | |
| 1119 | let resize = InteractiveResize { |
| 1120 | window, |
| 1121 | original_window_size, |
| 1122 | data: InteractiveResizeData { edges }, |
| 1123 | }; |
| 1124 | self.interactive_resize = Some(resize); |
| 1125 | |
| 1126 | true |
| 1127 | } |
| 1128 | |
| 1129 | pub fn interactive_resize_update( |
| 1130 | &mut self, |
| 1131 | window: &W::Id, |
| 1132 | delta: Point<f64, Logical>, |
| 1133 | ) -> bool { |
| 1134 | let Some(resize) = &self.interactive_resize else { |
| 1135 | return false; |
| 1136 | }; |
| 1137 | |
| 1138 | if window != &resize.window { |
| 1139 | return false; |
| 1140 | } |
| 1141 | |
| 1142 | let original_window_size = resize.original_window_size; |
| 1143 | let edges = resize.data.edges; |
| 1144 | |
| 1145 | if edges.intersects(ResizeEdge::LEFT_RIGHT) { |
| 1146 | let mut dx = delta.x; |
| 1147 | if edges.contains(ResizeEdge::LEFT) { |
| 1148 | dx = -dx; |
| 1149 | }; |
| 1150 | |
| 1151 | let window_width = (original_window_size.w + dx).round() as i32; |
| 1152 | self.set_window_width(Some(window), SizeChange::SetFixed(window_width), false); |
| 1153 | } |
| 1154 | |
| 1155 | if edges.intersects(ResizeEdge::TOP_BOTTOM) { |
| 1156 | let mut dy = delta.y; |
| 1157 | if edges.contains(ResizeEdge::TOP) { |
| 1158 | dy = -dy; |
| 1159 | }; |
| 1160 | |
| 1161 | let window_height = (original_window_size.h + dy).round() as i32; |
| 1162 | self.set_window_height(Some(window), SizeChange::SetFixed(window_height), false); |
| 1163 | } |
| 1164 | |
| 1165 | true |
| 1166 | } |
| 1167 | |
| 1168 | pub fn interactive_resize_end(&mut self, window: Option<&W::Id>) { |
| 1169 | let Some(resize) = &self.interactive_resize else { |
| 1170 | return; |
| 1171 | }; |
| 1172 | |
| 1173 | if let Some(window) = window { |
| 1174 | if window != &resize.window { |
| 1175 | return; |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | self.interactive_resize = None; |
| 1180 | } |
| 1181 | |
| 1182 | pub fn refresh(&mut self, is_active: bool, is_focused: bool) { |
| 1183 | let active = self.active_window_id.clone(); |
| 1184 | for tile in &mut self.tiles { |
| 1185 | let win = tile.window_mut(); |
| 1186 | |
| 1187 | win.set_active_in_column(true); |
| 1188 | win.set_floating(true); |
| 1189 | |
| 1190 | let mut is_active = is_active && Some(win.id()) == active.as_ref(); |
| 1191 | if self.options.deactivate_unfocused_windows { |
| 1192 | is_active &= is_focused; |
| 1193 | } |
| 1194 | win.set_activated(is_active); |
| 1195 | |
| 1196 | let resize_data = self |
| 1197 | .interactive_resize |
| 1198 | .as_ref() |
| 1199 | .filter(|resize| &resize.window == win.id()) |
| 1200 | .map(|resize| resize.data); |
| 1201 | win.set_interactive_resize(resize_data); |
| 1202 | |
| 1203 | let border_config = self.options.layout.border.merged_with(&win.rules().border); |
| 1204 | let bounds = compute_toplevel_bounds(border_config, self.working_area.size); |
| 1205 | win.set_bounds(bounds); |
| 1206 | |
| 1207 | // If transactions are disabled, also disable combined throttling, for more |
| 1208 | // intuitive behavior. |
| 1209 | let intent = if self.options.disable_resize_throttling { |
| 1210 | ConfigureIntent::CanSend |
| 1211 | } else { |
| 1212 | win.configure_intent() |
| 1213 | }; |
| 1214 | |
| 1215 | if matches!( |
| 1216 | intent, |
| 1217 | ConfigureIntent::CanSend | ConfigureIntent::ShouldSend |
| 1218 | ) { |
| 1219 | win.send_pending_configure(); |
| 1220 | } |
| 1221 | |
| 1222 | win.refresh(); |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | pub fn clamp_within_working_area( |
| 1227 | &self, |
| 1228 | pos: Point<f64, Logical>, |
| 1229 | size: Size<f64, Logical>, |
| 1230 | ) -> Point<f64, Logical> { |
| 1231 | let mut rect = Rectangle::new(pos, size); |
| 1232 | clamp_preferring_top_left_in_area(self.working_area, &mut rect); |
| 1233 | rect.loc |
| 1234 | } |
| 1235 | |
| 1236 | pub fn scale_by_working_area(&self, pos: Point<f64, SizeFrac>) -> Point<f64, Logical> { |
| 1237 | Data::scale_by_working_area(self.working_area, pos) |
| 1238 | } |
| 1239 | |
| 1240 | pub fn logical_to_size_frac(&self, logical_pos: Point<f64, Logical>) -> Point<f64, SizeFrac> { |
| 1241 | Data::logical_to_size_frac_in_working_area(self.working_area, logical_pos) |
| 1242 | } |
| 1243 | |
| 1244 | fn move_and_animate(&mut self, idx: usize, new_pos: Point<f64, Logical>) { |
| 1245 | // Moves up to this logical pixel distance are not animated. |
| 1246 | const ANIMATION_THRESHOLD_SQ: f64 = 10. * 10.; |
| 1247 | |
| 1248 | let tile = &mut self.tiles[idx]; |
| 1249 | let data = &mut self.data[idx]; |
| 1250 | |
| 1251 | let prev_pos = data.logical_pos; |
| 1252 | data.set_logical_pos(new_pos); |
| 1253 | let new_pos = data.logical_pos; |
| 1254 | |
| 1255 | let diff = prev_pos - new_pos; |
| 1256 | if diff.x * diff.x + diff.y * diff.y > ANIMATION_THRESHOLD_SQ { |
| 1257 | tile.animate_move_from(prev_pos - new_pos); |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | pub fn new_window_size( |
| 1262 | &self, |
| 1263 | width: Option<PresetSize>, |
| 1264 | height: Option<PresetSize>, |
| 1265 | rules: &ResolvedWindowRules, |
| 1266 | ) -> Size<i32, Logical> { |
| 1267 | let border = self.options.layout.border.merged_with(&rules.border); |
| 1268 | |
| 1269 | let resolve = |size: Option<PresetSize>, working_area_size: f64| { |
| 1270 | if let Some(size) = size { |
| 1271 | let size = match resolve_preset_size(size, working_area_size) { |
| 1272 | ResolvedSize::Tile(mut size) => { |
| 1273 | if !border.off { |
| 1274 | size -= border.width * 2.; |
| 1275 | } |
| 1276 | size |
| 1277 | } |
| 1278 | ResolvedSize::Window(size) => size, |
| 1279 | }; |
| 1280 | |
| 1281 | max(1, size.floor() as i32) |
| 1282 | } else { |
| 1283 | 0 |
| 1284 | } |
| 1285 | }; |
| 1286 | |
| 1287 | let width = resolve(width, self.working_area.size.w); |
| 1288 | let height = resolve(height, self.working_area.size.h); |
| 1289 | |
| 1290 | Size::from((width, height)) |
| 1291 | } |
| 1292 | |
| 1293 | pub fn stored_or_default_tile_pos(&self, tile: &Tile<W>) -> Option<Point<f64, Logical>> { |
| 1294 | let pos = tile.floating_pos.map(|pos| self.scale_by_working_area(pos)); |
| 1295 | pos.or_else(|| { |
| 1296 | tile.window().rules().default_floating_position.map(|pos| { |
| 1297 | let relative_to = pos.relative_to; |
| 1298 | let size = tile.tile_size(); |
| 1299 | let area = self.working_area; |
| 1300 | |
| 1301 | let mut pos = Point::from((pos.x.0, pos.y.0)); |
| 1302 | if relative_to == RelativeTo::TopRight |
| 1303 | || relative_to == RelativeTo::BottomRight |
| 1304 | || relative_to == RelativeTo::Right |
| 1305 | { |
| 1306 | pos.x = area.size.w - size.w - pos.x; |
| 1307 | } |
| 1308 | if relative_to == RelativeTo::BottomLeft |
| 1309 | || relative_to == RelativeTo::BottomRight |
| 1310 | || relative_to == RelativeTo::Bottom |
| 1311 | { |
| 1312 | pos.y = area.size.h - size.h - pos.y; |
| 1313 | } |
| 1314 | if relative_to == RelativeTo::Top || relative_to == RelativeTo::Bottom { |
| 1315 | pos.x += area.size.w / 2.0 - size.w / 2.0 |
| 1316 | } |
| 1317 | if relative_to == RelativeTo::Left || relative_to == RelativeTo::Right { |
| 1318 | pos.y += area.size.h / 2.0 - size.h / 2.0 |
| 1319 | } |
| 1320 | |
| 1321 | pos + self.working_area.loc |
| 1322 | }) |
| 1323 | }) |
| 1324 | } |
| 1325 | |
| 1326 | #[cfg(test)] |
| 1327 | pub fn view_size(&self) -> Size<f64, Logical> { |
| 1328 | self.view_size |
| 1329 | } |
| 1330 | |
| 1331 | pub fn working_area(&self) -> Rectangle<f64, Logical> { |
| 1332 | self.working_area |
| 1333 | } |
| 1334 | |
| 1335 | #[cfg(test)] |
| 1336 | pub fn scale(&self) -> f64 { |
| 1337 | self.scale |
| 1338 | } |
| 1339 | |
| 1340 | #[cfg(test)] |
| 1341 | pub fn clock(&self) -> &Clock { |
| 1342 | &self.clock |
| 1343 | } |
| 1344 | |
| 1345 | #[cfg(test)] |
| 1346 | pub fn options(&self) -> &Rc<Options> { |
| 1347 | &self.options |
| 1348 | } |
| 1349 | |
| 1350 | #[cfg(test)] |
| 1351 | pub fn verify_invariants(&self) { |
| 1352 | assert!(self.scale > 0.); |
| 1353 | assert!(self.scale.is_finite()); |
| 1354 | assert_eq!(self.tiles.len(), self.data.len()); |
| 1355 | |
| 1356 | for (i, (tile, data)) in zip(&self.tiles, &self.data).enumerate() { |
| 1357 | use crate::layout::SizingMode; |
| 1358 | |
| 1359 | assert!(Rc::ptr_eq(&self.options, &tile.options)); |
| 1360 | assert_eq!(self.view_size, tile.view_size()); |
| 1361 | assert_eq!(self.clock, tile.clock); |
| 1362 | assert_eq!(self.scale, tile.scale()); |
| 1363 | tile.verify_invariants(); |
| 1364 | |
| 1365 | if let Some(idx) = tile.floating_preset_width_idx { |
| 1366 | assert!(idx < self.options.layout.preset_column_widths.len()); |
| 1367 | } |
| 1368 | if let Some(idx) = tile.floating_preset_height_idx { |
| 1369 | assert!(idx < self.options.layout.preset_window_heights.len()); |
| 1370 | } |
| 1371 | |
| 1372 | assert_eq!( |
| 1373 | tile.window().pending_sizing_mode(), |
| 1374 | SizingMode::Normal, |
| 1375 | "floating windows cannot be maximized or fullscreen" |
| 1376 | ); |
| 1377 | |
| 1378 | data.verify_invariants(); |
| 1379 | |
| 1380 | let mut data2 = *data; |
| 1381 | data2.update(tile); |
| 1382 | data2.update_config(self.working_area); |
| 1383 | assert_eq!(data, &data2, "tile data must be up to date"); |
| 1384 | |
| 1385 | for tile_below in &self.tiles[i + 1..] { |
| 1386 | assert!( |
| 1387 | !tile_below.window().is_child_of(tile.window()), |
| 1388 | "children must be stacked above parents" |
| 1389 | ); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | if let Some(id) = &self.active_window_id { |
| 1394 | assert!(!self.tiles.is_empty()); |
| 1395 | assert!(self.contains(id), "active window must be present in tiles"); |
| 1396 | } else { |
| 1397 | assert!(self.tiles.is_empty()); |
| 1398 | } |
| 1399 | |
| 1400 | if let Some(resize) = &self.interactive_resize { |
| 1401 | assert!( |
| 1402 | self.contains(&resize.window), |
| 1403 | "interactive resize window must be present in tiles" |
| 1404 | ); |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | fn compute_toplevel_bounds( |
| 1410 | border_config: niri_config::Border, |
| 1411 | working_area_size: Size<f64, Logical>, |
| 1412 | ) -> Size<i32, Logical> { |
| 1413 | let mut border = 0.; |
| 1414 | if !border_config.off { |
| 1415 | border = border_config.width * 2.; |
| 1416 | } |
| 1417 | |
| 1418 | Size::from(( |
| 1419 | f64::max(working_area_size.w - border, 1.), |
| 1420 | f64::max(working_area_size.h - border, 1.), |
| 1421 | )) |
| 1422 | .to_i32_floor() |
| 1423 | } |
| 1424 | |
| 1425 | fn resolve_preset_size(preset: PresetSize, view_size: f64) -> ResolvedSize { |
| 1426 | match preset { |
| 1427 | PresetSize::Proportion(proportion) => ResolvedSize::Tile(view_size * proportion), |
| 1428 | PresetSize::Fixed(width) => ResolvedSize::Window(f64::from(width)), |
| 1429 | } |
| 1430 | } |