From: Joe Wreschnig Date: Wed, 27 Feb 2013 22:56:02 +0000 (+0100) Subject: Remove hardcoded menu item offsets. Remove unused property. X-Git-Tag: version-1.0~105 X-Git-Url: https://git.yukkurigames.com/?a=commitdiff_plain;h=3a40cba25b9bb38887fe4809277d4c0f73462d12;p=enjoyable.git Remove hardcoded menu item offsets. Remove unused property. --- diff --git a/ApplicationController.h b/ApplicationController.h index e6e5614..581abc2 100644 --- a/ApplicationController.h +++ b/ApplicationController.h @@ -7,28 +7,26 @@ // #import + @class JoystickController; @class TargetController; @class ConfigsController; @interface ApplicationController : NSObject { - IBOutlet JoystickController *jsController; - IBOutlet TargetController *targetController; - IBOutlet ConfigsController *configsController; - - IBOutlet NSDrawer *drawer; - IBOutlet NSWindow *mainWindow; - IBOutlet NSToolbarItem* activeButton; - IBOutlet NSMenuItem* activeMenuItem; - IBOutlet NSMenu* dockMenuBase; + IBOutlet NSDrawer *drawer; + IBOutlet NSWindow *mainWindow; + IBOutlet NSToolbarItem *activeButton; + IBOutlet NSMenuItem *activeMenuItem; + IBOutlet NSMenu *dockMenuBase; } -@property(readwrite) BOOL active; -@property(strong, readonly) JoystickController * jsController; -@property(strong, readonly) TargetController * targetController; -@property(strong, readonly) ConfigsController * configsController; --(IBAction) toggleActivity: (id)sender; --(void) configsChanged; --(void) configChanged; +@property (strong) IBOutlet JoystickController *jsController; +@property (strong) IBOutlet TargetController *targetController; +@property (strong) IBOutlet ConfigsController *configsController; +@property (assign) BOOL active; + +- (IBAction)toggleActivity:(id)sender; +- (void)configsChanged; +- (void)configChanged; @end 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 diff --git a/ConfigsController.h b/ConfigsController.h index 7e79f15..7a83830 100644 --- a/ConfigsController.h +++ b/ConfigsController.h @@ -17,7 +17,6 @@ IBOutlet TargetController* targetController; Config* currentConfig; Config* neutralConfig; /* last config to be manually selected */ - ProcessSerialNumber attachedApplication; } -(IBAction) addPressed: (id)sender; @@ -30,7 +29,7 @@ @property(strong, readonly) Config* currentConfig; @property(strong, readonly) Config* currentNeutralConfig; @property(readonly) NSArray* configs; -@property(readonly) ProcessSerialNumber* targetApplication; + -(void) save; -(void) load; diff --git a/ConfigsController.m b/ConfigsController.m index cc16b6e..63211ae 100644 --- a/ConfigsController.m +++ b/ConfigsController.m @@ -26,6 +26,9 @@ [self activateConfig: neutralConfig forApplication: NULL]; } +// TODO: Not an appropriate way to track 'neutral configs', it should just +// always be the first config and be unremovable. + -(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn { if(currentConfig == config) return; @@ -33,7 +36,6 @@ if(psn) { if(!neutralConfig) neutralConfig = currentConfig; - attachedApplication = *psn; } else { neutralConfig = NULL; } @@ -183,10 +185,4 @@ [self restoreNeutralConfig]; } --(ProcessSerialNumber*) targetApplication { - if(neutralConfig) - return &attachedApplication; - return NULL; -} - @end