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 a5dcb6a9e43fe7325ba6a37b56e1611852c07fee..dc513aee13fcf9ec5e02179bc16afeee0d2b0146 100644 (file)
@@ -24,6 +24,5 @@
 
 - (IBAction)toggleActivity:(id)sender;
 - (void)configsChanged;
-- (void)configChanged;
 
 @end
index 9a2db2af3c1d31a5034f7cae19c235516b8ae03c..531d1f469a54aa57c31869ffc23587a10330610f 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 d38ada85fed674d0dc2fe8e2c93bc3959f8a11c1..0c32b8b705f6b87fd186f606655c6cc22c67c128 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 c46eb774171d30f796946562ae56db2e73ab62e4..f07f6d71f22f720b192ba9ccd529e33646357b7e 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"