Fix Settings window going off-screen after disconnecting 4K monitor

- Fix IsPositionValid to use SystemParameters.VirtualScreen* (DIP units)
  instead of MonitorInfo.WorkingArea (physical pixels) to avoid DPI
  coordinate mismatch on high-DPI and mixed-DPI multi-monitor setups.

- Fix AdjustWindowPosition and SetWindowPosition to correctly calculate
  the max boundary as VirtualScreenTop + VirtualScreenHeight - ActualHeight
  (was VirtualScreenHeight - ActualHeight), which incorrectly allowed
  windows to extend beyond the bottom/right edge when VirtualScreenTop
  or VirtualScreenLeft were non-zero (e.g., monitors above/left of primary).

Co-authored-by: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot] 2026-03-10 14:20:38 +00:00 committed by VictoriousRaptor
parent 8d042d2134
commit a8e755e561

View file

@ -179,8 +179,8 @@ public partial class SettingWindow
// Ensure window does not exceed screen boundaries
top = Math.Max(top, SystemParameters.VirtualScreenTop);
left = Math.Max(left, SystemParameters.VirtualScreenLeft);
top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight);
left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth);
top = Math.Min(top, SystemParameters.VirtualScreenTop + SystemParameters.VirtualScreenHeight - ActualHeight);
left = Math.Min(left, SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth - ActualWidth);
Top = top;
Left = left;
@ -191,23 +191,19 @@ public partial class SettingWindow
// Adjust window position if it exceeds screen boundaries
top = Math.Max(top, SystemParameters.VirtualScreenTop);
left = Math.Max(left, SystemParameters.VirtualScreenLeft);
top = Math.Min(top, SystemParameters.VirtualScreenHeight - ActualHeight);
left = Math.Min(left, SystemParameters.VirtualScreenWidth - ActualWidth);
top = Math.Min(top, SystemParameters.VirtualScreenTop + SystemParameters.VirtualScreenHeight - ActualHeight);
left = Math.Min(left, SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth - ActualWidth);
}
private static bool IsPositionValid(double top, double left)
{
foreach (var screen in MonitorInfo.GetDisplayMonitors())
{
var workingArea = screen.WorkingArea;
if (left >= workingArea.Left && left < workingArea.Right &&
top >= workingArea.Top && top < workingArea.Bottom)
{
return true;
}
}
return false;
// Use SystemParameters (DIP units) to match the coordinate system of Window.Top/Left.
// MonitorInfo.WorkingArea uses physical pixels which can differ from DIP units when DPI
// scaling is active, leading to incorrect results on high-DPI or mixed-DPI setups.
return left >= SystemParameters.VirtualScreenLeft &&
left < SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth &&
top >= SystemParameters.VirtualScreenTop &&
top < SystemParameters.VirtualScreenTop + SystemParameters.VirtualScreenHeight;
}
private double WindowLeft()