forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectacleScreenDetection.m
119 lines (80 loc) · 3.54 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#import "SpectacleScreenDetection.h"
#import "SpectacleUtilities.h"
@interface SpectacleScreenDetection (SpectacleScreenDetectionPrivate)
+ (NSScreen *)screenContainingRect: (CGRect)rect;
#pragma mark -
+ (CGFloat)percentageOfRect: (CGRect)rect withinFrameOfScreen: (CGRect)frameOfScreen;
#pragma mark -
+ (NSScreen *)nextOrPreviousScreenToFrameOfScreen: (CGRect)frameOfScreen inDirectionOfAction: (SpectacleWindowAction)action;
@end
#pragma mark -
@implementation SpectacleScreenDetection
+ (NSScreen *)screenWithAction: (SpectacleWindowAction)action andRect: (CGRect)rect {
NSScreen *result = [self screenContainingRect: rect];
if (MovingToNextOrPreviousDisplay(action)) {
result = [self nextOrPreviousScreenToFrameOfScreen: NSRectToCGRect([result frame]) inDirectionOfAction: action];
}
return result;
}
@end
#pragma mark -
@implementation SpectacleScreenDetection (SpectacleScreenDetectionPrivate)
+ (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;
}
#pragma mark -
+ (NSScreen *)nextOrPreviousScreenToFrameOfScreen: (CGRect)frameOfScreen inDirectionOfAction: (SpectacleWindowAction)action {
NSArray *screens = [NSScreen screens];
NSScreen *result = nil;
if ([screens count] <= 1) {
return result;
}
for (NSInteger i = 0; i < [screens count]; i++) {
NSScreen *currentScreen = screens[i];
CGRect currentFrameOfScreen = NSRectToCGRect([currentScreen frame]);
NSInteger nextOrPreviousIndex = i;
if (!CGRectEqualToRect(currentFrameOfScreen, frameOfScreen)) {
continue;
}
if (action == SpectacleWindowActionNextDisplay) {
nextOrPreviousIndex++;
} else if (action == SpectacleWindowActionPreviousDisplay) {
nextOrPreviousIndex--;
}
if (nextOrPreviousIndex < 0) {
nextOrPreviousIndex = [screens count] - 1;
} else if (nextOrPreviousIndex >= [screens count]) {
nextOrPreviousIndex = 0;
}
result = screens[nextOrPreviousIndex];
break;
}
return result;
}
@end