X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=ApplicationController.m;h=531d1f469a54aa57c31869ffc23587a10330610f;hp=445456526329370a5b8f3f2fc9f6b66e65848003;hb=f41f770d67370cbd71002515d81c0842b50cb04b;hpb=794561a4606fa9e31e3b2b077891f5b1e6084698 diff --git a/ApplicationController.m b/ApplicationController.m index 4454565..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]; + [NSUserDefaults.standardUserDefaults synchronize]; } - (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 { @@ -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; @@ -68,19 +81,18 @@ } [_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]; }