X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=EnjoyableApplicationDelegate.m;h=c477511686b30a9db005603dd3fc0bdac24c6300;hp=8afbb5bd5ab16afccebf3ace687a544697ed9705;hb=b8c30e0c06effb8f4a937378e5c4cf8a22f40c59;hpb=1315dd378437c84891c795c9927ff40d42d74cb3 diff --git a/EnjoyableApplicationDelegate.m b/EnjoyableApplicationDelegate.m index 8afbb5b..c477511 100644 --- a/EnjoyableApplicationDelegate.m +++ b/EnjoyableApplicationDelegate.m @@ -13,13 +13,15 @@ #import "NJOutputController.h" #import "NJEvents.h" -@implementation EnjoyableApplicationDelegate { - NSInteger mappingsMenuIndex; -} +@implementation EnjoyableApplicationDelegate -- (void)didSwitchApplication:(NSNotification *)notification { - NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; - [self.mappingsController activateMappingForProcess:currentApp.localizedName]; +- (void)didSwitchApplication:(NSNotification *)note { + NSRunningApplication *activeApp = note.userInfo[NSWorkspaceApplicationKey]; + NSString *name = activeApp.localizedName; + if (!name) + name = activeApp.bundleIdentifier; + if (name && ![name isEqualToString:NSRunningApplication.currentApplication.localizedName]) + [self.mappingsController activateMappingForProcess:name]; } - (void)applicationDidFinishLaunching:(NSNotification *)notification { @@ -44,19 +46,22 @@ name:NJEventTranslationDeactivated object:nil]; - while (![dockMenuBase itemAtIndex:mappingsMenuIndex++].tag); - - self.outputController.enabled = NO; [self.inputController setup]; [self.mappingsController load]; } -- (void)applicationWillTerminate:(NSNotification *)aNotification { - [NSUserDefaults.standardUserDefaults synchronize]; - [NSNotificationCenter.defaultCenter removeObserver:self]; +- (void)applicationDidBecomeActive:(NSNotification *)notification { + [window makeKeyAndOrderFront:nil]; +} + +- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication + hasVisibleWindows:(BOOL)flag { + [window makeKeyAndOrderFront:nil]; + return NO; } - (void)eventTranslationActivated:(NSNotification *)note { + [NSProcessInfo.processInfo disableAutomaticTermination:@"Input translation is active."]; [NSWorkspace.sharedWorkspace.notificationCenter addObserver:self selector:@selector(didSwitchApplication:) @@ -66,6 +71,7 @@ } - (void)eventTranslationDeactivated:(NSNotification *)note { + [NSProcessInfo.processInfo enableAutomaticTermination:@"Input translation is active."]; [NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self name:NSWorkspaceDidActivateApplicationNotification @@ -75,31 +81,75 @@ - (void)mappingListDidChange:(NSNotification *)note { NSArray *mappings = note.object; - NSInteger removeFrom = mappingsMenuIndex; - while (dockMenuBase.numberOfItems > removeFrom) - [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1]; + while (dockMenuBase.lastItem.representedObject) + [dockMenuBase removeLastItem]; int added = 0; 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:(NSMenuItem *)sender { + NJMapping *chosen = sender.representedObject; + [self.mappingsController activateMapping:chosen]; +} + +#define OUTPUT_PANE_MIN_WIDTH 390 + +- (CGFloat)splitView:(NSSplitView *)sender constrainMaxCoordinate:(CGFloat)proposedMax ofSubviewAt:(NSInteger)offset { + return proposedMax - OUTPUT_PANE_MIN_WIDTH; } -- (void)chooseMapping:(id)sender { - NSInteger idx = [dockMenuBase indexOfItem:sender] - mappingsMenuIndex; - NJMapping *chosen = self.mappingsController[idx]; - [_mappingsController activateMapping:chosen]; +- (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize { + NSView *inputView = splitView.subviews[0]; + NSView *outputView = splitView.subviews[1]; + if (outputView.frame.size.width < OUTPUT_PANE_MIN_WIDTH) { + NSSize frameSize = splitView.frame.size; + CGFloat inputWidth = frameSize.width - OUTPUT_PANE_MIN_WIDTH - splitView.dividerThickness; + inputView.frame = NSMakeRect(inputWidth, frameSize.height, + inputView.frame.size.width, + inputView.frame.size.height); + outputView.frame = NSMakeRect(inputWidth + splitView.dividerThickness, + 0, + OUTPUT_PANE_MIN_WIDTH, + frameSize.height); + } else + [splitView adjustSubviews]; } +- (NSMenu *)applicationDockMenu:(NSApplication *)sender { + NSMenu *menu = [[NSMenu alloc] init]; + int added = 0; + for (NJMapping *mapping in self.mappingsController) { + NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @""; + NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name + action:@selector(chooseMapping:) + keyEquivalent:keyEquiv]; + item.representedObject = mapping; + item.state = mapping == self.mappingsController.currentMapping; + [menu addItem:item]; + } + return menu; +} + +- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename { + NSURL *url = [NSURL fileURLWithPath:filename]; + [self.mappingsController addMappingWithContentsOfURL:url]; + return YES; +} + + @end