Clean-up of Joystick class. Refactor constructor to avoid mandatory 'post-constructor...
[enjoyable.git] / ApplicationController.m
1 //
2 // ApplicationController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 @implementation ApplicationController {
9 BOOL active;
10 }
11
12 @synthesize jsController, targetController, configsController;
13
14 - (void)didSwitchApplication:(NSNotification *)notification {
15 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
16 ProcessSerialNumber psn;
17 OSStatus err;
18 if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) {
19 [self->configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn];
20 } else {
21 NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil];
22 NSLog(@"Error getting PSN for %@: %@", currentApp.localizedName, error);
23 }
24 }
25
26 -(void) applicationDidFinishLaunching:(NSNotification*) notification {
27 [jsController setup];
28 [drawer open];
29 [targetController setEnabled: NO];
30 self.active = NO;
31 [configsController load];
32 [[[NSWorkspace sharedWorkspace] notificationCenter]
33 addObserver:self
34 selector:@selector(didSwitchApplication:)
35 name:NSWorkspaceDidActivateApplicationNotification
36 object:nil];
37 }
38
39 -(void) applicationWillTerminate: (NSNotification *)aNotification {
40 [configsController save];
41 [[[NSWorkspace sharedWorkspace] notificationCenter]
42 removeObserver:self
43 name:NSWorkspaceDidActivateApplicationNotification
44 object:nil];
45 }
46
47 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
48 hasVisibleWindows:(BOOL)flag
49 {
50 [mainWindow makeKeyAndOrderFront:self];
51 return YES;
52 }
53
54 - (BOOL)active {
55 return active;
56 }
57
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];
63 active = newActive;
64 }
65
66 - (IBAction)toggleActivity:(id)sender {
67 self.active = !self.active;
68 }
69
70 -(void) configsChanged {
71 while([dockMenuBase numberOfItems] > 2)
72 [dockMenuBase removeItemAtIndex: ([dockMenuBase numberOfItems] - 1)];
73
74 for(Config* config in [configsController configs]) {
75 [dockMenuBase addItemWithTitle:[config name] action:@selector(chooseConfig:) keyEquivalent:@""];
76 }
77 [self configChanged];
78 }
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)];
84 }
85
86 -(void) chooseConfig: (id) sender {
87 [configsController activateConfig: [configsController configs][([dockMenuBase indexOfItem: sender]-2)] forApplication: NULL];
88 }
89 @end