Skip to content

Commit 1cf3180

Browse files
committed
Merge pull request godotengine#113757 from bruvzg/mac_nfd_ext
[macOS] Prefer user specified file extensions over OS preferred one.
2 parents afefed5 + 0a6f8c2 commit 1cf3180

3 files changed

Lines changed: 32 additions & 5 deletions

File tree

platform/macos/display_server_macos.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@
10601060
Vector<String> files;
10611061
String url;
10621062
url.append_utf8([[[panel URL] path] UTF8String]);
1063-
files.push_back(url);
1063+
files.push_back([panel_delegate validateFilename:url]);
10641064
if (callback.is_valid()) {
10651065
if (p_options_in_cb) {
10661066
Variant v_result = true;
@@ -1179,7 +1179,7 @@
11791179
for (NSUInteger i = 0; i != [urls count]; ++i) {
11801180
String url;
11811181
url.append_utf8([[[urls objectAtIndex:i] path] UTF8String]);
1182-
files.push_back(url);
1182+
files.push_back([panel_delegate validateFilename:url]);
11831183
}
11841184
if (callback.is_valid()) {
11851185
if (p_options_in_cb) {

platform/macos/godot_open_save_delegate.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
@interface GodotOpenSaveDelegate : NSObject <NSOpenSavePanelDelegate> {
4242
NSSavePanel *dialog;
4343
NSMutableArray *allowed_types;
44+
Vector<Vector<String>> preferred_types;
4445

4546
HashMap<int, String> ctr_ids;
4647
Dictionary options;
@@ -51,7 +52,7 @@
5152
}
5253

5354
- (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &)p_filters options:(const TypedArray<Dictionary> &)p_options;
54-
- (void)setFileTypes:(NSMutableArray *)p_allowed_types;
55+
- (void)setFileTypes:(NSMutableArray *)p_allowed_types pref:(const Vector<Vector<String>> &)p_preftypes;
5556
- (void)popupOptionAction:(id)p_sender;
5657
- (void)popupCheckAction:(id)p_sender;
5758
- (void)popupFileAction:(id)p_sender;
@@ -60,5 +61,6 @@
6061
- (int)setDefaultInt:(const String &)p_name value:(int)p_value;
6162
- (int)setDefaultBool:(const String &)p_name value:(bool)p_value;
6263
- (void)setRootPath:(const String &)p_root_path;
64+
- (String)validateFilename:(const String &)p_path;
6365

6466
@end

platform/macos/godot_open_save_delegate.mm

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &
103103
}
104104

105105
NSMutableArray *new_allowed_types = [[NSMutableArray alloc] init];
106+
Vector<Vector<String>> pref_types;
106107
bool has_type_popup = false;
107108
{
108109
NSTextField *label = [NSTextField labelWithString:[NSString stringWithUTF8String:RTR("Format").utf8().get_data()]];
@@ -117,6 +118,7 @@ - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &
117118
NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame:NSZeroRect pullsDown:NO];
118119
for (int i = 0; i < p_filters.size(); i++) {
119120
Vector<String> tokens = p_filters[i].split(";");
121+
Vector<String> pref_type;
120122
if (tokens.size() >= 1) {
121123
String flt = tokens[0].strip_edges();
122124
String mime = (tokens.size() >= 3) ? tokens[2].strip_edges() : String();
@@ -135,9 +137,11 @@ - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &
135137
}
136138
if (ut) {
137139
[type_filters addObject:ut];
140+
pref_type.push_back(str.replace("*.", "").strip_edges());
138141
}
139142
} else {
140143
[type_filters addObject:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
144+
pref_type.push_back(str.replace("*.", "").strip_edges());
141145
}
142146
}
143147
}
@@ -158,6 +162,7 @@ - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &
158162
if ([type_filters count] > 0) {
159163
NSString *name_str = [NSString stringWithUTF8String:((tokens.size() == 1) ? tokens[0] : tokens[1].strip_edges()).utf8().get_data()];
160164
[new_allowed_types addObject:type_filters];
165+
pref_types.push_back(pref_type);
161166
[popup addItemWithTitle:name_str];
162167
}
163168
}
@@ -171,6 +176,7 @@ - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &
171176
}
172177
} else if (p_filters.size() == 1) {
173178
Vector<String> tokens = p_filters[0].split(";");
179+
Vector<String> pref_type;
174180
if (tokens.size() >= 1) {
175181
String flt = tokens[0].strip_edges();
176182
String mime = (tokens.size() >= 3) ? tokens[2] : String();
@@ -189,9 +195,11 @@ - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &
189195
}
190196
if (ut) {
191197
[type_filters addObject:ut];
198+
pref_type.push_back(str.replace("*.", "").strip_edges());
192199
}
193200
} else {
194201
[type_filters addObject:[NSString stringWithUTF8String:str.replace("*.", "").strip_edges().utf8().get_data()]];
202+
pref_type.push_back(str.replace("*.", "").strip_edges());
195203
}
196204
}
197205
}
@@ -210,10 +218,11 @@ - (void)makeAccessoryView:(NSSavePanel *)p_panel filters:(const Vector<String> &
210218

211219
if ([type_filters count] > 0) {
212220
[new_allowed_types addObject:type_filters];
221+
pref_types.push_back(pref_type);
213222
}
214223
}
215224
}
216-
[self setFileTypes:new_allowed_types];
225+
[self setFileTypes:new_allowed_types pref:pref_types];
217226
}
218227

219228
[base_view addSubview:view];
@@ -278,8 +287,9 @@ - (int)setDefaultBool:(const String &)p_name value:(bool)p_value {
278287
return cid;
279288
}
280289

281-
- (void)setFileTypes:(NSMutableArray *)p_allowed_types {
290+
- (void)setFileTypes:(NSMutableArray *)p_allowed_types pref:(const Vector<Vector<String>> &)p_preftypes {
282291
allowed_types = p_allowed_types;
292+
preferred_types = p_preftypes;
283293
}
284294

285295
- (instancetype)initWithDialog:(NSSavePanel *)p_dialog {
@@ -346,6 +356,21 @@ - (void)setRootPath:(const String &)p_root_path {
346356
root = p_root_path;
347357
}
348358

359+
- (String)validateFilename:(const String &)p_path {
360+
if (@available(macOS 11, *)) {
361+
if (allowed_types) {
362+
NSMutableArray *type_filters = [allowed_types objectAtIndex:cur_index];
363+
UTType *ut = [type_filters objectAtIndex:0];
364+
String ext = String::utf8([[ut preferredFilenameExtension] UTF8String]);
365+
Vector<String> pref_ext = preferred_types[cur_index];
366+
if (!pref_ext.is_empty() && !pref_ext.has(ext) && p_path.has_extension(ext)) {
367+
return p_path.get_basename() + "." + pref_ext[0];
368+
}
369+
}
370+
}
371+
return p_path;
372+
}
373+
349374
- (BOOL)panel:(id)sender validateURL:(NSURL *)url error:(NSError *_Nullable *)outError {
350375
if (root.is_empty()) {
351376
return YES;

0 commit comments

Comments
 (0)