X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=ApplicationController.m;fp=ApplicationController.m;h=e87421c062b0e173d01965140ca5bf2a21b8e5d1;hp=55abf057e250e9475ded859c096f231d83eede1a;hb=3a40cba25b9bb38887fe4809277d4c0f73462d12;hpb=250a974e2d9d2d2dd8dfb8a99b07a77c7e2f637a diff --git a/ApplicationController.m b/ApplicationController.m index 55abf05..e87421c 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -9,81 +9,91 @@ BOOL active; } -@synthesize jsController, targetController, configsController; +@synthesize jsController; +@synthesize targetController; +@synthesize configsController; - (void)didSwitchApplication:(NSNotification *)notification { NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; - ProcessSerialNumber psn; + ProcessSerialNumber psn; OSStatus err; if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) { - [self->configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn]; + [self.configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn]; } else { NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil]; NSLog(@"Error getting PSN for %@: %@", currentApp.localizedName, error); } } --(void) applicationDidFinishLaunching:(NSNotification*) notification { - [jsController setup]; - [drawer open]; - [targetController setEnabled: NO]; +- (void)applicationDidFinishLaunching:(NSNotification *)notification { + [drawer open]; + self.targetController.enabled = NO; self.active = NO; - [configsController load]; - [[[NSWorkspace sharedWorkspace] notificationCenter] + [self.jsController setup]; + [self.configsController load]; + [[NSWorkspace sharedWorkspace].notificationCenter addObserver:self selector:@selector(didSwitchApplication:) name:NSWorkspaceDidActivateApplicationNotification object:nil]; } --(void) applicationWillTerminate: (NSNotification *)aNotification { - [configsController save]; - [[[NSWorkspace sharedWorkspace] notificationCenter] +- (void)applicationWillTerminate:(NSNotification *)aNotification { + // TODO: Save immediately / shortly after changing and then enable + // sudden termination support. + [configsController save]; + [[NSWorkspace sharedWorkspace].notificationCenter removeObserver:self name:NSWorkspaceDidActivateApplicationNotification object:nil]; } -- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication - hasVisibleWindows:(BOOL)flag -{ - [mainWindow makeKeyAndOrderFront:self]; - return YES; -} +// TODO: Active state should probably be in the ConfigsController or +// JoystickController, not here. - (BOOL)active { return active; } - (void)setActive:(BOOL)newActive { - [activeButton setLabel:newActive ? @"Stop" : @"Start"]; - NSImage *buttonImage = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; - [activeButton setImage:buttonImage]; - [activeMenuItem setState:newActive]; - active = newActive; + activeButton.label = newActive ? @"Stop" : @"Start"; + activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; + activeMenuItem.state = newActive; + active = newActive; } - (IBAction)toggleActivity:(id)sender { self.active = !self.active; } --(void) configsChanged { - while([dockMenuBase numberOfItems] > 2) - [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)]; +- (NSUInteger)firstConfigMenuIndex { + NSUInteger count = dockMenuBase.numberOfItems; + for (int i = 0; i < count; ++i) + if ([dockMenuBase itemAtIndex:i].isSeparatorItem) + return i + 1; + return count; +} - for(Config* config in [configsController configs]) { - [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""]; - } - [self configChanged]; +- (void)configsChanged { + NSUInteger removeFrom = [self firstConfigMenuIndex]; + while (dockMenuBase.numberOfItems > removeFrom) + [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1]; + for (Config *config in self.configsController.configs) + [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""]; + [self configChanged]; } --(void) configChanged { - Config* current = [configsController currentConfig]; - NSArray* configs = [configsController configs]; - for(int i=0; i<[configs count]; i++) - [[dockMenuBase itemAtIndex: (2+i)] setState: (configs[i] == current)]; + +- (void)configChanged { + NSUInteger firstConfig = [self firstConfigMenuIndex]; + Config *current = self.configsController.currentConfig; + NSArray *configs = self.configsController.configs; + for (int i = 0; i < configs.count; ++i) + [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current; } --(void) chooseConfig: (id) sender { - [configsController activateConfig: [configsController configs][([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL]; +- (void)chooseConfig:(id)sender { + int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex]; + Config *chosen = self.configsController.configs[idx]; + [configsController activateConfig:chosen forApplication:NULL]; } @end