Move “click to open” logic to cell

This will make it easier to change the logic, since 3 different parts of the file browser depend on it: accepting first responder, selecting items, and single-clicking items.
This commit is contained in:
Allan Odgaard
2013-10-25 22:57:39 +02:00
parent 1d37649c10
commit 44cdbc57c6
5 changed files with 31 additions and 47 deletions

View File

@@ -431,11 +431,9 @@ static NSSet* VisibleItems (NSOutlineView* outlineView, FSItem* root, NSMutableS
NSInteger row = [anOutlineView clickedRow];
if(col != -1 && row != -1)
{
OFBPathInfoCell* cell = (OFBPathInfoCell*)[anOutlineView preparedCellAtColumn:col row:row];
NSInteger hit = [cell hitTestForEvent:[NSApp currentEvent] inRect:[anOutlineView frameOfCellAtColumn:col row:row] ofView:anOutlineView];
if(hit & OakImageAndTextCellHitImage)
return NO;
else if(hit & NSCellHitTrackableArea)
NSCell* cell = [anOutlineView preparedCellAtColumn:col row:row];
NSUInteger hit = [cell hitTestForEvent:[NSApp currentEvent] inRect:[anOutlineView frameOfCellAtColumn:col row:row] ofView:anOutlineView];
if(hit & (OFBPathInfoCellHitOpenItem | OFBPathInfoCellHitRevealItem | NSCellHitTrackableArea))
return NO;
}
return YES;

View File

@@ -1082,21 +1082,15 @@ static NSMutableSet* SymmetricDifference (NSMutableSet* aSet, NSMutableSet* anot
NSInteger row = [_outlineView clickedRow];
NSInteger col = [_outlineView clickedColumn];
col = row != -1 && col == -1 ? 0 : col; // Clicking a row which participates in multi-row selection causes clickedColumn to return -1 <rdar://10382268>
OFBPathInfoCell* cell = (OFBPathInfoCell*)[_outlineView preparedCellAtColumn:col row:row];
NSInteger hit = [cell hitTestForEvent:[NSApp currentEvent] inRect:[_outlineView frameOfCellAtColumn:col row:row] ofView:_outlineView];
if(hit & OakImageAndTextCellHitImage)
{
NSURL* itemURL = ((FSItem*)[_outlineView itemAtRow:row]).url;
if(([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask) && [itemURL isFileURL])
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[ itemURL ]];
else [self didDoubleClickOutlineView:sender];
}
NSCell* cell = [_outlineView preparedCellAtColumn:col row:row];
NSUInteger hit = [cell hitTestForEvent:[NSApp currentEvent] inRect:[_outlineView frameOfCellAtColumn:col row:row] ofView:_outlineView];
FSItem* item = [_outlineView itemAtRow:row];
if((hit & OFBPathInfoCellHitRevealItem) && [item.url isFileURL])
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[ item.url ]];
else if(hit & (OFBPathInfoCellHitOpenItem | OFBPathInfoCellHitRevealItem))
[self didDoubleClickOutlineView:sender];
else if(hit & OFBPathInfoCellHitCloseButton)
{
FSItem* item = [_outlineView itemAtRow:row];
[_delegate fileBrowser:self closeURL:item.url];
}
}
- (IBAction)reload:(id)sender

View File

@@ -26,10 +26,6 @@
@property (nonatomic, retain) NSIndexSet* draggedRows;
- (void)performDoubleClick:(id)sender;
- (BOOL)isPointInImage:(NSPoint)point;
- (BOOL)isPointInText:(NSPoint)aPoint;
- (BOOL)isPointInCloseButton:(NSPoint)aPoint;
@end
@implementation OFBOutlineView
@@ -142,20 +138,21 @@
- (BOOL)shouldActivate
{
NSEvent* event = [NSApp currentEvent];
BOOL res = [event type] != NSLeftMouseDown || ([event modifierFlags] & (NSShiftKeyMask | NSControlKeyMask | NSAlternateKeyMask));
id firstResponder = [[self window] firstResponder];
res = res || ([firstResponder respondsToSelector:@selector(delegate)] && [firstResponder delegate] == self);
res = res || fieldEditorWasUp;
if(res)
if(([firstResponder respondsToSelector:@selector(delegate)] && [firstResponder delegate] == self) || fieldEditorWasUp)
return YES;
NSPoint p = [self convertPoint:[event locationInWindow] fromView:nil];
if([self isPointInImage:p] || [self isPointInCloseButton:p])
return NO; // Dont activate when clicking an image to open a document
NSEvent* event = [NSApp currentEvent];
if([event type] != NSLeftMouseDown)
return YES;
return [self isRowSelected:[self rowAtPoint:p]] && (event.modifierFlags & NSCommandKeyMask) == 0;
NSInteger row = [self rowAtPoint:[self convertPoint:[event locationInWindow] fromView:nil]];
NSUInteger hit = row == -1 ? 0 : [[self preparedCellAtColumn:0 row:row] hitTestForEvent:event inRect:[self frameOfCellAtColumn:0 row:row] ofView:self];
if(hit & (OFBPathInfoCellHitOpenItem | OFBPathInfoCellHitRevealItem | NSCellHitTrackableArea))
return NO;
NSPoint p = [self convertPoint:[event locationInWindow] fromView:nil];
return [self isRowSelected:[self rowAtPoint:p]] && !(event.modifierFlags & NSCommandKeyMask);
}
- (BOOL)acceptsFirstResponder
@@ -343,18 +340,4 @@
}
[super mouseExited:anEvent];
}
// ========================
// = Hit Testing The Cell =
// ========================
- (NSUInteger)hitTestForPoint:(NSPoint)aPoint
{
NSInteger row = [self rowAtPoint:aPoint];
return row == -1 ? 0 : [[self preparedCellAtColumn:0 row:row] hitTestForEvent:[NSApp currentEvent] inRect:[self frameOfCellAtColumn:0 row:row] ofView:self];
}
- (BOOL)isPointInImage:(NSPoint)aPoint { return ([self hitTestForPoint:aPoint] & OakImageAndTextCellHitImage) == OakImageAndTextCellHitImage; }
- (BOOL)isPointInText:(NSPoint)aPoint { return ([self hitTestForPoint:aPoint] & OakImageAndTextCellHitText) == OakImageAndTextCellHitText; }
- (BOOL)isPointInCloseButton:(NSPoint)aPoint { return ([self hitTestForPoint:aPoint] & OFBPathInfoCellHitCloseButton) == OFBPathInfoCellHitCloseButton; }
@end

View File

@@ -3,6 +3,8 @@
enum {
OFBPathInfoCellHitCloseButton = (1 << 12),
OFBPathInfoCellHitOpenItem = (1 << 13),
OFBPathInfoCellHitRevealItem = (1 << 14),
};
PUBLIC @interface OFBPathInfoCell : OakImageAndTextCell

View File

@@ -174,7 +174,14 @@ static void DrawSpinner (NSRect cellFrame, BOOL isFlipped, NSColor* color, doubl
if(NSMouseInRect(point, [self closeButtonRectInFrame:cellFrame], [controlView isFlipped]))
return NSCellHitContentArea | NSCellHitTrackableArea | OFBPathInfoCellHitCloseButton;
return [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
NSUInteger res = [super hitTestForEvent:event inRect:cellFrame ofView:controlView];
if((res & OakImageAndTextCellHitImage) && ([event type] == NSLeftMouseDown || [event type] == NSLeftMouseUp) && !([event modifierFlags] & (NSShiftKeyMask | NSControlKeyMask)))
{
if(([event modifierFlags] & NSCommandKeyMask))
res |= OFBPathInfoCellHitRevealItem;
else res |= OFBPathInfoCellHitOpenItem;
}
return res;
}
- (BOOL)trackMouse:(NSEvent*)theEvent inRect:(NSRect)cellFrame ofView:(NSView*)controlView untilMouseUp:(BOOL)untilMouseUp