2020-10-15 18:03:25 +02:00
|
|
|
var 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
|
|
|
|
2020-09-23 22:44:54 +01:00
|
|
|
var settingsDataset = (function () {
|
|
|
|
var settingsElement = document.getElementById("default-settings");
|
|
|
|
if (settingsElement === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var dataset = settingsElement.dataset;
|
|
|
|
if (dataset === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return dataset;
|
|
|
|
})();
|
|
|
|
|
2020-09-23 22:14:43 +01:00
|
|
|
function getSettingValue(settingName) {
|
2020-09-23 22:44:54 +01:00
|
|
|
var current = getCurrentValue('rustdoc-' + settingName);
|
|
|
|
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`.
|
2020-09-23 22:44:54 +01:00
|
|
|
var def = settingsDataset[settingName.replace(/-/g,'_')];
|
|
|
|
if (def !== undefined) {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2020-09-23 22:14:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var localStoredTheme = getSettingValue("theme");
|
2018-01-22 23:44:08 +01:00
|
|
|
|
2018-02-20 20:11:58 +01:00
|
|
|
var savedHref = [];
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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) {
|
2018-11-16 16:31:07 +01:00
|
|
|
var length = arr.length;
|
2020-05-17 15:34:59 +02:00
|
|
|
var i;
|
2021-05-09 13:49:22 -07:00
|
|
|
if (reversed) {
|
|
|
|
for (i = length - 1; i >= 0; --i) {
|
|
|
|
if (func(arr[i])) {
|
2018-09-12 23:31:21 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-09 13:49:22 -07:00
|
|
|
for (i = 0; i < length; ++i) {
|
|
|
|
if (func(arr[i])) {
|
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
|
|
|
}
|
|
|
|
|
2018-12-02 01:22:44 +01:00
|
|
|
function onEachLazy(lazyArray, func, reversed) {
|
|
|
|
return onEach(
|
|
|
|
Array.prototype.slice.call(lazyArray),
|
|
|
|
func,
|
|
|
|
reversed);
|
|
|
|
}
|
|
|
|
|
2021-01-23 14:55:24 +01:00
|
|
|
// eslint-disable-next-line no-unused-vars
|
2021-05-14 13:56:15 +02:00
|
|
|
function hasOwnPropertyRustdoc(obj, property) {
|
2020-05-17 15:34:59 +02:00
|
|
|
return Object.prototype.hasOwnProperty.call(obj, property);
|
|
|
|
}
|
|
|
|
|
2018-10-16 22:15:27 -07:00
|
|
|
function updateLocalStorage(name, value) {
|
2021-02-10 14:53:22 +01:00
|
|
|
try {
|
|
|
|
window.localStorage.setItem(name, value);
|
|
|
|
} catch(e) {
|
|
|
|
// 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 {
|
2021-02-10 14:55:53 +01:00
|
|
|
return window.localStorage.getItem(name);
|
2021-02-10 14:53:22 +01:00
|
|
|
} catch(e) {
|
|
|
|
return null;
|
2018-10-16 22:15:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-21 12:49:01 +02:00
|
|
|
function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
|
2021-11-23 19:22:29 -08:00
|
|
|
var newHref = mainStyleElem.href.replace(
|
|
|
|
/\/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) {
|
2020-10-23 18:58:42 +02:00
|
|
|
updateLocalStorage("rustdoc-theme", newTheme);
|
|
|
|
}
|
|
|
|
|
2018-03-08 00:11:17 +01:00
|
|
|
if (styleElem.href === newHref) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var found = false;
|
2018-02-20 20:11:58 +01:00
|
|
|
if (savedHref.length === 0) {
|
2018-12-16 14:44:44 +01:00
|
|
|
onEachLazy(document.getElementsByTagName("link"), function(el) {
|
2018-02-20 20:11:58 +01:00
|
|
|
savedHref.push(el.href);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onEach(savedHref, function(el) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateLocalStorage("rustdoc-use-system-theme", value);
|
|
|
|
|
|
|
|
// update the toggle if we're on the settings page
|
|
|
|
var toggle = document.getElementById("use-system-theme");
|
|
|
|
if (toggle && toggle instanceof HTMLInputElement) {
|
|
|
|
toggle.checked = value;
|
|
|
|
}
|
2019-05-27 15:57:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-11 02:53:37 +02:00
|
|
|
var updateSystemTheme = (function() {
|
|
|
|
if (!window.matchMedia) {
|
|
|
|
// fallback to the CSS computed value
|
|
|
|
return function() {
|
2021-02-15 10:11:37 +01:00
|
|
|
var cssTheme = getComputedStyle(document.documentElement)
|
2020-10-11 02:53:37 +02:00
|
|
|
.getPropertyValue('content');
|
|
|
|
|
|
|
|
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
|
|
|
|
var mql = window.matchMedia("(prefers-color-scheme: dark)");
|
|
|
|
|
|
|
|
function handlePreferenceChange(mql) {
|
|
|
|
// maybe the user has disabled the setting in the meantime!
|
2020-09-23 22:14:43 +01:00
|
|
|
if (getSettingValue("use-system-theme") !== "false") {
|
|
|
|
var lightTheme = getSettingValue("preferred-light-theme") || "light";
|
|
|
|
var darkTheme = getSettingValue("preferred-dark-theme") || "dark";
|
2020-10-11 02:53:37 +02:00
|
|
|
|
|
|
|
if (mql.matches) {
|
|
|
|
// prefers a dark theme
|
2021-03-05 16:09:46 +01:00
|
|
|
switchTheme(window.currentTheme, window.mainTheme, darkTheme, true);
|
2020-10-11 02:53:37 +02:00
|
|
|
} else {
|
|
|
|
// prefers a light theme, or has no preference
|
2021-03-05 16:09:46 +01:00
|
|
|
switchTheme(window.currentTheme, window.mainTheme, lightTheme, true);
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mql.addListener(handlePreferenceChange);
|
|
|
|
|
|
|
|
return function() {
|
|
|
|
handlePreferenceChange(mql);
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
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) {
|
|
|
|
updateLocalStorage("rustdoc-preferred-dark-theme", localStoredTheme);
|
|
|
|
}
|
|
|
|
|
2020-10-11 02:53:37 +02:00
|
|
|
// call the function to initialize the theme at least once!
|
|
|
|
updateSystemTheme();
|
|
|
|
} else {
|
2020-10-11 17:52:47 +02:00
|
|
|
switchTheme(
|
2021-03-05 16:09:46 +01:00
|
|
|
window.currentTheme,
|
|
|
|
window.mainTheme,
|
2020-09-23 22:14:43 +01:00
|
|
|
getSettingValue("theme") || "light",
|
2020-10-11 17:52:47 +02:00
|
|
|
false
|
|
|
|
);
|
2020-10-11 02:53:37 +02:00
|
|
|
}
|