bug 864107 - constrain restored window dimensions to available screen area. r=ttaubert
authorJonathan Kew <jkew@mozilla.com>
Fri, 26 Apr 2013 12:41:29 +0100
changeset 140999 dde0f2ca02d877b8c12fac0d8cb58878a386de84
parent 140998 447cf900cd4f9fed2997882d443739b962d3c1a6
child 141000 1f53f6fc47f03d2ce894e74af5580e7e888d6391
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersttaubert
bugs864107
milestone23.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
bug 864107 - constrain restored window dimensions to available screen area. r=ttaubert
browser/components/sessionstore/src/SessionStore.jsm
--- a/browser/components/sessionstore/src/SessionStore.jsm
+++ b/browser/components/sessionstore/src/SessionStore.jsm
@@ -78,16 +78,18 @@ Cu.import("resource://gre/modules/debug.
 Cu.import("resource://gre/modules/TelemetryTimestamps.jsm", this);
 Cu.import("resource://gre/modules/TelemetryStopwatch.jsm", this);
 Cu.import("resource://gre/modules/osfile.jsm", this);
 Cu.import("resource://gre/modules/PrivateBrowsingUtils.jsm", this);
 Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js", this);
 
 XPCOMUtils.defineLazyServiceGetter(this, "gSessionStartup",
   "@mozilla.org/browser/sessionstartup;1", "nsISessionStartup");
+XPCOMUtils.defineLazyServiceGetter(this, "gScreenManager",
+  "@mozilla.org/gfx/screenmanager;1", "nsIScreenManager");
 
 // List of docShell capabilities to (re)store. These are automatically
 // retrieved from a given docShell if not already collected before.
 // This is made so they're automatically in sync with all nsIDocShell.allow*
 // properties.
 let gDocShellCapabilities = (function () {
   let caps;
 
@@ -3574,16 +3576,41 @@ let SessionStoreInternal = {
    * @param aSidebar
    *        Sidebar command
    */
   restoreDimensions: function ssi_restoreDimensions(aWindow, aWidth, aHeight, aLeft, aTop, aSizeMode, aSidebar) {
     var win = aWindow;
     var _this = this;
     function win_(aName) { return _this._getWindowDimension(win, aName); }
 
+    // find available space on the screen where this window is being placed
+    let screen = gScreenManager.screenForRect(aLeft, aTop, aWidth, aHeight);
+    if (screen) {
+      let screenLeft = {}, screenTop = {}, screenWidth = {}, screenHeight = {};
+      screen.GetAvailRectDisplayPix(screenLeft, screenTop, screenWidth, screenHeight);
+      // constrain the dimensions to the actual space available
+      if (aWidth > screenWidth.value) {
+        aWidth = screenWidth.value;
+      }
+      if (aHeight > screenHeight.value) {
+        aHeight = screenHeight.value;
+      }
+      // and then pull the window within the screen's bounds
+      if (aLeft < screenLeft.value) {
+        aLeft = screenLeft.value;
+      } else if (aLeft + aWidth > screenLeft.value + screenWidth.value) {
+        aLeft = screenLeft.value + screenWidth.value - aWidth;
+      }
+      if (aTop < screenTop.value) {
+        aTop = screenTop.value;
+      } else if (aTop + aHeight > screenTop.value + screenHeight.value) {
+        aTop = screenTop.value + screenHeight.value - aHeight;
+      }
+    }
+
     // only modify those aspects which aren't correct yet
     if (aWidth && aHeight && (aWidth != win_("width") || aHeight != win_("height"))) {
       aWindow.resizeTo(aWidth, aHeight);
     }
     if (!isNaN(aLeft) && !isNaN(aTop) && (aLeft != win_("screenX") || aTop != win_("screenY"))) {
       aWindow.moveTo(aLeft, aTop);
     }
     if (aSizeMode && win_("sizemode") != aSizeMode)