Skip to content

Commit 4ccd935

Browse files
committed
Maintain the selection in the file browser when the commit selection changes
- only maintains one item, if multiple items are selected then only the first one will stay selected - if the file is removed in the new commit then don't select anything, then if a new commit is selected that does have that file it will be selected again - this does not maintain the expanded state of folders other than the one(s) that contain the selected file - in PBGitHistory.xib Tree Controller turn off: - Avoid Empty Selection - Select Inserted Objects
1 parent 6a8f495 commit 4ccd935

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

PBGitHistoryController.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
IBOutlet NSArrayController* commitController;
2424
IBOutlet NSTreeController* treeController;
2525
IBOutlet NSOutlineView* fileBrowser;
26+
NSArray *currentFileBrowserSelectionPath;
2627
IBOutlet NSTableView* commitList;
2728
IBOutlet PBCollapsibleSplitView *historySplitView;
2829
QLPreviewPanel* previewPanel;

PBGitHistoryController.m

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
@interface PBGitHistoryController ()
3030

3131
- (void) updateBranchFilterMatrix;
32+
- (void) restoreFileBrowserSelection;
33+
- (void) saveFileBrowserSelection;
3234

3335
@end
3436

@@ -89,8 +91,10 @@ - (void) updateKeys
8991

9092
selectedCommit = [[commitController selectedObjects] lastObject];
9193

92-
if (self.selectedCommitDetailsIndex == kHistoryTreeViewIndex)
94+
if (self.selectedCommitDetailsIndex == kHistoryTreeViewIndex) {
9395
self.gitTree = selectedCommit.tree;
96+
[self restoreFileBrowserSelection];
97+
}
9498
else // kHistoryDetailViewIndex
9599
self.webCommit = selectedCommit;
96100

@@ -153,15 +157,60 @@ - (void) updateStatus
153157
self.status = [NSString stringWithFormat:@"%d commits loaded", [[commitController arrangedObjects] count]];
154158
}
155159

160+
- (void) restoreFileBrowserSelection
161+
{
162+
if (self.selectedCommitDetailsIndex != kHistoryTreeViewIndex)
163+
return;
164+
165+
NSArray *children = [treeController content];
166+
if ([children count] == 0)
167+
return;
168+
169+
NSIndexPath *path = [[NSIndexPath alloc] init];
170+
if ([currentFileBrowserSelectionPath count] == 0)
171+
path = [path indexPathByAddingIndex:0];
172+
else {
173+
for (NSString *pathComponent in currentFileBrowserSelectionPath) {
174+
PBGitTree *child = nil;
175+
NSUInteger childIndex = 0;
176+
for (child in children) {
177+
if ([child.path isEqualToString:pathComponent]) {
178+
path = [path indexPathByAddingIndex:childIndex];
179+
children = child.children;
180+
break;
181+
}
182+
childIndex++;
183+
}
184+
if (!child)
185+
return;
186+
}
187+
}
188+
189+
[treeController setSelectionIndexPath:path];
190+
}
191+
192+
- (void) saveFileBrowserSelection
193+
{
194+
NSArray *objects = [treeController selectedObjects];
195+
NSArray *content = [treeController content];
196+
197+
if ([objects count] && [content count]) {
198+
PBGitTree *treeItem = [objects objectAtIndex:0];
199+
currentFileBrowserSelectionPath = [treeItem.fullPath componentsSeparatedByString:@"/"];
200+
}
201+
}
202+
156203
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
157204
{
158205
if ([(NSString *)context isEqualToString: @"commitChange"]) {
159206
[self updateKeys];
207+
[self restoreFileBrowserSelection];
160208
return;
161209
}
162210

163211
if ([(NSString *)context isEqualToString: @"treeChange"]) {
164212
[self updateQuicklookForce: NO];
213+
[self saveFileBrowserSelection];
165214
return;
166215
}
167216

PBGitHistoryView.xib

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@
5858
</object>
5959
<string key="NSObjectClassName">PBGitTree</string>
6060
<object class="_NSManagedProxy" key="_NSManagedProxy"/>
61-
<bool key="NSAvoidsEmptySelection">YES</bool>
6261
<bool key="NSPreservesSelection">YES</bool>
63-
<bool key="NSSelectsInsertedObjects">YES</bool>
6462
<string key="NSTreeContentChildrenKey">children</string>
6563
<string key="NSTreeContentLeafKey">leaf</string>
6664
</object>

0 commit comments

Comments
 (0)