From 30f40c368cf11684956e992cb13a8f70b5c5e0ce Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Mon, 18 Mar 2013 01:19:19 +0100 Subject: [PATCH] Tweak some animations, clean up for preparation to move to app delegate. --- Classes/EnjoyableApplicationDelegate.m | 19 ++-- Classes/NJDeviceViewController.m | 4 +- Classes/NJMappingsController.h | 16 ++- Classes/NJMappingsController.m | 144 ++++++++++++++----------- Classes/NJMappingsViewController.m | 13 --- Info.plist | 2 +- 6 files changed, 113 insertions(+), 85 deletions(-) diff --git a/Classes/EnjoyableApplicationDelegate.m b/Classes/EnjoyableApplicationDelegate.m index 26ea09c..9fef7c3 100644 --- a/Classes/EnjoyableApplicationDelegate.m +++ b/Classes/EnjoyableApplicationDelegate.m @@ -139,17 +139,20 @@ NSURL *URL = [NSURL fileURLWithPath:filename]; NJMapping *mapping = [NJMapping mappingWithContentsOfURL:URL error:&error]; - if (mapping) { - [self.mappingsController addOrMergeMapping:mapping]; - return YES; + if ([self.mappingsController[mapping.name] hasConflictWith:mapping]) { + [self.mappingsController promptForMapping:mapping atIndex:self.mappingsController.count]; + } else if (self.mappingsController[mapping.name]) { + [self.mappingsController[mapping.name] mergeEntriesFrom:mapping]; + } else if (mapping) { + [self.mappingsController addMapping:mapping]; } else { [window presentError:error modalForWindow:window delegate:nil didPresentSelector:nil contextInfo:nil]; - return NO; } + return !!mapping; } - (void)mappingWasChosen:(NJMapping *)mapping { @@ -213,8 +216,12 @@ NSError *error; NJMapping *mapping = [NJMapping mappingWithContentsOfURL:panel.URL error:&error]; - if (mapping) { - [self.mappingsController addOrMergeMapping:mapping]; + if ([self.mappingsController[mapping.name] hasConflictWith:mapping]) { + [self.mappingsController promptForMapping:mapping atIndex:self.mappingsController.count]; + } else if (self.mappingsController[mapping.name]) { + [self.mappingsController[mapping.name] mergeEntriesFrom:mapping]; + } else if (mapping) { + [self.mappingsController addMapping:mapping]; } else { [window presentError:error modalForWindow:window diff --git a/Classes/NJDeviceViewController.m b/Classes/NJDeviceViewController.m index dfb2dca..f9ff141 100644 --- a/Classes/NJDeviceViewController.m +++ b/Classes/NJDeviceViewController.m @@ -50,7 +50,7 @@ [self.inputsTree beginUpdates]; [self.inputsTree insertItemsAtIndexes:[[NSIndexSet alloc] initWithIndex:idx] inParent:nil - withAnimation:NSTableViewAnimationSlideLeft]; + withAnimation:NSTableViewAnimationEffectFade]; [self reexpandAll]; [self.inputsTree endUpdates]; self.noDevicesNotice.hidden = YES; @@ -61,7 +61,7 @@ [self.inputsTree beginUpdates]; [self.inputsTree removeItemsAtIndexes:[[NSIndexSet alloc] initWithIndex:idx] inParent:nil - withAnimation:NSTableViewAnimationSlideLeft]; + withAnimation:NSTableViewAnimationEffectFade]; [self.inputsTree endUpdates]; self.noDevicesNotice.hidden = anyDevices || !self.hidStoppedNotice.isHidden; } diff --git a/Classes/NJMappingsController.h b/Classes/NJMappingsController.h index 39aaf79..85ba54f 100644 --- a/Classes/NJMappingsController.h +++ b/Classes/NJMappingsController.h @@ -15,13 +15,27 @@ NJMappingsViewControllerDelegate> @property (nonatomic, readonly) NJMapping *currentMapping; +@property (nonatomic, readonly) NSUInteger count; + @property (nonatomic, strong) IBOutlet NJMappingsViewController *mvc; - (NJMapping *)objectForKeyedSubscript:(NSString *)name; - (NJMapping *)objectAtIndexedSubscript:(NSUInteger)idx; +- (NSInteger)indexOfMapping:(NJMapping *)mapping; + +- (void)addMapping:(NJMapping *)mapping; +- (void)insertMapping:(NJMapping *)mapping atIndex:(NSInteger)idx; +- (void)removeMappingAtIndex:(NSInteger)idx; +- (void)mergeMapping:(NJMapping *)mapping intoMapping:(NJMapping *)existing; +- (void)moveMoveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx; + +- (void)mappingsChanged; + +- (void)promptForMapping:(NJMapping *)mapping atIndex:(NSInteger)idx; + // FIXME: Doesn't belong here. + - (void)activateMapping:(NJMapping *)mapping; - (void)activateMappingForProcess:(NSRunningApplication *)app; -- (void)addOrMergeMapping:(NJMapping *)mapping; - (void)save; - (void)load; diff --git a/Classes/NJMappingsController.m b/Classes/NJMappingsController.m index 9c7b641..2b08a52 100644 --- a/Classes/NJMappingsController.m +++ b/Classes/NJMappingsController.m @@ -138,6 +138,47 @@ } } +- (NSInteger)indexOfMapping:(NJMapping *)mapping { + return [_mappings indexOfObjectIdenticalTo:mapping]; +} + +- (void)mergeMapping:(NJMapping *)mapping intoMapping:(NJMapping *)existing { + [existing mergeEntriesFrom:mapping]; + [self mappingsChanged]; + if (existing == _currentMapping) { + // FIXME: Hack to trigger updates when renaming. + _currentMapping = nil; + NJMapping *manual = _manualMapping; + [self activateMapping:existing]; + _manualMapping = manual; + } +} + +- (void)addMapping:(NJMapping *)mapping { + [self insertMapping:mapping atIndex:_mappings.count]; +} + +- (void)insertMapping:(NJMapping *)mapping atIndex:(NSInteger)idx { + [_mappings insertObject:mapping atIndex:idx]; + [self mappingsChanged]; +} + +- (void)removeMappingAtIndex:(NSInteger)idx { + NSInteger currentIdx = [self indexOfMapping:_currentMapping]; + [_mappings removeObjectAtIndex:idx]; + [self activateMapping:self[MIN(currentIdx, _mappings.count - 1)]]; + [self mappingsChanged]; +} + +- (void)moveMoveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx { + [_mappings moveObjectAtIndex:fromIdx toIndex:toIdx]; + [self mappingsChanged]; +} + +- (NSUInteger)count { + return _mappings.count; +} + - (void)mappingConflictDidResolve:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo { @@ -147,70 +188,46 @@ [alert.window orderOut:nil]; switch (returnCode) { case NSAlertFirstButtonReturn: // Merge - [oldMapping mergeEntriesFrom:newMapping]; - _currentMapping = nil; + [self mergeMapping:newMapping intoMapping:oldMapping]; [self activateMapping:oldMapping]; - [self mappingsChanged]; break; case NSAlertThirdButtonReturn: // New Mapping [self.mvc.mappingList beginUpdates]; - [_mappings addObject:newMapping]; + [self addMapping:newMapping]; [self.mvc addedMappingAtIndex:_mappings.count - 1 startEditing:YES]; [self.mvc.mappingList endUpdates]; [self activateMapping:newMapping]; - [self mappingsChanged]; break; default: // Cancel, other. break; } } -- (void)addOrMergeMapping:(NJMapping *)mapping { - [self addOrMergeMapping:mapping atIndex:-1]; -} - -- (void)addOrMergeMapping:(NJMapping *)mapping atIndex:(NSInteger)idx { +- (void)promptForMapping:(NJMapping *)mapping atIndex:(NSInteger)idx { NSWindow *window = NSApplication.sharedApplication.keyWindow; - if (mapping) { - NJMapping *mergeInto = self[mapping.name]; - if ([mergeInto hasConflictWith:mapping]) { - NSAlert *conflictAlert = [[NSAlert alloc] init]; - conflictAlert.messageText = NSLocalizedString(@"import conflict prompt", @"Title of import conflict alert"); - conflictAlert.informativeText = - [NSString stringWithFormat:NSLocalizedString(@"import conflict in %@", @"Explanation of import conflict"), - mapping.name]; - [conflictAlert addButtonWithTitle:NSLocalizedString(@"import and merge", @"button to merge imported mappings")]; - [conflictAlert addButtonWithTitle:NSLocalizedString(@"cancel import", @"button to cancel import")]; - [conflictAlert addButtonWithTitle:NSLocalizedString(@"import new mapping", @"button to import as new mapping")]; - [conflictAlert beginSheetModalForWindow:window - modalDelegate:self - didEndSelector:@selector(mappingConflictDidResolve:returnCode:contextInfo:) - contextInfo:(void *)CFBridgingRetain(@{ @"old mapping": mergeInto, - @"new mapping": mapping })]; - } else if (mergeInto) { - [mergeInto mergeEntriesFrom:mapping]; - [self activateMapping:mergeInto]; - [self mappingsChanged]; - } else { - if (idx == -1) - idx = _mappings.count; - [self.mvc.mappingList beginUpdates]; - [_mappings insertObject:mapping atIndex:idx]; - [self.mvc addedMappingAtIndex:idx startEditing:NO]; - [self.mvc.mappingList endUpdates]; - [self activateMapping:mapping]; - [self mappingsChanged]; - } - } + NJMapping *mergeInto = self[mapping.name]; + NSAlert *conflictAlert = [[NSAlert alloc] init]; + conflictAlert.messageText = NSLocalizedString(@"import conflict prompt", @"Title of import conflict alert"); + conflictAlert.informativeText = + [NSString stringWithFormat:NSLocalizedString(@"import conflict in %@", @"Explanation of import conflict"), + mapping.name]; + [conflictAlert addButtonWithTitle:NSLocalizedString(@"import and merge", @"button to merge imported mappings")]; + [conflictAlert addButtonWithTitle:NSLocalizedString(@"cancel import", @"button to cancel import")]; + [conflictAlert addButtonWithTitle:NSLocalizedString(@"import new mapping", @"button to import as new mapping")]; + [conflictAlert beginSheetModalForWindow:window + modalDelegate:self + didEndSelector:@selector(mappingConflictDidResolve:returnCode:contextInfo:) + contextInfo:(void *)CFBridgingRetain(@{ @"old mapping": mergeInto, + @"new mapping": mapping })]; } -- (NSInteger)numberOfMappings:(NJMappingsViewController *)dvc { - return _mappings.count; +- (NSInteger)numberOfMappings:(NJMappingsViewController *)mvc { + return self.count; } -- (NJMapping *)mappingsViewController:(NJMappingsViewController *)dvc +- (NJMapping *)mappingsViewController:(NJMappingsViewController *)mvc mappingForIndex:(NSUInteger)idx { - return _mappings[idx]; + return self[idx]; } - (void)mappingsViewController:(NJMappingsViewController *)mvc @@ -221,14 +238,16 @@ - (BOOL)mappingsViewController:(NJMappingsViewController *)mvc canMoveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx { - return fromIdx != toIdx && fromIdx != 0 && toIdx != 0 && toIdx < (NSInteger)_mappings.count; + return fromIdx != toIdx && fromIdx != 0 && toIdx != 0; } - (void)mappingsViewController:(NJMappingsViewController *)mvc moveMappingFromIndex:(NSInteger)fromIdx toIndex:(NSInteger)toIdx { - [_mappings moveObjectAtIndex:fromIdx toIndex:toIdx]; - [self mappingsChanged]; + [mvc.mappingList beginUpdates]; + [mvc.mappingList moveRowAtIndex:fromIdx toIndex:toIdx]; + [self moveMoveMappingFromIndex:fromIdx toIndex:toIdx]; + [mvc.mappingList endUpdates]; } - (BOOL)mappingsViewController:(NJMappingsViewController *)mvc @@ -238,14 +257,10 @@ - (void)mappingsViewController:(NJMappingsViewController *)mvc removeMappingAtIndex:(NSInteger)idx { - NJMapping *old = self[idx]; - [self.mvc.mappingList beginUpdates]; - [_mappings removeObjectAtIndex:idx]; - [self.mvc removedMappingAtIndex:idx]; - [self.mvc.mappingList endUpdates]; - if (old == _currentMapping) - [self activateMapping:self[MIN(idx, _mappings.count - 1)]]; - [self mappingsChanged]; + [mvc.mappingList beginUpdates]; + [mvc removedMappingAtIndex:idx]; + [self removeMappingAtIndex:idx]; + [mvc.mappingList endUpdates]; } - (BOOL)mappingsViewController:(NJMappingsViewController *)mvc @@ -254,18 +269,23 @@ error:(NSError **)error { NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url error:error]; - [self addOrMergeMapping:mapping atIndex:index]; + if ([self[mapping.name] hasConflictWith:mapping]) { + [self promptForMapping:mapping atIndex:index]; + } else if (self[mapping.name]) { + [self[mapping.name] mergeEntriesFrom:mapping]; + } else if (mapping) { + [self insertMapping:mapping atIndex:index]; + } return !!mapping; } - (void)mappingsViewController:(NJMappingsViewController *)mvc addMapping:(NJMapping *)mapping { - [self.mvc.mappingList beginUpdates]; - [_mappings addObject:mapping]; - [self.mvc addedMappingAtIndex:_mappings.count - 1 startEditing:YES]; - [self.mvc.mappingList endUpdates]; + [mvc.mappingList beginUpdates]; + [mvc addedMappingAtIndex:_mappings.count startEditing:YES]; + [self addMapping:mapping]; + [mvc.mappingList endUpdates]; [self activateMapping:mapping]; - [self mappingsChanged]; } - (void)mappingsViewController:(NJMappingsViewController *)mvc diff --git a/Classes/NJMappingsViewController.m b/Classes/NJMappingsViewController.m index fd9ce41..e7c28da 100644 --- a/Classes/NJMappingsViewController.m +++ b/Classes/NJMappingsViewController.m @@ -36,9 +36,6 @@ [self.delegate mappingsViewController:self moveMappingFromIndex:fromIdx toIndex:toIdx]; - [self.mappingList beginUpdates]; - [self.mappingList moveRowAtIndex:fromIdx toIndex:toIdx]; - [self.mappingList endUpdates]; [self.mappingList scrollRowToVisible:toIdx]; [self.mappingList selectRowIndexes:[[NSIndexSet alloc] initWithIndex:toIdx] byExtendingSelection:NO]; @@ -50,9 +47,6 @@ [self.delegate mappingsViewController:self moveMappingFromIndex:fromIdx toIndex:toIdx]; - [self.mappingList beginUpdates]; - [self.mappingList moveRowAtIndex:fromIdx toIndex:toIdx]; - [self.mappingList endUpdates]; [self.mappingList scrollRowToVisible:toIdx]; [self.mappingList selectRowIndexes:[[NSIndexSet alloc] initWithIndex:toIdx] byExtendingSelection:NO]; @@ -75,10 +69,8 @@ - (void)addedMappingAtIndex:(NSInteger)index startEditing:(BOOL)startEditing { [self.mappingList abortEditing]; - [self.mappingList beginUpdates]; [self.mappingList insertRowsAtIndexes:[[NSIndexSet alloc] initWithIndex:index] withAnimation:startEditing ? 0 : NSTableViewAnimationSlideLeft]; - [self.mappingList endUpdates]; if (startEditing) { [self.mappingListTrigger performClick:self]; [self.mappingList editColumn:0 row:index withEvent:nil select:YES]; @@ -88,10 +80,8 @@ - (void)removedMappingAtIndex:(NSInteger)index { [self.mappingList abortEditing]; - [self.mappingList beginUpdates]; [self.mappingList removeRowsAtIndexes:[[NSIndexSet alloc] initWithIndex:index] withAnimation:NSTableViewAnimationEffectFade]; - [self.mappingList endUpdates]; } - (void)changedActiveMappingToIndex:(NSInteger)index { @@ -145,9 +135,6 @@ NSString *value = [pboard stringForType:PB_ROW]; NSInteger srcRow = [value intValue]; row -= srcRow < row; - [self.mappingList beginUpdates]; - [self.mappingList moveRowAtIndex:srcRow toIndex:row]; - [self.mappingList endUpdates]; [self.delegate mappingsViewController:self moveMappingFromIndex:srcRow toIndex:row]; diff --git a/Info.plist b/Info.plist index 93df827..a750109 100644 --- a/Info.plist +++ b/Info.plist @@ -46,7 +46,7 @@ CFBundleSignature ???? CFBundleVersion - 483 + 502 LSApplicationCategoryType public.app-category.utilities NSHumanReadableCopyright -- 2.30.2