2 // ApplicationController.m
5 // Created by Sam McCall on 4/05/09.
8 #import "ApplicationController.h"
11 #import "ConfigsController.h"
12 #import "JoystickController.h"
13 #import "TargetController.h"
15 @implementation ApplicationController {
19 @synthesize jsController;
20 @synthesize targetController;
21 @synthesize configsController;
23 - (void)didSwitchApplication:(NSNotification *)notification {
24 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
25 [self.configsController activateConfigForProcess:currentApp.localizedName];
28 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
30 self.targetController.enabled = NO;
32 [self.jsController setup];
33 [self.configsController load];
34 [[NSWorkspace sharedWorkspace].notificationCenter
36 selector:@selector(didSwitchApplication:)
37 name:NSWorkspaceDidActivateApplicationNotification
41 - (void)applicationWillTerminate:(NSNotification *)aNotification {
42 // TODO: Save immediately / shortly after changing and then enable
43 // sudden termination support.
44 [configsController save];
45 [[NSWorkspace sharedWorkspace].notificationCenter
47 name:NSWorkspaceDidActivateApplicationNotification
51 // TODO: Active state should probably be in the ConfigsController or
52 // JoystickController, not here.
58 - (void)setActive:(BOOL)newActive {
59 activeButton.label = newActive ? @"Stop" : @"Start";
60 activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
61 activeMenuItem.state = newActive;
65 - (IBAction)toggleActivity:(id)sender {
66 self.active = !self.active;
69 - (NSUInteger)firstConfigMenuIndex {
70 NSUInteger count = dockMenuBase.numberOfItems;
71 for (int i = 0; i < count; ++i)
72 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
77 - (void)configsChanged {
78 NSUInteger removeFrom = [self firstConfigMenuIndex];
79 while (dockMenuBase.numberOfItems > removeFrom)
80 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
81 for (Config *config in self.configsController.configs)
82 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
86 - (void)configChanged {
87 NSUInteger firstConfig = [self firstConfigMenuIndex];
88 Config *current = self.configsController.currentConfig;
89 NSArray *configs = self.configsController.configs;
90 for (int i = 0; i < configs.count; ++i)
91 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
94 - (void)chooseConfig:(id)sender {
95 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
96 Config *chosen = self.configsController.configs[idx];
97 [configsController activateConfig:chosen];