Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Snap the crop frame to the picture's centre lines while moving it (#369)

cre8ive-marketinge9bf17399c95
diff --git a/src/Filee.App/Controls/CropCanvas.cs b/src/Filee.App/Controls/CropCanvas.cs
index 86cc920a..c51728da 100644
--- a/src/Filee.App/Controls/CropCanvas.cs
+++ b/src/Filee.App/Controls/CropCanvas.cs
@@ -103,6 +103,9 @@ public sealed class CropCanvas : Control
private CropHandle _hoverHandle = CropHandle.None;
private Point _dragStartPoint;
private Rect _dragStartCrop;
+
+ // The frame being moved sits on the picture's vertical / horizontal centre line: its guide is drawn.
+ private bool _snappedX, _snappedY;
private const double MinSizePx = 24.0;
// ───── Motion: tweens evaluated against the clock at each render, so a lost frame never leaves one half done ─────
@@ -409,6 +412,7 @@ public sealed class CropCanvas : Control
context.DrawImage(SourceImage, source, imgBounds);
DrawGrid(context, crop, _grid.Value(now));
+ DrawCentreGuides(context, imgBounds);
DrawFrame(context, crop, now);
DrawSizeChip(context, crop, imgBounds, bounds);
}
@@ -460,6 +464,20 @@ public sealed class CropCanvas : Control
}
}
+ /// <summary>The picture's centre line the moved frame snapped to, across the whole picture.</summary>
+ private void DrawCentreGuides(DrawingContext context, Rect picture)
+ {
+ if (_activeHandle != CropHandle.Body || !(_snappedX || _snappedY))
+ return;
+ var pen = new Pen(AccentBrush ?? Brushes.MediumPurple, 1, DashStyle.Dash);
+ var x = Math.Round(picture.Center.X) + 0.5;
+ var y = Math.Round(picture.Center.Y) + 0.5;
+ if (_snappedX)
+ context.DrawLine(pen, new Point(x, picture.Top), new Point(x, picture.Bottom));
+ if (_snappedY)
+ context.DrawLine(pen, new Point(picture.Left, y), new Point(picture.Right, y));
+ }
+
private void DrawFrame(DrawingContext context, Rect crop, long now)
{
var accent = AccentBrush ?? Brushes.MediumPurple;
@@ -738,6 +756,8 @@ public sealed class CropCanvas : Control
var newX = SafeClamp(_dragStartCrop.X + deltaX, imgBounds.Left, Math.Max(imgBounds.Left, imgBounds.Right - _dragStartCrop.Width));
var newY = SafeClamp(_dragStartCrop.Y + deltaY, imgBounds.Top, Math.Max(imgBounds.Top, imgBounds.Bottom - _dragStartCrop.Height));
+ (newX, _snappedX) = CropMath.SnapToCentre(newX, _dragStartCrop.Width, imgBounds.Left, imgBounds.Right);
+ (newY, _snappedY) = CropMath.SnapToCentre(newY, _dragStartCrop.Height, imgBounds.Top, imgBounds.Bottom);
SetCropFromScreen(new Rect(newX, newY, _dragStartCrop.Width, _dragStartCrop.Height), imgBounds);
return;
@@ -897,6 +917,7 @@ public sealed class CropCanvas : Control
{
_dragStartPoint = pt;
_dragStartCrop = crop;
+ _snappedX = _snappedY = false;
e.Pointer.Capture(this);
e.Handled = true;
PopHandle(_activeHandle);
diff --git a/src/Filee.App/Controls/CropMath.cs b/src/Filee.App/Controls/CropMath.cs
index f14cb460..14f06064 100644
--- a/src/Filee.App/Controls/CropMath.cs
+++ b/src/Filee.App/Controls/CropMath.cs
@@ -60,6 +60,22 @@ public static class CropMath
return new Rect((double)x / width, (double)y / height, normalized.Width, normalized.Height);
}
+ /// <summary>How close (screen pixels) the frame's centre has to come to the picture's centre line to snap to it.</summary>
+ public const double SnapDistance = 8;
+
+ /// <summary>
+ /// One side of a frame being dragged (issue #369): its start moved so its centre sits on the centre of
+ /// <paramref name="min"/>…<paramref name="max"/> when it comes within <see cref="SnapDistance"/> of it. A frame as
+ /// wide as the picture has nowhere to go and never counts as snapped.
+ /// </summary>
+ public static (double Start, bool Snapped) SnapToCentre(double start, double length, double min, double max)
+ {
+ var centre = (min + max) / 2;
+ if (length >= max - min - 0.5 || Math.Abs(start + length / 2 - centre) > SnapDistance)
+ return (start, false);
+ return (centre - length / 2, true);
+ }
+
/// <summary>Space between the frame and the size chip.</summary>
public const double ChipGap = 10;
diff --git a/tests/Filee.App.Tests/CropMathTests.cs b/tests/Filee.App.Tests/CropMathTests.cs
index 707b8362..426d2ad0 100644
--- a/tests/Filee.App.Tests/CropMathTests.cs
+++ b/tests/Filee.App.Tests/CropMathTests.cs
@@ -1,5 +1,5 @@
-// The crop overlay's arithmetic: ratios and their portrait forms, keyboard nudges, the frame in pixels and where the
-// size chip goes.
+// The crop overlay's arithmetic: ratios and their portrait forms, keyboard nudges, snapping to the centre, the frame
+// in pixels and where the size chip goes.
using Avalonia;
using Filee.App.Controls;
@@ -22,6 +22,24 @@ public class CropMathTests
Assert.Equal(expected, CropMath.Ratio(mode, portrait, 1600, 1000), 6);
}
+ [Theory]
+ // A 100 px wide frame over a 0…1000 px picture: centred at 500 when its start is 450.
+ [InlineData(450, 450, true)] // already centred
+ [InlineData(443, 450, true)] // 7 px off: pulled in
+ [InlineData(458, 450, true)] // 8 px off: still pulled in
+ [InlineData(459, 459, false)] // 9 px off: left alone
+ [InlineData(100, 100, false)] // far away
+ public void A_moved_frame_snaps_to_the_centre_line(double start, double expected, bool snapped)
+ {
+ Assert.Equal((expected, snapped), CropMath.SnapToCentre(start, 100, 0, 1000));
+ }
+
+ [Fact]
+ public void A_frame_as_wide_as_the_picture_never_counts_as_snapped()
+ {
+ Assert.Equal((0.0, false), CropMath.SnapToCentre(0, 1000, 0, 1000));
+ }
+
[Fact]
public void Only_fixed_ratios_that_arent_square_swap()
{
diff --git a/tests/Filee.App.Tests/CropOverlayTests.cs b/tests/Filee.App.Tests/CropOverlayTests.cs
index 90d47a32..7afe5eb7 100644
--- a/tests/Filee.App.Tests/CropOverlayTests.cs
+++ b/tests/Filee.App.Tests/CropOverlayTests.cs
@@ -111,6 +111,32 @@ public class CropOverlayTests
});
}
+ [AvaloniaFact]
+ public void A_frame_moved_near_the_middle_snaps_to_the_centre_lines()
+ {
+ WithOverlay("en", false, ["harbour.jpg"], (window, vm, _suffix) =>
+ {
+ var canvas = window.CropCanvas;
+ canvas.NormalizedCropRect = new Rect(0.1, 0.1, 0.5, 0.5); // centre at 35 %, 35 %
+ Settle(window);
+ var image = canvas.CalculateImageBounds();
+ var origin = canvas.TranslatePoint(default, window)!.Value;
+ var start = canvas.GetScreenCropRect(image).Center + (Vector)origin;
+ // To 3 screen pixels short of the picture's centre on both axes: inside the snap distance.
+ var target = image.Center + (Vector)origin - new Vector(3, 3);
+ window.MouseDown(start, MouseButton.Left);
+ window.MouseMove(start + (target - start) / 2);
+ window.MouseMove(target);
+ Pump();
+ Save(window, "crop-snap-en.png");
+ window.MouseUp(target, MouseButton.Left);
+
+ var frame = canvas.NormalizedCropRect;
+ Assert.Equal(0.5, frame.X + frame.Width / 2, 3);
+ Assert.Equal(0.5, frame.Y + frame.Height / 2, 3);
+ });
+ }
+
[AvaloniaFact]
public void Keys_nudge_the_frame_open_the_dial_reset_and_close()
{