Post active mapping changes through notification center rather than to the applicatio...
[enjoyable.git] / ApplicationController.m
index 4454565..531d1f4 100644 (file)
@@ -11,6 +11,7 @@
 #import "ConfigsController.h"
 #import "JoystickController.h"
 #import "TargetController.h"
+#import "NJEvents.h"
 
 @implementation ApplicationController {
     BOOL active;
     self.targetController.enabled = NO;
     [self.jsController setup];
     [self.configsController load];
-    [[NSWorkspace sharedWorkspace].notificationCenter
-     addObserver:self
-     selector:@selector(didSwitchApplication:)
-     name:NSWorkspaceDidActivateApplicationNotification
-     object:nil];
+    [NSNotificationCenter.defaultCenter
+        addObserver:self
+        selector:@selector(mappingDidChange:)
+        name:NJEventMappingChanged
+        object:nil];
 }
 
 - (void)applicationWillTerminate:(NSNotification *)aNotification {
-       [[NSUserDefaults standardUserDefaults] synchronize];
-    [[NSWorkspace sharedWorkspace].notificationCenter
-     removeObserver:self
-     name:NSWorkspaceDidActivateApplicationNotification
-     object:nil];
+       [NSUserDefaults.standardUserDefaults synchronize];
 }
 
 - (IBAction)toggleActivity:(id)sender {
     self.jsController.sendingRealEvents = sendRealEvents;
     activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
     activeMenuItem.state = sendRealEvents;
+    
+    if (sendRealEvents) {
+        [NSWorkspace.sharedWorkspace.notificationCenter
+            addObserver:self
+            selector:@selector(didSwitchApplication:)
+            name:NSWorkspaceDidActivateApplicationNotification
+            object:nil];
+        NSLog(@"Listening for application changes.");
+    } else {
+        [NSWorkspace.sharedWorkspace.notificationCenter
+            removeObserver:self
+            name:NSWorkspaceDidActivateApplicationNotification
+            object:nil];
+        NSLog(@"Ignoring application changes.");
+    }
+
 }
 
 - (NSInteger)firstConfigMenuIndex {
@@ -56,7 +69,7 @@
 }
 
 - (void)configsChanged {
-    NSInteger removeFrom = [self firstConfigMenuIndex];
+    NSInteger removeFrom = self.firstConfigMenuIndex;
     while (dockMenuBase.numberOfItems > removeFrom)
         [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
     int added = 0;
         
     }
     [_targetController refreshConfigs];
-    [self configChanged];
 }
 
-- (void)configChanged {
-    NSInteger firstConfig = [self firstConfigMenuIndex];
-    Config *current = self.configsController.currentConfig;
+- (void)mappingDidChange:(NSNotification *)note {
+    NSInteger firstConfig = self.firstConfigMenuIndex;
+    Config *current = note.object;
     NSArray *configs = self.configsController.configs;
-    for (int i = 0; i < configs.count; ++i)
+    for (NSUInteger i = 0; i < configs.count; ++i)
         [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
 }
 
 - (void)chooseConfig:(id)sender {
-    NSInteger idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
+    NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstConfigMenuIndex;
     Config *chosen = self.configsController.configs[idx];
     [_configsController activateConfig:chosen];
 }