f1513996c2d8687d8a3c47d9e97d60012f04a35c
[enjoyable.git] / Classes / EnjoyableApplicationDelegate.m
1 //
2 // EnjoyableApplicationDelegate.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "EnjoyableApplicationDelegate.h"
9
10 #import "NJMapping.h"
11 #import "NJMappingsController.h"
12 #import "NJEvents.h"
13
14 @implementation EnjoyableApplicationDelegate {
15 NSStatusItem *statusItem;
16 }
17
18 - (void)didSwitchApplication:(NSNotification *)note {
19 NSRunningApplication *activeApp = note.userInfo[NSWorkspaceApplicationKey];
20 if (activeApp)
21 [self.mappingsController activateMappingForProcess:activeApp];
22 }
23
24 - (void)applicationWillFinishLaunching:(NSNotification *)notification {
25 [NSNotificationCenter.defaultCenter
26 addObserver:self
27 selector:@selector(mappingDidChange:)
28 name:NJEventMappingChanged
29 object:nil];
30 [NSNotificationCenter.defaultCenter
31 addObserver:self
32 selector:@selector(eventTranslationActivated:)
33 name:NJEventTranslationActivated
34 object:nil];
35 [NSNotificationCenter.defaultCenter
36 addObserver:self
37 selector:@selector(eventTranslationDeactivated:)
38 name:NJEventTranslationDeactivated
39 object:nil];
40
41 [self.mappingsController load];
42
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;
48 }
49
50 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
51 [window makeKeyAndOrderFront:nil];
52 }
53
54 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
55 hasVisibleWindows:(BOOL)flag {
56 [self restoreToForeground:theApplication];
57 return NO;
58 }
59
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:)
67 object:self];
68 }
69
70 - (void)applicationWillBecomeActive:(NSNotification *)notification {
71 [self restoreToForeground:notification];
72 }
73
74 - (void)transformIntoElement:(id)sender {
75 ProcessSerialNumber psn = { 0, kCurrentProcess };
76 TransformProcessType(&psn, kProcessTransformToUIElementApplication);
77 }
78
79 - (void)flashStatusItem {
80 if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
81 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
82 } else {
83 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
84 }
85
86 }
87
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];
95 return NO;
96 }
97
98 - (void)eventTranslationActivated:(NSNotification *)note {
99 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
100 [NSWorkspace.sharedWorkspace.notificationCenter
101 addObserver:self
102 selector:@selector(didSwitchApplication:)
103 name:NSWorkspaceDidActivateApplicationNotification
104 object:nil];
105 }
106
107 - (void)eventTranslationDeactivated:(NSNotification *)note {
108 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
109 [NSWorkspace.sharedWorkspace.notificationCenter
110 removeObserver:self
111 name:NSWorkspaceDidActivateApplicationNotification
112 object:nil];
113 }
114
115 - (void)mappingDidChange:(NSNotification *)note {
116 if (!window.isVisible)
117 for (int i = 0; i < 4; ++i)
118 [self performSelector:@selector(flashStatusItem)
119 withObject:self
120 afterDelay:0.2 * i];
121 }
122
123 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
124 return dockMenu;
125 }
126
127 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
128 [self restoreToForeground:sender];
129 NSURL *url = [NSURL fileURLWithPath:filename];
130 [self.mappingsController addMappingWithContentsOfURL:url];
131 return YES;
132 }
133
134 - (void)mappingWasChosen:(NJMapping *)mapping {
135 [self.mappingsController activateMapping:mapping];
136 }
137
138 - (void)mappingListShouldOpen {
139 [self restoreToForeground:self];
140 [self.mappingsController mappingPressed:self];
141 }
142
143
144 @end