Clean up save/restore. Write preferences immediately but continue relying on automati...
[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 [[NSUserDefaults standardUserDefaults] synchronize];
43 [[NSWorkspace sharedWorkspace].notificationCenter
44 removeObserver:self
45 name:NSWorkspaceDidActivateApplicationNotification
46 object:nil];
47 }
48
49 // TODO: Active state should probably be in the ConfigsController or
50 // JoystickController, not here.
51
52 - (BOOL)active {
53 return active;
54 }
55
56 - (void)setActive:(BOOL)newActive {
57 activeButton.label = newActive ? @"Stop" : @"Start";
58 activeButton.image = [NSImage imageNamed:newActive ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"];
59 activeMenuItem.state = newActive;
60 active = newActive;
61 }
62
63 - (IBAction)toggleActivity:(id)sender {
64 self.active = !self.active;
65 }
66
67 - (NSUInteger)firstConfigMenuIndex {
68 NSUInteger count = dockMenuBase.numberOfItems;
69 for (int i = 0; i < count; ++i)
70 if ([dockMenuBase itemAtIndex:i].isSeparatorItem)
71 return i + 1;
72 return count;
73 }
74
75 - (void)configsChanged {
76 NSUInteger removeFrom = [self firstConfigMenuIndex];
77 while (dockMenuBase.numberOfItems > removeFrom)
78 [dockMenuBase removeItemAtIndex:dockMenuBase.numberOfItems - 1];
79 for (Config *config in self.configsController.configs)
80 [dockMenuBase addItemWithTitle:config.name action:@selector(chooseConfig:) keyEquivalent:@""];
81 [self configChanged];
82 }
83
84 - (void)configChanged {
85 NSUInteger firstConfig = [self firstConfigMenuIndex];
86 Config *current = self.configsController.currentConfig;
87 NSArray *configs = self.configsController.configs;
88 for (int i = 0; i < configs.count; ++i)
89 [dockMenuBase itemAtIndex:i + firstConfig].state = configs[i] == current;
90 }
91
92 - (void)chooseConfig:(id)sender {
93 int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex];
94 Config *chosen = self.configsController.configs[idx];
95 [configsController activateConfig:chosen];
96 }
97 @end