]> khamidou — GitHub mirrors - electron.git/commitdiff
Make <input type="file"\> remember last used directory.
authorHaojian Wu <hokein.wu@gmail.com>
Mon, 13 Jul 2015 08:19:30 +0000 (16:19 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Thu, 16 Jul 2015 11:29:12 +0000 (19:29 +0800)
atom/browser/web_dialog_helper.cc

index bbf16b5fc23cbe82f5cf8bb7f722094329d43761..93cb32cba7c7b2f6385235eb4d197a0df629f4fe 100644 (file)
@@ -7,10 +7,13 @@
 #include <string>
 #include <vector>
 
+#include "atom/browser/atom_browser_context.h"
+#include "atom/browser/native_window.h"
 #include "atom/browser/ui/file_dialog.h"
 #include "base/bind.h"
 #include "base/files/file_enumerator.h"
 #include "base/files/file_path.h"
+#include "base/prefs/pref_service.h"
 #include "base/strings/utf_string_conversions.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/web_contents.h"
@@ -20,6 +23,8 @@
 
 namespace {
 
+const char kSelectFileLastDirectory[] = "selectfile.last_directory";
+
 file_dialog::Filters GetFileTypesFromAcceptType(
     const std::vector<base::string16>& accept_types) {
   file_dialog::Filters filters;
@@ -103,9 +108,13 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
     }
 
     std::vector<base::FilePath> paths;
+    AtomBrowserContext* browser_context = static_cast<AtomBrowserContext*>(
+        window_->web_contents()->GetBrowserContext());
+    base::FilePath default_file_path = browser_context->prefs()->GetFilePath(
+        kSelectFileLastDirectory).Append(params.default_file_name);
     if (file_dialog::ShowOpenDialog(window_,
                                     base::UTF16ToUTF8(params.title),
-                                    params.default_file_name,
+                                    default_file_path,
                                     filters,
                                     flags,
                                     &paths)) {
@@ -115,6 +124,10 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents,
         info.display_name = path.BaseName().value();
         result.push_back(info);
       }
+      if (!paths.empty()) {
+        browser_context->prefs()->SetFilePath(kSelectFileLastDirectory,
+                                              paths[0].DirName());
+      }
     }
   }