Skip to content

Commit

Permalink
snap window edges to close output edges
Browse files Browse the repository at this point in the history
  • Loading branch information
Koranir committed Feb 4, 2025
1 parent 3b0aa9e commit 7a71b87
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/shell/grabs/moving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,35 @@ impl MoveGrab {

let mut window_geo = self.window.geometry();
window_geo.loc += location.to_i32_round() + grab_state.window_offset;

if matches!(self.previous, ManagedLayer::Floating | ManagedLayer::Sticky) {
let loc = (grab_state.window_offset.to_f64() + grab_state.location).as_local();
let size = window_geo.size.to_f64().as_local();
let output_geom = self
.cursor_output
.geometry()
.to_f64()
.to_local(&self.cursor_output);
let output_loc = output_geom.loc;
let output_size = output_geom.size;

grab_state.location.x = if (loc.x - output_loc.x).abs() < 10.0 {
output_loc.x - grab_state.window_offset.x as f64
} else if ((loc.x + size.w) - (output_loc.x + output_size.w)).abs() < 10.0 {
output_loc.x + output_size.w - grab_state.window_offset.x as f64 - size.w
} else {
grab_state.location.x
};
grab_state.location.y = if (loc.y - output_loc.y).abs() < 10.0 {
output_loc.y - grab_state.window_offset.y as f64
} else if ((loc.y + size.h) - (output_loc.y + output_size.h)).abs() < 10.0 {
output_loc.y + output_size.h - grab_state.window_offset.y as f64 - size.h
} else {
grab_state.location.y
};
dbg!(grab_state.location);
}

for output in shell.outputs() {
if let Some(overlap) = output.geometry().as_logical().intersection(window_geo) {
if self.window_outputs.insert(output.clone()) {
Expand Down
40 changes: 40 additions & 0 deletions src/shell/layout/floating/grabs/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct ResizeSurfaceGrab {
window: CosmicMapped,
edges: ResizeEdge,
output: Output,
initial_window_location: Point<i32, Local>,
initial_window_size: Size<i32, Logical>,
last_window_size: Size<i32, Logical>,
release: ReleaseMode,
Expand Down Expand Up @@ -91,6 +92,25 @@ impl ResizeSurfaceGrab {
}

new_window_width = (self.initial_window_size.w as f64 + dx) as i32;

// If the resizing vertical edge is close to our output's edge in the same direction, snap to it.
let output_geom = self.output.geometry().to_local(&self.output);
if self.edges.intersects(ResizeEdge::LEFT) {
if (self.initial_window_location.x - dx as i32 - output_geom.loc.x).abs() < 10 {
new_window_width = self.initial_window_size.w - output_geom.loc.x
+ self.initial_window_location.x;
}
} else {
if (self.initial_window_location.x + self.initial_window_size.w + dx as i32
- output_geom.loc.x
- output_geom.size.w)
.abs()
< 10
{
new_window_width =
output_geom.loc.x - self.initial_window_location.x + output_geom.size.w;
}
}
}

if self.edges.intersects(top_bottom) {
Expand All @@ -99,6 +119,25 @@ impl ResizeSurfaceGrab {
}

new_window_height = (self.initial_window_size.h as f64 + dy) as i32;

// If the resizing horizontal edge is close to our output's edge in the same direction, snap to it.
let output_geom = self.output.geometry().to_local(&self.output);
if self.edges.intersects(ResizeEdge::TOP) {
if (self.initial_window_location.y - dy as i32 - output_geom.loc.y).abs() < 10 {
new_window_height = self.initial_window_size.h - output_geom.loc.y
+ self.initial_window_location.y;
}
} else {
if (self.initial_window_location.y + self.initial_window_size.h + dy as i32
- output_geom.loc.y
- output_geom.size.h)
.abs()
< 10
{
new_window_height =
output_geom.loc.y - self.initial_window_location.y + output_geom.size.h;
}
}
}

let (min_size, max_size) = (self.window.min_size(), self.window.max_size());
Expand Down Expand Up @@ -414,6 +453,7 @@ impl ResizeSurfaceGrab {
window: mapped,
edges,
output,
initial_window_location,
initial_window_size,
last_window_size: initial_window_size,
release,
Expand Down

0 comments on commit 7a71b87

Please sign in to comment.