electron / electron

:electron: Build cross-platform desktop apps with JavaScript, HTML, and CSS
https://electronjs.org
MIT License
113.94k stars 15.36k forks source link

Unable to set default filter to 'All Files (`*.*`)' in Windows save file dialog #19492

Open rmobis opened 5 years ago

rmobis commented 5 years ago

Preflight Checklist

Problem Description

It is not possible to set the default filter on a save file dialog in Windows to All Files (*.*). This is a nuisance because often when you want to save a file with no/unknown extension, such as .babelrc, it appends the first filter's extension. Example: https://github.com/microsoft/vscode/issues/28425

image

Now, I know this is intentionally not allowed, as can be seen in shell/browser/ui/file_dialog_win#L206:

// By default, *.* will be added to the file name if file type is "*.*". In
// Electron, we disable it to make a better experience.
// ...
for (size_t i = 0; i < filterspec.size(); ++i) {
    if (std::wstring(filterspec[i].pszSpec) != L"*.*") {
        // SetFileTypeIndex is regarded as one-based index.
        dialog->SetFileTypeIndex(i + 1);
        dialog->SetDefaultExtension(filterspec[i].pszSpec);
        break;
    }
}

It basically picks the first filter as the default, except for the All Files one because of a supposed issue where it appends *.* to the file name. However, there has to be a way to do this, because other programs do (unfortunately I couldn't think of any open source one so we could look at the sources).

Proposed Solution

No real ready solution in mind. Just gotta figure out how to allow it and avoid the issue. Maybe if instead of setting the default extension to "*.*" we set it "" when the All Files filter is used, we could get both things to work.

Alternatives Considered

Verify whether the appending *.* to filename issue actually happens. If it doesn't, we can just cut the code out.

Additional Information

Fiddle: https://gist.github.com/e9cf0de5417180669245e79b10c2f898

TongDaDa commented 5 years ago

Same problem here.

TongDaDa commented 5 years ago

On Mac, it works fine, and on the windows, It always appears "All files *".

For this demo, I used the URL(https://codepen.io/tongdada-the-sans/pen/OKjxgJ) in my electron.

And the images:

企业微信截图_15647151318763

屏幕快照 2019-08-02 上午11 01 31

As for how to resolve this situation, I just found a solution as far that appending a suffix on the filename, although It already has a .ext on its name. see below.

企业微信截图_15647266618763

On windows10, we can add two ext on the filename, but the selection of the saveFiledialog of the system always show an All type(*).

I don't know how to resolve it.

cunlic commented 11 months ago

This is a huge issue specifically in VS Code. With it defaulting to "Plain Text (.txt)" as the filetype for unknown new files, the user can't see ANY* files in the existing directory when saving to look for duplicates, naming patterns to follow, or single-click an existing file to use as a template and rename etc.

(I came here from this VS Code bug report from 6 years ago) https://github.com/microsoft/vscode/issues/28425

Surely this can be fixed within Electron, but if it can't, this feels like another desperate need for an AHK script.