Use menu delegate for the file browser’s context menu

This simplifies the code (API) and also allows us to skip having to select the clicked item, as the outline view will handle this for us (using a “special” selection indication that can work in parallel with regular selection).
This commit is contained in:
Allan Odgaard
2015-07-20 12:08:32 +02:00
parent f942869bee
commit 37ed461ca6
3 changed files with 7 additions and 29 deletions

View File

@@ -103,7 +103,7 @@ static NSImage* IconImage (NSURL* url, NSSize size = NSMakeSize(16, 16))
}
@end
@interface OakFileBrowser () <OFBOutlineViewMenuDelegate, NSMenuDelegate>
@interface OakFileBrowser () <NSMenuDelegate>
{
OBJC_WATCH_LEAKS(OakFileBrowser);
OFBHeaderView* _headerView;
@@ -197,7 +197,9 @@ static NSMutableSet* SymmetricDifference (NSMutableSet* aSet, NSMutableSet* anot
_outlineView.target = self;
_outlineView.action = @selector(didSingleClickOutlineView:);
_outlineView.doubleAction = @selector(didDoubleClickOutlineView:);
_outlineView.menuDelegate = self;
_outlineView.menu = [NSMenu new];
_outlineView.menu.delegate = self;
if([[[[NSUserDefaults standardUserDefaults] objectForKey:kUserDefaultsFileBrowserStyleKey] lowercaseString] isEqualToString:@"sourcelist"])
_outlineView.renderAsSourceList = YES;
@@ -997,13 +999,6 @@ static NSMutableSet* SymmetricDifference (NSMutableSet* aSet, NSMutableSet* anot
[aMenu addItemWithTitle:@"No available actions" action:@selector(nop:) keyEquivalent:@""];
}
- (NSMenu*)menuForOutlineView:(NSOutlineView*)anOutlineView
{
NSMenu* menu = [NSMenu new];
[self updateMenu:menu];
return menu;
}
- (BOOL)menuHasKeyEquivalent:(NSMenu*)aMenu forEvent:(NSEvent*)anEvent target:(id*)anId action:(SEL*)aSEL
{
static std::string const keys[] = { "@N", "^@n", "@A" };
@@ -1031,7 +1026,8 @@ static NSMutableSet* SymmetricDifference (NSMutableSet* aSet, NSMutableSet* anot
- (void)menuNeedsUpdate:(NSMenu*)aMenu
{
[aMenu removeAllItems];
[aMenu addItemWithTitle:@"Dummy" action:NULL keyEquivalent:@""];
if(aMenu == _actionsView.actionsPopUpButton.menu)
[aMenu addItemWithTitle:@"Dummy" action:NULL keyEquivalent:@""];
[self updateMenu:aMenu];
}

View File

@@ -1,11 +1,6 @@
#import <oak/debug.h>
@protocol OFBOutlineViewMenuDelegate
- (NSMenu*)menuForOutlineView:(NSOutlineView*)anOutlineView;
@end
@interface OFBOutlineView : NSOutlineView
@property (nonatomic, weak) id <OFBOutlineViewMenuDelegate> menuDelegate;
@property (nonatomic) BOOL renderAsSourceList;
@property (nonatomic) BOOL recursiveRequest;
- (void)performEditSelectedRow:(id)sender;

View File

@@ -108,7 +108,7 @@
- (void)showContextMenu:(id)sender
{
if(NSMenu* menu = [self.menuDelegate menuForOutlineView:self])
if(NSMenu* menu = [self menuForEvent:[NSApp currentEvent]])
{
NSInteger row = [self selectedRow] != -1 ? [self selectedRow] : 0;
NSRect rect = [self convertRect:[self rectOfRow:row] toView:nil];
@@ -132,19 +132,6 @@
}
}
- (NSMenu*)menuForEvent:(NSEvent*)theEvent
{
if(!self.menuDelegate)
return [super menuForEvent:theEvent];
NSInteger row = [self rowAtPoint:[self convertPoint:[theEvent locationInWindow] fromView:nil]];
if(row != -1 && [self.delegate respondsToSelector:@selector(outlineView:shouldSelectItem:)] && ![self.delegate outlineView:self shouldSelectItem:[self itemAtRow:row]])
row = -1;
if(row != -1 && ![self.selectedRowIndexes containsIndex:row])
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
return [self.menuDelegate menuForOutlineView:self];
}
// =============================
// = Accepting First Responder =
// =============================