-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Allow dragging a folder on the Welcome window to open a repository - Closes #468 and #247 #469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (C) 2015-2018 Pierre-Olivier Latour <info@pol-online.net> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#import <Cocoa/Cocoa.h> | ||
|
||
@interface DragAndDropImageView : NSImageView <NSDraggingDestination> | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Copyright (C) 2015-2018 Pierre-Olivier Latour <info@pol-online.net> | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
#import "DragAndDropImageView.h" | ||
#import "AppDelegate.h" | ||
|
||
@interface DragAndDropImageView() | ||
@property (nonatomic, assign) BOOL isHighlighted; | ||
@end | ||
|
||
@implementation DragAndDropImageView | ||
|
||
- (void)awakeFromNib { | ||
[super awakeFromNib]; | ||
[self registerForDraggedTypes:@[NSFilenamesPboardType]]; | ||
} | ||
|
||
- (void)dealloc { | ||
[self unregisterDraggedTypes]; | ||
} | ||
|
||
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender { | ||
BOOL isDragOperationGeneric = (NSDragOperationGeneric & [sender draggingSourceOperationMask]) == NSDragOperationGeneric; | ||
NSPasteboard *pasteboard = [sender draggingPasteboard]; | ||
if (isDragOperationGeneric && [self directoryURLFromPasteboard:pasteboard]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On modern macOS, drag operations should be validated using the multi-contents support of Here, that would be: [pasteboard canReadObjectForClasses:@[ NSURL.class ] options:@{
NSPasteboardURLReadingFileURLsOnlyKey: @YES,
NSPasteboardURLReadingContentsConformToTypesKey: @[
(__bridge id)kUTTypeDirectory
]
}] |
||
self.isHighlighted = YES; | ||
return NSDragOperationGeneric; | ||
} else { | ||
return NSDragOperationNone; | ||
} | ||
} | ||
|
||
- (void)draggingExited:(id<NSDraggingInfo>)sender { | ||
self.isHighlighted = NO; | ||
} | ||
|
||
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender { | ||
NSPasteboard *pasteboard = [sender draggingPasteboard]; | ||
NSURL *directoryURL = [self directoryURLFromPasteboard:pasteboard]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On modern macOS, drag operations should be performed using the multi-contents support of Here, that would be: NSArray* urls = [pasteboard readObjectsForClasses:@[ NSURL.class ] options:@{
NSPasteboardURLReadingFileURLsOnlyKey: @YES,
NSPasteboardURLReadingContentsConformToTypesKey: @[
(__bridge id)kUTTypeDirectory
]
}] |
||
if (directoryURL) { | ||
AppDelegate *appDelegate = (AppDelegate *)[[NSApplication sharedApplication] delegate]; | ||
[appDelegate openRepositoryWithURL:directoryURL]; | ||
return YES; | ||
} else { | ||
return NO; | ||
} | ||
} | ||
|
||
- (void)concludeDragOperation:(id<NSDraggingInfo>)sender { | ||
self.isHighlighted = NO; | ||
} | ||
|
||
- (void)drawRect:(NSRect)dirtyRect { | ||
[super drawRect:dirtyRect]; | ||
|
||
if (self.isHighlighted) { | ||
NSBezierPath *path = [NSBezierPath bezierPathWithRoundedRect:dirtyRect xRadius:10 yRadius:10]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// systemBlueColor from https://developer.apple.com/design/human-interface-guidelines/macos/visual-design/color/ | ||
NSColor *higlightColor = [NSColor colorWithRed:27.0/255.0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo in name, and also, perhaps better to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
green:173.0/255.0 | ||
blue:248.0/255.0 | ||
alpha:0.2]; | ||
[higlightColor set]; | ||
[path fill]; | ||
} | ||
} | ||
|
||
#pragma mark - | ||
|
||
// Returns first directory URL from the given pasteboard | ||
- (NSURL *)directoryURLFromPasteboard:(NSPasteboard *)pasteboard { | ||
NSString *desiredType = [pasteboard availableTypeFromArray:@[NSFilenamesPboardType]]; | ||
if (![desiredType isEqualToString:NSFilenamesPboardType]) { | ||
return nil; | ||
} | ||
NSArray *filenames = [pasteboard propertyListForType:NSFilenamesPboardType]; | ||
NSURL *url = [NSURL fileURLWithPath:[filenames firstObject]]; | ||
NSFileManager *fileManager = [NSFileManager defaultManager]; | ||
BOOL isDirectory = NO; | ||
if ([fileManager fileExistsAtPath:[url path] isDirectory:&isDirectory] && isDirectory) { | ||
return url; | ||
} else { | ||
return nil; | ||
} | ||
} | ||
|
||
- (void)setIsHighlighted:(BOOL)isHighlighted { | ||
if (_isHighlighted != isHighlighted) { | ||
_isHighlighted = isHighlighted; | ||
|
||
[self setNeedsDisplay:YES]; | ||
} | ||
} | ||
|
||
@end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On modern macOS, drag support should be registered either using one of the non-deprecated pasteboard types (post-10.14) or a UTI. Here, use
@[ (__bridge id)kUTTypeFileURL ]
orNSURL.readableTypeIdentifiersForItemProvider
, whichever you prefer.