Big rename part 2: 'config' etc. to 'mapping.
[enjoyable.git] / ApplicationController.m
index 2edac94..50f9c29 100644 (file)
 //
 //  Created by Sam McCall on 4/05/09.
 //
-#include <Carbon/Carbon.h>
 
-@implementation ApplicationController
+#import "ApplicationController.h"
 
-@synthesize jsController, targetController, configsController;
+#import "NJMapping.h"
+#import "NJMappingsController.h"
+#import "NJInputController.h"
+#import "TargetController.h"
+#import "NJEvents.h"
 
-static BOOL active;
-
-pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData);
-
--(void) applicationDidFinishLaunching: (NSNotification*) notification {
-       [jsController setup];
-       [drawer open];
-       [targetController setEnabled: false];
-       [self setActive: NO];
-       [configsController load];
-       EventTypeSpec et;
-       et.eventClass = kEventClassApplication;
-       et.eventKind = kEventAppFrontSwitched;
-       EventHandlerUPP handler = NewEventHandlerUPP(appSwitch);
-       InstallApplicationEventHandler(handler, 1, &et, (void *)CFBridgingRetain(self), NULL);
+@implementation ApplicationController {
+    BOOL active;
 }
 
--(void) applicationWillTerminate: (NSNotification *)aNotification {
-       [configsController save];
+- (void)didSwitchApplication:(NSNotification *)notification {
+    NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
+    [self.mappingsController activateMappingForProcess:currentApp.localizedName];
 }
 
-- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
-                                        hasVisibleWindows:(BOOL)flag
-{      
-       [mainWindow makeKeyAndOrderFront:self];
-       return YES;
+- (void)applicationDidFinishLaunching:(NSNotification *)notification {
+    [drawer open];
+    self.targetController.enabled = NO;
+    [self.inputController setup];
+    [self.mappingsController load];
+    [NSNotificationCenter.defaultCenter
+     addObserver:self
+     selector:@selector(mappingDidChange:)
+     name:NJEventMappingChanged
+     object:nil];
+    [NSNotificationCenter.defaultCenter
+     addObserver:self
+     selector:@selector(eventTranslationActivated:)
+     name:NJEventTranslationActivated
+     object:nil];
+    [NSNotificationCenter.defaultCenter
+     addObserver:self
+     selector:@selector(eventTranslationDeactivated:)
+     name:NJEventTranslationDeactivated
+     object:nil];
 }
 
-pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData) {
-       ApplicationController* self = (__bridge ApplicationController*)userData;
-       NSDictionary* currentApp = [[NSWorkspace sharedWorkspace] activeApplication];
-       ProcessSerialNumber psn;
-       psn.lowLongOfPSN = [currentApp[@"NSApplicationProcessSerialNumberLow"] longValue];
-       psn.highLongOfPSN = [currentApp[@"NSApplicationProcessSerialNumberHigh"] longValue];
-       [self->configsController applicationSwitchedTo: currentApp[@"NSApplicationName"] withPsn: psn];
-       return noErr;
+- (void)applicationWillTerminate:(NSNotification *)aNotification {
+       [NSUserDefaults.standardUserDefaults synchronize];
 }
 
--(BOOL) active {
-       return active;
+- (void)eventTranslationActivated:(NSNotification *)note {
+    activeButton.image = [NSImage imageNamed:@"NSStopProgressFreestandingTemplate"];
+    activeMenuItem.state = [note.object translatingEvents];
+    [NSWorkspace.sharedWorkspace.notificationCenter
+     addObserver:self
+     selector:@selector(didSwitchApplication:)
+     name:NSWorkspaceDidActivateApplicationNotification
+     object:nil];
+    NSLog(@"Listening for application changes.");
 }
 
--(void) setActive: (BOOL) newActive {
-       [activeButton setLabel: (newActive ? @"Stop" : @"Start")];
-       [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]];
-       [activeMenuItem setState: (newActive ? 1 : 0)];
-       active = newActive;
+- (void)eventTranslationDeactivated:(NSNotification *)note {
+    activeButton.image = [NSImage imageNamed:@"NSGoRightTemplate"];
+    activeMenuItem.state = [note.object translatingEvents];
+    [NSWorkspace.sharedWorkspace.notificationCenter
+     removeObserver:self
+     name:NSWorkspaceDidActivateApplicationNotification
+     object:nil];
+    NSLog(@"Ignoring application changes.");
 }
 
--(IBAction) toggleActivity: (id)sender {
-       [self setActive: ![self active]];
+- (IBAction)toggleActivity:(id)sender {
+    self.inputController.translatingEvents = !self.inputController.translatingEvents;
 }
 
--(void) configsChanged {
-       while([dockMenuBase numberOfItems] > 2)
-               [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)];
+- (NSInteger)firstMappingMenuIndex {
+    for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i)
+        if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
+            return i + 1;
+    return dockMenuBase.numberOfItems;
+}
 
-       for(Config* config in [configsController configs]) {
-               [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""];
-       }
-       [self configChanged];
+- (void)mappingsChanged {
+    NSInteger removeFrom = self.firstMappingMenuIndex;
+    while (dockMenuBase.numberOfItems > removeFrom)
+        [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
+    int added = 0;
+    for (NJMapping *mapping in self.mappingsController.mappings) {
+        NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
+        [dockMenuBase addItemWithTitle:mapping.name
+                                action:@selector(chooseMapping:)
+                         keyEquivalent:keyEquiv];
+        
+    }
+    [_targetController refreshMappings];
 }
--(void) configChanged {
-       Config* current = [configsController currentConfig];
-       NSArray* configs = [configsController configs];
-       for(int i=0; i<[configs count]; i++)
-               [[dockMenuBase itemAtIndex: (2+i)] setState: ((configs[i] == current) ? YES : NO)];
+
+- (void)mappingDidChange:(NSNotification *)note {
+    NSInteger firstMapping = self.firstMappingMenuIndex;
+    NJMapping *current = note.object;
+    NSArray *mappings = self.mappingsController.mappings;
+    for (NSUInteger i = 0; i < mappings.count; ++i)
+        [dockMenuBase itemAtIndex:i + firstMapping].state = mappings[i] == current;
 }
 
--(void) chooseConfig: (id) sender {
-       [configsController activateConfig: [configsController configs][([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL];
+- (void)chooseMapping:(id)sender {
+    NSInteger idx = [dockMenuBase indexOfItem:sender] - self.firstMappingMenuIndex;
+    NJMapping *chosen = self.mappingsController.mappings[idx];
+    [_mappingsController activateMapping:chosen];
 }
 @end