b86a5f2067f8ebc36e09e8bdc58bd90eda2b70ff
[enjoyable.git] / ApplicationController.m
1 //
2 // ApplicationController.m
3 // Enjoy
4 //
5 // Created by Sam McCall on 4/05/09.
6 //
7
8 #import "ApplicationController.h"
9
10 #import "Config.h"
11 #import "ConfigsController.h"
12 #import "JoystickController.h"
13 #import "TargetController.h"
14
15 @implementation ApplicationController {
16 BOOL active;
17 }
18
19 - (void)didSwitchApplication:(NSNotification *)notification {
20 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
21 [self.configsController activateConfigForProcess:currentApp.localizedName];
22 }
23
24 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
25 [drawer open];
26 self.targetController.enabled = NO;
27 [self.jsController setup];
28 [self.configsController load];
29 [[NSWorkspace sharedWorkspace].notificationCenter
30 addObserver:self
31 selector:@selector(didSwitchApplication:)
32 name:NSWorkspaceDidActivateApplicationNotification
33 object:nil];
34 }
35
36 - (void)applicationWillTerminate:(NSNotification *)aNotification {
37 [[NSUserDefaults standardUserDefaults] synchronize];
38 [[NSWorkspace sharedWorkspace].notificationCenter
39 removeObserver:self
40 name:NSWorkspaceDidActivateApplicationNotification
41 object:nil];
42 }
43
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;
50 }
51
52 - (NSInteger)firstConfigMenuIndex {
53 NSInteger count = dockMenuBase.numberOfItems;
54 for (int i = 0; i < count; ++i)
55 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
56 return i + 1;
57 return count;
58 }
59
60 - (void)configsChanged {
61 NSInteger removeFrom = [self firstConfigMenuIndex];
62 while (dockMenuBase.numberOfItems > removeFrom)
63 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
64 int added = 0;
65 for (Config *config in self.configsController.configs) {
66 NSString *keyEquiv = ++added < 10 ? @(added).stringValue : @"";
67 [dockMenuBase addItemWithTitle:config.name
68 action:@selector(chooseConfig:)
69 keyEquivalent:keyEquiv];
70
71 }
72 [_targetController refreshConfigs];
73 [self configChanged];
74 }
75
76 - (void)configChanged {
77 NSInteger firstConfig = [self firstConfigMenuIndex];
78 Config *current = self.configsController.currentConfig;
79 NSArray *configs = self.configsController.configs;
80 for (int i = 0; i < configs.count; ++i)
81 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
82 }
83
84 - (void)chooseConfig:(id)sender {
85 NSInteger idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
86 Config *chosen = self.configsController.configs[idx];
87 [_configsController activateConfig:chosen];
88 }
89 @end