b1d9907fb850cd85f7f05679c5acdbb8d6ce3224
[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 <Sparkle/Sparkle.h>
9
10 #import "EnjoyableApplicationDelegate.h"
11
12 #import "NJMapping.h"
13 #import "NJMappingsController.h"
14 #import "NJEvents.h"
15
16 @implementation EnjoyableApplicationDelegate {
17 NSStatusItem *statusItem;
18 }
19
20 - (void)didSwitchApplication:(NSNotification *)note {
21 NSRunningApplication *activeApp = note.userInfo[NSWorkspaceApplicationKey];
22 if (activeApp)
23 [self.mappingsController activateMappingForProcess:activeApp];
24 }
25
26 - (void)applicationWillFinishLaunching:(NSNotification *)notification {
27 [NSNotificationCenter.defaultCenter
28 addObserver:self
29 selector:@selector(mappingDidChange:)
30 name:NJEventMappingChanged
31 object:nil];
32 [NSNotificationCenter.defaultCenter
33 addObserver:self
34 selector:@selector(eventTranslationActivated:)
35 name:NJEventTranslationActivated
36 object:nil];
37 [NSNotificationCenter.defaultCenter
38 addObserver:self
39 selector:@selector(eventTranslationDeactivated:)
40 name:NJEventTranslationDeactivated
41 object:nil];
42
43 [self.mappingsController load];
44
45 statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:36];
46 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
47 statusItem.highlightMode = YES;
48 statusItem.menu = statusItemMenu;
49 statusItem.target = self;
50 }
51
52 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
53 if ([NSUserDefaults.standardUserDefaults boolForKey:@"hidden in status item"]
54 && NSRunningApplication.currentApplication.wasLaunchedAsLoginItemOrResume)
55 [self transformIntoElement:nil];
56 else
57 [window makeKeyAndOrderFront:nil];
58 }
59
60 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
61 hasVisibleWindows:(BOOL)flag {
62 [self restoreToForeground:theApplication];
63 return NO;
64 }
65
66 - (void)restoreToForeground:(id)sender {
67 ProcessSerialNumber psn = { 0, kCurrentProcess };
68 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
69 [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
70 [window makeKeyAndOrderFront:sender];
71 [NSObject cancelPreviousPerformRequestsWithTarget:self
72 selector:@selector(transformIntoElement:)
73 object:self];
74 [NSUserDefaults.standardUserDefaults setBool:NO forKey:@"hidden in status item"];
75 }
76
77 - (void)applicationWillBecomeActive:(NSNotification *)notification {
78 if (window.isVisible)
79 [self restoreToForeground:notification];
80 }
81
82 - (void)transformIntoElement:(id)sender {
83 ProcessSerialNumber psn = { 0, kCurrentProcess };
84 TransformProcessType(&psn, kProcessTransformToUIElementApplication);
85 [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"hidden in status item"];
86 }
87
88 - (void)flashStatusItem {
89 if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
90 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
91 } else {
92 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
93 }
94
95 }
96
97 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
98 [theApplication hide:theApplication];
99 // If we turn into a UIElement right away, the application cancels
100 // the deactivation events. The dock icon disappears, but an
101 // unresponsive menu bar remains until the user clicks somewhere.
102 // So delay just long enough to be past the end handling that.
103 [self performSelector:@selector(transformIntoElement:) withObject:self afterDelay:0.001];
104 return NO;
105 }
106
107 - (void)eventTranslationActivated:(NSNotification *)note {
108 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
109 [NSWorkspace.sharedWorkspace.notificationCenter
110 addObserver:self
111 selector:@selector(didSwitchApplication:)
112 name:NSWorkspaceDidActivateApplicationNotification
113 object:nil];
114 }
115
116 - (void)eventTranslationDeactivated:(NSNotification *)note {
117 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
118 [NSWorkspace.sharedWorkspace.notificationCenter
119 removeObserver:self
120 name:NSWorkspaceDidActivateApplicationNotification
121 object:nil];
122 }
123
124 - (void)mappingDidChange:(NSNotification *)note {
125 if (!window.isVisible)
126 for (int i = 0; i < 4; ++i)
127 [self performSelector:@selector(flashStatusItem)
128 withObject:self
129 afterDelay:0.2 * i];
130 }
131
132 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
133 return dockMenu;
134 }
135
136 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
137 [self restoreToForeground:sender];
138 NSURL *url = [NSURL fileURLWithPath:filename];
139 [self.mappingsController addMappingWithContentsOfURL:url];
140 return YES;
141 }
142
143 - (void)mappingWasChosen:(NJMapping *)mapping {
144 [self.mappingsController activateMapping:mapping];
145 }
146
147 - (void)mappingListShouldOpen {
148 [self restoreToForeground:self];
149 [self.mappingsController mappingPressed:self];
150 }
151
152 - (void)loginItemPromptDidEnd:(NSWindow *)sheet
153 returnCode:(int)returnCode
154 contextInfo:(void *)contextInfo {
155 if (returnCode == NSAlertDefaultReturn) {
156 [NSRunningApplication.currentApplication addToLoginItems];
157 // If we're going to automatically start, don't bug the user
158 // about automatic updates next boot - they probably want it,
159 // and if they don't they probably want a prompt for it less.
160 SUUpdater.sharedUpdater.automaticallyChecksForUpdates = YES;
161 }
162 }
163
164 - (void)loginItemPromptDidDismiss:(NSWindow *)sheet
165 returnCode:(int)returnCode
166 contextInfo:(void *)contextInfo {
167 [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"explained login items"];
168 [window performClose:sheet];
169 }
170
171 - (BOOL)windowShouldClose:(NSWindow *)sender {
172 if (sender != window
173 || NSRunningApplication.currentApplication.isLoginItem
174 || [NSUserDefaults.standardUserDefaults boolForKey:@"explained login items"])
175 return YES;
176 NSBeginAlertSheet(
177 NSLocalizedString(@"login items prompt", @"alert prompt for adding to login items"),
178 NSLocalizedString(@"login items add button", @"button to add to login items"),
179 NSLocalizedString(@"login items don't add button", @"button to not add to login items"),
180 nil, window, self,
181 @selector(loginItemPromptDidEnd:returnCode:contextInfo:),
182 @selector(loginItemPromptDidDismiss:returnCode:contextInfo:),
183 NULL,
184 NSLocalizedString(@"login items explanation", @"a brief explanation of login items")
185 );
186 for (int i = 0; i < 10; ++i)
187 [self performSelector:@selector(flashStatusItem)
188 withObject:self
189 afterDelay:0.5 * i];
190 return NO;
191 }
192
193
194 @end