browser/components/customizableui/src/ScrollbarSampler.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/customizableui/src/ScrollbarSampler.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,70 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = ["ScrollbarSampler"];
    1.11 +
    1.12 +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    1.13 +
    1.14 +Cu.import("resource://gre/modules/Services.jsm");
    1.15 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    1.16 +XPCOMUtils.defineLazyModuleGetter(this, "Promise",
    1.17 +                                  "resource://gre/modules/Promise.jsm");
    1.18 +
    1.19 +let gSystemScrollbarWidth = null;
    1.20 +
    1.21 +this.ScrollbarSampler = {
    1.22 +  getSystemScrollbarWidth: function() {
    1.23 +    let deferred = Promise.defer();
    1.24 +
    1.25 +    if (gSystemScrollbarWidth !== null) {
    1.26 +      deferred.resolve(gSystemScrollbarWidth);
    1.27 +      return deferred.promise;
    1.28 +    }
    1.29 +
    1.30 +    this._sampleSystemScrollbarWidth().then(function(systemScrollbarWidth) {
    1.31 +      gSystemScrollbarWidth = systemScrollbarWidth;
    1.32 +      deferred.resolve(gSystemScrollbarWidth);
    1.33 +    });
    1.34 +    return deferred.promise;
    1.35 +  },
    1.36 +
    1.37 +  resetSystemScrollbarWidth: function() {
    1.38 +    gSystemScrollbarWidth = null;
    1.39 +  },
    1.40 +
    1.41 +  _sampleSystemScrollbarWidth: function() {
    1.42 +    let deferred = Promise.defer();
    1.43 +    let hwin = Services.appShell.hiddenDOMWindow;
    1.44 +    let hdoc = hwin.document.documentElement;
    1.45 +    let iframe = hwin.document.createElementNS("http://www.w3.org/1999/xhtml",
    1.46 +                                               "html:iframe");
    1.47 +    iframe.setAttribute("srcdoc", '<body style="overflow-y: scroll"></body>');
    1.48 +    hdoc.appendChild(iframe);
    1.49 +
    1.50 +    let cwindow = iframe.contentWindow;
    1.51 +    let utils = cwindow.QueryInterface(Ci.nsIInterfaceRequestor)
    1.52 +                       .getInterface(Ci.nsIDOMWindowUtils);
    1.53 +
    1.54 +    cwindow.addEventListener("load", function onLoad(aEvent) {
    1.55 +      cwindow.removeEventListener("load", onLoad);
    1.56 +      let sbWidth = {};
    1.57 +      try {
    1.58 +        utils.getScrollbarSize(true, sbWidth, {});
    1.59 +      } catch(e) {
    1.60 +        Cu.reportError("Could not sample scrollbar size: " + e + " -- " +
    1.61 +                       e.stack);
    1.62 +        sbWidth.value = 0;
    1.63 +      }
    1.64 +      // Minimum width of 10 so that we have enough padding:
    1.65 +      sbWidth.value = Math.max(sbWidth.value, 10);
    1.66 +      deferred.resolve(sbWidth.value);
    1.67 +      iframe.remove();
    1.68 +    });
    1.69 +
    1.70 +    return deferred.promise;
    1.71 +  }
    1.72 +};
    1.73 +Object.freeze(this.ScrollbarSampler);

mercurial