From 1199c56e932a91bd4bd4154f47635787ef3bb893 Mon Sep 17 00:00:00 2001 From: Pieter de Bie Date: Tue, 16 Jun 2009 18:28:37 +0100 Subject: [PATCH] HistoryController: Use a programatically created context menu This allows us to differentiate between one or multiple files, and will also come in use when we want to create a context menu from the webview later on We now use representedObjects to store the selected paths. That should make it possible to reuse the menu's from somewhere else. --- PBGitHistoryController.h | 4 ++ PBGitHistoryController.m | 36 +++++++++++++++-- PBGitHistoryView.xib | 83 +++------------------------------------- PBQLOutlineView.m | 2 +- 4 files changed, 42 insertions(+), 83 deletions(-) diff --git a/PBGitHistoryController.h b/PBGitHistoryController.h index 0007760..f7298c4 100644 --- a/PBGitHistoryController.h +++ b/PBGitHistoryController.h @@ -41,6 +41,10 @@ - (IBAction) toggleQuickView: sender; - (IBAction) openSelectedFile: sender; - (void) updateQuicklookForce: (BOOL) force; + +// Context menu methods +- (NSMenu *)contextMenuForTreeView; +- (NSArray *)menuItemsForPaths:(NSArray *)paths; - (void)showCommitsFromTree:(id)sender; - (void)showInFinderAction:(id)sender; diff --git a/PBGitHistoryController.m b/PBGitHistoryController.m index 00a09c7..a02c041 100644 --- a/PBGitHistoryController.m +++ b/PBGitHistoryController.m @@ -226,8 +226,8 @@ // TODO: Enable this from webview as well! NSMutableArray *filePaths = [NSMutableArray arrayWithObjects:@"HEAD", @"--", NULL]; - for (PBGitTree *tree in [treeController selectedObjects]) - [filePaths addObject:[tree fullPath]]; + [filePaths addObjectsFromArray:[sender representedObject]]; + PBGitRevSpecifier *revSpec = [[PBGitRevSpecifier alloc] initWithParameters:filePaths]; repository.currentBranch = [repository addBranch:revSpec]; @@ -239,12 +239,40 @@ NSString *path; NSWorkspace *ws = [NSWorkspace sharedWorkspace]; - for (PBGitTree *tree in [treeController selectedObjects]) { - path = [workingDirectory stringByAppendingPathComponent:[tree fullPath]]; + for (NSString *filePath in [sender representedObject]) { + path = [workingDirectory stringByAppendingPathComponent:filePath]; [ws selectFile: path inFileViewerRootedAtPath:path]; } } +- (NSMenu *)contextMenuForTreeView +{ + NSArray *filePaths = [[treeController selectedObjects] valueForKey:@"fullPath"]; + + NSMenu *menu = [[NSMenu alloc] init]; + for (NSMenuItem *item in [self menuItemsForPaths:filePaths]) + [menu addItem:item]; + return menu; +} + +- (NSArray *)menuItemsForPaths:(NSArray *)paths +{ + BOOL multiple = [paths count] != 1; + NSMenuItem *finderItem = [[NSMenuItem alloc] initWithTitle:multiple? @"Show items in Finder" : @"Show item in Finder" + action:@selector(showInFinderAction:) + keyEquivalent:@""]; + NSMenuItem *historyItem = [[NSMenuItem alloc] initWithTitle:multiple? @"Show history of files" : @"Show history of file" + action:@selector(showCommitsFromTree:) + keyEquivalent:@""]; + + NSArray *menuItems = [NSArray arrayWithObjects:historyItem, finderItem, nil]; + for (NSMenuItem *item in menuItems) { + [item setTarget:self]; + [item setRepresentedObject:paths]; + } + + return menuItems; +} @end diff --git a/PBGitHistoryView.xib b/PBGitHistoryView.xib index 6e44446..329d003 100644 --- a/PBGitHistoryView.xib +++ b/PBGitHistoryView.xib @@ -8,9 +8,8 @@ 353.00 YES - - + YES @@ -1518,28 +1517,6 @@ YES - - - - YES - - - Show history of file(s) - - 2147483647 - - - - - - Show in Finder - - 2147483647 - - - - - @@ -2130,30 +2107,6 @@ 274 - - - showCommitsFromTree: - - - - 281 - - - - menu - - - - 282 - - - - showInFinderAction: - - - - 285 - @@ -2784,26 +2737,6 @@ - - 276 - - - YES - - - - - - - 275 - - - - - 284 - - - @@ -2883,12 +2816,8 @@ 27.ImportedFromIB2 272.IBPluginDependency 273.IBPluginDependency - 275.IBPluginDependency - 276.IBEditorWindowLastContentRect - 276.IBPluginDependency 28.IBPluginDependency 28.IBShouldRemoveOnLegacySave - 284.IBPluginDependency 29.IBPluginDependency 29.IBShouldRemoveOnLegacySave 3.IBPluginDependency @@ -3018,12 +2947,8 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{225, 683}, {202, 43}} - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -3055,7 +2980,7 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - {{76, 44}, {852, 432}} + {{358, 67}, {852, 432}} com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -3090,7 +3015,7 @@ - 285 + 286 @@ -3154,6 +3079,7 @@ commitList fileBrowser searchField + treeContextMenu treeController webView @@ -3163,6 +3089,7 @@ NSTableView NSOutlineView NSSearchField + NSMenu NSTreeController id diff --git a/PBQLOutlineView.m b/PBQLOutlineView.m index 24f43e1..459d908 100644 --- a/PBQLOutlineView.m +++ b/PBQLOutlineView.m @@ -74,7 +74,7 @@ [self selectRow:row byExtendingSelection:NO]; } - return [super menuForEvent:theEvent]; + return [controller contextMenuForTreeView]; } /* Implemented to satisfy datasourcee protocol */