Skip to content

Commit a4926ed

Browse files
committed
Scale down screens to make sure the window is actually intersecting them
1 parent f993efa commit a4926ed

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

Text-Grab/Utilities/WindowUtilities.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,24 @@ public static void SetWindowPosition(Window passedWindow)
4242
if (storedPosition != null
4343
&& storedPosition.Count == 4)
4444
{
45-
bool couldParseAll = false;
46-
couldParseAll = double.TryParse(storedPosition[0], out double parsedX);
47-
couldParseAll = double.TryParse(storedPosition[1], out double parsedY);
48-
couldParseAll = double.TryParse(storedPosition[2], out double parsedWid);
49-
couldParseAll = double.TryParse(storedPosition[3], out double parsedHei);
45+
bool couldParseX = double.TryParse(storedPosition[0], out double parsedX);
46+
bool couldParseY = double.TryParse(storedPosition[1], out double parsedY);
47+
bool couldParseW = double.TryParse(storedPosition[2], out double parsedWid);
48+
bool couldParseH = double.TryParse(storedPosition[3], out double parsedHei);
49+
50+
bool couldParseAll = couldParseX && couldParseY && couldParseW && couldParseH;
51+
5052
Rect storedSize = new((int)parsedX, (int)parsedY, (int)parsedWid, (int)parsedHei);
5153
DisplayInfo[] allScreens = DisplayInfo.AllDisplayInfos;
52-
WindowCollection allWindows = Application.Current.Windows;
5354

5455
if (parsedHei < 10 || parsedWid < 10)
5556
return;
5657

5758
foreach (DisplayInfo screen in allScreens)
5859
{
5960
Rect screenRect = screen.Bounds;
61+
DpiScale dpi = System.Windows.Media.VisualTreeHelper.GetDpi(passedWindow);
62+
screenRect = screenRect.GetScaledDownByDpi(dpi);
6063
if (screenRect.IntersectsWith(storedSize))
6164
isStoredRectWithinScreen = true;
6265
}
@@ -251,7 +254,7 @@ private static void TryInjectModifierKeyUp(ref List<INPUT> inputs, VirtualKeySho
251254
{
252255
WindowCollection allWindows = Application.Current.Windows;
253256

254-
foreach (var window in allWindows)
257+
foreach (Window window in allWindows)
255258
{
256259
if (window is T matchWindow)
257260
{
@@ -262,7 +265,15 @@ private static void TryInjectModifierKeyUp(ref List<INPUT> inputs, VirtualKeySho
262265

263266
// No Window Found, open a new one
264267
T newWindow = new();
265-
newWindow.Show();
268+
269+
try
270+
{
271+
newWindow.Show();
272+
}
273+
catch (Exception ex)
274+
{
275+
MessageBox.Show("An error occurred while trying to open a new window. Please try again.", ex.Message);
276+
}
266277
return newWindow;
267278
}
268279

0 commit comments

Comments
 (0)