forge
Explore
Log in

Don't you dare go Hollow

Remember floating window position

Browse files at this commit

53e1c58cc53c3868c236cc8877816ed4aab5b7b0Ivan MolodetskikhParent: 4b9ecdd
src/layout/floating.rsmodified+50 -29
@@ -10,7 +10,9 @@ use super::closing_window::{ClosingWindow, ClosingWindowRenderElement};
1010 use super::scrolling::ColumnWidth;
1111 use super::tile::{Tile, TileRenderElement, TileRenderSnapshot};
1212 use super::workspace::InteractiveResize;
13-use super::{ConfigureIntent, InteractiveResizeData, LayoutElement, Options, RemovedTile};
13+use super::{
14+ ConfigureIntent, InteractiveResizeData, LayoutElement, Options, RemovedTile, SizeFrac,
15+};
1416 use crate::animation::{Animation, Clock};
1517 use crate::niri_render_elements;
1618 use crate::render_helpers::renderer::NiriRenderer;
@@ -68,9 +70,6 @@ niri_render_elements! {
6870 }
6971 }
7072
71-/// Size-relative units.
72-struct SizeFrac;
73-
7473 /// Extra per-tile data.
7574 #[derive(Debug, Clone, Copy, PartialEq)]
7675 struct Data {
@@ -106,11 +105,30 @@ impl Data {
106105 rv
107106 }
108107
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+
109130 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);
114132
115133 // Make sure the window doesn't go too much off-screen. Numbers taken from Mutter.
116134 let min_on_screen_hor = f64::clamp(self.size.w / 4., 10., 75.);
@@ -152,12 +170,7 @@ impl Data {
152170 }
153171
154172 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);
161174
162175 // This will clamp the logical position to the current working area.
163176 self.recompute_logical_pos();
@@ -353,17 +366,11 @@ impl<W: LayoutElement> FloatingSpace<W> {
353366 self.tiles.is_empty()
354367 }
355368
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);
358371 }
359372
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) {
367374 tile.update_config(self.scale, self.options.clone());
368375
369376 // Restore the previous floating window size, and in case the tile is fullscreen,
@@ -402,9 +409,12 @@ impl<W: LayoutElement> FloatingSpace<W> {
402409 }
403410 }
404411
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+ });
408418
409419 let data = Data::new(self.working_area, &tile, pos);
410420 self.data.insert(idx, data);
@@ -413,7 +423,7 @@ impl<W: LayoutElement> FloatingSpace<W> {
413423 self.bring_up_descendants_of(idx);
414424 }
415425
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>) {
417427 // Activate the new window if above was active.
418428 let activate = Some(above) == self.active_window_id.as_ref();
419429
@@ -424,8 +434,9 @@ impl<W: LayoutElement> FloatingSpace<W> {
424434 let tile_size = tile.tile_size();
425435 let pos = above_pos + (above_size.to_point() - tile_size.to_point()).downscale(2.);
426436 let pos = self.clamp_within_working_area(pos, tile_size);
437+ tile.set_floating_pos(self.logical_to_size_frac(pos));
427438
428- self.add_tile_at(idx, tile, Some(pos), activate);
439+ self.add_tile_at(idx, tile, activate);
429440 }
430441
431442 fn bring_up_descendants_of(&mut self, idx: usize) {
@@ -467,7 +478,7 @@ impl<W: LayoutElement> FloatingSpace<W> {
467478
468479 fn remove_tile_by_idx(&mut self, idx: usize) -> RemovedTile<W> {
469480 let mut tile = self.tiles.remove(idx);
470- self.data.remove(idx);
481+ let data = self.data.remove(idx);
471482
472483 if self.tiles.is_empty() {
473484 self.active_window_id = None;
@@ -487,6 +498,8 @@ impl<W: LayoutElement> FloatingSpace<W> {
487498 if let Some(size) = tile.window().expected_size() {
488499 tile.set_floating_window_size(size);
489500 }
501+ // Store the floating position.
502+ tile.set_floating_pos(data.pos);
490503
491504 let width = ColumnWidth::Fixed(tile.window_size().w);
492505 RemovedTile {
@@ -934,6 +947,14 @@ impl<W: LayoutElement> FloatingSpace<W> {
934947 rect.loc
935948 }
936949
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+
937958 fn move_and_animate(&mut self, idx: usize, new_pos: Point<f64, Logical>) {
938959 // Moves up to this logical pixel distance are not animated.
939960 const ANIMATION_THRESHOLD_SQ: f64 = 10. * 10.;
src/layout/mod.rsmodified+8 -2
@@ -81,6 +81,9 @@ pub const RESIZE_ANIMATION_THRESHOLD: f64 = 10.;
8181 /// Pointer needs to move this far to pull a window from the layout.
8282 const INTERACTIVE_MOVE_START_THRESHOLD: f64 = 256. * 256.;
8383
84+/// Size-relative units.
85+pub struct SizeFrac;
86+
8487 niri_render_elements! {
8588 LayoutElementRenderElement<R> => {
8689 Wayland = WaylandSurfaceRenderElement<R>,
@@ -3482,14 +3485,17 @@ impl<W: LayoutElement> Layout<W> {
34823485 InsertPosition::Floating => {
34833486 let pos = move_.tile_render_location() - offset;
34843487
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+
34853492 // Set the floating size so it takes into account any window resizing that
34863493 // took place during the move.
3487- let mut tile = move_.tile;
34883494 if let Some(size) = tile.window().expected_size() {
34893495 tile.set_floating_window_size(size);
34903496 }
34913497
3492- mon.add_floating_tile(ws_idx, tile, Some(pos), true);
3498+ mon.add_floating_tile(ws_idx, tile, true);
34933499 }
34943500 }
34953501
src/layout/monitor.rsmodified+5 -11
@@ -324,16 +324,10 @@ impl<W: LayoutElement> Monitor<W> {
324324 }
325325 }
326326
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) {
334328 let workspace = &mut self.workspaces[workspace_idx];
335329
336- workspace.add_floating_tile(tile, pos, activate);
330+ workspace.add_floating_tile(tile, activate);
337331
338332 // After adding a new window, workspace becomes this output's own.
339333 workspace.original_output = OutputId::new(&self.output);
@@ -531,7 +525,7 @@ impl<W: LayoutElement> Monitor<W> {
531525 };
532526
533527 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);
535529 } else {
536530 self.add_tile(
537531 new_idx,
@@ -558,7 +552,7 @@ impl<W: LayoutElement> Monitor<W> {
558552 };
559553
560554 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);
562556 } else {
563557 self.add_tile(
564558 new_idx,
@@ -601,7 +595,7 @@ impl<W: LayoutElement> Monitor<W> {
601595 };
602596
603597 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);
605599 } else {
606600 self.add_tile(
607601 new_idx,
src/layout/tile.rsmodified+17 -1
@@ -9,7 +9,7 @@ use smithay::utils::{Logical, Point, Rectangle, Scale, Size, Transform};
99 use super::focus_ring::{FocusRing, FocusRingRenderElement};
1010 use super::opening_window::{OpenAnimation, OpeningWindowRenderElement};
1111 use super::{
12- LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options,
12+ LayoutElement, LayoutElementRenderElement, LayoutElementRenderSnapshot, Options, SizeFrac,
1313 RESIZE_ANIMATION_THRESHOLD,
1414 };
1515 use crate::animation::{Animation, Clock};
@@ -60,6 +60,13 @@ pub struct Tile<W: LayoutElement> {
6060 /// the window starts out in the tiling layout or fullscreen.
6161 floating_window_size: Option<Size<i32, Logical>>,
6262
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+
6370 /// The animation upon opening a window.
6471 open_animation: Option<OpenAnimation>,
6572
@@ -135,6 +142,7 @@ impl<W: LayoutElement> Tile<W> {
135142 fullscreen_size: Default::default(),
136143 unfullscreen_to_floating: false,
137144 floating_window_size: None,
145+ floating_pos: None,
138146 open_animation: None,
139147 resize_animation: None,
140148 move_x_animation: None,
@@ -950,6 +958,14 @@ impl<W: LayoutElement> Tile<W> {
950958 self.floating_window_size = Some(floating_window_size);
951959 }
952960
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+
953969 #[cfg(test)]
954970 pub fn verify_invariants(&self) {
955971 use approx::assert_abs_diff_eq;
src/layout/workspace.rsmodified+27 -17
@@ -18,7 +18,7 @@ use super::scrolling::{
1818 Column, ColumnWidth, InsertHint, InsertPosition, ScrollingSpace, ScrollingSpaceRenderElement,
1919 };
2020 use super::tile::{Tile, TileRenderSnapshot};
21-use super::{InteractiveResizeData, LayoutElement, Options, RemovedTile};
21+use super::{InteractiveResizeData, LayoutElement, Options, RemovedTile, SizeFrac};
2222 use crate::animation::Clock;
2323 use crate::niri_render_elements;
2424 use crate::render_helpers::renderer::NiriRenderer;
@@ -503,7 +503,7 @@ impl<W: LayoutElement> Workspace<W> {
503503 // If the tile is pending fullscreen, open it in the scrolling layout where it can go
504504 // fullscreen.
505505 if is_floating && !tile.window().is_pending_fullscreen() {
506- self.add_floating_tile(tile, None, activate);
506+ self.add_floating_tile(tile, activate);
507507 } else {
508508 self.add_tile(None, tile, activate, width, is_full_width);
509509 }
@@ -526,14 +526,9 @@ impl<W: LayoutElement> Workspace<W> {
526526 }
527527 }
528528
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) {
535530 self.enter_output_for_window(tile.window());
536- self.floating.add_tile(tile, pos, activate);
531+ self.floating.add_tile(tile, activate);
537532
538533 if activate || self.scrolling.is_empty() {
539534 self.floating_is_active = FloatingActive::Yes;
@@ -578,7 +573,7 @@ impl<W: LayoutElement> Workspace<W> {
578573 pub fn add_tile_right_of(
579574 &mut self,
580575 right_of: &W::Id,
581- tile: Tile<W>,
576+ mut tile: Tile<W>,
582577 width: ColumnWidth,
583578 is_full_width: bool,
584579 is_floating: bool,
@@ -605,8 +600,10 @@ impl<W: LayoutElement> Workspace<W> {
605600 let pos = render_pos
606601 + (right_of_tile.tile_size().to_point() - tile_size.to_point()).downscale(2.);
607602 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);
608605
609- self.floating.add_tile(tile, Some(pos), activate);
606+ self.floating.add_tile(tile, activate);
610607 if activate {
611608 self.floating_is_active = FloatingActive::Yes;
612609 }
@@ -1087,12 +1084,18 @@ impl<W: LayoutElement> Workspace<W> {
10871084 } else {
10881085 let mut removed = self.scrolling.remove_tile(&id, Transaction::new());
10891086 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);
10961099 if target_is_active {
10971100 self.floating_is_active = FloatingActive::Yes;
10981101 }
@@ -1436,6 +1439,13 @@ impl<W: LayoutElement> Workspace<W> {
14361439 self.floating_is_active.get()
14371440 }
14381441
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+
14391449 #[cfg(test)]
14401450 pub fn scrolling(&self) -> &ScrollingSpace<W> {
14411451 &self.scrolling