Bug 1384180 - Don't animate the stop->reload if the page loads within 150ms (too distracting). r?dao draft
authorJared Wein <jwein@mozilla.com>
Wed, 26 Jul 2017 07:08:00 -0400
changeset 619800 b1392cf72d1cdc17fd3172b0fb48ddf7004a4bce
parent 619763 fcbdff1214b5290a6d9e0eec6c5462ac9952632e
child 641002 3b012d3342ca6cf1705a29180b0d3c0b5bb60fe2
push id71815
push userbmo:jaws@mozilla.com
push dateWed, 02 Aug 2017 16:33:40 +0000
reviewersdao
bugs1384180
milestone57.0a1
Bug 1384180 - Don't animate the stop->reload if the page loads within 150ms (too distracting). r?dao MozReview-Commit-ID: 9KvMCRZo5LZ
browser/base/content/browser.js
--- a/browser/base/content/browser.js
+++ b/browser/base/content/browser.js
@@ -4869,16 +4869,19 @@ var XULBrowserWindow = {
     gIdentityHandler.updateIdentity(this._state, uri);
     TrackingProtection.onSecurityChange(this._state, aIsSimulated);
   },
 
   // simulate all change notifications after switching tabs
   onUpdateCurrentBrowser: function XWB_onUpdateCurrentBrowser(aStateFlags, aStatus, aMessage, aTotalProgress) {
     if (FullZoom.updateBackgroundTabs)
       FullZoom.onLocationChange(gBrowser.currentURI, true);
+
+    CombinedStopReload.onTabSwitch();
+
     var nsIWebProgressListener = Components.interfaces.nsIWebProgressListener;
     var loadingDone = aStateFlags & nsIWebProgressListener.STATE_STOP;
     // use a pseudo-object instead of a (potentially nonexistent) channel for getting
     // a correct error message - and make sure that the UI is always either in
     // loading (STATE_START) or done (STATE_STOP) mode
     this.onStateChange(
       gBrowser.webProgress,
       { URI: gBrowser.currentURI },
@@ -4973,16 +4976,17 @@ var CombinedStopReload = {
 
     this._initialized = true;
     if (XULBrowserWindow.stopCommand.getAttribute("disabled") != "true")
       reload.setAttribute("displaystop", "true");
     stop.addEventListener("click", this);
     this.reload = reload;
     this.stop = stop;
     this.stopReloadContainer = this.reload.parentNode;
+    this.timeWhenSwitchedToStop = 0;
 
     // Disable animations until the browser has fully loaded.
     this.animate = false;
     let startupInfo = Cc["@mozilla.org/toolkit/app-startup;1"]
                         .getService(Ci.nsIAppStartup)
                         .getStartupInfo();
     if (startupInfo.sessionRestored) {
       this.startAnimationPrefMonitoring();
@@ -5040,22 +5044,35 @@ var CombinedStopReload = {
         return;
       this.animate = Services.prefs.getBoolPref("toolkit.cosmeticAnimations.enabled") &&
                      Services.prefs.getBoolPref("browser.stopReloadAnimation.enabled");
       Services.prefs.addObserver("toolkit.cosmeticAnimations.enabled", this);
       this.stopReloadContainer.addEventListener("animationend", this);
     });
   },
 
+  onTabSwitch() {
+    // Reset the time in the event of a tabswitch since the stored time
+    // would have been associated with the previous tab.
+    this.timeWhenSwitchedToStop = 0;
+  },
+
   switchToStop(aRequest, aWebProgress) {
     if (!this._initialized || !this._shouldSwitch(aRequest))
       return;
 
+    // Store the time that we switched to the stop button only if a request
+    // is active. Requests are null if the switch is related to a tabswitch.
+    // This is used to determine if we should show the stop->reload animation.
+    if (aRequest) {
+      this.timeWhenSwitchedToStop = window.performance.now();
+    }
+
     let shouldAnimate = AppConstants.MOZ_PHOTON_ANIMATIONS &&
-                        aRequest instanceof Ci.nsIRequest &&
+                        aRequest &&
                         aWebProgress.isTopLevel &&
                         aWebProgress.isLoadingDocument &&
                         !gBrowser.tabAnimationsInProgress &&
                         this.animate;
 
     this._cancelTransition();
     if (shouldAnimate) {
       BrowserUtils.setToolbarButtonHeightProperty(this.stopReloadContainer);
@@ -5067,20 +5084,21 @@ var CombinedStopReload = {
   },
 
   switchToReload(aRequest, aWebProgress) {
     if (!this._initialized || !this._shouldSwitch(aRequest) ||
         !this.reload.hasAttribute("displaystop"))
       return;
 
     let shouldAnimate = AppConstants.MOZ_PHOTON_ANIMATIONS &&
-                        aRequest instanceof Ci.nsIRequest &&
+                        aRequest &&
                         aWebProgress.isTopLevel &&
                         !aWebProgress.isLoadingDocument &&
                         !gBrowser.tabAnimationsInProgress &&
+                        this._loadTimeExceedsMinimumForAnimation() &&
                         this.animate;
 
     if (shouldAnimate) {
       BrowserUtils.setToolbarButtonHeightProperty(this.stopReloadContainer);
       this.stopReloadContainer.setAttribute("animate", "true");
     } else {
       this.stopReloadContainer.removeAttribute("animate");
     }
@@ -5103,16 +5121,26 @@ var CombinedStopReload = {
     this.reload.disabled = true;
     this._timer = setTimeout(function(self) {
       self._timer = 0;
       self.reload.disabled = XULBrowserWindow.reloadCommand
                                              .getAttribute("disabled") == "true";
     }, 650, this);
   },
 
+  _loadTimeExceedsMinimumForAnimation() {
+    // If the time between switching to the stop button then switching to
+    // the reload button exceeds 150ms, then we will show the animation.
+    // If we don't know when we switched to stop (a tabswitch occured while
+    // the page was loading), then we will not prevent the animation from
+    // occuring.
+    return !this.timeWhenSwitchedToStop ||
+           window.performance.now() - this.timeWhenSwitchedToStop > 150;
+  },
+
   _shouldSwitch(aRequest) {
     if (!aRequest ||
         !aRequest.originalURI ||
         aRequest.originalURI.spec.startsWith("about:reader"))
       return true;
 
     if (aRequest.originalURI.schemeIs("chrome") ||
         aRequest.originalURI.schemeIs("about"))