Remember floating window position
Browse files at this commit53e1c58cc53c3868c236cc8877816ed4aab5b7b0Parent: 4b9ecdd5 changed files +107 -60
- src/layout/floating.rs +50 -29
- src/layout/mod.rs +8 -2
- src/layout/monitor.rs +5 -11
- src/layout/tile.rs +17 -1
- src/layout/workspace.rs +27 -17
src/layout/floating.rsmodified+50 -29
| @@ -10,7 +10,9 @@ use super::closing_window::{ClosingWindow, ClosingWindowRenderElement}; | ||
| 10 | 10 | use super::scrolling::ColumnWidth; |
| 11 | 11 | use super::tile::{Tile, TileRenderElement, TileRenderSnapshot}; |
| 12 | 12 | use super::workspace::InteractiveResize; |
| 13 | -use super::{ConfigureIntent, InteractiveResizeData, LayoutElement, Options, RemovedTile}; | |
| 13 | +use super::{ | |
| 14 | + ConfigureIntent, InteractiveResizeData, LayoutElement, Options, RemovedTile, SizeFrac, | |
| 15 | +}; | |
| 14 | 16 | use crate::animation::{Animation, Clock}; |
| 15 | 17 | use crate::niri_render_elements; |
| 16 | 18 | use crate::render_helpers::renderer::NiriRenderer; |
| @@ -68,9 +70,6 @@ niri_render_elements! { | ||
| 68 | 70 | } |
| 69 | 71 | } |
| 70 | 72 | |
| 71 | -/// Size-relative units. | |
| 72 | -struct SizeFrac; | |
| 73 | - | |
| 74 | 73 | /// Extra per-tile data. |
| 75 | 74 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 76 | 75 | struct Data { |
| @@ -106,11 +105,30 @@ impl Data { | ||
| 106 | 105 | rv |
| 107 | 106 | } |
| 108 | 107 | |
| 108 | + pub fn scale_by_working_area( | |
| 109 | + area: Rectangle<f64, Logical>, | |
| 110 | + pos: Point<f64, SizeFrac>, | |
| 111 | + ) -> Point<f64, Logical> { | |
| 112 | + let mut logical_pos = Point::from((pos.x, pos.y)); | |
| 113 | + logical_pos.x *= area.size.w; | |
| 114 | + logical_pos.y *= area.size.h; | |
| 115 | + logical_pos += area.loc; | |
| 116 | + logical_pos | |
| 117 | + } | |
| 118 | + | |
| 119 | + pub fn logical_to_size_frac_in_working_area( | |
| 120 | + area: Rectangle<f64, Logical>, | |
| 121 | + logical_pos: Point<f64, Logical>, | |
| 122 | + ) -> Point<f64, SizeFrac> { | |
| 123 | + let pos = logical_pos - area.loc; | |
| 124 | + let mut pos = Point::from((pos.x, pos.y)); | |
| 125 | + pos.x /= f64::max(area.size.w, 1.0); | |
| 126 | + pos.y /= f64::max(area.size.h, 1.0); | |
| 127 | + pos | |
| 128 | + } | |
| 129 | + | |
| 109 | 130 | fn recompute_logical_pos(&mut self) { |
| 110 | - let mut logical_pos = Point::from((self.pos.x, self.pos.y)); | |
| 111 | - logical_pos.x *= self.working_area.size.w; | |
| 112 | - logical_pos.y *= self.working_area.size.h; | |
| 113 | - logical_pos += self.working_area.loc; | |
| 131 | + let mut logical_pos = Self::scale_by_working_area(self.working_area, self.pos); | |
| 114 | 132 | |
| 115 | 133 | // Make sure the window doesn't go too much off-screen. Numbers taken from Mutter. |
| 116 | 134 | let min_on_screen_hor = f64::clamp(self.size.w / 4., 10., 75.); |
| @@ -152,12 +170,7 @@ impl Data { | ||
| 152 | 170 | } |
| 153 | 171 | |
| 154 | 172 | pub fn set_logical_pos(&mut self, logical_pos: Point<f64, Logical>) { |
| 155 | - let pos = logical_pos - self.working_area.loc; | |
| 156 | - let mut pos = Point::from((pos.x, pos.y)); | |
| 157 | - pos.x /= f64::max(self.working_area.size.w, 1.0); | |
| 158 | - pos.y /= f64::max(self.working_area.size.h, 1.0); | |
| 159 | - | |
| 160 | - self.pos = pos; | |
| 173 | + self.pos = Self::logical_to_size_frac_in_working_area(self.working_area, logical_pos); | |
| 161 | 174 | |
| 162 | 175 | // This will clamp the logical position to the current working area. |
| 163 | 176 | self.recompute_logical_pos(); |
| @@ -353,17 +366,11 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 353 | 366 | self.tiles.is_empty() |
| 354 | 367 | } |
| 355 | 368 | |
| 356 | - pub fn add_tile(&mut self, tile: Tile<W>, pos: Option<Point<f64, Logical>>, activate: bool) { | |
| 357 | - self.add_tile_at(0, tile, pos, activate); | |
| 369 | + pub fn add_tile(&mut self, tile: Tile<W>, activate: bool) { | |
| 370 | + self.add_tile_at(0, tile, activate); | |
| 358 | 371 | } |
| 359 | 372 | |
| 360 | - fn add_tile_at( | |
| 361 | - &mut self, | |
| 362 | - mut idx: usize, | |
| 363 | - mut tile: Tile<W>, | |
| 364 | - pos: Option<Point<f64, Logical>>, | |
| 365 | - activate: bool, | |
| 366 | - ) { | |
| 373 | + fn add_tile_at(&mut self, mut idx: usize, mut tile: Tile<W>, activate: bool) { | |
| 367 | 374 | tile.update_config(self.scale, self.options.clone()); |
| 368 | 375 | |
| 369 | 376 | // Restore the previous floating window size, and in case the tile is fullscreen, |
| @@ -402,9 +409,12 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 402 | 409 | } |
| 403 | 410 | } |
| 404 | 411 | |
| 405 | - let pos = pos.unwrap_or_else(|| { | |
| 406 | - center_preferring_top_left_in_area(self.working_area, tile.tile_size()) | |
| 407 | - }); | |
| 412 | + let pos = tile | |
| 413 | + .floating_pos() | |
| 414 | + .map(|pos| self.scale_by_working_area(pos)) | |
| 415 | + .unwrap_or_else(|| { | |
| 416 | + center_preferring_top_left_in_area(self.working_area, tile.tile_size()) | |
| 417 | + }); | |
| 408 | 418 | |
| 409 | 419 | let data = Data::new(self.working_area, &tile, pos); |
| 410 | 420 | self.data.insert(idx, data); |
| @@ -413,7 +423,7 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 413 | 423 | self.bring_up_descendants_of(idx); |
| 414 | 424 | } |
| 415 | 425 | |
| 416 | - pub fn add_tile_above(&mut self, above: &W::Id, tile: Tile<W>) { | |
| 426 | + pub fn add_tile_above(&mut self, above: &W::Id, mut tile: Tile<W>) { | |
| 417 | 427 | // Activate the new window if above was active. |
| 418 | 428 | let activate = Some(above) == self.active_window_id.as_ref(); |
| 419 | 429 | |
| @@ -424,8 +434,9 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 424 | 434 | let tile_size = tile.tile_size(); |
| 425 | 435 | let pos = above_pos + (above_size.to_point() - tile_size.to_point()).downscale(2.); |
| 426 | 436 | let pos = self.clamp_within_working_area(pos, tile_size); |
| 437 | + tile.set_floating_pos(self.logical_to_size_frac(pos)); | |
| 427 | 438 | |
| 428 | - self.add_tile_at(idx, tile, Some(pos), activate); | |
| 439 | + self.add_tile_at(idx, tile, activate); | |
| 429 | 440 | } |
| 430 | 441 | |
| 431 | 442 | fn bring_up_descendants_of(&mut self, idx: usize) { |
| @@ -467,7 +478,7 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 467 | 478 | |
| 468 | 479 | fn remove_tile_by_idx(&mut self, idx: usize) -> RemovedTile<W> { |
| 469 | 480 | let mut tile = self.tiles.remove(idx); |
| 470 | - self.data.remove(idx); | |
| 481 | + let data = self.data.remove(idx); | |
| 471 | 482 | |
| 472 | 483 | if self.tiles.is_empty() { |
| 473 | 484 | self.active_window_id = None; |
| @@ -487,6 +498,8 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 487 | 498 | if let Some(size) = tile.window().expected_size() { |
| 488 | 499 | tile.set_floating_window_size(size); |
| 489 | 500 | } |
| 501 | + // Store the floating position. | |
| 502 | + tile.set_floating_pos(data.pos); | |
| 490 | 503 | |
| 491 | 504 | let width = ColumnWidth::Fixed(tile.window_size().w); |
| 492 | 505 | RemovedTile { |
| @@ -934,6 +947,14 @@ impl<W: LayoutElement> FloatingSpace<W> { | ||
| 934 | 947 | rect.loc |
| 935 | 948 | } |
| 936 | 949 | |
| 950 | + fn scale_by_working_area(&self, pos: Point<f64, SizeFrac>) -> Point<f64, Logical> { | |
| 951 | + Data::scale_by_working_area(self.working_area, pos) | |
| 952 | + } | |
| 953 | + | |
| 954 | + pub fn logical_to_size_frac(&self, logical_pos: Point<f64, Logical>) -> Point<f64, SizeFrac> { | |
| 955 | + Data::logical_to_size_frac_in_working_area(self.working_area, logical_pos) | |
| 956 | + } | |
| 957 | + | |
| 937 | 958 | fn move_and_animate(&mut self, idx: usize, new_pos: Point<f64, Logical>) { |
| 938 | 959 | // Moves up to this logical pixel distance are not animated. |
| 939 | 960 | const ANIMATION_THRESHOLD_SQ: f64 = 10. * 10.; |
src/layout/mod.rsmodified+8 -2
| @@ -81,6 +81,9 @@ pub const RESIZE_ANIMATION_THRESHOLD: f64 = 10.; | ||
| 81 | 81 | /// Pointer needs to move this far to pull a window from the layout. |
| 82 | 82 | const INTERACTIVE_MOVE_START_THRESHOLD: f64 = 256. * 256.; |
| 83 | 83 | |
| 84 | +/// Size-relative units. | |
| 85 | +pub struct SizeFrac; | |
| 86 | + | |
| 84 | 87 | niri_render_elements! { |
| 85 | 88 | LayoutElementRenderElement<R> => { |
| 86 | 89 | Wayland = WaylandSurfaceRenderElement<R>, |
| @@ -3482,14 +3485,17 @@ impl<W: LayoutElement> Layout<W> { | ||
| 3482 | 3485 | InsertPosition::Floating => { |
| 3483 | 3486 | let pos = move_.tile_render_location() - offset; |
| 3484 | 3487 | |
| 3488 | + let mut tile = move_.tile; | |
| 3489 | + let pos = mon.workspaces[ws_idx].floating_logical_to_size_frac(pos); | |
| 3490 | + tile.set_floating_pos(pos); | |
| 3491 | + | |
| 3485 | 3492 | // Set the floating size so it takes into account any window resizing that |
| 3486 | 3493 | // took place during the move. |
| 3487 | - let mut tile = move_.tile; | |
| 3488 | 3494 | if let Some(size) = tile.window().expected_size() { |
| 3489 | 3495 | tile.set_floating_window_size(size); |
| 3490 | 3496 | } |
| 3491 | 3497 | |
| 3492 | - mon.add_floating_tile(ws_idx, tile, Some(pos), true); | |
| 3498 | + mon.add_floating_tile(ws_idx, tile, true); | |
| 3493 | 3499 | } |
| 3494 | 3500 | } |
| 3495 | 3501 |
src/layout/monitor.rsmodified+5 -11
| @@ -324,16 +324,10 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 324 | 324 | } |
| 325 | 325 | } |
| 326 | 326 | |
| 327 | - pub fn add_floating_tile( | |
| 328 | - &mut self, | |
| 329 | - mut workspace_idx: usize, | |
| 330 | - tile: Tile<W>, | |
| 331 | - pos: Option<Point<f64, Logical>>, | |
| 332 | - activate: bool, | |
| 333 | - ) { | |
| 327 | + pub fn add_floating_tile(&mut self, mut workspace_idx: usize, tile: Tile<W>, activate: bool) { | |
| 334 | 328 | let workspace = &mut self.workspaces[workspace_idx]; |
| 335 | 329 | |
| 336 | - workspace.add_floating_tile(tile, pos, activate); | |
| 330 | + workspace.add_floating_tile(tile, activate); | |
| 337 | 331 | |
| 338 | 332 | // After adding a new window, workspace becomes this output's own. |
| 339 | 333 | workspace.original_output = OutputId::new(&self.output); |
| @@ -531,7 +525,7 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 531 | 525 | }; |
| 532 | 526 | |
| 533 | 527 | if removed.is_floating { |
| 534 | - self.add_floating_tile(new_idx, removed.tile, None, true); | |
| 528 | + self.add_floating_tile(new_idx, removed.tile, true); | |
| 535 | 529 | } else { |
| 536 | 530 | self.add_tile( |
| 537 | 531 | new_idx, |
| @@ -558,7 +552,7 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 558 | 552 | }; |
| 559 | 553 | |
| 560 | 554 | if removed.is_floating { |
| 561 | - self.add_floating_tile(new_idx, removed.tile, None, true); | |
| 555 | + self.add_floating_tile(new_idx, removed.tile, true); | |
| 562 | 556 | } else { |
| 563 | 557 | self.add_tile( |
| 564 | 558 | new_idx, |
| @@ -601,7 +595,7 @@ impl<W: LayoutElement> Monitor<W> { | ||
| 601 | 595 | }; |
| 602 | 596 | |
| 603 | 597 | if removed.is_floating { |
| 604 | - self.add_floating_tile(new_idx, removed.tile, None, activate); | |
| 598 | + self.add_floating_tile(new_idx, removed.tile, activate); | |
| 605 | 599 | } else { |
| 606 | 600 | self.add_tile( |
| 607 | 601 | new_idx, |
src/layout/tile.rsmodified+17 -1
| @@ -9,7 +9,7 @@ use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform}; | ||
| 9 | 9 | use super::focus_ring::{FocusRing, FocusRingRenderElement}; |
| 10 | 10 | use super::opening_window::{OpenAnimation, OpeningWindowRenderElement}; |
| 11 | 11 | use super::{ |
| 12 | - LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options, | |
| 12 | + LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options, SizeFrac, | |
| 13 | 13 | RESIZE_ANIMATION_THRESHOLD, |
| 14 | 14 | }; |
| 15 | 15 | use crate::animation::{Animation, Clock}; |
| @@ -60,6 +60,13 @@ pub struct Tile<W: LayoutElement> { | ||
| 60 | 60 | /// the window starts out in the tiling layout or fullscreen. |
| 61 | 61 | floating_window_size: Option<Size<i32, Logical>>, |
| 62 | 62 | |
| 63 | + /// The position that the tile should assume when going floating, relative to the floating | |
| 64 | + /// space working area. | |
| 65 | + /// | |
| 66 | + /// This is generally the last position the tile had when it was floating. It can be unknown if | |
| 67 | + /// the window starts out in the tiling layout. | |
| 68 | + floating_pos: Option<Point<f64, SizeFrac>>, | |
| 69 | + | |
| 63 | 70 | /// The animation upon opening a window. |
| 64 | 71 | open_animation: Option<OpenAnimation>, |
| 65 | 72 | |
| @@ -135,6 +142,7 @@ impl<W: LayoutElement> Tile<W> { | ||
| 135 | 142 | fullscreen_size: Default::default(), |
| 136 | 143 | unfullscreen_to_floating: false, |
| 137 | 144 | floating_window_size: None, |
| 145 | + floating_pos: None, | |
| 138 | 146 | open_animation: None, |
| 139 | 147 | resize_animation: None, |
| 140 | 148 | move_x_animation: None, |
| @@ -950,6 +958,14 @@ impl<W: LayoutElement> Tile<W> { | ||
| 950 | 958 | self.floating_window_size = Some(floating_window_size); |
| 951 | 959 | } |
| 952 | 960 | |
| 961 | + pub fn floating_pos(&self) -> Option<Point<f64, SizeFrac>> { | |
| 962 | + self.floating_pos | |
| 963 | + } | |
| 964 | + | |
| 965 | + pub fn set_floating_pos(&mut self, floating_pos: Point<f64, SizeFrac>) { | |
| 966 | + self.floating_pos = Some(floating_pos); | |
| 967 | + } | |
| 968 | + | |
| 953 | 969 | #[cfg(test)] |
| 954 | 970 | pub fn verify_invariants(&self) { |
| 955 | 971 | use approx::assert_abs_diff_eq; |
src/layout/workspace.rsmodified+27 -17
| @@ -18,7 +18,7 @@ use super::scrolling::{ | ||
| 18 | 18 | Column, ColumnWidth, InsertHint, InsertPosition, ScrollingSpace, ScrollingSpaceRenderElement, |
| 19 | 19 | }; |
| 20 | 20 | use super::tile::{Tile, TileRenderSnapshot}; |
| 21 | -use super::{InteractiveResizeData, LayoutElement, Options, RemovedTile}; | |
| 21 | +use super::{InteractiveResizeData, LayoutElement, Options, RemovedTile, SizeFrac}; | |
| 22 | 22 | use crate::animation::Clock; |
| 23 | 23 | use crate::niri_render_elements; |
| 24 | 24 | use crate::render_helpers::renderer::NiriRenderer; |
| @@ -503,7 +503,7 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 503 | 503 | // If the tile is pending fullscreen, open it in the scrolling layout where it can go |
| 504 | 504 | // fullscreen. |
| 505 | 505 | if is_floating && !tile.window().is_pending_fullscreen() { |
| 506 | - self.add_floating_tile(tile, None, activate); | |
| 506 | + self.add_floating_tile(tile, activate); | |
| 507 | 507 | } else { |
| 508 | 508 | self.add_tile(None, tile, activate, width, is_full_width); |
| 509 | 509 | } |
| @@ -526,14 +526,9 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 526 | 526 | } |
| 527 | 527 | } |
| 528 | 528 | |
| 529 | - pub fn add_floating_tile( | |
| 530 | - &mut self, | |
| 531 | - tile: Tile<W>, | |
| 532 | - pos: Option<Point<f64, Logical>>, | |
| 533 | - activate: bool, | |
| 534 | - ) { | |
| 529 | + pub fn add_floating_tile(&mut self, tile: Tile<W>, activate: bool) { | |
| 535 | 530 | self.enter_output_for_window(tile.window()); |
| 536 | - self.floating.add_tile(tile, pos, activate); | |
| 531 | + self.floating.add_tile(tile, activate); | |
| 537 | 532 | |
| 538 | 533 | if activate || self.scrolling.is_empty() { |
| 539 | 534 | self.floating_is_active = FloatingActive::Yes; |
| @@ -578,7 +573,7 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 578 | 573 | pub fn add_tile_right_of( |
| 579 | 574 | &mut self, |
| 580 | 575 | right_of: &W::Id, |
| 581 | - tile: Tile<W>, | |
| 576 | + mut tile: Tile<W>, | |
| 582 | 577 | width: ColumnWidth, |
| 583 | 578 | is_full_width: bool, |
| 584 | 579 | is_floating: bool, |
| @@ -605,8 +600,10 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 605 | 600 | let pos = render_pos |
| 606 | 601 | + (right_of_tile.tile_size().to_point() - tile_size.to_point()).downscale(2.); |
| 607 | 602 | let pos = self.floating.clamp_within_working_area(pos, tile_size); |
| 603 | + let pos = self.floating.logical_to_size_frac(pos); | |
| 604 | + tile.set_floating_pos(pos); | |
| 608 | 605 | |
| 609 | - self.floating.add_tile(tile, Some(pos), activate); | |
| 606 | + self.floating.add_tile(tile, activate); | |
| 610 | 607 | if activate { |
| 611 | 608 | self.floating_is_active = FloatingActive::Yes; |
| 612 | 609 | } |
| @@ -1087,12 +1084,18 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1087 | 1084 | } else { |
| 1088 | 1085 | let mut removed = self.scrolling.remove_tile(&id, Transaction::new()); |
| 1089 | 1086 | removed.tile.stop_move_animations(); |
| 1090 | - let pos = self.floating.clamp_within_working_area( | |
| 1091 | - render_pos + Point::from((50., 50.)), | |
| 1092 | - removed.tile.tile_size(), | |
| 1093 | - ); | |
| 1094 | - self.floating | |
| 1095 | - .add_tile(removed.tile, Some(pos), target_is_active); | |
| 1087 | + | |
| 1088 | + // Come up with a default floating position close to the tile position. | |
| 1089 | + if removed.tile.floating_pos().is_none() { | |
| 1090 | + let pos = self.floating.clamp_within_working_area( | |
| 1091 | + render_pos + Point::from((50., 50.)), | |
| 1092 | + removed.tile.tile_size(), | |
| 1093 | + ); | |
| 1094 | + let pos = self.floating.logical_to_size_frac(pos); | |
| 1095 | + removed.tile.set_floating_pos(pos); | |
| 1096 | + } | |
| 1097 | + | |
| 1098 | + self.floating.add_tile(removed.tile, target_is_active); | |
| 1096 | 1099 | if target_is_active { |
| 1097 | 1100 | self.floating_is_active = FloatingActive::Yes; |
| 1098 | 1101 | } |
| @@ -1436,6 +1439,13 @@ impl<W: LayoutElement> Workspace<W> { | ||
| 1436 | 1439 | self.floating_is_active.get() |
| 1437 | 1440 | } |
| 1438 | 1441 | |
| 1442 | + pub fn floating_logical_to_size_frac( | |
| 1443 | + &self, | |
| 1444 | + logical_pos: Point<f64, Logical>, | |
| 1445 | + ) -> Point<f64, SizeFrac> { | |
| 1446 | + self.floating.logical_to_size_frac(logical_pos) | |
| 1447 | + } | |
| 1448 | + | |
| 1439 | 1449 | #[cfg(test)] |
| 1440 | 1450 | pub fn scrolling(&self) -> &ScrollingSpace<W> { |
| 1441 | 1451 | &self.scrolling |