Skip to content

Commit 7a71b87

Browse files
committed
snap window edges to close output edges
1 parent 3b0aa9e commit 7a71b87

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/shell/grabs/moving.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,35 @@ impl MoveGrab {
383383

384384
let mut window_geo = self.window.geometry();
385385
window_geo.loc += location.to_i32_round() + grab_state.window_offset;
386+
387+
if matches!(self.previous, ManagedLayer::Floating | ManagedLayer::Sticky) {
388+
let loc = (grab_state.window_offset.to_f64() + grab_state.location).as_local();
389+
let size = window_geo.size.to_f64().as_local();
390+
let output_geom = self
391+
.cursor_output
392+
.geometry()
393+
.to_f64()
394+
.to_local(&self.cursor_output);
395+
let output_loc = output_geom.loc;
396+
let output_size = output_geom.size;
397+
398+
grab_state.location.x = if (loc.x - output_loc.x).abs() < 10.0 {
399+
output_loc.x - grab_state.window_offset.x as f64
400+
} else if ((loc.x + size.w) - (output_loc.x + output_size.w)).abs() < 10.0 {
401+
output_loc.x + output_size.w - grab_state.window_offset.x as f64 - size.w
402+
} else {
403+
grab_state.location.x
404+
};
405+
grab_state.location.y = if (loc.y - output_loc.y).abs() < 10.0 {
406+
output_loc.y - grab_state.window_offset.y as f64
407+
} else if ((loc.y + size.h) - (output_loc.y + output_size.h)).abs() < 10.0 {
408+
output_loc.y + output_size.h - grab_state.window_offset.y as f64 - size.h
409+
} else {
410+
grab_state.location.y
411+
};
412+
dbg!(grab_state.location);
413+
}
414+
386415
for output in shell.outputs() {
387416
if let Some(overlap) = output.geometry().as_logical().intersection(window_geo) {
388417
if self.window_outputs.insert(output.clone()) {

src/shell/layout/floating/grabs/resize.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub struct ResizeSurfaceGrab {
5858
window: CosmicMapped,
5959
edges: ResizeEdge,
6060
output: Output,
61+
initial_window_location: Point<i32, Local>,
6162
initial_window_size: Size<i32, Logical>,
6263
last_window_size: Size<i32, Logical>,
6364
release: ReleaseMode,
@@ -91,6 +92,25 @@ impl ResizeSurfaceGrab {
9192
}
9293

9394
new_window_width = (self.initial_window_size.w as f64 + dx) as i32;
95+
96+
// If the resizing vertical edge is close to our output's edge in the same direction, snap to it.
97+
let output_geom = self.output.geometry().to_local(&self.output);
98+
if self.edges.intersects(ResizeEdge::LEFT) {
99+
if (self.initial_window_location.x - dx as i32 - output_geom.loc.x).abs() < 10 {
100+
new_window_width = self.initial_window_size.w - output_geom.loc.x
101+
+ self.initial_window_location.x;
102+
}
103+
} else {
104+
if (self.initial_window_location.x + self.initial_window_size.w + dx as i32
105+
- output_geom.loc.x
106+
- output_geom.size.w)
107+
.abs()
108+
< 10
109+
{
110+
new_window_width =
111+
output_geom.loc.x - self.initial_window_location.x + output_geom.size.w;
112+
}
113+
}
94114
}
95115

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

101121
new_window_height = (self.initial_window_size.h as f64 + dy) as i32;
122+
123+
// If the resizing horizontal edge is close to our output's edge in the same direction, snap to it.
124+
let output_geom = self.output.geometry().to_local(&self.output);
125+
if self.edges.intersects(ResizeEdge::TOP) {
126+
if (self.initial_window_location.y - dy as i32 - output_geom.loc.y).abs() < 10 {
127+
new_window_height = self.initial_window_size.h - output_geom.loc.y
128+
+ self.initial_window_location.y;
129+
}
130+
} else {
131+
if (self.initial_window_location.y + self.initial_window_size.h + dy as i32
132+
- output_geom.loc.y
133+
- output_geom.size.h)
134+
.abs()
135+
< 10
136+
{
137+
new_window_height =
138+
output_geom.loc.y - self.initial_window_location.y + output_geom.size.h;
139+
}
140+
}
102141
}
103142

104143
let (min_size, max_size) = (self.window.min_size(), self.window.max_size());
@@ -414,6 +453,7 @@ impl ResizeSurfaceGrab {
414453
window: mapped,
415454
edges,
416455
output,
456+
initial_window_location,
417457
initial_window_size,
418458
last_window_size: initial_window_size,
419459
release,

0 commit comments

Comments
 (0)