Merge pull request #10015 from electron/input-dialog-filter-titles

Add filter descriptions for common accept types
This commit is contained in:
Kevin Sawicki 2017-07-18 09:10:11 -07:00 committed by GitHub
commit 2bac358064

View file

@ -120,8 +120,13 @@ file_dialog::Filters GetFileTypesFromAcceptType(
std::vector<base::FilePath::StringType> extensions; std::vector<base::FilePath::StringType> extensions;
int valid_type_count = 0;
std::string description;
for (const auto& accept_type : accept_types) { for (const auto& accept_type : accept_types) {
std::string ascii_type = base::UTF16ToASCII(accept_type); std::string ascii_type = base::UTF16ToASCII(accept_type);
auto old_extension_size = extensions.size();
if (ascii_type[0] == '.') { if (ascii_type[0] == '.') {
// If the type starts with a period it is assumed to be a file extension, // If the type starts with a period it is assumed to be a file extension,
// like `.txt`, // so we just have to add it to the list. // like `.txt`, // so we just have to add it to the list.
@ -130,9 +135,19 @@ file_dialog::Filters GetFileTypesFromAcceptType(
// Skip the first character. // Skip the first character.
extensions.push_back(extension.substr(1)); extensions.push_back(extension.substr(1));
} else { } else {
// For MIME Type, `audio/*, vidio/*, image/* if (ascii_type == "image/*")
description = "Image Files";
else if (ascii_type == "audio/*")
description = "Audio Files";
else if (ascii_type == "video/*")
description = "Video Files";
// For MIME Type, `audio/*, video/*, image/*
net::GetExtensionsForMimeType(ascii_type, &extensions); net::GetExtensionsForMimeType(ascii_type, &extensions);
} }
if (extensions.size() > old_extension_size)
valid_type_count++;
} }
// If no valid exntesion is added, return empty filters. // If no valid exntesion is added, return empty filters.
@ -140,6 +155,13 @@ file_dialog::Filters GetFileTypesFromAcceptType(
return filters; return filters;
filters.push_back(file_dialog::Filter()); filters.push_back(file_dialog::Filter());
if (valid_type_count > 1 ||
(valid_type_count == 1 && description.empty() && extensions.size() > 1))
description = "Custom Files";
filters[0].first = description;
for (const auto& extension : extensions) { for (const auto& extension : extensions) {
#if defined(OS_WIN) #if defined(OS_WIN)
filters[0].second.push_back(base::UTF16ToASCII(extension)); filters[0].second.push_back(base::UTF16ToASCII(extension));