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.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Crop: the ratio picked for one picture carries on to the next (#542)
cre8ive-marketingbebeb1881424
diff --git a/src/Filee.App/Controls/CropCanvas.cs b/src/Filee.App/Controls/CropCanvas.csindex c51728da..9b7cfad6 100644--- a/src/Filee.App/Controls/CropCanvas.cs+++ b/src/Filee.App/Controls/CropCanvas.cs@@ -161,9 +161,10 @@ public sealed class CropCanvas : Control// The picture is never mirrored, also in a right-to-left UI.public CropCanvas() => FlowDirection = FlowDirection.LeftToRight;- public void ResetCrop()+ /// <summary>Starts the frame over: the whole picture, or <paramref name="frame"/> (a ratio carried from the last picture).</summary>+ public void ResetCrop(Rect? frame = null){- SetRect(new Rect(0, 0, 1, 1), animate: true);+ SetRect(frame ?? new Rect(0, 0, 1, 1), animate: true);}public void SetAspectRatio(double ratio)diff --git a/src/Filee.App/Controls/CropMath.cs b/src/Filee.App/Controls/CropMath.csindex b7bca2e4..2ad1a721 100644--- a/src/Filee.App/Controls/CropMath.cs+++ b/src/Filee.App/Controls/CropMath.cs@@ -43,6 +43,19 @@ public static class CropMathreturn $"{parts[1]}:{parts[0]}";}+ /// <summary>+ /// The largest frame of <paramref name="ratio"/> (width / height in pixels) on a picture, centred, in parts of the+ /// picture; the whole picture for 0 (free).+ /// </summary>+ public static Rect Centered(double ratio, int width, int height)+ {+ if (ratio <= 0 || width <= 0 || height <= 0)+ return new Rect(0, 0, 1, 1);+ var w = Math.Min(1, ratio * height / width);+ var h = Math.Min(1, width / (ratio * height));+ return new Rect((1 - w) / 2, (1 - h) / 2, w, h);+ }+/// <summary>The frame in pixels of the picture (at least 1 × 1).</summary>public static PixelSize PixelSize(Rect normalized, int width, int height) =>new(Math.Max(1, (int)Math.Round(normalized.Width * width)), Math.Max(1, (int)Math.Round(normalized.Height * height)));diff --git a/src/Filee.App/ViewModels/CropOverlayViewModel.cs b/src/Filee.App/ViewModels/CropOverlayViewModel.csindex 2ab53d48..da9e1993 100644--- a/src/Filee.App/ViewModels/CropOverlayViewModel.cs+++ b/src/Filee.App/ViewModels/CropOverlayViewModel.cs@@ -134,9 +134,18 @@ public sealed partial class CropOverlayViewModel : ObservableObject, IDisposablepublic string OrientationText => _loc[IsPortrait ? "crop.portrait" : "crop.landscape"];public event Action? CloseRequested;- public event Action? ResetRequested;++ /// <summary>The frame starts over: the whole picture, or the frame given (a ratio carried from the last picture).</summary>+ public event Action<Rect?>? ResetRequested;public event Action<double>? AspectRatioChanged;+ /// <summary>+ /// The ratio the user picked, kept for the next pictures of the batch (#542), and its orientation; null while none+ /// was picked or after Reset, so the next picture starts free.+ /// </summary>+ private string? _batchAspect;+ private bool _batchPortrait;+public Rect CurrentCropRect { get; set; } = new(0.05, 0.05, 0.9, 0.9);public CropOverlayViewModel(IReadOnlyList<string> files, Preset preset, ILocalizer loc, UserDataStore store, ILogger? log = null)@@ -186,14 +195,17 @@ public sealed partial class CropOverlayViewModel : ObservableObject, IDisposableOriginalWidth = origW;OriginalHeight = origH;- SelectedAspect = "free";- IsPortrait = origH > origW;+ // A ratio picked for an earlier picture carries on (#542): the frame starts as large as it fits, centred,+ // in the orientation picked for it. Free (nothing picked yet) starts on the whole picture.+ var mode = _batchAspect ?? "free";+ SelectedAspect = mode;+ IsPortrait = CropMath.CanSwap(mode) ? _batchPortrait : origH > origW;IsAspectDialOpen = false;- AspectRatio = 0;- AspectRatioChanged?.Invoke(0);- CurrentCropRect = new Rect(0.05, 0.05, 0.9, 0.9);- UpdateDimensions(CurrentCropRect);- ResetRequested?.Invoke();+ AspectRatio = CropMath.Ratio(mode, IsPortrait, origW, origH);+ AspectRatioChanged?.Invoke(AspectRatio);+ var frame = CropMath.Centered(AspectRatio, origW, origH);+ UpdateDimensions(frame);+ ResetRequested?.Invoke(frame);}catch (Exception ex){@@ -303,11 +315,12 @@ public sealed partial class CropOverlayViewModel : ObservableObject, IDisposable[RelayCommand]public void Reset(){+ _batchAspect = null; // the next pictures start free againSelectedAspect = "free";IsPortrait = OriginalHeight > OriginalWidth;AspectRatio = 0;AspectRatioChanged?.Invoke(0);- ResetRequested?.Invoke();+ ResetRequested?.Invoke(null);}/// <summary>Cancel, or Close once finished. A crop still saving finishes and goes into the history.</summary>@@ -353,6 +366,7 @@ public sealed partial class CropOverlayViewModel : ObservableObject, IDisposableprivate void ApplyAspect(string mode){+ (_batchAspect, _batchPortrait) = (mode == "free" ? null : mode, IsPortrait);SelectedAspect = mode;AspectRatio = CropMath.Ratio(mode, IsPortrait, OriginalWidth, OriginalHeight);AspectRatioChanged?.Invoke(AspectRatio);diff --git a/src/Filee.App/Views/CropOverlayWindow.axaml.cs b/src/Filee.App/Views/CropOverlayWindow.axaml.csindex 068c2abe..4894e304 100644--- a/src/Filee.App/Views/CropOverlayWindow.axaml.cs+++ b/src/Filee.App/Views/CropOverlayWindow.axaml.cs@@ -87,7 +87,7 @@ public partial class CropOverlayWindow : Window{_vm = vm;vm.CloseRequested += Close;- vm.ResetRequested += () => Canvas.ResetCrop();+ vm.ResetRequested += frame => Canvas.ResetCrop(frame);vm.AspectRatioChanged += ratio => Canvas.SetAspectRatio(ratio);vm.PropertyChanged += OnViewModelChanged;Canvas.CropRectChanged += rect => vm.UpdateDimensions(rect);diff --git a/tests/Filee.App.Tests/CropMathTests.cs b/tests/Filee.App.Tests/CropMathTests.csindex 7a021537..3b5975cc 100644--- a/tests/Filee.App.Tests/CropMathTests.cs+++ b/tests/Filee.App.Tests/CropMathTests.cs@@ -80,6 +80,21 @@ public class CropMathTestsAssert.Equal(new PixelSize(1, 1), CropMath.PixelSize(new Rect(0, 0, 0.0001, 0), 1600, 1000));}+ [Fact]+ public void A_carried_ratio_starts_as_the_largest_centred_frame()+ {+ // 4:5 on a landscape 1600 × 1000: the whole height, 800 wide in the middle.+ Assert.Equal(new Rect(0.25, 0, 0.5, 1), CropMath.Centered(4.0 / 5, 1600, 1000));+ // 16:9 on an upright 1000 × 1600: the whole width, 562.5 high in the middle.+ var wide = CropMath.Centered(16.0 / 9, 1000, 1600);+ Assert.Equal((0, 1), (wide.X, wide.Width));+ Assert.Equal(562.5 / 1600, wide.Height, 6);+ Assert.Equal((1 - wide.Height) / 2, wide.Y, 6);+ // The picture's own ratio, and free, fill it.+ Assert.Equal(new Rect(0, 0, 1, 1), CropMath.Centered(1.6, 1600, 1000));+ Assert.Equal(new Rect(0, 0, 1, 1), CropMath.Centered(0, 1600, 1000));+ }+[Fact]public void The_size_chip_sits_under_the_frame_inside_it_or_above_it(){diff --git a/tests/Filee.App.Tests/CropOverlayTests.cs b/tests/Filee.App.Tests/CropOverlayTests.csindex 834e61d6..e436a7e4 100644--- a/tests/Filee.App.Tests/CropOverlayTests.cs+++ b/tests/Filee.App.Tests/CropOverlayTests.cs@@ -329,27 +329,67 @@ public class CropOverlayTests}[AvaloniaFact]- public void The_next_file_starts_free_and_full_in_view_and_canvas_alike()+ public void The_next_file_keeps_the_chosen_ratio_as_large_as_it_fits_centred_in_view_and_canvas_alike(){+ // #542: a batch for Instagram picks 4:5 once, not for every picture.WithOverlay("en", false, ["harbour.jpg", "tall.jpg"], (window, vm, _suffix) =>{vm.SetAspect("16:9");- vm.SwapOrientation();+ vm.SwapOrientation(); // 9:16_ = vm.CropAndSaveAsync();WaitFor(() => vm.CurrentIndex == 1 && !vm.IsBusy && vm.PreviewImage is not null);Pump();+ Assert.Equal("16:9", vm.SelectedAspect);+ Assert.True(vm.IsPortrait);+ Assert.Equal("9:16", vm.AspectLabel);+ Assert.Equal(9.0 / 16, vm.AspectRatio, 6);+ Assert.Equal(9.0 / 16, window.CropCanvas.AspectRatio, 6);+ // 1000 × 1600: the whole height, 900 wide, in the middle.+ var frame = new Rect(0.05, 0, 0.9, 1);+ AssertClose(frame, window.CropCanvas.NormalizedCropRect);+ AssertClose(frame, vm.CurrentCropRect);+ Assert.Equal("900 × 1600 px", vm.DimensionsText);+ Assert.Equal(AppHost.Get<ILocalizer>()["crop.save"], vm.SaveText);+ });+ }++ [AvaloniaFact]+ public void A_batch_starts_free_until_a_ratio_is_picked_and_again_after_reset()+ {+ WithOverlay("en", false, ["harbour.jpg", "tall.jpg", "two.jpg"], (window, vm, _suffix) =>+ {+ // Nothing picked: the next picture starts free, on the whole picture.+ _ = vm.CropAndSaveAsync();+ WaitFor(() => vm.CurrentIndex == 1 && !vm.IsBusy && vm.PreviewImage is not null);+ Pump();Assert.Equal("free", vm.SelectedAspect);- Assert.Equal(0, vm.AspectRatio);Assert.Equal(0, window.CropCanvas.AspectRatio);Assert.True(vm.IsPortrait); // the second picture is uprightAssert.Equal(new Rect(0, 0, 1, 1), window.CropCanvas.NormalizedCropRect);- Assert.Equal(new Rect(0, 0, 1, 1), vm.CurrentCropRect);Assert.Equal("1000 × 1600 px", vm.DimensionsText);- Assert.Equal(AppHost.Get<ILocalizer>()["crop.save"], vm.SaveText);++ // Picked, then R (Reset): back to free for this picture and the next.+ vm.SetAspect("4:3");+ vm.Reset();+ _ = vm.CropAndSaveAsync();+ WaitFor(() => vm.CurrentIndex == 2 && !vm.IsBusy && vm.PreviewImage is not null);+ Pump();+ Assert.Equal("free", vm.SelectedAspect);+ Assert.False(vm.IsPortrait);+ Assert.Equal(new Rect(0, 0, 1, 1), window.CropCanvas.NormalizedCropRect);+ Assert.Equal("1600 × 1000 px", vm.DimensionsText);});}+ private static void AssertClose(Rect expected, Rect actual)+ {+ Assert.Equal(expected.X, actual.X, 3);+ Assert.Equal(expected.Y, actual.Y, 3);+ Assert.Equal(expected.Width, actual.Width, 3);+ Assert.Equal(expected.Height, actual.Height, 3);+ }+// ───────────────────────── Helpers ─────────────────────────private static void WithOverlay(string language, bool dark, string[] names, Action<CropOverlayWindow, CropOverlayViewModel, string> act)