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;