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;
1616 use crate::render_helpers::renderer::NiriRenderer;
1717 use crate::render_helpers::RenderTarget;
1818 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};
2020 use crate::window::ResolvedWindowRules;
2121
2222 /// Space for floating windows.
@@ -105,6 +105,24 @@ impl Data {
105105 logical_pos.x *= self.working_area.size.w;
106106 logical_pos.y *= self.working_area.size.h;
107107 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+
108126 self.logical_pos = logical_pos;
109127 }
110128
@@ -118,7 +136,13 @@ impl Data {
118136 }
119137
120138 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();
122146 }
123147
124148 pub fn set_logical_pos(&mut self, logical_pos: Point<f64, Logical>) {
@@ -129,7 +153,7 @@ impl Data {
129153
130154 self.pos = pos;
131155
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.
133157 self.recompute_logical_pos();
134158 }
135159
@@ -361,16 +385,15 @@ impl<W: LayoutElement> FloatingSpace<W> {
361385 }
362386 }
363387
364- let mut pos = pos.unwrap_or_else(|| {
388+ let pos = pos.unwrap_or_else(|| {
365389 let area_size = self.working_area.size.to_point();
366390 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.);
368395 self.working_area.loc + offset
369396 });
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.);
374397
375398 let data = Data::new(self.working_area, &tile, pos);
376399 self.data.insert(idx, data);
@@ -387,7 +410,9 @@ impl<W: LayoutElement> FloatingSpace<W> {
387410
388411 let above_pos = self.data[idx].logical_pos;
389412 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);
391416
392417 self.add_tile_at(idx, tile, Some(pos), activate);
393418 }
@@ -796,6 +821,16 @@ impl<W: LayoutElement> FloatingSpace<W> {
796821 }
797822 }
798823
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+
799834 #[cfg(test)]
800835 pub fn working_area(&self) -> Rectangle<f64, Logical> {
801836 self.working_area
src/layout/workspace.rsmodified+9 -3
@@ -572,11 +572,14 @@ impl<W: LayoutElement> Workspace<W> {
572572 .tiles_with_render_positions()
573573 .find(|(tile, _)| tile.window().id() == right_of)
574574 .unwrap();
575+
575576 // Position the new tile in the center above the right_of tile. Think a dialog
576577 // opening on top of a window.
578+ let tile_size = tile.tile_size();
577579 let pos = render_pos
578- + (right_of_tile.tile_size().to_point() - tile.tile_size().to_point())
579- .downscale(2.);
580+ + (right_of_tile.tile_size().to_point() - tile_size.to_point()).downscale(2.);
581+ let pos = self.floating.clamp_within_working_area(pos, tile_size);
582+
580583 self.floating.add_tile(tile, Some(pos), activate);
581584 if activate {
582585 self.floating_is_active = true;
@@ -929,7 +932,10 @@ impl<W: LayoutElement> Workspace<W> {
929932 } else {
930933 let mut removed = self.scrolling.remove_tile(&id, Transaction::new());
931934 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+ );
933939 self.floating
934940 .add_tile(removed.tile, Some(pos), target_is_active);
935941 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 {
272272 x
273273 }
274274
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+
275287 #[cfg(feature = "dbus")]
276288 pub fn show_screenshot_notification(image_path: Option<PathBuf>) {
277289 let mut notification = notify_rust::Notification::new();
@@ -309,3 +321,40 @@ pub fn cause_panic() {
309321 let b = Duration::from_secs(2);
310322 let _ = a - b;
311323 }
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+}