Tweak some animations, clean up for preparation to move to app delegate.
authorJoe Wreschnig <joe.wreschnig@gmail.com>
Mon, 18 Mar 2013 00:19:19 +0000 (01:19 +0100)
committerJoe Wreschnig <joe.wreschnig@gmail.com>
Mon, 18 Mar 2013 00:19:19 +0000 (01:19 +0100)
Classes/EnjoyableApplicationDelegate.m
Classes/NJDeviceViewController.m
Classes/NJMappingsController.h
Classes/NJMappingsController.m
Classes/NJMappingsViewController.m
Info.plist

index 26ea09cdf3321660eaceef0cc96dce8e03d155e5..9fef7c378357aebc223570ee477bbb9c24da66c0 100644 (file)
     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 {
                       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
index dfb2dcad72aac10328c24a04ac36e18d77d0e736..f9ff141d1e53a0c01e91d8478f397469e74623af 100644 (file)
@@ -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;
 }
index 39aaf79ba802c0b6d35e8014f90039b231383568..85ba54ff873e1b14b13eed9d0ff2fdf2bd86b7b1 100644 (file)
                                             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;
 
index 9c7b641e848ab9e7eec9b7d611a4d89670272afe..2b08a52a3db05228e687b96a89d1e4e504b02d47 100644 (file)
     }
 }
 
+- (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 {
     [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
 - (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
 
 - (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
                          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
index fd9ce4175a4508fd92429a2a91ac197b205ee286..e7c28dab189a82475aada87e8aff7f6d3fa8a51f 100644 (file)
@@ -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];
 
 - (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];
 
 - (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 {
         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];
index 93df827d3b62f030bdc40a496e5a58052dad2c82..a750109be106e085aed3a9c96ceb0e1d6dccf386 100644 (file)
@@ -46,7 +46,7 @@
        <key>CFBundleSignature</key>
        <string>????</string>
        <key>CFBundleVersion</key>
-       <string>483</string>
+       <string>502</string>
        <key>LSApplicationCategoryType</key>
        <string>public.app-category.utilities</string>
        <key>NSHumanReadableCopyright</key>