Bug 1318830 - Listen for the FullZoomChange event on synthetic documents to trigger updating the zoom-control in the location bar. r?gijs draft
authorJared Wein <jwein@mozilla.com>
Tue, 07 Mar 2017 14:24:17 -0500
changeset 494765 c56686abba0dc356e5a1e63b81b9ec9e2a6dc8c7
parent 494260 6583496f169cd8a13c531ed16e98e8bf313eda8e
child 548189 5b87bcf585c4e84a915a1af55d44123734563090
push id48123
push userjwein@mozilla.com
push dateTue, 07 Mar 2017 19:25:05 +0000
reviewersgijs
bugs1318830
milestone54.0a1
Bug 1318830 - Listen for the FullZoomChange event on synthetic documents to trigger updating the zoom-control in the location bar. r?gijs MozReview-Commit-ID: CeZ8qsz97Yz
browser/modules/URLBarZoom.jsm
toolkit/content/browser-content.js
--- a/browser/modules/URLBarZoom.jsm
+++ b/browser/modules/URLBarZoom.jsm
@@ -21,42 +21,51 @@ var URLBarZoom = {
 function fullZoomObserver(aSubject, aTopic) {
   // If the tab was the last one in its window and has been dragged to another
   // window, the original browser's window will be unavailable here. Since that
   // window is closing, we can just ignore this notification.
   if (!aSubject.ownerGlobal) {
     return;
   }
 
-  // Only allow pulse animation for zoom changes, not tab switching.
   let animate = (aTopic != "browser-fullZoom:location-change");
   updateZoomButton(aSubject, animate);
 }
 
 function onEndSwapDocShells(event) {
   updateZoomButton(event.originalTarget);
 }
 
-function updateZoomButton(aBrowser, aAnimate = false) {
+  /**
+   * Updates the zoom button in the location bar.
+   *
+   * @param {object} aBrowser The browser that the zoomed content resides in.
+   * @param {boolean} aAnimate Should be True for all cases unless the zoom
+   *   change is related to tab switching. Optional
+   * @param {number} aValue The value that should be used for the zoom control.
+   *   If not provided then the value will be read from the window. Useful
+   *   if the data on the window may be stale.
+   */
+function updateZoomButton(aBrowser, aAnimate = false, aValue = undefined) {
   let win = aBrowser.ownerGlobal;
   if (aBrowser != win.gBrowser.selectedBrowser) {
     return;
   }
 
   let customizableZoomControls = win.document.getElementById("zoom-controls");
   let zoomResetButton = win.document.getElementById("urlbar-zoom-button");
 
   // Ensure that zoom controls haven't already been added to browser in Customize Mode
   if (customizableZoomControls &&
       customizableZoomControls.getAttribute("cui-areatype") == "toolbar") {
     zoomResetButton.hidden = true;
     return;
   }
 
-  let zoomFactor = Math.round(win.ZoomManager.zoom * 100);
+  let zoomFactor = Math.round((aValue || win.ZoomManager.zoom) * 100);
   if (zoomFactor != 100) {
     // Check if zoom button is visible and update label if it is
     if (zoomResetButton.hidden) {
       zoomResetButton.hidden = false;
     }
     if (aAnimate) {
       zoomResetButton.setAttribute("animate", "true");
     } else {
@@ -68,8 +77,11 @@ function updateZoomButton(aBrowser, aAni
     // Hide button if zoom is at 100%
     zoomResetButton.hidden = true;
   }
 }
 
 Services.obs.addObserver(fullZoomObserver, "browser-fullZoom:zoomChange", false);
 Services.obs.addObserver(fullZoomObserver, "browser-fullZoom:zoomReset", false);
 Services.obs.addObserver(fullZoomObserver, "browser-fullZoom:location-change", false);
+Services.mm.addMessageListener("SyntheticDocument:ZoomChange", function(aMessage) {
+  updateZoomButton(aMessage.target, true, aMessage.data.value);
+});
--- a/toolkit/content/browser-content.js
+++ b/toolkit/content/browser-content.js
@@ -1772,16 +1772,30 @@ let DateTimePickerListener = {
       default:
         break;
     }
   },
 }
 
 DateTimePickerListener.init();
 
+let URLBarZoom = {
+  init() {
+    addEventListener("FullZoomChange", e => {
+      if (!e.target.mozSyntheticDocument) {
+        return;
+      }
+      sendSyncMessage("SyntheticDocument:ZoomChange",
+        {"value": docShell.contentViewer.fullZoom});
+    });
+  }
+};
+
+URLBarZoom.init();
+
 addEventListener("mozshowdropdown", event => {
   if (!event.isTrusted)
     return;
 
   if (!SelectContentHelper.open) {
     new SelectContentHelper(event.target, {isOpenedViaTouch: false}, this);
   }
 });