27
27
#import < Cocoa/Cocoa.h>
28
28
#import < UniformTypeIdentifiers/UTType.h>
29
29
30
+ static void AddFileExtensionType (NSMutableArray *types, const char *pattern_ptr)
31
+ {
32
+ if (!*pattern_ptr) {
33
+ return ; // in case the string had an extra ';' at the end.
34
+ }
35
+
36
+ // -[UTType typeWithFilenameExtension] will return nil if there's a period in the string. It's better to
37
+ // allow too many files than not allow the one the user actually needs, so just take the part after the '.'
38
+ const char *dot = SDL_strrchr (pattern_ptr, ' .' );
39
+ NSString *extstr = [NSString stringWithFormat: @" %s " , dot ? (dot + 1 ) : pattern_ptr];
40
+ if (@available (macOS 11.0 , *)) {
41
+ UTType *uttype = [UTType typeWithFilenameExtension: extstr];
42
+ if (uttype) { // still failed? Don't add the pattern. This is what the pre-macOS11 path does internally anyhow.
43
+ [types addObject: uttype];
44
+ }
45
+ } else {
46
+ [types addObject: extstr];
47
+ }
48
+ }
49
+
30
50
void SDL_SYS_ShowFileDialogWithProperties (SDL_FileDialogType type, SDL_DialogFileCallback callback, void *userdata, SDL_PropertiesID props)
31
51
{
32
52
SDL_Window* window = SDL_GetPointerProperty (props, SDL_PROP_FILE_DIALOG_WINDOW_POINTER, NULL );
@@ -87,7 +107,7 @@ void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFil
87
107
88
108
if (filters) {
89
109
// On macOS 11.0 and up, this is an array of UTType. Prior to that, it's an array of NSString
90
- NSMutableArray *types = [[NSMutableArray alloc ] initWithCapacity: nfilters ];
110
+ NSMutableArray *types = [[NSMutableArray alloc ] initWithCapacity: nfilters];
91
111
92
112
int has_all_files = 0 ;
93
113
for (int i = 0 ; i < nfilters; i++) {
@@ -102,21 +122,14 @@ void SDL_SYS_ShowFileDialogWithProperties(SDL_FileDialogType type, SDL_DialogFil
102
122
for (char *c = pattern; *c; c++) {
103
123
if (*c == ' ;' ) {
104
124
*c = ' \0 ' ;
105
- if (@available (macOS 11.0 , *)) {
106
- [types addObject: [UTType typeWithFilenameExtension: [NSString stringWithFormat: @" %s " , pattern_ptr]]];
107
- } else {
108
- [types addObject: [NSString stringWithFormat: @" %s " , pattern_ptr]];
109
- }
125
+ AddFileExtensionType (types, pattern_ptr);
110
126
pattern_ptr = c + 1 ;
111
127
} else if (*c == ' *' ) {
112
128
has_all_files = 1 ;
113
129
}
114
130
}
115
- if (@available (macOS 11.0 , *)) {
116
- [types addObject: [UTType typeWithFilenameExtension: [NSString stringWithFormat: @" %s " , pattern_ptr]]];
117
- } else {
118
- [types addObject: [NSString stringWithFormat: @" %s " , pattern_ptr]];
119
- }
131
+
132
+ AddFileExtensionType (types, pattern_ptr); // get the last piece of the string.
120
133
121
134
SDL_free (pattern);
122
135
}
0 commit comments