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 *activeApp = note.userInfo[NSWorkspaceApplicationKey];
21 [self.mappingsController activateMappingForProcess:activeApp];
24 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
25 [NSNotificationCenter.defaultCenter
27 selector:@selector(mappingDidChange:)
28 name:NJEventMappingChanged
30 [NSNotificationCenter.defaultCenter
32 selector:@selector(mappingListDidChange:)
33 name:NJEventMappingListChanged
35 [NSNotificationCenter.defaultCenter
37 selector:@selector(eventTranslationActivated:)
38 name:NJEventTranslationActivated
40 [NSNotificationCenter.defaultCenter
42 selector:@selector(eventTranslationDeactivated:)
43 name:NJEventTranslationDeactivated
46 [self.inputController setup];
47 [self.mappingsController load];
50 - (void)applicationDidBecomeActive:(NSNotification *)notification {
51 [window makeKeyAndOrderFront:nil];
54 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
55 hasVisibleWindows:(BOOL)flag {
56 [window makeKeyAndOrderFront:nil];
60 - (void)eventTranslationActivated:(NSNotification *)note {
61 [NSProcessInfo.processInfo disableAutomaticTermination:@"Input translation is active."];
62 [NSWorkspace.sharedWorkspace.notificationCenter
64 selector:@selector(didSwitchApplication:)
65 name:NSWorkspaceDidActivateApplicationNotification
67 NSLog(@"Listening for application changes.");
70 - (void)eventTranslationDeactivated:(NSNotification *)note {
71 [NSProcessInfo.processInfo enableAutomaticTermination:@"Input translation is active."];
72 [NSWorkspace.sharedWorkspace.notificationCenter
74 name:NSWorkspaceDidActivateApplicationNotification
76 NSLog(@"Ignoring application changes.");
79 - (void)mappingListDidChange:(NSNotification *)note {
80 NSArray *mappings = note.object;
81 while (dockMenuBase.lastItem.representedObject)
82 [dockMenuBase removeLastItem];
84 for (NJMapping *mapping in mappings) {
85 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
86 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
87 action:@selector(chooseMapping:)
88 keyEquivalent:keyEquiv];
89 item.representedObject = mapping;
90 item.state = mapping == self.mappingsController.currentMapping;
91 [dockMenuBase addItem:item];
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;
102 - (void)chooseMapping:(NSMenuItem *)sender {
103 NJMapping *chosen = sender.representedObject;
104 [self.mappingsController activateMapping:chosen];
107 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
108 NSMenu *menu = [[NSMenu alloc] init];
110 for (NJMapping *mapping in self.mappingsController) {
111 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
112 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
113 action:@selector(chooseMapping:)
114 keyEquivalent:keyEquiv];
115 item.representedObject = mapping;
116 item.state = mapping == self.mappingsController.currentMapping;
122 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
123 NSURL *url = [NSURL fileURLWithPath:filename];
124 [self.mappingsController addMappingWithContentsOfURL:url];