From ffe3a464f2ad02e01e7d4d8fe80a762fecfaed18 Mon Sep 17 00:00:00 2001 From: Allan Odgaard Date: Thu, 21 Aug 2014 15:00:08 +0200 Subject: [PATCH] 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. --- Frameworks/Find/src/Find.mm | 24 ++++++++++++++++++++- Frameworks/Find/src/FindWindowController.h | 1 - Frameworks/Find/src/FindWindowController.mm | 13 ----------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/Frameworks/Find/src/Find.mm b/Frameworks/Find/src/Find.mm index fa5744ff..8a9026f2 100644 --- a/Frameworks/Find/src/Find.mm +++ b/Frameworks/Find/src/Find.mm @@ -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 diff --git a/Frameworks/Find/src/FindWindowController.h b/Frameworks/Find/src/FindWindowController.h index 28a5f173..709c4038 100644 --- a/Frameworks/Find/src/FindWindowController.h +++ b/Frameworks/Find/src/FindWindowController.h @@ -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; diff --git a/Frameworks/Find/src/FindWindowController.mm b/Frameworks/Find/src/FindWindowController.mm index 4f215619..031868ca 100644 --- a/Frameworks/Find/src/FindWindowController.mm +++ b/Frameworks/Find/src/FindWindowController.mm @@ -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;