forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectacleUtilities.m
155 lines (106 loc) · 5.15 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
#import "SpectacleUtilities.h"
#import "SpectacleWindowPositionManager.h"
#import "SpectacleConstants.h"
@interface ZeroKitHotKey : ZKHotKey
@end
#pragma mark -
@interface SpectacleUtilities (SpectacleUtilitiesPrivate)
+ (void)updateHotKey: (ZKHotKey *)hotKey withPotentiallyNewDefaultHotKey: (ZKHotKey *)defaultHotKey;
#pragma mark -
+ (NSDictionary *)defaultHotKeysWithNames: (NSArray *)names;
@end
#pragma mark -
@implementation SpectacleUtilities
+ (void)displayAccessibilityAPIAlert {
NSAlert *alert = [NSAlert new];
NSURL *preferencePaneURL = [NSURL fileURLWithPath: [SpectacleUtilities pathForPreferencePaneNamed: @"UniversalAccessPref"]];
[alert setAlertStyle: NSWarningAlertStyle];
[alert setMessageText: ZKLocalizedString(@"Spectacle requires that the Accessibility API be enabled")];
[alert setInformativeText: ZKLocalizedString(@"Would you like to open the Universal Access preferences so that you can turn on \"Enable access for assistive devices\"?")];
[alert addButtonWithTitle: ZKLocalizedString(@"Open Universal Access Preferences")];
[alert addButtonWithTitle: ZKLocalizedString(@"Stop Spectacle")];
switch ([alert runModal]) {
case NSAlertFirstButtonReturn:
[[NSWorkspace sharedWorkspace] openURL: preferencePaneURL];
break;
case NSAlertSecondButtonReturn:
default:
break;
}
}
+ (void)displayRunningInBackgroundAlertWithCallback: (void (^)(BOOL, BOOL))callback {
NSAlert *alert = [NSAlert new];
[alert setAlertStyle: NSInformationalAlertStyle];
[alert setShowsSuppressionButton: YES];
[alert setMessageText: ZKLocalizedString(@"This will cause Spectacle to run in the background")];
[alert setInformativeText: ZKLocalizedString(@"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: ZKLocalizedString(@"OK")];
[alert addButtonWithTitle: ZKLocalizedString(@"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;
}
}
#pragma mark -
+ (NSArray *)hotKeyNames {
NSBundle *bundle = [SpectacleUtilities applicationBundle];
NSString *path = [bundle pathForResource: SpectacleHotKeyNamesPropertyListFile ofType: ZKPropertyListFileExtension];
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 setHotKeyAction: action];
[SpectacleUtilities updateHotKey: hotKey withPotentiallyNewDefaultHotKey: defaultHotKeys[hotKeyName]];
[hotKeys addObject: hotKey];
}
}
return hotKeys;
}
@end
#pragma mark -
@implementation ZeroKitHotKey
@end
#pragma mark -
@implementation SpectacleUtilities (SpectacleUtilitiesPrivate)
+ (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 setHotKeyCode: defaultHotKeyCode];
[hotKey setHotKeyModifiers: [defaultHotKey hotKeyModifiers]];
}
}
#pragma mark -
+ (NSDictionary *)defaultHotKeysWithNames: (NSArray *)names {
NSBundle *bundle = [SpectacleUtilities applicationBundle];
NSString *path = [bundle pathForResource: ZKDefaultPreferencesFile ofType: ZKPropertyListFileExtension];
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;
}
@end