Files
OpenNest/OpenNest.Desktop/Interaction/ZoomWindowTool.cs
T
ajandClaude Opus 5.5 cf6a7df3be feat(desktop): share zoom window, measure area, hover tip and resize scaling
ZoomWindowTool and MeasureAreaTool port the WinForms Zoom Window and Select
Area actions onto the shared plate tool framework; PartHoverTip is the plate
view's delayed drawing-name tooltip; PlateZoom gives the Zoom To areas.
ViewTransform now scales with the viewport on resize as DrawControl does and
zooms about its center by the menu's 1.5 step.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 20:09:17 -05:00

106 lines
3.4 KiB
C#

using System;
using OpenNest.Geometry;
namespace OpenNest.Desktop.Interaction;
/// <summary>
/// The WinForms Zoom Window action (View &gt; Zoom To &gt; Area), toolkit-neutral: a left drag of
/// more than <see cref="DragPixels"/> zooms to the box dragged; a shorter press sets the first
/// corner, and the next click sets the second and zooms. A right press cancels the box. One zoom
/// ends the tool, and Escape from Select does not bring it back.
/// </summary>
public sealed class ZoomWindowTool : PlateTool
{
/// <summary>A drag longer than this, in pixels, zooms on release; a shorter one waits for a second click.</summary>
public const double DragPixels = 40;
private readonly PlateToolManager tools;
private bool awaitingSecondCorner;
/// <param name="tools">The manager hosting the tool, which returns to Select after the zoom.</param>
public ZoomWindowTool(IPlateToolHost host, PlateToolManager tools)
: base(host)
{
this.tools = tools ?? throw new ArgumentNullException(nameof(tools));
}
public override string Name => "Zoom Window";
// One zoom ends this tool; Escape from Select must not bring it back.
public override bool ResumeOnEscape => false;
/// <summary>Busy once the first corner is set and the tool waits for the second click.</summary>
public override bool IsBusy => awaitingSecondCorner;
/// <summary>The box between the corners so far, or null before the first press.</summary>
public SelectionWindow? Window { get; private set; }
public override void Cancel()
{
Window = null;
awaitingSecondCorner = false;
Host.Invalidate();
}
public override bool PointerPressed(ToolPointer pointer)
{
if (pointer.Button == ToolButton.Right)
{
Cancel();
return true;
}
if (pointer.Button != ToolButton.Left)
return false;
if (!awaitingSecondCorner)
{
Window = new SelectionWindow(pointer.World, pointer.World);
Host.Invalidate();
}
return true;
}
public override bool PointerMoved(ToolPointer pointer)
{
if (Window is not { } window || !(awaitingSecondCorner || pointer.Button == ToolButton.Left))
return false;
Window = window with { Second = pointer.World };
Host.Invalidate();
return true;
}
public override bool PointerReleased(ToolPointer pointer)
{
if (pointer.Button != ToolButton.Left || Window is not { } window)
return false;
window = window with { Second = pointer.World };
Window = window;
if (!awaitingSecondCorner
&& Host.View.LengthToScreen(window.First.DistanceTo(window.Second)) <= DragPixels)
{
awaitingSecondCorner = true;
Host.Invalidate();
return true;
}
ZoomAndFinish(window);
return true;
}
// A drag cut short (Alt+Tab, say) drops the box; a box waiting for its second click stays.
public override void PointerCaptureLost()
{
if (awaitingSecondCorner)
return;
Window = null;
Host.Invalidate();
}
private void ZoomAndFinish(SelectionWindow window)
{
// A box with no width or height zooms nothing, as WinForms, but still ends the tool.
Host.View.ZoomToArea(window.Box);
Window = null;
awaitingSecondCorner = false;
tools.Select();
}
}