From 8a8af0e4a94b142ccc7e93eeef211a8625bd153d Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Sun, 14 Feb 2010 00:50:57 -0300 Subject: [PATCH] Fixed: CPArrayController could get out of bounds selections if a call to rearrangeObjects caused objects to disappear from the arranged set. --- AppKit/CPArrayController.j | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/AppKit/CPArrayController.j b/AppKit/CPArrayController.j index 236502f9e..f61bd2c26 100644 --- a/AppKit/CPArrayController.j +++ b/AppKit/CPArrayController.j @@ -209,7 +209,27 @@ - (void)rearrangeObjects { + // Rearranging reapplies the selection criteria and may cause objects to disappear, + // so take care of the selection. + // + // Sometimes rearrangeObjects is called by setContent which may cause two rounds of + // selection preservation. This is okay because setContent temporarily clears the + // selection and so this code below ends up preserving nothing in that case. + var oldSelection = nil, + oldSelectionIndexes = [[self selectionIndexes] copy]; + + if ([self preservesSelection]) + oldSelection = [self selectedObjects]; + + // Avoid out of bounds selections. + _selectionIndexes = [CPIndexSet indexSet]; + [self _setArrangedObjects:[self arrangeObjects:[self contentArray]]]; + + if (oldSelection) + [self setSelectedObjects:oldSelection]; + else + [self setSelectionIndexes:oldSelectionIndexes]; } - (void)_setArrangedObjects:(id)value