forked from fe9lix/CodingKeys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppDelegate.m
More file actions
125 lines (95 loc) · 3.91 KB
/
AppDelegate.m
File metadata and controls
125 lines (95 loc) · 3.91 KB
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
#import "AppDelegate.h"
#import "AppService.h"
#import "HotKeyService.h"
#import "HotKey.h"
#import "LaunchService.h"
#import <Carbon/Carbon.h>
@interface AppDelegate ()
@property (weak) IBOutlet NSMenu *statusMenu;
@property (strong) NSStatusItem *statusItem;
@property (weak) IBOutlet NSMenuItem *launchAtStartupMenuItem;
@end
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self setup];
}
- (void)setup {
[self setupNotifications];
[self registerHotKeys];
}
- (void)awakeFromNib {
[self setupStatusBarItem];
}
- (void)setupStatusBarItem {
self.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
self.statusItem.menu = self.statusMenu;
self.statusItem.image = [NSImage imageNamed:@"status_bar_icon"];
self.statusItem.alternateImage = [NSImage imageNamed:@"status_bar_icon_alternate"];
self.statusItem.highlightMode = YES;
BOOL isLaunchedAtStartup = [[LaunchService sharedService] isLaunchedAtStartup];
self.launchAtStartupMenuItem.state = isLaunchedAtStartup ? NSOnState : NSOffState;
}
- (void)setupNotifications {
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(didActivateApplication:)
name:NSWorkspaceDidActivateApplicationNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didTriggerHotKey:)
name:HotKeyHandlerDidTriggerHotKey
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeHotKeys:)
name:AppServiceDidChangeHotKeys
object:nil];
}
- (void)didActivateApplication:(NSNotification *)notification {
[self registerHotKeys];
}
- (void)registerHotKeys {
[self unregisterHotKeys];
NSString *activeAppName = [self activeApplicationName];
if ([[AppService sharedService] isAppRegistered:activeAppName]) {
[self registerHotKeysForApp:activeAppName];
}
}
- (void)registerHotKeysForApp:(NSString *)app {
HotKeyService *hotKeyService = [HotKeyService sharedService];
NSArray *hotKeys = [[AppService sharedService] hotKeysForAppWithName:app];
for (HotKey *hotKey in hotKeys) {
[hotKeyService registerHotKey:hotKey];
}
}
- (void)unregisterHotKeys {
[[HotKeyService sharedService] unregisterAllHotKeys];
}
- (NSString *)activeApplicationName {
NSDictionary *activeApp = [[NSWorkspace sharedWorkspace] activeApplication];
return (NSString *)[activeApp objectForKey:@"NSApplicationName"];
}
- (void)didTriggerHotKey:(NSNotification *)notification {
HotKey *hotKey = notification.userInfo[@"hotKey"];
NSArray *mappedHotKeys = [hotKey mappedHotKeysForAppWithName:[self activeApplicationName]];
if ([mappedHotKeys count] == 0) {
return;
}
[[HotKeyService sharedService] dispatchKeyEventsForHotKeys:mappedHotKeys];
}
- (void)didChangeHotKeys:(NSNotification *)notification {
[self registerHotKeys];
}
- (IBAction)toggleLaunchAtStartup:(id)sender {
BOOL enabled = (self.launchAtStartupMenuItem.state == NSOnState);
self.launchAtStartupMenuItem.state = enabled ? NSOffState : NSOnState;
[[LaunchService sharedService] launchAtStartup:!enabled];
}
- (IBAction)keyMappingsClicked:(id)sender {
[[AppService sharedService] openKeyMappings];
}
- (IBAction)helpClicked:(id)sender {
[[AppService sharedService] openAboutURL];
}
- (IBAction)quitClicked:(id)sender {
[NSApp terminate:self];
}
@end