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;
31 [self.jsController setup];
32 [self.configsController load];
33 [[NSWorkspace sharedWorkspace].notificationCenter
35 selector:@selector(didSwitchApplication:)
36 name:NSWorkspaceDidActivateApplicationNotification
40 - (void)applicationWillTerminate:(NSNotification *)aNotification {
41 [[NSUserDefaults standardUserDefaults] synchronize];
42 [[NSWorkspace sharedWorkspace].notificationCenter
44 name:NSWorkspaceDidActivateApplicationNotification
48 - (IBAction)toggleActivity:(id)sender {
49 BOOL sendRealEvents = !self.jsController.sendingRealEvents;
50 self.jsController.sendingRealEvents = sendRealEvents;
51 activeButton.label = sendRealEvents ? @"Stop" : @"Start";
52 activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
53 activeMenuItem.state = sendRealEvents;
56 - (NSUInteger)firstConfigMenuIndex {
57 NSUInteger count = dockMenuBase.numberOfItems;
58 for (int i = 0; i < count; ++i)
59 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
64 - (void)configsChanged {
65 NSUInteger removeFrom = [self firstConfigMenuIndex];
66 while (dockMenuBase.numberOfItems > removeFrom)
67 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
68 for (Config *config in self.configsController.configs)
69 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
73 - (void)configChanged {
74 NSUInteger firstConfig = [self firstConfigMenuIndex];
75 Config *current = self.configsController.currentConfig;
76 NSArray *configs = self.configsController.configs;
77 for (int i = 0; i < configs.count; ++i)
78 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
81 - (void)chooseConfig:(id)sender {
82 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
83 Config *chosen = self.configsController.configs[idx];
84 [configsController activateConfig:chosen];