From 19eadf9e688d8c087b47f83f8634593fddd641ac Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Thu, 28 Feb 2013 02:06:53 +0100 Subject: [PATCH] Clean up manual (previously 'neutral') vs. automatic (i.e. process-driven) configuration switching. --- ApplicationController.m | 11 +---- ConfigsController.h | 6 +-- ConfigsController.m | 90 +++++++++++++++++------------------------ TargetConfig.m | 2 +- 4 files changed, 41 insertions(+), 68 deletions(-) diff --git a/ApplicationController.m b/ApplicationController.m index e87421c..09be1c0 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -15,14 +15,7 @@ - (void)didSwitchApplication:(NSNotification *)notification { NSRunningApplication *currentApp = notification.userInfo[NSWorkspaceApplicationKey]; - ProcessSerialNumber psn; - OSStatus err; - if ((err = GetProcessForPID(currentApp.processIdentifier, &psn)) == noErr) { - [self.configsController applicationSwitchedTo:currentApp.localizedName withPsn:psn]; - } else { - NSError *error = [NSError errorWithDomain:NSOSStatusErrorDomain code:err userInfo:nil]; - NSLog(@"Error getting PSN for %@: %@", currentApp.localizedName, error); - } + [self.configsController activateConfigForProcess:currentApp.localizedName]; } - (void)applicationDidFinishLaunching:(NSNotification *)notification { @@ -94,6 +87,6 @@ - (void)chooseConfig:(id)sender { int idx = [dockMenuBase indexOfItem:sender] - [self firstConfigMenuIndex]; Config *chosen = self.configsController.configs[idx]; - [configsController activateConfig:chosen forApplication:NULL]; + [configsController activateConfig:chosen]; } @end diff --git a/ConfigsController.h b/ConfigsController.h index 5d2b4d1..32e0c61 100644 --- a/ConfigsController.h +++ b/ConfigsController.h @@ -19,18 +19,16 @@ - (IBAction)addPressed:(id)sender; - (IBAction)removePressed:(id)sender; -- (void)activateConfig:(Config *)config forApplication:(ProcessSerialNumber *)psn; +- (void)activateConfig:(Config *)config; +- (void)activateConfigForProcess:(NSString *)processName; - (NSDictionary *)dumpAll; - (void)loadAllFrom:(NSDictionary*) dict; @property (readonly) Config *currentConfig; -@property (readonly) Config *currentNeutralConfig; @property (readonly) NSArray *configs; - (void)save; - (void)load; -- (void)applicationSwitchedTo:(NSString *)name withPsn:(ProcessSerialNumber)psn; - @end diff --git a/ConfigsController.m b/ConfigsController.m index 10adcee..50861cf 100644 --- a/ConfigsController.m +++ b/ConfigsController.m @@ -7,7 +7,7 @@ @implementation ConfigsController { NSMutableArray *configs; - Config *neutralConfig; + Config *manualConfig; } @synthesize currentConfig; @@ -16,36 +16,35 @@ - (id)init { if ((self = [super init])) { configs = [[NSMutableArray alloc] init]; - currentConfig = [[Config alloc] init]; - [currentConfig setName: @"(default)"]; + currentConfig = [[Config alloc] init]; + currentConfig.name = @"(default)"; + manualConfig = currentConfig; [configs addObject:currentConfig]; } return self; } -// TODO: Neutral config stuff is a mess. +- (Config *)objectForKeyedSubscript:(NSString *)name { + for (Config *config in configs) + if ([name isEqualToString:config.name]) + return config; + return nil; +} --(void) restoreNeutralConfig { - if(!neutralConfig) - return; - [self activateConfig: neutralConfig forApplication: NULL]; +- (void)activateConfigForProcess:(NSString *)processName { + Config *oldConfig = manualConfig; + [self activateConfig:self[processName]]; + manualConfig = oldConfig; } --(void) activateConfig: (Config*)config forApplication: (ProcessSerialNumber*) psn { - if(currentConfig == config) +- (void)activateConfig:(Config *)config { + if (!config) + config = manualConfig; + if (currentConfig == config) return; - - if(psn) { - if(!neutralConfig) - neutralConfig = currentConfig; - } else { - neutralConfig = NULL; - } - - if(currentConfig != NULL) { - [targetController reset]; - } + manualConfig = config; currentConfig = config; + [targetController reset]; [removeButton setEnabled:configs[0] != config]; [targetController load]; [(ApplicationController *)[[NSApplication sharedApplication] delegate] configChanged]; @@ -66,8 +65,13 @@ if (tableView.selectedRow == 0) return; - Config *current_config = configs[tableView.selectedRow]; + Config *toRemove = configs[tableView.selectedRow]; [configs removeObjectAtIndex:tableView.selectedRow]; + + if (toRemove == currentConfig) + currentConfig = configs[0]; + if (toRemove == manualConfig) + manualConfig = configs[0]; // remove all "switch to configuration" actions for (Config *config in configs) { @@ -75,7 +79,7 @@ for (id key in entries) { Target *target = entries[key]; if ([target isKindOfClass:[TargetConfig class]] - && [(TargetConfig *)target config] == current_config) + && [(TargetConfig *)target config] == toRemove) [entries removeObjectForKey: key]; } } @@ -85,7 +89,7 @@ -(void)tableViewSelectionDidChange:(NSNotification *)notify { if (tableView.selectedRow >= 0) - [self activateConfig:configs[tableView.selectedRow] forApplication: NULL]; + [self activateConfig:configs[tableView.selectedRow]]; } - (id)tableView:(NSTableView *)view objectValueForTableColumn:(NSTableColumn *)column row:(int)index { @@ -107,16 +111,6 @@ - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(int)index { return index > 0; -} - -- (Config *)currentConfig { - return currentConfig; -} - -- (Config *)currentNeutralConfig { - if (neutralConfig) - return neutralConfig; - return currentConfig; } -(void) save { @@ -141,9 +135,9 @@ [ary addObject: cfgInfo]; } envelope[@"configurationList"] = ary; - envelope[@"selectedIndex"] = @([configs indexOfObject: [self currentNeutralConfig] ]); return envelope; } + -(void) loadAllFrom: (NSDictionary*) envelope{ if(envelope == NULL) return; @@ -163,25 +157,13 @@ } } - configs = newConfigs; - [tableView reloadData]; - currentConfig = NULL; - [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged]; - - int index = [envelope[@"selectedIndex"] intValue]; - if (index < configs.count) - [self activateConfig: configs[index] forApplication: NULL]; -} - --(void) applicationSwitchedTo: (NSString*) name withPsn: (ProcessSerialNumber) psn { - for(int i=0; i<[configs count]; i++) { - Config* cfg = configs[i]; - if([[cfg name] isEqualToString: name]) { - [self activateConfig: cfg forApplication: &psn]; - return; - } - } - [self restoreNeutralConfig]; + if (newConfigs.count) { + configs = newConfigs; + [tableView reloadData]; + currentConfig = configs[0]; + manualConfig = configs[0]; + [(ApplicationController *)[[NSApplication sharedApplication] delegate] configsChanged]; + } } @end diff --git a/TargetConfig.m b/TargetConfig.m index f62755d..8e4fc40 100644 --- a/TargetConfig.m +++ b/TargetConfig.m @@ -30,7 +30,7 @@ } -(void) trigger { - [[(ApplicationController *)[[NSApplication sharedApplication] delegate] configsController] activateConfig:config forApplication: NULL]; + [[(ApplicationController *)[[NSApplication sharedApplication] delegate] configsController] activateConfig:config]; } @end -- 2.20.1