#import "NJOutputController.h"
#import "NJEvents.h"
+#define PB_ROW @"com.yukkurigames.Enjoyable.MappingRow"
+
@implementation NJMappingsController {
NSMutableArray *_mappings;
NJMapping *manualMapping;
+ NSString *draggingName;
}
- (id)init {
return self;
}
+- (void)awakeFromNib {
+ [tableView registerForDraggedTypes:@[PB_ROW]];
+ [tableView setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
+}
+
- (NJMapping *)objectForKeyedSubscript:(NSString *)name {
for (NJMapping *mapping in _mappings)
if ([name isEqualToString:mapping.name])
[self save];
[tableView reloadData];
popoverActivate.title = _currentMapping.name;
+ [self updateInterfaceForCurrentMapping];
[NSNotificationCenter.defaultCenter
postNotificationName:NJEventMappingListChanged
object:_mappings];
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 {
NSLog(@"Switching to mapping %@.", mapping.name);
manualMapping = mapping;
_currentMapping = mapping;
- [removeButton setEnabled:_mappings[0] != mapping];
+ [self updateInterfaceForCurrentMapping];
[outputController loadCurrent];
- popoverActivate.title = _currentMapping.name;
- NSUInteger selected = [_mappings indexOfObject:mapping];
- [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:selected] byExtendingSelection:NO];
- [NSUserDefaults.standardUserDefaults setInteger:selected forKey:@"selected"];
[NSNotificationCenter.defaultCenter postNotificationName:NJEventMappingChanged
object:_currentMapping];
}
- (IBAction)addPressed:(id)sender {
NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"];
[_mappings addObject:newMapping];
- [self mappingsChanged];
[self activateMapping:newMapping];
+ [self mappingsChanged];
[tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
}
return;
[_mappings removeObjectAtIndex:tableView.selectedRow];
- [self mappingsChanged];
[self activateMapping:_mappings[0]];
+ [self mappingsChanged];
}
-(void)tableViewSelectionDidChange:(NSNotification *)notify {
_mappings = newMappings;
if (selected >= newMappings.count)
selected = 0;
- [self mappingsChanged];
[self activateMapping:_mappings[selected]];
+ [self mappingsChanged];
}
}
[_mappings addObject:mapping];
}
- [self mappingsChanged];
[self activateMapping:mapping];
- [outputController loadCurrent];
+ [self mappingsChanged];
if (conflict && !mergeInto) {
[tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
return;
[panel close];
NSError *error;
- NSDictionary *serialization = [mapping serialize];
- NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
- options:NSJSONWritingPrettyPrinted
- error:&error];
- if (!error)
- [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
-
+ [mapping writeToURL:panel.URL error:&error];
if (error) {
[window presentError:error
modalForWindow:window
}
- (IBAction)mappingPressed:(id)sender {
- [popover showRelativeToRect:popoverActivate.bounds ofView:popoverActivate preferredEdge:NSMinXEdge];
+ [popover showRelativeToRect:popoverActivate.bounds
+ ofView:popoverActivate
+ preferredEdge:NSMinXEdge];
}
- (void)popoverWillShow:(NSNotification *)notification {
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];
+ }
+}
+
+- (BOOL)tableView:(NSTableView *)tableView
+ acceptDrop:(id <NSDraggingInfo>)info
+ row:(NSInteger)row
+ dropOperation:(NSTableViewDropOperation)dropOperation {
+ NSPasteboard *pboard = [info draggingPasteboard];
+ if ([pboard.types containsObject:PB_ROW]) {
+ NSString *value = [pboard stringForType:PB_ROW];
+ NSUInteger srcRow = [value intValue];
+ [_mappings moveObjectAtIndex:srcRow toIndex:row];
+ [self mappingsChanged];
+ return YES;
+ } else {
+ return NO;
+ }
+}
+
+- (NSDragOperation)tableView:(NSTableView *)tableView_
+ validateDrop:(id <NSDraggingInfo>)info
+ proposedRow:(NSInteger)row
+ proposedDropOperation:(NSTableViewDropOperation)dropOperation {
+ NSPasteboard *pboard = [info draggingPasteboard];
+ if ([pboard.types containsObject:PB_ROW]) {
+ [tableView_ setDropRow:MAX(1, row) dropOperation:NSTableViewDropAbove];
+ return NSDragOperationGeneric;
+ } else {
+ return NSDragOperationNone;
+ }
+}
+
+- (NSArray *)tableView:(NSTableView *)tableView_
+namesOfPromisedFilesDroppedAtDestination:(NSURL *)dropDestination
+forDraggedRowsWithIndexes:(NSIndexSet *)indexSet {
+ NJMapping *toSave = self[indexSet.firstIndex];
+ NSString *filename = [[toSave.name stringByFixingPathComponent]
+ stringByAppendingPathExtension:@"enjoyable"];
+ NSURL *dst = [dropDestination URLByAppendingPathComponent:filename];
+ dst = [NSFileManager.defaultManager generateUniqueURLWithBase:dst];
+ NSError *error;
+ if (![toSave writeToURL:dst error:&error]) {
+ [tableView_ presentError:error];
+ return @[];
+ } else {
+ return @[dst.lastPathComponent];
+ }
+}
+
+- (BOOL)tableView:(NSTableView *)tableView_
+writeRowsWithIndexes:(NSIndexSet *)rowIndexes
+ toPasteboard:(NSPasteboard *)pboard {
+ if (rowIndexes.count == 1 && rowIndexes.firstIndex != 0) {
+ [pboard declareTypes:@[PB_ROW, NSFilesPromisePboardType] owner:nil];
+ [pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW];
+ [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];
+ return YES;
+ } else {
+ return NO;
+ }
+}
+
@end