From b7bdec189ae24250621bf26531e6de577d2bddf4 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 14 Jan 2011 12:10:52 -0300 Subject: [PATCH] Fixed: when an outline view node was expanded, selections below it would not stick to their items if the newly expanded node also had previously expanded children. --- AppKit/CPOutlineView.j | 18 ++++++++++-------- Tests/AppKit/CPOutlineViewTest.j | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/AppKit/CPOutlineView.j b/AppKit/CPOutlineView.j index 7e36d7671..67c7a6943 100644 --- a/AppKit/CPOutlineView.j +++ b/AppKit/CPOutlineView.j @@ -349,9 +349,16 @@ var CPOutlineViewCoalesceSelectionNotificationStateOff = 0, { [self _noteItemWillExpand:anItem]; + var previousRowCount = [self numberOfRows]; + + itemInfo.isExpanded = YES; + // XXX Shouldn't the items reload before the notification is sent? + [self _noteItemDidExpand:anItem]; + [self reloadItem:anItem reloadChildren:YES]; + // Shift selection indexes below so that the same items remain selected. - var newRowCount = [_outlineViewDataSource outlineView:self numberOfChildrenOfItem:anItem]; - if (newRowCount) + var rowCountDelta = [self numberOfRows] - previousRowCount; + if (rowCountDelta) { var selection = [self selectedRowIndexes], expandIndex = [self rowForItem:anItem] + 1; @@ -359,15 +366,10 @@ var CPOutlineViewCoalesceSelectionNotificationStateOff = 0, if ([selection intersectsIndexesInRange:CPMakeRange(expandIndex, _itemsForRows.length)]) { [self _noteSelectionIsChanging]; - [selection shiftIndexesStartingAtIndex:expandIndex by:newRowCount]; + [selection shiftIndexesStartingAtIndex:expandIndex by:rowCountDelta]; [self _setSelectedRowIndexes:selection]; // _noteSelectionDidChange will be suppressed. } } - - itemInfo.isExpanded = YES; - // XXX Shouldn't the items reload before the notification is sent? - [self _noteItemDidExpand:anItem]; - [self reloadItem:anItem reloadChildren:YES]; } if (shouldExpandChildren) diff --git a/Tests/AppKit/CPOutlineViewTest.j b/Tests/AppKit/CPOutlineViewTest.j index 21227e434..7bac8a20d 100644 --- a/Tests/AppKit/CPOutlineViewTest.j +++ b/Tests/AppKit/CPOutlineViewTest.j @@ -138,6 +138,29 @@ [self assert:1 equals:[delegate selectionChangeCount] message:"selection notifications during expandItem"]; } +/*! + Test selection updates when an expanded node has pre-expanded children. +*/ +- (void)testExpandWithSelectionBelowAndExpandedChildren +{ + // [".1", ".1.1", ".1.2", ".1.2.1", ".1.2.2", ".2", ".3", ".3.1"] + [outlineView collapseItem:".1"]; + + var preSelection = [CPIndexSet indexSet]; + [preSelection addIndex:[outlineView rowForItem:".2"]]; + [preSelection addIndex:[outlineView rowForItem:".3.1"]]; + + [outlineView selectRowIndexes:preSelection byExtendingSelection:NO]; + var delegate = [TestNotificationsDelegate new]; + [delegate setTester:self]; + [delegate setExpectedSelectedItems:[".2", ".3.1"]]; + [outlineView setDelegate:delegate]; + + [outlineView expandItem:".1"]; + // The delegate will check the selection update but not the count. + [self assert:2 equals:[[outlineView selectedRowIndexes] count] message:"selections should remain"]; +} + /*! Test that the outline view is properly careful about not encoding its non-encodable delegate and data source.