2 // ApplicationController.m
5 // Created by Sam McCall on 4/05/09.
8 @implementation ApplicationController {
12 @synthesize jsController, targetController, configsController;
14 - (void)didSwitchApplication:(NSNotification *)notification {
15 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
16 ProcessSerialNumber psn;
18 if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) {
19 [self->configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn];
21 NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
22 NSLog(@"Error getting PSN for %@: %@", currentApp.localizedName, error);
26 -(void) applicationDidFinishLaunching:(NSNotification*) notification {
29 [targetController setEnabled: NO];
31 [configsController load];
32 [[[NSWorkspace sharedWorkspace] notificationCenter]
34 selector:@selector(didSwitchApplication:)
35 name:NSWorkspaceDidActivateApplicationNotification
39 -(void) applicationWillTerminate: (NSNotification *)aNotification {
40 [configsController save];
41 [[[NSWorkspace sharedWorkspace] notificationCenter]
43 name:NSWorkspaceDidActivateApplicationNotification
47 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
48 hasVisibleWindows:(BOOL)flag
50 [mainWindow makeKeyAndOrderFront:self];
58 - (void)setActive:(BOOL)newActive {
59 [activeButton setLabel:newActive ? @"Stop" : @"Start"];
60 NSImage *buttonImage = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
61 [activeButton setImage:buttonImage];
62 [activeMenuItem setState:newActive];
66 - (IBAction)toggleActivity:(id)sender {
67 self.active = !self.active;
70 -(void) configsChanged {
71 while([dockMenuBase numberOfItems] > 2)
72 [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)];
74 for(Config* config in [configsController configs]) {
75 [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""];
79 -(void) configChanged {
80 Config* current = [configsController currentConfig];
81 NSArray* configs = [configsController configs];
82 for(int i=0; i<[configs count]; i++)
83 [[dockMenuBase itemAtIndex: (2+i)] setState: (configs[i] == current)];
86 -(void) chooseConfig: (id) sender {
87 [configsController activateConfig: [configsController configs][([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL];