Browse Source

Sync native and embedded dialog missing extension handling.

bruvzg 6 months ago
parent
commit
1bdf84b31c
2 changed files with 70 additions and 10 deletions
  1. 35 5
      editor/gui/editor_file_dialog.cpp
  2. 35 5
      scene/gui/file_dialog.cpp

+ 35 - 5
editor/gui/editor_file_dialog.cpp

@@ -127,8 +127,26 @@ void EditorFileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_file
 		emit_signal(SNAME("files_selected"), files);
 		emit_signal(SNAME("files_selected"), files);
 	} else {
 	} else {
 		if (mode == FILE_MODE_SAVE_FILE) {
 		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;
 				int idx = p_filter;
 				if (filters.size() > 1) {
 				if (filters.size() > 1) {
 					idx--;
 					idx--;
@@ -138,7 +156,7 @@ void EditorFileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_file
 					int filter_slice_count = flt.get_slice_count(",");
 					int filter_slice_count = flt.get_slice_count(",");
 					for (int j = 0; j < filter_slice_count; j++) {
 					for (int j = 0; j < filter_slice_count; j++) {
 						String str = (flt.get_slice(",", j).strip_edges());
 						String str = (flt.get_slice(",", j).strip_edges());
-						if (f.match(str)) {
+						if (f.matchn(str)) {
 							valid = true;
 							valid = true;
 							break;
 							break;
 						}
 						}
@@ -147,9 +165,21 @@ void EditorFileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_file
 					if (!valid && filter_slice_count > 0) {
 					if (!valid && filter_slice_count > 0) {
 						String str = (flt.get_slice(",", 0).strip_edges());
 						String str = (flt.get_slice(",", 0).strip_edges());
 						f += str.substr(1, str.length() - 1);
 						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);
 			emit_signal(SNAME("file_selected"), f);
 		} else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
 		} else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
 			emit_signal(SNAME("file_selected"), f);
 			emit_signal(SNAME("file_selected"), f);
@@ -578,9 +608,9 @@ void EditorFileDialog::_action_pressed() {
 		bool valid = false;
 		bool valid = false;
 
 
 		if (filter->get_selected() == filter->get_item_count() - 1) {
 		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) {
 		} else if (filters.size() > 1 && filter->get_selected() == 0) {
-			// match all filters
+			// Match all filters.
 			for (int i = 0; i < filters.size(); i++) {
 			for (int i = 0; i < filters.size(); i++) {
 				String flt = filters[i].get_slice(";", 0);
 				String flt = filters[i].get_slice(";", 0);
 				for (int j = 0; j < flt.get_slice_count(","); j++) {
 				for (int j = 0; j < flt.get_slice_count(","); j++) {

+ 35 - 5
scene/gui/file_dialog.cpp

@@ -153,8 +153,26 @@ void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector<String>
 		emit_signal(SNAME("files_selected"), files);
 		emit_signal(SNAME("files_selected"), files);
 	} else {
 	} else {
 		if (mode == FILE_MODE_SAVE_FILE) {
 		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;
 				int idx = p_filter;
 				if (filters.size() > 1) {
 				if (filters.size() > 1) {
 					idx--;
 					idx--;
@@ -164,7 +182,7 @@ void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector<String>
 					int filter_slice_count = flt.get_slice_count(",");
 					int filter_slice_count = flt.get_slice_count(",");
 					for (int j = 0; j < filter_slice_count; j++) {
 					for (int j = 0; j < filter_slice_count; j++) {
 						String str = (flt.get_slice(",", j).strip_edges());
 						String str = (flt.get_slice(",", j).strip_edges());
-						if (f.match(str)) {
+						if (f.matchn(str)) {
 							valid = true;
 							valid = true;
 							break;
 							break;
 						}
 						}
@@ -173,9 +191,21 @@ void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector<String>
 					if (!valid && filter_slice_count > 0) {
 					if (!valid && filter_slice_count > 0) {
 						String str = (flt.get_slice(",", 0).strip_edges());
 						String str = (flt.get_slice(",", 0).strip_edges());
 						f += str.substr(1, str.length() - 1);
 						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);
 			emit_signal(SNAME("file_selected"), f);
 		} else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
 		} else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
 			emit_signal(SNAME("file_selected"), f);
 			emit_signal(SNAME("file_selected"), f);
@@ -501,9 +531,9 @@ void FileDialog::_action_pressed() {
 		bool valid = false;
 		bool valid = false;
 
 
 		if (filter->get_selected() == filter->get_item_count() - 1) {
 		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) {
 		} else if (filters.size() > 1 && filter->get_selected() == 0) {
-			// match all filters
+			// Match all filters.
 			for (int i = 0; i < filters.size(); i++) {
 			for (int i = 0; i < filters.size(); i++) {
 				String flt = filters[i].get_slice(";", 0);
 				String flt = filters[i].get_slice(";", 0);
 				for (int j = 0; j < flt.get_slice_count(","); j++) {
 				for (int j = 0; j < flt.get_slice_count(","); j++) {