313ba9a2fbf65df4a82e89369a7ceade015b0204
[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 *)note {
19 NSRunningApplication *currentApp = note.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)applicationDidBecomeActive:(NSNotification *)notification {
50 [window makeKeyAndOrderFront:nil];
51 }
52
53 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
54 hasVisibleWindows:(BOOL)flag {
55 [window makeKeyAndOrderFront:nil];
56 return NO;
57 }
58
59 - (void)eventTranslationActivated:(NSNotification *)note {
60 [NSProcessInfo.processInfo disableAutomaticTermination:@"Input translation is active."];
61 [NSWorkspace.sharedWorkspace.notificationCenter
62 addObserver:self
63 selector:@selector(didSwitchApplication:)
64 name:NSWorkspaceDidActivateApplicationNotification
65 object:nil];
66 NSLog(@"Listening for application changes.");
67 }
68
69 - (void)eventTranslationDeactivated:(NSNotification *)note {
70 [NSProcessInfo.processInfo enableAutomaticTermination:@"Input translation is active."];
71 [NSWorkspace.sharedWorkspace.notificationCenter
72 removeObserver:self
73 name:NSWorkspaceDidActivateApplicationNotification
74 object:nil];
75 NSLog(@"Ignoring application changes.");
76 }
77
78 - (void)mappingListDidChange:(NSNotification *)note {
79 NSArray *mappings = note.object;
80 while (dockMenuBase.lastItem.representedObject)
81 [dockMenuBase removeLastItem];
82 int added = 0;
83 for (NJMapping *mapping in mappings) {
84 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
85 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
86 action:@selector(chooseMapping:)
87 keyEquivalent:keyEquiv];
88 item.representedObject = mapping;
89 item.state = mapping == self.mappingsController.currentMapping;
90 [dockMenuBase addItem:item];
91 }
92 [_outputController refreshMappings];
93 }
94
95 - (void)mappingDidChange:(NSNotification *)note {
96 NJMapping *current = note.object;
97 for (NSMenuItem *item in dockMenuBase.itemArray)
98 if (item.representedObject)
99 item.state = item.representedObject == current;
100 }
101
102 - (void)chooseMapping:(NSMenuItem *)sender {
103 NJMapping *chosen = sender.representedObject;
104 [self.mappingsController activateMapping:chosen];
105 }
106
107 @end