From 0f90dd3b3ea9c97897c56554ab87d677f514b788 Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Thu, 20 Jan 2022 21:48:23 -0800 Subject: [PATCH] Playlist View: Support undoing and redoing Drag and Drop operations within the playlist --- Playlist/DNDArrayController.h | 3 +++ Playlist/DNDArrayController.m | 23 +++++++++++++++++++++++ Playlist/PlaylistController.m | 20 ++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/Playlist/DNDArrayController.h b/Playlist/DNDArrayController.h index a10b0d4a3..05f8fdf4d 100755 --- a/Playlist/DNDArrayController.h +++ b/Playlist/DNDArrayController.h @@ -28,5 +28,8 @@ extern NSString *iTunesDropType; // utility methods -(void)moveObjectsInArrangedObjectsFromIndexes:(NSIndexSet *)indexSet toIndex:(NSUInteger)insertIndex; +// This is needed to undo the above +- (void)moveObjectsFromIndex:(NSUInteger)fromIndex + toArrangedObjectIndexes:(NSIndexSet *)indexSet; @end diff --git a/Playlist/DNDArrayController.m b/Playlist/DNDArrayController.m index 3b74ae6fa..50875e6cb 100755 --- a/Playlist/DNDArrayController.m +++ b/Playlist/DNDArrayController.m @@ -119,5 +119,28 @@ NSString *iTunesDropType = @"com.apple.tv.metadata"; } } +- (void)moveObjectsFromIndex:(NSUInteger)fromIndex + toArrangedObjectIndexes:(NSIndexSet *)indexSet { + NSArray *objects = [self arrangedObjects]; + NSUInteger index = [indexSet firstIndex]; + + NSUInteger itemIndex = 0; + id object; + + NSArray *itemsSet = [objects objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(fromIndex, [indexSet count])]]; + + for (NSUInteger i = 0; i < [itemsSet count]; i++) { + [self removeObjectAtArrangedObjectIndex:fromIndex]; + } + + while (NSNotFound != index) { + object = itemsSet[itemIndex++]; + + [self insertObject:object atArrangedObjectIndex:index]; + + index = [indexSet indexGreaterThanIndex:index]; + } +} + @end diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index 49a53de91..b4bb516e2 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -363,8 +363,28 @@ static inline void dispatch_sync_reentrant(dispatch_queue_t queue, dispatch_bloc if ([self shuffle] != ShuffleOff) [self resetShuffleList]; } +// This action is only needed to revert the one that follows it +- (void)moveObjectsFromIndex:(NSUInteger)fromIndex + toArrangedObjectIndexes:(NSIndexSet *)indexSet { + [[[self undoManager] prepareWithInvocationTarget:self] + moveObjectsInArrangedObjectsFromIndexes:indexSet toIndex:fromIndex]; + NSString *actionName = + [NSString stringWithFormat:@"Reordering %lu entries", (unsigned long) [indexSet count]]; + [[self undoManager] setActionName:actionName]; + + [super moveObjectsFromIndex:fromIndex toArrangedObjectIndexes:indexSet]; + + [playbackController playlistDidChange:self]; +} + - (void)moveObjectsInArrangedObjectsFromIndexes:(NSIndexSet *)indexSet toIndex:(NSUInteger)insertIndex { + [[[self undoManager] prepareWithInvocationTarget:self] + moveObjectsFromIndex:insertIndex toArrangedObjectIndexes:indexSet]; + NSString *actionName = + [NSString stringWithFormat:@"Reordering %lu entries", (unsigned long) [indexSet count]]; + [[self undoManager] setActionName:actionName]; + [super moveObjectsInArrangedObjectsFromIndexes:indexSet toIndex:insertIndex]; #if 0 // syncPlaylistEntries is already called for rearrangement