@@ -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