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 [[NSUserDefaults standardUserDefaults] synchronize];
43 [[NSWorkspace sharedWorkspace].notificationCenter
45 name:NSWorkspaceDidActivateApplicationNotification
49 // TODO: Active state should probably be in the ConfigsController or
50 // JoystickController, not here.
56 - (void)setActive:(BOOL)newActive {
57 activeButton.label = newActive ? @"Stop" : @"Start";
58 activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
59 activeMenuItem.state = newActive;
63 - (IBAction)toggleActivity:(id)sender {
64 self.active = !self.active;
67 - (NSUInteger)firstConfigMenuIndex {
68 NSUInteger count = dockMenuBase.numberOfItems;
69 for (int i = 0; i < count; ++i)
70 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
75 - (void)configsChanged {
76 NSUInteger removeFrom = [self firstConfigMenuIndex];
77 while (dockMenuBase.numberOfItems > removeFrom)
78 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
79 for (Config *config in self.configsController.configs)
80 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
84 - (void)configChanged {
85 NSUInteger firstConfig = [self firstConfigMenuIndex];
86 Config *current = self.configsController.currentConfig;
87 NSArray *configs = self.configsController.configs;
88 for (int i = 0; i < configs.count; ++i)
89 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
92 - (void)chooseConfig:(id)sender {
93 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
94 Config *chosen = self.configsController.configs[idx];
95 [configsController activateConfig:chosen];