This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
/
SpectacleScreenDetection.m
145 lines (103 loc) · 4.82 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#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 SpectacleScreenDetection.screensInConsistentOrder) {
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 = SpectacleScreenDetection.screensInConsistentOrder;
NSScreen *result = nil;
if (screens.count <= 1) {
return result;
}
NSLog(@"Discovered %lu screen(s).", (unsigned long)screens.count);
NSLog(@"Current screen: %@", RectToString(frameOfScreen));
for (NSInteger i = 0; i < screens.count; i++) {
NSScreen *currentScreen = screens[i];
NSLog(@" Screen %ld: %@", (long)i, RectToString(NSRectToCGRect(currentScreen.frame)));
}
if (action == SpectacleWindowActionNextDisplay) {
NSLog(@"Selecting the screen of the NEXT display.");
} else if (action == SpectacleWindowActionPreviousDisplay) {
NSLog(@"Selecting the screen of the PREVIOUS display.");
}
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;
}
NSLog(@"Index of the current screen: %ld", (long)i);
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;
}
NSLog(@"Selected screen: %@", RectToString(NSRectToCGRect(result.frame)));
return result;
}
# pragma mark -
+ (NSArray *)screensInConsistentOrder {
NSArray *screens = NSScreen.screens;
NSArray *result = [[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);
}];
return result;
}
@end