8fed5d61f3d047ce6d46586f86e7ba41811a47ed
[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 @synthesize jsController;
20 @synthesize targetController;
21 @synthesize configsController;
22
23 - (void)didSwitchApplication:(NSNotification *)notification {
24 NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey];
25 [self.configsController activateConfigForProcess:currentApp.localizedName];
26 }
27
28 - (void)applicationDidFinishLaunching:(NSNotification *)notification {
29 [drawer open];
30 self.targetController.enabled = NO;
31 self.active = NO;
32 [self.jsController setup];
33 [self.configsController load];
34 [[NSWorkspace sharedWorkspace].notificationCenter
35 addObserver:self
36 selector:@selector(didSwitchApplication:)
37 name:NSWorkspaceDidActivateApplicationNotification
38 object:nil];
39 }
40
41 - (void)applicationWillTerminate:(NSNotification *)aNotification {
42 // TODO: Save immediately / shortly after changing and then enable
43 // sudden termination support.
44 [configsController save];
45 [[NSWorkspace sharedWorkspace].notificationCenter
46 removeObserver:self
47 name:NSWorkspaceDidActivateApplicationNotification
48 object:nil];
49 }
50
51 // TODO: Active state should probably be in the ConfigsController or
52 // JoystickController, not here.
53
54 - (BOOL)active {
55 return active;
56 }
57
58 - (void)setActive:(BOOL)newActive {
59 activeButton.label = newActive ? @"Stop" : @"Start";
60 activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
61 activeMenuItem.state = newActive;
62 active = newActive;
63 }
64
65 - (IBAction)toggleActivity:(id)sender {
66 self.active = !self.active;
67 }
68
69 - (NSUInteger)firstConfigMenuIndex {
70 NSUInteger count = dockMenuBase.numberOfItems;
71 for (int i = 0; i < count; ++i)
72 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
73 return i + 1;
74 return count;
75 }
76
77 - (void)configsChanged {
78 NSUInteger removeFrom = [self firstConfigMenuIndex];
79 while (dockMenuBase.numberOfItems > removeFrom)
80 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
81 for (Config *config in self.configsController.configs)
82 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
83 [self configChanged];
84 }
85
86 - (void)configChanged {
87 NSUInteger firstConfig = [self firstConfigMenuIndex];
88 Config *current = self.configsController.currentConfig;
89 NSArray *configs = self.configsController.configs;
90 for (int i = 0; i < configs.count; ++i)
91 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
92 }
93
94 - (void)chooseConfig:(id)sender {
95 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
96 Config *chosen = self.configsController.configs[idx];
97 [configsController activateConfig:chosen];
98 }
99 @end