forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectaclePreferencesController.m
185 lines (134 loc) · 6.3 KB
/
SpectaclePreferencesController.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
#import "SpectaclePreferencesController.h"
#import "SpectacleHotKeyManager.h"
#import "SpectacleHotKeyValidator.h"
#import "SpectacleWindowPositionManager.h"
#import "SpectacleUtilities.h"
#import "SpectacleConstants.h"
@interface SpectaclePreferencesController (SpectaclePreferencesControllerPrivate)
- (void)loadRegisteredHotKeys;
#pragma mark -
- (void)enableHotKeyRecorders: (BOOL)enabled;
@end
#pragma mark -
@implementation SpectaclePreferencesController
- (id)init {
if ((self = [super initWithWindowNibName: SpectaclePreferencesWindowNibName])) {
hotKeyManager = [SpectacleHotKeyManager sharedManager];
}
return self;
}
#pragma mark -
- (void)windowDidLoad {
NSInteger loginItemEnabledState = NSOffState;
BOOL isStatusItemEnabled = [[NSUserDefaults standardUserDefaults] boolForKey: SpectacleStatusItemEnabledPreference];
hotKeyRecorders = [[NSDictionary alloc] initWithObjectsAndKeys:
moveToCenterHotKeyRecorder, SpectacleWindowActionMoveToCenter,
moveToFullscreenHotKeyRecorder, SpectacleWindowActionMoveToFullscreen,
moveToLeftHotKeyRecorder, SpectacleWindowActionMoveToLeftHalf,
moveToRightHotKeyRecorder, SpectacleWindowActionMoveToRightHalf,
moveToTopHotKeyRecorder, SpectacleWindowActionMoveToTopHalf,
moveToBottomHotKeyRecorder, SpectacleWindowActionMoveToBottomHalf,
moveToUpperLeftHotKeyRecorder, SpectacleWindowActionMoveToUpperLeft,
moveToLowerLeftHotKeyRecorder, SpectacleWindowActionMoveToLowerLeft,
moveToUpperRightHotKeyRecorder, SpectacleWindowActionMoveToUpperRight,
moveToLowerRightHotKeyRecorder, SpectacleWindowActionMoveToLowerRight,
moveToNextDisplayHotKeyRecorder, SpectacleWindowActionMoveToNextDisplay,
moveToPreviousDisplayHotKeyRecorder, SpectacleWindowActionMoveToPreviousDisplay,
moveToNextThirdHotKeyRecorder, SpectacleWindowActionMoveToNextThird,
moveToPreviousThirdHotKeyRecorder, SpectacleWindowActionMoveToPreviousThird,
makeLargerHotKeyRecorder, SpectacleWindowActionMakeLarger,
makeSmallerHotKeyRecorder, SpectacleWindowActionMakeSmaller,
undoLastMoveHotKeyRecorder, SpectacleWindowActionUndoLastMove,
redoLastMoveHotKeyRecorder, SpectacleWindowActionRedoLastMove, nil];
[self loadRegisteredHotKeys];
if ([SpectacleUtilities isLoginItemEnabledForBundle: [SpectacleUtilities applicationBundle]]) {
loginItemEnabledState = NSOnState;
}
[loginItemEnabled setState: loginItemEnabledState];
[statusItemEnabled selectItemWithTag: isStatusItemEnabled ? 0 : 1];
}
#pragma mark -
- (void)toggleWindow: (id)sender {
if ([[self window] isKeyWindow]) {
[self hideWindow: sender];
} else {
[self showWindow: sender];
}
}
#pragma mark -
- (void)hideWindow: (id)sender {
[self close];
}
#pragma mark -
- (void)hotKeyRecorder: (ZKHotKeyRecorder *)hotKeyRecorder didReceiveNewHotKey: (ZKHotKey *)hotKey {
SpectacleWindowPositionManager *windowPositionManager = [SpectacleWindowPositionManager sharedManager];
[hotKey setHotKeyAction: ^(ZKHotKey *hotKey) {
[windowPositionManager moveFrontMostWindowWithAction: [windowPositionManager windowActionForHotKey: hotKey]];
}];
[hotKeyManager registerHotKey: hotKey];
}
- (void)hotKeyRecorder: (ZKHotKeyRecorder *)hotKeyRecorder didClearExistingHotKey: (ZKHotKey *)hotKey {
[hotKeyManager unregisterHotKeyForName: [hotKey hotKeyName]];
}
#pragma mark -
- (IBAction)toggleLoginItem: (id)sender {
NSBundle *applicationBundle = [SpectacleUtilities applicationBundle];
if ([loginItemEnabled state] == NSOnState) {
[SpectacleUtilities enableLoginItemForBundle: applicationBundle];
} else{
[SpectacleUtilities disableLoginItemForBundle: applicationBundle];
}
}
- (IBAction)toggleStatusItem: (id)sender {
NSString *notificationName = SpectacleStatusItemEnabledNotification;
BOOL isStatusItemEnabled = YES;
__block BOOL statusItemStateChanged = YES;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([userDefaults boolForKey: SpectacleStatusItemEnabledPreference] == ([[sender selectedItem] tag] == 0)) {
return;
}
if ([[sender selectedItem] tag] != 0) {
notificationName = SpectacleStatusItemDisabledNotification;
isStatusItemEnabled = NO;
if (![userDefaults boolForKey: SpectacleBackgroundAlertSuppressedPreference]) {
[SpectacleUtilities displayRunningInBackgroundAlertWithCallback: ^(BOOL isConfirmed, BOOL isSuppressed) {
if (!isConfirmed) {
statusItemStateChanged = NO;
[sender selectItemWithTag: 0];
}
[userDefaults setBool: isSuppressed forKey: SpectacleBackgroundAlertSuppressedPreference];
}];
}
}
if (statusItemStateChanged) {
[[NSNotificationCenter defaultCenter] postNotificationName: notificationName object: self];
[userDefaults setBool: isStatusItemEnabled forKey: SpectacleStatusItemEnabledPreference];
}
}
@end
#pragma mark -
@implementation SpectaclePreferencesController (SpectaclePreferencesControllerPrivate)
- (void)loadRegisteredHotKeys {
SpectacleHotKeyValidator *hotKeyValidator = [SpectacleHotKeyValidator new];
for (NSString *hotKeyName in [hotKeyRecorders allKeys]) {
ZKHotKeyRecorder *hotKeyRecorder = hotKeyRecorders[hotKeyName];
ZKHotKey *hotKey = [hotKeyManager registeredHotKeyForName: hotKeyName];
[hotKeyRecorder setHotKeyName: hotKeyName];
if (hotKey) {
[hotKeyRecorder setHotKey: hotKey];
}
[hotKeyRecorder setDelegate: self];
[hotKeyRecorder setAdditionalHotKeyValidators: @[hotKeyValidator]];
}
[self enableHotKeyRecorders: YES];
}
#pragma mark -
- (void)enableHotKeyRecorders: (BOOL)enabled {
for (ZKHotKeyRecorder *hotKeyRecorder in [hotKeyRecorders allValues]) {
if (!enabled) {
[hotKeyRecorder setHotKey: nil];
}
[hotKeyRecorder setEnabled: enabled];
}
}
@end