Improve website.
[enjoyable.git] / Classes / 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 *activeApp = note.userInfo[NSWorkspaceApplicationKey];
20 NSString *name = activeApp.localizedName;
21 if (!name)
22 name = activeApp.bundleIdentifier;
23 if (name && ![name isEqualToString:NSRunningApplication.currentApplication.localizedName])
24 [self.mappingsController activateMappingForProcess:name];
25 }
26
27 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
28 [NSNotificationCenter.defaultCenter
29 addObserver:self
30 selector:@selector(mappingDidChange:)
31 name:NJEventMappingChanged
32 object:nil];
33 [NSNotificationCenter.defaultCenter
34 addObserver:self
35 selector:@selector(mappingListDidChange:)
36 name:NJEventMappingListChanged
37 object:nil];
38 [NSNotificationCenter.defaultCenter
39 addObserver:self
40 selector:@selector(eventTranslationActivated:)
41 name:NJEventTranslationActivated
42 object:nil];
43 [NSNotificationCenter.defaultCenter
44 addObserver:self
45 selector:@selector(eventTranslationDeactivated:)
46 name:NJEventTranslationDeactivated
47 object:nil];
48
49 [self.inputController setup];
50 [self.mappingsController load];
51 }
52
53 - (void)applicationDidBecomeActive:(NSNotification *)notification {
54 [window makeKeyAndOrderFront:nil];
55 }
56
57 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
58 hasVisibleWindows:(BOOL)flag {
59 [window makeKeyAndOrderFront:nil];
60 return NO;
61 }
62
63 - (void)eventTranslationActivated:(NSNotification *)note {
64 [NSProcessInfo.processInfo disableAutomaticTermination:@"Input translation is active."];
65 [NSWorkspace.sharedWorkspace.notificationCenter
66 addObserver:self
67 selector:@selector(didSwitchApplication:)
68 name:NSWorkspaceDidActivateApplicationNotification
69 object:nil];
70 NSLog(@"Listening for application changes.");
71 }
72
73 - (void)eventTranslationDeactivated:(NSNotification *)note {
74 [NSProcessInfo.processInfo enableAutomaticTermination:@"Input translation is active."];
75 [NSWorkspace.sharedWorkspace.notificationCenter
76 removeObserver:self
77 name:NSWorkspaceDidActivateApplicationNotification
78 object:nil];
79 NSLog(@"Ignoring application changes.");
80 }
81
82 - (void)mappingListDidChange:(NSNotification *)note {
83 NSArray *mappings = note.object;
84 while (dockMenuBase.lastItem.representedObject)
85 [dockMenuBase removeLastItem];
86 int added = 0;
87 for (NJMapping *mapping in mappings) {
88 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
89 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
90 action:@selector(chooseMapping:)
91 keyEquivalent:keyEquiv];
92 item.representedObject = mapping;
93 item.state = mapping == self.mappingsController.currentMapping;
94 [dockMenuBase addItem:item];
95 }
96 }
97
98 - (void)mappingDidChange:(NSNotification *)note {
99 NJMapping *current = note.object;
100 for (NSMenuItem *item in dockMenuBase.itemArray)
101 if (item.representedObject)
102 item.state = item.representedObject == current;
103 }
104
105 - (void)chooseMapping:(NSMenuItem *)sender {
106 NJMapping *chosen = sender.representedObject;
107 [self.mappingsController activateMapping:chosen];
108 }
109
110 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
111 NSMenu *menu = [[NSMenu alloc] init];
112 int added = 0;
113 for (NJMapping *mapping in self.mappingsController) {
114 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
115 NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
116 action:@selector(chooseMapping:)
117 keyEquivalent:keyEquiv];
118 item.representedObject = mapping;
119 item.state = mapping == self.mappingsController.currentMapping;
120 [menu addItem:item];
121 }
122 return menu;
123 }
124
125 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
126 NSURL *url = [NSURL fileURLWithPath:filename];
127 [self.mappingsController addMappingWithContentsOfURL:url];
128 return YES;
129 }
130
131
132 @end