2 // ApplicationController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "ApplicationController.h"
11 #import "ConfigsController.h"
12 #import "JoystickController.h"
13 #import "TargetController.h"
16 @implementation ApplicationController {
20 - (void)didSwitchApplication:(NSNotification *)notification {
21 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
22 [self.configsController activateConfigForProcess:currentApp.localizedName];
25 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
27 self.targetController.enabled = NO;
28 [self.jsController setup];
29 [self.configsController load];
30 [NSNotificationCenter.defaultCenter
32 selector:@selector(mappingDidChange:)
33 name:NJEventMappingChanged
37 - (void)applicationWillTerminate:(NSNotification *)aNotification {
38 [NSUserDefaults.standardUserDefaults synchronize];
41 - (IBAction)toggleActivity:(id)sender {
42 BOOL sendRealEvents = !self.jsController.sendingRealEvents;
43 self.jsController.sendingRealEvents = sendRealEvents;
44 activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
45 activeMenuItem.state = sendRealEvents;
48 [NSWorkspace.sharedWorkspace.notificationCenter
50 selector:@selector(didSwitchApplication:)
51 name:NSWorkspaceDidActivateApplicationNotification
53 NSLog(@"Listening for application changes.");
55 [NSWorkspace.sharedWorkspace.notificationCenter
57 name:NSWorkspaceDidActivateApplicationNotification
59 NSLog(@"Ignoring application changes.");
64 - (NSInteger)firstConfigMenuIndex {
65 for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
66 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
68 return dockMenuBase.numberOfItems;
71 - (void)configsChanged {
72 NSInteger removeFrom = self.firstConfigMenuIndex;
73 while (dockMenuBase.numberOfItems > removeFrom)
74 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
76 for (Config *config in self.configsController.configs) {
77 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
78 [dockMenuBase addItemWithTitle:config.name
79 action:@selector(chooseConfig:)
80 keyEquivalent:keyEquiv];
83 [_targetController refreshConfigs];
86 - (void)mappingDidChange:(NSNotification *)note {
87 NSInteger firstConfig = self.firstConfigMenuIndex;
88 Config *current = note.object;
89 NSArray *configs = self.configsController.configs;
90 for (NSUInteger i = 0; i < configs.count; ++i)
91 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
94 - (void)chooseConfig:(id)sender {
95 NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstConfigMenuIndex;
96 Config *chosen = self.configsController.configs[idx];
97 [_configsController activateConfig:chosen];