Bug 597230 - Update zoom and scroll when the viewport resized [r=mfinkle]
authorMatt Brubeck <mbrubeck@mozilla.com>
Thu, 23 Sep 2010 14:20:44 -0700
changeset 66704 3922870bd52511eb71db460f3bd0c992fe7e63a2
parent 66703 16cb55545f6601dc897c0880d3f3294c0752f606
child 66705 8711040ec26f61470fb48c8539ceb65794b3012f
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmfinkle
bugs597230
Bug 597230 - Update zoom and scroll when the viewport resized [r=mfinkle]
mobile/chrome/content/browser.js
--- a/mobile/chrome/content/browser.js
+++ b/mobile/chrome/content/browser.js
@@ -222,40 +222,41 @@ var Browser = {
       let index = stylesheet.insertRule("." + style + " {}", stylesheet.cssRules.length);
       this.styles[style] = stylesheet.cssRules[index].style;
     }
 
     function resizeHandler(e) {
       if (e.target != window)
         return;
 
-      // XXX is this code right here actually needed?
       let w = window.innerWidth;
       let h = window.innerHeight;
+
+      // XXX is this code right here actually needed?
       let maximize = (document.documentElement.getAttribute("sizemode") == "maximized");
       if (maximize && w > screen.width)
         return;
 
       let toolbarHeight = Math.round(document.getElementById("toolbar-main").getBoundingClientRect().height);
-      let scaledDefaultH = (kDefaultBrowserWidth * (h / w));
-      let scaledScreenH = (window.screen.width * (h / w));
-      let dpiScale = Services.prefs.getIntPref("zoom.dpiScale") / 100;
 
       Browser.styles["window-width"].width = w + "px";
       Browser.styles["window-height"].height = h + "px";
       Browser.styles["toolbar-height"].height = toolbarHeight + "px";
 
       // Tell the UI to resize the browser controls
       BrowserUI.sizeControls(w, h);
 
       // XXX this should really only happen on browser startup, not every resize
       Browser.hideSidebars();
 
+      let oldWidth = window.cachedWidth || w;
+      window.cachedWidth = w;
+
       for (let i = Browser.tabs.length - 1; i >= 0; i--)
-        Browser.tabs[i].updateViewportSize();
+        Browser.tabs[i].updateViewportSize(w / oldWidth);
 
       // XXX page scrollbox jumps to a strange value on resize. Scrolling it will
       // bound it to a sane place, but not where we were when the resize began :(
       Browser.hideTitlebar();
 
       // XXX Hack. Browser also behaves badly on resize.
       getBrowser().style.display = "none";
       getBrowser().style.display = "block";
@@ -2355,26 +2356,30 @@ Tab.prototype = {
   },
 
   /** Update browser styles when the viewport metadata changes. */
   updateViewportMetadata: function updateViewportMetadata(aMetadata) {
     this._metadata = aMetadata;
     this.updateViewportSize();
   },
 
-  /** Update browser size when the metadata or the window size changes. */
-  updateViewportSize: function updateViewportSize() {
+  /**
+   * Update browser size when the metadata or the window size changes.
+   * If the window size changes, aRatio = (new width / old width).
+   */
+  updateViewportSize: function updateViewportSize(aRatio) {
     let browser = this._browser;
     if (!browser)
       return;
 
+    let screenW = window.innerWidth;
+    let screenH = window.innerHeight;
+
     let metadata = this.metadata;
     if (!metadata.autoSize) {
-      let screenW = window.innerWidth;
-      let screenH = window.innerHeight;
       let viewportW = metadata.width;
       let viewportH = metadata.height;
 
       // If (scale * width) < device-width, increase the width (bug 561413).
       let maxInitialZoom = metadata.defaultZoom || metadata.maxZoom;
       if (maxInitialZoom && viewportW)
         viewportW = Math.max(viewportW, screenW / maxInitialZoom);
 
@@ -2386,21 +2391,32 @@ Tab.prototype = {
       } else if (!validW && validH) {
         viewportW = viewportH * (screenW / screenH);
       } else {
         viewportW = kDefaultBrowserWidth;
         viewportH = kDefaultBrowserWidth * (screenH / screenW);
       }
 
       browser.setWindowSize(viewportW, viewportH);
-    }
-    else {
-      let browserBCR = browser.getBoundingClientRect();
-      let w = browserBCR.width;
-      let h = browserBCR.height;
+
+      if (aRatio) {
+        let pos = browser.getPosition();
+
+        // zoom to keep the same portion of the document visible
+        let oldScale = browser.scale;
+        let newScale = this.clampZoomLevel(oldScale * aRatio);
+        browser.scale = newScale;
+
+        // ...and keep the same top-left corner of the visible rect
+        let scaleRatio = newScale / oldScale;
+        browser.scrollTo(pos.x * scaleRatio, pos.y * scaleRatio);
+      }
+    } else {
+      let w = screenW;
+      let h = screenH;
       if (metadata.defaultZoom != 1.0) {
         let dpiScale = Services.prefs.getIntPref("zoom.dpiScale") / 100;
         w /= dpiScale;
         h /= dpiScale;
       }
 
       browser.setWindowSize(w, h);
     }