forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectacleScreenDetection.m
117 lines (85 loc) · 3.98 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
#import "SpectacleScreenDetection.h"
#import "SpectacleUtilities.h"
@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;
}
#pragma mark -
+ (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;
}
NSArray *screensInConsistentOrder = [[screens sortedArrayWithOptions: NSSortStable usingComparator: ^(NSScreen *screenOne, NSScreen *screenTwo) {
if (CGPointEqualToPoint(screenOne.frame.origin, CGPointMake(0, 0))) {
return NSOrderedAscending;
} else if (CGPointEqualToPoint(screenTwo.frame.origin, CGPointMake(0, 0))) {
return NSOrderedDescending;
}
return (NSComparisonResult)(screenTwo.frame.origin.y - screenOne.frame.origin.y);
}] sortedArrayWithOptions: NSSortStable usingComparator: ^(NSScreen *screenOne, NSScreen *screenTwo) {
if (CGPointEqualToPoint(screenOne.frame.origin, CGPointMake(0, 0))) {
return NSOrderedAscending;
} else if (CGPointEqualToPoint(screenTwo.frame.origin, CGPointMake(0, 0))) {
return NSOrderedDescending;
}
return (NSComparisonResult)(screenTwo.frame.origin.x - screenOne.frame.origin.x);
}];
for (NSInteger i = 0; i < screensInConsistentOrder.count; i++) {
NSScreen *currentScreen = screensInConsistentOrder[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