Bug 1067903 - Part 1: Autoselect first autocomplete result. r=mak
authorBlair McBride <bmcbride@mozilla.com>
Mon, 24 Nov 2014 12:18:20 +1300
changeset 241408 a3829376e88f2983d2b0fb886f733989151cc4f4
parent 241407 2acc754928c80d372ca1f70f5e1f2694fc2639b3
child 241409 77a8dfd5559e2966370ce9144e6c47c8da10f97a
push id4311
push userraliiev@mozilla.com
push dateMon, 12 Jan 2015 19:37:41 +0000
treeherdermozilla-beta@150c9fed433b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmak
bugs1067903
milestone36.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 1067903 - Part 1: Autoselect first autocomplete result. r=mak
browser/base/content/urlbarBindings.xml
--- a/browser/base/content/urlbarBindings.xml
+++ b/browser/base/content/urlbarBindings.xml
@@ -927,16 +927,45 @@
       <field name="_maxResults">0</field>
 
       <field name="_bundle" readonly="true">
         Cc["@mozilla.org/intl/stringbundle;1"].
           getService(Ci.nsIStringBundleService).
           createBundle("chrome://browser/locale/places/places.properties");
       </field>
 
+      <!-- Override this so that navigating between items results in an item
+           always being selected. This is contrary to the normal behaviour where
+           if you navigate beyond either end of the list, no item will be
+           selected. -->
+      <method name="getNextIndex">
+        <parameter name="reverse"/>
+        <parameter name="amount"/>
+        <parameter name="index"/>
+        <parameter name="maxRow"/>
+        <body><![CDATA[
+          if (maxRow < 0)
+            return -1;
+
+          let newIndex = index + (reverse ? -1 : 1) * amount;
+
+          // We don't want to wrap if navigation in any direction by one item.
+          // Otherwise we clamp to one end of the list.
+          // ie, hitting page-down will only cause is to wrap if we're already
+          // at one end of the list.
+          if (newIndex < 0) {
+            newIndex = index > 0 ? 0 : maxRow;
+          } else if (newIndex > maxRow) {
+            newIndex = index < maxRow ? maxRow : 0;
+          }
+
+          return newIndex;
+        ]]></body>
+      </method>
+
       <property name="maxResults" readonly="true">
         <getter>
           <![CDATA[
             if (!this._maxResults) {
               var prefService =
                 Components.classes["@mozilla.org/preferences-service;1"]
                           .getService(Components.interfaces.nsIPrefBranch);
               this._maxResults = prefService.getIntPref("browser.urlbar.maxRichResults");
@@ -1034,16 +1063,28 @@
               // Undefined result label, do nothing.
             }
 
             return label;
           ]]>
         </body>
       </method>
 
+      <method name="onResultsAdded">
+        <body>
+          <![CDATA[
+            if (!Services.prefs.getBoolPref("browser.urlbar.unifiedcomplete"))
+              return;
+
+            if (this._matchCount > 0 && this.selectedIndex == -1)
+              this.selectedIndex = 0;
+          ]]>
+        </body>
+      </method>
+
     </implementation>
   </binding>
 
   <binding id="addon-progress-notification" extends="chrome://global/content/bindings/notification.xml#popup-notification">
     <content align="start">
       <xul:image class="popup-notification-icon"
                  xbl:inherits="popupid,src=icon"/>
       <xul:vbox flex="1">