From 0549eb442e8d1e8d36327aa8493c3e9fd095a864 Mon Sep 17 00:00:00 2001 From: donlaci Date: Thu, 9 Jan 2025 13:04:00 +0100 Subject: [PATCH 1/2] [Workspaces] fixing bug: editor starts outside of visible desktop area --- .../WorkspacesEditor/MainWindow.xaml.cs | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs b/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs index 8f9944cc9034..f35026091532 100644 --- a/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs +++ b/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs @@ -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; @@ -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); @@ -88,6 +89,20 @@ public MainWindow(MainViewModel mainViewModel) cancellationToken.Token); } + private bool IsEditorInsideVisibleArea() + { + System.Windows.Forms.Screen[] allScreens = System.Windows.Forms.Screen.AllScreens; + 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) From cf5b8a3707a39245844b7ccbc43ae4d2c4563dfd Mon Sep 17 00:00:00 2001 From: Laszlo Nemeth <57342539+donlaci@users.noreply.github.com> Date: Mon, 13 Jan 2025 20:11:59 +0100 Subject: [PATCH 2/2] Update src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs Co-authored-by: Seraphima Zykova --- src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs b/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs index f35026091532..f5fc7bcac609 100644 --- a/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs +++ b/src/modules/Workspaces/WorkspacesEditor/MainWindow.xaml.cs @@ -91,7 +91,7 @@ public MainWindow(MainViewModel mainViewModel) private bool IsEditorInsideVisibleArea() { - System.Windows.Forms.Screen[] allScreens = System.Windows.Forms.Screen.AllScreens; + System.Windows.Forms.Screen[] allScreens = MonitorHelper.GetDpiUnawareScreens(); Rectangle commonBounds = allScreens[0].Bounds; for (int screenIndex = 1; screenIndex < allScreens.Length; screenIndex++) {