From ea524c42e5d8391ec5559b739faca661de96cd62 Mon Sep 17 00:00:00 2001 From: Kent Sutherland Date: Wed, 10 Jul 2013 01:24:31 -0400 Subject: [PATCH] Added "Open Submodule" menu item to contextual menu of submodules. A bit more discoverable than double-clicking the submodule row. --- Classes/Controllers/PBGitSidebarController.m | 54 ++++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/Classes/Controllers/PBGitSidebarController.m b/Classes/Controllers/PBGitSidebarController.m index d7ed42b61..1b6366b02 100644 --- a/Classes/Controllers/PBGitSidebarController.m +++ b/Classes/Controllers/PBGitSidebarController.m @@ -205,6 +205,24 @@ - (void)setHistorySearch:(NSString *)searchString mode:(NSInteger)mode [historyViewController.searchController setHistorySearch:searchString mode:mode]; } +- (void) openSubmoduleFromMenuItem:(NSMenuItem *)menuItem +{ + [self openSubmodule:[menuItem representedObject]]; +} + +- (void) openSubmodule:(PBGitSubmodule *)submodule +{ + NSURL *submoduleURL = [NSURL fileURLWithPath:[submodule path]]; + + if (submoduleURL) { + [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:submoduleURL display:YES completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError *error) { + if (error) { + [NSApp presentError:error]; + } + }]; + } +} + #pragma mark NSOutlineView delegate methods - (void)outlineViewSelectionDidChange:(NSNotification *)notification @@ -233,12 +251,7 @@ - (void)doubleClicked:(id)object { if ([[sourceView itemAtRow:rowNumber] isKindOfClass:[PBGitSVSubmoduleItem class]]) { PBGitSVSubmoduleItem *subModule = [sourceView itemAtRow:rowNumber]; - NSURL *url = [NSURL fileURLWithPath:subModule.submodule.path]; - [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:url - display:YES - error:nil]; - - ; + [self openSubmodule:[subModule submodule]]; } } @@ -354,7 +367,7 @@ - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTabl - (void) updateActionMenu { - [actionButton setEnabled:([[self selectedItem] ref] != nil)]; + [actionButton setEnabled:([[self selectedItem] ref] != nil || [[self selectedItem] isKindOfClass:[PBGitSVSubmoduleItem class]])]; } - (void) addMenuItemsForRef:(PBGitRef *)ref toMenu:(NSMenu *)menu @@ -366,6 +379,17 @@ - (void) addMenuItemsForRef:(PBGitRef *)ref toMenu:(NSMenu *)menu [menu addItem:menuItem]; } +- (void) addMenuItemsForSubmodule:(PBGitSubmodule *)submodule toMenu:(NSMenu *)menu +{ + if (!submodule) + return; + + NSMenuItem *menuItem = [menu addItemWithTitle:NSLocalizedString(@"Open Submodule", @"Open Submodule menu item") action:@selector(openSubmoduleFromMenuItem:) keyEquivalent:@""]; + + [menuItem setTarget:self]; + [menuItem setRepresentedObject:submodule]; +} + - (NSMenuItem *) actionIconItem { NSMenuItem *actionIconItem = [[NSMenuItem alloc] initWithTitle:@"" action:NULL keyEquivalent:@""]; @@ -379,15 +403,21 @@ - (NSMenuItem *) actionIconItem - (NSMenu *) menuForRow:(NSInteger)row { PBSourceViewItem *viewItem = [sourceView itemAtRow:row]; - PBGitRef *ref = [viewItem ref]; - if (!ref) + PBGitSubmodule *submodule = nil; + + if ([viewItem isKindOfClass:[PBGitSVSubmoduleItem class]]) { + submodule = [(PBGitSVSubmoduleItem *)viewItem submodule]; + } + + if (!ref && !submodule) return nil; NSMenu *menu = [[NSMenu alloc] init]; [menu setAutoenablesItems:NO]; [self addMenuItemsForRef:ref toMenu:menu]; - + [self addMenuItemsForSubmodule:submodule toMenu:menu]; + return menu; } @@ -399,6 +429,10 @@ - (void) menuNeedsUpdate:(NSMenu *)menu PBGitRef *ref = [[self selectedItem] ref]; [self addMenuItemsForRef:ref toMenu:menu]; + + if ([[self selectedItem] isKindOfClass:[PBGitSVSubmoduleItem class]]) { + [self addMenuItemsForSubmodule:[(PBGitSVSubmoduleItem *)[self selectedItem] submodule] toMenu:menu]; + } }