Skip to content

A Swiss Army knife for developers.

[Windows] the window may be off screen after unplugging a monitor (#1321)

Etienne Baudoux6d56e37f2efd
diff --git a/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs b/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
index 512ada995..ed6b02235 100644
--- a/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
+++ b/src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
@@ -209,21 +209,29 @@ public partial class MainWindow : MicaWindowWithOverlay
double DPI_SCALE = dpiHelper.LogicalToDeviceUnitsScalingFactorX;
var windowInteropHelper = new WindowInteropHelper(this);
var screen = Screen.FromHandle(windowInteropHelper.Handle);
+ Guard.IsNotNull(screen);
SixLabors.ImageSharp.Rectangle? bounds = _settingsProvider.GetSetting(PredefinedSettings.MainWindowBounds);
-
- if (bounds is null)
+ bool isWindowVisibleOnScreen
+ = bounds is not null
+ && screen.WorkingArea.Contains(
+ new Rectangle(
+ bounds.Value.X,
+ bounds.Value.Y,
+ bounds.Value.Width,
+ bounds.Value.Height));
+
+ if (!isWindowVisibleOnScreen || bounds is null)
{
int width = (int)(Math.Max(screen.WorkingArea.Width - 400, 1200) / DPI_SCALE);
int height = (int)(Math.Max(screen.WorkingArea.Height - 200, 600) / DPI_SCALE);
// Center the window on the screen.
- bounds
- = new(
- x: (int)(((screen.WorkingArea.Width / DPI_SCALE) - width) / 2),
- y: (int)(((screen.WorkingArea.Height / DPI_SCALE) - height) / 2),
- width,
- height);
+ bounds = new(
+ x: (int)(((screen.WorkingArea.Width / DPI_SCALE) - width) / 2),
+ y: (int)(((screen.WorkingArea.Height / DPI_SCALE) - height) / 2),
+ width,
+ height);
}
Left = bounds.Value.X;