Skip to content

Commit 5fa1e51

Browse files
committed
Add MIME argument to the FileDialog.add_filter.
1 parent 9a5d6d1 commit 5fa1e51

File tree

5 files changed

+66
-7
lines changed

5 files changed

+66
-7
lines changed

doc/classes/FileDialog.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
<return type="void" />
1414
<param index="0" name="filter" type="String" />
1515
<param index="1" name="description" type="String" default="&quot;&quot;" />
16+
<param index="2" name="mime_type" type="String" default="&quot;&quot;" />
1617
<description>
17-
Adds a comma-separated file name [param filter] option to the [FileDialog] with an optional [param description], which restricts what files can be picked.
18+
Adds a comma-separated file extensions [param filter] and comma-separated MIME types [param mime_type] option to the [FileDialog] with an optional [param description], which restricts what files can be picked.
1819
A [param filter] should be of the form [code]"filename.extension"[/code], where filename and extension can be [code]*[/code] to match any string. Filters starting with [code].[/code] (i.e. empty filenames) are not allowed.
19-
For example, a [param filter] of [code]"*.png, *.jpg"[/code] and a [param description] of [code]"Images"[/code] results in filter text "Images (*.png, *.jpg)".
20+
For example, a [param filter] of [code]"*.png, *.jpg"[/code], a [param mime_type] of [code]image/png, image/jpeg[/code] and a [param description] of [code]"Images"[/code] results in filter text "Images (*.png, *.jpg)".
21+
[b]Note:[/b] Embedded file dialog and Windows file dialog support only file extensions, while Android, Linux, and macOS file dialogs also support MIME types.
2022
</description>
2123
</method>
2224
<method name="add_option">

misc/extension_api_validation/4.5-stable.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,10 @@ Validate extension JSON: Error: Field 'classes/AnimationPlayer/methods/set_curre
5454
Validate extension JSON: Error: Field 'classes/AnimationPlayer/signals/current_animation_changed/arguments/0': type changed value in new API, from "String" to "StringName".
5555

5656
Return types and parameters changed to StringName to improve performance. Compatibility methods registered; No compatibility system for signal arguments.
57+
58+
59+
GH-XXXXXX
60+
---------
61+
Validate extension JSON: Error: Field 'classes/FileDialog/methods/add_filter/arguments': size changed value in new API, from 2 to 3.
62+
63+
Optional argument added. Compatibility method registered.

scene/gui/file_dialog.compat.inc

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**************************************************************************/
2+
/* file_dialog.compat.inc */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#ifndef DISABLE_DEPRECATED
32+
33+
void FileDialog::_add_filter_bind_compat_XXXXXX(const String &p_filter, const String &p_description) {
34+
add_filter(p_filter, p_description, "");
35+
}
36+
37+
void FileDialog::_bind_compatibility_methods() {
38+
ClassDB::bind_compatibility_method(D_METHOD("add_filter", "filter", "description"), &FileDialog::_add_filter_bind_compat_XXXXXX, DEFVAL(""));
39+
}
40+
41+
#endif

scene/gui/file_dialog.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
/**************************************************************************/
3030

3131
#include "file_dialog.h"
32+
#include "file_dialog.compat.inc"
3233

3334
#include "core/config/project_settings.h"
3435
#include "core/io/dir_access.h"
@@ -1247,12 +1248,14 @@ void FileDialog::clear_filters() {
12471248
invalidate();
12481249
}
12491250

1250-
void FileDialog::add_filter(const String &p_filter, const String &p_description) {
1251+
void FileDialog::add_filter(const String &p_filter, const String &p_description, const String &p_mime) {
12511252
ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot.");
1252-
if (p_description.is_empty()) {
1253+
if (p_description.is_empty() && p_mime.is_empty()) {
12531254
filters.push_back(p_filter);
1254-
} else {
1255+
} else if (p_mime.is_empty()) {
12551256
filters.push_back(vformat("%s ; %s", p_filter, p_description));
1257+
} else {
1258+
filters.push_back(vformat("%s ; %s ; %s", p_filter, p_description, p_mime));
12561259
}
12571260
update_filters();
12581261
invalidate();
@@ -2035,7 +2038,7 @@ void FileDialog::_bind_methods() {
20352038
ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
20362039

20372040
ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
2038-
ClassDB::bind_method(D_METHOD("add_filter", "filter", "description"), &FileDialog::add_filter, DEFVAL(""));
2041+
ClassDB::bind_method(D_METHOD("add_filter", "filter", "description", "mime_type"), &FileDialog::add_filter, DEFVAL(""), DEFVAL(""));
20392042
ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
20402043
ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
20412044
ClassDB::bind_method(D_METHOD("clear_filename_filter"), &FileDialog::clear_filename_filter);

scene/gui/file_dialog.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,13 +370,19 @@ class FileDialog : public ConfirmationDialog {
370370
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
371371
static void _bind_methods();
372372

373+
#ifndef DISABLE_DEPRECATED
374+
void _add_filter_bind_compat_XXXXXX(const String &p_filter, const String &p_description = "");
375+
376+
static void _bind_compatibility_methods();
377+
#endif
378+
373379
public:
374380
virtual void set_visible(bool p_visible) override;
375381
virtual void popup(const Rect2i &p_rect = Rect2i()) override;
376382

377383
void popup_file_dialog();
378384
void clear_filters();
379-
void add_filter(const String &p_filter, const String &p_description = "");
385+
void add_filter(const String &p_filter, const String &p_description = "", const String &p_mime = "");
380386
void set_filters(const Vector<String> &p_filters);
381387
Vector<String> get_filters() const;
382388
void clear_filename_filter();

0 commit comments

Comments
 (0)