Bug 994724 - Clean up ZoomHelper logic. r=maragrete
authorRobin Ricard <ricard.robin@gmail.com>
Mon, 13 Oct 2014 23:59:00 +0200
changeset 210601 39cc3da6233e7a502c2e61b0722cd60e461d87e6
parent 210600 8a7b0e87d26d0792b5e66caff652d8f30c1b2452
child 210602 92a91ac105d1b4ab8cdc5b7bab647c043d3da79b
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmaragrete
bugs994724
milestone36.0a1
Bug 994724 - Clean up ZoomHelper logic. r=maragrete
mobile/android/chrome/content/FindHelper.js
mobile/android/chrome/content/ZoomHelper.js
mobile/android/chrome/content/aboutReader.js
--- a/mobile/android/chrome/content/FindHelper.js
+++ b/mobile/android/chrome/content/FindHelper.js
@@ -73,13 +73,13 @@ var FindHelper = {
           Cu.reportError("Warning: selected tab changed during find!");
           // fall through and restore viewport on the initial tab anyway
         }
         this._targetTab.setViewport(JSON.parse(this._initialViewport));
         this._targetTab.sendViewportUpdate();
       }
     } else {
       // Disabled until bug 1014113 is fixed
-      //ZoomHelper.zoomToRect(aData.rect, -1, false, true);
+      // ZoomHelper.zoomToRect(aData.rect);
       this._viewportChanged = true;
     }
   }
 };
--- a/mobile/android/chrome/content/ZoomHelper.js
+++ b/mobile/android/chrome/content/ZoomHelper.js
@@ -76,72 +76,76 @@ var ZoomHelper = {
             dw > minDifference && dw < maxDifference);
   },
 
   /* Zoom to an element, optionally keeping a particular part of it
    * in view if it is really tall.
    */
   zoomToElement: function(aElement, aClickY = -1, aCanZoomOut = true, aCanScrollHorizontally = true) {
     let rect = ElementTouchHelper.getBoundingContentRect(aElement);
-    ZoomHelper.zoomToRect(rect, aClickY, aCanZoomOut, aCanScrollHorizontally, aElement);
-  },
 
-  zoomToRect: function(aRect, aClickY = -1, aCanZoomOut = true, aCanScrollHorizontally = true, aElement) {
     const margin = 15;
 
-    if(!aRect.h || !aRect.w) {
-      aRect.h = aRect.height;
-      aRect.w = aRect.width;
-    }
 
     let viewport = BrowserApp.selectedTab.getViewport();
-    let bRect = new Rect(aCanScrollHorizontally ? Math.max(viewport.cssPageLeft, aRect.x - margin) : viewport.cssX,
-                         aRect.y,
-                         aCanScrollHorizontally ? aRect.w + 2 * margin : viewport.cssWidth,
-                         aRect.h);
+    rect = new Rect(aCanScrollHorizontally ? Math.max(viewport.cssPageLeft, rect.x - margin) : viewport.cssX,
+                    rect.y,
+                    aCanScrollHorizontally ? rect.w + 2 * margin : viewport.cssWidth,
+                    rect.h);
     // constrict the rect to the screen's right edge
-    bRect.width = Math.min(bRect.width, viewport.cssPageRight - bRect.x);
+    rect.width = Math.min(rect.width, viewport.cssPageRight - rect.x);
 
     // if the rect is already taking up most of the visible area and is stretching the
     // width of the page, then we want to zoom out instead.
     if (aElement) {
       if (BrowserEventHandler.mReflozPref) {
         let zoomFactor = BrowserApp.selectedTab.getZoomToMinFontSize(aElement);
 
-        bRect.width = zoomFactor <= 1.0 ? bRect.width : gScreenWidth / zoomFactor;
-        bRect.height = zoomFactor <= 1.0 ? bRect.height : bRect.height / zoomFactor;
-        if (zoomFactor == 1.0 || ZoomHelper.isRectZoomedIn(bRect, viewport)) {
+        rect.width = zoomFactor <= 1.0 ? rect.width : gScreenWidth / zoomFactor;
+        rect.height = zoomFactor <= 1.0 ? rect.height : rect.height / zoomFactor;
+        if (zoomFactor == 1.0 || ZoomHelper.isRectZoomedIn(rect, viewport)) {
           if (aCanZoomOut) {
             ZoomHelper.zoomOut();
           }
           return;
         }
-      } else if (ZoomHelper.isRectZoomedIn(bRect, viewport)) {
+      } else if (ZoomHelper.isRectZoomedIn(rect, viewport)) {
         if (aCanZoomOut) {
           ZoomHelper.zoomOut();
         }
         return;
       }
+
+      ZoomHelper.zoomToRect(rect, aClickY);
     }
+  },
 
-    let rect = {};
+  /* Zoom to a specific part of the screen defined by a rect,
+   * optionally keeping a particular part of it in view
+   * if it is really tall.
+   */
+  zoomToRect: function(aRect, aClickY = -1) {
+    let viewport = BrowserApp.selectedTab.getViewport();
+
+    let rect = {
+      x: aRect.x,
+      y: aRect.y,
+      w: aRect.width,
+      h: Math.min(aRect.width * viewport.cssHeight / viewport.cssWidth, aRect.height)
+    };
 
     rect.type = "Browser:ZoomToRect";
-    rect.x = bRect.x;
-    rect.y = bRect.y;
-    rect.w = bRect.width;
-    rect.h = Math.min(bRect.width * viewport.cssHeight / viewport.cssWidth, bRect.height);
 
     if (aClickY >= 0) {
       // if the block we're zooming to is really tall, and we want to keep a particular
       // part of it in view, then adjust the y-coordinate of the target rect accordingly.
-      // the 1.2 multiplier is just a little fuzz to compensate for bRect including horizontal
+      // the 1.2 multiplier is just a little fuzz to compensate for aRect including horizontal 
       // margins but not vertical ones.
       let cssTapY = viewport.cssY + aClickY;
-      if ((bRect.height > rect.h) && (cssTapY > rect.y + (rect.h * 1.2))) {
+      if ((aRect.height > rect.h) && (cssTapY > rect.y + (rect.h * 1.2))) {
         rect.y = cssTapY - (rect.h / 2);
       }
     }
 
     if (rect.w > viewport.cssWidth || rect.h > viewport.cssHeight) {
       BrowserEventHandler.resetMaxLineBoxWidth();
     }
 
--- a/mobile/android/chrome/content/aboutReader.js
+++ b/mobile/android/chrome/content/aboutReader.js
@@ -290,17 +290,17 @@ AboutReader.prototype = {
   _scrollPage: function Reader_scrollPage(scrollByPixels) {
     let viewport = BrowserApp.selectedTab.getViewport();
     let newY = Math.min(Math.max(viewport.cssY + scrollByPixels, viewport.cssPageTop), viewport.cssPageBottom);
     let newRect = new Rect(viewport.cssX, newY, viewport.cssWidth, viewport.cssHeight);
 
     this._setToolbarVisibility(false);
     this._setBrowserToolbarVisiblity(false);
     this._scrolled  = true;
-    ZoomHelper.zoomToRect(newRect, -1, false, false);
+    ZoomHelper.zoomToRect(newRect, -1);
   },
 
   _updateToggleButton: function Reader_updateToggleButton() {
     let classes = this._doc.getElementById("toggle-button").classList;
 
     if (this._isReadingListItem == 1) {
       classes.add("on");
     } else {