theme.js (2274B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 const THEME_PREF = "devtools.theme"; 8 9 /* eslint-disable no-unused-vars */ 10 11 /** 12 * Returns the "auto" theme. 13 */ 14 const getAutoTheme = (exports.getAutoTheme = () => { 15 return Services.appinfo.chromeColorSchemeIsDark ? "dark" : "light"; 16 }); 17 18 /** 19 * Returns the string value of the current theme, 20 * like "dark" or "light". 21 */ 22 const getTheme = (exports.getTheme = () => { 23 const theme = getThemePrefValue(); 24 if (theme == "auto") { 25 return getAutoTheme(); 26 } 27 return theme; 28 }); 29 30 /** 31 * Returns the value of the pref of the current theme, 32 * like "auto", "dark" or "light". 33 */ 34 const getThemePrefValue = (exports.getThemePrefValue = () => { 35 return Services.prefs.getCharPref(THEME_PREF, ""); 36 }); 37 38 /** 39 * Returns the color of a CSS variable (--theme-toolbar-background, --theme-highlight-red), 40 * for the current toolbox theme, or null if the variable does not exist, or it's not a 41 * registered property, or doesn't have a <color> syntax. 42 * 43 * @param {string} variableName 44 * @param {Window} win: The window into which the variable should be defined. 45 * @returns {string | null} 46 */ 47 const getCssVariableColor = (exports.getCssVariableColor = ( 48 variableName, 49 win 50 ) => { 51 const value = win 52 .getComputedStyle(win.document.documentElement) 53 .getPropertyValue(variableName); 54 55 if (!value) { 56 console.warn("Unknown", variableName, "CSS variable"); 57 return null; 58 } 59 60 return value; 61 }); 62 63 /** 64 * Set the theme preference. 65 */ 66 const setTheme = (exports.setTheme = newTheme => { 67 Services.prefs.setCharPref(THEME_PREF, newTheme); 68 }); 69 70 /** 71 * Add an observer for theme changes. 72 */ 73 const addThemeObserver = (exports.addThemeObserver = observer => { 74 Services.obs.addObserver(observer, "look-and-feel-changed"); 75 Services.prefs.addObserver(THEME_PREF, observer); 76 }); 77 78 /** 79 * Remove an observer for theme changes. 80 */ 81 const removeThemeObserver = (exports.removeThemeObserver = observer => { 82 Services.obs.removeObserver(observer, "look-and-feel-changed"); 83 Services.prefs.removeObserver(THEME_PREF, observer); 84 }); 85 /* eslint-enable */