2 /* global resourcesSuffix */
3 var darkThemes = ["dark", "ayu"];
4 window.currentTheme = document.getElementById("themeStyle");
5 window.mainTheme = document.getElementById("mainThemeStyle");
7 var settingsDataset = (function () {
8 var settingsElement = document.getElementById("default-settings");
9 if (settingsElement === null) {
12 var dataset = settingsElement.dataset;
13 if (dataset === undefined) {
19 function getSettingValue(settingName) {
20 var current = getCurrentValue('rustdoc-' + settingName);
21 if (current !== null) {
24 if (settingsDataset !== null) {
25 var def = settingsDataset[settingName.replace(/-/g,'_')];
26 if (def !== undefined) {
33 var localStoredTheme = getSettingValue("theme");
37 // eslint-disable-next-line no-unused-vars
38 function hasClass(elem, className) {
39 return elem && elem.classList && elem.classList.contains(className);
42 // eslint-disable-next-line no-unused-vars
43 function addClass(elem, className) {
44 if (!elem || !elem.classList) {
47 elem.classList.add(className);
50 // eslint-disable-next-line no-unused-vars
51 function removeClass(elem, className) {
52 if (!elem || !elem.classList) {
55 elem.classList.remove(className);
58 function onEach(arr, func, reversed) {
59 if (arr && arr.length > 0 && func) {
60 var length = arr.length;
62 if (reversed !== true) {
63 for (i = 0; i < length; ++i) {
64 if (func(arr[i]) === true) {
69 for (i = length - 1; i >= 0; --i) {
70 if (func(arr[i]) === true) {
79 function onEachLazy(lazyArray, func, reversed) {
81 Array.prototype.slice.call(lazyArray),
86 // eslint-disable-next-line no-unused-vars
87 function hasOwnProperty(obj, property) {
88 return Object.prototype.hasOwnProperty.call(obj, property);
91 function updateLocalStorage(name, value) {
93 window.localStorage.setItem(name, value);
95 // localStorage is not accessible, do nothing
99 function getCurrentValue(name) {
101 return window.localStorage.getItem(name);
107 function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
108 var fullBasicCss = "rustdoc" + resourcesSuffix + ".css";
109 var fullNewTheme = newTheme + resourcesSuffix + ".css";
110 var newHref = mainStyleElem.href.replace(fullBasicCss, fullNewTheme);
112 // If this new value comes from a system setting or from the previously
113 // saved theme, no need to save it.
114 if (saveTheme === true) {
115 updateLocalStorage("rustdoc-theme", newTheme);
118 if (styleElem.href === newHref) {
123 if (savedHref.length === 0) {
124 onEachLazy(document.getElementsByTagName("link"), function(el) {
125 savedHref.push(el.href);
128 onEach(savedHref, function(el) {
129 if (el === newHref) {
134 if (found === true) {
135 styleElem.href = newHref;
139 // This function is called from "main.js".
140 // eslint-disable-next-line no-unused-vars
141 function useSystemTheme(value) {
142 if (value === undefined) {
146 updateLocalStorage("rustdoc-use-system-theme", value);
148 // update the toggle if we're on the settings page
149 var toggle = document.getElementById("use-system-theme");
150 if (toggle && toggle instanceof HTMLInputElement) {
151 toggle.checked = value;
155 var updateSystemTheme = (function() {
156 if (!window.matchMedia) {
157 // fallback to the CSS computed value
159 var cssTheme = getComputedStyle(document.documentElement)
160 .getPropertyValue('content');
165 JSON.parse(cssTheme) || "light",
171 // only listen to (prefers-color-scheme: dark) because light is the default
172 var mql = window.matchMedia("(prefers-color-scheme: dark)");
174 function handlePreferenceChange(mql) {
175 // maybe the user has disabled the setting in the meantime!
176 if (getSettingValue("use-system-theme") !== "false") {
177 var lightTheme = getSettingValue("preferred-light-theme") || "light";
178 var darkTheme = getSettingValue("preferred-dark-theme") || "dark";
181 // prefers a dark theme
182 switchTheme(window.currentTheme, window.mainTheme, darkTheme, true);
184 // prefers a light theme, or has no preference
185 switchTheme(window.currentTheme, window.mainTheme, lightTheme, true);
188 // note: we save the theme so that it doesn't suddenly change when
189 // the user disables "use-system-theme" and reloads the page or
190 // navigates to another page
194 mql.addListener(handlePreferenceChange);
197 handlePreferenceChange(mql);
201 if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
202 // update the preferred dark theme if the user is already using a dark theme
203 // See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
204 if (getSettingValue("use-system-theme") === null
205 && getSettingValue("preferred-dark-theme") === null
206 && darkThemes.indexOf(localStoredTheme) >= 0) {
207 updateLocalStorage("rustdoc-preferred-dark-theme", localStoredTheme);
210 // call the function to initialize the theme at least once!
216 getSettingValue("theme") || "light",