Post active mapping changes through notification center rather than to the applicatio...
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 2 Mar 2013 20:53:01 +0000 (21:53 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Sat, 2 Mar 2013 20:53:01 +0000 (21:53 +0100)
ApplicationController.h
ApplicationController.m
ConfigsController.m
Enjoyable.xcodeproj/project.pbxproj
NJEvents.h [new file with mode: 0644]

index a5dcb6a..dc513ae 100644 (file)
@@ -24,6 +24,5 @@
 
 - (IBAction)toggleActivity:(id)sender;
 - (void)configsChanged;
-- (void)configChanged;
 
 @end
index 9a2db2a..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];
 }
 
 - (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 {
         
     }
     [_targetController refreshConfigs];
-    [self configChanged];
 }
 
-- (void)configChanged {
+- (void)mappingDidChange:(NSNotification *)note {
     NSInteger firstConfig = self.firstConfigMenuIndex;
-    Config *current = self.configsController.currentConfig;
+    Config *current = note.object;
     NSArray *configs = self.configsController.configs;
     for (NSUInteger i = 0; i < configs.count; ++i)
         [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
index d38ada8..0c32b8b 100644 (file)
@@ -12,6 +12,7 @@
 #import "ConfigsController.h"
 #import "Target.h"
 #import "TargetController.h"
+#import "NJEvents.h"
 
 @implementation ConfigsController {
     NSMutableArray *_configs;
 
 - (void)activateConfigForProcess:(NSString *)processName {
     Config *oldConfig = manualConfig;
-    [self activateConfig:self[processName]];
+    Config *newConfig = self[processName];
+    if (!newConfig)
+        newConfig = oldConfig;
+    if (newConfig != _currentConfig)
+        [self activateConfig:newConfig];
     manualConfig = oldConfig;
 }
 
 - (void)activateConfig:(Config *)config {
     if (!config)
         config = manualConfig;
-    if (_currentConfig == config)
-        return;
+    NSLog(@"Switching to mapping %@.", config.name);
     manualConfig = config;
     _currentConfig = config;
     [removeButton setEnabled:_configs[0] != config];
     [targetController loadCurrent];
-    [(ApplicationController *)NSApplication.sharedApplication.delegate configChanged];
+    [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
+                                                      object:_currentConfig];
     [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[_configs indexOfObject:config]] byExtendingSelection:NO];
 }
 
@@ -61,6 +66,7 @@
     [tableView reloadData];
     [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
     [tableView editColumn:0 row:_configs.count - 1 withEvent:nil select:YES];
+    [self activateConfig:newConfig];
 }
 
 - (IBAction)removePressed:(id)sender {
 }
 
 - (void)save {
-    NSLog(@"Saving defaults.");
+    NSLog(@"Saving mappings to defaults.");
     [NSUserDefaults.standardUserDefaults setObject:[self dumpAll] forKey:@"configurations"];
 }
 
index c46eb77..f07f6d7 100644 (file)
@@ -90,6 +90,7 @@
                EE1D7C9416E0ECCF00B000EB /* NSError+Description.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+Description.h"; sourceTree = "<group>"; };
                EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+Description.m"; sourceTree = "<group>"; };
                EEF86B7316E2241000674B87 /* NJActionPathElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NJActionPathElement.h; sourceTree = "<group>"; };
+               EEF86B7416E298CD00674B87 /* NJEvents.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NJEvents.h; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
                                8BEFAD9E15C476DC00823AEC /* TargetToggleMouseScope.h */,
                                8BEFAD9F15C476DC00823AEC /* TargetToggleMouseScope.m */,
                                EEF86B7316E2241000674B87 /* NJActionPathElement.h */,
+                               EEF86B7416E298CD00674B87 /* NJEvents.h */,
                        );
                        name = Classes;
                        sourceTree = "<group>";
                                CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES;
                                CLANG_WARN_OBJC_RECEIVER_WEAK = NO;
                                CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES;
-                               CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+                               CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
                                GCC_C_LANGUAGE_STANDARD = c99;
                                GCC_OPTIMIZATION_LEVEL = 0;
                                GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
                                GCC_WARN_PEDANTIC = YES;
                                GCC_WARN_SHADOW = YES;
                                GCC_WARN_SIGN_COMPARE = YES;
-                               GCC_WARN_STRICT_SELECTOR_MATCH = YES;
+                               GCC_WARN_STRICT_SELECTOR_MATCH = NO;
                                GCC_WARN_UNDECLARED_SELECTOR = YES;
                                GCC_WARN_UNINITIALIZED_AUTOS = YES;
                                GCC_WARN_UNUSED_FUNCTION = YES;
                                CLANG_WARN_OBJC_IMPLICIT_ATOMIC_PROPERTIES = YES;
                                CLANG_WARN_OBJC_RECEIVER_WEAK = NO;
                                CLANG_WARN_SUSPICIOUS_IMPLICIT_CONVERSION = YES;
-                               CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
+                               CLANG_WARN__DUPLICATE_METHOD_MATCH = NO;
                                GCC_C_LANGUAGE_STANDARD = c99;
                                GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
                                GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;
                                GCC_WARN_PEDANTIC = YES;
                                GCC_WARN_SHADOW = YES;
                                GCC_WARN_SIGN_COMPARE = YES;
-                               GCC_WARN_STRICT_SELECTOR_MATCH = YES;
+                               GCC_WARN_STRICT_SELECTOR_MATCH = NO;
                                GCC_WARN_UNDECLARED_SELECTOR = YES;
                                GCC_WARN_UNINITIALIZED_AUTOS = YES;
                                GCC_WARN_UNUSED_FUNCTION = YES;
diff --git a/NJEvents.h b/NJEvents.h
new file mode 100644 (file)
index 0000000..153a69a
--- /dev/null
@@ -0,0 +1,9 @@
+//
+//  NJEvents.h
+//  Enjoyable
+//
+//  Created by Joe Wreschnig on 3/2/13.
+//
+//
+
+#define NJEventMappingChanged @"NJEventMappingChanged"