Skip to content

Commit 067f3ec

Browse files
authored
Merge pull request #436 from TheJoeFin/testing-centers
Testing centers
2 parents 6321022 + 584272c commit 067f3ec

File tree

3 files changed

+173
-37
lines changed

3 files changed

+173
-37
lines changed

Tests/ScreenLayoutTests.cs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
using System.Windows;
2+
using Text_Grab;
3+
4+
namespace Tests;
5+
public class ScreenLayoutTests
6+
{
7+
/*
8+
DISPLAY1 X=0,Y=0,Width=3440,Height=1400
9+
DISPLAY2 X=3440,Y=-1163,Width=2400,Height=3760
10+
DISPLAY3 X=-1920,Y=387,Width=1920,Height=1030
11+
*/
12+
private static Rect display1 = new(0, 0, 3440, 1400);
13+
private static Rect display2 = new(3440, -1163, 2400, 3760);
14+
private static Rect display3 = new(-1920, 387, 1920, 1030);
15+
16+
/*
17+
Param Display 1 Display 6 Display 5
18+
X 0 1920 3840
19+
Y 0 -460 -468
20+
Width 1920 1920 3840
21+
Height 1152 1032 2040
22+
*/
23+
private static Rect display4 = new(0, 0, 1920, 1152);
24+
private static Rect display5 = new(3840, -468, 3840, 2040);
25+
private static Rect display6 = new(1920, -460, 1920, 1032);
26+
27+
28+
[Fact]
29+
public void ShouldFindCenterOfEachRect()
30+
{
31+
Point center1 = display1.CenterPoint();
32+
Point center2 = display2.CenterPoint();
33+
Point center3 = display3.CenterPoint();
34+
Point center4 = display4.CenterPoint();
35+
Point center5 = display5.CenterPoint();
36+
Point center6 = display6.CenterPoint();
37+
38+
Assert.True(display1.Contains(center1));
39+
Assert.True(display2.Contains(center2));
40+
Assert.True(display3.Contains(center3));
41+
Assert.True(display4.Contains(center4));
42+
Assert.True(display5.Contains(center5));
43+
Assert.True(display6.Contains(center6));
44+
45+
Assert.False(display1.Contains(center3));
46+
Assert.False(display1.Contains(center2));
47+
48+
Assert.False(display2.Contains(center1));
49+
Assert.False(display2.Contains(center3));
50+
51+
Assert.False(display3.Contains(center1));
52+
Assert.False(display3.Contains(center2));
53+
54+
55+
Assert.False(display4.Contains(center5));
56+
Assert.False(display4.Contains(center6));
57+
58+
Assert.False(display5.Contains(center4));
59+
Assert.False(display5.Contains(center6));
60+
61+
Assert.False(display6.Contains(center4));
62+
Assert.False(display6.Contains(center5));
63+
}
64+
65+
[Fact]
66+
public void SmallRectanglesContained()
67+
{
68+
/*
69+
FullscreenGrab fullScreenGrab = allFullscreenGrab[count];
70+
fullScreenGrab.WindowStartupLocation = WindowStartupLocation.Manual;
71+
fullScreenGrab.Width = 40;
72+
fullScreenGrab.Height = 40;
73+
fullScreenGrab.DestinationTextBox = destinationTextBox;
74+
fullScreenGrab.WindowState = WindowState.Normal;
75+
76+
Point screenCenterPoint = screen.GetCenterPoint();
77+
Point windowCenterPoint = fullScreenGrab.GetWindowCenter();
78+
79+
fullScreenGrab.Left = screenCenterPoint.X - windowCenterPoint.X;
80+
fullScreenGrab.Top = screenCenterPoint.Y - windowCenterPoint.Y;
81+
*/
82+
83+
int sideLength = 40;
84+
85+
double smallLeft1 = display1.CenterPoint().X - (sideLength / 2);
86+
double smallTop1 = display1.CenterPoint().Y - (sideLength / 2);
87+
Rect smallRect1 = new(smallLeft1, smallTop1, sideLength, sideLength);
88+
Assert.True(display1.Contains(smallRect1));
89+
Assert.False(display2.Contains(smallRect1));
90+
Assert.False(display3.Contains(smallRect1));
91+
92+
double smallLeft2 = display2.CenterPoint().X - (sideLength / 2);
93+
double smallTop2 = display2.CenterPoint().Y - (sideLength / 2);
94+
Rect smallRect2 = new(smallLeft2, smallTop2, sideLength, sideLength);
95+
96+
Assert.True(display2.Contains(smallRect2));
97+
Assert.False(display1.Contains(smallRect2));
98+
Assert.False(display3.Contains(smallRect2));
99+
100+
double smallLeft3 = display3.CenterPoint().X - (sideLength / 2);
101+
double smallTop3 = display3.CenterPoint().Y - (sideLength / 2);
102+
Rect smallRect3 = new(smallLeft3, smallTop3, sideLength, sideLength);
103+
104+
Assert.True(display3.Contains(smallRect3));
105+
Assert.False(display1.Contains(smallRect3));
106+
Assert.False(display2.Contains(smallRect3));
107+
}
108+
109+
[Fact]
110+
public void SmallRectanglesContained456()
111+
{
112+
int sideLength = 40;
113+
114+
double smallLeft4 = display4.CenterPoint().X - (sideLength / 2);
115+
double smallTop4 = display4.CenterPoint().Y - (sideLength / 2);
116+
Rect smallRect4 = new(smallLeft4, smallTop4, sideLength, sideLength);
117+
Assert.True(display4.Contains(smallRect4));
118+
Assert.False(display5.Contains(smallRect4));
119+
Assert.False(display6.Contains(smallRect4));
120+
121+
double smallLeft5 = display5.CenterPoint().X - (sideLength / 2);
122+
double smallTop5 = display5.CenterPoint().Y - (sideLength / 2);
123+
Rect smallRect5 = new(smallLeft5, smallTop5, sideLength, sideLength);
124+
125+
Assert.True(display5.Contains(smallRect5));
126+
Assert.False(display4.Contains(smallRect5));
127+
Assert.False(display6.Contains(smallRect5));
128+
129+
double smallLeft6 = display6.CenterPoint().X - (sideLength / 2);
130+
double smallTop6 = display6.CenterPoint().Y - (sideLength / 2);
131+
Rect smallRect6 = new(smallLeft6, smallTop6, sideLength, sideLength);
132+
133+
Assert.True(display6.Contains(smallRect6));
134+
Assert.False(display4.Contains(smallRect6));
135+
Assert.False(display5.Contains(smallRect6));
136+
}
137+
}

Text-Grab/Extensions/ShapeExtensions.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,11 @@ public static bool IsGood(this Rect rect)
7474

7575
return true;
7676
}
77+
78+
public static System.Windows.Point CenterPoint(this Rect rect)
79+
{
80+
double x = rect.Left + (rect.Width / 2);
81+
double y = rect.Top + (rect.Height / 2);
82+
return new(x, y);
83+
}
7784
}

Text-Grab/Utilities/WindowUtilities.cs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
4-
using System.Drawing;
53
using System.Linq;
6-
using System.Security.Permissions;
7-
using System.Text.RegularExpressions;
84
using System.Threading.Tasks;
95
using System.Windows;
106
using System.Windows.Controls;
@@ -30,26 +26,26 @@ public static void AddTextToOpenWindow(string textToAdd)
3026

3127
public static void SetWindowPosition(Window passedWindow)
3228
{
33-
string storedPostionString = "";
29+
string storedPositionString = "";
3430

3531
if (passedWindow is EditTextWindow)
36-
storedPostionString = Properties.Settings.Default.EditTextWindowSizeAndPosition;
32+
storedPositionString = Settings.Default.EditTextWindowSizeAndPosition;
3733

3834
if (passedWindow is GrabFrame)
39-
storedPostionString = Properties.Settings.Default.GrabFrameWindowSizeAndPosition;
35+
storedPositionString = Settings.Default.GrabFrameWindowSizeAndPosition;
4036

41-
List<string> storedPostion = new(storedPostionString.Split(','));
37+
List<string> storedPosition = new(storedPositionString.Split(','));
4238

4339
bool isStoredRectWithinScreen = false;
4440

45-
if (storedPostion != null
46-
&& storedPostion.Count == 4)
41+
if (storedPosition != null
42+
&& storedPosition.Count == 4)
4743
{
4844
bool couldParseAll = false;
49-
couldParseAll = double.TryParse(storedPostion[0], out double parsedX);
50-
couldParseAll = double.TryParse(storedPostion[1], out double parsedY);
51-
couldParseAll = double.TryParse(storedPostion[2], out double parsedWid);
52-
couldParseAll = double.TryParse(storedPostion[3], out double parsedHei);
45+
couldParseAll = double.TryParse(storedPosition[0], out double parsedX);
46+
couldParseAll = double.TryParse(storedPosition[1], out double parsedY);
47+
couldParseAll = double.TryParse(storedPosition[2], out double parsedWid);
48+
couldParseAll = double.TryParse(storedPosition[3], out double parsedHei);
5349
Rect storedSize = new((int)parsedX, (int)parsedY, (int)parsedWid, (int)parsedHei);
5450
IEnumerable<Screen> allScreens = Screen.AllScreens;
5551
WindowCollection allWindows = Application.Current.Windows;
@@ -83,8 +79,8 @@ public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)
8379
int numberOfScreens = allScreens.Count();
8480

8581
foreach (Window window in allWindows)
86-
if (window is FullscreenGrab)
87-
allFullscreenGrab.Add((FullscreenGrab)window);
82+
if (window is FullscreenGrab grab)
83+
allFullscreenGrab.Add(grab);
8884

8985
int numberOfFullscreenGrabWindowsToCreate = numberOfScreens - allFullscreenGrab.Count;
9086

@@ -95,34 +91,30 @@ public static void LaunchFullScreenGrab(TextBox? destinationTextBox = null)
9591

9692
int count = 0;
9793

94+
double sideLength = 40;
95+
9896
foreach (Screen screen in allScreens)
9997
{
100-
FullscreenGrab fullscreenGrab = allFullscreenGrab[count];
101-
fullscreenGrab.WindowStartupLocation = WindowStartupLocation.Manual;
102-
fullscreenGrab.Width = 400;
103-
fullscreenGrab.Height = 200;
104-
fullscreenGrab.DestinationTextBox = destinationTextBox;
105-
fullscreenGrab.WindowState = WindowState.Normal;
106-
107-
System.Windows.Point screenCenterPoint = screen.GetCenterPoint();
108-
System.Windows.Point windowCenterPoint = fullscreenGrab.GetWindowCenter();
98+
FullscreenGrab fullScreenGrab = allFullscreenGrab[count];
99+
fullScreenGrab.WindowStartupLocation = WindowStartupLocation.Manual;
100+
fullScreenGrab.Width = sideLength;
101+
fullScreenGrab.Height = sideLength;
102+
fullScreenGrab.DestinationTextBox = destinationTextBox;
103+
fullScreenGrab.WindowState = WindowState.Normal;
109104

110-
double virtualScreenTop = SystemParameters.VirtualScreenTop;
111-
double virtualScreenLeft = SystemParameters.VirtualScreenLeft;
112-
double virtualScreenWidth = SystemParameters.VirtualScreenWidth;
113-
double virtualScreenHeight = SystemParameters.VirtualScreenHeight;
105+
Point screenCenterPoint = screen.GetCenterPoint();
114106

115-
fullscreenGrab.Left = screenCenterPoint.X - windowCenterPoint.X;
116-
fullscreenGrab.Top = screenCenterPoint.Y - windowCenterPoint.Y;
107+
fullScreenGrab.Left = screenCenterPoint.X - (sideLength / 2);
108+
fullScreenGrab.Top = screenCenterPoint.Y - (sideLength / 2);
117109

118-
fullscreenGrab.Show();
119-
fullscreenGrab.Activate();
110+
fullScreenGrab.Show();
111+
fullScreenGrab.Activate();
120112

121113
count++;
122114
}
123115
}
124116

125-
public static System.Windows.Point GetCenterPoint(this Screen screen)
117+
public static Point GetCenterPoint(this Screen screen)
126118
{
127119
double x = screen.WpfBounds.Left + (screen.WpfBounds.Width / 2);
128120
double y = screen.WpfBounds.Top + (screen.WpfBounds.Height / 2);
@@ -164,7 +156,7 @@ internal static async void CloseAllFullscreenGrabs()
164156

165157
if (fsg.DestinationTextBox is not null)
166158
{
167-
// TODO 3.0 Find out how to re normaize an ETW when FSG had it minimzed
159+
// TODO 3.0 Find out how to re normalize an ETW when FSG had it minimized
168160
isFromEditWindow = true;
169161
// if (fsg.EditWindow.WindowState == WindowState.Minimized)
170162
// fsg.EditWindow.WindowState = WindowState.Normal;
@@ -201,7 +193,7 @@ internal static async Task TryInsertString(string stringToInsert)
201193
{
202194
await Task.Delay(TimeSpan.FromSeconds(Settings.Default.InsertDelay));
203195

204-
List<INPUT> inputs = new List<INPUT>();
196+
List<INPUT> inputs = new();
205197
// make sure keys are up.
206198
TryInjectModifierKeyUp(ref inputs, VirtualKeyShort.LCONTROL);
207199
TryInjectModifierKeyUp(ref inputs, VirtualKeyShort.RCONTROL);
@@ -264,7 +256,7 @@ private static void TryInjectModifierKeyUp(ref List<INPUT> inputs, VirtualKeySho
264256
}
265257

266258
// No Window Found, open a new one
267-
T newWindow = new T();
259+
T newWindow = new();
268260
newWindow.Show();
269261
return newWindow;
270262
}

0 commit comments

Comments
 (0)