Win: Remove adding '*.*' to file name in a save dialog.
authorHaojian Wu <hokein.wu@gmail.com>
Tue, 27 Oct 2015 05:10:34 +0000 (13:10 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 27 Oct 2015 12:15:20 +0000 (20:15 +0800)
atom/browser/ui/file_dialog_win.cc

index da00dc54e2fc684034c1a028311c654347efa3ab..edbb4b332b54972baacad921e17522bd65df869c 100644 (file)
@@ -79,8 +79,25 @@ class FileDialog {
     if (!title.empty())
       GetPtr()->SetTitle(base::UTF8ToUTF16(title).c_str());
 
-    if (!filterspec.empty())
-      GetPtr()->SetDefaultExtension(filterspec.front().pszSpec);
+    // By default, *.* will be added to the file name if file type is "*.*". In
+    // Electron, we disable it to make a better experience.
+    //
+    // From MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/
+    // bb775970(v=vs.85).aspx
+    //
+    // If SetDefaultExtension is not called, the dialog will not update
+    // automatically when user choose a new file type in the file dialog.
+    //
+    // We set file extension to the first none-wildcard extension to make
+    // sure the dialog will update file extension automatically.
+    for (size_t i = 0; i < filterspec.size(); ++i) {
+      if (std::wstring(filterspec[i].pszSpec) != L"*.*") {
+        // SetFileTypeIndex is regarded as one-based index.
+        GetPtr()->SetFileTypeIndex(i+1);
+        GetPtr()->SetDefaultExtension(filterspec[i].pszSpec);
+        break;
+      }
+    }
 
     SetDefaultFolder(default_path);
   }