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 - (void)didSwitchApplication:(NSNotification *)notification {
20 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
21 [self.configsController activateConfigForProcess:currentApp.localizedName];
24 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
26 self.targetController.enabled = NO;
27 [self.jsController setup];
28 [self.configsController load];
29 [[NSWorkspace sharedWorkspace].notificationCenter
31 selector:@selector(didSwitchApplication:)
32 name:NSWorkspaceDidActivateApplicationNotification
36 - (void)applicationWillTerminate:(NSNotification *)aNotification {
37 [[NSUserDefaults standardUserDefaults] synchronize];
38 [[NSWorkspace sharedWorkspace].notificationCenter
40 name:NSWorkspaceDidActivateApplicationNotification
44 - (IBAction)toggleActivity:(id)sender {
45 BOOL sendRealEvents = !self.jsController.sendingRealEvents;
46 self.jsController.sendingRealEvents = sendRealEvents;
47 activeButton.label = sendRealEvents ? @"Stop" : @"Start";
48 activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
49 activeMenuItem.state = sendRealEvents;
52 - (NSInteger)firstConfigMenuIndex {
53 NSInteger count = dockMenuBase.numberOfItems;
54 for (int i = 0; i < count; ++i)
55 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
60 - (void)configsChanged {
61 NSInteger removeFrom = [self firstConfigMenuIndex];
62 while (dockMenuBase.numberOfItems > removeFrom)
63 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
64 for (Config *config in self.configsController.configs)
65 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
66 [_targetController refreshConfigs];
70 - (void)configChanged {
71 NSInteger firstConfig = [self firstConfigMenuIndex];
72 Config *current = self.configsController.currentConfig;
73 NSArray *configs = self.configsController.configs;
74 for (int i = 0; i < configs.count; ++i)
75 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
78 - (void)chooseConfig:(id)sender {
79 NSInteger idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
80 Config *chosen = self.configsController.configs[idx];
81 [_configsController activateConfig:chosen];