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 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-29 20:09:17 -05:00
1 parent cf6a7df3be
commit ca6e8cd4ce
7 files changed
+354 -10

No files matched your search

@@ -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
{
/// <summary>
/// View &gt; Zoom In, Zoom Out and Zoom To (Area, Plate, Selected), Tools &gt; Measure Area and
/// the delayed hover tip, as the WinForms plate view: the zoom and measure tools are the
/// shared <see cref="ZoomWindowTool"/> and <see cref="MeasureAreaTool"/>, the tip the shared
/// <see cref="PartHoverTip"/> on a dispatcher timer.
/// </summary>
public sealed partial class PlateCanvas
{
private readonly PartHoverTip hoverTip = new();
private DispatcherTimer hoverTimer;
/// <summary>View &gt; Zoom In and the header's button: half again about the view center.</summary>
public void ZoomIn() => ZoomAboutCenter(ViewTransform.CommandZoomFactor);
/// <summary>View &gt; Zoom Out and the header's button.</summary>
public void ZoomOut() => ZoomAboutCenter(1 / ViewTransform.CommandZoomFactor);
/// <summary>View &gt; Zoom To &gt; Plate: the sheet without parts outside it.</summary>
public void ZoomToPlate() => ZoomTo(PlateZoom.PlateArea(plate));
/// <summary>View &gt; Zoom To &gt; Selected: nothing happens with no part selected.</summary>
public void ZoomToSelected() => ZoomTo(PlateZoom.SelectedArea(plate, selection));
/// <summary>View &gt; Zoom To &gt; Area and the header's button: starts the Zoom Window tool.</summary>
public void StartZoomWindow()
{
if (plate != null)
tools.SetTool(new ZoomWindowTool(this, tools));
}
/// <summary>Tools &gt; Measure Area: starts the tool that shows the largest clear box at the pointer.</summary>
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();
}
/// <returns>Whether a shown tip was hidden.</returns>
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);
}
}
}
}
+15 -7
View File
@@ -31,7 +31,7 @@ namespace OpenNest.Avalonia.Controls
/// highlights the part under the cursor. Left and right clicks go to the active plate tool
/// (<see cref="Tools"/>, Select by default); Escape steps out of it.
/// </summary>
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);
/// <summary>Everything one frame draws, built on the UI thread and only read on the render thread.</summary>
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.
/// </summary>
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()
+65
View File
@@ -0,0 +1,65 @@
using System;
using Avalonia.Input;
using Avalonia.Interactivity;
using OpenNest.Avalonia.Controls;
namespace OpenNest.Avalonia
{
/// <summary>
/// View &gt; Zoom To (Area, Fit, Plate, Selected), Zoom In (Ctrl+Plus) and Zoom Out
/// (Ctrl+Minus); Nest &gt; First, Last, Next (Ctrl+Right) and Previous (Ctrl+Left) Plate; and
/// Tools &gt; Measure Area, as the WinForms main form's menus.
/// </summary>
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<PlateCanvas> 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);
}
}
}
+16 -3
View File
@@ -35,6 +35,13 @@
<MenuItem x:Name="SelectAllMenuItem" Header="Select _All" InputGesture="Ctrl+A" Click="OnSelectAll" />
</MenuItem>
<MenuItem Header="_Nest">
<MenuItem Header="_First Plate" Click="OnFirstPlate" />
<MenuItem Header="_Last Plate" Click="OnLastPlate" />
<Separator />
<!-- Page Down and Page Up also step plates, as the nest header's buttons say. -->
<MenuItem Header="_Next Plate" InputGesture="Ctrl+Right" Click="OnNextPlate" />
<MenuItem Header="_Previous Plate" InputGesture="Ctrl+Left" Click="OnPreviousPlate" />
<Separator />
<MenuItem x:Name="AutoNestMenuItem" Header="_Auto Nest" Click="OnAutoNest" />
</MenuItem>
<MenuItem Header="_Plate">
@@ -54,14 +61,20 @@
<MenuItem x:Name="DrawSupportMenuItem" Header="Draw _Support" ToggleType="CheckBox"
Click="OnToggleDrawSupport" />
<Separator />
<MenuItem Header="Zoom to _Fit" InputGesture="F" Click="OnFit" />
<MenuItem Header="_Previous Plate" InputGesture="PageUp" Click="OnPreviousPlate" />
<MenuItem Header="_Next Plate" InputGesture="PageDown" Click="OnNextPlate" />
<MenuItem Header="_Zoom To">
<MenuItem Header="_Area" Click="OnZoomToArea" />
<MenuItem Header="_Fit" InputGesture="F" Click="OnFit" />
<MenuItem Header="_Plate" Click="OnZoomToPlate" />
<MenuItem Header="_Selected" Click="OnZoomToSelected" />
</MenuItem>
<MenuItem Header="Zoom _In" InputGesture="Ctrl+OemPlus" Click="OnZoomIn" />
<MenuItem Header="Zoom _Out" InputGesture="Ctrl+OemMinus" Click="OnZoomOut" />
<Separator />
<MenuItem Header="Next N_est" InputGesture="Ctrl+Tab" Click="OnNextNest" />
<MenuItem Header="Previous Ne_st" InputGesture="Ctrl+Shift+Tab" Click="OnPreviousNest" />
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_Measure Area" Click="OnMeasureArea" />
<MenuItem x:Name="OptionsMenuItem" Header="_Options..." Click="OnOptions" />
<Separator />
<MenuItem x:Name="SetOffsetIncrementMenuItem" Header="Set Offset Increment" InputGesture="F5" Click="OnSetOffsetIncrement" />
+1
View File
@@ -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.
+23
View File
@@ -0,0 +1,23 @@
using Avalonia.Interactivity;
namespace OpenNest.Avalonia
{
/// <summary>The nest header's zoom buttons, as the WinForms nest window's.</summary>
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();
}
}
}
+6
View File
@@ -34,6 +34,12 @@
<TextBlock x:Name="PlateLabel" VerticalAlignment="Center" FontSize="13" Margin="6,0" />
<Button Content="&#x25B6;" ToolTip.Tip="Next plate (Page Down)" Click="OnNextPlate" />
<Button Content="&#x23ED;" ToolTip.Tip="Last plate" Click="OnLastPlate" />
<!-- The zoom buttons, as the WinForms nest window's header: half again about the center. -->
<Border Width="1" Margin="4,2" Background="#D0D4D8" />
<Button Content="&#x2212;" ToolTip.Tip="Zoom Out (Ctrl+Minus)" Click="OnZoomOut" />
<Button Content="+" ToolTip.Tip="Zoom In (Ctrl+Plus)" Click="OnZoomIn" />
<Button Content="Fit" ToolTip.Tip="Zoom To Fit (F)" Click="OnZoomToFit" />
<Button Content="Area" ToolTip.Tip="Zoom To Area" Click="OnZoomToArea" />
</StackPanel>
</Border>
<c:PlateCanvas x:Name="Canvas" />