Remove hardcoded menu item offsets. Remove unused property.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 27 Feb 2013 22:56:02 +0000 (23:56 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Wed, 27 Feb 2013 22:56:02 +0000 (23:56 +0100)
ApplicationController.h
ApplicationController.m
ConfigsController.h
ConfigsController.m

index e6e5614..581abc2 100644 (file)
@@ -7,28 +7,26 @@
 //
 
 #import <Cocoa/Cocoa.h>
+
 @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
index 55abf05..e87421c 100644 (file)
@@ -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
index 7e79f15..7a83830 100644 (file)
@@ -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;
 
index cc16b6e..63211ae 100644 (file)
@@ -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;
        }
        [self restoreNeutralConfig];
 }
 
--(ProcessSerialNumber*) targetApplication {
-       if(neutralConfig)
-               return &attachedApplication;
-       return NULL;
-}
-
 @end