Improve implementation of collapsing/expanding search results

We now collapse if more than half the items are expanded, otherwise expand. Previously we relied on a property to track the collapsed/expanded state, which could be out of sync if the user manually changed the state for half the items, and we also needed to reset the state variables when updating search results.
This commit is contained in:
Allan Odgaard
2014-08-21 15:00:08 +02:00
parent baff330459
commit ffe3a464f2
3 changed files with 23 additions and 15 deletions

View File

@@ -444,7 +444,6 @@ NSString* const FFFindWasTriggeredByEnter = @"FFFindWasTriggeredByEnter";
self.windowController.busy = YES;
self.windowController.statusString = MSG_SEARCHING_FMT;
self.windowController.showsResultsOutlineView = YES;
self.windowController.showResultsCollapsed = NO;
self.windowController.disableResultsCheckBoxes = _documentSearch.documentIdentifier != nil;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(folderSearchDidReceiveResults:) name:FFDocumentSearchDidReceiveResultsNotification object:_documentSearch];
@@ -808,6 +807,27 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si
[self openDocumentForSelectedRow:self];
}
// ===========================
// = Expand/Collapse Results =
// ===========================
- (BOOL)resultsCollapsed
{
NSUInteger expanded = 0;
for(NSDictionary* fileMatch in _matches)
expanded += [_windowController.resultsOutlineView isItemExpanded:fileMatch] ? 1 : 0;
return [_matches count] && 2 * expanded <= [_matches count];
}
- (IBAction)takeLevelToFoldFrom:(id)sender
{
if(self.resultsCollapsed)
[_windowController.resultsOutlineView expandItem:nil expandChildren:YES];
else [_windowController.resultsOutlineView collapseItem:nil collapseChildren:YES];
[_windowController.resultsOutlineView setNeedsDisplay:YES];
}
// ==================
// = Go to… Submenu =
// ==================
@@ -905,6 +925,8 @@ static NSAttributedString* AttributedStringForMatch (std::string const& text, si
return self.performedReplaceAll && !self.performedSaveAll;
else if(aMenuItem.action == @selector(saveDocument:) || aMenuItem.action == @selector(saveDocumentAs:))
return NO;
else if(aMenuItem.action == @selector(takeLevelToFoldFrom:) && aMenuItem.tag == -1)
[aMenuItem setTitle:self.resultsCollapsed ? @"Expand Results" : @"Collapse Results"];
return YES;
}
@end

View File

@@ -13,7 +13,6 @@ extern NSString* const FFSearchInOpenFiles;
@property (nonatomic) BOOL showsResultsOutlineView;
@property (nonatomic) BOOL disableResultsCheckBoxes;
@property (nonatomic) BOOL showResultsCollapsed;
@property (nonatomic) BOOL showReplacementPreviews;
@property (nonatomic) NSString* projectFolder;

View File

@@ -790,14 +790,6 @@ static NSButton* OakCreateStopSearchButton ()
[self.resultsOutlineView setOutlineTableColumn:[self.resultsOutlineView tableColumnWithIdentifier:flag ? @"match" : @"checkbox"]];
}
- (void)setShowResultsCollapsed:(BOOL)flag
{
if(_showResultsCollapsed = flag)
[self.resultsOutlineView collapseItem:nil collapseChildren:YES];
else [self.resultsOutlineView expandItem:nil expandChildren:YES];
[self.resultsOutlineView setNeedsDisplay:YES];
}
- (void)setShowReplacementPreviews:(BOOL)flag
{
if(_showReplacementPreviews != flag)
@@ -922,7 +914,6 @@ static NSButton* OakCreateStopSearchButton ()
- (IBAction)toggleFollowSymbolicLinks:(id)sender { self.followSymbolicLinks = !self.followSymbolicLinks; }
- (IBAction)toggleSearchHiddenFolders:(id)sender { self.searchHiddenFolders = !self.searchHiddenFolders; }
- (IBAction)takeLevelToFoldFrom:(id)sender { self.showResultsCollapsed = !self.showResultsCollapsed; }
- (IBAction)selectNextResult:(id)sender
{
@@ -958,8 +949,6 @@ static NSButton* OakCreateStopSearchButton ()
if([self.resultsOutlineView numberOfRows] == 0)
return;
self.showResultsCollapsed = NO;
NSInteger row = [self.resultsOutlineView selectedRow];
if(row == -1)
row = [self.resultsOutlineView numberOfRows];
@@ -1077,8 +1066,6 @@ static NSButton* OakCreateStopSearchButton ()
[aMenuItem setState:self.followSymbolicLinks ? NSOnState : NSOffState];
else if(aMenuItem.action == @selector(toggleSearchHiddenFolders:))
[aMenuItem setState:self.searchHiddenFolders ? NSOnState : NSOffState];
else if(aMenuItem.action == @selector(takeLevelToFoldFrom:) && aMenuItem.tag == -1)
[aMenuItem setTitle:self.showResultsCollapsed ? @"Expand Results" : @"Collapse Results"];
else if(aMenuItem.action == @selector(goToParentFolder:))
res = self.searchFolder != nil;
return res;