feat(editor): edit the crop box on the preview, locked to the output proportions
The Camera step now shows its crop box on the Source picture with its mask on
by default, and the box is moved and resized right there (owner review 1, R5).
A press takes a handle within 6 px, then the wipe divider, then the inside of
the box, then pans; a resize stays anchored at the opposite corner or edge and
locked to the ratio, Alt resizes about the centre. Esc cancels a drag, and once
the box is taken (a press on it or Enter) the arrows, + and - nudge it. Cursors
follow the same priority and screen readers read "x, y, size" from a read-only
value pattern.
Each finished drag goes through CropCommit to CommitCropCommand as one undo
step; a run of key presses merges into one ("Camera.Crop"). The commit writes
the locked box with the offsets at 0, so the render keeps it as drawn. Editing
is gated on the Camera step, the mask on, an editable project, a current
format and frames to show.
PreviewRenderer frames Source through ViewFor, so the turned, mirrored and
straightened view frame is what the box is drawn on; the mask comes from
EditableCrop on the Camera step and FootprintInView elsewhere, and the horizon
overlay is mapped into the view. A straighten grid shows while IsStraightening.
xaml-lint now requires a tooltip on PreviewViewport.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
aaf9c0bba7
commit
6f15717463
13 files changed
+1298
-65
No files matched your search
@@ -278,6 +278,217 @@ public sealed class VisualsSmokeTests
|
||||
}
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void ACropDragMovesThenResizesLockedAndIsReportedOncePerDrag() => WpfHost.Run(() =>
|
||||
{
|
||||
var preview = CropPreview(new Rect(0.3, 0.2, 0.3, 0.6), ratio: 0.5);
|
||||
Render(preview, 800, 450);
|
||||
var edits = new List<CropEditEventArgs>();
|
||||
preview.CropEditCompleted += (_, e) => edits.Add(e);
|
||||
|
||||
// Inside the box: a move; only the overlay follows the pointer until the button is released.
|
||||
preview.Press(At(preview, 0.45, 0.5));
|
||||
Assert.Equal(PreviewViewport.DragMode.MoveCrop, preview.Dragging);
|
||||
preview.DragTo(At(preview, 0.55, 0.55), aboutCentre: false);
|
||||
Assert.Empty(edits, "nothing is reported while dragging");
|
||||
Assert.Approximately(0.4, preview.ShownCrop.X, 1e-6, "the overlay follows");
|
||||
Assert.Approximately(0.3, preview.EditCrop.X, 0, "the box itself waits for the release");
|
||||
preview.EndDrag(commit: true);
|
||||
var moved = Assert.Contains(edits, _ => true).Crop;
|
||||
Assert.Within(0.4, 0.25, moved.X, moved.Y, 1e-6);
|
||||
Assert.False(edits[0].IsNudge);
|
||||
Assert.Equal(moved, preview.EditCrop, "the finished box is the edited one");
|
||||
|
||||
// The bottom-right handle, dragged far outside: anchored at the top-left corner, locked, clamped to the view.
|
||||
preview.Press(At(preview, 0.7, 0.85));
|
||||
Assert.Equal(PreviewViewport.DragMode.ResizeCrop, preview.Dragging);
|
||||
preview.DragTo(At(preview, 1.5, 1.5), aboutCentre: false);
|
||||
preview.EndDrag(commit: true);
|
||||
Assert.Count(2, edits);
|
||||
var resized = edits[1].Crop;
|
||||
Assert.Within(0.4, 0.25, resized.X, resized.Y, 1e-9, "anchored at the opposite corner");
|
||||
Assert.Approximately(0.5, resized.Width / resized.Height, 1e-9, "the ratio stays locked");
|
||||
Assert.Approximately(1, resized.Bottom, 1e-9, "as large as the view lets it be");
|
||||
Assert.LessOrEqual(resized.Right, 1);
|
||||
|
||||
// Alt resizes about the centre.
|
||||
preview.Press(At(preview, resized.Right, resized.Bottom));
|
||||
preview.DragTo(At(preview, resized.Right - 0.05, resized.Bottom - 0.1), aboutCentre: true);
|
||||
preview.EndDrag(commit: true);
|
||||
var centred = edits[2].Crop;
|
||||
Assert.Within(resized.X + resized.Width / 2, resized.Y + resized.Height / 2, centred.X + centred.Width / 2, centred.Y + centred.Height / 2, 1e-9);
|
||||
Assert.Approximately(0.5, centred.Width / centred.Height, 1e-9);
|
||||
|
||||
// A press released where it started, and a pan outside the box, report nothing.
|
||||
preview.Press(At(preview, centred.X + centred.Width / 2, centred.Y + centred.Height / 2));
|
||||
preview.EndDrag(commit: true);
|
||||
preview.Press(At(preview, 0.05, 0.05));
|
||||
Assert.Equal(PreviewViewport.DragMode.Pan, preview.Dragging);
|
||||
preview.DragTo(At(preview, 0.1, 0.1), aboutCentre: false);
|
||||
preview.EndDrag(commit: true);
|
||||
Assert.Count(3, edits);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void APressTakesAHandleThenTheDividerThenTheBoxThenPansAtEveryZoom() => WpfHost.Run(() =>
|
||||
{
|
||||
var preview = CropPreview(new Rect(0.4, 0.4, 0.2, 0.2), ratio: 1);
|
||||
preview.OriginalSource = Bitmap(2000, 1500);
|
||||
preview.IsCompareEnabled = true;
|
||||
preview.WipePosition = 0.5;
|
||||
Render(preview, 800, 450);
|
||||
var surface = Assert.NotNull(FindChild<PreviewSurface>(preview));
|
||||
foreach (var zoom in new double?[] { null, 1, 2 })
|
||||
{
|
||||
if (zoom is { } z) preview.Zoom = z;
|
||||
var label = zoom is null ? "fit" : $"zoom {zoom}";
|
||||
// The image column under the divider, and a box of 0.06 × 0.12 around it (corner handles well away).
|
||||
var divider = surface.ActualWidth / 2;
|
||||
var nx = preview.View.ToImage(divider, 0).X / 2000;
|
||||
var y = At(preview, 0, 0.5).Y;
|
||||
PreviewViewport.DragMode Take(Rect box, double x)
|
||||
{
|
||||
preview.EditCrop = box;
|
||||
preview.Press(new Point(x, y));
|
||||
var mode = preview.Dragging;
|
||||
preview.EndDrag(commit: false);
|
||||
return mode;
|
||||
}
|
||||
Assert.Equal(PreviewViewport.DragMode.ResizeCrop, Take(new Rect(nx, 0.44, 0.06, 0.12), divider), $"{label}: a handle on the divider");
|
||||
Assert.Equal(PreviewViewport.DragMode.Wipe, Take(new Rect(nx - 0.03, 0.44, 0.06, 0.12), divider), $"{label}: the divider inside the box");
|
||||
Assert.Equal(PreviewViewport.DragMode.MoveCrop, Take(new Rect(nx - 0.03, 0.44, 0.06, 0.12), At(preview, nx + 0.012, 0.5).X), $"{label}: inside");
|
||||
Assert.Equal(PreviewViewport.DragMode.Pan, Take(new Rect(nx - 0.03, 0.44, 0.06, 0.12), At(preview, nx + 0.06, 0.5).X), $"{label}: outside");
|
||||
}
|
||||
|
||||
// Not editable: no handle and no box to take, the divider and pan only.
|
||||
preview.CropEditable = false;
|
||||
preview.EditCrop = new Rect(0.4, 0.4, 0.2, 0.2);
|
||||
preview.Press(At(preview, 0.4, 0.4));
|
||||
Assert.Equal(PreviewViewport.DragMode.Pan, preview.Dragging);
|
||||
preview.EndDrag(commit: false);
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void EscCancelsADragAndTheKeysNudgeTheBoxOnceItIsTaken() => WpfHost.Run(() =>
|
||||
{
|
||||
var preview = CropPreview(new Rect(0.3, 0.2, 0.3, 0.6), ratio: 0.5);
|
||||
var window = OffScreen(preview, 800, 450);
|
||||
try
|
||||
{
|
||||
var edits = new List<CropEditEventArgs>();
|
||||
preview.CropEditCompleted += (_, e) => edits.Add(e);
|
||||
Assert.False(Press(preview, Key.Right), "until the box is taken, the arrows step frames");
|
||||
|
||||
// Esc cancels a drag: the box stays, nothing is reported.
|
||||
preview.Press(At(preview, 0.45, 0.5));
|
||||
preview.DragTo(At(preview, 0.6, 0.6), aboutCentre: false);
|
||||
Assert.True(Press(preview, Key.Escape));
|
||||
Assert.Equal(PreviewViewport.DragMode.None, preview.Dragging);
|
||||
Assert.Equal(new Rect(0.3, 0.2, 0.3, 0.6), preview.EditCrop);
|
||||
Assert.Equal(new Rect(0.3, 0.2, 0.3, 0.6), preview.ShownCrop);
|
||||
Assert.Empty(edits);
|
||||
|
||||
// The press took the box: the arrows move it 1 %, + and − resize it about its centre, each reported as a nudge.
|
||||
Assert.True(preview.IsCropSelected);
|
||||
Assert.True(Press(preview, Key.Right));
|
||||
Assert.Count(1, edits);
|
||||
Assert.True(edits[0].IsNudge);
|
||||
Assert.Approximately(0.31, preview.EditCrop.X, 1e-9);
|
||||
Assert.True(preview.Nudge(Key.Down, ModifierKeys.Shift), "Shift moves 10 %");
|
||||
Assert.Approximately(0.3, preview.EditCrop.Y, 1e-9);
|
||||
var before = preview.EditCrop;
|
||||
Assert.True(Press(preview, Key.OemMinus));
|
||||
var shrunk = preview.EditCrop;
|
||||
Assert.Less(shrunk.Height, before.Height);
|
||||
Assert.Approximately(0.5, shrunk.Width / shrunk.Height, 1e-9);
|
||||
Assert.Within(before.X + before.Width / 2, before.Y + before.Height / 2, shrunk.X + shrunk.Width / 2, shrunk.Y + shrunk.Height / 2, 1e-9);
|
||||
Assert.Count(3, edits);
|
||||
|
||||
// Esc gives the arrows back; Enter takes the box from the keyboard.
|
||||
Assert.True(Press(preview, Key.Escape));
|
||||
Assert.False(preview.IsCropSelected);
|
||||
Assert.False(Press(preview, Key.Left));
|
||||
Assert.True(Press(preview, Key.Enter));
|
||||
Assert.True(Press(preview, Key.Left));
|
||||
Assert.Count(4, edits);
|
||||
|
||||
// A box that can no longer change gives the keys back.
|
||||
preview.CropEditable = false;
|
||||
Assert.False(preview.IsCropSelected);
|
||||
Assert.False(Press(preview, Key.Right));
|
||||
}
|
||||
finally
|
||||
{
|
||||
window.Close();
|
||||
}
|
||||
});
|
||||
|
||||
[Test]
|
||||
public void TheEditableBoxDrawsItsHandlesAndScreenReadersReadIt() => WpfHost.Run(() =>
|
||||
{
|
||||
var card = ColorOf("Brush.Card");
|
||||
var preview = CropPreview(new Rect(0.25, 0.2, 0.5, 0.6), ratio: 0.5 / 0.6);
|
||||
Render(preview, 800, 450);
|
||||
var surface = Assert.NotNull(FindChild<PreviewSurface>(preview));
|
||||
Color HandlePixel(double nx, double ny)
|
||||
{
|
||||
var bitmap = Render(preview, 800, 450);
|
||||
var (x, y) = preview.View.ToView(nx * 2000, ny * 1500);
|
||||
var p = surface.TranslatePoint(new Point(x, y), preview);
|
||||
return PixelAt(bitmap, (int)Math.Round(p.X), (int)Math.Round(p.Y));
|
||||
}
|
||||
foreach (var (nx, ny) in new[] { (0.25, 0.2), (0.75, 0.8), (0.5, 0.2), (0.75, 0.5) })
|
||||
Assert.True(Near(HandlePixel(nx, ny), card, 8), $"a Card handle at ({nx}, {ny})");
|
||||
preview.CropMaskMode = CropMaskMode.ShowResult;
|
||||
Assert.True(Near(HandlePixel(0.25, 0.8), card, 8), "the handles draw in either mask mode");
|
||||
|
||||
var peer = UIElementAutomationPeer.CreatePeerForElement(preview);
|
||||
var value = Assert.NotNull(peer.GetPattern(PatternInterface.Value) as System.Windows.Automation.Provider.IValueProvider);
|
||||
Assert.Equal(Loc.L("Editor.Preview.CropValue", Loc.L("Common.Percent", 25), Loc.L("Common.Percent", 20), Loc.L("Common.Percent", 60)), value.Value);
|
||||
Assert.True(value.IsReadOnly);
|
||||
|
||||
preview.CropMaskMode = CropMaskMode.ShowLoss;
|
||||
preview.CropEditable = false;
|
||||
Assert.False(Near(HandlePixel(0.25, 0.2), card, 8), "no handles while the box cannot change");
|
||||
Assert.Null(peer.GetPattern(PatternInterface.Value), "and no value to read");
|
||||
});
|
||||
|
||||
private static PreviewViewport CropPreview(Rect crop, double ratio) => new()
|
||||
{
|
||||
Source = Bitmap(2000, 1500),
|
||||
ShowCropMask = true,
|
||||
CropEditable = true,
|
||||
CropLockRatio = ratio,
|
||||
EditCrop = crop,
|
||||
};
|
||||
|
||||
/// <summary>The surface point of the normalised image point (<paramref name="nx"/>, <paramref name="ny"/>) of a 2000 × 1500 preview.</summary>
|
||||
private static Point At(PreviewViewport preview, double nx, double ny)
|
||||
{
|
||||
var (x, y) = preview.View.ToView(nx * 2000, ny * 1500);
|
||||
return new Point(x, y);
|
||||
}
|
||||
|
||||
/// <summary>A window off screen around <paramref name="element"/>, so it has a presentation source for key events.</summary>
|
||||
private static Window OffScreen(FrameworkElement element, double width, double height)
|
||||
{
|
||||
var window = new Window
|
||||
{
|
||||
Content = new Border { Child = element },
|
||||
Width = width,
|
||||
Height = height,
|
||||
WindowStyle = WindowStyle.None,
|
||||
WindowStartupLocation = WindowStartupLocation.Manual,
|
||||
Left = -20000,
|
||||
Top = -20000,
|
||||
ShowActivated = false,
|
||||
ShowInTaskbar = false,
|
||||
};
|
||||
window.Show();
|
||||
window.UpdateLayout();
|
||||
return window;
|
||||
}
|
||||
|
||||
private static IEnumerable<(string Name, FrameworkElement Element, double Width, double Height)> Samples()
|
||||
{
|
||||
var n = 120_000;
|
||||
@@ -342,6 +553,15 @@ public sealed class VisualsSmokeTests
|
||||
ShowCropMask = true,
|
||||
CropRect = new Rect(0.1, 0.1, 0.8, 0.8),
|
||||
}, 800, 450);
|
||||
yield return ("PreviewViewport.CropEdit", new PreviewViewport
|
||||
{
|
||||
Source = Bitmap(2000, 1500),
|
||||
ShowCropMask = true,
|
||||
CropEditable = true,
|
||||
CropLockRatio = 0.5,
|
||||
EditCrop = new Rect(0.3, 0.2, 0.3, 0.6),
|
||||
ShowGrid = true,
|
||||
}, 800, 450);
|
||||
|
||||
yield return ("SequenceCard", SampleCard(), 236, 208);
|
||||
yield return ("SegmentedControl", SampleSegments(), 120, 32);
|
||||
@@ -438,8 +658,13 @@ public sealed class VisualsSmokeTests
|
||||
return false;
|
||||
}
|
||||
|
||||
private static void Press(UIElement target, Key key) =>
|
||||
target.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(target)!, 0, key) { RoutedEvent = Keyboard.KeyDownEvent });
|
||||
/// <summary>Raises a key press on <paramref name="target"/>; true when something handled it.</summary>
|
||||
private static bool Press(UIElement target, Key key)
|
||||
{
|
||||
var args = new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(target)!, 0, key) { RoutedEvent = Keyboard.KeyDownEvent };
|
||||
target.RaiseEvent(args);
|
||||
return args.Handled;
|
||||
}
|
||||
|
||||
private static RenderTargetBitmap Render(FrameworkElement element, double width, double height)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user