X-Git-Url: https://git.yukkurigames.com/?p=enjoyable.git;a=blobdiff_plain;f=NJMappingsController.m;h=a1171a0088ffc0f69c64d26e43f23d449467cf69;hp=e2ac93c49a54310bcc8f0ff2cdd7b7552d5f9743;hb=f0909929a979b62a110e230e8b384cafaed33eba;hpb=1315dd378437c84891c795c9927ff40d42d74cb3 diff --git a/NJMappingsController.m b/NJMappingsController.m index e2ac93c..a1171a0 100644 --- a/NJMappingsController.m +++ b/NJMappingsController.m @@ -42,6 +42,8 @@ - (void)mappingsChanged { [self save]; [tableView reloadData]; + popoverActivate.title = _currentMapping.name; + [self updateInterfaceForCurrentMapping]; [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingListChanged object:_mappings]; @@ -55,29 +57,43 @@ count:len]; } - - (void)activateMappingForProcess:(NSString *)processName { - NJMapping *oldMapping = manualMapping; - NJMapping *newMapping = self[processName]; - if (!newMapping) - newMapping = oldMapping; - if (newMapping != _currentMapping) - [self activateMapping:newMapping]; - manualMapping = oldMapping; + if ([manualMapping.name.lowercaseString isEqualToString:@"@application"]) { + manualMapping.name = processName; + [self mappingsChanged]; + } else { + NJMapping *oldMapping = manualMapping; + NJMapping *newMapping = self[processName]; + if (!newMapping) + newMapping = oldMapping; + if (newMapping != _currentMapping) + [self activateMapping:newMapping]; + manualMapping = oldMapping; + } +} + +- (void)updateInterfaceForCurrentMapping { + NSUInteger selected = [_mappings indexOfObject:_currentMapping]; + [removeButton setEnabled:selected != 0]; + [moveDown setEnabled:selected && selected != _mappings.count - 1]; + [moveUp setEnabled:selected > 1]; + popoverActivate.title = _currentMapping.name; + [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selected] byExtendingSelection:NO]; + [NSUserDefaults.standardUserDefaults setInteger:selected forKey:@"selected"]; } - (void)activateMapping:(NJMapping *)mapping { if (!mapping) mapping = manualMapping; + if (mapping == _currentMapping) + return; NSLog(@"Switching to mapping %@.", mapping.name); manualMapping = mapping; _currentMapping = mapping; - [removeButton setEnabled:_mappings[0] != mapping]; + [self updateInterfaceForCurrentMapping]; [outputController loadCurrent]; - popoverActivate.title = _currentMapping.name; [NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged object:_currentMapping]; - [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[_mappings indexOfObject:mapping]] byExtendingSelection:NO]; } - (IBAction)addPressed:(id)sender { @@ -118,28 +134,24 @@ } - (BOOL)tableView:(NSTableView *)view shouldEditTableColumn:(NSTableColumn *)column row:(NSInteger)index { - return index > 0; + return YES; } - (void)save { NSLog(@"Saving mappings to defaults."); - [NSUserDefaults.standardUserDefaults setValuesForKeysWithDictionary:[self dumpAll]]; -} - -- (void)load { - [self loadAllFrom:NSUserDefaults.standardUserDefaults.dictionaryRepresentation]; -} - -- (NSDictionary *)dumpAll { NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_mappings.count]; for (NJMapping *mapping in _mappings) [ary addObject:[mapping serialize]]; - NSUInteger current = _currentMapping ? [_mappings indexOfObject:_currentMapping] : 0; - return @{ @"mappings": ary, @"selected": @(current) }; + [NSUserDefaults.standardUserDefaults setObject:ary forKey:@"mappings"]; +} + +- (void)load { + NSUInteger selected = [NSUserDefaults.standardUserDefaults integerForKey:@"selected"]; + NSArray *mappings = [NSUserDefaults.standardUserDefaults arrayForKey:@"mappings"]; + [self loadAllFrom:mappings andActivate:selected]; } -- (void)loadAllFrom:(NSDictionary*)envelope { - NSArray *storedMappings = envelope[@"mappings"]; +- (void)loadAllFrom:(NSArray *)storedMappings andActivate:(NSUInteger)selected { NSMutableArray* newMappings = [[NSMutableArray alloc] initWithCapacity:storedMappings.count]; // have to do two passes in case mapping1 refers to mapping2 via a NJOutputMapping @@ -160,12 +172,11 @@ } if (newMappings.count) { - unsigned current = [envelope[@"selected"] unsignedIntValue]; - if (current >= newMappings.count) - current = 0; _mappings = newMappings; + if (selected >= newMappings.count) + selected = 0; + [self activateMapping:_mappings[selected]]; [self mappingsChanged]; - [self activateMapping:_mappings[current]]; } } @@ -281,6 +292,7 @@ if (result != NSFileHandlingPanelOKButton) return; [panel close]; + [NSProcessInfo.processInfo disableSuddenTermination]; NSError *error; NSDictionary *serialization = [mapping serialize]; NSData *json = [NSJSONSerialization dataWithJSONObject:serialization @@ -289,6 +301,7 @@ if (!error) [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error]; + [NSProcessInfo.processInfo enableSuddenTermination]; if (error) { [window presentError:error modalForWindow:window @@ -311,4 +324,21 @@ popoverActivate.state = NSOffState; } +- (IBAction)moveUpPressed:(id)sender { + NSUInteger idx = [_mappings indexOfObject:_currentMapping]; + if (idx > 1 && idx != NSNotFound) { + [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1]; + [self mappingsChanged]; + } +} + +- (IBAction)moveDownPressed:(id)sender { + NSUInteger idx = [_mappings indexOfObject:_currentMapping]; + if (idx < _mappings.count - 1) { + [_mappings exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1]; + [self mappingsChanged]; + } +} + + @end