Bug 802640 - Break out a zoomToElement function from the DoubleTap handler. r=wesj
authorKartikaya Gupta <kgupta@mozilla.com>
Tue, 23 Oct 2012 17:51:22 -0400
changeset 111318 8459554cbfe92f2a66eb793b31dde575c7676232
parent 111317 82d78390fda1a69232f8d1bf0035a9c65e49d869
child 111319 02bcbfb2838365858dc41a9488d0d253b2e53652
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewerswesj
bugs802640
milestone19.0a1
Bug 802640 - Break out a zoomToElement function from the DoubleTap handler. r=wesj
mobile/android/chrome/content/browser.js
--- a/mobile/android/chrome/content/browser.js
+++ b/mobile/android/chrome/content/browser.js
@@ -3785,53 +3785,60 @@ var BrowserEventHandler = {
     }
 
     while (element && !this._shouldZoomToElement(element))
       element = element.parentNode;
 
     if (!element) {
       this._zoomOut();
     } else {
-      const margin = 15;
-      let rect = ElementTouchHelper.getBoundingContentRect(element);
-
-      let viewport = BrowserApp.selectedTab.getViewport();
-      let bRect = new Rect(Math.max(viewport.cssPageLeft, rect.x - margin),
-                           rect.y,
-                           rect.w + 2 * margin,
-                           rect.h);
-      // constrict the rect to the screen's right edge
-      bRect.width = Math.min(bRect.width, viewport.cssPageRight - bRect.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 (this._isRectZoomedIn(bRect, viewport)) {
-        this._zoomOut();
-        return;
-      }
-
-      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 the block we're zooming to is really tall, and the user double-tapped
-      // more than a screenful of height from the top of it, then adjust the y-coordinate
-      // so that we center the actual point the user double-tapped upon. this prevents
-      // flying to the top of a page when double-tapping to zoom in (bug 761721).
+      this._zoomToElement(element, data.y);
+    }
+  },
+
+  /* Zoom to an element, optionally keeping a particular part of it
+   * in view if it is really tall.
+   */
+  _zoomToElement: function(aElement, aClickY = -1) {
+    const margin = 15;
+    let rect = ElementTouchHelper.getBoundingContentRect(aElement);
+
+    let viewport = BrowserApp.selectedTab.getViewport();
+    let bRect = new Rect(Math.max(viewport.cssPageLeft, rect.x - margin),
+                         rect.y,
+                         rect.w + 2 * margin,
+                         rect.h);
+    // constrict the rect to the screen's right edge
+    bRect.width = Math.min(bRect.width, viewport.cssPageRight - bRect.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 (this._isRectZoomedIn(bRect, viewport)) {
+      this._zoomOut();
+      return;
+    }
+
+    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
       // margins but not vertical ones.
-      let cssTapY = viewport.cssY + data.y;
+      let cssTapY = viewport.cssY + aClickY;
       if ((bRect.height > rect.h) && (cssTapY > rect.y + (rect.h * 1.2))) {
         rect.y = cssTapY - (rect.h / 2);
       }
-
-      sendMessageToJava({ gecko: rect });
-    }
+    }
+
+    sendMessageToJava({ gecko: rect });
   },
 
   _zoomInAndSnapToElement: function(aX, aY, aElement) {
     let viewport = BrowserApp.selectedTab.getViewport();
     if (viewport.zoom < 1.0) {
       // We don't want to do this on zoom out.
       return;
     }