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/. */
7 const variableFileContents = require("raw!chrome://devtools/skin/variables.css");
9 const THEME_SELECTOR_STRINGS = {
10 light: ":root.theme-light {",
11 dark: ":root.theme-dark {",
14 const THEME_PREF = "devtools.theme";
17 * Takes a theme name and returns the contents of its variable rule block.
18 * The first time this runs fetches the variables CSS file and caches it.
20 function getThemeFile(name) {
21 // If there's no theme expected for this name, use `light` as default.
22 const selector = THEME_SELECTOR_STRINGS[name] || THEME_SELECTOR_STRINGS.light;
24 // This is a pretty naive way to find the contents between:
28 // There is test coverage for this feature (browser_theme.js)
29 // so if an } is introduced in the variables file it will catch that.
30 let theme = variableFileContents;
31 theme = theme.substring(theme.indexOf(selector));
32 theme = theme.substring(0, theme.indexOf("}"));
38 * Returns the "auto" theme.
40 const getAutoTheme = (exports.getAutoTheme = () => {
41 return Services.appinfo.chromeColorSchemeIsDark ? "dark" : "light";
45 * Returns the string value of the current theme,
46 * like "dark" or "light".
48 const getTheme = (exports.getTheme = () => {
49 const theme = Services.prefs.getCharPref(THEME_PREF);
50 if (theme == "auto") {
51 return getAutoTheme();
57 * Returns a color indicated by `type` (like "toolbar-background", or
58 * "highlight-red"), with the ability to specify a theme, or use whatever the
59 * current theme is if left unset. If theme not found, falls back to "light"
60 * theme. Returns null if the type cannot be found for the theme given.
62 /* eslint-disable no-unused-vars */
63 const getColor = (exports.getColor = (type, theme) => {
64 const themeName = theme || getTheme();
65 let themeFile = getThemeFile(themeName);
66 let match = themeFile.match(new RegExp("--theme-" + type + ": (.*);"));
67 const variableMatch = match ? match[1].match(/var\((.*)\)/) : null;
69 // Check if the match is a color variable and retrieve the value of the color variable
72 themeFile = getThemeFile("root");
73 match = themeFile.match(new RegExp(`${variableMatch[1]}: (.*);`));
76 // Return the appropriate variable in the theme, or otherwise, null.
77 return match ? match[1] : null;
81 * Set the theme preference.
83 const setTheme = (exports.setTheme = newTheme => {
84 Services.prefs.setCharPref(THEME_PREF, newTheme);
88 * Add an observer for theme changes.
90 const addThemeObserver = (exports.addThemeObserver = observer => {
91 Services.obs.addObserver(observer, "look-and-feel-changed");
92 Services.prefs.addObserver(THEME_PREF, observer);
96 * Remove an observer for theme changes.
98 const removeThemeObserver = (exports.removeThemeObserver = observer => {
99 Services.obs.removeObserver(observer, "look-and-feel-changed");
100 Services.prefs.removeObserver(THEME_PREF, observer);