Publish activated/deactivated as notifications.
[enjoyable.git] / ApplicationController.m
index a3ad5ed..1acc692 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
+    [NSNotificationCenter.defaultCenter
+     addObserver:self
+     selector:@selector(mappingDidChange:)
+     name:NJEventMappingChanged
+     object:nil];
+    [NSNotificationCenter.defaultCenter
+     addObserver:self
+     selector:@selector(eventTranslationActivated:)
+     name:NJEventTranslationActivated
+     object:nil];
+    [NSNotificationCenter.defaultCenter
+     addObserver:self
+     selector:@selector(eventTranslationDeactivated:)
+     name:NJEventTranslationDeactivated
+     object:nil];
+}
+
+- (void)applicationWillTerminate:(NSNotification *)aNotification {
+       [NSUserDefaults.standardUserDefaults synchronize];
+}
+
+- (void)eventTranslationActivated:(NSNotification *)note {
+    activeButton.image = [NSImage imageNamed:@"NSStopProgressFreestandingTemplate"];
+    activeMenuItem.state = [note.object translatingEvents];
+    [NSWorkspace.sharedWorkspace.notificationCenter
      addObserver:self
      selector:@selector(didSwitchApplication:)
      name:NSWorkspaceDidActivateApplicationNotification
      object:nil];
+    NSLog(@"Listening for application changes.");
 }
 
-- (void)applicationWillTerminate:(NSNotification *)aNotification {
-       [[NSUserDefaults standardUserDefaults] synchronize];
-    [[NSWorkspace sharedWorkspace].notificationCenter
+- (void)eventTranslationDeactivated:(NSNotification *)note {
+    activeButton.image = [NSImage imageNamed:@"NSGoRightTemplate"];
+    activeMenuItem.state = [note.object translatingEvents];
+    [NSWorkspace.sharedWorkspace.notificationCenter
      removeObserver:self
      name:NSWorkspaceDidActivateApplicationNotification
      object:nil];
+    NSLog(@"Ignoring application changes.");
 }
 
 - (IBAction)toggleActivity:(id)sender {
-    BOOL sendRealEvents = !self.jsController.sendingRealEvents;
-    self.jsController.sendingRealEvents = sendRealEvents;
-    activeButton.label = sendRealEvents ? @"Stop" : @"Start";
-    activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
-    activeMenuItem.state = sendRealEvents;
+    self.jsController.translatingEvents = !self.jsController.translatingEvents;
 }
 
 - (NSInteger)firstConfigMenuIndex {
-    NSInteger count = dockMenuBase.numberOfItems;
-    for (int i = 0; i < count; ++i)
+    for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
         if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
             return i + 1;
-    return count;
+    return dockMenuBase.numberOfItems;
 }
 
 - (void)configsChanged {
-    NSInteger removeFrom = [self firstConfigMenuIndex];
+    NSInteger 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:@""];
+    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)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];
 }