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.

Crop overlay: A over the picture opens the aspect dial around the pointer

KnifeLemonbad5b7958406
diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md
index 8fd5af4e..7cdd35a9 100644
--- a/docs/ARCHITECTURE.md
+++ b/docs/ARCHITECTURE.md
@@ -136,10 +136,12 @@ multi-frame pictures refused, the EXIF thumbnail dropped), one history entry for
arrow-key nudges follow at once, and `PlayAnimations` (from `Motion.Enabled`) turns all of it off.
- `Controls/AspectDial` is a small donut of the ratios (free, original, 1:1, 4:3, 3:2, 16:9): each slice a
`RingSegment` button that answers only inside its wedge (`DonutGeometry.Wedge`, rounded by the softness), the hub
- swaps portrait and landscape. `AspectGlyph` is the dial in miniature on the toolbar button that opens it, and
- `CropSummaryRing` the end summary's donut (a slice per file).
+ swaps portrait and landscape. A with the pointer over the stage opens it around the pointer, as the donut opens,
+ growing from its middle and kept inside the window; A without a pointer there (the keyboard) and its toolbar button
+ open it over the button with a slice focused for the arrow keys. `AspectGlyph` is the dial in miniature on that
+ button, and `CropSummaryRing` the end summary's donut (a slice per file).
- `Controls/CropMath` holds the arithmetic (ratios and their portrait forms, nudging by whole pixels, the frame in
- pixels, where the size chip goes) and is unit tested.
+ pixels, where the size chip goes, where the dial opens) and is unit tested.
## Settings and data
diff --git a/src/Filee.App/Controls/CropMath.cs b/src/Filee.App/Controls/CropMath.cs
index bddf6121..f14cb460 100644
--- a/src/Filee.App/Controls/CropMath.cs
+++ b/src/Filee.App/Controls/CropMath.cs
@@ -1,5 +1,5 @@
// The crop overlay's arithmetic without a window: the ratios of the aspect dial, keyboard nudges, the frame's size in
-// pixels and where the size chip sits next to the frame.
+// pixels, where the size chip sits next to the frame and where the aspect dial opens.
using Avalonia;
@@ -79,4 +79,26 @@ public static class CropMath
y = frame.Top - ChipGap - chip.Height;
return new Point(x, Math.Clamp(y, area.Top, Math.Max(area.Top, area.Bottom - chip.Height)));
}
+
+ /// <summary>Space kept between the aspect dial and the edges of the stage.</summary>
+ public const double DialMargin = 8;
+
+ /// <summary>Space between the aspect dial and the toolbar button it grows out of.</summary>
+ public const double DialGap = 4;
+
+ /// <summary>
+ /// Top-left corner of the aspect dial (a square of <paramref name="side"/>) on a stage of <paramref name="stage"/>:
+ /// centred on <paramref name="pointer"/> when A was pressed with the pointer over the stage, as the donut opens
+ /// around the pointer; otherwise centred over its toolbar button, a little above <paramref name="buttonTop"/> (the
+ /// middle of the button's top edge). Kept inside the stage.
+ /// </summary>
+ public static Point DialOrigin(Point? pointer, Point buttonTop, Size stage, double side)
+ {
+ var (x, y) = pointer is { } p
+ ? (p.X - side / 2, p.Y - side / 2)
+ : (buttonTop.X - side / 2, buttonTop.Y - side - DialGap);
+ return new Point(
+ Math.Clamp(x, DialMargin, Math.Max(DialMargin, stage.Width - side - DialMargin)),
+ Math.Clamp(y, DialMargin, Math.Max(DialMargin, stage.Height - side - DialMargin)));
+ }
}
diff --git a/src/Filee.App/Views/CropOverlayWindow.axaml.cs b/src/Filee.App/Views/CropOverlayWindow.axaml.cs
index 7edd238d..068c2abe 100644
--- a/src/Filee.App/Views/CropOverlayWindow.axaml.cs
+++ b/src/Filee.App/Views/CropOverlayWindow.axaml.cs
@@ -12,14 +12,24 @@ namespace Filee.App.Views;
/// <summary>
/// The crop overlay's window: placement, keys (Enter saves, Esc closes, R resets, A opens the aspect dial, X swaps
-/// portrait and landscape, arrows move the frame, Shift × 10), the aspect dial over the toolbar and moving, sizing and
-/// maximizing the frameless window.
+/// portrait and landscape, arrows move the frame, Shift × 10), the aspect dial (around the pointer when A is pressed
+/// with the pointer over the stage, else over its toolbar button) and moving, sizing and maximizing the frameless
+/// window.
/// </summary>
public partial class CropOverlayWindow : Window
{
private CropOverlayViewModel? _vm;
private bool _dialByKeyboard;
+ /// <summary>Where the pointer is over the stage; null while it is outside the window.</summary>
+ private Point? _pointer;
+
+ /// <summary>Where the next opening of the dial centres it (A over the stage); null opens it over its button.</summary>
+ private Point? _openDialAt;
+
+ /// <summary>The centre of the open dial when it opened at the pointer.</summary>
+ private Point? _dialAt;
+
public CropOverlayWindow()
{
InitializeComponent();
@@ -30,6 +40,9 @@ public partial class CropOverlayWindow : Window
AddHandler(KeyDownEvent, OnKeyDownFirst, RoutingStrategies.Tunnel);
ResizeGrip.PointerPressed += OnGripPressed;
Stage.SizeChanged += (_, _) => PlaceDial();
+ // Every move over the stage, also over the canvas and the buttons that handle it.
+ Stage.AddHandler(PointerMovedEvent, (_, e) => _pointer = e.GetPosition(Stage), RoutingStrategies.Tunnel, handledEventsToo: true);
+ Stage.PointerExited += (_, _) => _pointer = null;
Closed += (_, _) => Motion.Changed -= OnMotionChanged;
}
@@ -100,6 +113,9 @@ public partial class CropOverlayWindow : Window
{
if (open)
{
+ // Around the pointer it grows from its middle, as the donut does; over the button, out of the button.
+ (_dialAt, _openDialAt) = (_openDialAt, null);
+ Dial.RenderTransformOrigin = _dialAt is null ? new RelativePoint(0.5, 1, RelativeUnit.Relative) : RelativePoint.Center;
PlaceDial();
Dial.IsVisible = true;
var byKeyboard = _dialByKeyboard;
@@ -121,15 +137,13 @@ public partial class CropOverlayWindow : Window
Dial.IsVisible = false;
}
- /// <summary>Centres the dial over its toolbar button, a little above it, inside the window.</summary>
+ /// <summary>Centres the dial on the pointer it opened at, or over its toolbar button, inside the window.</summary>
private void PlaceDial()
{
if (!DialButton.IsEffectivelyVisible || DialButton.TranslatePoint(new Point(DialButton.Bounds.Width / 2, 0), Stage) is not { } anchor)
return;
- var side = Dial.Side; // measured only once visible
- var x = Math.Clamp(anchor.X - side / 2, 8, Math.Max(8, Stage.Bounds.Width - side - 8));
- var y = Math.Max(8, anchor.Y - side - 4);
- Dial.Margin = new Thickness(x, y, 0, 0);
+ var origin = CropMath.DialOrigin(_dialAt, anchor, Stage.Bounds.Size, Dial.Side);
+ Dial.Margin = new Thickness(origin.X, origin.Y, 0, 0);
}
private void OnScrimPressed(object? sender, PointerPressedEventArgs e)
@@ -184,8 +198,12 @@ public partial class CropOverlayWindow : Window
e.Handled = true;
break;
case Key.A when plain && vm.IsEditing:
- _dialByKeyboard = true;
+ // With the pointer over the stage the dial opens around it, to be picked from with the mouse; without,
+ // over its button with a slice focused for the arrow keys.
+ _openDialAt = dialOpen ? null : _pointer;
+ _dialByKeyboard = _openDialAt is null;
vm.ToggleAspectDial();
+ _openDialAt = null; // used by now, or the dial didn't open (busy)
e.Handled = true;
break;
case Key.X when plain && vm.IsEditing:
diff --git a/tests/Filee.App.Tests/CropMathTests.cs b/tests/Filee.App.Tests/CropMathTests.cs
index d2ed1772..707b8362 100644
--- a/tests/Filee.App.Tests/CropMathTests.cs
+++ b/tests/Filee.App.Tests/CropMathTests.cs
@@ -80,4 +80,28 @@ public class CropMathTests
Assert.Equal(0, CropMath.SizeChipOrigin(new Rect(0, 100, 40, 40), chip, area).X);
Assert.Equal(700, CropMath.SizeChipOrigin(new Rect(760, 100, 40, 40), chip, area).X);
}
+
+ [Fact]
+ public void The_dial_opens_around_the_pointer_or_over_its_button_inside_the_stage()
+ {
+ var stage = new Size(1000, 700);
+ const double side = 246;
+ var button = new Point(400, 620);
+
+ // With the pointer over the stage: centred on it.
+ Assert.Equal(new Point(377, 177), CropMath.DialOrigin(new Point(500, 300), button, stage, side));
+
+ // Near an edge or a corner: moved in until it fits, with a margin.
+ Assert.Equal(new Point(CropMath.DialMargin, CropMath.DialMargin), CropMath.DialOrigin(new Point(20, 10), button, stage, side));
+ Assert.Equal(new Point(1000 - side - CropMath.DialMargin, 700 - side - CropMath.DialMargin),
+ CropMath.DialOrigin(new Point(990, 690), button, stage, side));
+
+ // Without a pointer (the keyboard): centred over the button, just above it.
+ Assert.Equal(new Point(277, 620 - side - CropMath.DialGap), CropMath.DialOrigin(null, button, stage, side));
+ // A button near the side still gets the whole dial.
+ Assert.Equal(CropMath.DialMargin, CropMath.DialOrigin(null, new Point(30, 620), stage, side).X);
+
+ // A stage smaller than the dial: from its top left corner.
+ Assert.Equal(new Point(CropMath.DialMargin, CropMath.DialMargin), CropMath.DialOrigin(new Point(100, 100), button, new Size(200, 200), side));
+ }
}
diff --git a/tests/Filee.App.Tests/CropOverlayTests.cs b/tests/Filee.App.Tests/CropOverlayTests.cs
index 067669b6..9706f0f2 100644
--- a/tests/Filee.App.Tests/CropOverlayTests.cs
+++ b/tests/Filee.App.Tests/CropOverlayTests.cs
@@ -3,10 +3,12 @@
using System.IO;
using Avalonia;
+using Avalonia.Controls;
using Avalonia.Headless;
using Avalonia.Headless.XUnit;
using Avalonia.Input;
using Avalonia.Threading;
+using Avalonia.VisualTree;
using Filee.App.Controls;
using Filee.App.Services;
using Filee.App.ViewModels;
@@ -155,6 +157,80 @@ public class CropOverlayTests
});
}
+ [AvaloniaTheory]
+ [InlineData("en", false)]
+ [InlineData("en", true)]
+ [InlineData("ar", false)]
+ public void A_over_the_picture_opens_the_dial_around_the_pointer(string language, bool dark)
+ {
+ WithOverlay(language, dark, ["harbour.jpg", "two.jpg"], (window, vm, suffix) =>
+ {
+ var canvas = window.CropCanvas;
+ var dial = window.AspectDial;
+ var origin = (Vector)canvas.TranslatePoint(default, window)!.Value;
+ var image = canvas.CalculateImageBounds().Translate(origin);
+ Point DialCentre() => dial.TranslatePoint(new Point(dial.Bounds.Width / 2, dial.Bounds.Height / 2), window)!.Value;
+
+ // No pointer over the stage yet (the keyboard): over its button, as before.
+ Press(window, Key.A);
+ Settle(window);
+ Assert.True(vm.IsAspectDialOpen);
+ var button = window.GetVisualDescendants().OfType<Button>().Single(b => b.Name == "DialButton");
+ var buttonTop = button.TranslatePoint(new Point(button.Bounds.Width / 2, 0), window)!.Value;
+ Assert.Equal(buttonTop.X, DialCentre().X, 0.5);
+ Assert.Equal(buttonTop.Y - CropMath.DialGap, dial.TranslatePoint(new Point(0, dial.Bounds.Height), window)!.Value.Y, 0.5);
+ Press(window, Key.Escape);
+ Assert.False(vm.IsAspectDialOpen);
+ Assert.True(window.IsVisible);
+
+ // The pointer over the picture: A opens the dial around it.
+ var at = new Point(image.X + image.Width * 0.62, image.Y + image.Height * 0.4);
+ window.MouseMove(at);
+ Press(window, Key.A);
+ Settle(window);
+ Assert.True(vm.IsAspectDialOpen);
+ Assert.Equal(at.X, DialCentre().X, 0.5);
+ Assert.Equal(at.Y, DialCentre().Y, 0.5);
+ Save(window, $"crop-dial-pointer-{suffix}.png");
+
+ // The arrows go round the slices, not the frame; A again closes it.
+ var frame = canvas.NormalizedCropRect;
+ Press(window, Key.Right);
+ Assert.Equal(frame, canvas.NormalizedCropRect);
+ Press(window, Key.A);
+ Assert.False(vm.IsAspectDialOpen);
+
+ // Picked with a click: the slice under the pointer.
+ Press(window, Key.A);
+ Settle(window);
+ var square = dial.Segments.ToList().FindIndex(s => s.Mode == "1:1");
+ var onSquare = dial.TranslatePoint(DonutGeometry.PointAt(new Point(dial.Bounds.Width / 2, dial.Bounds.Height / 2),
+ (dial.InnerRadius + dial.OuterRadius) / 2, DonutGeometry.MidAngle(square, dial.Segments.Count())), window)!.Value;
+ window.MouseMove(onSquare);
+ window.MouseDown(onSquare, MouseButton.Left);
+ window.MouseUp(onSquare, MouseButton.Left);
+ Pump();
+ Assert.Equal("1:1", vm.SelectedAspect);
+ Assert.False(vm.IsAspectDialOpen);
+
+ // Near a corner of the window: moved in until the whole dial shows.
+ var corner = new Point(window.Bounds.Width - 30, 30);
+ window.MouseMove(corner);
+ Press(window, Key.A);
+ Settle(window);
+ var stage = dial.GetVisualParent<Visual>()!;
+ var bounds = new Rect(dial.TranslatePoint(default, stage)!.Value, dial.Bounds.Size);
+ Assert.True(new Rect(stage.Bounds.Size).Deflate(CropMath.DialMargin - 0.5).Contains(bounds), $"{bounds} outside {stage.Bounds}");
+ Press(window, Key.Escape);
+
+ // The toolbar button opens it over the button again, whatever A did before.
+ window.MouseMove(at);
+ vm.ToggleAspectDial();
+ Settle(window);
+ Assert.Equal(buttonTop.X, DialCentre().X, 0.5);
+ });
+ }
+
[AvaloniaFact]
public void Fixed_ratios_follow_the_pictures_orientation_and_swap()
{