1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-09 12:50:35 +00:00

Support MIME types in file dialog filters on macOS and Linux.

This commit is contained in:
Pāvels Nadtočajevs
2024-11-17 12:25:23 +02:00
parent bdf625bd54
commit e1f129cb52
19 changed files with 242 additions and 58 deletions

View File

@@ -1218,50 +1218,83 @@ void EditorFileDialog::update_filters() {
if (filters.size() > 1) {
String all_filters;
String all_mime;
String all_filters_full;
String all_mime_full;
const int max_filters = 5;
// "All Recognized" display name.
for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
String flt = filters[i].get_slicec(';', 0).strip_edges();
if (i > 0) {
if (!all_filters.is_empty() && !flt.is_empty()) {
all_filters += ", ";
}
all_filters += flt;
String mime = filters[i].get_slicec(';', 2).strip_edges();
if (!all_mime.is_empty() && !mime.is_empty()) {
all_mime += ", ";
}
all_mime += mime;
}
// "All Recognized" filter.
for (int i = 0; i < filters.size(); i++) {
String flt = filters[i].get_slicec(';', 0).strip_edges();
if (i > 0) {
if (!all_filters_full.is_empty() && !flt.is_empty()) {
all_filters_full += ",";
}
all_filters_full += flt;
String mime = filters[i].get_slicec(';', 2).strip_edges();
if (!all_mime_full.is_empty() && !mime.is_empty()) {
all_mime_full += ",";
}
all_mime_full += mime;
}
String native_all_name;
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_MIME)) {
native_all_name += all_filters;
}
if (!native_all_name.is_empty()) {
native_all_name += ", ";
}
native_all_name += all_mime;
if (max_filters < filters.size()) {
all_filters += ", ...";
native_all_name += ", ...";
}
String f = TTR("All Recognized") + " (" + all_filters + ")";
filter->add_item(f);
processed_filters.push_back(all_filters_full + ";" + f);
filter->add_item(atr(ETR("All Recognized")) + " (" + all_filters + ")");
processed_filters.push_back(all_filters_full + ";" + atr(ETR("All Recognized")) + " (" + native_all_name + ")" + ";" + all_mime_full);
}
for (int i = 0; i < filters.size(); i++) {
String flt = filters[i].get_slicec(';', 0).strip_edges();
String desc = filters[i].get_slice(";", 1).strip_edges();
if (desc.length()) {
String f = desc + " (" + flt + ")";
filter->add_item(f);
processed_filters.push_back(flt + ";" + f);
String desc = filters[i].get_slicec(';', 1).strip_edges();
String mime = filters[i].get_slicec(';', 2).strip_edges();
String native_name;
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_MIME)) {
native_name += flt;
}
if (!native_name.is_empty() && !mime.is_empty()) {
native_name += ", ";
}
native_name += mime;
if (!desc.is_empty()) {
filter->add_item(atr(desc) + " (" + flt + ")");
processed_filters.push_back(flt + ";" + atr(desc) + " (" + native_name + ");" + mime);
} else {
String f = "(" + flt + ")";
filter->add_item(f);
processed_filters.push_back(flt + ";" + f);
filter->add_item("(" + flt + ")");
processed_filters.push_back(flt + ";(" + native_name + ");" + mime);
}
}
String f = TTR("All Files") + " (*.*)";
filter->add_item(f);
processed_filters.push_back("*.*;" + f);
processed_filters.push_back("*.*;" + f + ";application/octet-stream");
}
void EditorFileDialog::clear_filters() {