Bug 736028 - Page thumbnails should be 1/9th the size of the screen. r=ttaubert
authorJared Wein <jwein@mozilla.com>
Thu, 12 Apr 2012 00:02:27 -0700
changeset 94811 1bd7dbc2653acf53831b462f0e12766c58705731
parent 94810 412d3777596a123131d4547361dd46df87e5701c
child 94812 527a2b7e7f4e530d2e3939cc51dc0cc9c1326c9a
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersttaubert
bugs736028
milestone14.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 736028 - Page thumbnails should be 1/9th the size of the screen. r=ttaubert
browser/components/thumbnails/PageThumbs.jsm
--- a/browser/components/thumbnails/PageThumbs.jsm
+++ b/browser/components/thumbnails/PageThumbs.jsm
@@ -8,32 +8,16 @@ let EXPORTED_SYMBOLS = ["PageThumbs", "P
 
 const Cu = Components.utils;
 const Cc = Components.classes;
 const Ci = Components.interfaces;
 
 const HTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
 
 /**
- * The default width for page thumbnails.
- *
- * Hint: This is the default value because the 'New Tab Page' is the only
- *       client for now.
- */
-const THUMBNAIL_WIDTH = 400;
-
-/**
- * The default height for page thumbnails.
- *
- * Hint: This is the default value because the 'New Tab Page' is the only
- *       client for now.
- */
-const THUMBNAIL_HEIGHT = 225;
-
-/**
  * The default background color for page thumbnails.
  */
 const THUMBNAIL_BG_COLOR = "#fff";
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
   "resource://gre/modules/NetUtil.jsm");
@@ -41,16 +25,23 @@ XPCOMUtils.defineLazyModuleGetter(this, 
 XPCOMUtils.defineLazyModuleGetter(this, "Services",
   "resource://gre/modules/Services.jsm");
 
 /**
  * Singleton providing functionality for capturing web page thumbnails and for
  * accessing them if already cached.
  */
 let PageThumbs = {
+
+  /**
+   * The calculated width and height of the thumbnails.
+   */
+  _thumbnailWidth : 0,
+  _thumbnailHeight : 0,
+
   /**
    * The scheme to use for thumbnail urls.
    */
   get scheme() "moz-page-thumb",
 
   /**
    * The static host to use for thumbnail urls.
    */
@@ -166,41 +157,59 @@ let PageThumbs = {
    * Determines the crop size for a given content window.
    * @param aWindow The content window.
    * @return An array containing width, height and scale.
    */
   _determineCropSize: function PageThumbs_determineCropSize(aWindow) {
     let sw = aWindow.innerWidth;
     let sh = aWindow.innerHeight;
 
-    let scale = Math.max(THUMBNAIL_WIDTH / sw, THUMBNAIL_HEIGHT / sh);
+    let [thumbnailWidth, thumbnailHeight] = this._getThumbnailSize();
+    let scale = Math.max(thumbnailWidth / sw, thumbnailHeight / sh);
     let scaledWidth = sw * scale;
     let scaledHeight = sh * scale;
 
-    if (scaledHeight > THUMBNAIL_HEIGHT)
-      sh -= Math.floor(Math.abs(scaledHeight - THUMBNAIL_HEIGHT) * scale);
+    if (scaledHeight > thumbnailHeight)
+      sh -= Math.floor(Math.abs(scaledHeight - thumbnailHeight) * scale);
 
-    if (scaledWidth > THUMBNAIL_WIDTH)
-      sw -= Math.floor(Math.abs(scaledWidth - THUMBNAIL_WIDTH) * scale);
+    if (scaledWidth > thumbnailWidth)
+      sw -= Math.floor(Math.abs(scaledWidth - thumbnailWidth) * scale);
 
     return [sw, sh, scale];
   },
 
   /**
    * Creates a new hidden canvas element.
    * @return The newly created canvas.
    */
   _createCanvas: function PageThumbs_createCanvas() {
     let doc = Services.appShell.hiddenDOMWindow.document;
     let canvas = doc.createElementNS(HTML_NAMESPACE, "canvas");
     canvas.mozOpaque = true;
     canvas.mozImageSmoothingEnabled = true;
-    canvas.width = THUMBNAIL_WIDTH;
-    canvas.height = THUMBNAIL_HEIGHT;
+    let [thumbnailWidth, thumbnailHeight] = this._getThumbnailSize();
+    canvas.width = thumbnailWidth;
+    canvas.height = thumbnailHeight;
     return canvas;
+  },
+
+  /**
+   * Calculates the thumbnail size based on current desktop's dimensions.
+   * @return The calculated thumbnail size or a default if unable to calculate.
+   */
+  _getThumbnailSize: function PageThumbs_getThumbnailSize() {
+    if (!this._thumbnailWidth || !this._thumbnailHeight) {
+      let screenManager = Cc["@mozilla.org/gfx/screenmanager;1"]
+                            .getService(Ci.nsIScreenManager);
+      let left = {}, top = {}, width = {}, height = {};
+      screenManager.primaryScreen.GetRect(left, top, width, height);
+      this._thumbnailWidth = Math.round(width.value / 3);
+      this._thumbnailHeight = Math.round(height.value / 3);
+    }
+    return [this._thumbnailWidth, this._thumbnailHeight];
   }
 };
 
 /**
  * A singleton handling the storage of page thumbnails.
  */
 let PageThumbsCache = {
   /**