This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 833
/
SpectacleScreenDetection.m
102 lines (69 loc) · 3.43 KB
/
SpectacleScreenDetection.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#import "SpectacleScreenDetection.h"
#import "SpectacleUtilities.h"
@interface SpectacleScreenDetection (SpectacleScreenDetectionPrivate)
+ (NSScreen *)screenAdjacentToFrameOfScreen: (CGRect)frameOfScreen inDirectionOfAction: (SpectacleWindowAction)action;
+ (NSScreen *)screenContainingRect: (CGRect)rect;
#pragma mark -
+ (CGFloat)percentageOfRect: (CGRect)rect withinFrameOfScreen: (CGRect)frameOfScreen;
@end
#pragma mark -
@implementation SpectacleScreenDetection
+ (NSScreen *)screenWithAction: (SpectacleWindowAction)action andRect: (CGRect)rect {
NSScreen *result = [self screenContainingRect: rect];
if (MovingToDisplay(action)) {
result = [self screenAdjacentToFrameOfScreen: NSRectToCGRect([result frame]) inDirectionOfAction: action];
}
return result;
}
@end
#pragma mark -
@implementation SpectacleScreenDetection (SpectacleScreenDetectionPrivate)
+ (NSScreen *)screenAdjacentToFrameOfScreen: (CGRect)frameOfScreen inDirectionOfAction: (SpectacleWindowAction)action {
NSScreen *result = nil;
for (NSScreen *currentScreen in [NSScreen screens]) {
CGRect currentFrameOfScreen = NSRectToCGRect([currentScreen frame]);
if (CGRectEqualToRect(currentFrameOfScreen, frameOfScreen)) {
continue;
}
if ((action == SpectacleWindowActionLeftDisplay) && RectIsLeftOfRect(currentFrameOfScreen, frameOfScreen)) {
result = currentScreen;
} else if ((action == SpectacleWindowActionRightDisplay) && RectIsRightOfRect(currentFrameOfScreen, frameOfScreen)) {
result = currentScreen;
} else if ((action == SpectacleWindowActionTopDisplay) && RectIsAboveRect(currentFrameOfScreen, frameOfScreen)) {
result = currentScreen;
} else if ((action == SpectacleWindowActionBottomDisplay) && RectIsBelowRect(currentFrameOfScreen, frameOfScreen)) {
result = currentScreen;
}
}
return result;
}
+ (NSScreen *)screenContainingRect: (CGRect)rect {
CGFloat largestPercentageOfRectWithinFrameOfScreen = 0.0f;
NSScreen *result = [NSScreen mainScreen];
for (NSScreen *currentScreen in [NSScreen screens]) {
CGRect currentFrameOfScreen = NSRectToCGRect([currentScreen frame]);
CGRect flippedRect = rect;
CGFloat percentageOfRectWithinCurrentFrameOfScreen = 0.0f;
flippedRect.origin.y = FlipVerticalOriginOfRectInRect(flippedRect, currentFrameOfScreen);
if (CGRectContainsRect(currentFrameOfScreen, flippedRect)) {
result = currentScreen;
break;
}
percentageOfRectWithinCurrentFrameOfScreen = [self percentageOfRect: flippedRect withinFrameOfScreen: currentFrameOfScreen];
if (percentageOfRectWithinCurrentFrameOfScreen > largestPercentageOfRectWithinFrameOfScreen) {
largestPercentageOfRectWithinFrameOfScreen = percentageOfRectWithinCurrentFrameOfScreen;
result = currentScreen;
}
}
return result;
}
#pragma mark -
+ (CGFloat)percentageOfRect: (CGRect)rect withinFrameOfScreen: (CGRect)frameOfScreen {
CGRect intersectionOfRectAndFrameOfScreen = CGRectIntersection(rect, frameOfScreen);
CGFloat result = 0.0f;
if (!CGRectIsNull(intersectionOfRectAndFrameOfScreen)) {
result = AreaOfRect(intersectionOfRectAndFrameOfScreen) / AreaOfRect(rect);
}
return result;
}
@end