feat(editor): visual crop, rotate/mirror/flip/straighten, colour looks (owner review 1 phase 4) (#26)
CI / Build and test (push) Successful in 8m19s
CI / Windows tests and installer (push) Skipped
CI / YAML check (push) Successful in 7s
Release / Build, test and publish (push) Successful in 12m9s
CI / PostgreSQL 16 integration (push) Successful in 7m49s
CI / PostgreSQL 17 integration (push) Successful in 7m19s
CI / Docker image (push) Successful in 46s
Release / Server image (push) Successful in 1m14s

Owner review 1 R5 + R6. Camera rotation, mirror, flip, straighten and crop handles locked to the output; an Exposure Look group; Export output size ("proportions win"); settings format 2 with older-node gates. Also fixes project text and hashes differing between Windows and Linux.

Closes #19
Closes #20

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
This commit was merged in pull request #26.
This commit is contained in:
Alby96andClaude Opus 5.5 authored and Alby96 committed 2026-11-05 01:09:09 +01:00
1 parent db389a54f2
commit 188b2c2e55
355 files changed
+10549 -1061

No files matched your search

+5
View File
@@ -39,6 +39,7 @@
<ui:IconData x:Key="Icon.Eye" Stroke="M1.5 8 C3.5 4 12.5 4 14.5 8 C12.5 12 3.5 12 1.5 8 Z M5.8 8 A2.2 2.2 0 1 0 10.2 8 A2.2 2.2 0 1 0 5.8 8 Z" />
<ui:IconData x:Key="Icon.Filmstrip" Stroke="M3.0 2.5 H13.0 A1.5 1.5 0 0 1 14.5 4.0 V12.0 A1.5 1.5 0 0 1 13.0 13.5 H3.0 A1.5 1.5 0 0 1 1.5 12.0 V4.0 A1.5 1.5 0 0 1 3.0 2.5 Z M4.5 2.5 V13.5 M11.5 2.5 V13.5 M1.5 6 H4.5 M1.5 10 H4.5 M11.5 6 H14.5 M11.5 10 H14.5" />
<ui:IconData x:Key="Icon.Filter" Stroke="M1.5 2.5 H14.5 L9.5 8.5 V13.5 L6.5 12 V8.5 Z" />
<ui:IconData x:Key="Icon.Flip" Stroke="M1.5 8 H3 M5.5 8 H7.5 M10 8 H12 M14 8 H14.5 M4 6 H12.5 V1.5 Z" Fill="M4 10 L12.5 14.5 V10 Z" />
<ui:IconData x:Key="Icon.Folder" Stroke="M1.5 4 A1 1 0 0 1 2.5 3 H6 L7.5 4.5 H13.5 A1 1 0 0 1 14.5 5.5 V12.5 A1 1 0 0 1 13.5 13.5 H2.5 A1 1 0 0 1 1.5 12.5 Z" />
<ui:IconData x:Key="Icon.FolderOpen" Stroke="M1.5 12.5 V4 A1 1 0 0 1 2.5 3 H6 L7.5 4.5 H12.5 V6.5 M1.5 12.5 L3.5 7 H14.5 L12.5 13.5 H2.5 A1 1 0 0 1 1.5 12.5 Z" />
<ui:IconData x:Key="Icon.Forward" Stroke="M3 8 H13 M9 4 L13 8 L9 12" IsDirectional="True" />
@@ -60,6 +61,7 @@
<ui:IconData x:Key="Icon.Logs" Stroke="M3.5 1.5 H9.5 L12.5 4.5 V14.5 H3.5 Z M5.5 7 H10.5 M5.5 9.5 H10.5 M5.5 12 H8.5" IsDirectional="True" />
<ui:IconData x:Key="Icon.Menu" Stroke="M2.5 4 H13.5 M2.5 8 H13.5 M2.5 12 H13.5" />
<ui:IconData x:Key="Icon.Minus" Stroke="M3 8 H13" />
<ui:IconData x:Key="Icon.Mirror" Stroke="M8 1.5 V3 M8 5.5 V7.5 M8 10 V12 M8 14 V14.5 M6 4 V12.5 H1.5 Z" Fill="M10 4 L14.5 12.5 H10 Z" />
<ui:IconData x:Key="Icon.Moon" Stroke="M9.5 1.8 A6.5 6.5 0 1 0 14.2 11.2 A5.5 5.5 0 0 1 9.5 1.8 Z" />
<ui:IconData x:Key="Icon.More" Fill="M2.4 8 A1.1 1.1 0 1 0 4.6 8 A1.1 1.1 0 1 0 2.4 8 ZM6.9 8 A1.1 1.1 0 1 0 9.1 8 A1.1 1.1 0 1 0 6.9 8 ZM11.4 8 A1.1 1.1 0 1 0 13.6 8 A1.1 1.1 0 1 0 11.4 8 Z" />
<ui:IconData x:Key="Icon.Motion" Stroke="M1.5 8 C3.5 3 5.5 3 7.5 8 C9.5 13 11.5 13 14.5 8" />
@@ -75,6 +77,8 @@
<ui:IconData x:Key="Icon.Reset" Stroke="M3.67 6.00 A5 5 0 1 1 5.50 12.83 M3.2 3.2 V6.2 H6.2" />
<ui:IconData x:Key="Icon.Retry" Stroke="M10.75 3.24 A5.5 5.5 0 1 1 4.46 3.79 M13.2 2.6 L13.2 5.3 L10.5 5.3" />
<ui:IconData x:Key="Icon.Ring" Stroke="M4.5 8 A3.5 3.5 0 1 0 11.5 8 A3.5 3.5 0 1 0 4.5 8 Z" />
<ui:IconData x:Key="Icon.RotateLeft" Stroke="M5.5 9.5 H10.5 A1 1 0 0 1 11.5 10.5 V13.5 A1 1 0 0 1 10.5 14.5 H5.5 A1 1 0 0 1 4.5 13.5 V10.5 A1 1 0 0 1 5.5 9.5 Z M3.02 6.18 A5.5 5.5 0 0 1 13.17 10.38 M2.39 3.86 L3.02 6.18 L5.19 5.16" />
<ui:IconData x:Key="Icon.RotateRight" Stroke="M5.5 9.5 H10.5 A1 1 0 0 1 11.5 10.5 V13.5 A1 1 0 0 1 10.5 14.5 H5.5 A1 1 0 0 1 4.5 13.5 V10.5 A1 1 0 0 1 5.5 9.5 Z M2.83 10.38 A5.5 5.5 0 0 1 12.98 6.18 M10.81 5.16 L12.98 6.18 L13.61 3.86" />
<ui:IconData x:Key="Icon.Search" Stroke="M2.5 7 A4.5 4.5 0 1 0 11.5 7 A4.5 4.5 0 1 0 2.5 7 Z M10.4 10.4 L14 14" />
<ui:IconData x:Key="Icon.Sequence" Stroke="M2 12.5 L11.5 3 L13 4.5 L3.5 14 H2 Z" />
<ui:IconData x:Key="Icon.Server" Stroke="M4.5 12.5 A3 3 0 0 1 4.2 6.5 A4 4 0 0 1 11.9 5.5 A3.5 3.5 0 0 1 12.2 12.5 Z" />
@@ -85,6 +89,7 @@
<ui:IconData x:Key="Icon.Star" Stroke="M8 1.8 L9.9 5.8 L14.2 6.3 L11 9.3 L11.9 13.6 L8 11.4 L4.1 13.6 L5 9.3 L1.8 6.3 L6.1 5.8 Z" />
<ui:IconData x:Key="Icon.StarFilled" Fill="M8 1.8 L9.9 5.8 L14.2 6.3 L11 9.3 L11.9 13.6 L8 11.4 L4.1 13.6 L5 9.3 L1.8 6.3 L6.1 5.8 Z" />
<ui:IconData x:Key="Icon.Stop" Fill="M5.0 3.5 H11.0 A1.5 1.5 0 0 1 12.5 5.0 V11.0 A1.5 1.5 0 0 1 11.0 12.5 H5.0 A1.5 1.5 0 0 1 3.5 11.0 V5.0 A1.5 1.5 0 0 1 5.0 3.5 Z" />
<ui:IconData x:Key="Icon.Straighten" Stroke="M1.5 12.5 H14.5 M2.5 12.5 L13 6 M8.45 8.81 A7 7 0 0 1 9.50 12.50" />
<ui:IconData x:Key="Icon.SuccessCircle" Stroke="M1.5 8 A6.5 6.5 0 1 0 14.5 8 A6.5 6.5 0 1 0 1.5 8 Z M5 8.3 L7.1 10.3 L11 6" />
<ui:IconData x:Key="Icon.Sun" Stroke="M5 8 A3 3 0 1 0 11 8 A3 3 0 1 0 5 8 Z M8 1 V2.5 M8 13.5 V15 M1 8 H2.5 M13.5 8 H15 M3 3 L4 4 M12 12 L13 13 M3 13 L4 12 M12 4 L13 3" />
<ui:IconData x:Key="Icon.Sync" Stroke="M9.88 2.83 A5.5 5.5 0 0 1 9.88 13.17 M6.12 13.17 A5.5 5.5 0 0 1 6.12 2.83 M13.5 6.5 L13.2 9.4 L10.6 8.4 M2.5 9.5 L2.8 6.6 L5.4 7.6" />
+73 -5
View File
@@ -1,6 +1,7 @@
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Media;
using Chronos.Core.Imaging.Geometry;
namespace Chronos.Ui.Visuals;
@@ -18,6 +19,16 @@ public sealed class PreviewSurface : DrawingElement
private const double LabelSize = 11;
/// <summary>Crop handles: squares of this many view units (DIPs) whatever the zoom, and their stroke.</summary>
private const double HandleSize = 8;
private const double HandleStroke = 1.5;
/// <summary>The selected box's ring (DESIGN §4 focus: 2 px Accent outside), drawn this far outside the handles' centres.</summary>
private const double SelectedRing = 2;
/// <summary>Cells per side of the straighten grid.</summary>
private const int GridCells = 8;
private readonly VisualCollection _layers;
private readonly DrawingVisual _corrected = new();
private readonly DrawingVisual _original = new();
@@ -82,13 +93,14 @@ public sealed class PreviewSurface : DrawingElement
if (view.Scale != _overlayScale) RefreshOverlay();
}
/// <summary>Rebuilds the overlays (crop mask, region boundary, motion vectors) in image coordinates.</summary>
/// <summary>Rebuilds the overlays (straighten grid, crop mask, region boundary, motion vectors) in image coordinates.</summary>
internal void RefreshOverlay()
{
_overlayScale = _view.Matrix.M11;
using var dc = _overlay.RenderOpen();
if (Owner is not { } owner || _imageSize.IsEmpty || !(_overlayScale > 0)) return;
var unit = 1 / _overlayScale;
if (owner.ShowGrid) DrawGrid(dc, unit);
if (owner.ShowCropMask) DrawCropMask(dc, owner, unit);
if (owner.ShowRegions && owner.RegionBoundary is { Count: > 1 } boundary) DrawRegion(dc, boundary, unit);
if (owner.ShowVectors && owner.MotionVectors is { Count: > 0 } vectors) DrawVectors(dc, vectors, owner.VectorScale, unit);
@@ -139,18 +151,74 @@ public sealed class PreviewSurface : DrawingElement
_wipeClip.Rect = visible ? new Rect(0, 0, Math.Max(0, Owner!.WipePosition * ActualWidth), Math.Max(0, ActualHeight)) : Rect.Empty;
}
/// <summary>Everything outside the crop: dimmed with the scrim ("what I lose") or hidden ("how it will look").</summary>
/// <summary>
/// Everything outside the crop: dimmed with the scrim ("what I lose") or hidden ("how it will look"). An editable box
/// (owner review 1, R5) has an Accent outline and 8 handles in both modes, rule-of-thirds lines while dragged, and a
/// 2 px Accent ring outside it while it has the keyboard (<see cref="PreviewViewport.IsCropSelected"/>).
/// </summary>
private void DrawCropMask(DrawingContext dc, PreviewViewport owner, double unit)
{
var image = new Rect(_imageSize);
var n = owner.CropRect;
var crop = Rect.Intersect(image, new Rect(n.X * image.Width, n.Y * image.Height, n.Width * image.Width, n.Height * image.Height));
var n = owner.ShownCrop;
var crop = n.IsEmpty ? Rect.Empty : Rect.Intersect(image, new Rect(n.X * image.Width, n.Y * image.Height, n.Width * image.Width, n.Height * image.Height));
if (crop.IsEmpty) crop = new Rect(image.TopLeft, new Size(0, 0));
var outside = new CombinedGeometry(GeometryCombineMode.Exclude, new RectangleGeometry(image), new RectangleGeometry(crop));
outside.Freeze();
var loss = owner.CropMaskMode == CropMaskMode.ShowLoss;
dc.DrawGeometry(loss ? GetBrush("Brush.Scrim") : owner.Background ?? GetBrush("Brush.Preview"), null, outside);
if (loss && !crop.IsEmpty) dc.DrawRectangle(null, CreatePen("Brush.TextMuted", unit), crop);
if (crop.Width <= 0 || crop.Height <= 0) return;
if (!owner.CropEditable)
{
if (loss) dc.DrawRectangle(null, CreatePen("Brush.TextMuted", unit), crop);
return;
}
if (owner.IsCropDragging) DrawThirds(dc, crop, unit);
dc.DrawRectangle(null, CreatePen("Brush.Accent", HandleStroke * unit), crop);
if (owner.IsCropSelected)
{
var ring = (HandleSize / 2 + SelectedRing) * unit;
var focus = crop;
focus.Inflate(ring, ring);
dc.DrawRectangle(null, CreatePen("Brush.Accent", SelectedRing * unit), focus);
}
var (fill, stroke, half) = (GetBrush("Brush.Card"), CreatePen("Brush.Accent", HandleStroke * unit), HandleSize * unit / 2);
var box = new RectD(crop.X, crop.Y, crop.Width, crop.Height);
foreach (var handle in ViewportMath.CropHandles)
{
var (x, y) = ViewportMath.HandleAt(box, handle);
dc.DrawRectangle(fill, stroke, new Rect(x - half, y - half, 2 * half, 2 * half));
}
}
/// <summary>The rule-of-thirds lines inside the dragged box (1 px Text at 50 %).</summary>
private void DrawThirds(DrawingContext dc, Rect crop, double unit)
{
var pen = CreatePen("Brush.Text", unit);
dc.PushOpacity(0.5);
for (var k = 1; k < 3; k++)
{
var x = crop.X + crop.Width * k / 3;
var y = crop.Y + crop.Height * k / 3;
dc.DrawLine(pen, new Point(x, crop.Y), new Point(x, crop.Bottom));
dc.DrawLine(pen, new Point(crop.X, y), new Point(crop.Right, y));
}
dc.Pop();
}
/// <summary>The straighten grid: 8 × 8 cells over the whole picture (1 px Text at 30 %), to level the horizon against.</summary>
private void DrawGrid(DrawingContext dc, double unit)
{
var pen = CreatePen("Brush.Text", unit);
var (width, height) = (_imageSize.Width, _imageSize.Height);
dc.PushOpacity(0.3);
for (var k = 1; k < GridCells; k++)
{
var x = width * k / GridCells;
var y = height * k / GridCells;
dc.DrawLine(pen, new Point(x, 0), new Point(x, height));
dc.DrawLine(pen, new Point(0, y), new Point(width, y));
}
dc.Pop();
}
private void DrawRegion(DrawingContext dc, IReadOnlyList<Point> boundary, double unit)
+385 -17
View File
@@ -1,10 +1,13 @@
using System.Collections;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Chronos.Core.Imaging.Geometry;
using Chronos.Core.Localization;
namespace Chronos.Ui.Visuals;
@@ -35,6 +38,18 @@ public enum CropMaskMode
/// <summary>A motion-field sample in image pixels: origin (X, Y) and displacement (Dx, Dy).</summary>
public readonly record struct MotionVector(float X, float Y, float Dx, float Dy);
/// <summary>A finished edit of the crop box (<see cref="PreviewViewport.CropEditCompletedEvent"/>).</summary>
/// <param name="crop">The box, normalised in the image, locked to <see cref="PreviewViewport.CropLockRatio"/> and inside [0,1]².</param>
/// <param name="isNudge">A key press (arrows, + and −): a run of them is one undo step; a drag is one on its own.</param>
public sealed class CropEditEventArgs(RoutedEvent routedEvent, Rect crop, bool isNudge) : RoutedEventArgs(routedEvent)
{
public Rect Crop { get; } = crop;
public bool IsNudge { get; } = isNudge;
}
public delegate void CropEditEventHandler(object sender, CropEditEventArgs e);
/// <summary>
/// Editor preview (BRIEF §12.3): the corrected image with an optional original shown left of a draggable wipe
/// divider (label from <see cref="WipeLabel"/>), zoom to fit / 1:1 (raw pixels) / wheel zoom around the cursor,
@@ -45,6 +60,16 @@ public readonly record struct MotionVector(float X, float Y, float Dx, float Dy)
/// Fit and 1:1; Ctrl+0 fits, Ctrl+Shift+1 shows 1:1 (Ctrl+1 to Ctrl+6 switch the client's sections). Screen readers
/// see an image (named by AutomationProperties.Name) whose status is the zoom and whose help text is the shot data,
/// holding the tools.
/// <para>
/// While <see cref="CropEditable"/> (owner review 1, R5) the crop box <see cref="EditCrop"/> has 8 handles and is edited
/// on the picture, locked to <see cref="CropLockRatio"/> (<see cref="CropFrame"/>). A press takes, in order, a handle
/// within 6 px (resize: a corner anchored at the opposite corner, an edge at the opposite edge; Alt about the centre),
/// the wipe divider, the inside of the box (move), else pans. Only the overlay follows the pointer;
/// <see cref="CropEditCompletedEvent"/> fires once when the button is released, and Esc cancels the drag. A press on the
/// box (or Enter) gives it the keyboard: arrows move it 1 % (Shift 10 %), + and − resize it 2 % about its centre, each
/// firing the event; Esc or a press outside gives the arrows back to the editor (frame steps). Screen readers read the
/// box as the image's value ("x 12 %, y 5 %, size 80 %").
/// </para>
/// </summary>
[TemplatePart(Name = PartSurface, Type = typeof(FrameworkElement))]
[TemplatePart(Name = PartTools, Type = typeof(FrameworkElement))]
@@ -106,6 +131,22 @@ public class PreviewViewport : Control
public static readonly DependencyProperty CropMaskModeProperty = DependencyProperty.Register(
nameof(CropMaskMode), typeof(CropMaskMode), typeof(PreviewViewport), new PropertyMetadata(CropMaskMode.ShowLoss, OnOverlayChanged));
public static readonly DependencyProperty CropEditableProperty = DependencyProperty.Register(
nameof(CropEditable), typeof(bool), typeof(PreviewViewport), new PropertyMetadata(false, OnCropEditableChanged));
public static readonly DependencyProperty CropLockRatioProperty = DependencyProperty.Register(
nameof(CropLockRatio), typeof(double), typeof(PreviewViewport), new PropertyMetadata(1.0, OnOverlayChanged, CoerceRatio));
public static readonly DependencyProperty EditCropProperty = DependencyProperty.Register(
nameof(EditCrop), typeof(Rect), typeof(PreviewViewport),
new FrameworkPropertyMetadata(new Rect(0, 0, 1, 1), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnEditCropChanged));
public static readonly DependencyProperty ShowGridProperty = DependencyProperty.Register(
nameof(ShowGrid), typeof(bool), typeof(PreviewViewport), new PropertyMetadata(false, OnOverlayChanged));
public static readonly RoutedEvent CropEditCompletedEvent = EventManager.RegisterRoutedEvent(
nameof(CropEditCompleted), RoutingStrategy.Bubble, typeof(CropEditEventHandler), typeof(PreviewViewport));
public static readonly DependencyProperty InfoTextProperty = DependencyProperty.Register(nameof(InfoText), typeof(string), typeof(PreviewViewport));
public static readonly DependencyProperty ToolsContentProperty = DependencyProperty.Register(
@@ -115,6 +156,15 @@ public class PreviewViewport : Control
private const double MaxZoom = 32;
private const double DividerGrip = 6;
/// <summary>How near a crop handle a press takes it, in view units (DIPs).</summary>
private const double HandleGrip = 6;
/// <summary>The arrows move the box by this share of the view (Shift: ten times).</summary>
private const double NudgeStep = 0.01;
/// <summary>+ and − resize the box by this share of the largest box.</summary>
private const double ResizeStep = 0.02;
private PreviewSurface? _surface;
private FrameworkElement? _tools;
private FrameworkElement? _info;
@@ -125,6 +175,17 @@ public class PreviewViewport : Control
private DragMode _drag;
private Point _dragStart;
private ViewTransform _dragOrigin;
private CropHandle _handle;
private RectD _cropStart;
private RectD? _cropDraft;
/// <summary>The pointer's offset from the dragged handle at the press, normalised (the handle does not jump to the pointer).</summary>
private (double X, double Y) _grab;
private Point _lastPointer;
/// <summary>The box has the keyboard: the arrows move it instead of stepping frames.</summary>
private bool _cropSelected;
static PreviewViewport()
{
@@ -138,11 +199,21 @@ public class PreviewViewport : Control
CommandBindings.Add(new CommandBinding(ActualSizeCommand, (_, _) => SetCurrentValue(ZoomModeProperty, PreviewZoomMode.Actual)));
}
private enum DragMode
/// <summary>Raised once per finished crop edit: a drag released (not cancelled), or a key press while the box has the keyboard.</summary>
public event CropEditEventHandler CropEditCompleted
{
add => AddHandler(CropEditCompletedEvent, value);
remove => RemoveHandler(CropEditCompletedEvent, value);
}
/// <summary>What the left button is dragging.</summary>
internal enum DragMode
{
None,
Pan,
Wipe,
MoveCrop,
ResizeCrop,
}
/// <summary>Corrected image (the output look).</summary>
@@ -185,6 +256,30 @@ public class PreviewViewport : Control
public CropMaskMode CropMaskMode { get => (CropMaskMode)GetValue(CropMaskModeProperty); set => SetValue(CropMaskModeProperty, value); }
/// <summary>The crop box is edited on the picture (handles, move, resize, keyboard): the mask shows <see cref="EditCrop"/>.</summary>
public bool CropEditable { get => (bool)GetValue(CropEditableProperty); set => SetValue(CropEditableProperty, value); }
/// <summary>The box's width over its height in normalised image units (<see cref="CropFrame"/>'s lock ratio).</summary>
public double CropLockRatio { get => (double)GetValue(CropLockRatioProperty); set => SetValue(CropLockRatioProperty, value); }
/// <summary>The edited crop box, normalised (0…1); set to the finished edit before <see cref="CropEditCompleted"/> fires (two-way).</summary>
public Rect EditCrop { get => (Rect)GetValue(EditCropProperty); set => SetValue(EditCropProperty, value); }
/// <summary>A fine 8 × 8 grid over the whole picture (while the straighten is set).</summary>
public bool ShowGrid { get => (bool)GetValue(ShowGridProperty); set => SetValue(ShowGridProperty, value); }
/// <summary>The crop the mask draws: the box being dragged, the edited box, or <see cref="CropRect"/>.</summary>
internal Rect ShownCrop => _cropDraft is { } draft ? new Rect(draft.X, draft.Y, draft.Width, draft.Height) : CropEditable ? EditCrop : CropRect;
/// <summary>A crop drag is under way (the rule-of-thirds lines show).</summary>
internal bool IsCropDragging => _drag is DragMode.MoveCrop or DragMode.ResizeCrop;
/// <summary>The box has the keyboard (arrows, + and −).</summary>
internal bool IsCropSelected => _cropSelected;
/// <summary>What the left button is dragging now.</summary>
internal DragMode Dragging => _drag;
/// <summary>Shot data shown in the bottom-right pill.</summary>
public string? InfoText { get => (string?)GetValue(InfoTextProperty); set => SetValue(InfoTextProperty, value); }
@@ -250,30 +345,38 @@ public class PreviewViewport : Control
var p = e.GetPosition(_surface);
if (e.ClickCount == 2)
{
CancelDrag();
ToggleZoom(p);
e.Handled = true;
return;
}
_drag = IsNearDivider(p) ? DragMode.Wipe : DragMode.Pan;
_dragStart = p;
_dragOrigin = _view;
if (!CaptureMouse()) _drag = DragMode.None;
Press(p);
if (!CaptureMouse()) EndDrag(commit: false);
e.Handled = true;
}
/// <summary>The middle button pans wherever it is pressed, the crop box included (it may fill the view at any zoom).</summary>
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.Handled || _surface is null || e.ChangedButton != MouseButton.Middle || _drag != DragMode.None) return;
Focus();
PressPan(e.GetPosition(_surface));
if (!CaptureMouse()) EndDrag(commit: false);
e.Handled = true;
}
protected override void OnMouseUp(MouseButtonEventArgs e)
{
base.OnMouseUp(e);
if (e.ChangedButton == MouseButton.Middle && _drag == DragMode.Pan) ReleaseMouseCapture();
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_drag == DragMode.None || _surface is null) return;
var p = e.GetPosition(_surface);
if (_drag == DragMode.Wipe)
{
var width = ViewSize.Width;
if (width > 0) SetCurrentValue(WipePositionProperty, p.X / width);
return;
}
UpdateView(ViewportMath.Clamp(ViewportMath.Pan(_dragOrigin, p.X - _dragStart.X, p.Y - _dragStart.Y),
_imageSize.Width, _imageSize.Height, ViewSize.Width, ViewSize.Height));
DragTo(e.GetPosition(_surface), (Keyboard.Modifiers & ModifierKeys.Alt) != 0);
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
@@ -285,7 +388,54 @@ public class PreviewViewport : Control
protected override void OnLostMouseCapture(MouseEventArgs e)
{
base.OnLostMouseCapture(e);
_drag = DragMode.None;
EndDrag(commit: true);
}
/// <summary>
/// Esc cancels a crop drag (or gives the arrows back); with the box selected, arrows move it and + and − resize it;
/// Alt, pressed or released during a resize, switches it about the centre and back.
/// </summary>
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.Handled) return;
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if (key is Key.LeftAlt or Key.RightAlt && _drag == DragMode.ResizeCrop)
{
DragTo(_lastPointer, aboutCentre: true);
e.Handled = true;
return;
}
if (key == Key.Escape && (IsCropDragging || _cropSelected))
{
if (IsCropDragging) CancelDrag();
else Select(false);
e.Handled = true;
return;
}
if (!CropEditable || _drag != DragMode.None) return;
if (key == Key.Enter && Keyboard.Modifiers == ModifierKeys.None && !_cropSelected)
{
Select(true);
e.Handled = true;
return;
}
if (_cropSelected && Nudge(key, Keyboard.Modifiers)) e.Handled = true;
}
protected override void OnKeyUp(KeyEventArgs e)
{
base.OnKeyUp(e);
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if (key is not (Key.LeftAlt or Key.RightAlt) || _drag != DragMode.ResizeCrop) return;
DragTo(_lastPointer, aboutCentre: false);
e.Handled = true;
}
protected override void OnLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnLostKeyboardFocus(e);
if (!IsKeyboardFocusWithin) Select(false);
}
protected override void OnMouseWheel(MouseWheelEventArgs e)
@@ -305,12 +455,179 @@ public class PreviewViewport : Control
base.OnQueryCursor(e);
if (e.Handled || _surface is null || !ReferenceEquals(e.OriginalSource, _surface)) return;
var p = e.GetPosition(_surface);
if (_drag == DragMode.Wipe || IsNearDivider(p)) e.Cursor = Cursors.SizeWE;
var handle = _drag switch
{
DragMode.ResizeCrop => _handle,
DragMode.MoveCrop => CropHandle.Inside,
DragMode.None => Movable(HitCrop(p)),
_ => CropHandle.None,
};
// The same order as a press: a handle, the divider, the inside of the box, then pan.
var onDivider = _drag == DragMode.Wipe || (_drag == DragMode.None && handle is CropHandle.None or CropHandle.Inside && IsNearDivider(p));
if (onDivider) e.Cursor = Cursors.SizeWE;
else if (CursorOf(handle) is { } cursor) e.Cursor = cursor;
else if (IsPannable()) e.Cursor = Cursors.SizeAll;
else return;
e.Handled = true;
}
/// <summary>
/// A press at <paramref name="p"/> (surface DIPs): a crop handle, the wipe divider, inside the crop box, else pan, in
/// that order. A press on the box gives it the keyboard, a press outside gives the arrows back.
/// </summary>
internal void Press(Point p)
{
var handle = Movable(HitCrop(p));
_drag = handle is not (CropHandle.None or CropHandle.Inside) ? DragMode.ResizeCrop
: IsNearDivider(p) ? DragMode.Wipe
: handle == CropHandle.Inside ? DragMode.MoveCrop
: DragMode.Pan;
Select(IsCropDragging);
_dragStart = p;
_lastPointer = p;
_dragOrigin = _view;
if (!IsCropDragging) return;
_handle = handle;
_cropStart = Normalised(EditCrop);
_cropDraft = _cropStart;
var (hx, hy) = ViewportMath.HandleAt(_cropStart, handle);
var (nx, ny) = ToNormalised(p);
_grab = (nx - hx, ny - hy);
_surface?.RefreshOverlay();
}
/// <summary>
/// The box takes the keyboard (a press on it, Enter) or gives it back (a press outside, Esc, focus lost): drawn with a
/// 2 px Accent ring outside it while it has it, so a keyboard user sees where the arrows go.
/// </summary>
private void Select(bool selected)
{
if (_cropSelected == selected) return;
_cropSelected = selected;
_surface?.RefreshOverlay();
}
/// <summary>A middle-button press at <paramref name="p"/>: a pan, whatever is under it.</summary>
internal void PressPan(Point p)
{
_drag = DragMode.Pan;
_dragStart = p;
_lastPointer = p;
_dragOrigin = _view;
}
/// <summary>
/// The inside of a box that cannot move (it spans the view both ways: no crop, or the largest box of the view's own
/// proportions) is no move target while the view pans: a press there pans, as outside the box.
/// </summary>
private CropHandle Movable(CropHandle handle)
{
if (handle != CropHandle.Inside || !IsPannable()) return handle;
var box = Normalised(EditCrop);
return CropFrame.HasSlack(box, horizontal: true) || CropFrame.HasSlack(box, horizontal: false) ? handle : CropHandle.None;
}
/// <summary>The pointer moved to <paramref name="p"/> during a drag; <paramref name="aboutCentre"/> (Alt) resizes about the box's centre.</summary>
internal void DragTo(Point p, bool aboutCentre)
{
_lastPointer = p;
switch (_drag)
{
case DragMode.Wipe:
var width = ViewSize.Width;
if (width > 0) SetCurrentValue(WipePositionProperty, p.X / width);
return;
case DragMode.Pan:
UpdateView(ViewportMath.Clamp(ViewportMath.Pan(_dragOrigin, p.X - _dragStart.X, p.Y - _dragStart.Y),
_imageSize.Width, _imageSize.Height, ViewSize.Width, ViewSize.Height));
return;
case DragMode.MoveCrop:
var (x0, y0) = ToNormalised(_dragStart);
var (x1, y1) = ToNormalised(p);
_cropDraft = CropFrame.Move(_cropStart, x1 - x0, y1 - y0);
break;
case DragMode.ResizeCrop:
var (nx, ny) = ToNormalised(p);
_cropDraft = CropFrame.ResizeFrom(_cropStart, _handle, nx - _grab.X, ny - _grab.Y, CropLockRatio, aboutCentre);
break;
default:
return;
}
_surface?.RefreshOverlay();
}
/// <summary>The drag ends (button released, capture lost): a changed crop box becomes <see cref="EditCrop"/> and is reported once.</summary>
internal void EndDrag(bool commit)
{
var (dragged, draft, start) = (IsCropDragging, _cropDraft, _cropStart);
_drag = DragMode.None;
_handle = CropHandle.None;
_cropDraft = null;
if (!dragged) return;
_surface?.RefreshOverlay();
if (commit && draft is { } box && box != start) Complete(box, isNudge: false);
}
/// <summary>Esc: the drag ends without a change.</summary>
internal void CancelDrag()
{
if (_drag == DragMode.None) return;
EndDrag(commit: false);
if (IsMouseCaptured) ReleaseMouseCapture();
}
/// <summary>A key press on the selected box: arrows move it 1 % (Shift 10 %), + and − resize it 2 % about its centre.</summary>
internal bool Nudge(Key key, ModifierKeys modifiers)
{
if (modifiers is not (ModifierKeys.None or ModifierKeys.Shift)) return false;
var step = modifiers == ModifierKeys.Shift ? 10 * NudgeStep : NudgeStep;
var box = Normalised(EditCrop);
RectD? next = key switch
{
Key.Left => CropFrame.Move(box, -step, 0),
Key.Right => CropFrame.Move(box, step, 0),
Key.Up => CropFrame.Move(box, 0, -step),
Key.Down => CropFrame.Move(box, 0, step),
Key.OemPlus or Key.Add => ViewportMath.ResizeCrop(box, ResizeStep, CropLockRatio),
Key.OemMinus or Key.Subtract => ViewportMath.ResizeCrop(box, -ResizeStep, CropLockRatio),
_ => null,
};
if (next is not { } moved) return false;
if (moved != box) Complete(moved, isNudge: true);
return true;
}
private static Cursor? CursorOf(CropHandle handle) => handle switch
{
CropHandle.TopLeft or CropHandle.BottomRight => Cursors.SizeNWSE,
CropHandle.TopRight or CropHandle.BottomLeft => Cursors.SizeNESW,
CropHandle.Left or CropHandle.Right => Cursors.SizeWE,
CropHandle.Top or CropHandle.Bottom => Cursors.SizeNS,
CropHandle.Inside => Cursors.SizeAll,
_ => null,
};
private static RectD Normalised(Rect rect) => rect.IsEmpty ? new RectD(0, 0, 1, 1) : new RectD(rect.X, rect.Y, rect.Width, rect.Height);
private void Complete(RectD box, bool isNudge)
{
var rect = new Rect(box.X, box.Y, box.Width, box.Height);
SetCurrentValue(EditCropProperty, rect);
RaiseEvent(new CropEditEventArgs(CropEditCompletedEvent, rect, isNudge));
}
/// <summary>The crop handle or the inside of the box under <paramref name="p"/>; none while the crop is not editable.</summary>
private CropHandle HitCrop(Point p) => CropEditable && !_imageSize.IsEmpty
? ViewportMath.HitCrop(_view, _imageSize.Width, _imageSize.Height, Normalised(EditCrop), p.X, p.Y, HandleGrip)
: CropHandle.None;
private (double X, double Y) ToNormalised(Point p)
{
if (_imageSize.IsEmpty || !(_view.Scale > 0)) return (0, 0);
var (x, y) = _view.ToImage(p.X, p.Y);
return (x / _imageSize.Width, y / _imageSize.Height);
}
protected override void OnDpiChanged(DpiScale oldDpi, DpiScale newDpi)
{
base.OnDpiChanged(oldDpi, newDpi);
@@ -321,6 +638,19 @@ public class PreviewViewport : Control
private static object CoerceUnit(DependencyObject d, object value) => double.IsNaN((double)value) ? 0.5 : Math.Clamp((double)value, 0, 1);
private static object CoerceRatio(DependencyObject d, object value) => (double)value is > 0 and var ratio && double.IsFinite(ratio) ? ratio : 1.0;
private static void OnCropEditableChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var viewport = (PreviewViewport)d;
if (e.NewValue is false)
{
viewport.Select(false);
viewport.CancelDrag();
}
viewport._surface?.RefreshOverlay();
}
private static object CoerceZoom(DependencyObject d, object value) =>
double.IsFinite((double)value) ? Math.Clamp((double)value, MinZoom, MaxZoom) : 1.0;
@@ -342,6 +672,26 @@ public class PreviewViewport : Control
private static void OnOverlayChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((PreviewViewport)d)._surface?.RefreshOverlay();
/// <summary>The box moved or resized (a drag, a nudge, the editor): drawn again, and told to a screen reader as the Value it reads.</summary>
private static void OnEditCropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var viewport = (PreviewViewport)d;
viewport._surface?.RefreshOverlay();
if (!viewport.CropEditable || UIElementAutomationPeer.FromElement(viewport) is not { } peer) return;
var (before, after) = (CropValueText((Rect)e.OldValue, viewport.CropLockRatio), CropValueText((Rect)e.NewValue, viewport.CropLockRatio));
if (before != after) peer.RaisePropertyChangedEvent(ValuePatternIdentifiers.ValueProperty, before, after);
}
/// <summary>"x 12 %, y 5 %, size 80 %": the box's left edge, top edge and share of the largest box; empty for none.</summary>
internal static string CropValueText(Rect crop, double lockRatio)
{
if (crop.IsEmpty) return string.Empty;
var (size, _, _) = CropFrame.ToRows(new RectD(crop.X, crop.Y, crop.Width, crop.Height), lockRatio);
return Loc.L("Editor.Preview.CropValue", Percent(crop.X), Percent(crop.Y), Percent(size));
}
private static string Percent(double share) => Loc.L("Common.Percent", Math.Round(share * 100));
private static void OnZoomModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => ((PreviewViewport)d).ApplyZoomMode();
private static void OnZoomChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
@@ -434,8 +784,26 @@ public class PreviewViewport : Control
}
/// <summary>The preview seen by UI Automation: an image whose status gives the zoom ("50 %") and whose help text is the shot data.</summary>
public sealed class PreviewViewportAutomationPeer(PreviewViewport owner) : FrameworkElementAutomationPeer(owner)
public sealed class PreviewViewportAutomationPeer(PreviewViewport owner) : FrameworkElementAutomationPeer(owner), IValueProvider
{
/// <summary>The crop box while it is edited on the picture: "x 12 %, y 5 %, size 80 %" (left edge, top edge, share of the largest box).</summary>
public string Value
{
get
{
var viewport = (PreviewViewport)Owner;
return PreviewViewport.CropValueText(viewport.EditCrop, viewport.CropLockRatio);
}
}
/// <summary>The box is moved and resized with the pointer and the keys, not typed.</summary>
public bool IsReadOnly => true;
public void SetValue(string value) => throw new ElementNotEnabledException();
public override object GetPattern(PatternInterface patternInterface) =>
patternInterface == PatternInterface.Value && ((PreviewViewport)Owner).CropEditable ? this : base.GetPattern(patternInterface);
protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Image;
protected override string GetClassNameCore() => nameof(PreviewViewport);
+61
View File
@@ -1,3 +1,5 @@
using Chronos.Core.Imaging.Geometry;
namespace Chronos.Ui.Visuals;
/// <summary>Uniform scale and translation from image pixels to view units: view = image × <see cref="Scale"/> + offset.</summary>
@@ -62,6 +64,65 @@ public static class ViewportMath
public static double WheelZoom(double scale, double notches, double minScale, double maxScale) =>
Math.Clamp(scale * Math.Pow(1.25, notches), minScale, maxScale);
/// <summary>The crop box's handles (owner review 1, R5): the four corners, then the four edge midpoints.</summary>
public static IReadOnlyList<CropHandle> CropHandles { get; } =
[
CropHandle.TopLeft, CropHandle.TopRight, CropHandle.BottomRight, CropHandle.BottomLeft,
CropHandle.Top, CropHandle.Right, CropHandle.Bottom, CropHandle.Left,
];
/// <summary>Where <paramref name="handle"/> sits on <paramref name="crop"/>, in the crop's units; its centre for the others.</summary>
public static (double X, double Y) HandleAt(RectD crop, CropHandle handle) => handle switch
{
CropHandle.TopLeft => (crop.X, crop.Y),
CropHandle.Top => (crop.CenterX, crop.Y),
CropHandle.TopRight => (crop.Right, crop.Y),
CropHandle.Right => (crop.Right, crop.CenterY),
CropHandle.BottomRight => (crop.Right, crop.Bottom),
CropHandle.Bottom => (crop.CenterX, crop.Bottom),
CropHandle.BottomLeft => (crop.X, crop.Bottom),
CropHandle.Left => (crop.X, crop.CenterY),
_ => (crop.CenterX, crop.CenterY),
};
/// <summary>
/// What a press at view point (<paramref name="viewX"/>, <paramref name="viewY"/>) takes of the normalised
/// <paramref name="crop"/> over an image of <paramref name="imageWidth"/>×<paramref name="imageHeight"/> pixels shown
/// through <paramref name="view"/>: the nearest handle within <paramref name="grip"/> view units (corners first on a
/// tie), else <see cref="CropHandle.Inside"/> inside the box, else <see cref="CropHandle.None"/>. The grip is in view
/// units, so it is the same on screen whatever the zoom.
/// </summary>
public static CropHandle HitCrop(ViewTransform view, double imageWidth, double imageHeight, RectD crop, double viewX, double viewY, double grip)
{
if (!(view.Scale > 0) || !(imageWidth > 0) || !(imageHeight > 0)) return CropHandle.None;
var best = CropHandle.None;
var nearest = double.MaxValue;
foreach (var handle in CropHandles)
{
var (nx, ny) = HandleAt(crop, handle);
var (x, y) = view.ToView(nx * imageWidth, ny * imageHeight);
var distance = double.Max(double.Abs(x - viewX), double.Abs(y - viewY));
if (distance > grip || distance >= nearest) continue;
(best, nearest) = (handle, distance);
}
if (best != CropHandle.None) return best;
var (left, top) = view.ToView(crop.X * imageWidth, crop.Y * imageHeight);
var (right, bottom) = view.ToView(crop.Right * imageWidth, crop.Bottom * imageHeight);
return viewX >= left && viewX <= right && viewY >= top && viewY <= bottom ? CropHandle.Inside : CropHandle.None;
}
/// <summary>
/// <paramref name="crop"/> resized by <paramref name="step"/> of the largest box of <paramref name="lockRatio"/> (+ grows,
/// − shrinks) about its centre, locked and kept inside the view (the + and − keys).
/// </summary>
public static RectD ResizeCrop(RectD crop, double step, double lockRatio)
{
var largest = CropFrame.Largest(lockRatio);
var height = double.Clamp(crop.Height + step * largest.Height, largest.Height * CropFrame.MinimumShare, largest.Height);
var width = height * lockRatio;
return CropFrame.Clamp(new RectD(crop.CenterX - width / 2, crop.CenterY - height / 2, width, height), lockRatio);
}
private static double ClampAxis(double offset, double size, double view)
{
if (size <= view) return (view - size) / 2;