2 // EnjoyableApplicationDelegate.m
5 // Created by Sam McCall on 4/05/09.
8 #import "EnjoyableApplicationDelegate.h"
11 #import "NJMappingsController.h"
14 @implementation EnjoyableApplicationDelegate {
15 NSStatusItem *statusItem;
18 - (void)didSwitchApplication:(NSNotification *)note {
19 NSRunningApplication *activeApp = note.userInfo[NSWorkspaceApplicationKey];
21 [self.mappingsController activateMappingForProcess:activeApp];
24 - (void)applicationWillFinishLaunching:(NSNotification *)notification {
25 [NSNotificationCenter.defaultCenter
27 selector:@selector(mappingDidChange:)
28 name:NJEventMappingChanged
30 [NSNotificationCenter.defaultCenter
32 selector:@selector(eventTranslationActivated:)
33 name:NJEventTranslationActivated
35 [NSNotificationCenter.defaultCenter
37 selector:@selector(eventTranslationDeactivated:)
38 name:NJEventTranslationDeactivated
41 [self.mappingsController load];
43 statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:36];
44 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
45 statusItem.highlightMode = YES;
46 statusItem.menu = statusItemMenu;
47 statusItem.target = self;
50 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
51 [window makeKeyAndOrderFront:nil];
54 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
55 hasVisibleWindows:(BOOL)flag {
56 [self restoreToForeground:theApplication];
60 - (void)restoreToForeground:(id)sender {
61 ProcessSerialNumber psn = { 0, kCurrentProcess };
62 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
63 [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
64 [window makeKeyAndOrderFront:sender];
65 [NSObject cancelPreviousPerformRequestsWithTarget:self
66 selector:@selector(transformIntoElement:)
70 - (void)applicationWillBecomeActive:(NSNotification *)notification {
71 [self restoreToForeground:notification];
74 - (void)transformIntoElement:(id)sender {
75 ProcessSerialNumber psn = { 0, kCurrentProcess };
76 TransformProcessType(&psn, kProcessTransformToUIElementApplication);
79 - (void)flashStatusItem {
80 if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
81 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
83 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
88 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
89 [theApplication hide:theApplication];
90 // If we turn into a UIElement right away, the application cancels
91 // the deactivation events. The dock icon disappears, but an
92 // unresponsive menu bar remains until the user clicks somewhere.
93 // So delay just long enough to be past the end handling that.
94 [self performSelector:@selector(transformIntoElement:) withObject:self afterDelay:0.001];
98 - (void)eventTranslationActivated:(NSNotification *)note {
99 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
100 [NSWorkspace.sharedWorkspace.notificationCenter
102 selector:@selector(didSwitchApplication:)
103 name:NSWorkspaceDidActivateApplicationNotification
107 - (void)eventTranslationDeactivated:(NSNotification *)note {
108 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
109 [NSWorkspace.sharedWorkspace.notificationCenter
111 name:NSWorkspaceDidActivateApplicationNotification
115 - (void)mappingDidChange:(NSNotification *)note {
116 if (!window.isVisible)
117 for (int i = 0; i < 4; ++i)
118 [self performSelector:@selector(flashStatusItem)
123 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
127 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
128 [self restoreToForeground:sender];
129 NSURL *url = [NSURL fileURLWithPath:filename];
130 [self.mappingsController addMappingWithContentsOfURL:url];
134 - (void)mappingWasChosen:(NJMapping *)mapping {
135 [self.mappingsController activateMapping:mapping];
138 - (void)mappingListShouldOpen {
139 [self restoreToForeground:self];
140 [self.mappingsController mappingPressed:self];