2 // EnjoyableApplicationDelegate.m
5 // Created by Sam McCall on 4/05/09.
8 #import "EnjoyableApplicationDelegate.h"
11 #import "NJMappingsController.h"
12 #import "NJDeviceController.h"
13 #import "NJOutputController.h"
16 @implementation EnjoyableApplicationDelegate
18 - (void)didSwitchApplication:(NSNotification *)note {
19 NSRunningApplication *currentApp = note.userInfo[NSWorkspaceApplicationKey];
20 [self.mappingsController activateMappingForProcess:currentApp.localizedName];
23 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
24 [NSNotificationCenter.defaultCenter
26 selector:@selector(mappingDidChange:)
27 name:NJEventMappingChanged
29 [NSNotificationCenter.defaultCenter
31 selector:@selector(mappingListDidChange:)
32 name:NJEventMappingListChanged
34 [NSNotificationCenter.defaultCenter
36 selector:@selector(eventTranslationActivated:)
37 name:NJEventTranslationActivated
39 [NSNotificationCenter.defaultCenter
41 selector:@selector(eventTranslationDeactivated:)
42 name:NJEventTranslationDeactivated
45 [self.inputController setup];
46 [self.mappingsController load];
49 - (void)applicationDidBecomeActive:(NSNotification *)notification {
50 [window makeKeyAndOrderFront:nil];
53 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
54 hasVisibleWindows:(BOOL)flag {
55 [window makeKeyAndOrderFront:nil];
59 - (void)eventTranslationActivated:(NSNotification *)note {
60 [NSProcessInfo.processInfo disableAutomaticTermination:@"Input translation is active."];
61 [NSWorkspace.sharedWorkspace.notificationCenter
63 selector:@selector(didSwitchApplication:)
64 name:NSWorkspaceDidActivateApplicationNotification
66 NSLog(@"Listening for application changes.");
69 - (void)eventTranslationDeactivated:(NSNotification *)note {
70 [NSProcessInfo.processInfo enableAutomaticTermination:@"Input translation is active."];
71 [NSWorkspace.sharedWorkspace.notificationCenter
73 name:NSWorkspaceDidActivateApplicationNotification
75 NSLog(@"Ignoring application changes.");
78 - (void)mappingListDidChange:(NSNotification *)note {
79 NSArray *mappings = note.object;
80 while (dockMenuBase.lastItem.representedObject)
81 [dockMenuBase removeLastItem];
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];
94 - (void)mappingDidChange:(NSNotification *)note {
95 NJMapping *current = note.object;
96 for (NSMenuItem *item in dockMenuBase.itemArray)
97 if (item.representedObject)
98 item.state = item.representedObject == current;
101 - (void)chooseMapping:(NSMenuItem *)sender {
102 NJMapping *chosen = sender.representedObject;
103 [self.mappingsController activateMapping:chosen];