mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-28 01:31:34 -05:00
fix(reporting): open the report viewer centered on the main form
FormStartPosition.CenterParent is not applied to modeless windows, so the viewer opened wherever Windows placed it. Center it on its owner in OnLoad, shrunk to fit and kept inside that screen's working area. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
b026bd19d2
commit
2b23150801
2 files changed
+35
-3
No files matched your search
@@ -285,6 +285,22 @@ public class NestReportExportTests
|
||||
Assert.Equal(before, Directory.Exists(reports) ? Directory.GetDirectories(reports).Length : 0);
|
||||
});
|
||||
|
||||
[Fact]
|
||||
public void ViewerCentersOnItsOwnerInsideTheScreen()
|
||||
{
|
||||
var area = Screen.PrimaryScreen!.WorkingArea;
|
||||
var owner = new System.Drawing.Rectangle(area.Left + 100, area.Top + 50, 600, 400);
|
||||
|
||||
var small = NestReportViewerForm.CenteredBounds(owner, new System.Drawing.Size(200, 100));
|
||||
Assert.Equal(owner.Left + 200, small.Left);
|
||||
Assert.Equal(owner.Top + 150, small.Top);
|
||||
|
||||
// Larger than the owner: still centered on it, then shrunk and kept on screen.
|
||||
var large = NestReportViewerForm.CenteredBounds(owner,
|
||||
new System.Drawing.Size(area.Width + 500, area.Height + 500));
|
||||
Assert.Equal(area, large);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BusyAfterDialogRejectsWriteWithoutTouchingDestination() => RunSta(() =>
|
||||
{
|
||||
|
||||
@@ -25,9 +25,9 @@ namespace OpenNest.Forms
|
||||
this.suggestedFileName = suggestedFileName;
|
||||
Text = $"Nest Report - {nestName}";
|
||||
Icon = Properties.Resources.app_icon;
|
||||
StartPosition = FormStartPosition.CenterParent;
|
||||
var area = Screen.FromPoint(Cursor.Position).WorkingArea;
|
||||
Size = new Size(System.Math.Min(1000, area.Width), System.Math.Min(900, area.Height));
|
||||
// CenterParent is ignored for modeless windows; OnLoad centers on the owner instead.
|
||||
StartPosition = FormStartPosition.Manual;
|
||||
Size = new Size(1000, 900);
|
||||
|
||||
var saveButton = new ToolStripButton("Save As...") { Name = "btnSaveAs" };
|
||||
saveButton.Click += (_, _) => SaveAs();
|
||||
@@ -45,6 +45,7 @@ namespace OpenNest.Forms
|
||||
|
||||
protected override async void OnLoad(EventArgs e)
|
||||
{
|
||||
Bounds = CenteredBounds(Owner?.Bounds ?? Screen.FromPoint(Cursor.Position).WorkingArea, Size);
|
||||
base.OnLoad(e);
|
||||
try
|
||||
{
|
||||
@@ -76,6 +77,21 @@ namespace OpenNest.Forms
|
||||
printButton.Enabled = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Centers <paramref name="size"/> on <paramref name="owner"/>, shrunk to fit and kept inside
|
||||
/// the working area of the screen holding the owner's center.
|
||||
/// </summary>
|
||||
internal static Rectangle CenteredBounds(Rectangle owner, Size size)
|
||||
{
|
||||
var center = new Point(owner.Left + owner.Width / 2, owner.Top + owner.Height / 2);
|
||||
var area = Screen.FromPoint(center).WorkingArea;
|
||||
var width = System.Math.Min(size.Width, area.Width);
|
||||
var height = System.Math.Min(size.Height, area.Height);
|
||||
var left = System.Math.Clamp(center.X - width / 2, area.Left, area.Right - width);
|
||||
var top = System.Math.Clamp(center.Y - height / 2, area.Top, area.Bottom - height);
|
||||
return new Rectangle(left, top, width, height);
|
||||
}
|
||||
|
||||
private void SaveAs()
|
||||
{
|
||||
using var dlg = new SaveFileDialog
|
||||
|
||||
Reference in new issue
Block a user