Bug 599132 - Clean up updateViewportSize [r=stechz]
authorMatt Brubeck <mbrubeck@mozilla.com>
Fri, 08 Oct 2010 14:38:36 -0700
changeset 2176 604c4cbfc09129765027d5c15a98fe47ef665641
parent 2175 04dbed7f2854c160487f13ee67f211a3c22fb2f7
child 2177 a67f6b26894235df217118c54c5aaaefd09fc1c1
push id1831
push usermbrubeck@mozilla.com
push dateFri, 08 Oct 2010 21:38:58 +0000
reviewersstechz
bugs599132
Bug 599132 - Clean up updateViewportSize [r=stechz]
chrome/content/browser.js
--- a/chrome/content/browser.js
+++ b/chrome/content/browser.js
@@ -266,18 +266,26 @@ var Browser = {
       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(w / oldWidth);
+      for (let i = Browser.tabs.length - 1; i >= 0; i--) {
+        let tab = Browser.tabs[i];
+        let oldContentWindowWidth = tab.browser.contentWindowWidth;
+        tab.updateViewportSize();
+
+        // If the viewport width is still the same, the page layout has not
+        // changed, so we can keep keep the same content on-screen.
+        if (tab.browser.contentWindowWidth == oldContentWindowWidth)
+          tab.restoreViewportPosition(oldWidth, w);
+      }
 
       // 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.
       let browser = getBrowser();
       browser.style.display = "none";
@@ -2350,30 +2358,38 @@ 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.
-   * If the window size changes, aRatio = (new width / old width).
    */
-  updateViewportSize: function updateViewportSize(aRatio) {
+  updateViewportSize: function updateViewportSize() {
     let browser = this._browser;
     if (!browser)
       return;
 
     let screenW = window.innerWidth;
     let screenH = window.innerHeight;
+    let viewportW, viewportH;
 
     let metadata = this.metadata;
-    if (!metadata.autoSize) {
-      let viewportW = metadata.width;
-      let viewportH = metadata.height;
+    if (metadata.autoSize) {
+      viewportW = screenW;
+      viewportH = screenH;
+      if (metadata.defaultZoom != 1.0) {
+        let dpiScale = Services.prefs.getIntPref("zoom.dpiScale") / 100;
+        viewportW /= dpiScale;
+        viewportH /= dpiScale;
+      }
+    } else {
+      viewportW = metadata.width;
+      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);
 
       let validW = viewportW > 0;
       let validH = viewportH > 0;
@@ -2381,42 +2397,32 @@ Tab.prototype = {
       if (validW && !validH) {
         viewportH = viewportW * (screenH / screenW);
       } else if (!validW && validH) {
         viewportW = viewportH * (screenW / screenH);
       } else if (!validW && !validH) {
         viewportW = kDefaultBrowserWidth;
         viewportH = kDefaultBrowserWidth * (screenH / screenW);
       }
-
-      browser.setWindowSize(viewportW, viewportH);
-
-      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);
     }
+    browser.setWindowSize(viewportW, viewportH);
+  },
+
+  restoreViewportPosition: function restoreViewportPosition(aOldWidth, aNewWidth) {
+    let browser = this._browser;
+    let pos = browser.getPosition();
+
+    // zoom to keep the same portion of the document visible
+    let oldScale = browser.scale;
+    let newScale = this.clampZoomLevel(oldScale * aNewWidth / aOldWidth);
+    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);
   },
 
   startLoading: function startLoading() {
     if (this._loading) throw "Already Loading!";
     this._loading = true;
   },
 
   endLoading: function endLoading() {
@@ -2490,19 +2496,19 @@ Tab.prototype = {
       this._loading = false;
 
       Util.executeSoon(function() {
         Elements.browsers.removeChild(browser);
       });
     }
   },
 
-  clampZoomLevel: function clampZoomLevel(zl) {
+  clampZoomLevel: function clampZoomLevel(aScale) {
     let browser = this._browser;
-    let bounded = Math.min(Math.max(ZoomManager.MIN, zl), ZoomManager.MAX);
+    let bounded = Util.clamp(aScale, ZoomManager.MIN, ZoomManager.MAX);
 
     let md = this.metadata;
     if (md && md.minZoom)
       bounded = Math.max(bounded, md.minZoom);
     if (md && md.maxZoom)
       bounded = Math.min(bounded, md.maxZoom);
 
     bounded = Math.max(bounded, this.getPageZoomLevel());