Clean up manual (previously 'neutral') vs. automatic (i.e. process-driven) configurat...
[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;
13 @synthesize targetController;
14 @synthesize configsController;
15
16 - (void)didSwitchApplication:(NSNotification *)notification {
17 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
18 [self.configsController activateConfigForProcess:currentApp.localizedName];
19 }
20
21 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
22 [drawer open];
23 self.targetController.enabled = NO;
24 self.active = NO;
25 [self.jsController setup];
26 [self.configsController load];
27 [[NSWorkspace sharedWorkspace].notificationCenter
28 addObserver:self
29 selector:@selector(didSwitchApplication:)
30 name:NSWorkspaceDidActivateApplicationNotification
31 object:nil];
32 }
33
34 - (void)applicationWillTerminate:(NSNotification *)aNotification {
35 // TODO: Save immediately / shortly after changing and then enable
36 // sudden termination support.
37 [configsController save];
38 [[NSWorkspace sharedWorkspace].notificationCenter
39 removeObserver:self
40 name:NSWorkspaceDidActivateApplicationNotification
41 object:nil];
42 }
43
44 // TODO: Active state should probably be in the ConfigsController or
45 // JoystickController, not here.
46
47 - (BOOL)active {
48 return active;
49 }
50
51 - (void)setActive:(BOOL)newActive {
52 activeButton.label = newActive ? @"Stop" : @"Start";
53 activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
54 activeMenuItem.state = newActive;
55 active = newActive;
56 }
57
58 - (IBAction)toggleActivity:(id)sender {
59 self.active = !self.active;
60 }
61
62 - (NSUInteger)firstConfigMenuIndex {
63 NSUInteger count = dockMenuBase.numberOfItems;
64 for (int i = 0; i < count; ++i)
65 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
66 return i + 1;
67 return count;
68 }
69
70 - (void)configsChanged {
71 NSUInteger removeFrom = [self firstConfigMenuIndex];
72 while (dockMenuBase.numberOfItems > removeFrom)
73 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
74 for (Config *config in self.configsController.configs)
75 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
76 [self configChanged];
77 }
78
79 - (void)configChanged {
80 NSUInteger firstConfig = [self firstConfigMenuIndex];
81 Config *current = self.configsController.currentConfig;
82 NSArray *configs = self.configsController.configs;
83 for (int i = 0; i < configs.count; ++i)
84 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
85 }
86
87 - (void)chooseConfig:(id)sender {
88 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
89 Config *chosen = self.configsController.configs[idx];
90 [configsController activateConfig:chosen];
91 }
92 @end