1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/devtools/shared/theme-switching.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,88 @@ 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 +(function() { 1.9 + const DEVTOOLS_SKIN_URL = "chrome://browser/skin/devtools/"; 1.10 + 1.11 + function forceStyle() { 1.12 + let computedStyle = window.getComputedStyle(document.documentElement); 1.13 + if (!computedStyle) { 1.14 + // Null when documentElement is not ready. This method is anyways not 1.15 + // required then as scrollbars would be in their state without flushing. 1.16 + return; 1.17 + } 1.18 + let display = computedStyle.display; // Save display value 1.19 + document.documentElement.style.display = "none"; 1.20 + window.getComputedStyle(document.documentElement).display; // Flush 1.21 + document.documentElement.style.display = display; // Restore 1.22 + } 1.23 + 1.24 + function switchTheme(newTheme, oldTheme) { 1.25 + if (newTheme === oldTheme) { 1.26 + return; 1.27 + } 1.28 + 1.29 + if (oldTheme && newTheme != oldTheme) { 1.30 + StylesheetUtils.removeSheet( 1.31 + window, 1.32 + DEVTOOLS_SKIN_URL + oldTheme + "-theme.css", 1.33 + "author" 1.34 + ); 1.35 + } 1.36 + 1.37 + StylesheetUtils.loadSheet( 1.38 + window, 1.39 + DEVTOOLS_SKIN_URL + newTheme + "-theme.css", 1.40 + "author" 1.41 + ); 1.42 + 1.43 + // Floating scrollbars à la osx 1.44 + let hiddenDOMWindow = Cc["@mozilla.org/appshell/appShellService;1"] 1.45 + .getService(Ci.nsIAppShellService) 1.46 + .hiddenDOMWindow; 1.47 + if (!hiddenDOMWindow.matchMedia("(-moz-overlay-scrollbars)").matches) { 1.48 + let scrollbarsUrl = Services.io.newURI( 1.49 + DEVTOOLS_SKIN_URL + "floating-scrollbars-light.css", null, null); 1.50 + 1.51 + if (newTheme == "dark") { 1.52 + StylesheetUtils.loadSheet( 1.53 + window, 1.54 + scrollbarsUrl, 1.55 + "agent" 1.56 + ); 1.57 + } else if (oldTheme == "dark") { 1.58 + StylesheetUtils.removeSheet( 1.59 + window, 1.60 + scrollbarsUrl, 1.61 + "agent" 1.62 + ); 1.63 + } 1.64 + forceStyle(); 1.65 + } 1.66 + 1.67 + document.documentElement.classList.remove("theme-" + oldTheme); 1.68 + document.documentElement.classList.add("theme-" + newTheme); 1.69 + } 1.70 + 1.71 + function handlePrefChange(event, data) { 1.72 + if (data.pref == "devtools.theme") { 1.73 + switchTheme(data.newValue, data.oldValue); 1.74 + } 1.75 + } 1.76 + 1.77 + const { classes: Cc, interfaces: Ci, utils: Cu } = Components; 1.78 + 1.79 + Cu.import("resource://gre/modules/Services.jsm"); 1.80 + Cu.import("resource:///modules/devtools/gDevTools.jsm"); 1.81 + const {devtools} = Components.utils.import("resource://gre/modules/devtools/Loader.jsm", {}); 1.82 + const StylesheetUtils = devtools.require("sdk/stylesheet/utils"); 1.83 + 1.84 + let theme = Services.prefs.getCharPref("devtools.theme"); 1.85 + switchTheme(theme); 1.86 + 1.87 + gDevTools.on("pref-changed", handlePrefChange); 1.88 + window.addEventListener("unload", function() { 1.89 + gDevTools.off("pref-changed", handlePrefChange); 1.90 + }); 1.91 +})();