Bug 734666 - Style Editor default filename for saving. r=harth
authorWillian Gustavo Veiga <contact@willianveiga.com>
Mon, 06 Jan 2014 09:52:29 -0500
changeset 162230 974401cb4866373d56909b7cc54331fd954b7b54
parent 162180 93fe56269382e012a6b28ea1eb6c90ae90130584
child 162231 5b19ca07781b5cbede4974f4fe46431d47121919
push id25941
push userkwierso@gmail.com
push dateTue, 07 Jan 2014 01:20:54 +0000
treeherdermozilla-central@ce917d3dd7c8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersharth
bugs734666
milestone29.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 734666 - Style Editor default filename for saving. r=harth
browser/devtools/styleeditor/StyleEditorUtil.jsm
browser/devtools/styleeditor/StyleSheetEditor.jsm
--- a/browser/devtools/styleeditor/StyleEditorUtil.jsm
+++ b/browser/devtools/styleeditor/StyleEditorUtil.jsm
@@ -167,18 +167,21 @@ this.wire = function wire(aRoot, aSelect
  * @param boolean toSave
  *        If true, the user is selecting a filename to save.
  * @param nsIWindow parentWindow
  *        Optional parent window. If null the parent window of the file picker
  *        will be the window of the attached input element.
  * @param callback
  *        The callback method, which will be called passing in the selected
  *        file or null if the user did not pick one.
+ * @param AString suggestedFilename
+ *        The suggested filename when toSave is true.
  */
-this.showFilePicker = function showFilePicker(path, toSave, parentWindow, callback)
+this.showFilePicker = function showFilePicker(path, toSave, parentWindow,
+                                              callback, suggestedFilename)
 {
   if (typeof(path) == "string") {
     try {
       if (Services.io.extractScheme(path) == "file") {
         let uri = Services.io.newURI(path, null, null);
         let file = uri.QueryInterface(Ci.nsIFileURL).file;
         callback(file);
         return;
@@ -208,14 +211,18 @@ this.showFilePicker = function showFileP
   let fpCallback = function(result) {
     if (result == Ci.nsIFilePicker.returnCancel) {
       callback(null);
     } else {
       callback(fp.file);
     }
   };
 
+  if (toSave && suggestedFilename) {
+    fp.defaultString = suggestedFilename;
+  }
+
   fp.init(parentWindow, _(key + ".title"), mode);
   fp.appendFilters(_(key + ".filter"), "*.css");
   fp.appendFilters(fp.filterAll);
   fp.open(fpCallback);
   return;
 }
--- a/browser/devtools/styleeditor/StyleSheetEditor.jsm
+++ b/browser/devtools/styleeditor/StyleSheetEditor.jsm
@@ -14,16 +14,17 @@ const Cu = Components.utils;
 const require = Cu.import("resource://gre/modules/devtools/Loader.jsm", {}).devtools.require;
 const Editor  = require("devtools/sourceeditor/editor");
 const promise = require("sdk/core/promise");
 const {CssLogic} = require("devtools/styleinspector/css-logic");
 
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/FileUtils.jsm");
 Cu.import("resource://gre/modules/NetUtil.jsm");
+Cu.import("resource://gre/modules/osfile.jsm");
 Cu.import("resource:///modules/devtools/shared/event-emitter.js");
 Cu.import("resource:///modules/devtools/StyleEditorUtil.jsm");
 
 const LOAD_ERROR = "error-load";
 const SAVE_ERROR = "error-save";
 
 // max update frequency in ms (avoid potential typing lag and/or flicker)
 // @see StyleEditor.updateStylesheet
@@ -354,18 +355,23 @@ StyleSheetEditor.prototype = {
           callback(returnFile);
         }
         this.sourceEditor.setClean();
 
         this.emit("property-change");
       }.bind(this));
     };
 
-    showFilePicker(file || this._styleSheetFilePath, true, this._window, onFile);
-  },
+    let defaultName;
+    if (this._friendlyName) {
+      defaultName = OS.Path.basename(this._friendlyName);
+    }
+    showFilePicker(file || this._styleSheetFilePath, true, this._window,
+                   onFile, defaultName);
+ },
 
   /**
     * Retrieve custom key bindings objects as expected by Editor.
     * Editor action names are not displayed to the user.
     *
     * @return {array} key binding objects for the source editor
     */
   _getKeyBindings: function() {
@@ -389,18 +395,18 @@ StyleSheetEditor.prototype = {
     this.styleSheet.off("property-change", this._onPropertyChange);
     this.styleSheet.off("error", this._onError);
   }
 }
 
 
 const TAB_CHARS = "\t";
 
-const OS = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
-const LINE_SEPARATOR = OS === "WINNT" ? "\r\n" : "\n";
+const CURRENT_OS = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
+const LINE_SEPARATOR = CURRENT_OS === "WINNT" ? "\r\n" : "\n";
 
 /**
  * Prettify minified CSS text.
  * This prettifies CSS code where there is no indentation in usual places while
  * keeping original indentation as-is elsewhere.
  *
  * @param string text
  *        The CSS source to prettify.