Bug 1525318 - Quantumbar: Make clicking one off buttons work. r=Standard8
authorDão Gottwald <dao@mozilla.com>
Thu, 14 Feb 2019 15:46:16 +0000
changeset 459145 a420bc5350d7ac1c8e25ef5c53d7e35339b30252
parent 459144 39bb681343d6f429ced7ea706649e48890d27c28
child 459146 fdb6f449e8c0858442997e9fdf5d35fbc35d5fec
push id35556
push userdvarga@mozilla.com
push dateFri, 15 Feb 2019 01:38:24 +0000
treeherdermozilla-central@b29c87add05f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersStandard8
bugs1525318
milestone67.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 1525318 - Quantumbar: Make clicking one off buttons work. r=Standard8 Differential Revision: https://phabricator.services.mozilla.com/D19776
browser/components/urlbar/UrlbarInput.jsm
browser/components/urlbar/UrlbarUtils.jsm
--- a/browser/components/urlbar/UrlbarInput.jsm
+++ b/browser/components/urlbar/UrlbarInput.jsm
@@ -212,53 +212,64 @@ class UrlbarInput {
    */
   handleCommand(event, openWhere, openParams = {}, triggeringPrincipal = null) {
     let isMouseEvent = event instanceof this.window.MouseEvent;
     if (isMouseEvent && event.button == 2) {
       // Do nothing for right clicks.
       return;
     }
 
-    // TODO: Hook up one-off button handling.
     // Determine whether to use the selected one-off search button.  In
     // one-off search buttons parlance, "selected" means that the button
     // has been navigated to via the keyboard.  So we want to use it if
     // the triggering event is not a mouse click -- i.e., it's a Return
     // key -- or if the one-off was mouse-clicked.
-    // let selectedOneOff = this.popup.oneOffSearchButtons.selectedButton;
-    // if (selectedOneOff &&
-    //     isMouseEvent &&
-    //     event.originalTarget != selectedOneOff) {
-    //   selectedOneOff = null;
-    // }
-    //
-    // // Do the command of the selected one-off if it's not an engine.
-    // if (selectedOneOff && !selectedOneOff.engine) {
-    //   selectedOneOff.doCommand();
-    //   return;
-    // }
+    let selectedOneOff;
+    if (this.view.isOpen) {
+      selectedOneOff = this.view.oneOffSearchButtons.selectedButton;
+      if (selectedOneOff &&
+          isMouseEvent &&
+          event.target != selectedOneOff) {
+        selectedOneOff = null;
+      }
+      // Do the command of the selected one-off if it's not an engine.
+      if (selectedOneOff && !selectedOneOff.engine) {
+        selectedOneOff.doCommand();
+        return;
+      }
+    }
 
-    // Use the selected result if we have one; this should always be the case
+    // Use the selected result if we have one; this is usually the case
     // when the view is open.
-    let result = this.view.selectedResult;
+    let result = !selectedOneOff && this.view.selectedResult;
     if (result) {
       this.pickResult(event, result);
       return;
     }
 
-    // Use the current value if we don't have a UrlbarResult e.g. because the
-    // view is closed.
-    let url = this.value;
+    let url;
+    if (selectedOneOff) {
+      // If there's a selected one-off button then load a search using
+      // the button's engine.
+      [url, openParams.postData] = UrlbarUtils.getSearchQueryUrl(
+        selectedOneOff.engine, this._lastSearchString);
+      this._recordSearch(selectedOneOff.engine, event);
+    } else {
+      // Use the current value if we don't have a UrlbarResult e.g. because the
+      // view is closed.
+      url = this.value;
+      openParams.postData = null;
+    }
+
     if (!url) {
       return;
     }
 
     let where = openWhere || this._whereToOpen(event);
 
-    openParams.postData = null;
     openParams.allowInheritPrincipal = false;
 
     // TODO: Work out how we get the user selection behavior, probably via passing
     // it in, since we don't have the old autocomplete controller to work with.
     // BrowserUsageTelemetry.recordUrlbarSelectedResultMethod(
     //   event, this.userSelectionBehavior);
 
     url = this._maybeCanonizeURL(event, url) || url.trim();
--- a/browser/components/urlbar/UrlbarUtils.jsm
+++ b/browser/components/urlbar/UrlbarUtils.jsm
@@ -251,25 +251,41 @@ var UrlbarUtils = {
       case UrlbarUtils.RESULT_TYPE.KEYWORD:
         return {
           url: result.payload.url,
           postData: result.payload.postData ?
             this.getPostDataStream(result.payload.postData) : null,
         };
       case UrlbarUtils.RESULT_TYPE.SEARCH: {
         const engine = Services.search.getEngineByName(result.payload.engine);
-        let [url, postData] = getSearchQueryUrl(
+        let [url, postData] = this.getSearchQueryUrl(
           engine, result.payload.suggestion || result.payload.query);
         return {url, postData};
       }
     }
     return {url: null, postData: null};
   },
 
   /**
+   * Get the url to load for the search query.
+   *
+   * @param {nsISearchEngine} engine
+   *   The engine to generate the query for.
+   * @param {string} query
+   *   The query string to search for.
+   * @returns {array}
+   *   Returns an array containing the query url (string) and the
+   *    post data (object).
+   */
+  getSearchQueryUrl(engine, query) {
+    let submission = engine.getSubmission(query, null, "keyword");
+    return [submission.uri.spec, submission.postData];
+  },
+
+  /**
    * Tries to initiate a speculative connection to a given url.
    * @param {nsISearchEngine|nsIURI|URL|string} urlOrEngine entity to initiate
    *        a speculative connection for.
    * @param {window} window the window from where the connection is initialized.
    * @note This is not infallible, if a speculative connection cannot be
    *       initialized, it will be a no-op.
    */
   setupSpeculativeConnection(urlOrEngine, window) {
@@ -321,33 +337,16 @@ var UrlbarUtils = {
 
       pasteData = pasteData.substring(pasteData.indexOf(":") + 1);
     }
     return pasteData;
   },
 };
 
 /**
- * Get the url to load for the search query and records in telemetry that it
- * is being loaded.
- *
- * @param {nsISearchEngine} engine
- *   The engine to generate the query for.
- * @param {string} query
- *   The query string to search for.
- * @returns {array}
- *   Returns an array containing the query url (string) and the
- *    post data (object).
- */
-function getSearchQueryUrl(engine, query) {
-  let submission = engine.getSubmission(query, null, "keyword");
-  return [submission.uri.spec, submission.postData];
-}
-
-/**
  * UrlbarQueryContext defines a user's autocomplete input from within the urlbar.
  * It supplements it with details of how the search results should be obtained
  * and what they consist of.
  */
 class UrlbarQueryContext {
   /**
    * Constructs the UrlbarQueryContext instance.
    *