2022-05-02 14:32:56 +02:00
|
|
|
"use strict";
|
|
|
|
|
2022-04-25 14:33:53 +02:00
|
|
|
const darkThemes = ["dark", "ayu"];
|
2021-03-05 16:09:46 +01:00
|
|
|
window.currentTheme = document.getElementById("themeStyle");
|
|
|
|
window.mainTheme = document.getElementById("mainThemeStyle");
|
2020-09-23 22:14:43 +01:00
|
|
|
|
2022-04-25 14:33:53 +02:00
|
|
|
const settingsDataset = (function () {
|
|
|
|
const settingsElement = document.getElementById("default-settings");
|
2020-09-23 22:44:54 +01:00
|
|
|
if (settingsElement === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-25 14:33:53 +02:00
|
|
|
const dataset = settingsElement.dataset;
|
2020-09-23 22:44:54 +01:00
|
|
|
if (dataset === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return dataset;
|
|
|
|
})();
|
|
|
|
|
2020-09-23 22:14:43 +01:00
|
|
|
function getSettingValue(settingName) {
|
2022-04-25 14:33:53 +02:00
|
|
|
const current = getCurrentValue(settingName);
|
2020-09-23 22:44:54 +01:00
|
|
|
if (current !== null) {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
if (settingsDataset !== null) {
|
2021-07-19 17:13:22 +01:00
|
|
|
// See the comment for `default_settings.into_iter()` etc. in
|
|
|
|
// `Options::from_matches` in `librustdoc/config.rs`.
|
2022-05-07 20:18:23 +02:00
|
|
|
const def = settingsDataset[settingName.replace(/-/g,"_")];
|
2020-09-23 22:44:54 +01:00
|
|
|
if (def !== undefined) {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-09-23 22:14:43 +01:00
|
|
|
}
|
|
|
|
|
2022-04-25 14:33:53 +02:00
|
|
|
const localStoredTheme = getSettingValue("theme");
|
2018-01-22 23:44:08 +01:00
|
|
|
|
2022-04-25 14:33:53 +02:00
|
|
|
const savedHref = [];
|
2018-02-20 20:11:58 +01:00
|
|
|
|
2021-01-23 14:55:24 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2018-11-06 01:40:12 +01:00
|
|
|
function hasClass(elem, className) {
|
2018-12-02 01:22:44 +01:00
|
|
|
return elem && elem.classList && elem.classList.contains(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 14:55:24 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2018-11-06 01:40:12 +01:00
|
|
|
function addClass(elem, className) {
|
2018-11-16 16:31:07 +01:00
|
|
|
if (!elem || !elem.classList) {
|
|
|
|
return;
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
2018-11-16 16:31:07 +01:00
|
|
|
elem.classList.add(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
2021-01-23 14:55:24 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2018-11-06 01:40:12 +01:00
|
|
|
function removeClass(elem, className) {
|
2018-11-16 16:31:07 +01:00
|
|
|
if (!elem || !elem.classList) {
|
|
|
|
return;
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
2018-11-16 16:31:07 +01:00
|
|
|
elem.classList.remove(className);
|
2018-11-06 01:40:12 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 21:17:22 -08:00
|
|
|
/**
|
|
|
|
* Run a callback for every element of an Array.
|
|
|
|
* @param {Array<?>} arr - The array to iterate over
|
|
|
|
* @param {function(?)} func - The callback
|
|
|
|
* @param {boolean} [reversed] - Whether to iterate in reverse
|
|
|
|
*/
|
2018-09-12 23:31:21 +02:00
|
|
|
function onEach(arr, func, reversed) {
|
2018-02-20 20:11:58 +01:00
|
|
|
if (arr && arr.length > 0 && func) {
|
2021-05-09 13:49:22 -07:00
|
|
|
if (reversed) {
|
2022-04-25 14:33:53 +02:00
|
|
|
const length = arr.length;
|
|
|
|
for (let i = length - 1; i >= 0; --i) {
|
2021-05-09 13:49:22 -07:00
|
|
|
if (func(arr[i])) {
|
2018-09-12 23:31:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-25 14:33:53 +02:00
|
|
|
for (const elem of arr) {
|
|
|
|
if (func(elem)) {
|
2018-09-12 23:31:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 16:16:53 +02:00
|
|
|
return false;
|
2018-02-20 20:11:58 +01:00
|
|
|
}
|
|
|
|
|
2021-12-16 21:17:22 -08:00
|
|
|
/**
|
|
|
|
* Turn an HTMLCollection or a NodeList into an Array, then run a callback
|
|
|
|
* for every element. This is useful because iterating over an HTMLCollection
|
|
|
|
* or a "live" NodeList while modifying it can be very slow.
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection
|
|
|
|
* https://developer.mozilla.org/en-US/docs/Web/API/NodeList
|
|
|
|
* @param {NodeList<?>|HTMLCollection<?>} lazyArray - An array to iterate over
|
|
|
|
* @param {function(?)} func - The callback
|
|
|
|
* @param {boolean} [reversed] - Whether to iterate in reverse
|
|
|
|
*/
|
2018-12-02 01:22:44 +01:00
|
|
|
function onEachLazy(lazyArray, func, reversed) {
|
|
|
|
return onEach(
|
|
|
|
Array.prototype.slice.call(lazyArray),
|
|
|
|
func,
|
|
|
|
reversed);
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:15:27 -07:00
|
|
|
function updateLocalStorage(name, value) {
|
2021-02-10 14:53:22 +01:00
|
|
|
try {
|
2022-01-10 14:50:16 +01:00
|
|
|
window.localStorage.setItem("rustdoc-" + name, value);
|
2022-05-19 17:19:08 +02:00
|
|
|
} catch (e) {
|
2021-02-10 14:53:22 +01:00
|
|
|
// localStorage is not accessible, do nothing
|
2018-10-16 22:15:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getCurrentValue(name) {
|
2021-02-10 14:53:22 +01:00
|
|
|
try {
|
2022-01-10 14:50:16 +01:00
|
|
|
return window.localStorage.getItem("rustdoc-" + name);
|
2022-05-19 17:19:08 +02:00
|
|
|
} catch (e) {
|
2021-02-10 14:53:22 +01:00
|
|
|
return null;
|
2018-10-16 22:15:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:49:01 +02:00
|
|
|
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
|
2022-04-25 14:33:53 +02:00
|
|
|
const newHref = mainStyleElem.href.replace(
|
2021-11-23 19:22:29 -08:00
|
|
|
/\/rustdoc([^/]*)\.css/, "/" + newTheme + "$1" + ".css");
|
2018-02-20 20:11:58 +01:00
|
|
|
|
2020-10-23 18:58:42 +02:00
|
|
|
// If this new value comes from a system setting or from the previously
|
|
|
|
// saved theme, no need to save it.
|
2021-05-09 13:49:22 -07:00
|
|
|
if (saveTheme) {
|
2022-01-10 14:50:16 +01:00
|
|
|
updateLocalStorage("theme", newTheme);
|
2020-10-23 18:58:42 +02:00
|
|
|
}
|
|
|
|
|
2018-03-08 00:11:17 +01:00
|
|
|
if (styleElem.href === newHref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-25 14:33:53 +02:00
|
|
|
let found = false;
|
2018-02-20 20:11:58 +01:00
|
|
|
if (savedHref.length === 0) {
|
2022-05-03 12:03:17 +08:00
|
|
|
onEachLazy(document.getElementsByTagName("link"), el => {
|
2018-02-20 20:11:58 +01:00
|
|
|
savedHref.push(el.href);
|
|
|
|
});
|
|
|
|
}
|
2022-05-03 12:03:17 +08:00
|
|
|
onEach(savedHref, el => {
|
2018-02-20 20:11:58 +01:00
|
|
|
if (el === newHref) {
|
|
|
|
found = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2021-05-09 13:49:22 -07:00
|
|
|
if (found) {
|
2018-02-20 20:11:58 +01:00
|
|
|
styleElem.href = newHref;
|
|
|
|
}
|
2018-01-22 23:44:08 +01:00
|
|
|
}
|
|
|
|
|
2021-03-05 16:09:46 +01:00
|
|
|
// This function is called from "main.js".
|
2021-01-23 14:55:24 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2020-10-11 02:53:37 +02:00
|
|
|
function useSystemTheme(value) {
|
|
|
|
if (value === undefined) {
|
|
|
|
value = true;
|
|
|
|
}
|
|
|
|
|
2022-01-10 14:50:16 +01:00
|
|
|
updateLocalStorage("use-system-theme", value);
|
2020-10-11 02:53:37 +02:00
|
|
|
|
|
|
|
// update the toggle if we're on the settings page
|
2022-04-25 14:33:53 +02:00
|
|
|
const toggle = document.getElementById("use-system-theme");
|
2020-10-11 02:53:37 +02:00
|
|
|
if (toggle && toggle instanceof HTMLInputElement) {
|
|
|
|
toggle.checked = value;
|
|
|
|
}
|
2019-05-27 15:57:44 +02:00
|
|
|
}
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
const updateSystemTheme = (function () {
|
2020-10-11 02:53:37 +02:00
|
|
|
if (!window.matchMedia) {
|
|
|
|
// fallback to the CSS computed value
|
2022-05-03 12:03:17 +08:00
|
|
|
return () => {
|
2022-04-25 14:33:53 +02:00
|
|
|
const cssTheme = getComputedStyle(document.documentElement)
|
2022-05-07 20:18:23 +02:00
|
|
|
.getPropertyValue("content");
|
2020-10-11 02:53:37 +02:00
|
|
|
|
|
|
|
switchTheme(
|
2021-03-05 16:09:46 +01:00
|
|
|
window.currentTheme,
|
|
|
|
window.mainTheme,
|
2021-01-23 14:55:24 +01:00
|
|
|
JSON.parse(cssTheme) || "light",
|
2020-10-11 02:53:37 +02:00
|
|
|
true
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// only listen to (prefers-color-scheme: dark) because light is the default
|
2022-04-25 14:33:53 +02:00
|
|
|
const mql = window.matchMedia("(prefers-color-scheme: dark)");
|
2020-10-11 02:53:37 +02:00
|
|
|
|
|
|
|
function handlePreferenceChange(mql) {
|
2022-05-03 12:03:17 +08:00
|
|
|
const use = theme => {
|
2022-01-07 18:21:08 -05:00
|
|
|
switchTheme(window.currentTheme, window.mainTheme, theme, true);
|
|
|
|
};
|
2020-10-11 02:53:37 +02:00
|
|
|
// maybe the user has disabled the setting in the meantime!
|
2020-09-23 22:14:43 +01:00
|
|
|
if (getSettingValue("use-system-theme") !== "false") {
|
2022-04-25 14:33:53 +02:00
|
|
|
const lightTheme = getSettingValue("preferred-light-theme") || "light";
|
|
|
|
const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
|
2020-10-11 02:53:37 +02:00
|
|
|
|
|
|
|
if (mql.matches) {
|
2022-01-07 18:21:08 -05:00
|
|
|
use(darkTheme);
|
2020-10-11 02:53:37 +02:00
|
|
|
} else {
|
|
|
|
// prefers a light theme, or has no preference
|
2022-01-07 18:21:08 -05:00
|
|
|
use(lightTheme);
|
2020-10-11 02:53:37 +02:00
|
|
|
}
|
|
|
|
// note: we save the theme so that it doesn't suddenly change when
|
|
|
|
// the user disables "use-system-theme" and reloads the page or
|
|
|
|
// navigates to another page
|
2022-01-07 18:21:08 -05:00
|
|
|
} else {
|
|
|
|
use(getSettingValue("theme"));
|
2020-10-11 02:53:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mql.addListener(handlePreferenceChange);
|
|
|
|
|
2022-05-03 12:03:17 +08:00
|
|
|
return () => {
|
2020-10-11 02:53:37 +02:00
|
|
|
handlePreferenceChange(mql);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2022-01-23 13:04:17 -08:00
|
|
|
function switchToSavedTheme() {
|
|
|
|
switchTheme(
|
|
|
|
window.currentTheme,
|
|
|
|
window.mainTheme,
|
|
|
|
getSettingValue("theme") || "light",
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-09-23 22:14:43 +01:00
|
|
|
if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
|
2020-10-15 18:03:25 +02:00
|
|
|
// update the preferred dark theme if the user is already using a dark theme
|
|
|
|
// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
|
2020-09-23 22:14:43 +01:00
|
|
|
if (getSettingValue("use-system-theme") === null
|
|
|
|
&& getSettingValue("preferred-dark-theme") === null
|
2020-10-15 18:03:25 +02:00
|
|
|
&& darkThemes.indexOf(localStoredTheme) >= 0) {
|
2022-01-10 14:50:16 +01:00
|
|
|
updateLocalStorage("preferred-dark-theme", localStoredTheme);
|
2020-10-15 18:03:25 +02:00
|
|
|
}
|
|
|
|
|
2020-10-11 02:53:37 +02:00
|
|
|
// call the function to initialize the theme at least once!
|
|
|
|
updateSystemTheme();
|
|
|
|
} else {
|
2022-01-23 13:04:17 -08:00
|
|
|
switchToSavedTheme();
|
2020-10-11 02:53:37 +02:00
|
|
|
}
|
2022-01-23 13:04:17 -08:00
|
|
|
|
|
|
|
// If we navigate away (for example to a settings page), and then use the back or
|
|
|
|
// forward button to get back to a page, the theme may have changed in the meantime.
|
|
|
|
// But scripts may not be re-loaded in such a case due to the bfcache
|
|
|
|
// (https://web.dev/bfcache/). The "pageshow" event triggers on such navigations.
|
|
|
|
// Use that opportunity to update the theme.
|
|
|
|
// We use a setTimeout with a 0 timeout here to put the change on the event queue.
|
|
|
|
// For some reason, if we try to change the theme while the `pageshow` event is
|
|
|
|
// running, it sometimes fails to take effect. The problem manifests on Chrome,
|
|
|
|
// specifically when talking to a remote website with no caching.
|
2022-05-03 12:03:17 +08:00
|
|
|
window.addEventListener("pageshow", ev => {
|
2022-01-23 13:04:17 -08:00
|
|
|
if (ev.persisted) {
|
|
|
|
setTimeout(switchToSavedTheme, 0);
|
|
|
|
}
|
|
|
|
});
|