Bug 1377498 - Add an experimental pref to focus the URL bar from content search access points draft
authorPanos Astithas <past@mozilla.com>
Tue, 01 Aug 2017 14:43:11 +0300
changeset 619242 841d48e99af94d9fa761cc98700a58656b2df0d9
parent 618757 44121dbcac6a9d3ff18ed087a09b3205e5a04db1
child 640348 266764595bb14c41bfd8049d2f499a16a1415d17
push id71624
push userbmo:past@mozilla.com
push dateTue, 01 Aug 2017 19:56:01 +0000
bugs1377498
milestone56.0a1
Bug 1377498 - Add an experimental pref to focus the URL bar from content search access points MozReview-Commit-ID: fVHtg4rZNc
browser/app/profile/firefox.js
browser/base/content/contentSearchUI.js
browser/modules/ContentSearch.jsm
--- a/browser/app/profile/firefox.js
+++ b/browser/app/profile/firefox.js
@@ -411,16 +411,20 @@ pref("browser.search.context.loadInBackg
 pref("browser.search.hiddenOneOffs", "");
 
 // Mirrors whether the search-container widget is in the navigation toolbar. The
 // default value of this preference must match the DEFAULT_AREA_PLACEMENTS of
 // UITelemetry.jsm, the navbarPlacements of CustomizableUI.jsm, and the
 // position and attributes of the search-container element in browser.xul.
 pref("browser.search.widget.inNavBar", true);
 
+// This makes content search access points (home, new tab) move focus to the
+// URL bar when they receive a key press.
+pref("browser.search.content.focusUrlbar", false);
+
 #ifndef RELEASE_OR_BETA
 pref("browser.search.reset.enabled", true);
 #endif
 
 pref("browser.sessionhistory.max_entries", 50);
 
 // Built-in default permissions.
 pref("permissions.manager.defaultsUrl", "resource://app/defaults/permissions");
--- a/browser/base/content/contentSearchUI.js
+++ b/browser/base/content/contentSearchUI.js
@@ -282,25 +282,17 @@ ContentSearchUIController.prototype = {
       }
     }
 
     this._sendMsg("Search", eventData);
     this.addInputValueToFormHistory();
   },
 
   _onInput() {
-    if (!this.input.value) {
-      this._stickyInputValue = "";
-      this._hideSuggestions();
-    } else if (this.input.value != this._stickyInputValue) {
-      // Only fetch new suggestions if the input value has changed.
-      this._getSuggestions();
-      this.selectAndUpdateInput(-1);
-    }
-    this._updateSearchWithHeader();
+    this._focusLocationBar(this.input.value);
   },
 
   _onKeypress(event) {
     let selectedIndexDelta = 0;
     let selectedSuggestionDelta = 0;
     let selectedOneOffDelta = 0;
 
     switch (event.keyCode) {
@@ -634,16 +626,20 @@ ContentSearchUIController.prototype = {
   },
 
   _speculativeConnect() {
     if (this.defaultEngine) {
       this._sendMsg("SpeculativeConnect", this.defaultEngine.name);
     }
   },
 
+  _focusLocationBar(input) {
+    this._sendMsg("FocusLocationBar", input);
+  },
+
   _makeTableRow(type, suggestionStr, currentRow, searchWords) {
     let row = document.createElementNS(HTML_NS, "tr");
     row.dir = "auto";
     row.classList.add("contentSearchSuggestionRow");
     row.classList.add(type);
     row.setAttribute("role", "presentation");
     row.addEventListener("mousemove", this);
     row.addEventListener("mouseup", this);
--- a/browser/modules/ContentSearch.jsm
+++ b/browser/modules/ContentSearch.jsm
@@ -57,16 +57,19 @@ const MAX_SUGGESTIONS = 6;
  *     cancelled.
  *     data: { engineName, searchString, healthReportKey, searchPurpose }
  *   SetCurrentEngine
  *     Sets the current engine.
  *     data: the name of the engine
  *   SpeculativeConnect
  *     Speculatively connects to an engine.
  *     data: the name of the engine
+ *   FocusLocationBar
+ *     Moves focus to the location bar.
+ *     data: any input text to set the field to
  *
  * Outbound messages have the following types:
  *
  *   CurrentEngine
  *     Broadcast when the current engine changes.
  *     data: see _currentEngineObj
  *   CurrentState
  *     Broadcast when the current search state changes.
@@ -449,16 +452,25 @@ this.ContentSearch = {
     if (msg.target.contentWindow) {
       engine.speculativeConnect({
         window: msg.target.contentWindow,
         originAttributes: msg.target.contentPrincipal.originAttributes
       });
     }
   },
 
+  _onMessageFocusLocationBar(msg, input) {
+    if (Services.prefs.getBoolPref("browser.search.content.focusUrlbar", false)) {
+      msg.target.ownerGlobal.focusAndSelectUrlBar();
+      if (input) {
+        msg.target.ownerGlobal.gURLBar.value = input;
+      }
+    }
+  },
+
   async _onObserve(data) {
     if (data === "engine-current") {
       let engine = await this._currentEngineObj();
       this._broadcast("CurrentEngine", engine);
     } else if (data !== "engine-default") {
       // engine-default is always sent with engine-current and isn't otherwise
       // relevant to content searches.
       let state = await this.currentStateObj();