2 // EnjoyableApplicationDelegate.m
5 // Created by Sam McCall on 4/05/09.
8 #import <Sparkle/Sparkle.h>
10 #import "EnjoyableApplicationDelegate.h"
13 #import "NJMappingsController.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(eventSimulationStarted:)
35 name:NJEventSimulationStarted
37 [NSNotificationCenter.defaultCenter
39 selector:@selector(eventSimulationStopped:)
40 name:NJEventSimulationStopped
43 [self.mappingsController load];
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;
52 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
53 if ([NSUserDefaults.standardUserDefaults boolForKey:@"hidden in status item"]
54 && NSRunningApplication.currentApplication.wasLaunchedAsLoginItemOrResume)
55 [self transformIntoElement:nil];
57 [window makeKeyAndOrderFront:nil];
60 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
61 hasVisibleWindows:(BOOL)flag {
62 [self restoreToForeground:theApplication];
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:)
74 [NSUserDefaults.standardUserDefaults setBool:NO forKey:@"hidden in status item"];
77 - (void)applicationWillBecomeActive:(NSNotification *)notification {
79 [self restoreToForeground:notification];
82 - (void)transformIntoElement:(id)sender {
83 ProcessSerialNumber psn = { 0, kCurrentProcess };
84 TransformProcessType(&psn, kProcessTransformToUIElementApplication);
85 [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"hidden in status item"];
88 - (void)flashStatusItem {
89 if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
90 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
92 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
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];
107 - (void)eventSimulationStarted:(NSNotification *)note {
108 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
109 [NSWorkspace.sharedWorkspace.notificationCenter
111 selector:@selector(didSwitchApplication:)
112 name:NSWorkspaceDidActivateApplicationNotification
116 - (void)eventSimulationStopped:(NSNotification *)note {
117 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
118 [NSWorkspace.sharedWorkspace.notificationCenter
120 name:NSWorkspaceDidActivateApplicationNotification
124 - (void)mappingDidChange:(NSNotification *)note {
125 if (!window.isVisible)
126 for (int i = 0; i < 4; ++i)
127 [self performSelector:@selector(flashStatusItem)
132 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
136 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
137 [self restoreToForeground:sender];
139 NSURL *URL = [NSURL fileURLWithPath:filename];
140 NJMapping *mapping = [NJMapping mappingWithContentsOfURL:URL
143 [self.mappingsController addOrMergeMapping:mapping];
146 [window presentError:error
147 modalForWindow:window
149 didPresentSelector:nil
155 - (void)mappingWasChosen:(NJMapping *)mapping {
156 [self.mappingsController activateMapping:mapping];
159 - (void)mappingListShouldOpen {
160 [self restoreToForeground:self];
161 [self.mappingsController.mvc mappingTriggerClicked:self];
164 - (void)loginItemPromptDidEnd:(NSWindow *)sheet
165 returnCode:(int)returnCode
166 contextInfo:(void *)contextInfo {
167 if (returnCode == NSAlertDefaultReturn) {
168 [NSRunningApplication.currentApplication addToLoginItems];
169 // If we're going to automatically start, don't bug the user
170 // about automatic updates next boot - they probably want it,
171 // and if they don't they probably want a prompt for it less.
172 SUUpdater.sharedUpdater.automaticallyChecksForUpdates = YES;
176 - (void)loginItemPromptDidDismiss:(NSWindow *)sheet
177 returnCode:(int)returnCode
178 contextInfo:(void *)contextInfo {
179 [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"explained login items"];
180 [window performClose:sheet];
183 - (BOOL)windowShouldClose:(NSWindow *)sender {
185 || NSRunningApplication.currentApplication.isLoginItem
186 || [NSUserDefaults.standardUserDefaults boolForKey:@"explained login items"])
189 NSLocalizedString(@"login items prompt", @"alert prompt for adding to login items"),
190 NSLocalizedString(@"login items add button", @"button to add to login items"),
191 NSLocalizedString(@"login items don't add button", @"button to not add to login items"),
193 @selector(loginItemPromptDidEnd:returnCode:contextInfo:),
194 @selector(loginItemPromptDidDismiss:returnCode:contextInfo:),
196 NSLocalizedString(@"login items explanation", @"a brief explanation of login items")
198 for (int i = 0; i < 10; ++i)
199 [self performSelector:@selector(flashStatusItem)
205 - (void)importMappingClicked:(id)sender {
206 NSOpenPanel *panel = [NSOpenPanel openPanel];
207 panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
208 [panel beginSheetModalForWindow:window
209 completionHandler:^(NSInteger result) {
210 if (result != NSFileHandlingPanelOKButton)
214 NJMapping *mapping = [NJMapping mappingWithContentsOfURL:panel.URL
217 [self.mappingsController addOrMergeMapping:mapping];
219 [window presentError:error
220 modalForWindow:window
222 didPresentSelector:nil
229 - (void)exportMappingClicked:(id)sender {
230 NSSavePanel *panel = [NSSavePanel savePanel];
231 panel.allowedFileTypes = @[ @"enjoyable" ];
232 NJMapping *mapping = self.mappingsController.currentMapping;
233 panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent];
234 [panel beginSheetModalForWindow:window
235 completionHandler:^(NSInteger result) {
236 if (result != NSFileHandlingPanelOKButton)
240 if (![mapping writeToURL:panel.URL error:&error]) {
241 [window presentError:error
242 modalForWindow:window
244 didPresentSelector:nil