X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FEnjoyableApplicationDelegate.m;h=26ea09cdf3321660eaceef0cc96dce8e03d155e5;hp=f02ec90663ba6e4d29a7db97076b4f6f38e85d9f;hb=24bdb92798b9abe86c7954042a47523791736b7c;hpb=f8ae7787fe3fe272eaf04586e3007339c907fb33 diff --git a/Classes/EnjoyableApplicationDelegate.m b/Classes/EnjoyableApplicationDelegate.m index f02ec90..26ea09c 100644 --- a/Classes/EnjoyableApplicationDelegate.m +++ b/Classes/EnjoyableApplicationDelegate.m @@ -5,6 +5,8 @@ // Created by Sam McCall on 4/05/09. // +#import + #import "EnjoyableApplicationDelegate.h" #import "NJMapping.h" @@ -29,13 +31,13 @@ object:nil]; [NSNotificationCenter.defaultCenter addObserver:self - selector:@selector(eventTranslationActivated:) - name:NJEventTranslationActivated + selector:@selector(eventSimulationStarted:) + name:NJEventSimulationStarted object:nil]; [NSNotificationCenter.defaultCenter addObserver:self - selector:@selector(eventTranslationDeactivated:) - name:NJEventTranslationDeactivated + selector:@selector(eventSimulationStopped:) + name:NJEventSimulationStopped object:nil]; [self.mappingsController load]; @@ -48,14 +50,11 @@ } - (void)applicationDidFinishLaunching:(NSNotification *)notification { - if (NSRunningApplication.currentApplication.wasLaunchedAsLoginItemOrResume - && [NSUserDefaults.standardUserDefaults boolForKey:@"hidden in status item"]) { - [self transformIntoElement:self]; - NSApplication *app = notification.object; - [app deactivate]; - } else { + if ([NSUserDefaults.standardUserDefaults boolForKey:@"hidden in status item"] + && NSRunningApplication.currentApplication.wasLaunchedAsLoginItemOrResume) + [self transformIntoElement:nil]; + else [window makeKeyAndOrderFront:nil]; - } } - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication @@ -105,7 +104,7 @@ return NO; } -- (void)eventTranslationActivated:(NSNotification *)note { +- (void)eventSimulationStarted:(NSNotification *)note { statusItem.image = [NSImage imageNamed:@"Status Menu Icon"]; [NSWorkspace.sharedWorkspace.notificationCenter addObserver:self @@ -114,7 +113,7 @@ object:nil]; } -- (void)eventTranslationDeactivated:(NSNotification *)note { +- (void)eventSimulationStopped:(NSNotification *)note { statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"]; [NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self @@ -136,9 +135,21 @@ - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename { [self restoreToForeground:sender]; - NSURL *url = [NSURL fileURLWithPath:filename]; - [self.mappingsController addMappingWithContentsOfURL:url]; - return YES; + NSError *error; + NSURL *URL = [NSURL fileURLWithPath:filename]; + NJMapping *mapping = [NJMapping mappingWithContentsOfURL:URL + error:&error]; + if (mapping) { + [self.mappingsController addOrMergeMapping:mapping]; + return YES; + } else { + [window presentError:error + modalForWindow:window + delegate:nil + didPresentSelector:nil + contextInfo:nil]; + return NO; + } } - (void)mappingWasChosen:(NJMapping *)mapping { @@ -147,8 +158,95 @@ - (void)mappingListShouldOpen { [self restoreToForeground:self]; - [self.mappingsController mappingPressed:self]; + [self.mappingsController.mvc mappingTriggerClicked:self]; +} + +- (void)loginItemPromptDidEnd:(NSWindow *)sheet + returnCode:(int)returnCode + contextInfo:(void *)contextInfo { + if (returnCode == NSAlertDefaultReturn) { + [NSRunningApplication.currentApplication addToLoginItems]; + // If we're going to automatically start, don't bug the user + // about automatic updates next boot - they probably want it, + // and if they don't they probably want a prompt for it less. + SUUpdater.sharedUpdater.automaticallyChecksForUpdates = YES; + } } +- (void)loginItemPromptDidDismiss:(NSWindow *)sheet + returnCode:(int)returnCode + contextInfo:(void *)contextInfo { + [NSUserDefaults.standardUserDefaults setBool:YES forKey:@"explained login items"]; + [window performClose:sheet]; +} + +- (BOOL)windowShouldClose:(NSWindow *)sender { + if (sender != window + || NSRunningApplication.currentApplication.isLoginItem + || [NSUserDefaults.standardUserDefaults boolForKey:@"explained login items"]) + return YES; + NSBeginAlertSheet( + NSLocalizedString(@"login items prompt", @"alert prompt for adding to login items"), + NSLocalizedString(@"login items add button", @"button to add to login items"), + NSLocalizedString(@"login items don't add button", @"button to not add to login items"), + nil, window, self, + @selector(loginItemPromptDidEnd:returnCode:contextInfo:), + @selector(loginItemPromptDidDismiss:returnCode:contextInfo:), + NULL, + NSLocalizedString(@"login items explanation", @"a brief explanation of login items") + ); + for (int i = 0; i < 10; ++i) + [self performSelector:@selector(flashStatusItem) + withObject:self + afterDelay:0.5 * i]; + return NO; +} + +- (void)importMappingClicked:(id)sender { + NSOpenPanel *panel = [NSOpenPanel openPanel]; + panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ]; + [panel beginSheetModalForWindow:window + completionHandler:^(NSInteger result) { + if (result != NSFileHandlingPanelOKButton) + return; + [panel close]; + NSError *error; + NJMapping *mapping = [NJMapping mappingWithContentsOfURL:panel.URL + error:&error]; + if (mapping) { + [self.mappingsController addOrMergeMapping:mapping]; + } else { + [window presentError:error + modalForWindow:window + delegate:nil + didPresentSelector:nil + contextInfo:nil]; + } + }]; + +} + +- (void)exportMappingClicked:(id)sender { + NSSavePanel *panel = [NSSavePanel savePanel]; + panel.allowedFileTypes = @[ @"enjoyable" ]; + NJMapping *mapping = self.mappingsController.currentMapping; + panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent]; + [panel beginSheetModalForWindow:window + completionHandler:^(NSInteger result) { + if (result != NSFileHandlingPanelOKButton) + return; + [panel close]; + NSError *error; + if (![mapping writeToURL:panel.URL error:&error]) { + [window presentError:error + modalForWindow:window + delegate:nil + didPresentSelector:nil + contextInfo:nil]; + } + }]; +} + + @end