388103b38e2a8b867a9640a629bbbe1608960d27
[enjoyable.git] / EnjoyableApplicationDelegate.m
1 //
2 // EnjoyableApplicationDelegate.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "EnjoyableApplicationDelegate.h"
9
10 #import "NJMapping.h"
11 #import "NJMappingsController.h"
12 #import "NJDeviceController.h"
13 #import "NJOutputController.h"
14 #import "NJEvents.h"
15
16 @implementation EnjoyableApplicationDelegate
17
18 - (void)didSwitchApplication:(NSNotification *)notification {
19 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
20 [self.mappingsController activateMappingForProcess:currentApp.localizedName];
21 }
22
23 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
24 [NSNotificationCenter.defaultCenter
25 addObserver:self
26 selector:@selector(mappingDidChange:)
27 name:NJEventMappingChanged
28 object:nil];
29 [NSNotificationCenter.defaultCenter
30 addObserver:self
31 selector:@selector(mappingListDidChange:)
32 name:NJEventMappingListChanged
33 object:nil];
34 [NSNotificationCenter.defaultCenter
35 addObserver:self
36 selector:@selector(eventTranslationActivated:)
37 name:NJEventTranslationActivated
38 object:nil];
39 [NSNotificationCenter.defaultCenter
40 addObserver:self
41 selector:@selector(eventTranslationDeactivated:)
42 name:NJEventTranslationDeactivated
43 object:nil];
44
45 [self.inputController setup];
46 [self.mappingsController load];
47 }
48
49 - (void)applicationWillTerminate:(NSNotification *)aNotification {
50 [NSUserDefaults.standardUserDefaults synchronize];
51 [NSNotificationCenter.defaultCenter removeObserver:self];
52 }
53
54 - (void)eventTranslationActivated:(NSNotification *)note {
55 [NSWorkspace.sharedWorkspace.notificationCenter
56 addObserver:self
57 selector:@selector(didSwitchApplication:)
58 name:NSWorkspaceDidActivateApplicationNotification
59 object:nil];
60 NSLog(@"Listening for application changes.");
61 }
62
63 - (void)eventTranslationDeactivated:(NSNotification *)note {
64 [NSWorkspace.sharedWorkspace.notificationCenter
65 removeObserver:self
66 name:NSWorkspaceDidActivateApplicationNotification
67 object:nil];
68 NSLog(@"Ignoring application changes.");
69 }
70
71 - (void)mappingListDidChange:(NSNotification *)note {
72 NSArray *mappings = note.object;
73 while (dockMenuBase.lastItem.representedObject)
74 [dockMenuBase removeLastItem];
75 int added = 0;
76 for (NJMapping *mapping in mappings) {
77 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
78 NSMenuItem *item = [dockMenuBase addItemWithTitle:mapping.name
79 action:@selector(chooseMapping:)
80 keyEquivalent:keyEquiv];
81 item.representedObject = mapping;
82 }
83 [_outputController refreshMappings];
84 }
85
86 - (void)mappingDidChange:(NSNotification *)note {
87 NJMapping *current = note.object;
88 for (NSMenuItem *item in dockMenuBase.itemArray)
89 if (item.representedObject)
90 item.state = item.representedObject == current;
91 }
92
93 - (void)chooseMapping:(NSMenuItem *)sender {
94 NJMapping *chosen = sender.representedObject;
95 [self.mappingsController activateMapping:chosen];
96 }
97
98 @end