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

[Workspaces] fixing bug: editor starts outside of visible desktop area #36769

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
21 changes: 18 additions & 3 deletions src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Interop;

using ManagedCommon;
using Microsoft.PowerToys.Telemetry;
using WorkspacesEditor.Utils;
Expand All @@ -30,9 +31,9 @@ public MainWindow(MainViewModel mainViewModel)
MainViewModel = mainViewModel;
mainViewModel.SetMainWindow(this);

if (Properties.Settings.Default.Height == -1)
if (Properties.Settings.Default.Height == -1 || !IsEditorInsideVisibleArea())
{
// This is the very first time the window is created. Place it on the screen center
// This is the very first time the window is created or it would be placed outside the visible area (monitor rearrangement). Place it on the screen center
WindowInteropHelper windowInteropHelper = new WindowInteropHelper(this);
System.Windows.Forms.Screen screen = System.Windows.Forms.Screen.FromHandle(windowInteropHelper.Handle);
double dpi = MonitorHelper.GetScreenDpiFromScreen(screen);
Expand Down Expand Up @@ -88,6 +89,20 @@ public MainWindow(MainViewModel mainViewModel)
cancellationToken.Token);
}

private bool IsEditorInsideVisibleArea()
{
System.Windows.Forms.Screen[] allScreens = MonitorHelper.GetDpiUnawareScreens();
Rectangle commonBounds = allScreens[0].Bounds;
for (int screenIndex = 1; screenIndex < allScreens.Length; screenIndex++)
{
Rectangle rectangle = allScreens[screenIndex].Bounds;
commonBounds = Rectangle.Union(rectangle, commonBounds);
}

Rectangle editorBounds = new Rectangle((int)Properties.Settings.Default.Left, (int)Properties.Settings.Default.Top, (int)Properties.Settings.Default.Width, (int)Properties.Settings.Default.Height);
return editorBounds.IntersectsWith(commonBounds);
}

private void SavePosition()
{
if (WindowState == WindowState.Maximized)
Expand Down
Loading