Bug 1494274 - Port clickSelectsAll and doubleClickSelectsAll behavior from urlbarBindings.xml to UrlbarInput.jsm. r=mak
authorDão Gottwald <dao@mozilla.com>
Fri, 28 Sep 2018 17:57:53 +0200
changeset 494493 91d895e3296e015b520127645aa8345d26510b1d
parent 494492 64129f7c59689e0201fc9fb94c233c0d73edf17f
child 494494 ddd66696c9f2979e6bde0137adf5d855b4ade04d
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1494274
milestone64.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 1494274 - Port clickSelectsAll and doubleClickSelectsAll behavior from urlbarBindings.xml to UrlbarInput.jsm. r=mak
browser/components/urlbar/UrlbarInput.jsm
browser/components/urlbar/UrlbarPrefs.jsm
--- a/browser/components/urlbar/UrlbarInput.jsm
+++ b/browser/components/urlbar/UrlbarInput.jsm
@@ -12,16 +12,20 @@ XPCOMUtils.defineLazyModuleGetters(this,
   PrivateBrowsingUtils: "resource://gre/modules/PrivateBrowsingUtils.jsm",
   QueryContext: "resource:///modules/UrlbarController.jsm",
   Services: "resource://gre/modules/Services.jsm",
   UrlbarController: "resource:///modules/UrlbarController.jsm",
   UrlbarPrefs: "resource:///modules/UrlbarPrefs.jsm",
   UrlbarView: "resource:///modules/UrlbarView.jsm",
 });
 
+XPCOMUtils.defineLazyServiceGetter(this, "ClipboardHelper",
+                                   "@mozilla.org/widget/clipboardhelper;1",
+                                   "nsIClipboardHelper");
+
 /**
  * Represents the urlbar <textbox>.
  * Also forwards important textbox properties and methods.
  */
 class UrlbarInput {
   /**
    * @param {object} options
    *   The initial options for UrlbarInput.
@@ -30,16 +34,18 @@ class UrlbarInput {
    * @param {object} options.panel
    *   The <panel> element.
    * @param {UrlbarController} [options.controller]
    *   Optional fake controller to override the built-in UrlbarController.
    *   Intended for use in unit tests only.
    */
   constructor(options = {}) {
     this.textbox = options.textbox;
+    this.textbox.clickSelectsAll = UrlbarPrefs.get("clickSelectsAll");
+
     this.panel = options.panel;
     this.window = this.textbox.ownerGlobal;
     this.controller = options.controller || new UrlbarController();
     this.view = new UrlbarView(this);
     this.valueIsTyped = false;
     this.userInitiatedFocus = false;
     this.isPrivate = PrivateBrowsingUtils.isWindowPrivate(this.window);
 
@@ -85,20 +91,21 @@ class UrlbarInput {
             return this[setter](val);
           }
           return this.textbox[property] = val;
         },
       });
     }
 
     this.addEventListener("input", this);
-    this.inputField.addEventListener("select", this);
+    this.inputField.addEventListener("mousedown", this);
     this.inputField.addEventListener("overflow", this);
     this.inputField.addEventListener("underflow", this);
     this.inputField.addEventListener("scrollend", this);
+    this.inputField.addEventListener("select", this);
 
     this.inputField.controllers.insertControllerAt(0, new CopyCutController(this));
   }
 
   /* Shortens the given value, usually by removing http:// and trailing slashes,
    * such that calling nsIURIFixup::createFixupURI with the result will produce
    * the same URI.
    *
@@ -296,16 +303,25 @@ class UrlbarInput {
       }
     }
 
     return action;
   }
 
   // Event handlers below.
 
+  _onmousedown(event) {
+    if (event.button == 0 &&
+        event.detail == 2 &&
+        UrlbarPrefs.get("doubleClickSelectsAll")) {
+      this.editor.selectAll();
+      event.preventDefault();
+    }
+  }
+
   _oninput(event) {
     this.valueIsTyped = true;
 
     // XXX Fill in lastKey, and add anything else we need.
     this.controller.handleQuery(new QueryContext({
       searchString: event.target.value,
       lastKey: "",
       maxResults: UrlbarPrefs.get("maxRichResults"),
@@ -322,17 +338,17 @@ class UrlbarInput {
       return;
     }
 
     let val = this._getSelectedValueForClipboard();
     if (!val) {
       return;
     }
 
-    Services.clipboard.copyStringToClipboard(val, Services.clipboard.kSelectionClipboard);
+    ClipboardHelper.copyStringToClipboard(val, Services.clipboard.kSelectionClipboard);
   }
 
   _onoverflow(event) {
     const targetIsPlaceholder =
       !event.originalTarget.classList.contains("anonymous-div");
     // We only care about the non-placeholder text.
     // This shouldn't be needed, see bug 1487036.
     if (targetIsPlaceholder) {
@@ -391,19 +407,17 @@ class CopyCutController {
 
       let event = urlbar.window.document.createEvent("UIEvents");
       event.initUIEvent("input", true, false, this.window, 0);
       urlbar.dispatchEvent(event);
 
       urlbar.window.SetPageProxyState("invalid");
     }
 
-    Cc["@mozilla.org/widget/clipboardhelper;1"]
-      .getService(Ci.nsIClipboardHelper)
-      .copyString(val);
+    ClipboardHelper.copyString(val);
   }
 
   /**
    * @param {string} command
    * @returns {boolean}
    *   Whether the command is handled by this controller.
    */
   supportsCommand(command) {
--- a/browser/components/urlbar/UrlbarPrefs.jsm
+++ b/browser/components/urlbar/UrlbarPrefs.jsm
@@ -38,21 +38,31 @@ const PREF_URLBAR_DEFAULTS = new Map([
   // autofilled even if the user hasn't actually visited them.
   ["autoFill.searchEngines", false],
 
   // Affects the frecency threshold of the autofill algorithm.  The threshold is
   // the mean of all origin frecencies plus one standard deviation multiplied by
   // this value.  See UnifiedComplete.
   ["autoFill.stddevMultiplier", [0.0, "getFloatPref"]],
 
+  // If true, this optimizes for replacing the full URL rather than editing
+  // part of it. This also copies the urlbar value to the selection clipboard
+  // on systems that support it.
+  ["clickSelectsAll", false],
+
   // The amount of time (ms) to wait after the user has stopped typing before
   // fetching results.  However, we ignore this for the very first result (the
   // "heuristic" result).  We fetch it as fast as possible.
   ["delay", 50],
 
+  // If true, this optimizes for replacing the full URL rather than selecting a
+  // portion of it. This also copies the urlbar value to the selection
+  // clipboard on systems that support it.
+  ["doubleClickSelectsAll", false],
+
   // When true, `javascript:` URLs are not included in search results.
   ["filter.javascript", true],
 
   // Allows results from one search to be reused in the next search.  One of the
   // INSERTMETHOD values.
   ["insertMethod", UrlbarUtils.INSERTMETHOD.MERGE_RELATED],
 
   // Controls how URLs are matched against the user's search string.  See