From f41f770d67370cbd71002515d81c0842b50cb04b Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Sat, 2 Mar 2013 21:53:01 +0100 Subject: [PATCH] Post active mapping changes through notification center rather than to the application delegate directly. Don't listen for application switch notifications unless the mappings are active. --- ApplicationController.h | 1 - ApplicationController.m | 36 +++++++++++++++++++---------- ConfigsController.m | 16 +++++++++---- Enjoyable.xcodeproj/project.pbxproj | 10 ++++---- NJEvents.h | 9 ++++++++ 5 files changed, 50 insertions(+), 22 deletions(-) create mode 100644 NJEvents.h diff --git a/ApplicationController.h b/ApplicationController.h index a5dcb6a..dc513ae 100644 --- a/ApplicationController.h +++ b/ApplicationController.h @@ -24,6 +24,5 @@ - (IBAction)toggleActivity:(id)sender; - (void)configsChanged; -- (void)configChanged; @end diff --git a/ApplicationController.m b/ApplicationController.m index 9a2db2a..531d1f4 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -11,6 +11,7 @@ #import "ConfigsController.h" #import "JoystickController.h" #import "TargetController.h" +#import "NJEvents.h" @implementation ApplicationController { BOOL active; @@ -26,19 +27,15 @@ 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 { @@ -46,6 +43,22 @@ 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 { @@ -68,12 +81,11 @@ } [_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; diff --git a/ConfigsController.m b/ConfigsController.m index d38ada8..0c32b8b 100644 --- a/ConfigsController.m +++ b/ConfigsController.m @@ -12,6 +12,7 @@ #import "ConfigsController.h" #import "Target.h" #import "TargetController.h" +#import "NJEvents.h" @implementation ConfigsController { NSMutableArray *_configs; @@ -37,20 +38,24 @@ - (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 { @@ -98,7 +104,7 @@ } - (void)save { - NSLog(@"Saving defaults."); + NSLog(@"Saving mappings to defaults."); [NSUserDefaults.standardUserDefaults setObject:[self dumpAll] forKey:@"configurations"]; } diff --git a/Enjoyable.xcodeproj/project.pbxproj b/Enjoyable.xcodeproj/project.pbxproj index c46eb77..f07f6d7 100644 --- a/Enjoyable.xcodeproj/project.pbxproj +++ b/Enjoyable.xcodeproj/project.pbxproj @@ -90,6 +90,7 @@ EE1D7C9416E0ECCF00B000EB /* NSError+Description.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+Description.h"; sourceTree = ""; }; EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+Description.m"; sourceTree = ""; }; EEF86B7316E2241000674B87 /* NJActionPathElement.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NJActionPathElement.h; sourceTree = ""; }; + EEF86B7416E298CD00674B87 /* NJEvents.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = NJEvents.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -145,6 +146,7 @@ 8BEFAD9E15C476DC00823AEC /* TargetToggleMouseScope.h */, 8BEFAD9F15C476DC00823AEC /* TargetToggleMouseScope.m */, EEF86B7316E2241000674B87 /* NJActionPathElement.h */, + EEF86B7416E298CD00674B87 /* NJEvents.h */, ); name = Classes; sourceTree = ""; @@ -392,7 +394,7 @@ 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; @@ -407,7 +409,7 @@ 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; @@ -433,7 +435,7 @@ 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; @@ -447,7 +449,7 @@ 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 index 0000000..153a69a --- /dev/null +++ b/NJEvents.h @@ -0,0 +1,9 @@ +// +// NJEvents.h +// Enjoyable +// +// Created by Joe Wreschnig on 3/2/13. +// +// + +#define NJEventMappingChanged @"NJEventMappingChanged" -- 2.20.1