2 // EnjoyableApplicationDelegate.m
5 // Created by Sam McCall on 4/05/09.
8 #import "EnjoyableApplicationDelegate.h"
11 #import "NJMappingsController.h"
12 #import "NJDeviceController.h"
13 #import "NJOutputController.h"
16 @implementation EnjoyableApplicationDelegate {
17 NSStatusItem *statusItem;
20 - (void)didSwitchApplication:(NSNotification *)note {
21 NSRunningApplication *activeApp = note.userInfo[NSWorkspaceApplicationKey];
23 [self.mappingsController activateMappingForProcess:activeApp];
26 - (void)applicationWillFinishLaunching:(NSNotification *)notification {
27 [NSNotificationCenter.defaultCenter
29 selector:@selector(mappingDidChange:)
30 name:NJEventMappingChanged
32 [NSNotificationCenter.defaultCenter
34 selector:@selector(mappingListDidChange:)
35 name:NJEventMappingListChanged
37 [NSNotificationCenter.defaultCenter
39 selector:@selector(eventTranslationActivated:)
40 name:NJEventTranslationActivated
42 [NSNotificationCenter.defaultCenter
44 selector:@selector(eventTranslationDeactivated:)
45 name:NJEventTranslationDeactivated
48 [self.mappingsController load];
50 statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:36];
51 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
52 statusItem.highlightMode = YES;
53 statusItem.menu = statusItemMenu;
54 statusItem.target = self;
57 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
58 [self.inputController setup];
59 [window makeKeyAndOrderFront:nil];
62 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
63 hasVisibleWindows:(BOOL)flag {
64 [self restoreToForeground:theApplication];
68 - (void)restoreToForeground:(id)sender {
69 ProcessSerialNumber psn = { 0, kCurrentProcess };
70 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
71 [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
72 [window makeKeyAndOrderFront:sender];
73 [NSObject cancelPreviousPerformRequestsWithTarget:self
74 selector:@selector(transformIntoElement:)
78 - (void)transformIntoElement:(id)sender {
79 ProcessSerialNumber psn = { 0, kCurrentProcess };
80 TransformProcessType(&psn, kProcessTransformToUIElementApplication);
83 - (void)flashStatusItem {
84 if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
85 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
87 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
92 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
93 [theApplication hide:theApplication];
94 // If we turn into a UIElement right away, the application cancels
95 // the deactivation events. The dock icon disappears, but an
96 // unresponsive menu bar remains until the user clicks somewhere.
97 // So delay just long enough to be past the end handling that.
98 [self performSelector:@selector(transformIntoElement:) withObject:self afterDelay:0.001];
102 - (void)eventTranslationActivated:(NSNotification *)note {
103 [dockMenu itemAtIndex:0].state = NSOnState;
104 [statusItemMenu itemAtIndex:0].state = NSOnState;
105 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
106 [NSWorkspace.sharedWorkspace.notificationCenter
108 selector:@selector(didSwitchApplication:)
109 name:NSWorkspaceDidActivateApplicationNotification
113 - (void)eventTranslationDeactivated:(NSNotification *)note {
114 [dockMenu itemAtIndex:0].state = NSOffState;
115 [statusItemMenu itemAtIndex:0].state = NSOffState;
116 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
117 [NSWorkspace.sharedWorkspace.notificationCenter
119 name:NSWorkspaceDidActivateApplicationNotification
123 - (void)restoreWindowAndShowMappings:(id)sender {
124 [self restoreToForeground:sender];
125 [self.mappingsController mappingPressed:sender];
128 - (void)addMappingsToMenu:(NSMenu *)menu withKeys:(BOOL)withKeys atIndex:(NSInteger)index {
129 static const NSUInteger MAXIMUM_ITEMS = 5;
131 for (NJMapping *mapping in self.mappingsController) {
132 NSString *keyEquiv = (++added < 10 && withKeys) ? @(added).stringValue : @"";
133 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
134 action:@selector(chooseMapping:)
135 keyEquivalent:keyEquiv];
136 item.representedObject = mapping;
137 item.state = mapping == self.mappingsController.currentMapping;
138 [menu insertItem:item atIndex:index++];
139 if (added == MAXIMUM_ITEMS && self.mappingsController.mappings.count > MAXIMUM_ITEMS + 1) {
140 NSMenuItem *end = [[NSMenuItem alloc] initWithTitle:@"…"
141 action:@selector(restoreWindowAndShowMappings:)
144 [menu insertItem:end atIndex:index++];
150 - (void)mappingListDidChange:(NSNotification *)note {
151 while (mappingsMenu.lastItem.representedObject)
152 [mappingsMenu removeLastItem];
153 [self addMappingsToMenu:mappingsMenu withKeys:YES atIndex:mappingsMenu.numberOfItems];
154 while ([statusItemMenu itemAtIndex:2].representedObject)
155 [statusItemMenu removeItemAtIndex:2];
156 [self addMappingsToMenu:statusItemMenu withKeys:NO atIndex:2];
159 - (void)mappingDidChange:(NSNotification *)note {
160 NJMapping *current = note.object;
161 for (NSMenuItem *item in mappingsMenu.itemArray)
162 if (item.representedObject)
163 item.state = item.representedObject == current;
164 for (NSMenuItem *item in statusItemMenu.itemArray)
165 if (item.representedObject)
166 item.state = item.representedObject == current;
168 if (!window.isVisible)
169 for (int i = 0; i < 4; ++i)
170 [self performSelector:@selector(flashStatusItem)
175 - (void)chooseMapping:(NSMenuItem *)sender {
176 NJMapping *chosen = sender.representedObject;
177 [self.mappingsController activateMapping:chosen];
180 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
181 while (dockMenu.lastItem.representedObject)
182 [dockMenu removeLastItem];
183 [self addMappingsToMenu:dockMenu withKeys:NO atIndex:dockMenu.numberOfItems];
187 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
188 [self restoreToForeground:sender];
189 NSURL *url = [NSURL fileURLWithPath:filename];
190 [self.mappingsController addMappingWithContentsOfURL:url];