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 - (NSUInteger)firstConfigMenuIndex {
53 NSUInteger count = dockMenuBase.numberOfItems;
54 for (int i = 0; i < count; ++i)
55 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
60 - (void)configsChanged {
61 NSUInteger 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:@""];
69 - (void)configChanged {
70 NSUInteger firstConfig = [self firstConfigMenuIndex];
71 Config *current = self.configsController.currentConfig;
72 NSArray *configs = self.configsController.configs;
73 for (int i = 0; i < configs.count; ++i)
74 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
77 - (void)chooseConfig:(id)sender {
78 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
79 Config *chosen = self.configsController.configs[idx];
80 [_configsController activateConfig:chosen];