From 9ba68ad5a4378bd897fc611929f201681bc71c16 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Tue, 26 Feb 2013 19:57:43 +0100 Subject: [PATCH] Switch from kEventAppFrontSwitched (Carbon) to NSWorkspaceDidActivateApplicationNotification (Cocoa) to detect application switching. --- ApplicationController.m | 68 ++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/ApplicationController.m b/ApplicationController.m index 2edac94..55abf05 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -4,31 +4,44 @@ // // Created by Sam McCall on 4/05/09. // -#include -@implementation ApplicationController +@implementation ApplicationController { + BOOL active; +} @synthesize jsController, targetController, configsController; -static BOOL active; - -pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void* userData); +- (void)didSwitchApplication:(NSNotification *)notification { + NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; + ProcessSerialNumber psn; + OSStatus err; + if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) { + [self->configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn]; + } else { + NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil]; + NSLog(@"Error getting PSN for %@: %@", currentApp.localizedName, error); + } +} --(void) applicationDidFinishLaunching: (NSNotification*) notification { +-(void) applicationDidFinishLaunching:(NSNotification*) notification { [jsController setup]; [drawer open]; - [targetController setEnabled: false]; - [self setActive: NO]; + [targetController setEnabled: NO]; + self.active = NO; [configsController load]; - EventTypeSpec et; - et.eventClass = kEventClassApplication; - et.eventKind = kEventAppFrontSwitched; - EventHandlerUPP handler = NewEventHandlerUPP(appSwitch); - InstallApplicationEventHandler(handler, 1, &et, (void *)CFBridgingRetain(self), NULL); + [[[NSWorkspace sharedWorkspace] notificationCenter] + addObserver:self + selector:@selector(didSwitchApplication:) + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; } -(void) applicationWillTerminate: (NSNotification *)aNotification { [configsController save]; + [[[NSWorkspace sharedWorkspace] notificationCenter] + removeObserver:self + name:NSWorkspaceDidActivateApplicationNotification + object:nil]; } - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication @@ -38,29 +51,20 @@ pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void return YES; } -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; -} - --(BOOL) active { - return active; +- (BOOL)active { + return active; } --(void) setActive: (BOOL) newActive { - [activeButton setLabel: (newActive ? @"Stop" : @"Start")]; - [activeButton setImage: [NSImage imageNamed: (newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate" )]]; - [activeMenuItem setState: (newActive ? 1 : 0)]; +- (void)setActive:(BOOL)newActive { + [activeButton setLabel:newActive ? @"Stop" : @"Start"]; + NSImage *buttonImage = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; + [activeButton setImage:buttonImage]; + [activeMenuItem setState:newActive]; active = newActive; } --(IBAction) toggleActivity: (id)sender { - [self setActive: ![self active]]; +- (IBAction)toggleActivity:(id)sender { + self.active = !self.active; } -(void) configsChanged { @@ -76,7 +80,7 @@ pascal OSStatus appSwitch(EventHandlerCallRef handlerChain, EventRef event, void 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)]; + [[dockMenuBase itemAtIndex: (2+i)] setState: (configs[i] == current)]; } -(void) chooseConfig: (id) sender { -- 2.30.2