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