Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Jul 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/app/dev/platforms/desktop/DevToys.Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,21 +209,29 @@ private void SetPositionAndSize()
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;
Expand Down
Loading