File dialog filter different between CEF and Chrome

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

File dialog filter different between CEF and Chrome

Postby lwttai » Fri Apr 10, 2020 7:45 am

Click on the select button of html:
Code: Select all
<input type="file" accept=".jpg, .png, .jpeg">


In chrome's fileDialog there are only 2 filters:
--customfile (*.jpg;*.png;*.jpeg)
--all (*.*)
In CEF's fileDialog there are 4 filters:
--JPG file (*.jpg)
--PNG file (*.png)
--JPEG file (*.jpeg)
--all (*.*)

If I want to provide a file dialog perform like chrome's one, is popup my own file dialog with CefDialogHandler::OnFileDialog my only option?

BTW, I notice the comments mention about '(c) combined description and file extension delimited using "|" and ";" (e.g. "Image Types|.png;.gif;.jpg")'. When or how the html or js look like will create such kind of filter.
Code: Select all
class CefDialogHandler : public virtual CefBaseRefCounted {
 public:
  typedef cef_file_dialog_mode_t FileDialogMode;

  ///
  // Called to run a file chooser dialog. |mode| represents the type of dialog
  // to display. |title| to the title to be used for the dialog and may be empty
  // to show the default title ("Open" or "Save" depending on the mode).
  // |default_file_path| is the path with optional directory and/or file name
  // component that should be initially selected in the dialog. |accept_filters|
  // are used to restrict the selectable file types and may any combination of
  // (a) valid lower-cased MIME types (e.g. "text/*" or "image/*"),
  // (b) individual file extensions (e.g. ".txt" or ".png"), or (c) combined
  // description and file extension delimited using "|" and ";" (e.g.
  // "Image Types|.png;.gif;.jpg"). |selected_accept_filter| is the 0-based
  // index of the filter that should be selected by default. To display a custom
  // dialog return true and execute |callback| either inline or at a later time.
  // To display the default dialog return false.
  ///
  /*--cef(optional_param=title,optional_param=default_file_path,
          optional_param=accept_filters,index_param=selected_accept_filter)--*/
  virtual bool OnFileDialog(CefRefPtr<CefBrowser> browser,
                            FileDialogMode mode,
                            const CefString& title,
                            const CefString& default_file_path,
                            const std::vector<CefString>& accept_filters,
                            int selected_accept_filter,
                            CefRefPtr<CefFileDialogCallback> callback) {
    return false;
  }
};
lwttai
Techie
 
Posts: 24
Joined: Mon Oct 14, 2019 12:18 am

Re: File dialog filter different between CEF and Chrome

Postby magreenblatt » Fri Apr 10, 2020 11:36 am

Use a wildcard mime type, like “image/*”. See http://tests/dialogs in cefclient for an example.
magreenblatt
Site Admin
 
Posts: 12379
Joined: Fri May 29, 2009 6:57 pm

Re: File dialog filter different between CEF and Chrome

Postby lwttai » Wed Apr 15, 2020 12:20 am

Using a wildcard mime type seems not a good try. Some web app just work with certain extensions
I have check the http://tests/dialogs with cefclient.exe.
It's using native code not html or any js to input "Image Types|.png;.gif;.jpg" pattern as filter in dialog_test.cc.
Code: Select all
    if (dialog_state_->mode_ != FILE_DIALOG_OPEN_FOLDER) {
      // Build filters based on mime time.
      accept_filters.push_back("text/*");

      // Build filters based on file extension.
      accept_filters.push_back(".log");
      accept_filters.push_back(".patch");

      // Add specific filters as-is.
      accept_filters.push_back("Document Files|.doc;.odt");
      accept_filters.push_back("Image Files|.png;.jpg;.gif");
      accept_filters.push_back("PDF Files|.pdf");
    }


In my opinion, a default filter with all extensions that <input> provide and separate filter for each extension is more humanized.
e.g. <input type="file" accept=".jpg, .png, .jpeg">:
--Custom file (*.jpg;*.png;*.jpeg) = default
--JPG file (*.jpg)
--PNG file (*.png)
--JPEG file (*.jpeg)
--all (*.*)
vs CEF behavior
--JPG file (*.jpg)
--PNG file (*.png)
--JPEG file (*.jpeg)
--all (*.*)
vs Chrome behavior
--customfile (*.jpg;*.png;*.jpeg)
--all (*.*)

I would like to add something like this in FormatFilterForExtensions() at file_dialog_runner_win.cc
Code: Select all
  // Add a generic description "Custom Files" at first if multiple types specified.
  std::set<std::wstring> uniq_exts;
  for (size_t i = 0; i < file_ext.size(); ++i) {
     auto exts = base::SplitString(file_ext[i], L";",
        base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
     for (const auto& ext : exts) {
        uniq_exts.insert(ext);
     }
  }
  if (uniq_exts.size() > 1) {
     std::wstring custom_files_desc = l10n_util::GetStringUTF16(IDS_CUSTOM_FILES);
     std::wstring custom_files_exts = *(uniq_exts.cbegin());

     for (auto it = ++uniq_exts.cbegin(); it != uniq_exts.cend(); ++it) {
        custom_files_exts += L';' + *it;
     }
     custom_files_desc += L" (" + custom_files_exts + L")";

     result += custom_files_desc + L'\0' + custom_files_exts + L'\0';
  }
lwttai
Techie
 
Posts: 24
Joined: Mon Oct 14, 2019 12:18 am

Re: File dialog filter different between CEF and Chrome

Postby magreenblatt » Wed Apr 15, 2020 2:38 pm

lwttai wrote:In my opinion, a default filter with all extensions that <input> provide and separate filter for each extension is more humanized.

This is issue #860. A PR to implement this is welcome.
magreenblatt
Site Admin
 
Posts: 12379
Joined: Fri May 29, 2009 6:57 pm


Re: File dialog filter different between CEF and Chrome

Postby hachiko » Tue Jan 18, 2022 6:00 am

magreenblatt wrote:
lwttai wrote:In my opinion, a default filter with all extensions that <input> provide and separate filter for each extension is more humanized.

This is issue #860. A PR to implement this is welcome.

https://bitbucket.org/chromiumembedded/ ... r-with-all
A PR for mac is suppied.
hachiko
Newbie
 
Posts: 4
Joined: Wed Aug 25, 2021 4:23 am


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 16 guests