revert: use deprecated setAllowedFileTypes in macOS dialogs (#49470)

* revert: use deprectated setAllowedFileTypes in macOS dialogs

Closes https://github.com/electron/electron/issues/48191

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

* chore: remove stray import

Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
This commit is contained in:
trop[bot]
2026-01-21 17:57:39 +01:00
committed by GitHub
parent 486a9a61d2
commit 5ed82c16e8

View File

@@ -11,7 +11,6 @@
#import <Cocoa/Cocoa.h> #import <Cocoa/Cocoa.h>
#import <CoreServices/CoreServices.h> #import <CoreServices/CoreServices.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
#include "base/apple/foundation_util.h" #include "base/apple/foundation_util.h"
#include "base/apple/scoped_cftyperef.h" #include "base/apple/scoped_cftyperef.h"
@@ -30,7 +29,7 @@
@interface PopUpButtonHandler : NSObject @interface PopUpButtonHandler : NSObject
@property(nonatomic, weak) NSSavePanel* savePanel; @property(nonatomic, weak) NSSavePanel* savePanel;
@property(nonatomic, strong) NSArray* contentTypesList; @property(nonatomic, strong) NSArray* fileTypesList;
- (instancetype)initWithPanel:(NSSavePanel*)panel - (instancetype)initWithPanel:(NSSavePanel*)panel
andTypesList:(NSArray*)typesList; andTypesList:(NSArray*)typesList;
@@ -41,14 +40,14 @@
@implementation PopUpButtonHandler @implementation PopUpButtonHandler
@synthesize savePanel; @synthesize savePanel;
@synthesize contentTypesList; @synthesize fileTypesList;
- (instancetype)initWithPanel:(NSSavePanel*)panel - (instancetype)initWithPanel:(NSSavePanel*)panel
andTypesList:(NSArray*)typesList { andTypesList:(NSArray*)typesList {
self = [super init]; self = [super init];
if (self) { if (self) {
[self setSavePanel:panel]; [self setSavePanel:panel];
[self setContentTypesList:typesList]; [self setFileTypesList:typesList];
} }
return self; return self;
} }
@@ -56,19 +55,17 @@
- (void)selectFormat:(id)sender { - (void)selectFormat:(id)sender {
NSPopUpButton* button = (NSPopUpButton*)sender; NSPopUpButton* button = (NSPopUpButton*)sender;
NSInteger selectedItemIndex = [button indexOfSelectedItem]; NSInteger selectedItemIndex = [button indexOfSelectedItem];
NSArray* list = [self contentTypesList]; NSArray* list = [self fileTypesList];
NSArray* content_types = [list objectAtIndex:selectedItemIndex]; NSArray* fileTypes = [list objectAtIndex:selectedItemIndex];
__block BOOL allowAllFiles = NO; #pragma clang diagnostic push
[content_types #pragma clang diagnostic ignored "-Wdeprecated-declarations"
enumerateObjectsUsingBlock:^(UTType* type, NSUInteger idx, BOOL* stop) { // If we meet a '*' file extension, we allow all file types.
if ([[type preferredFilenameExtension] isEqual:@"*"]) { if ([fileTypes count] == 0 || [fileTypes containsObject:@"*"])
allowAllFiles = YES; [[self savePanel] setAllowedFileTypes:nil];
*stop = YES; else
} [[self savePanel] setAllowedFileTypes:fileTypes];
}]; #pragma clang diagnostic pop
[[self savePanel] setAllowedContentTypes:allowAllFiles ? @[] : content_types];
} }
@end @end
@@ -105,7 +102,7 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
// Create array to keep file types and their name. // Create array to keep file types and their name.
for (const Filter& filter : filters) { for (const Filter& filter : filters) {
NSMutableOrderedSet* content_types_set = NSMutableOrderedSet* file_type_set =
[NSMutableOrderedSet orderedSetWithCapacity:filters.size()]; [NSMutableOrderedSet orderedSetWithCapacity:filters.size()];
[filter_names addObject:@(filter.first.c_str())]; [filter_names addObject:@(filter.first.c_str())];
@@ -119,46 +116,30 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
ext.erase(0, pos + 1); ext.erase(0, pos + 1);
} }
if (ext == "*") { [file_type_set addObject:@(ext.c_str())];
[content_types_set addObject:[UTType typeWithFilenameExtension:@"*"]];
break;
} else if (ext == "app") {
// This handles a bug on macOS where the "app" extension by default
// maps to "com.apple.application-file", which is for an Application
// file (older single-file carbon based apps), and not modern
// Application Bundles (multi file packages as you'd see for all modern
// applications).
UTType* superType =
[UTType typeWithIdentifier:@"com.apple.application-bundle"];
if (UTType* utt = [UTType typeWithFilenameExtension:@"app"
conformingToType:superType]) {
[content_types_set addObject:utt];
}
} else {
if (UTType* utt = [UTType typeWithFilenameExtension:@(ext.c_str())])
[content_types_set addObject:utt];
}
} }
[file_types_list addObject:content_types_set]; [file_types_list addObject:[file_type_set array]];
} }
NSArray* content_types = [file_types_list objectAtIndex:0]; // Passing empty array to setAllowedFileTypes will cause exception.
NSArray* file_types = nil;
NSUInteger count = [file_types_list count];
if (count > 0) {
file_types = [[file_types_list objectAtIndex:0] allObjects];
// If we meet a '*' file extension, we allow all the file types and no
// need to set the specified file types.
if ([file_types count] == 0 || [file_types containsObject:@"*"])
file_types = nil;
}
__block BOOL allowAllFiles = NO; #pragma clang diagnostic push
[content_types #pragma clang diagnostic ignored "-Wdeprecated-declarations"
enumerateObjectsUsingBlock:^(UTType* type, NSUInteger idx, BOOL* stop) { [dialog setAllowedFileTypes:file_types];
if ([[type preferredFilenameExtension] isEqual:@"*"]) { #pragma clang diagnostic pop
allowAllFiles = YES;
*stop = YES;
}
}];
[dialog setAllowedContentTypes:allowAllFiles ? @[] : content_types]; if (count <= 1)
return; // don't add file format picker
// Don't add file format picker.
if ([file_types_list count] <= 1)
return;
// Add file format picker. // Add file format picker.
ElectronAccessoryView* accessoryView = [[ElectronAccessoryView alloc] ElectronAccessoryView* accessoryView = [[ElectronAccessoryView alloc]