forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectacleWindowPositionManager.m
347 lines (246 loc) · 14.7 KB
/
SpectacleWindowPositionManager.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#import "SpectacleWindowPositionManager.h"
#import "SpectacleWindowPositionCalculator.h"
#import "SpectacleScreenDetection.h"
#import "SpectacleHistory.h"
#import "SpectacleHistoryItem.h"
#import "SpectacleUtilities.h"
#import "SpectacleConstants.h"
#import "ZKAccessibilityElementAdditions.h"
#define Resizing(action) ((action == SpectacleWindowActionLarger) || (action == SpectacleWindowActionSmaller))
#pragma mark -
#define UndoOrRedo(action) ((action == SpectacleWindowActionUndo) || (action == SpectacleWindowActionRedo))
#pragma mark -
#define BlacklistedWindowRect(applicationName, windowRect) [NSString stringWithFormat: @"%@ - %@", applicationName, WindowRectToString(windowRect)]
#pragma mark -
@interface SpectacleWindowPositionManager (SpectacleWindowPositionManagerPrivate)
- (CGRect)rectOfWindowWithAccessibilityElement: (ZKAccessibilityElement *)accessibilityElement;
#pragma mark -
- (void)moveWindowRect: (CGRect)windowRect frameOfScreen: (CGRect)frameOfScreen visibleFrameOfScreen: (CGRect)visibleFrameOfScreen frontMostWindowElement: (ZKAccessibilityElement *)frontMostWindowElement action: (SpectacleWindowAction)action;
- (void)moveWindowRect: (CGRect)windowRect frontMostWindowElement: (ZKAccessibilityElement *)frontMostWindowElement;
@end
#pragma mark -
@interface SpectacleWindowPositionManager (WindowHistory)
- (SpectacleHistory *)historyForCurrentApplication;
#pragma mark -
- (void)undoOrRedoHistoryWithAction: (SpectacleWindowAction)action;
- (BOOL)moveWithHistoryItem: (SpectacleHistoryItem *)historyItem visibleFrameOfScreen: (CGRect)visibleFrameOfScreen action: (SpectacleWindowAction)action;
@end
#pragma mark -
@implementation SpectacleWindowPositionManager
static SpectacleWindowPositionManager *sharedInstance = nil;
- (id)init {
if ((self = [super init])) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *path = [[SpectacleUtilities applicationBundle] pathForResource: SpectacleBlacklistedApplicationsPropertyListFile
ofType: ZKPropertyListFileExtension];
applicationHistories = [NSMutableDictionary new];
blacklistedWindowRects = [NSMutableSet setWithArray: [userDefaults arrayForKey: SpectacleBlacklistedWindowRectsPreference]];
blacklistedApplications = [NSMutableSet setWithArray: [NSArray arrayWithContentsOfFile: path]];
}
return self;
}
#pragma mark -
+ (SpectacleWindowPositionManager *)sharedManager {
@synchronized(self) {
if (!sharedInstance) {
sharedInstance = [self new];
}
}
return sharedInstance;
}
#pragma mark -
- (void)moveFrontMostWindowWithAction: (SpectacleWindowAction)action {
ZKAccessibilityElement *frontMostWindowElement = [ZKAccessibilityElement frontMostWindowElement];
CGRect frontMostWindowRect = [self rectOfWindowWithAccessibilityElement: frontMostWindowElement];
CGRect previousFrontMostWindowRect = CGRectNull;
NSScreen *screenOfDisplay = [SpectacleScreenDetection screenWithAction: action andRect: frontMostWindowRect];
CGRect frameOfScreen = CGRectNull;
CGRect visibleFrameOfScreen = CGRectNull;
SpectacleHistory *history = [self historyForCurrentApplication];
SpectacleHistoryItem *historyItem = nil;
if (screenOfDisplay) {
frameOfScreen = NSRectToCGRect([screenOfDisplay frame]);
visibleFrameOfScreen = NSRectToCGRect([screenOfDisplay visibleFrame]);
}
if (CGRectIsNull(frontMostWindowRect) || CGRectIsNull(frameOfScreen) || CGRectIsNull(visibleFrameOfScreen) || CGRectEqualToRect(frontMostWindowRect, frameOfScreen)) {
NSBeep();
return;
}
if (UndoOrRedo(action)) {
[self undoOrRedoHistoryWithAction: action];
return;
}
if ([history isEmpty]) {
historyItem = [SpectacleHistoryItem historyItemFromAccessibilityElement: frontMostWindowElement
windowRect: frontMostWindowRect];
[history addHistoryItem: historyItem];
}
frontMostWindowRect.origin.y = FlipVerticalOriginOfRectInRect(frontMostWindowRect, frameOfScreen);
if (MovingToNextOrPreviousDisplay(action) && RectFitsInRect(frontMostWindowRect, visibleFrameOfScreen)) {
action = SpectacleWindowActionCenter;
}
previousFrontMostWindowRect = frontMostWindowRect;
if (Resizing(action)) {
CGFloat sizeOffset = ((action == SpectacleWindowActionLarger) ? 1.0 : -1.0) * SpectacleWindowSizeOffset;
frontMostWindowRect = [SpectacleWindowPositionCalculator calculateResizedWindowRect: frontMostWindowRect visibleFrameOfScreen: visibleFrameOfScreen sizeOffset: sizeOffset];
} else {
frontMostWindowRect = [SpectacleWindowPositionCalculator calculateWindowRect: frontMostWindowRect visibleFrameOfScreen: visibleFrameOfScreen action: action];
}
if (CGRectEqualToRect(previousFrontMostWindowRect, frontMostWindowRect)) {
NSBeep();
return;
}
frontMostWindowRect.origin.y = FlipVerticalOriginOfRectInRect(frontMostWindowRect, frameOfScreen);
historyItem = [SpectacleHistoryItem historyItemFromAccessibilityElement: frontMostWindowElement
windowRect: frontMostWindowRect];
[history addHistoryItem: historyItem];
[self moveWindowRect: frontMostWindowRect frameOfScreen: frameOfScreen visibleFrameOfScreen: visibleFrameOfScreen frontMostWindowElement: frontMostWindowElement action: action];
}
#pragma mark -
- (void)undoLastWindowAction {
[self moveFrontMostWindowWithAction: SpectacleWindowActionUndo];
}
- (void)redoLastWindowAction {
[self moveFrontMostWindowWithAction: SpectacleWindowActionRedo];
}
#pragma mark -
- (SpectacleWindowAction)windowActionForHotKey: (ZKHotKey *)hotKey {
NSString *name = [hotKey hotKeyName];
SpectacleWindowAction windowAction = SpectacleWindowActionNone;
if ([name isEqualToString: SpectacleWindowActionMoveToCenter]) {
windowAction = SpectacleWindowActionCenter;
} else if ([name isEqualToString: SpectacleWindowActionMoveToFullscreen]) {
windowAction = SpectacleWindowActionFullscreen;
} else if ([name isEqualToString: SpectacleWindowActionMoveToLeftHalf]) {
windowAction = SpectacleWindowActionLeftHalf;
} else if ([name isEqualToString: SpectacleWindowActionMoveToRightHalf]) {
windowAction = SpectacleWindowActionRightHalf;
} else if ([name isEqualToString: SpectacleWindowActionMoveToTopHalf]) {
windowAction = SpectacleWindowActionTopHalf;
} else if ([name isEqualToString: SpectacleWindowActionMoveToBottomHalf]) {
windowAction = SpectacleWindowActionBottomHalf;
} else if ([name isEqualToString: SpectacleWindowActionMoveToUpperLeft]) {
windowAction = SpectacleWindowActionUpperLeft;
} else if ([name isEqualToString: SpectacleWindowActionMoveToLowerLeft]) {
windowAction = SpectacleWindowActionLowerLeft;
} else if ([name isEqualToString: SpectacleWindowActionMoveToUpperRight]) {
windowAction = SpectacleWindowActionUpperRight;
} else if ([name isEqualToString: SpectacleWindowActionMoveToLowerRight]) {
windowAction = SpectacleWindowActionLowerRight;
} else if ([name isEqualToString: SpectacleWindowActionMoveToNextDisplay]) {
windowAction = SpectacleWindowActionNextDisplay;
} else if ([name isEqualToString: SpectacleWindowActionMoveToPreviousDisplay]) {
windowAction = SpectacleWindowActionPreviousDisplay;
} else if ([name isEqualToString: SpectacleWindowActionMoveToNextThird]) {
windowAction = SpectacleWindowActionNextThird;
} else if ([name isEqualToString: SpectacleWindowActionMoveToPreviousThird]) {
windowAction = SpectacleWindowActionPreviousThird;
} else if ([name isEqualToString: SpectacleWindowActionMakeLarger]) {
windowAction = SpectacleWindowActionLarger;
} else if ([name isEqualToString: SpectacleWindowActionMakeSmaller]) {
windowAction = SpectacleWindowActionSmaller;
} else if ([name isEqualToString: SpectacleWindowActionUndoLastMove]) {
windowAction = SpectacleWindowActionUndo;
} else if ([name isEqualToString: SpectacleWindowActionRedoLastMove]) {
windowAction = SpectacleWindowActionRedo;
}
return windowAction;
}
@end
#pragma mark -
@implementation SpectacleWindowPositionManager (SpectacleWindowPositionManagerPrivate)
- (CGRect)rectOfWindowWithAccessibilityElement: (ZKAccessibilityElement *)accessibilityElement {
CGRect result = CGRectNull;
if (accessibilityElement) {
CFTypeRef windowPositionValue = [accessibilityElement valueOfAttribute: kAXPositionAttribute type: kAXValueCGPointType];
CFTypeRef windowSizeValue = [accessibilityElement valueOfAttribute: kAXSizeAttribute type: kAXValueCGSizeType];
CGPoint windowPosition;
CGSize windowSize;
AXValueGetValue(windowPositionValue, kAXValueCGPointType, (void *)&windowPosition);
AXValueGetValue(windowSizeValue, kAXValueCGSizeType, (void *)&windowSize);
CFRelease(windowPositionValue);
CFRelease(windowSizeValue);
result = CGRectMake(windowPosition.x, windowPosition.y, windowSize.width, windowSize.height);
}
return result;
}
#pragma mark -
- (void)moveWindowRect: (CGRect)windowRect frameOfScreen: (CGRect)frameOfScreen visibleFrameOfScreen: (CGRect)visibleFrameOfScreen frontMostWindowElement: (ZKAccessibilityElement *)frontMostWindowElement action: (SpectacleWindowAction)action {
NSString *frontMostApplicationName = [ZKAccessibilityElement frontMostApplicationName];
NSString *blacklistedWindowRect = BlacklistedWindowRect(frontMostApplicationName, windowRect);
if ([blacklistedWindowRects containsObject: blacklistedWindowRect] || [blacklistedApplications containsObject: frontMostApplicationName]) {
NSBeep();
return;
}
CGRect previousWindowRect = [self rectOfWindowWithAccessibilityElement: frontMostWindowElement];
[self moveWindowRect: windowRect frontMostWindowElement: frontMostWindowElement];
CGRect movedWindowRect = [self rectOfWindowWithAccessibilityElement: frontMostWindowElement];
if (MovingToThirdOfDisplay(action) && !CGRectEqualToRect(movedWindowRect, windowRect)) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSBeep();
[blacklistedWindowRects addObject: blacklistedWindowRect];
[userDefaults setObject: [blacklistedWindowRects allObjects] forKey: SpectacleBlacklistedWindowRectsPreference];
[self moveWindowRect: previousWindowRect frontMostWindowElement: frontMostWindowElement];
return;
}
movedWindowRect.origin.y = FlipVerticalOriginOfRectInRect(movedWindowRect, frameOfScreen);
if (!CGRectContainsRect(visibleFrameOfScreen, movedWindowRect) && (action != SpectacleWindowActionUndo) && (action != SpectacleWindowActionRedo)) {
if (movedWindowRect.origin.x + movedWindowRect.size.width > visibleFrameOfScreen.origin.x + visibleFrameOfScreen.size.width) {
movedWindowRect.origin.x = (visibleFrameOfScreen.origin.x + visibleFrameOfScreen.size.width) - movedWindowRect.size.width;
} else if (movedWindowRect.origin.x < visibleFrameOfScreen.origin.x) {
movedWindowRect.origin.x = visibleFrameOfScreen.origin.x;
}
if (movedWindowRect.origin.y + movedWindowRect.size.height > visibleFrameOfScreen.origin.y + visibleFrameOfScreen.size.height) {
movedWindowRect.origin.y = (visibleFrameOfScreen.origin.y + visibleFrameOfScreen.size.height) - movedWindowRect.size.height;
} else if (movedWindowRect.origin.y < visibleFrameOfScreen.origin.y) {
movedWindowRect.origin.y = visibleFrameOfScreen.origin.y;
}
movedWindowRect.size = windowRect.size;
movedWindowRect.origin.y = FlipVerticalOriginOfRectInRect(movedWindowRect, frameOfScreen);
[self moveWindowRect: movedWindowRect frontMostWindowElement: frontMostWindowElement];
}
}
- (void)moveWindowRect: (CGRect)windowRect frontMostWindowElement: (ZKAccessibilityElement *)frontMostWindowElement {
AXValueRef windowRectPositionRef = AXValueCreate(kAXValueCGPointType, (const void *)&windowRect.origin);
AXValueRef windowRectSizeRef = AXValueCreate(kAXValueCGSizeType, (const void *)&windowRect.size);
[frontMostWindowElement setValue: windowRectSizeRef forAttribute: kAXSizeAttribute];
[frontMostWindowElement setValue: windowRectPositionRef forAttribute: kAXPositionAttribute];
[frontMostWindowElement setValue: windowRectSizeRef forAttribute: kAXSizeAttribute];
CFRelease(windowRectPositionRef);
CFRelease(windowRectSizeRef);
}
@end
#pragma mark -
@implementation SpectacleWindowPositionManager (WindowHistory)
- (SpectacleHistory *)historyForCurrentApplication {
NSString *applicationName = [ZKAccessibilityElement frontMostApplicationName];
if (!applicationHistories[applicationName]) {
applicationHistories[applicationName] = [SpectacleHistory new];
}
return applicationHistories[applicationName];
}
#pragma mark -
- (void)undoOrRedoHistoryWithAction: (SpectacleWindowAction)action {
SpectacleHistory *history = [self historyForCurrentApplication];
SpectacleHistoryItem *historyItem = (action == SpectacleWindowActionUndo) ? [history previousHistoryItem] : [history nextHistoryItem];
ZKAccessibilityElement *accessibilityElement = [historyItem accessibilityElement];
CGRect windowRect = [self rectOfWindowWithAccessibilityElement: accessibilityElement];
NSScreen *screenOfDisplay = [SpectacleScreenDetection screenWithAction: action andRect: windowRect];
CGRect visibleFrameOfScreen = CGRectNull;
if (screenOfDisplay) {
visibleFrameOfScreen = NSRectToCGRect([screenOfDisplay visibleFrame]);
}
if (![self moveWithHistoryItem: historyItem visibleFrameOfScreen: visibleFrameOfScreen action: action]) {
NSBeep();
}
}
- (BOOL)moveWithHistoryItem: (SpectacleHistoryItem *)historyItem visibleFrameOfScreen: (CGRect)visibleFrameOfScreen action: (SpectacleWindowAction)action {
ZKAccessibilityElement *frontMostWindowElement = [historyItem accessibilityElement];
CGRect windowRect = [historyItem windowRect];
if (!historyItem || !frontMostWindowElement || CGRectIsNull(windowRect)) {
return NO;
}
[self moveWindowRect: windowRect frameOfScreen: CGRectNull visibleFrameOfScreen: visibleFrameOfScreen frontMostWindowElement: frontMostWindowElement action: action];
return YES;
}
@end