Save after deleting a config. Reset target to no-op when deleting its config, rather...
[enjoyable.git] / ApplicationController.m
index 55abf05..4454565 100644 (file)
@@ -5,85 +5,83 @@
 //  Created by Sam McCall on 4/05/09.
 //
 
+#import "ApplicationController.h"
+
+#import "Config.h"
+#import "ConfigsController.h"
+#import "JoystickController.h"
+#import "TargetController.h"
+
 @implementation ApplicationController {
     BOOL active;
 }
 
-@synthesize jsController, targetController, configsController;
-
 - (void)didSwitchApplication:(NSNotification *)notification {
     NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
-       ProcessSerialNumber psn;
-    OSStatus err;
-    if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) {
-        [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);
-    }
+    [self.configsController activateConfigForProcess:currentApp.localizedName];
 }
 
--(void) applicationDidFinishLaunching:(NSNotification*) notification {
-       [jsController setup];
-       [drawer open];
-       [targetController setEnabled: NO];
-    self.active = NO;
-       [configsController load];
-    [[[NSWorkspace sharedWorkspace] notificationCenter]
+- (void)applicationDidFinishLaunching:(NSNotification *)notification {
+    [drawer open];
+    self.targetController.enabled = NO;
+    [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 {
+       [[NSUserDefaults standardUserDefaults] synchronize];
+    [[NSWorkspace sharedWorkspace].notificationCenter
      removeObserver:self
      name:NSWorkspaceDidActivateApplicationNotification
      object:nil];
 }
 
-- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
-                                        hasVisibleWindows:(BOOL)flag
-{      
-       [mainWindow makeKeyAndOrderFront:self];
-       return YES;
-}
-
-- (BOOL)active {
-    return active;
+- (IBAction)toggleActivity:(id)sender {
+    BOOL sendRealEvents = !self.jsController.sendingRealEvents;
+    self.jsController.sendingRealEvents = sendRealEvents;
+    activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
+    activeMenuItem.state = sendRealEvents;
 }
 
-- (void)setActive:(BOOL)newActive {
-       [activeButton setLabel:newActive ? @"Stop" : @"Start"];
-    NSImage *buttonImage = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
-       [activeButton setImage:buttonImage];
-       [activeMenuItem setState:newActive];
-       active = newActive;
+- (NSInteger)firstConfigMenuIndex {
+    for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
+        if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
+            return i + 1;
+    return dockMenuBase.numberOfItems;
 }
 
-- (IBAction)toggleActivity:(id)sender {
-    self.active = !self.active;
+- (void)configsChanged {
+    NSInteger removeFrom = [self firstConfigMenuIndex];
+    while (dockMenuBase.numberOfItems > removeFrom)
+        [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
+    int added = 0;
+    for (Config *config in self.configsController.configs) {
+        NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
+        [dockMenuBase addItemWithTitle:config.name
+                                action:@selector(chooseConfig:)
+                         keyEquivalent:keyEquiv];
+        
+    }
+    [_targetController refreshConfigs];
+    [self configChanged];
 }
 
--(void) configsChanged {
-       while([dockMenuBase numberOfItems] > 2)
-               [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)];
-
-       for(Config* config in [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 {
+    NSInteger 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 {
+    NSInteger idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
+    Config *chosen = self.configsController.configs[idx];
+    [_configsController activateConfig:chosen];
 }
 @end