2 // ApplicationController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "ApplicationController.h"
11 #import "ConfigsController.h"
12 #import "NJInputController.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
35 [NSNotificationCenter.defaultCenter
37 selector:@selector(eventTranslationActivated:)
38 name:NJEventTranslationActivated
40 [NSNotificationCenter.defaultCenter
42 selector:@selector(eventTranslationDeactivated:)
43 name:NJEventTranslationDeactivated
47 - (void)applicationWillTerminate:(NSNotification *)aNotification {
48 [NSUserDefaults.standardUserDefaults synchronize];
51 - (void)eventTranslationActivated:(NSNotification *)note {
52 activeButton.image = [NSImage imageNamed:@"NSStopProgressFreestandingTemplate"];
53 activeMenuItem.state = [note.object translatingEvents];
54 [NSWorkspace.sharedWorkspace.notificationCenter
56 selector:@selector(didSwitchApplication:)
57 name:NSWorkspaceDidActivateApplicationNotification
59 NSLog(@"Listening for application changes.");
62 - (void)eventTranslationDeactivated:(NSNotification *)note {
63 activeButton.image = [NSImage imageNamed:@"NSGoRightTemplate"];
64 activeMenuItem.state = [note.object translatingEvents];
65 [NSWorkspace.sharedWorkspace.notificationCenter
67 name:NSWorkspaceDidActivateApplicationNotification
69 NSLog(@"Ignoring application changes.");
72 - (IBAction)toggleActivity:(id)sender {
73 self.jsController.translatingEvents = !self.jsController.translatingEvents;
76 - (NSInteger)firstConfigMenuIndex {
77 for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
78 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
80 return dockMenuBase.numberOfItems;
83 - (void)configsChanged {
84 NSInteger removeFrom = self.firstConfigMenuIndex;
85 while (dockMenuBase.numberOfItems > removeFrom)
86 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
88 for (Config *config in self.configsController.configs) {
89 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
90 [dockMenuBase addItemWithTitle:config.name
91 action:@selector(chooseConfig:)
92 keyEquivalent:keyEquiv];
95 [_targetController refreshConfigs];
98 - (void)mappingDidChange:(NSNotification *)note {
99 NSInteger firstConfig = self.firstConfigMenuIndex;
100 Config *current = note.object;
101 NSArray *configs = self.configsController.configs;
102 for (NSUInteger i = 0; i < configs.count; ++i)
103 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
106 - (void)chooseConfig:(id)sender {
107 NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstConfigMenuIndex;
108 Config *chosen = self.configsController.configs[idx];
109 [_configsController activateConfig:chosen];