2 // ApplicationController.m
5 // Created by Sam McCall on 4/05/09.
8 @implementation ApplicationController {
12 @synthesize jsController;
13 @synthesize targetController;
14 @synthesize configsController;
16 - (void)didSwitchApplication:(NSNotification *)notification {
17 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
18 [self.configsController activateConfigForProcess:currentApp.localizedName];
21 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
23 self.targetController.enabled = NO;
25 [self.jsController setup];
26 [self.configsController load];
27 [[NSWorkspace sharedWorkspace].notificationCenter
29 selector:@selector(didSwitchApplication:)
30 name:NSWorkspaceDidActivateApplicationNotification
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
40 name:NSWorkspaceDidActivateApplicationNotification
44 // TODO: Active state should probably be in the ConfigsController or
45 // JoystickController, not here.
51 - (void)setActive:(BOOL)newActive {
52 activeButton.label = newActive ? @"Stop" : @"Start";
53 activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
54 activeMenuItem.state = newActive;
58 - (IBAction)toggleActivity:(id)sender {
59 self.active = !self.active;
62 - (NSUInteger)firstConfigMenuIndex {
63 NSUInteger count = dockMenuBase.numberOfItems;
64 for (int i = 0; i < count; ++i)
65 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
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:@""];
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;
87 - (void)chooseConfig:(id)sender {
88 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
89 Config *chosen = self.configsController.configs[idx];
90 [configsController activateConfig:chosen];