bc7c4689c3fb75d6eb942123a58f17f723400a7c
[enjoyable.git] / ApplicationController.m
1 //
2 // ApplicationController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "ApplicationController.h"
9
10 #import "Config.h"
11 #import "ConfigsController.h"
12 #import "NJInputController.h"
13 #import "TargetController.h"
14 #import "NJEvents.h"
15
16 @implementation ApplicationController {
17 BOOL active;
18 }
19
20 - (void)didSwitchApplication:(NSNotification *)notification {
21 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
22 [self.configsController activateConfigForProcess:currentApp.localizedName];
23 }
24
25 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
26 [drawer open];
27 self.targetController.enabled = NO;
28 [self.jsController setup];
29 [self.configsController load];
30 [NSNotificationCenter.defaultCenter
31 addObserver:self
32 selector:@selector(mappingDidChange:)
33 name:NJEventMappingChanged
34 object:nil];
35 [NSNotificationCenter.defaultCenter
36 addObserver:self
37 selector:@selector(eventTranslationActivated:)
38 name:NJEventTranslationActivated
39 object:nil];
40 [NSNotificationCenter.defaultCenter
41 addObserver:self
42 selector:@selector(eventTranslationDeactivated:)
43 name:NJEventTranslationDeactivated
44 object:nil];
45 }
46
47 - (void)applicationWillTerminate:(NSNotification *)aNotification {
48 [NSUserDefaults.standardUserDefaults synchronize];
49 }
50
51 - (void)eventTranslationActivated:(NSNotification *)note {
52 activeButton.image = [NSImage imageNamed:@"NSStopProgressFreestandingTemplate"];
53 activeMenuItem.state = [note.object translatingEvents];
54 [NSWorkspace.sharedWorkspace.notificationCenter
55 addObserver:self
56 selector:@selector(didSwitchApplication:)
57 name:NSWorkspaceDidActivateApplicationNotification
58 object:nil];
59 NSLog(@"Listening for application changes.");
60 }
61
62 - (void)eventTranslationDeactivated:(NSNotification *)note {
63 activeButton.image = [NSImage imageNamed:@"NSGoRightTemplate"];
64 activeMenuItem.state = [note.object translatingEvents];
65 [NSWorkspace.sharedWorkspace.notificationCenter
66 removeObserver:self
67 name:NSWorkspaceDidActivateApplicationNotification
68 object:nil];
69 NSLog(@"Ignoring application changes.");
70 }
71
72 - (IBAction)toggleActivity:(id)sender {
73 self.jsController.translatingEvents = !self.jsController.translatingEvents;
74 }
75
76 - (NSInteger)firstConfigMenuIndex {
77 for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
78 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
79 return i + 1;
80 return dockMenuBase.numberOfItems;
81 }
82
83 - (void)configsChanged {
84 NSInteger removeFrom = self.firstConfigMenuIndex;
85 while (dockMenuBase.numberOfItems > removeFrom)
86 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
87 int added = 0;
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];
93
94 }
95 [_targetController refreshConfigs];
96 }
97
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;
104 }
105
106 - (void)chooseConfig:(id)sender {
107 NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstConfigMenuIndex;
108 Config *chosen = self.configsController.configs[idx];
109 [_configsController activateConfig:chosen];
110 }
111 @end