Fix NSFilenamesPboardType -> NSPasteboardTypeFileURL deprecation warning

This is a little tricky because it's not a simple map. With
NSPasteboardTypeFileURL, we have to use readObjectsForClasses:options:
to obtain a list of URL objects and then turn them into file path
strings. Previously you could just get a list of file names directly
with NSFilenamesPboardType. Also, this new enum was only defined in
10.13, so we have to maintain parallel paths to support both types of
getting the list of file names from the pasteboard. Also refactored the
code that use this to a function in Miscllaneous.m to avoid the
headache. Note that the old NSFilenamesPboardType method still works
today, so this is mostly to clean up old code and deprecation warnings.

Also made the "new file here" plugin accept both the old and new
pasteboard types, just in case.
This commit is contained in:
Yee Cheng Chin
2022-10-05 20:02:18 -07:00
parent 94564fdc40
commit dc1ad365de
8 changed files with 79 additions and 32 deletions
+1
View File
@@ -1299,6 +1299,7 @@
<string>MacVim</string>
<key>NSSendTypes</key>
<array>
<string>public.file-url</string>
<string>NSFilenamesPboardType</string>
</array>
<key>NSUserData</key>
+2 -5
View File
@@ -1647,12 +1647,9 @@ fsEventCallback(ConstFSEventStreamRef streamRef,
- (void)newFileHere:(NSPasteboard *)pboard userData:(NSString *)userData
error:(NSString **)error
{
if (![[pboard types] containsObject:NSFilenamesPboardType]) {
ASLogNotice(@"Pasteboard contains no NSFilenamesPboardType");
NSArray<NSString *> *filenames = extractPasteboardFilenames(pboard);
if (filenames == nil || filenames.count == 0)
return;
}
NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
NSString *path = [filenames lastObject];
BOOL dirIndicator;
+2 -2
View File
@@ -244,8 +244,8 @@ static void grid_free(Grid *grid) {
helper = [[MMTextViewHelper alloc] init];
[helper setTextView:self];
[self registerForDraggedTypes:[NSArray arrayWithObjects:
NSFilenamesPboardType, NSPasteboardTypeString, nil]];
[self registerForDraggedTypes:@[getPasteboardFilenamesType(),
NSPasteboardTypeString]];
ligatures = NO;
+1 -2
View File
@@ -819,8 +819,7 @@
- (NSArray *)acceptableDragTypes
{
return [NSArray arrayWithObjects:NSFilenamesPboardType,
NSPasteboardTypeString, nil];
return @[getPasteboardFilenamesType(), NSPasteboardTypeString];
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
+5 -5
View File
@@ -519,9 +519,9 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b)
NSString *string = [pboard stringForType:NSPasteboardTypeString];
[[self vimController] dropString:string];
return YES;
} else if ([[pboard types] containsObject:NSFilenamesPboardType]) {
NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
[[self vimController] dropFiles:files forceOpen:NO];
} else if ([[pboard types] containsObject:getPasteboardFilenamesType()]) {
NSArray<NSString *> *filenames = extractPasteboardFilenames(pboard);
[[self vimController] dropFiles:filenames forceOpen:NO];
return YES;
}
@@ -533,7 +533,7 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b)
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType]
if ( [[pboard types] containsObject:getPasteboardFilenamesType()]
&& (sourceDragMask & NSDragOperationCopy) )
return NSDragOperationCopy;
if ( [[pboard types] containsObject:NSPasteboardTypeString]
@@ -548,7 +548,7 @@ KeyboardInputSourcesEqual(TISInputSourceRef a, TISInputSourceRef b)
NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
NSPasteboard *pboard = [sender draggingPasteboard];
if ( [[pboard types] containsObject:NSFilenamesPboardType]
if ( [[pboard types] containsObject:getPasteboardFilenamesType()]
&& (sourceDragMask & NSDragOperationCopy) )
return NSDragOperationCopy;
if ( [[pboard types] containsObject:NSPasteboardTypeString]
+13 -17
View File
@@ -151,8 +151,7 @@ enum {
[[tabBarControl addTabButton] setTarget:self];
[[tabBarControl addTabButton] setAction:@selector(addNewTab:)];
[tabBarControl setAllowsDragBetweenWindows:NO];
[tabBarControl registerForDraggedTypes:
[NSArray arrayWithObject:NSFilenamesPboardType]];
[tabBarControl registerForDraggedTypes:[NSArray arrayWithObject:getPasteboardFilenamesType()]];
[tabBarControl setAutoresizingMask:NSViewWidthSizable|NSViewMinYMargin];
@@ -588,7 +587,7 @@ enum {
forTabAtIndex:(NSUInteger)tabIndex
{
NSPasteboard *pb = [sender draggingPasteboard];
return [[pb types] containsObject:NSFilenamesPboardType]
return [[pb types] containsObject:getPasteboardFilenamesType()]
? NSDragOperationCopy
: NSDragOperationNone;
}
@@ -598,22 +597,19 @@ enum {
forTabAtIndex:(NSUInteger)tabIndex
{
NSPasteboard *pb = [sender draggingPasteboard];
if ([[pb types] containsObject:NSFilenamesPboardType]) {
NSArray *filenames = [pb propertyListForType:NSFilenamesPboardType];
if ([filenames count] == 0)
return NO;
if (tabIndex != NSNotFound) {
// If dropping on a specific tab, only open one file
[vimController file:[filenames objectAtIndex:0]
draggedToTabAtIndex:tabIndex];
} else {
// Files were dropped on empty part of tab bar; open them all
[vimController filesDraggedToTabBar:filenames];
}
return YES;
} else {
NSArray<NSString*>* filenames = extractPasteboardFilenames(pb);
if (filenames == nil || filenames.count == 0)
return NO;
if (tabIndex != NSNotFound) {
// If dropping on a specific tab, only open one file
[vimController file:[filenames objectAtIndex:0]
draggedToTabAtIndex:tabIndex];
} else {
// Files were dropped on empty part of tab bar; open them all
[vimController filesDraggedToTabBar:filenames];
}
return YES;
}
+4 -1
View File
@@ -169,8 +169,11 @@ NSView *showHiddenFilesView();
NSString *normalizeFilename(NSString *filename);
NSArray *normalizeFilenames(NSArray *filenames);
BOOL shouldUseYosemiteTabBarStyle();
BOOL shouldUseMojaveTabBarStyle();
int getCurrentAppearance(NSAppearance *appearance);
// Pasteboard helpers
NSPasteboardType getPasteboardFilenamesType();
NSArray<NSString*>* extractPasteboardFilenames(NSPasteboard *pboard);
+51
View File
@@ -350,3 +350,54 @@ getCurrentAppearance(NSAppearance *appearance){
#endif
return flag;
}
/// Returns the pasteboard type to use for retrieving file names from a list of
/// files.
/// @return The pasteboard type that can be passed to NSPasteboard for registration.
NSPasteboardType getPasteboardFilenamesType()
{
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_13
return NSPasteboardTypeFileURL;
#else
return NSFilenamesPboardType;
#endif
}
/// Extract the list of file names from a pasteboard.
NSArray<NSString*>* extractPasteboardFilenames(NSPasteboard *pboard)
{
// NSPasteboardTypeFileURL is only available in 10.13, and
// NSFilenamesPboardType was deprecated soon after that (10.14).
// As such if we are building with min deployed OS 10.13, we need to use
// the new method (using NSPasteboardTypeFileURL /
// readObjectsForClasses:options:) because otherwise we will get
// deprecation warnings. Otherwise, we just use NSFilenamesPboardType. It
// will still work if run in a newer OS since it's simply deprecated, and
// the OS still supports it.
#if MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_13
if (![pboard.types containsObject:NSPasteboardTypeFileURL]) {
ASLogNotice(@"Pasteboard contains no NSPasteboardTypeFileURL");
return nil;
}
NSArray<NSURL*> *fileurls = [pboard readObjectsForClasses:@[NSURL.class]
options:@{NSPasteboardURLReadingFileURLsOnlyKey: [NSNumber numberWithBool:YES]}];
if (fileurls == nil || fileurls.count == 0) {
return nil;
}
NSMutableArray<NSString *> *filenames = [NSMutableArray arrayWithCapacity:fileurls.count];
for (int i = 0; i < fileurls.count; i++) {
[filenames addObject:fileurls[i].path];
}
return filenames;
#else
if (![pboard.types containsObject:NSFilenamesPboardType]) {
ASLogNotice(@"Pasteboard contains no NSFilenamesPboardType");
return nil;
}
NSArray<NSString *> *filenames = [pboard propertyListForType:NSFilenamesPboardType];
return filenames;
#endif
}