Skip to content

Commit

Permalink
Scale down screens to make sure the window is actually intersecting them
Browse files Browse the repository at this point in the history
  • Loading branch information
TheJoeFin committed Sep 5, 2024
1 parent f993efa commit a4926ed
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions Text-Grab/Utilities/WindowUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,24 @@ public static void SetWindowPosition(Window passedWindow)
if (storedPosition != null
&& storedPosition.Count == 4)
{
bool couldParseAll = false;
couldParseAll = double.TryParse(storedPosition[0], out double parsedX);
couldParseAll = double.TryParse(storedPosition[1], out double parsedY);
couldParseAll = double.TryParse(storedPosition[2], out double parsedWid);
couldParseAll = double.TryParse(storedPosition[3], out double parsedHei);
bool couldParseX = double.TryParse(storedPosition[0], out double parsedX);
bool couldParseY = double.TryParse(storedPosition[1], out double parsedY);
bool couldParseW = double.TryParse(storedPosition[2], out double parsedWid);
bool couldParseH = double.TryParse(storedPosition[3], out double parsedHei);

bool couldParseAll = couldParseX && couldParseY && couldParseW && couldParseH;

Rect storedSize = new((int)parsedX, (int)parsedY, (int)parsedWid, (int)parsedHei);
DisplayInfo[] allScreens = DisplayInfo.AllDisplayInfos;
WindowCollection allWindows = Application.Current.Windows;

if (parsedHei < 10 || parsedWid < 10)
return;

foreach (DisplayInfo screen in allScreens)
{
Rect screenRect = screen.Bounds;
DpiScale dpi = System.Windows.Media.VisualTreeHelper.GetDpi(passedWindow);
screenRect = screenRect.GetScaledDownByDpi(dpi);
if (screenRect.IntersectsWith(storedSize))
isStoredRectWithinScreen = true;
}
Expand Down Expand Up @@ -251,7 +254,7 @@ private static void TryInjectModifierKeyUp(ref List<INPUT> inputs, VirtualKeySho
{
WindowCollection allWindows = Application.Current.Windows;

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

// No Window Found, open a new one
T newWindow = new();
newWindow.Show();

try
{
newWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while trying to open a new window. Please try again.", ex.Message);
}
return newWindow;
}

Expand Down

0 comments on commit a4926ed

Please sign in to comment.