Support relaunching in the background as part of resume/launch items.
[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 if (NSRunningApplication.currentApplication.wasLaunchedAsLoginItemOrResume
52 && [NSUserDefaults.standardUserDefaults boolForKey:@"hidden in status item"]) {
53 [self transformIntoElement:self];
54 NSApplication *app = notification.object;
55 [app deactivate];
56 } else {
57 [window makeKeyAndOrderFront:nil];
58 }
59 }
60
61 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
62 hasVisibleWindows:(BOOL)flag {
63 [self restoreToForeground:theApplication];
64 return NO;
65 }
66
67 - (void)restoreToForeground:(id)sender {
68 ProcessSerialNumber psn = { 0, kCurrentProcess };
69 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
70 [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
71 [window makeKeyAndOrderFront:sender];
72 [NSObject cancelPreviousPerformRequestsWithTarget:self
73 selector:@selector(transformIntoElement:)
74 object:self];
75 [NSUserDefaults.standardUserDefaults setBool:NO forKey:@"hidden in status item"];
76 }
77
78 - (void)applicationWillBecomeActive:(NSNotification *)notification {
79 if (window.isVisible)
80 [self restoreToForeground:notification];
81 }
82
83 - (void)transformIntoElement:(id)sender {
84 ProcessSerialNumber psn = { 0, kCurrentProcess };
85 TransformProcessType(&psn, kProcessTransformToUIElementApplication);
86 [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"hidden in status item"];
87 }
88
89 - (void)flashStatusItem {
90 if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
91 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
92 } else {
93 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
94 }
95
96 }
97
98 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
99 [theApplication hide:theApplication];
100 // If we turn into a UIElement right away, the application cancels
101 // the deactivation events. The dock icon disappears, but an
102 // unresponsive menu bar remains until the user clicks somewhere.
103 // So delay just long enough to be past the end handling that.
104 [self performSelector:@selector(transformIntoElement:) withObject:self afterDelay:0.001];
105 return NO;
106 }
107
108 - (void)eventTranslationActivated:(NSNotification *)note {
109 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
110 [NSWorkspace.sharedWorkspace.notificationCenter
111 addObserver:self
112 selector:@selector(didSwitchApplication:)
113 name:NSWorkspaceDidActivateApplicationNotification
114 object:nil];
115 }
116
117 - (void)eventTranslationDeactivated:(NSNotification *)note {
118 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
119 [NSWorkspace.sharedWorkspace.notificationCenter
120 removeObserver:self
121 name:NSWorkspaceDidActivateApplicationNotification
122 object:nil];
123 }
124
125 - (void)mappingDidChange:(NSNotification *)note {
126 if (!window.isVisible)
127 for (int i = 0; i < 4; ++i)
128 [self performSelector:@selector(flashStatusItem)
129 withObject:self
130 afterDelay:0.2 * i];
131 }
132
133 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
134 return dockMenu;
135 }
136
137 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
138 [self restoreToForeground:sender];
139 NSURL *url = [NSURL fileURLWithPath:filename];
140 [self.mappingsController addMappingWithContentsOfURL:url];
141 return YES;
142 }
143
144 - (void)mappingWasChosen:(NJMapping *)mapping {
145 [self.mappingsController activateMapping:mapping];
146 }
147
148 - (void)mappingListShouldOpen {
149 [self restoreToForeground:self];
150 [self.mappingsController mappingPressed:self];
151 }
152
153
154 @end