Revision: 26310
          http://sourceforge.net/p/bibdesk/svn/26310
Author:   hofman
Date:     2021-06-26 21:30:30 +0000 (Sat, 26 Jun 2021)
Log Message:
-----------
Animate undo of insert/remove bookmarks, bracket beginUpdates/endUpdates by 
observing undo begin/end

Modified Paths:
--------------
    trunk/bibdesk/BDSKBookmarkController.h
    trunk/bibdesk/BDSKBookmarkController.m
    trunk/bibdesk/BDSKSearchBookmarkController.h
    trunk/bibdesk/BDSKSearchBookmarkController.m

Modified: trunk/bibdesk/BDSKBookmarkController.h
===================================================================
--- trunk/bibdesk/BDSKBookmarkController.h      2021-06-26 18:24:05 UTC (rev 
26309)
+++ trunk/bibdesk/BDSKBookmarkController.h      2021-06-26 21:30:30 UTC (rev 
26310)
@@ -57,6 +57,7 @@
     NSUndoManager *undoManager;
     NSArray *draggedBookmarks;
     NSMutableDictionary *toolbarItems;
+    BOOL needsBeginUpdates;
 }
 
 + (id)sharedBookmarkController;

Modified: trunk/bibdesk/BDSKBookmarkController.m
===================================================================
--- trunk/bibdesk/BDSKBookmarkController.m      2021-06-26 18:24:05 UTC (rev 
26309)
+++ trunk/bibdesk/BDSKBookmarkController.m      2021-06-26 21:30:30 UTC (rev 
26310)
@@ -189,8 +189,9 @@
     NSTableViewAnimationOptions options = NSTableViewAnimationEffectGap | 
NSTableViewAnimationSlideDown;
     if (animate == NO || [self isWindowLoaded] == NO || [[self window] 
isVisible] == NO || [NSAnimationContext defaultAnimationTimeInterval] <= 0.0)
         options = NSTableViewAnimationEffectNone;
-    if (isPartial == NO)
+    if (isPartial == NO || needsBeginUpdates)
         [outlineView beginUpdates];
+    needsBeginUpdates = NO;
     [outlineView insertItemsAtIndexes:indexes inParent:OV_ITEM(parent) 
withAnimation:options];
     [parent insertChildren:newBookmarks atIndexes:indexes];
     if (isPartial == NO)
@@ -201,8 +202,9 @@
     NSTableViewAnimationOptions options = NSTableViewAnimationEffectGap | 
NSTableViewAnimationSlideUp;
     if (animate == NO || [self isWindowLoaded] == NO || [[self window] 
isVisible] == NO || [NSAnimationContext defaultAnimationTimeInterval] <= 0.0)
         options = NSTableViewAnimationEffectNone;
-    if (isPartial == NO)
+    if (isPartial == NO || needsBeginUpdates)
         [outlineView beginUpdates];
+    needsBeginUpdates = NO;
     [outlineView removeItemsAtIndexes:indexes inParent:OV_ITEM(parent) 
withAnimation:options];
     [parent removeChildrenAtIndexes:indexes];
     if (isPartial == NO)
@@ -213,8 +215,9 @@
     NSTableViewAnimationOptions options = NSTableViewAnimationEffectGap | 
NSTableViewAnimationSlideUp;
     if (animate == NO || [self isWindowLoaded] == NO || [[self window] 
isVisible] == NO || [NSAnimationContext defaultAnimationTimeInterval] <= 0.0)
         options = NSTableViewAnimationEffectNone;
-    if (isPartial == NO)
+    if (isPartial == NO || needsBeginUpdates)
         [outlineView beginUpdates];
+    needsBeginUpdates = NO;
     [outlineView removeItemsAtIndexes:indexes inParent:OV_ITEM(parent) 
withAnimation:options];
     [parent removeChildrenAtIndexes:indexes];
     indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange([indexes 
firstIndex], [newBookmarks count])];
@@ -334,9 +337,25 @@
 
 #pragma mark Undo support
 
+- (void)handleWillUndoChange:(NSNotification *)notification {
+    needsBeginUpdates = YES;
+}
+
+- (void)handleDidUndoChange:(NSNotification *)notification {
+    if (needsBeginUpdates == NO)
+        [outlineView endUpdates];
+    needsBeginUpdates = NO;
+}
+
 - (NSUndoManager *)undoManager {
-    if(undoManager == nil)
+    if(undoManager == nil) {
         undoManager = [[NSUndoManager alloc] init];
+        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+        [nc addObserver:self selector:@selector(handleWillUndoChange:) 
name:NSUndoManagerWillUndoChangeNotification object:undoManager];
+        [nc addObserver:self selector:@selector(handleWillUndoChange:) 
name:NSUndoManagerWillRedoChangeNotification object:undoManager];
+        [nc addObserver:self selector:@selector(handleDidUndoChange:) 
name:NSUndoManagerDidUndoChangeNotification object:undoManager];
+        [nc addObserver:self selector:@selector(handleDidUndoChange:) 
name:NSUndoManagerDidRedoChangeNotification object:undoManager];
+    }
     return undoManager;
 }
 
@@ -383,14 +402,12 @@
 }
 
 - (void)setBookmarks:(NSArray *)newChildren atIndexes:(NSIndexSet *)indexes 
ofBookmark:(BDSKBookmark *)bookmark {
-    [outlineView beginUpdates];
     NSIndexSet *removeIndexes = indexes ?: [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(0, [bookmark countOfChildren])];
     if ([removeIndexes count] > 0)
-        [self removeBookmarksAtIndexes:removeIndexes ofBookmark:bookmark 
partial:YES animate:NO];
+        [self removeBookmarksAtIndexes:removeIndexes ofBookmark:bookmark 
partial:YES animate:YES];
     NSIndexSet *insertIndexes = indexes ?: [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(0, [newChildren count])];
     if ([insertIndexes count] > 0)
-        [self insertBookmarks:newChildren atIndexes:insertIndexes 
ofBookmark:bookmark partial:YES animate:NO];
-    [outlineView endUpdates];
+        [self insertBookmarks:newChildren atIndexes:insertIndexes 
ofBookmark:bookmark partial:YES animate:YES];
 }
 
 #pragma mark KVO
@@ -424,13 +441,13 @@
             case NSKeyValueChangeInsertion:
                 if ([keyPath isEqualToString:CHILDREN_KEY]) {
                     [self startObservingBookmarks:newValue];
-                    [[[self undoManager] prepareWithInvocationTarget:self] 
removeBookmarksAtIndexes:indexes ofBookmark:bookmark partial:NO animate:NO];
+                    [[[self undoManager] prepareWithInvocationTarget:self] 
removeBookmarksAtIndexes:indexes ofBookmark:bookmark partial:YES animate:YES];
                 }
                 break;
             case NSKeyValueChangeRemoval:
                 if ([keyPath isEqualToString:CHILDREN_KEY]) {
                     [self stopObservingBookmarks:oldValue];
-                    [[[self undoManager] prepareWithInvocationTarget:self] 
insertBookmarks:[[oldValue copy] autorelease] atIndexes:indexes 
ofBookmark:bookmark partial:NO animate:NO];
+                    [[[self undoManager] prepareWithInvocationTarget:self] 
insertBookmarks:[[oldValue copy] autorelease] atIndexes:indexes 
ofBookmark:bookmark partial:YES animate:YES];
                 }
                 break;
             case NSKeyValueChangeReplacement:

Modified: trunk/bibdesk/BDSKSearchBookmarkController.h
===================================================================
--- trunk/bibdesk/BDSKSearchBookmarkController.h        2021-06-26 18:24:05 UTC 
(rev 26309)
+++ trunk/bibdesk/BDSKSearchBookmarkController.h        2021-06-26 21:30:30 UTC 
(rev 26310)
@@ -56,6 +56,7 @@
     NSArray *draggedBookmarks;
     NSMutableDictionary *toolbarItems;
     NSUndoManager *undoManager;
+    BOOL needsBeginUpdates;
 }
 
 + (id)sharedBookmarkController;

Modified: trunk/bibdesk/BDSKSearchBookmarkController.m
===================================================================
--- trunk/bibdesk/BDSKSearchBookmarkController.m        2021-06-26 18:24:05 UTC 
(rev 26309)
+++ trunk/bibdesk/BDSKSearchBookmarkController.m        2021-06-26 21:30:30 UTC 
(rev 26310)
@@ -154,8 +154,9 @@
     NSTableViewAnimationOptions options = NSTableViewAnimationEffectGap | 
NSTableViewAnimationSlideDown;
     if (animate == NO || [self isWindowLoaded] == NO || [[self window] 
isVisible] == NO || [NSAnimationContext defaultAnimationTimeInterval] <= 0.0)
         options = NSTableViewAnimationEffectNone;
-    if (isPartial == NO)
+    if (isPartial == NO || needsBeginUpdates)
         [outlineView beginUpdates];
+    needsBeginUpdates = NO;
     [outlineView insertItemsAtIndexes:indexes inParent:OV_ITEM(parent) 
withAnimation:options];
     [parent insertChildren:newBookmarks atIndexes:indexes];
     if (isPartial == NO)
@@ -166,8 +167,9 @@
     NSTableViewAnimationOptions options = NSTableViewAnimationEffectGap | 
NSTableViewAnimationSlideUp;
     if (animate == NO || [self isWindowLoaded] == NO || [[self window] 
isVisible] == NO || [NSAnimationContext defaultAnimationTimeInterval] <= 0.0)
         options = NSTableViewAnimationEffectNone;
-    if (isPartial == NO)
+    if (isPartial == NO || needsBeginUpdates)
         [outlineView beginUpdates];
+    needsBeginUpdates = NO;
     [outlineView removeItemsAtIndexes:indexes inParent:OV_ITEM(parent) 
withAnimation:options];
     [parent removeChildrenAtIndexes:indexes];
     if (isPartial == NO)
@@ -178,8 +180,9 @@
     NSTableViewAnimationOptions options = NSTableViewAnimationEffectGap | 
NSTableViewAnimationSlideUp;
     if (animate == NO || [self isWindowLoaded] == NO || [[self window] 
isVisible] == NO || [NSAnimationContext defaultAnimationTimeInterval] <= 0.0)
         options = NSTableViewAnimationEffectNone;
-    if (isPartial == NO)
+    if (isPartial == NO || needsBeginUpdates)
         [outlineView beginUpdates];
+    needsBeginUpdates = NO;
     [outlineView removeItemsAtIndexes:indexes inParent:OV_ITEM(parent) 
withAnimation:options];
     [parent removeChildrenAtIndexes:indexes];
     indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange([indexes 
firstIndex], [newBookmarks count])];
@@ -296,9 +299,25 @@
 
 #pragma mark Undo support
 
+- (void)handleWillUndoChange:(NSNotification *)notification {
+    needsBeginUpdates = YES;
+}
+
+- (void)handleDidUndoChange:(NSNotification *)notification {
+    if (needsBeginUpdates == NO)
+        [outlineView endUpdates];
+    needsBeginUpdates = NO;
+}
+
 - (NSUndoManager *)undoManager {
-    if(undoManager == nil)
+    if(undoManager == nil) {
         undoManager = [[NSUndoManager alloc] init];
+        NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
+        [nc addObserver:self selector:@selector(handleWillUndoChange:) 
name:NSUndoManagerWillUndoChangeNotification object:undoManager];
+        [nc addObserver:self selector:@selector(handleWillUndoChange:) 
name:NSUndoManagerWillRedoChangeNotification object:undoManager];
+        [nc addObserver:self selector:@selector(handleDidUndoChange:) 
name:NSUndoManagerDidUndoChangeNotification object:undoManager];
+        [nc addObserver:self selector:@selector(handleDidUndoChange:) 
name:NSUndoManagerDidRedoChangeNotification object:undoManager];
+    }
     return undoManager;
 }
 
@@ -335,14 +354,12 @@
 }
 
 - (void)setBookmarks:(NSArray *)newChildren atIndexes:(NSIndexSet *)indexes 
ofBookmark:(BDSKSearchBookmark *)bookmark {
-    [outlineView beginUpdates];
     NSIndexSet *removeIndexes = indexes ?: [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(0, [bookmark countOfChildren])];
     if ([removeIndexes count] > 0)
-        [self removeBookmarksAtIndexes:removeIndexes ofBookmark:bookmark 
partial:YES animate:NO];
+        [self removeBookmarksAtIndexes:removeIndexes ofBookmark:bookmark 
partial:YES animate:YES];
     NSIndexSet *insertIndexes = indexes ?: [NSIndexSet 
indexSetWithIndexesInRange:NSMakeRange(0, [newChildren count])];
     if ([insertIndexes count] > 0)
-        [self insertBookmarks:newChildren atIndexes:insertIndexes 
ofBookmark:bookmark partial:YES animate:NO];
-    [outlineView endUpdates];
+        [self insertBookmarks:newChildren atIndexes:insertIndexes 
ofBookmark:bookmark partial:YES animate:YES];
 }
 
 #pragma mark KVO
@@ -378,13 +395,13 @@
             case NSKeyValueChangeInsertion:
                 if ([keyPath isEqualToString:CHILDREN_KEY]) {
                     [self startObservingBookmarks:newValue];
-                    [[[self undoManager] prepareWithInvocationTarget:self] 
removeBookmarksAtIndexes:indexes ofBookmark:bookmark partial:NO animate:NO];
+                    [[[self undoManager] prepareWithInvocationTarget:self] 
removeBookmarksAtIndexes:indexes ofBookmark:bookmark partial:YES animate:YES];
                 }
                 break;
             case NSKeyValueChangeRemoval:
                 if ([keyPath isEqualToString:CHILDREN_KEY]) {
                     [self stopObservingBookmarks:oldValue];
-                    [[[self undoManager] prepareWithInvocationTarget:self] 
insertBookmarks:[[oldValue copy] autorelease] atIndexes:indexes 
ofBookmark:bookmark partial:NO animate:NO];
+                    [[[self undoManager] prepareWithInvocationTarget:self] 
insertBookmarks:[[oldValue copy] autorelease] atIndexes:indexes 
ofBookmark:bookmark partial:YES animate:YES];
                 }
                 break;
             case NSKeyValueChangeReplacement:

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to