From ca6e8cd4ce70caf8b1b52ec3daf8cad6f947b2a4 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Sun, 29 Nov 2026 20:09:17 -0500 Subject: [PATCH] feat(avalonia): zoom commands, zoom window, measure area and plate menu items View > Zoom To (Area, Fit, Plate, Selected), Zoom In (Ctrl+Plus) and Zoom Out (Ctrl+Minus), the nest header's zoom buttons, Nest > First/Last/Next (Ctrl+Right)/Previous (Ctrl+Left) Plate and Tools > Measure Area, as WinForms. The wheel zooms by the WinForms step, panning hides the hover highlight, and resting on a part shows its drawing name after a second. Co-Authored-By: Claude Opus 5.5 --- .../Controls/PlateCanvas.Zoom.cs | 228 ++++++++++++++++++ OpenNest.Avalonia/Controls/PlateCanvas.cs | 22 +- OpenNest.Avalonia/MainWindow.Zoom.cs | 65 +++++ OpenNest.Avalonia/MainWindow.axaml | 19 +- OpenNest.Avalonia/MainWindow.axaml.cs | 1 + OpenNest.Avalonia/NestView.Zoom.cs | 23 ++ OpenNest.Avalonia/NestView.axaml | 6 + 7 files changed, 354 insertions(+), 10 deletions(-) create mode 100644 OpenNest.Avalonia/Controls/PlateCanvas.Zoom.cs create mode 100644 OpenNest.Avalonia/MainWindow.Zoom.cs create mode 100644 OpenNest.Avalonia/NestView.Zoom.cs diff --git a/OpenNest.Avalonia/Controls/PlateCanvas.Zoom.cs b/OpenNest.Avalonia/Controls/PlateCanvas.Zoom.cs new file mode 100644 index 00000000..09fc3161 --- /dev/null +++ b/OpenNest.Avalonia/Controls/PlateCanvas.Zoom.cs @@ -0,0 +1,228 @@ +using Avalonia; +using Avalonia.Input; +using Avalonia.Threading; +using OpenNest.Desktop.Interaction; +using OpenNest.Desktop.Rendering; +using OpenNest.Geometry; +using SkiaSharp; + +namespace OpenNest.Avalonia.Controls +{ + /// + /// View > Zoom In, Zoom Out and Zoom To (Area, Plate, Selected), Tools > Measure Area and + /// the delayed hover tip, as the WinForms plate view: the zoom and measure tools are the + /// shared and , the tip the shared + /// on a dispatcher timer. + /// + public sealed partial class PlateCanvas + { + private readonly PartHoverTip hoverTip = new(); + private DispatcherTimer hoverTimer; + + /// View > Zoom In and the header's button: half again about the view center. + public void ZoomIn() => ZoomAboutCenter(ViewTransform.CommandZoomFactor); + + /// View > Zoom Out and the header's button. + public void ZoomOut() => ZoomAboutCenter(1 / ViewTransform.CommandZoomFactor); + + /// View > Zoom To > Plate: the sheet without parts outside it. + public void ZoomToPlate() => ZoomTo(PlateZoom.PlateArea(plate)); + + /// View > Zoom To > Selected: nothing happens with no part selected. + public void ZoomToSelected() => ZoomTo(PlateZoom.SelectedArea(plate, selection)); + + /// View > Zoom To > Area and the header's button: starts the Zoom Window tool. + public void StartZoomWindow() + { + if (plate != null) + tools.SetTool(new ZoomWindowTool(this, tools)); + } + + /// Tools > Measure Area: starts the tool that shows the largest clear box at the pointer. + public void StartMeasureArea() + { + if (plate != null) + tools.SetTool(new MeasureAreaTool(this)); + } + + private void ZoomAboutCenter(double factor) + { + if (plate == null || Bounds.Width <= 0 || Bounds.Height <= 0) + return; + view.Resize(Bounds.Width, Bounds.Height); + view.ZoomAboutCenter(factor); + fitPending = false; + InvalidateVisual(); + ViewChanged?.Invoke(this, System.EventArgs.Empty); + } + + private void ZoomTo(Box area) + { + if (area == null || Bounds.Width <= 0 || Bounds.Height <= 0) + return; + view.Resize(Bounds.Width, Bounds.Height); + if (!view.ZoomToArea(area)) + return; + fitPending = false; + InvalidateVisual(); + ViewChanged?.Invoke(this, System.EventArgs.Empty); + } + + // Space, on release as in WinForms, turns the measured box. + protected override void OnKeyUp(KeyEventArgs e) + { + base.OnKeyUp(e); + if (e.Key == Key.Space && e.KeyModifiers == KeyModifiers.None && tools.Current is MeasureAreaTool measure) + { + measure.ToggleDirection(); + e.Handled = true; + } + } + + private void InitializeHoverTip() + { + hoverTimer = new DispatcherTimer { Interval = PartHoverTip.Delay }; + hoverTimer.Tick += OnHoverTimer; + // As WinForms: any zoom, pan or resize hides the tip. No redraw is asked for here: + // a fit made while rendering changes the view, and the frame drawn next has no tip. + view.Changed += (_, _) => ClearHoverTip(); + } + + // As the WinForms view: in Select, with no button held and edits not locked, each move + // hides the tip and restarts the delay. + private void UpdateHoverTip(Point at, ToolButton button) + { + if (button != ToolButton.None || EditsLocked || tools.Current is not SelectTool) + { + if (ClearHoverTip()) + InvalidateVisual(); + return; + } + if (hoverTip.Move(new ScreenPoint(at.X, at.Y))) + InvalidateVisual(); + hoverTimer.Stop(); + hoverTimer.Start(); + } + + /// Whether a shown tip was hidden. + private bool ClearHoverTip() + { + hoverTimer?.Stop(); + return hoverTip.Clear(); + } + + private void OnHoverTimer(object sender, System.EventArgs e) + { + hoverTimer.Stop(); + if (hoverTip.Elapse(at => hitTester.PartAt(plate, ToWorld(new Point(at.X, at.Y))))) + InvalidateVisual(); + } + + private void BuildToolOverlays(Scene scene) + { + switch (tools.Current) + { + case ZoomWindowTool { Window: { } window }: + scene.ZoomWindow = window.Box; + break; + case MeasureAreaTool { Area: { } area } measure: + scene.MeasureArea = area; + scene.MeasureLabel = measure.Label; + break; + } + if (hoverTip.Text is { } tip) + { + scene.Tip = tip; + scene.TipAnchor = hoverTip.Anchor; + } + } + + private sealed partial class Scene + { + public Box ZoomWindow; + public Box MeasureArea; + public string MeasureLabel; + public string Tip; + public ScreenPoint TipAnchor; + } + + private sealed partial class PlatePainter + { + private static readonly SKColor ZoomWindowBorder = new(99, 162, 228); + private static readonly SKColor ZoomWindowFill = new(150, 200, 255, 90); + private static readonly SKColor MeasureBorder = new(50, 255, 50); + private static readonly SKColor MeasureFill = new(50, 255, 50, 50); + private static readonly SKColor MeasureText = new(0, 128, 0); // Green + private static readonly SKColor TipBack = new(255, 255, 255, 230); + private static readonly SKColor TipBorder = new(105, 105, 105); // DimGray + + private void DrawToolOverlays() + { + DrawZoomWindow(); + DrawMeasureArea(); + DrawTip(); + } + + // The WinForms Zoom Window box: a blue rectangle with a cross at its center. + private void DrawZoomWindow() + { + if (scene.ZoomWindow is not { } box) + return; + var rect = Screen(box); + using var fill = Fill(ZoomWindowFill); + fill.IsAntialias = false; + canvas.DrawRect(rect, fill); + using var border = Stroke(ZoomWindowBorder); + border.IsAntialias = false; + canvas.DrawRect(rect, border); + const float halfWidth = 10; + canvas.DrawLine(rect.MidX, rect.MidY - halfWidth, rect.MidX, rect.MidY + halfWidth, border); + canvas.DrawLine(rect.MidX - halfWidth, rect.MidY, rect.MidX + halfWidth, rect.MidY, border); + } + + // The measured box in green with its size centered in it, as WinForms (14 pt text). + private void DrawMeasureArea() + { + if (scene.MeasureArea is not { } box) + return; + var rect = Screen(box); + using var border = Stroke(MeasureBorder); + border.IsAntialias = false; + canvas.DrawRect(rect, border); + using var fill = Fill(MeasureFill); + fill.IsAntialias = false; + canvas.DrawRect(rect, fill); + if (string.IsNullOrEmpty(scene.MeasureLabel)) + return; + using var text = new SKPaint + { + Color = MeasureText, + IsAntialias = true, + TextSize = 18.7f, + TextAlign = SKTextAlign.Center, + }; + var metrics = text.FontMetrics; + canvas.DrawText(scene.MeasureLabel, rect.MidX, rect.MidY - (metrics.Ascent + metrics.Descent) / 2, text); + } + + // The drawing name beside the pointer on a white box, placed as WinForms places it. + private void DrawTip() + { + if (string.IsNullOrEmpty(scene.Tip)) + return; + using var text = new SKPaint { Color = SKColors.Black, IsAntialias = true, TextSize = 12 }; + var metrics = text.FontMetrics; + var height = metrics.Descent - metrics.Ascent; + var (x, y, width, boxHeight) = PartHoverTip.Place( + scene.TipAnchor, text.MeasureText(scene.Tip), height, scene.Bounds.Width); + var rect = SKRect.Create((float)x, (float)y, (float)width, (float)boxHeight); + using var back = Fill(TipBack); + canvas.DrawRect(rect, back); + using var border = Stroke(TipBorder); + border.IsAntialias = false; + canvas.DrawRect(rect, border); + canvas.DrawText(scene.Tip, (float)x + 3, (float)y + 2 - metrics.Ascent, text); + } + } + } +} diff --git a/OpenNest.Avalonia/Controls/PlateCanvas.cs b/OpenNest.Avalonia/Controls/PlateCanvas.cs index 80bd9e72..f4032449 100644 --- a/OpenNest.Avalonia/Controls/PlateCanvas.cs +++ b/OpenNest.Avalonia/Controls/PlateCanvas.cs @@ -31,7 +31,7 @@ namespace OpenNest.Avalonia.Controls /// highlights the part under the cursor. Left and right clicks go to the active plate tool /// (, Select by default); Escape steps out of it. /// - public sealed class PlateCanvas : Control, IPlateToolHost + public sealed partial class PlateCanvas : Control, IPlateToolHost { // Paths are built once per part pose and only read afterwards, so the render thread may draw // them while the UI thread builds others. @@ -63,6 +63,7 @@ namespace OpenNest.Avalonia.Controls var changed = !ReferenceEquals(plate, value); plate = value; HoveredPart = null; + ClearHoverTip(); if (changed) { selection.Plate = value; @@ -172,6 +173,7 @@ namespace OpenNest.Avalonia.Controls Focusable = true; hitTester = new PlateHitTester(part => PathsFor(part).Geometry); tools = new PlateToolManager(this); + InitializeHoverTip(); // While the place tool is active, edits move its copies. Edits = new PlateEdits(this) { Tools = tools }; selection.Changed += (_, _) => @@ -371,6 +373,7 @@ namespace OpenNest.Avalonia.Controls BuildCommonLines(scene); if (options.DrawSupport) BuildSupport(scene); + BuildToolOverlays(scene); } // A selected cut-off is drawn solid blue in place of its dashes, as in WinForms. @@ -461,7 +464,8 @@ namespace OpenNest.Avalonia.Controls return; } var at = e.GetPosition(this); - view.ZoomAt(new ScreenPoint(at.X, at.Y), System.Math.Pow(1.2, e.Delta.Y)); + // The WinForms step per wheel notch. + view.ZoomAt(new ScreenPoint(at.X, at.Y), System.Math.Pow(ViewTransform.ZoomInFactor, e.Delta.Y)); InvalidateVisual(); ViewChanged?.Invoke(this, EventArgs.Empty); e.Handled = true; @@ -505,6 +509,8 @@ namespace OpenNest.Avalonia.Controls CursorWorld = ToWorld(at); if (panStart is { } start) { + // As WinForms: moving with a button held hides the hover highlight and tip. + ClearHover(); view.Pan(at.X - start.X, at.Y - start.Y); panStart = at; InvalidateVisual(); @@ -523,6 +529,7 @@ namespace OpenNest.Avalonia.Controls HoveredPart = hovered; InvalidateVisual(); } + UpdateHoverTip(at, button); } ViewChanged?.Invoke(this, EventArgs.Empty); } @@ -585,10 +592,10 @@ namespace OpenNest.Avalonia.Controls private void ClearHover() { - if (HoveredPart == null) - return; + var shown = ClearHoverTip() | HoveredPart != null; HoveredPart = null; - InvalidateVisual(); + if (shown) + InvalidateVisual(); } private ToolPointer ToolPointerAt(Point at, ToolButton button, KeyModifiers keys) => @@ -670,7 +677,7 @@ namespace OpenNest.Avalonia.Controls Vector? Tab); /// Everything one frame draws, built on the UI thread and only read on the render thread. - private sealed class Scene + private sealed partial class Scene { public Rect Bounds; public SkiaSchemePalette Palette; @@ -738,7 +745,7 @@ namespace OpenNest.Avalonia.Controls /// Paints a scene in the WinForms view's order and pen sizes: widths and dashes are screen /// pixels, so parts drawn in world units scale them by the zoom. /// - private sealed class PlatePainter + private sealed partial class PlatePainter { private static readonly SKColor LeadColor = new(255, 69, 0); // OrangeRed private static readonly SKColor DisplayColor = new(70, 110, 140); @@ -841,6 +848,7 @@ namespace OpenNest.Avalonia.Controls DrawCommonLines(); DrawSupport(); DrawSelectionWindow(); + DrawToolOverlays(); } private void DrawSheet() diff --git a/OpenNest.Avalonia/MainWindow.Zoom.cs b/OpenNest.Avalonia/MainWindow.Zoom.cs new file mode 100644 index 00000000..3b631c65 --- /dev/null +++ b/OpenNest.Avalonia/MainWindow.Zoom.cs @@ -0,0 +1,65 @@ +using System; +using Avalonia.Input; +using Avalonia.Interactivity; +using OpenNest.Avalonia.Controls; + +namespace OpenNest.Avalonia +{ + /// + /// View > Zoom To (Area, Fit, Plate, Selected), Zoom In (Ctrl+Plus) and Zoom Out + /// (Ctrl+Minus); Nest > First, Last, Next (Ctrl+Right) and Previous (Ctrl+Left) Plate; and + /// Tools > Measure Area, as the WinForms main form's menus. + /// + public partial class MainWindow + { + private void AddViewKeyBindings() + { + foreach (var (key, action) in new (Key, Action)[] + { + (Key.OemPlus, () => OnZoomIn(null, null)), + (Key.OemMinus, () => OnZoomOut(null, null)), + (Key.Right, () => StepPlate(1)), + (Key.Left, () => StepPlate(-1)), + }) + { + KeyBindings.Add(new KeyBinding + { + Gesture = new KeyGesture(key, KeyModifiers.Control), + Command = new Command(action), + }); + } + } + + private void OnZoomIn(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomIn(); + + private void OnZoomOut(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomOut(); + + private void OnZoomToPlate(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomToPlate(); + + private void OnZoomToSelected(object sender, RoutedEventArgs e) => ActiveView?.PlateView.ZoomToSelected(); + + private void OnZoomToArea(object sender, RoutedEventArgs e) => StartTool(canvas => canvas.StartZoomWindow()); + + private void OnMeasureArea(object sender, RoutedEventArgs e) => StartTool(canvas => canvas.StartMeasureArea()); + + // The view takes the keyboard, so Escape (and Space for Measure Area) reach the tool after + // a menu click. + private void StartTool(Action start) + { + if (ActiveView is not { PlateView: { } canvas } view) + return; + start(canvas); + // Focusing the view would otherwise place a drawing picked up in the list. + view.CancelPickup(); + canvas.Focus(); + } + + private void OnFirstPlate(object sender, RoutedEventArgs e) => ActiveView?.ShowPlate(0); + + private void OnLastPlate(object sender, RoutedEventArgs e) + { + if (ActiveView is { } view) + view.ShowPlate(view.PlateCount - 1); + } + } +} diff --git a/OpenNest.Avalonia/MainWindow.axaml b/OpenNest.Avalonia/MainWindow.axaml index d70d4cb0..45f3742a 100644 --- a/OpenNest.Avalonia/MainWindow.axaml +++ b/OpenNest.Avalonia/MainWindow.axaml @@ -35,6 +35,13 @@ + + + + + + + @@ -54,14 +61,20 @@ - - - + + + + + + + + + diff --git a/OpenNest.Avalonia/MainWindow.axaml.cs b/OpenNest.Avalonia/MainWindow.axaml.cs index 99ab51ad..6df96388 100644 --- a/OpenNest.Avalonia/MainWindow.axaml.cs +++ b/OpenNest.Avalonia/MainWindow.axaml.cs @@ -115,6 +115,7 @@ namespace OpenNest.Avalonia Gesture = new KeyGesture(Key.F6), Command = new Command(() => OnSetRotationIncrement(null, null)), }); + AddViewKeyBindings(); // Ctrl+Tab before focus navigation takes the Tab key. AddHandler(KeyDownEvent, OnPreviewKeyDown, RoutingStrategies.Tunnel); // .nest files dropped anywhere on the window, the nest view included, open in tabs. diff --git a/OpenNest.Avalonia/NestView.Zoom.cs b/OpenNest.Avalonia/NestView.Zoom.cs new file mode 100644 index 00000000..a592e06a --- /dev/null +++ b/OpenNest.Avalonia/NestView.Zoom.cs @@ -0,0 +1,23 @@ +using Avalonia.Interactivity; + +namespace OpenNest.Avalonia +{ + /// The nest header's zoom buttons, as the WinForms nest window's. + public partial class NestView + { + private void OnZoomOut(object sender, RoutedEventArgs e) => Canvas.ZoomOut(); + + private void OnZoomIn(object sender, RoutedEventArgs e) => Canvas.ZoomIn(); + + private void OnZoomToFit(object sender, RoutedEventArgs e) => Canvas.ZoomToFit(); + + // The view takes the keyboard so Escape reaches the tool; a drawing picked up in the + // list would otherwise be placed by that focus. + private void OnZoomToArea(object sender, RoutedEventArgs e) + { + Canvas.StartZoomWindow(); + CancelPickup(); + Canvas.Focus(); + } + } +} diff --git a/OpenNest.Avalonia/NestView.axaml b/OpenNest.Avalonia/NestView.axaml index 75ec11bf..c9bbf2bc 100644 --- a/OpenNest.Avalonia/NestView.axaml +++ b/OpenNest.Avalonia/NestView.axaml @@ -34,6 +34,12 @@