using System; using OpenNest.Geometry; namespace OpenNest.Desktop.Interaction; /// /// The WinForms Zoom Window action (View > Zoom To > Area), toolkit-neutral: a left drag of /// more than 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. /// public sealed class ZoomWindowTool : PlateTool { /// A drag longer than this, in pixels, zooms on release; a shorter one waits for a second click. public const double DragPixels = 40; private readonly PlateToolManager tools; private bool awaitingSecondCorner; /// The manager hosting the tool, which returns to Select after the zoom. 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; /// Busy once the first corner is set and the tool waits for the second click. public override bool IsBusy => awaitingSecondCorner; /// The box between the corners so far, or null before the first press. 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(); } }