forked from eczarny/spectacle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpectacleApplicationController.m
208 lines (151 loc) · 7.75 KB
/
SpectacleApplicationController.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
#import <Sparkle/Sparkle.h>
#import <HockeySDK/HockeySDK.h>
#import "SpectacleApplicationController.h"
#import "SpectaclePreferencesController.h"
#import "SpectacleHotKeyManager.h"
#import "SpectacleUtilities.h"
#import "SpectacleConstants.h"
#import "ZKHotKeyTranslator.h"
@interface SpectacleApplicationController ()
@property (nonatomic) NSStatusItem *statusItem;
@property (nonatomic) NSDictionary *hotKeyMenuItems;
@property (nonatomic) SpectaclePreferencesController *preferencesController;
@end
#pragma mark -
@implementation SpectacleApplicationController
- (void)applicationDidFinishLaunching: (NSNotification *)notification {
NSNotificationCenter *notificationCenter = NSNotificationCenter.defaultCenter;
[SpectacleUtilities registerDefaultsForBundle: NSBundle.mainBundle];
_preferencesController = [SpectaclePreferencesController new];
_hotKeyMenuItems = [[NSDictionary alloc] initWithObjectsAndKeys:
_moveToCenterHotKeyMenuItem, SpectacleWindowActionMoveToCenter,
_moveToFullscreenHotKeyMenuItem, SpectacleWindowActionMoveToFullscreen,
_moveToLeftHotKeyMenuItem, SpectacleWindowActionMoveToLeftHalf,
_moveToRightHotKeyMenuItem, SpectacleWindowActionMoveToRightHalf,
_moveToTopHotKeyMenuItem, SpectacleWindowActionMoveToTopHalf,
_moveToBottomHotKeyMenuItem, SpectacleWindowActionMoveToBottomHalf,
_moveToUpperLeftHotKeyMenuItem, SpectacleWindowActionMoveToUpperLeft,
_moveToLowerLeftHotKeyMenuItem, SpectacleWindowActionMoveToLowerLeft,
_moveToUpperRightHotKeyMenuItem, SpectacleWindowActionMoveToUpperRight,
_moveToLowerRightHotKeyMenuItem, SpectacleWindowActionMoveToLowerRight,
_moveToNextDisplayHotKeyMenuItem, SpectacleWindowActionMoveToNextDisplay,
_moveToPreviousDisplayHotKeyMenuItem, SpectacleWindowActionMoveToPreviousDisplay,
_moveToNextThirdHotKeyMenuItem, SpectacleWindowActionMoveToNextThird,
_moveToPreviousThirdHotKeyMenuItem, SpectacleWindowActionMoveToPreviousThird,
_makeLargerHotKeyMenuItem, SpectacleWindowActionMakeLarger,
_makeSmallerHotKeyMenuItem, SpectacleWindowActionMakeSmaller,
_undoLastMoveHotKeyMenuItem, SpectacleWindowActionUndoLastMove,
_redoLastMoveHotKeyMenuItem, SpectacleWindowActionRedoLastMove, nil];
[self registerHotKeys];
NSUserDefaults *userDefaults = NSUserDefaults.standardUserDefaults;
BOOL automaticallyChecksForUpdates = [userDefaults boolForKey: SpectacleAutomaticUpdateCheckEnabledPreference];
BOOL statusItemEnabled = [userDefaults boolForKey: SpectacleStatusItemEnabledPreference];
if (statusItemEnabled) {
[self createStatusItem];
}
[notificationCenter addObserver: self
selector: @selector(enableStatusItem)
name: SpectacleStatusItemEnabledNotification
object: nil];
[notificationCenter addObserver: self
selector: @selector(disableStatusItem)
name: SpectacleStatusItemDisabledNotification
object: nil];
[notificationCenter addObserver: self
selector: @selector(updateHotKeyMenuItems)
name: SpectacleHotKeyChangedNotification
object: nil];
[notificationCenter addObserver: self
selector: @selector(updateHotKeyMenuItems)
name: SpectacleRestoreDefaultHotKeysNotification
object: nil];
[notificationCenter addObserver: self
selector: @selector(menuDidSendAction:)
name: NSMenuDidSendActionNotification
object: nil];
[SUUpdater.sharedUpdater setAutomaticallyChecksForUpdates: automaticallyChecksForUpdates];
[BITHockeyManager.sharedHockeyManager configureWithIdentifier: @"d1ff699d780232448962aacb48ee6ec7"];
[BITHockeyManager.sharedHockeyManager startManager];
[self updateHotKeyMenuItems];
switch (SpectacleUtilities.spectacleTrust) {
case SpectacleIsNotTrusted:
[[NSApplication sharedApplication] runModalForWindow: _accessiblityAccessDialogWindow];
break;
default:
break;
}
[userDefaults removeObjectForKey: SpectacleBlacklistedWindowRectsPreference];
}
#pragma mark -
- (BOOL)applicationShouldHandleReopen: (NSApplication *)application hasVisibleWindows: (BOOL)visibleWindows {
[self showPreferencesWindow: self];
return YES;
}
#pragma mark -
- (IBAction)showPreferencesWindow: (id)sender {
[_preferencesController showWindow: sender];
}
#pragma mark -
- (IBAction)openSystemPreferences: (id)sender {
NSURL *preferencePaneURL = [NSURL fileURLWithPath: [SpectacleUtilities pathForPreferencePaneNamed: SpectacleSecurityPreferencePaneName]];
NSBundle *applicationBundle = NSBundle.mainBundle;
NSURL *scriptURL = [applicationBundle URLForResource: SpectacleSecurityAndPrivacyPreferencesScriptName withExtension: SpectacleAppleScriptFileExtension];
[NSApplication.sharedApplication stopModal];
[_accessiblityAccessDialogWindow orderOut: self];
if (![[[NSAppleScript alloc] initWithContentsOfURL: scriptURL error: nil] executeAndReturnError: nil]) {
[NSWorkspace.sharedWorkspace openURL: preferencePaneURL];
}
}
#pragma mark -
- (IBAction)restoreDefaults: (id)sender {
[SpectacleUtilities displayRestoreDefaultsAlertWithCallback: ^(BOOL isConfirmed) {
if (isConfirmed) {
[SpectacleUtilities restoreDefaultHotKeys];
[NSNotificationCenter.defaultCenter postNotificationName: SpectacleRestoreDefaultHotKeysNotification object: self];
}
}];
}
#pragma mark -
- (void)createStatusItem {
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength: NSVariableStatusItemLength];
NSImage *statusImage = [[NSImage alloc] initWithContentsOfFile: [NSBundle.mainBundle pathForImageResource:SpectacleStatusItemIcon]];
[statusImage setTemplate:YES];
_statusItem.image = statusImage;
_statusItem.highlightMode = YES;
_statusItem.toolTip = [NSString stringWithFormat: @"Spectacle %@", SpectacleUtilities.applicationVersion];
[_statusItem setMenu: _statusItemMenu];
}
- (void)destroyStatusItem {
[NSStatusBar.systemStatusBar removeStatusItem: _statusItem];
}
#pragma mark -
- (void)updateHotKeyMenuItems {
SpectacleHotKeyManager *hotKeyManager = SpectacleHotKeyManager.sharedManager;
ZKHotKeyTranslator *hotKeyTranslator = ZKHotKeyTranslator.sharedTranslator;
for (NSString *hotKeyName in _hotKeyMenuItems.allKeys) {
NSMenuItem *hotKeyMenuItem = _hotKeyMenuItems[hotKeyName];
ZKHotKey *hotKey = [hotKeyManager registeredHotKeyForName: hotKeyName];
if (hotKey) {
hotKeyMenuItem.keyEquivalent = [[hotKeyTranslator translateKeyCode: hotKey.hotKeyCode] lowercaseString];
hotKeyMenuItem.keyEquivalentModifierMask = [ZKHotKeyTranslator convertModifiersToCocoafNecessary: hotKey.hotKeyModifiers];
} else {
hotKeyMenuItem.keyEquivalent = @"";
hotKeyMenuItem.keyEquivalentModifierMask = 0;
}
}
}
#pragma mark -
- (void)enableStatusItem {
[self createStatusItem];
}
- (void)disableStatusItem {
[self destroyStatusItem];
}
#pragma mark -
- (void)menuDidSendAction: (NSNotification *)notification {
NSMenuItem *menuItem = [notification.userInfo objectForKey: @"MenuItem"];
if (menuItem.tag == SpectacleMenuItemActivateIgnoringOtherApps) {
[NSApplication.sharedApplication activateIgnoringOtherApps: YES];
}
}
@end