browser/components/customizableui/src/ScrollbarSampler.jsm

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = ["ScrollbarSampler"];
michael@0 8
michael@0 9 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 13 XPCOMUtils.defineLazyModuleGetter(this, "Promise",
michael@0 14 "resource://gre/modules/Promise.jsm");
michael@0 15
michael@0 16 let gSystemScrollbarWidth = null;
michael@0 17
michael@0 18 this.ScrollbarSampler = {
michael@0 19 getSystemScrollbarWidth: function() {
michael@0 20 let deferred = Promise.defer();
michael@0 21
michael@0 22 if (gSystemScrollbarWidth !== null) {
michael@0 23 deferred.resolve(gSystemScrollbarWidth);
michael@0 24 return deferred.promise;
michael@0 25 }
michael@0 26
michael@0 27 this._sampleSystemScrollbarWidth().then(function(systemScrollbarWidth) {
michael@0 28 gSystemScrollbarWidth = systemScrollbarWidth;
michael@0 29 deferred.resolve(gSystemScrollbarWidth);
michael@0 30 });
michael@0 31 return deferred.promise;
michael@0 32 },
michael@0 33
michael@0 34 resetSystemScrollbarWidth: function() {
michael@0 35 gSystemScrollbarWidth = null;
michael@0 36 },
michael@0 37
michael@0 38 _sampleSystemScrollbarWidth: function() {
michael@0 39 let deferred = Promise.defer();
michael@0 40 let hwin = Services.appShell.hiddenDOMWindow;
michael@0 41 let hdoc = hwin.document.documentElement;
michael@0 42 let iframe = hwin.document.createElementNS("http://www.w3.org/1999/xhtml",
michael@0 43 "html:iframe");
michael@0 44 iframe.setAttribute("srcdoc", '<body style="overflow-y: scroll"></body>');
michael@0 45 hdoc.appendChild(iframe);
michael@0 46
michael@0 47 let cwindow = iframe.contentWindow;
michael@0 48 let utils = cwindow.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 49 .getInterface(Ci.nsIDOMWindowUtils);
michael@0 50
michael@0 51 cwindow.addEventListener("load", function onLoad(aEvent) {
michael@0 52 cwindow.removeEventListener("load", onLoad);
michael@0 53 let sbWidth = {};
michael@0 54 try {
michael@0 55 utils.getScrollbarSize(true, sbWidth, {});
michael@0 56 } catch(e) {
michael@0 57 Cu.reportError("Could not sample scrollbar size: " + e + " -- " +
michael@0 58 e.stack);
michael@0 59 sbWidth.value = 0;
michael@0 60 }
michael@0 61 // Minimum width of 10 so that we have enough padding:
michael@0 62 sbWidth.value = Math.max(sbWidth.value, 10);
michael@0 63 deferred.resolve(sbWidth.value);
michael@0 64 iframe.remove();
michael@0 65 });
michael@0 66
michael@0 67 return deferred.promise;
michael@0 68 }
michael@0 69 };
michael@0 70 Object.freeze(this.ScrollbarSampler);

mercurial