Remove unused imports.
[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 "NJEvents.h"
13
14 @implementation EnjoyableApplicationDelegate {
15 NSStatusItem *statusItem;
16 }
17
18 - (void)didSwitchApplication:(NSNotification *)note {
19 NSRunningApplication *activeApp = note.userInfo[NSWorkspaceApplicationKey];
20 if (activeApp)
21 [self.mappingsController activateMappingForProcess:activeApp];
22 }
23
24 - (void)applicationWillFinishLaunching:(NSNotification *)notification {
25 [NSNotificationCenter.defaultCenter
26 addObserver:self
27 selector:@selector(mappingDidChange:)
28 name:NJEventMappingChanged
29 object:nil];
30 [NSNotificationCenter.defaultCenter
31 addObserver:self
32 selector:@selector(eventTranslationActivated:)
33 name:NJEventTranslationActivated
34 object:nil];
35 [NSNotificationCenter.defaultCenter
36 addObserver:self
37 selector:@selector(eventTranslationDeactivated:)
38 name:NJEventTranslationDeactivated
39 object:nil];
40
41 [self.mappingsController load];
42
43 statusItem = [NSStatusBar.systemStatusBar statusItemWithLength:36];
44 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
45 statusItem.highlightMode = YES;
46 statusItem.menu = statusItemMenu;
47 statusItem.target = self;
48 }
49
50 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
51 [window makeKeyAndOrderFront:nil];
52 }
53
54 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
55 hasVisibleWindows:(BOOL)flag {
56 [self restoreToForeground:theApplication];
57 return NO;
58 }
59
60 - (void)restoreToForeground:(id)sender {
61 ProcessSerialNumber psn = { 0, kCurrentProcess };
62 TransformProcessType(&psn, kProcessTransformToForegroundApplication);
63 [NSApplication.sharedApplication activateIgnoringOtherApps:YES];
64 [window makeKeyAndOrderFront:sender];
65 [NSObject cancelPreviousPerformRequestsWithTarget:self
66 selector:@selector(transformIntoElement:)
67 object:self];
68 }
69
70 - (void)transformIntoElement:(id)sender {
71 ProcessSerialNumber psn = { 0, kCurrentProcess };
72 TransformProcessType(&psn, kProcessTransformToUIElementApplication);
73 }
74
75 - (void)flashStatusItem {
76 if ([statusItem.image.name isEqualToString:@"Status Menu Icon"]) {
77 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
78 } else {
79 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
80 }
81
82 }
83
84 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication {
85 [theApplication hide:theApplication];
86 // If we turn into a UIElement right away, the application cancels
87 // the deactivation events. The dock icon disappears, but an
88 // unresponsive menu bar remains until the user clicks somewhere.
89 // So delay just long enough to be past the end handling that.
90 [self performSelector:@selector(transformIntoElement:) withObject:self afterDelay:0.001];
91 return NO;
92 }
93
94 - (void)eventTranslationActivated:(NSNotification *)note {
95 statusItem.image = [NSImage imageNamed:@"Status Menu Icon"];
96 [NSWorkspace.sharedWorkspace.notificationCenter
97 addObserver:self
98 selector:@selector(didSwitchApplication:)
99 name:NSWorkspaceDidActivateApplicationNotification
100 object:nil];
101 }
102
103 - (void)eventTranslationDeactivated:(NSNotification *)note {
104 statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"];
105 [NSWorkspace.sharedWorkspace.notificationCenter
106 removeObserver:self
107 name:NSWorkspaceDidActivateApplicationNotification
108 object:nil];
109 }
110
111 - (void)mappingDidChange:(NSNotification *)note {
112 if (!window.isVisible)
113 for (int i = 0; i < 4; ++i)
114 [self performSelector:@selector(flashStatusItem)
115 withObject:self
116 afterDelay:0.2 * i];
117 }
118
119 - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
120 return dockMenu;
121 }
122
123 - (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
124 [self restoreToForeground:sender];
125 NSURL *url = [NSURL fileURLWithPath:filename];
126 [self.mappingsController addMappingWithContentsOfURL:url];
127 return YES;
128 }
129
130 - (void)mappingWasChosen:(NJMapping *)mapping {
131 [self.mappingsController activateMapping:mapping];
132 }
133
134 - (void)mappingListShouldOpen {
135 [self restoreToForeground:self];
136 [self.mappingsController mappingPressed:self];
137 }
138
139
140 @end