Support sudden and automatic termination.
[enjoyable.git] / EnjoyableApplicationDelegate.m
index 23324d3..313ba9a 100644 (file)
 #import "NJOutputController.h"
 #import "NJEvents.h"
 
-@implementation EnjoyableApplicationDelegate {
-    NSInteger mappingsMenuIndex;
-}
+@implementation EnjoyableApplicationDelegate
 
-- (void)didSwitchApplication:(NSNotification *)notification {
-    NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
+- (void)didSwitchApplication:(NSNotification *)note {
+    NSRunningApplication *currentApp = note.userInfo[NSWorkspaceApplicationKey];
     [self.mappingsController activateMappingForProcess:currentApp.localizedName];
 }
 
         selector:@selector(mappingDidChange:)
         name:NJEventMappingChanged
         object:nil];
+    [NSNotificationCenter.defaultCenter
+        addObserver:self
+        selector:@selector(mappingListDidChange:)
+        name:NJEventMappingListChanged
+        object:nil];
     [NSNotificationCenter.defaultCenter
         addObserver:self
         selector:@selector(eventTranslationActivated:)
         name:NJEventTranslationDeactivated
         object:nil];
 
-    mappingsMenuIndex = dockMenuBase.numberOfItems;
-    while (![dockMenuBase itemAtIndex:mappingsMenuIndex - 1].isSeparatorItem)
-        --mappingsMenuIndex;
-    
-    [drawer open];
-    self.outputController.enabled = NO;
     [self.inputController setup];
     [self.mappingsController load];
 }
 
-- (void)applicationWillTerminate:(NSNotification *)aNotification {
-       [NSUserDefaults.standardUserDefaults synchronize];
+- (void)applicationDidBecomeActive:(NSNotification *)notification {
+    [window makeKeyAndOrderFront:nil];
+}
+
+- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
+                    hasVisibleWindows:(BOOL)flag {
+    [window makeKeyAndOrderFront:nil];
+    return NO;
 }
 
 - (void)eventTranslationActivated:(NSNotification *)note {
-    activeButton.image = [NSImage imageNamed:@"NSStopProgressFreestandingTemplate"];
-    activeMenuItem.state = [note.object translatingEvents];
+    [NSProcessInfo.processInfo disableAutomaticTermination:@"Input translation is active."];
     [NSWorkspace.sharedWorkspace.notificationCenter
-     addObserver:self
-     selector:@selector(didSwitchApplication:)
-     name:NSWorkspaceDidActivateApplicationNotification
-     object:nil];
+        addObserver:self
+        selector:@selector(didSwitchApplication:)
+        name:NSWorkspaceDidActivateApplicationNotification
+        object:nil];
     NSLog(@"Listening for application changes.");
 }
 
 - (void)eventTranslationDeactivated:(NSNotification *)note {
-    activeButton.image = [NSImage imageNamed:@"NSGoRightTemplate"];
-    activeMenuItem.state = [note.object translatingEvents];
+    [NSProcessInfo.processInfo enableAutomaticTermination:@"Input translation is active."];
     [NSWorkspace.sharedWorkspace.notificationCenter
-     removeObserver:self
-     name:NSWorkspaceDidActivateApplicationNotification
-     object:nil];
+        removeObserver:self
+        name:NSWorkspaceDidActivateApplicationNotification
+        object:nil];
     NSLog(@"Ignoring application changes.");
 }
 
-- (IBAction)toggleActivity:(id)sender {
-    self.inputController.translatingEvents = !self.inputController.translatingEvents;
-}
-
-- (void)mappingsChanged {
-    NSInteger removeFrom = mappingsMenuIndex;
-    while (dockMenuBase.numberOfItems > removeFrom)
-        [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
+- (void)mappingListDidChange:(NSNotification *)note {
+    NSArray *mappings = note.object;
+    while (dockMenuBase.lastItem.representedObject)
+        [dockMenuBase removeLastItem];
     int added = 0;
-    for (NJMapping *mapping in self.mappingsController.mappings) {
+    for (NJMapping *mapping in mappings) {
         NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
-        [dockMenuBase addItemWithTitle:mapping.name
-                                action:@selector(chooseMapping:)
-                         keyEquivalent:keyEquiv];
-        
+        NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name
+                                                      action:@selector(chooseMapping:)
+                                               keyEquivalent:keyEquiv];
+        item.representedObject = mapping;
+        item.state = mapping == self.mappingsController.currentMapping;
+        [dockMenuBase addItem:item];
     }
     [_outputController refreshMappings];
 }
 
 - (void)mappingDidChange:(NSNotification *)note {
     NJMapping *current = note.object;
-    NSArray *mappings = self.mappingsController.mappings;
-    for (NSUInteger i = 0; i < mappings.count; ++i)
-        [dockMenuBase itemAtIndex:i + mappingsMenuIndex].state = mappings[i] == current;
+    for (NSMenuItem *item in dockMenuBase.itemArray)
+        if (item.representedObject)
+            item.state = item.representedObject == current;
 }
 
-- (void)chooseMapping:(id)sender {
-    NSInteger idx = [dockMenuBase indexOfItem:sender] - mappingsMenuIndex;
-    NJMapping *chosen = self.mappingsController.mappings[idx];
-    [_mappingsController activateMapping:chosen];
+- (void)chooseMapping:(NSMenuItem *)sender {
+    NJMapping *chosen = sender.representedObject;
+    [self.mappingsController activateMapping:chosen];
 }
 
 @end