Commits

Peter Hosey committed 6a17c62

First draft of saner selection validation.

Comments (0)

Files changed (1)

 	return ([item isKindOfClass:[RipChapter class]] && (item != chapterOfUnchapteredTracks));
 }
 
-- (BOOL)outlineView:(NSOutlineView *)outlineView shouldSelectItem:(id)item {
-	if (![item isKindOfClass:[RippableTrack class]])
-		return NO;
-	RippableTrack *track = item;
+- (NSIndexSet *)outlineView:(NSOutlineView *)outlineView selectionIndexesForProposedSelection:(NSIndexSet *)proposedSelectionIndexes {
+	NSMutableIndexSet *revisedSelectionIndexes = [[proposedSelectionIndexes mutableCopy] autorelease];
 
-	RipChapter *chapterOfExistingSelection = [self chapterOfSelectedTracks];
-	if (!chapterOfExistingSelection) {
-		//The user can always start a selection when none previously existed.
-		return YES;
-	}
+	__block RipChapter *chapterOfOtherSelectedTracks = nil;
+	__block NSUInteger previousIndex = NSNotFound;
 
-	RipChapter *chapter = [outlineView parentForItem:track];
-	if (chapter != chapterOfExistingSelection) {
-		//Don't allow selecting tracks in a different chapter than the already-selected tracks.
-		return NO;
-	}
+	[proposedSelectionIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
+		id item = [outlineView itemAtRow:idx];
 
-	NSUInteger indexOfProposedSelection = [chapter.sourceTracks indexOfObjectIdenticalTo:track];
+		if (![item isKindOfClass:[RippableTrack class]]) {
+			[revisedSelectionIndexes removeIndex:idx];
+		} else {
+			RippableTrack *track = item;
+			RipChapter *chapter = [outlineView parentForItem:track];
+			if (chapterOfOtherSelectedTracks && (chapter != chapterOfOtherSelectedTracks)) {
+				[revisedSelectionIndexes removeIndex:idx];
+			} else {
+				chapterOfOtherSelectedTracks = chapter;
+				if ((previousIndex != NSNotFound) && (idx != (previousIndex + 1UL))) {
+					//Contiguous selections only.
+					[revisedSelectionIndexes removeIndex:idx];
+				}
+				previousIndex = idx;
+			}
+		}
+	}];
 
-	//If selecting this row would extend the selection either up or down contiguously, allow it.
-	NSIndexSet *alreadySelectedTrackIndexes = [self indexesOfSelectionWithinChapter];
-	NSUInteger firstAlreadySelectedTrackIndex = [alreadySelectedTrackIndexes firstIndex];
-	if (firstAlreadySelectedTrackIndex > 0) {
-		if (indexOfProposedSelection == (firstAlreadySelectedTrackIndex - 1))
-			return YES;
-	}
-	NSUInteger lastAlreadySelectedTrackIndex = [alreadySelectedTrackIndexes lastIndex];
-	if (lastAlreadySelectedTrackIndex < ([chapter countOfSourceTracks] - 1)) {
-		if (indexOfProposedSelection == (lastAlreadySelectedTrackIndex + 1))
-			return YES;
-	}
-
-	return NO;
+	return revisedSelectionIndexes;
 }
 
 - (BOOL) outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard {