@@ -376,108 +376,9 @@ impl MonitorView { |
| 376 | 376 | return; |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | | - let mut snap_x: Option<i32> = None; |
| 380 | | - let mut snap_y: Option<i32> = None; |
| 381 | | - |
| 382 | | - let dragged = self.monitors[dragged_idx].scaled_rect; |
| 383 | | - |
| 384 | | - // First pass: threshold-based snapping for fine alignment |
| 385 | | - for (i, other) in self.monitors.iter().enumerate() { |
| 386 | | - if i == dragged_idx { |
| 387 | | - continue; |
| 388 | | - } |
| 389 | | - |
| 390 | | - let other_rect = other.scaled_rect; |
| 391 | | - |
| 392 | | - // Vertical snapping (x-axis) |
| 393 | | - // Snap left edge to right edge of other |
| 394 | | - let dist = (dragged.x - (other_rect.x + other_rect.width as i32)).abs(); |
| 395 | | - if dist < SNAP_THRESHOLD && snap_x.map_or(true, |sx| dist < (dragged.x - sx).abs()) { |
| 396 | | - snap_x = Some(other_rect.x + other_rect.width as i32); |
| 397 | | - } |
| 398 | | - |
| 399 | | - // Snap right edge to left edge of other |
| 400 | | - let dist = ((dragged.x + dragged.width as i32) - other_rect.x).abs(); |
| 401 | | - if dist < SNAP_THRESHOLD && snap_x.map_or(true, |sx| dist < (dragged.x - sx).abs()) { |
| 402 | | - snap_x = Some(other_rect.x - dragged.width as i32); |
| 403 | | - } |
| 404 | | - |
| 405 | | - // Snap left to left (align) |
| 406 | | - let dist = (dragged.x - other_rect.x).abs(); |
| 407 | | - if dist < SNAP_THRESHOLD && snap_x.map_or(true, |sx| dist < (dragged.x - sx).abs()) { |
| 408 | | - snap_x = Some(other_rect.x); |
| 409 | | - } |
| 410 | | - |
| 411 | | - // Horizontal snapping (y-axis) |
| 412 | | - // Snap top edge to bottom edge of other |
| 413 | | - let dist = (dragged.y - (other_rect.y + other_rect.height as i32)).abs(); |
| 414 | | - if dist < SNAP_THRESHOLD && snap_y.map_or(true, |sy| dist < (dragged.y - sy).abs()) { |
| 415 | | - snap_y = Some(other_rect.y + other_rect.height as i32); |
| 416 | | - } |
| 417 | | - |
| 418 | | - // Snap bottom edge to top edge of other |
| 419 | | - let dist = ((dragged.y + dragged.height as i32) - other_rect.y).abs(); |
| 420 | | - if dist < SNAP_THRESHOLD && snap_y.map_or(true, |sy| dist < (dragged.y - sy).abs()) { |
| 421 | | - snap_y = Some(other_rect.y - dragged.height as i32); |
| 422 | | - } |
| 423 | | - |
| 424 | | - // Snap top to top (align) |
| 425 | | - let dist = (dragged.y - other_rect.y).abs(); |
| 426 | | - if dist < SNAP_THRESHOLD && snap_y.map_or(true, |sy| dist < (dragged.y - sy).abs()) { |
| 427 | | - snap_y = Some(other_rect.y); |
| 428 | | - } |
| 429 | | - } |
| 430 | | - |
| 431 | | - // Apply threshold snaps |
| 432 | | - if let Some(x) = snap_x { |
| 433 | | - self.monitors[dragged_idx].scaled_rect.x = x; |
| 434 | | - } |
| 435 | | - if let Some(y) = snap_y { |
| 436 | | - self.monitors[dragged_idx].scaled_rect.y = y; |
| 437 | | - } |
| 438 | | - |
| 439 | | - // Second pass: close gaps - ensure monitor is adjacent to at least one other |
| 379 | + // Always snap to nearest to ensure adjacency, using directional awareness |
| 440 | 380 | let dragged = self.monitors[dragged_idx].scaled_rect; |
| 441 | | - if !self.is_adjacent_to_any(dragged_idx) { |
| 442 | | - self.snap_to_nearest(dragged_idx, dragged); |
| 443 | | - } |
| 444 | | - } |
| 445 | | - |
| 446 | | - /// Check if a monitor is adjacent (touching) any other monitor. |
| 447 | | - fn is_adjacent_to_any(&self, monitor_idx: usize) -> bool { |
| 448 | | - let rect = self.monitors[monitor_idx].scaled_rect; |
| 449 | | - |
| 450 | | - for (i, other) in self.monitors.iter().enumerate() { |
| 451 | | - if i == monitor_idx { |
| 452 | | - continue; |
| 453 | | - } |
| 454 | | - |
| 455 | | - if self.rects_adjacent(rect, other.scaled_rect) { |
| 456 | | - return true; |
| 457 | | - } |
| 458 | | - } |
| 459 | | - false |
| 460 | | - } |
| 461 | | - |
| 462 | | - /// Check if two rects are adjacent (touching edges, with possible overlap on perpendicular axis). |
| 463 | | - fn rects_adjacent(&self, a: Rect, b: Rect) -> bool { |
| 464 | | - // Check if they overlap on one axis and touch on the other |
| 465 | | - let a_right = a.x + a.width as i32; |
| 466 | | - let a_bottom = a.y + a.height as i32; |
| 467 | | - let b_right = b.x + b.width as i32; |
| 468 | | - let b_bottom = b.y + b.height as i32; |
| 469 | | - |
| 470 | | - // Horizontal adjacency: a's right touches b's left OR a's left touches b's right |
| 471 | | - // AND they overlap vertically |
| 472 | | - let horiz_touch = a_right == b.x || a.x == b_right; |
| 473 | | - let vert_overlap = a.y < b_bottom && a_bottom > b.y; |
| 474 | | - |
| 475 | | - // Vertical adjacency: a's bottom touches b's top OR a's top touches b's bottom |
| 476 | | - // AND they overlap horizontally |
| 477 | | - let vert_touch = a_bottom == b.y || a.y == b_bottom; |
| 478 | | - let horiz_overlap = a.x < b_right && a_right > b.x; |
| 479 | | - |
| 480 | | - (horiz_touch && vert_overlap) || (vert_touch && horiz_overlap) |
| 381 | + self.snap_to_nearest(dragged_idx, dragged); |
| 481 | 382 | } |
| 482 | 383 | |
| 483 | 384 | /// Snap a monitor to be adjacent to the nearest other monitor. |