From 1bdf84b31cb9963f83c92e61478c91e870f8ca5d Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Thu, 13 Feb 2025 09:26:07 +0200 Subject: [PATCH] Sync native and embedded dialog missing extension handling. --- editor/gui/editor_file_dialog.cpp | 40 +++++++++++++++++++++++++++---- scene/gui/file_dialog.cpp | 40 +++++++++++++++++++++++++++---- 2 files changed, 70 insertions(+), 10 deletions(-) diff --git a/editor/gui/editor_file_dialog.cpp b/editor/gui/editor_file_dialog.cpp index 0f4d1161d74..03fe1f7f988 100644 --- a/editor/gui/editor_file_dialog.cpp +++ b/editor/gui/editor_file_dialog.cpp @@ -127,8 +127,26 @@ void EditorFileDialog::_native_dialog_cb(bool p_ok, const Vector &p_file emit_signal(SNAME("files_selected"), files); } else { if (mode == FILE_MODE_SAVE_FILE) { - if (p_filter != 0 && p_filter != filter->get_item_count() - 1) { - bool valid = false; + bool valid = false; + + if (p_filter == filter->get_item_count() - 1) { + valid = true; // Match none. + } else if (filters.size() > 1 && p_filter == 0) { + // Match all filters. + for (int i = 0; i < filters.size(); i++) { + String flt = filters[i].get_slice(";", 0); + for (int j = 0; j < flt.get_slice_count(","); j++) { + String str = flt.get_slice(",", j).strip_edges(); + if (f.matchn(str)) { + valid = true; + break; + } + } + if (valid) { + break; + } + } + } else { int idx = p_filter; if (filters.size() > 1) { idx--; @@ -138,7 +156,7 @@ void EditorFileDialog::_native_dialog_cb(bool p_ok, const Vector &p_file int filter_slice_count = flt.get_slice_count(","); for (int j = 0; j < filter_slice_count; j++) { String str = (flt.get_slice(",", j).strip_edges()); - if (f.match(str)) { + if (f.matchn(str)) { valid = true; break; } @@ -147,9 +165,21 @@ void EditorFileDialog::_native_dialog_cb(bool p_ok, const Vector &p_file if (!valid && filter_slice_count > 0) { String str = (flt.get_slice(",", 0).strip_edges()); f += str.substr(1, str.length() - 1); + file->set_text(f.get_file()); + valid = true; } + } else { + valid = true; } } + + // Add first extension of filter if no valid extension is found. + if (!valid) { + int idx = p_filter; + String flt = filters[idx].get_slice(";", 0); + String ext = flt.get_slice(",", 0).strip_edges().get_extension(); + f += "." + ext; + } emit_signal(SNAME("file_selected"), f); } else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) { emit_signal(SNAME("file_selected"), f); @@ -578,9 +608,9 @@ void EditorFileDialog::_action_pressed() { bool valid = false; if (filter->get_selected() == filter->get_item_count() - 1) { - valid = true; // match none + valid = true; // Match none. } else if (filters.size() > 1 && filter->get_selected() == 0) { - // match all filters + // Match all filters. for (int i = 0; i < filters.size(); i++) { String flt = filters[i].get_slice(";", 0); for (int j = 0; j < flt.get_slice_count(","); j++) { diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp index 30acde0d31c..bd89fb19c01 100644 --- a/scene/gui/file_dialog.cpp +++ b/scene/gui/file_dialog.cpp @@ -153,8 +153,26 @@ void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector emit_signal(SNAME("files_selected"), files); } else { if (mode == FILE_MODE_SAVE_FILE) { - if (p_filter != 0 && p_filter != filter->get_item_count() - 1) { - bool valid = false; + bool valid = false; + + if (p_filter == filter->get_item_count() - 1) { + valid = true; // Match none. + } else if (filters.size() > 1 && p_filter == 0) { + // Match all filters. + for (int i = 0; i < filters.size(); i++) { + String flt = filters[i].get_slice(";", 0); + for (int j = 0; j < flt.get_slice_count(","); j++) { + String str = flt.get_slice(",", j).strip_edges(); + if (f.matchn(str)) { + valid = true; + break; + } + } + if (valid) { + break; + } + } + } else { int idx = p_filter; if (filters.size() > 1) { idx--; @@ -164,7 +182,7 @@ void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector int filter_slice_count = flt.get_slice_count(","); for (int j = 0; j < filter_slice_count; j++) { String str = (flt.get_slice(",", j).strip_edges()); - if (f.match(str)) { + if (f.matchn(str)) { valid = true; break; } @@ -173,9 +191,21 @@ void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector if (!valid && filter_slice_count > 0) { String str = (flt.get_slice(",", 0).strip_edges()); f += str.substr(1, str.length() - 1); + file->set_text(f.get_file()); + valid = true; } + } else { + valid = true; } } + + // Add first extension of filter if no valid extension is found. + if (!valid) { + int idx = p_filter; + String flt = filters[idx].get_slice(";", 0); + String ext = flt.get_slice(",", 0).strip_edges().get_extension(); + f += "." + ext; + } emit_signal(SNAME("file_selected"), f); } else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) { emit_signal(SNAME("file_selected"), f); @@ -501,9 +531,9 @@ void FileDialog::_action_pressed() { bool valid = false; if (filter->get_selected() == filter->get_item_count() - 1) { - valid = true; // match none + valid = true; // Match none. } else if (filters.size() > 1 && filter->get_selected() == 0) { - // match all filters + // Match all filters. for (int i = 0; i < filters.size(); i++) { String flt = filters[i].get_slice(";", 0); for (int j = 0; j < flt.get_slice_count(","); j++) {