refactor: migrate to non-deprecated allowedContentTypes on macOS (#46646)

refactor: migrated to non-deprecated allowedContentTypes on macOS

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] 2025-04-15 17:01:53 -04:00 committed by GitHub
parent c785b40703
commit f8a55100cc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -11,6 +11,7 @@
#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"
@ -29,7 +30,7 @@
@interface PopUpButtonHandler : NSObject @interface PopUpButtonHandler : NSObject
@property(nonatomic, assign) NSSavePanel* savePanel; @property(nonatomic, assign) NSSavePanel* savePanel;
@property(nonatomic, strong) NSArray* fileTypesList; @property(nonatomic, strong) NSArray* contentTypesList;
- (instancetype)initWithPanel:(NSSavePanel*)panel - (instancetype)initWithPanel:(NSSavePanel*)panel
andTypesList:(NSArray*)typesList; andTypesList:(NSArray*)typesList;
@ -40,14 +41,14 @@
@implementation PopUpButtonHandler @implementation PopUpButtonHandler
@synthesize savePanel; @synthesize savePanel;
@synthesize fileTypesList; @synthesize contentTypesList;
- (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 setFileTypesList:typesList]; [self setContentTypesList:typesList];
} }
return self; return self;
} }
@ -55,15 +56,19 @@
- (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 fileTypesList]; NSArray* list = [self contentTypesList];
NSArray* fileTypes = [list objectAtIndex:selectedItemIndex]; NSArray* content_types = [list objectAtIndex:selectedItemIndex];
// If we meet a '*' file extension, we allow all the file types and no __block BOOL allowAllFiles = NO;
// need to set the specified file types. [content_types
if ([fileTypes count] == 0 || [fileTypes containsObject:@"*"]) enumerateObjectsUsingBlock:^(UTType* type, NSUInteger idx, BOOL* stop) {
[[self savePanel] setAllowedFileTypes:nil]; if ([[type preferredFilenameExtension] isEqual:@"*"]) {
else allowAllFiles = YES;
[[self savePanel] setAllowedFileTypes:fileTypes]; *stop = YES;
}
}];
[[self savePanel] setAllowedContentTypes:allowAllFiles ? @[] : content_types];
} }
@end @end
@ -100,9 +105,10 @@ 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* file_type_set = NSMutableOrderedSet* content_types_set =
[NSMutableOrderedSet orderedSetWithCapacity:filters.size()]; [NSMutableOrderedSet orderedSetWithCapacity:filters.size()];
[filter_names addObject:@(filter.first.c_str())]; [filter_names addObject:@(filter.first.c_str())];
for (std::string ext : filter.second) { for (std::string ext : filter.second) {
// macOS is incapable of understanding multiple file extensions, // macOS is incapable of understanding multiple file extensions,
// so we need to tokenize the extension that's been passed in. // so we need to tokenize the extension that's been passed in.
@ -113,25 +119,34 @@ void SetAllowedFileTypes(NSSavePanel* dialog, const Filters& filters) {
ext.erase(0, pos + 1); ext.erase(0, pos + 1);
} }
[file_type_set addObject:@(ext.c_str())]; if (ext == "*") {
[content_types_set addObject:[UTType typeWithFilenameExtension:@"*"]];
break;
} else {
if (UTType* utt = [UTType typeWithFilenameExtension:@(ext.c_str())])
[content_types_set addObject:utt];
}
} }
[file_types_list addObject:[file_type_set array]];
[file_types_list addObject:content_types_set];
} }
// Passing empty array to setAllowedFileTypes will cause exception. // Don't add file format picker.
NSArray* file_types = nil; if ([file_types_list count] <= 1)
NSUInteger count = [file_types_list count]; return;
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;
}
[dialog setAllowedFileTypes:file_types];
if (count <= 1) NSArray* content_types = [file_types_list objectAtIndex:0];
return; // don't add file format picker
__block BOOL allowAllFiles = NO;
[content_types
enumerateObjectsUsingBlock:^(UTType* type, NSUInteger idx, BOOL* stop) {
if ([[type preferredFilenameExtension] isEqual:@"*"]) {
allowAllFiles = YES;
*stop = YES;
}
}];
[dialog setAllowedContentTypes:allowAllFiles ? @[] : content_types];
// Add file format picker. // Add file format picker.
ElectronAccessoryView* accessoryView = [[ElectronAccessoryView alloc] ElectronAccessoryView* accessoryView = [[ElectronAccessoryView alloc]