forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectacleUtilities.m
367 lines (259 loc) · 12.3 KB
/
SpectacleUtilities.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#import "SpectacleUtilities.h"
#import "SpectacleWindowPositionManager.h"
#import "SpectacleHotKeyManager.h"
#import "SpectacleConstants.h"
extern Boolean AXIsProcessTrustedWithOptions(CFDictionaryRef options) __attribute__((weak_import));
@interface ZeroKitHotKey : ZKHotKey
@end
#pragma mark -
@implementation SpectacleUtilities
+ (NSString *)applicationVersion {
NSBundle *bundle = NSBundle.mainBundle;
NSString *bundleVersion = [bundle objectForInfoDictionaryKey: @"CFBundleShortVersionString"];
if (!bundleVersion) {
bundleVersion = [bundle objectForInfoDictionaryKey: @"CFBundleVersion"];
}
return bundleVersion;
}
#pragma mark -
+ (void)registerDefaultsForBundle: (NSBundle *)bundle {
NSUserDefaults *defaults = NSUserDefaults.standardUserDefaults;
NSString *path = [bundle pathForResource: SpectacleDefaultPreferencesPropertyListFile ofType: SpectaclePropertyListFileExtension];
NSDictionary *applicationDefaults = [[NSDictionary alloc] initWithContentsOfFile: path];
[defaults registerDefaults: applicationDefaults];
}
#pragma mark -
+ (void)displayAccessibilityAPIAlert {
NSAlert *alert = [NSAlert new];
NSURL *preferencePaneURL = [NSURL fileURLWithPath: [SpectacleUtilities pathForPreferencePaneNamed: SpectacleUniversalAccessPreferencePaneName]];
alert.messageText = LocalizedString(@"Spectacle requires that the Accessibility API be enabled");
alert.informativeText = LocalizedString(@"Would you like to open the Universal Access preferences so that you can turn on \"Enable access for assistive devices\"?");
[alert addButtonWithTitle: LocalizedString(@"Open Universal Access Preferences")];
[alert addButtonWithTitle: LocalizedString(@"Stop Spectacle")];
switch ([alert runModal]) {
case NSAlertFirstButtonReturn:
[NSWorkspace.sharedWorkspace openURL: preferencePaneURL];
break;
case NSAlertSecondButtonReturn:
[NSApplication.sharedApplication terminate: self];
break;
default:
break;
}
}
+ (void)displayRunningInBackgroundAlertWithCallback: (void (^)(BOOL, BOOL))callback {
NSAlert *alert = [NSAlert new];
alert.alertStyle = NSInformationalAlertStyle;
alert.showsSuppressionButton = YES;
alert.messageText = LocalizedString(@"This will cause Spectacle to run in the background");
alert.informativeText = LocalizedString(@"Run Spectacle in the background without a menu in the status bar.\n\nTo access Spectacle's preferences click on Spectacle in Launchpad, or open Spectacle in Finder.");
[alert addButtonWithTitle: LocalizedString(@"OK")];
[alert addButtonWithTitle: LocalizedString(@"Cancel")];
NSInteger response = [alert runModal];
BOOL isAlertSuppressed = [alert.suppressionButton state] == NSOnState;
switch (response) {
case NSAlertFirstButtonReturn:
callback(YES, isAlertSuppressed);
break;
case NSAlertSecondButtonReturn:
callback(NO, isAlertSuppressed);
break;
default:
break;
}
}
+ (void)displayRestoreDefaultsAlertWithCallback: (void (^)(BOOL))callback {
NSAlert *alert = [NSAlert new];
alert.messageText = LocalizedString(@"This will restore Spectacle's default hot keys");
alert.informativeText = LocalizedString(@"Would you like to restore the default hot keys? Any custom hot keys will be lost.");
[alert addButtonWithTitle: LocalizedString(@"OK")];
[alert addButtonWithTitle: LocalizedString(@"Cancel")];
NSInteger response = [alert runModal];
switch (response) {
case NSAlertFirstButtonReturn:
callback(YES);
break;
case NSAlertSecondButtonReturn:
callback(NO);
break;
default:
break;
}
}
#pragma mark -
+ (SpectacleApplicationTrust)spectacleTrust {
BOOL result = SpectacleIsTrusted;
if ((AXIsProcessTrustedWithOptions != NULL) && !AXIsProcessTrustedWithOptions(NULL)) {
result = SpectacleIsNotTrustedOnOrAfterMavericks;
} else if (!AXAPIEnabled()) {
result = SpectacleIsNotTrustedBeforeMavericks;
}
return result;
}
#pragma mark -
+ (NSArray *)hotKeyNames {
NSBundle *bundle = NSBundle.mainBundle;
NSString *path = [bundle pathForResource: SpectacleHotKeyNamesPropertyListFile ofType: SpectaclePropertyListFileExtension];
NSArray *hotKeyNames = [NSArray arrayWithContentsOfFile: path];
return hotKeyNames;
}
#pragma mark -
+ (NSArray *)hotKeysFromDictionary: (NSDictionary *)dictionary action: (ZKHotKeyAction)action {
NSDictionary *defaultHotKeys = [SpectacleUtilities defaultHotKeysWithNames: dictionary.allKeys];
NSMutableArray *hotKeys = [NSMutableArray new];
[NSKeyedUnarchiver setClass: ZeroKitHotKey.class forClassName: @"SpectacleHotKey"];
for (NSData *hotKeyData in dictionary.allValues) {
ZKHotKey *hotKey = [NSKeyedUnarchiver unarchiveObjectWithData: hotKeyData];
if (![hotKey isClearedHotKey]) {
NSString *hotKeyName = hotKey.hotKeyName;
hotKey.hotKeyAction = action;
[SpectacleUtilities updateHotKey: hotKey withPotentiallyNewDefaultHotKey: defaultHotKeys[hotKeyName]];
[hotKeys addObject: hotKey];
}
}
return hotKeys;
}
#pragma mark -
+ (void)restoreDefaultHotKeys {
SpectacleWindowPositionManager *windowPositionManager = SpectacleWindowPositionManager.sharedManager;
SpectacleHotKeyManager *hotKeyManager = SpectacleHotKeyManager.sharedManager;
NSDictionary *defaultHotKeys = [SpectacleUtilities defaultHotKeysWithNames: SpectacleUtilities.hotKeyNames];
for (NSString *hotKeyName in defaultHotKeys) {
ZKHotKey *defaultHotKey = defaultHotKeys[hotKeyName];
defaultHotKey.hotKeyAction = ^(ZKHotKey *hotKey) {
[windowPositionManager moveFrontMostWindowWithAction: [windowPositionManager windowActionForHotKey: hotKey]];
};
[hotKeyManager registerHotKey: defaultHotKey];
}
}
#pragma mark -
+ (void)updateHotKey: (ZKHotKey *)hotKey withPotentiallyNewDefaultHotKey: (ZKHotKey *)defaultHotKey {
NSString *hotKeyName = hotKey.hotKeyName;
NSInteger defaultHotKeyCode;
if (![hotKeyName isEqualToString: SpectacleWindowActionMoveToLowerLeft] && ![hotKeyName isEqualToString: SpectacleWindowActionMoveToLowerRight]) {
return;
}
defaultHotKeyCode = defaultHotKey.hotKeyCode;
if ((hotKey.hotKeyCode == defaultHotKeyCode) && (hotKey.hotKeyModifiers == 768)) {
hotKey.hotKeyCode = defaultHotKeyCode;
hotKey.hotKeyModifiers = defaultHotKey.hotKeyModifiers;
}
}
#pragma mark -
+ (NSDictionary *)defaultHotKeysWithNames: (NSArray *)names {
NSBundle *bundle = NSBundle.mainBundle;
NSString *path = [bundle pathForResource: SpectacleDefaultPreferencesPropertyListFile ofType: SpectaclePropertyListFileExtension];
NSDictionary *applicationDefaults = [NSDictionary dictionaryWithContentsOfFile: path];
NSMutableDictionary *defaultHotKeys = [NSMutableDictionary new];
for (NSString *hotKeyName in names) {
NSData *defaultHotKeyData = applicationDefaults[hotKeyName];
ZKHotKey *defaultHotKey = [NSKeyedUnarchiver unarchiveObjectWithData: defaultHotKeyData];
defaultHotKeys[hotKeyName] = defaultHotKey;
}
return defaultHotKeys;
}
#pragma mark -
+ (NSString *)pathForPreferencePaneNamed: (NSString *)preferencePaneName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSAllDomainsMask, YES);
NSFileManager *fileManager = NSFileManager.defaultManager;
NSString *preferencePanePath = nil;
if (preferencePaneName) {
preferencePaneName = [preferencePaneName stringByAppendingFormat: @".%@", SpectaclePreferencePaneExtension];
for (__strong NSString *path in paths) {
path = [path stringByAppendingPathComponent: preferencePaneName];
if (path && [fileManager fileExistsAtPath: path isDirectory: nil]) {
preferencePanePath = path;
break;
}
}
if (!preferencePanePath) {
NSLog(@"There was a problem obtaining the path for the specified preference pane: %@", preferencePaneName);
}
}
return preferencePanePath;
}
#pragma mark -
+ (BOOL)isLoginItemEnabledForBundle: (NSBundle *)bundle {
LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
NSString *applicationPath = bundle.bundlePath;
BOOL result = NO;
if (sharedFileList) {
NSArray *sharedFileListArray = nil;
UInt32 seedValue;
sharedFileListArray = CFBridgingRelease(LSSharedFileListCopySnapshot(sharedFileList, &seedValue));
for (id sharedFile in sharedFileListArray) {
LSSharedFileListItemRef sharedFileListItem = (__bridge LSSharedFileListItemRef)sharedFile;
CFURLRef applicationPathURL = NULL;
LSSharedFileListItemResolve(sharedFileListItem, 0, (CFURLRef *)&applicationPathURL, NULL);
if (applicationPathURL != NULL) {
NSString *resolvedApplicationPath = [(__bridge NSURL *)applicationPathURL path];
CFRelease(applicationPathURL);
if ([resolvedApplicationPath compare: applicationPath] == NSOrderedSame) {
result = YES;
break;
}
}
}
CFRelease(sharedFileList);
} else {
NSLog(@"Unable to create the shared file list.");
}
return result;
}
#pragma mark -
+ (void)enableLoginItemForBundle: (NSBundle *)bundle {
LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
NSString *applicationPath = bundle.bundlePath;
NSURL *applicationPathURL = [NSURL fileURLWithPath: applicationPath];
if (sharedFileList) {
LSSharedFileListItemRef sharedFileListItem = LSSharedFileListInsertItemURL(sharedFileList, kLSSharedFileListItemLast, NULL, NULL, (__bridge CFURLRef)applicationPathURL, NULL, NULL);
if (sharedFileListItem) {
CFRelease(sharedFileListItem);
}
CFRelease(sharedFileList);
} else {
NSLog(@"Unable to create the shared file list.");
}
}
+ (void)disableLoginItemForBundle: (NSBundle *)bundle {
LSSharedFileListRef sharedFileList = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
NSString *applicationPath = bundle.bundlePath;
if (sharedFileList) {
NSArray *sharedFileListArray = nil;
UInt32 seedValue;
sharedFileListArray = CFBridgingRelease(LSSharedFileListCopySnapshot(sharedFileList, &seedValue));
for (id sharedFile in sharedFileListArray) {
LSSharedFileListItemRef sharedFileListItem = (__bridge LSSharedFileListItemRef)sharedFile;
CFURLRef applicationPathURL;
LSSharedFileListItemResolve(sharedFileListItem, 0, &applicationPathURL, NULL);
if (applicationPathURL != NULL) {
NSString *resolvedApplicationPath = [(__bridge NSURL *)applicationPathURL path];
if ([resolvedApplicationPath compare: applicationPath] == NSOrderedSame) {
LSSharedFileListItemRemove(sharedFileList, sharedFileListItem);
}
CFRelease(applicationPathURL);
}
}
CFRelease(sharedFileList);
} else {
NSLog(@"Unable to create the shared file list.");
}
}
#pragma mark -
+ (NSMutableDictionary *)stringAttributesWithShadow {
NSMutableParagraphStyle *paragraphStyle = NSParagraphStyle.defaultParagraphStyle.mutableCopy;
NSShadow *textShadow = [NSShadow new];
NSMutableDictionary *stringAttributes = [NSMutableDictionary new];
paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail;
paragraphStyle.alignment = NSCenterTextAlignment;
textShadow.shadowColor = [NSColor whiteColor];
textShadow.shadowOffset = NSMakeSize(0.0f, -1.0);
textShadow.shadowBlurRadius = 0.0f;
stringAttributes[NSParagraphStyleAttributeName] = paragraphStyle;
stringAttributes[NSShadowAttributeName] = textShadow;
return stringAttributes;
}
@end
#pragma mark -
@implementation ZeroKitHotKey
@end