Bug 1890689 accumulate input in LargerReceiverBlockSizeThanDesiredBuffering GTest...
[gecko.git] / devtools / client / shared / theme.js
blobe3c3ff608dffa1a95841eaae38664d025d505ee2
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/. */
5 "use strict";
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 {",
12   root: ":root {",
14 const THEME_PREF = "devtools.theme";
16 /**
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.
19  */
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:
25   // selector {
26   //   name: val;
27   // }
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("}"));
34   return theme;
37 /**
38  * Returns the "auto" theme.
39  */
40 const getAutoTheme = (exports.getAutoTheme = () => {
41   return Services.appinfo.chromeColorSchemeIsDark ? "dark" : "light";
42 });
44 /**
45  * Returns the string value of the current theme,
46  * like "dark" or "light".
47  */
48 const getTheme = (exports.getTheme = () => {
49   const theme = Services.prefs.getCharPref(THEME_PREF);
50   if (theme == "auto") {
51     return getAutoTheme();
52   }
53   return theme;
54 });
56 /**
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.
61  */
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
70   // if needed
71   if (variableMatch) {
72     themeFile = getThemeFile("root");
73     match = themeFile.match(new RegExp(`${variableMatch[1]}: (.*);`));
74   }
76   // Return the appropriate variable in the theme, or otherwise, null.
77   return match ? match[1] : null;
78 });
80 /**
81  * Set the theme preference.
82  */
83 const setTheme = (exports.setTheme = newTheme => {
84   Services.prefs.setCharPref(THEME_PREF, newTheme);
85 });
87 /**
88  * Add an observer for theme changes.
89  */
90 const addThemeObserver = (exports.addThemeObserver = observer => {
91   Services.obs.addObserver(observer, "look-and-feel-changed");
92   Services.prefs.addObserver(THEME_PREF, observer);
93 });
95 /**
96  * Remove an observer for theme changes.
97  */
98 const removeThemeObserver = (exports.removeThemeObserver = observer => {
99   Services.obs.removeObserver(observer, "look-and-feel-changed");
100   Services.prefs.removeObserver(THEME_PREF, observer);
102 /* eslint-enable */