forge
Explore
Log in

Don't you dare go Hollow

floating: Implement smarter clamping for window location

Browse files at this commit

c008e1c5bcab360425780997f70818fa4252c938Ivan MolodetskikhParent: 1aa60f0
A small part of the window always remains on-screen regardless of the working
area changes.

Interactive move lets the user position the window anywhere; automatic actions
like toggle-window-floating and dialog opening try to put the window fully
on-screen.

The size-fraction canonical floating window position remains unclamped, and
clamping happens when recomputing the logical position.
src/layout/floating.rsmodified+45 -10
@@ -16,7 +16,7 @@ use crate::niri_render_elements;
16 use crate::render_helpers::renderer::NiriRenderer;16 use crate::render_helpers::renderer::NiriRenderer;
17 use crate::render_helpers::RenderTarget;17 use crate::render_helpers::RenderTarget;
18 use crate::utils::transaction::TransactionBlocker;18 use crate::utils::transaction::TransactionBlocker;
19-use crate::utils::{ensure_min_max_size, ResizeEdge};19+use crate::utils::{clamp_preferring_top_left_in_area, ensure_min_max_size, ResizeEdge};
20 use crate::window::ResolvedWindowRules;20 use crate::window::ResolvedWindowRules;
21 21
22 /// Space for floating windows.22 /// Space for floating windows.
@@ -105,6 +105,24 @@ impl Data {
105 logical_pos.x *= self.working_area.size.w;105 logical_pos.x *= self.working_area.size.w;
106 logical_pos.y *= self.working_area.size.h;106 logical_pos.y *= self.working_area.size.h;
107 logical_pos += self.working_area.loc;107 logical_pos += self.working_area.loc;
108+
109+ // Make sure the window doesn't go too much off-screen. Numbers taken from Mutter.
110+ let min_on_screen_hor = f64::clamp(self.size.w / 4., 10., 75.);
111+ let min_on_screen_ver = f64::clamp(self.size.h / 4., 10., 75.);
112+ let max_off_screen_hor = f64::max(0., self.size.w - min_on_screen_hor);
113+ let max_off_screen_ver = f64::max(0., self.size.h - min_on_screen_ver);
114+
115+ logical_pos.x = f64::max(logical_pos.x, -max_off_screen_hor);
116+ logical_pos.y = f64::max(logical_pos.y, -max_off_screen_ver);
117+ logical_pos.x = f64::min(
118+ logical_pos.x,
119+ self.working_area.size.w - self.size.w + max_off_screen_hor,
120+ );
121+ logical_pos.y = f64::min(
122+ logical_pos.y,
123+ self.working_area.size.h - self.size.h + max_off_screen_ver,
124+ );
125+
108 self.logical_pos = logical_pos;126 self.logical_pos = logical_pos;
109 }127 }
110 128
@@ -118,7 +136,13 @@ impl Data {
118 }136 }
119 137
120 pub fn update<W: LayoutElement>(&mut self, tile: &Tile<W>) {138 pub fn update<W: LayoutElement>(&mut self, tile: &Tile<W>) {
121- self.size = tile.tile_size();139+ let size = tile.tile_size();
140+ if self.size == size {
141+ return;
142+ }
143+
144+ self.size = size;
145+ self.recompute_logical_pos();
122 }146 }
123 147
124 pub fn set_logical_pos(&mut self, logical_pos: Point<f64, Logical>) {148 pub fn set_logical_pos(&mut self, logical_pos: Point<f64, Logical>) {
@@ -129,7 +153,7 @@ impl Data {
129 153
130 self.pos = pos;154 self.pos = pos;
131 155
132- // This should get close to the same result as what we started with.156+ // This will clamp the logical position to the current working area.
133 self.recompute_logical_pos();157 self.recompute_logical_pos();
134 }158 }
135 159
@@ -361,16 +385,15 @@ impl<W: LayoutElement> FloatingSpace<W> {
361 }385 }
362 }386 }
363 387
364- let mut pos = pos.unwrap_or_else(|| {388+ let pos = pos.unwrap_or_else(|| {
365 let area_size = self.working_area.size.to_point();389 let area_size = self.working_area.size.to_point();
366 let tile_size = tile.tile_size().to_point();390 let tile_size = tile.tile_size().to_point();
367- let offset = (area_size - tile_size).downscale(2.);391+ let mut offset = (area_size - tile_size).downscale(2.);
392+ // Prefer top-left corner if larger than the working area.
393+ offset.x = f64::max(offset.x, 0.);
394+ offset.y = f64::max(offset.y, 0.);
368 self.working_area.loc + offset395 self.working_area.loc + offset
369 });396 });
370- // TODO: also nudge pos + size inside the area.
371- // TODO: smart padding (if doesn't fit, try without padding).
372- pos.x = f64::max(pos.x, self.working_area.loc.x + 8.);
373- pos.y = f64::max(pos.y, self.working_area.loc.y + 8.);
374 397
375 let data = Data::new(self.working_area, &tile, pos);398 let data = Data::new(self.working_area, &tile, pos);
376 self.data.insert(idx, data);399 self.data.insert(idx, data);
@@ -387,7 +410,9 @@ impl<W: LayoutElement> FloatingSpace<W> {
387 410
388 let above_pos = self.data[idx].logical_pos;411 let above_pos = self.data[idx].logical_pos;
389 let above_size = self.data[idx].size;412 let above_size = self.data[idx].size;
390- let pos = above_pos + (above_size.to_point() - tile.tile_size().to_point()).downscale(2.);413+ let tile_size = tile.tile_size();
414+ let pos = above_pos + (above_size.to_point() - tile_size.to_point()).downscale(2.);
415+ let pos = self.clamp_within_working_area(pos, tile_size);
391 416
392 self.add_tile_at(idx, tile, Some(pos), activate);417 self.add_tile_at(idx, tile, Some(pos), activate);
393 }418 }
@@ -796,6 +821,16 @@ impl<W: LayoutElement> FloatingSpace<W> {
796 }821 }
797 }822 }
798 823
824+ pub fn clamp_within_working_area(
825+ &self,
826+ pos: Point<f64, Logical>,
827+ size: Size<f64, Logical>,
828+ ) -> Point<f64, Logical> {
829+ let mut rect = Rectangle::from_loc_and_size(pos, size);
830+ clamp_preferring_top_left_in_area(self.working_area, &mut rect);
831+ rect.loc
832+ }
833+
799 #[cfg(test)]834 #[cfg(test)]
800 pub fn working_area(&self) -> Rectangle<f64, Logical> {835 pub fn working_area(&self) -> Rectangle<f64, Logical> {
801 self.working_area836 self.working_area
src/layout/workspace.rsmodified+9 -3
@@ -572,11 +572,14 @@ impl<W: LayoutElement> Workspace<W> {
572 .tiles_with_render_positions()572 .tiles_with_render_positions()
573 .find(|(tile, _)| tile.window().id() == right_of)573 .find(|(tile, _)| tile.window().id() == right_of)
574 .unwrap();574 .unwrap();
575+
575 // Position the new tile in the center above the right_of tile. Think a dialog576 // Position the new tile in the center above the right_of tile. Think a dialog
576 // opening on top of a window.577 // opening on top of a window.
578+ let tile_size = tile.tile_size();
577 let pos = render_pos579 let pos = render_pos
578- + (right_of_tile.tile_size().to_point() - tile.tile_size().to_point())580+ + (right_of_tile.tile_size().to_point() - tile_size.to_point()).downscale(2.);
579- .downscale(2.);581+ let pos = self.floating.clamp_within_working_area(pos, tile_size);
582+
580 self.floating.add_tile(tile, Some(pos), activate);583 self.floating.add_tile(tile, Some(pos), activate);
581 if activate {584 if activate {
582 self.floating_is_active = true;585 self.floating_is_active = true;
@@ -929,7 +932,10 @@ impl<W: LayoutElement> Workspace<W> {
929 } else {932 } else {
930 let mut removed = self.scrolling.remove_tile(&id, Transaction::new());933 let mut removed = self.scrolling.remove_tile(&id, Transaction::new());
931 removed.tile.stop_move_animations();934 removed.tile.stop_move_animations();
932- let pos = render_pos + Point::from((50., 50.));935+ let pos = self.floating.clamp_within_working_area(
936+ render_pos + Point::from((50., 50.)),
937+ removed.tile.tile_size(),
938+ );
933 self.floating939 self.floating
934 .add_tile(removed.tile, Some(pos), target_is_active);940 .add_tile(removed.tile, Some(pos), target_is_active);
935 if target_is_active {941 if target_is_active {
src/utils/mod.rsmodified+49 -0
@@ -272,6 +272,18 @@ pub fn ensure_min_max_size(mut x: i32, min_size: i32, max_size: i32) -> i32 {
272 x272 x
273 }273 }
274 274
275+pub fn clamp_preferring_top_left_in_area(
276+ area: Rectangle<f64, Logical>,
277+ rect: &mut Rectangle<f64, Logical>,
278+) {
279+ rect.loc.x = f64::min(rect.loc.x, area.loc.x + area.size.w - rect.size.w);
280+ rect.loc.y = f64::min(rect.loc.y, area.loc.y + area.size.h - rect.size.h);
281+
282+ // Clamp by top and left last so it takes precedence.
283+ rect.loc.x = f64::max(rect.loc.x, area.loc.x);
284+ rect.loc.y = f64::max(rect.loc.y, area.loc.y);
285+}
286+
275 #[cfg(feature = "dbus")]287 #[cfg(feature = "dbus")]
276 pub fn show_screenshot_notification(image_path: Option<PathBuf>) {288 pub fn show_screenshot_notification(image_path: Option<PathBuf>) {
277 let mut notification = notify_rust::Notification::new();289 let mut notification = notify_rust::Notification::new();
@@ -309,3 +321,40 @@ pub fn cause_panic() {
309 let b = Duration::from_secs(2);321 let b = Duration::from_secs(2);
310 let _ = a - b;322 let _ = a - b;
311 }323 }
324+
325+#[cfg(test)]
326+mod tests {
327+ use super::*;
328+
329+ #[test]
330+ fn test_clamp_preferring_top_left() {
331+ fn check(
332+ (ax, ay, aw, ah): (i32, i32, i32, i32),
333+ (rx, ry, rw, rh): (i32, i32, i32, i32),
334+ (ex, ey): (i32, i32),
335+ ) {
336+ let area = Rectangle::from_loc_and_size((ax, ay), (aw, ah)).to_f64();
337+ let mut rect = Rectangle::from_loc_and_size((rx, ry), (rw, rh)).to_f64();
338+ clamp_preferring_top_left_in_area(area, &mut rect);
339+ assert_eq!(rect.loc, Point::from((ex, ey)).to_f64());
340+ }
341+
342+ check((0, 0, 10, 20), (2, 3, 4, 5), (2, 3));
343+ check((0, 0, 10, 20), (-2, 3, 4, 5), (0, 3));
344+ check((0, 0, 10, 20), (2, -3, 4, 5), (2, 0));
345+ check((0, 0, 10, 20), (-2, -3, 4, 5), (0, 0));
346+
347+ check((1, 1, 10, 20), (2, 3, 4, 5), (2, 3));
348+ check((1, 1, 10, 20), (-2, 3, 4, 5), (1, 3));
349+ check((1, 1, 10, 20), (2, -3, 4, 5), (2, 1));
350+ check((1, 1, 10, 20), (-2, -3, 4, 5), (1, 1));
351+
352+ check((0, 0, 10, 20), (20, 3, 4, 5), (6, 3));
353+ check((0, 0, 10, 20), (2, 30, 4, 5), (2, 15));
354+ check((0, 0, 10, 20), (20, 30, 4, 5), (6, 15));
355+
356+ check((0, 0, 10, 20), (20, 30, 40, 5), (0, 15));
357+ check((0, 0, 10, 20), (20, 30, 4, 50), (6, 0));
358+ check((0, 0, 10, 20), (20, 30, 40, 50), (0, 0));
359+ }
360+}