X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=Classes%2FEnjoyableApplicationDelegate.m;h=f1513996c2d8687d8a3c47d9e97d60012f04a35c;hp=aaa9f00e5118ce373963795958b5445d20d7682b;hb=bda2aa3a8897f574b97c0bd125e84374087017a9;hpb=22efc1e5000d462091f3156ee5a8959be32cfb11 diff --git a/Classes/EnjoyableApplicationDelegate.m b/Classes/EnjoyableApplicationDelegate.m index aaa9f00..f151399 100644 --- a/Classes/EnjoyableApplicationDelegate.m +++ b/Classes/EnjoyableApplicationDelegate.m @@ -9,8 +9,6 @@ #import "NJMapping.h" #import "NJMappingsController.h" -#import "NJDeviceController.h" -#import "NJOutputController.h" #import "NJEvents.h" @implementation EnjoyableApplicationDelegate { @@ -29,11 +27,6 @@ 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:) @@ -55,7 +48,6 @@ } - (void)applicationDidFinishLaunching:(NSNotification *)notification { - [self.inputController setup]; [window makeKeyAndOrderFront:nil]; } @@ -75,6 +67,10 @@ object:self]; } +- (void)applicationWillBecomeActive:(NSNotification *)notification { + [self restoreToForeground:notification]; +} + - (void)transformIntoElement:(id)sender { ProcessSerialNumber psn = { 0, kCurrentProcess }; TransformProcessType(&psn, kProcessTransformToUIElementApplication); @@ -100,8 +96,6 @@ } - (void)eventTranslationActivated:(NSNotification *)note { - [dockMenu itemAtIndex:0].state = NSOnState; - [statusItemMenu itemAtIndex:0].state = NSOnState; statusItem.image = [NSImage imageNamed:@"Status Menu Icon"]; [NSWorkspace.sharedWorkspace.notificationCenter addObserver:self @@ -111,8 +105,6 @@ } - (void)eventTranslationDeactivated:(NSNotification *)note { - [dockMenu itemAtIndex:0].state = NSOffState; - [statusItemMenu itemAtIndex:0].state = NSOffState; statusItem.image = [NSImage imageNamed:@"Status Menu Icon Disabled"]; [NSWorkspace.sharedWorkspace.notificationCenter removeObserver:self @@ -120,51 +112,7 @@ object:nil]; } -- (void)restoreWindowAndShowMappings:(id)sender { - [self restoreToForeground:sender]; - [self.mappingsController mappingPressed:sender]; -} - -- (void)addMappingsToMenu:(NSMenu *)menu withKeys:(BOOL)withKeys atIndex:(NSInteger)index { - static const NSUInteger MAXIMUM_ITEMS = 5; - int added = 0; - for (NJMapping *mapping in self.mappingsController) { - NSString *keyEquiv = (++added < 10 && withKeys) ? @(added).stringValue : @""; - NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:mapping.name - action:@selector(chooseMapping:) - keyEquivalent:keyEquiv]; - item.representedObject = mapping; - item.state = mapping == self.mappingsController.currentMapping; - [menu insertItem:item atIndex:index++]; - if (added == MAXIMUM_ITEMS && self.mappingsController.mappings.count > MAXIMUM_ITEMS + 1) { - NSMenuItem *end = [[NSMenuItem alloc] initWithTitle:@"…" - action:@selector(restoreWindowAndShowMappings:) - keyEquivalent:@""]; - end.target = self; - [menu insertItem:end atIndex:index++]; - break; - } - } -} - -- (void)mappingListDidChange:(NSNotification *)note { - while (mappingsMenu.lastItem.representedObject) - [mappingsMenu removeLastItem]; - [self addMappingsToMenu:mappingsMenu withKeys:YES atIndex:mappingsMenu.numberOfItems]; - while ([statusItemMenu itemAtIndex:2].representedObject) - [statusItemMenu removeItemAtIndex:2]; - [self addMappingsToMenu:statusItemMenu withKeys:NO atIndex:2]; -} - - (void)mappingDidChange:(NSNotification *)note { - NJMapping *current = note.object; - for (NSMenuItem *item in mappingsMenu.itemArray) - if (item.representedObject) - item.state = item.representedObject == current; - for (NSMenuItem *item in statusItemMenu.itemArray) - if (item.representedObject) - item.state = item.representedObject == current; - if (!window.isVisible) for (int i = 0; i < 4; ++i) [self performSelector:@selector(flashStatusItem) @@ -172,15 +120,7 @@ afterDelay:0.2 * i]; } -- (void)chooseMapping:(NSMenuItem *)sender { - NJMapping *chosen = sender.representedObject; - [self.mappingsController activateMapping:chosen]; -} - - (NSMenu *)applicationDockMenu:(NSApplication *)sender { - while (dockMenu.lastItem.representedObject) - [dockMenu removeLastItem]; - [self addMappingsToMenu:dockMenu withKeys:NO atIndex:dockMenu.numberOfItems]; return dockMenu; } @@ -191,5 +131,14 @@ return YES; } +- (void)mappingWasChosen:(NJMapping *)mapping { + [self.mappingsController activateMapping:mapping]; +} + +- (void)mappingListShouldOpen { + [self restoreToForeground:self]; + [self.mappingsController mappingPressed:self]; +} + @end