From 6b48a986fde699e2cff270726876195eeeb01c53 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Mon, 16 Nov 2026 07:19:39 -0500 Subject: [PATCH] feat(plate-view): scale the view with the control when it resizes Opening the Cutting Inspection panel narrowed the plate view but kept its scale, so a plate that fit was left hanging off the edge until the user zoomed out. A resize now keeps the world point at the center in place and scales the view by the change in the control's usable size (less the fit border), taking the tighter of width and height, so a fitted plate stays fitted. The scale is measured from the size and zoom when the user last zoomed, so closing the panel restores exactly the zoom it took away; tiny or minimized sizes keep the old recentering only. Co-Authored-By: Claude Opus 5.5 --- .../Controls/PlateViewResizeTests.cs | 79 +++++++++++++++++++ OpenNest/Controls/DrawControl.cs | 47 ++++++++++- 2 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 OpenNest.WinForms.Tests/Controls/PlateViewResizeTests.cs diff --git a/OpenNest.WinForms.Tests/Controls/PlateViewResizeTests.cs b/OpenNest.WinForms.Tests/Controls/PlateViewResizeTests.cs new file mode 100644 index 00000000..1aa7aa09 --- /dev/null +++ b/OpenNest.WinForms.Tests/Controls/PlateViewResizeTests.cs @@ -0,0 +1,79 @@ +using OpenNest.Controls; +using OpenNest.Geometry; + +namespace OpenNest.WinForms.Tests.Controls; + +/// Resizing the plate view scales the view with it, so a fitted plate stays fitted. +public class PlateViewResizeTests +{ + private static void Run(Action body) => + StaTestThread.Run(body, TimeSpan.FromSeconds(30), "Plate view resize test timed out."); + + private static PlateView FittedView() + { + var view = new PlateView { Plate = new Plate(60, 120), Size = new System.Drawing.Size(1000, 600) }; + view.ZoomToFit(); + return view; + } + + private static void AssertPlateInside(PlateView view) + { + var lower = view.PointWorldToControl(new Vector(0, 0)); + var upper = view.PointWorldToControl(new Vector(120, 60)); + Assert.InRange(lower.X, 0, view.Width); + Assert.InRange(upper.X, 0, view.Width); + Assert.InRange(lower.Y, 0, view.Height); + Assert.InRange(upper.Y, 0, view.Height); + } + + [Fact] + public void NarrowingTheViewKeepsAFittedPlateInView() => Run(() => + { + using var view = FittedView(); + var center = view.PointControlToWorld(500, 300); + + view.Size = new System.Drawing.Size(520, 600); + + AssertPlateInside(view); + var moved = view.PointControlToWorld(260, 300); + Assert.Equal(center.X, moved.X, 1); + Assert.Equal(center.Y, moved.Y, 1); + }); + + [Fact] + public void RestoringTheSizeRestoresTheZoom() => Run(() => + { + using var view = FittedView(); + var scale = view.ViewScale; + + view.Size = new System.Drawing.Size(520, 600); + view.Size = new System.Drawing.Size(700, 600); + view.Size = new System.Drawing.Size(1000, 600); + + Assert.Equal(scale, view.ViewScale, 4); + }); + + [Fact] + public void ZoomingWhileNarrowMeasuresLaterResizesFromThatZoom() => Run(() => + { + using var view = FittedView(); + view.Size = new System.Drawing.Size(520, 600); + view.ZoomToPoint(new Vector(60, 30), 2f); + var zoomed = view.ViewScale; + + view.Size = new System.Drawing.Size(400, 600); + + Assert.Equal(zoomed * (400 - 50) / (520f - 50), view.ViewScale, 3); + }); + + [Fact] + public void GrowingOnlyTheUnconstrainedSideKeepsTheZoom() => Run(() => + { + using var view = FittedView(); + var scale = view.ViewScale; + + view.Size = new System.Drawing.Size(1000, 900); + + Assert.Equal(scale, view.ViewScale, 4); + }); +} diff --git a/OpenNest/Controls/DrawControl.cs b/OpenNest/Controls/DrawControl.cs index ce6cfc43..18a8e2aa 100644 --- a/OpenNest/Controls/DrawControl.cs +++ b/OpenNest/Controls/DrawControl.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; @@ -163,13 +163,54 @@ namespace OpenNest.Controls { base.OnResize(e); - origin.X += (Size.Width - lastSize.Width) * 0.5f; - origin.Y += (Size.Height - lastSize.Height) * 0.5f; + if (CanScaleOnResize(lastSize) && CanScaleOnResize(Size)) + ScaleViewToSize(); + else + { + origin.X += (Size.Width - lastSize.Width) * 0.5f; + origin.Y += (Size.Height - lastSize.Height) * 0.5f; + } lastSize = Size; Invalidate(); } + // The view the resize scaling is measured from: the control size and scale when the + // user last zoomed. Measuring from it, not from the previous size, lets a panel that + // opens and closes beside the view hand back exactly the zoom it took away. + private System.Drawing.Size resizeReferenceSize; + private float resizeReferenceScale; + private float resizeAppliedScale = float.NaN; + + private static bool CanScaleOnResize(System.Drawing.Size size) => + size.Width > BorderWidth && size.Height > BorderWidth; + + /// + /// Keeps what the user was looking at in view as the control resizes: the world point at + /// the center stays at the center, and the scale follows the control's usable size, so a + /// view that fit the plate still fits it when a side panel takes part of the width. + /// + private void ScaleViewToSize() + { + if (ViewScale != resizeAppliedScale || !CanScaleOnResize(resizeReferenceSize)) + { + resizeReferenceSize = lastSize; + resizeReferenceScale = ViewScale; + } + + var center = PointControlToWorld(lastSize.Width / 2, lastSize.Height / 2); + var ratio = System.Math.Min( + (Size.Width - BorderWidth) / (float)(resizeReferenceSize.Width - BorderWidth), + (Size.Height - BorderWidth) / (float)(resizeReferenceSize.Height - BorderWidth)); + var scale = System.Math.Clamp(resizeReferenceScale * ratio, ViewScaleMin, ViewScaleMax); + + ViewScale = scale; + origin.X = Size.Width * 0.5f - (float)center.X * scale; + origin.Y = Size.Height * 0.5f + (float)center.Y * scale; + UpdateMatrix(); + resizeAppliedScale = ViewScale; + } + public float LengthWorldToGui(double length) { return ViewScale * (float)length;