X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FEnjoyableApplicationDelegate.m;h=25b9937216283f5820ea401ede9f6496690c6846;hp=beab960bfe7c9df0bdabbb453119c9de9f8e2177;hb=a58f23691951d4d1e1961dddd514a7e9f3748203;hpb=4fd28dc6483fd7c8323196abdbf4f1833a284e03 diff --git a/Classes/EnjoyableApplicationDelegate.m b/Classes/EnjoyableApplicationDelegate.m index beab960..25b9937 100644 --- a/Classes/EnjoyableApplicationDelegate.m +++ b/Classes/EnjoyableApplicationDelegate.m @@ -15,6 +15,7 @@ @implementation EnjoyableApplicationDelegate { NSStatusItem *statusItem; + NSMutableArray *_errors; } - (void)didSwitchApplication:(NSNotification *)note { @@ -111,6 +112,8 @@ - (void)eventSimulationStarted:(NSNotification *)note { self.simulatingEventsButton.state = NSOnState; statusItem.image = [NSImage imageNamed:@"Status Menu Icon"]; + [NSProcessInfo.processInfo + disableAutomaticTermination:@"Event simulation running."]; [NSWorkspace.sharedWorkspace.notificationCenter addObserver:self selector:@selector(didSwitchApplication:) @@ -121,6 +124,8 @@ - (void)eventSimulationStopped:(NSNotification *)note { self.simulatingEventsButton.state = NSOffState; statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"]; + [NSProcessInfo.processInfo + enableAutomaticTermination:@"Event simulation running."]; [NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self name:NSWorkspaceDidActivateApplicationNotification @@ -143,6 +148,32 @@ return self.dockMenu; } +- (void)showNextError { + if (!self.window.attachedSheet && _errors.count) { + NSError *error = _errors.lastObject; + [_errors removeLastObject]; + [NSApplication.sharedApplication activateIgnoringOtherApps:YES]; + [self.window makeKeyAndOrderFront:nil]; + [self.window presentError:error + modalForWindow:self.window + delegate:self + didPresentSelector:@selector(didPresentErrorWithRecovery:contextInfo:) + contextInfo:nil]; + } +} + +- (void)didPresentErrorWithRecovery:(BOOL)didRecover + contextInfo:(void *)contextInfo { + [self showNextError]; +} + +- (void)presentErrorSheet:(NSError *)error { + if (!_errors) + _errors = [[NSMutableArray alloc] initWithCapacity:1]; + [_errors insertObject:error atIndex:0]; + [self showNextError]; +} + - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename { [self restoreToForeground:sender]; NSError *error; @@ -160,11 +191,7 @@ [self.mvc endUpdates]; [self.ic activateMapping:mapping]; } else { - [self.window presentError:error - modalForWindow:self.window - delegate:nil - didPresentSelector:nil - contextInfo:nil]; + [self presentErrorSheet:error]; } return !!mapping; } @@ -178,47 +205,6 @@ [self.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"]; - [self.window performClose:sheet]; -} - -- (BOOL)windowShouldClose:(NSWindow *)sender { - if (sender != self.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, self.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" ]; @@ -237,11 +223,7 @@ } else if (mapping) { [self.ic addMapping:mapping]; } else { - [self.window presentError:error - modalForWindow:self.window - delegate:nil - didPresentSelector:nil - contextInfo:nil]; + [self presentErrorSheet:error]; } }]; @@ -259,11 +241,7 @@ [panel close]; NSError *error; if (![mapping writeToURL:panel.URL error:&error]) { - [self.window presentError:error - modalForWindow:self.window - delegate:nil - didPresentSelector:nil - contextInfo:nil]; + [self presentErrorSheet:error]; } }]; } @@ -323,8 +301,7 @@ - (void)mappingsViewController:(NJMappingsViewController *)mvc renameMappingAtIndex:(NSInteger)index toName:(NSString *)name { - [self.ic renameMapping:self.ic.mappings[index] - to:name]; + [self.ic renameMapping:self.ic.mappings[index] to:name]; } - (BOOL)mappingsViewController:(NJMappingsViewController *)mvc @@ -443,18 +420,7 @@ } - (void)inputController:(NJInputController *)ic didError:(NSError *)error { - // Since the error shows the window, it can trigger another attempt - // to re-open the HID manager, which will also probably fail and error, - // so don't bother repeating ourselves. - if (!self.window.attachedSheet) { - [NSApplication.sharedApplication activateIgnoringOtherApps:YES]; - [self.window makeKeyAndOrderFront:nil]; - [self.window presentError:error - modalForWindow:self.window - delegate:nil - didPresentSelector:nil - contextInfo:nil]; - } + [self presentErrorSheet:error]; } - (NSInteger)numberOfDevicesInDeviceList:(NJDeviceViewController *)dvc {