From 59f9cf2ac1872866a9a2d4b83f56b96f131a6a5e Mon Sep 17 00:00:00 2001 From: nasso Date: Thu, 15 Oct 2020 18:03:25 +0200 Subject: [PATCH] Set preferred-dark-theme to the last dark theme If the user doesn't have a preferred dark theme but is already using a dark theme, set the preferred dark theme to be that theme --- src/librustdoc/html/static/storage.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/librustdoc/html/static/storage.js b/src/librustdoc/html/static/storage.js index cf8b806501f..a027d6845ea 100644 --- a/src/librustdoc/html/static/storage.js +++ b/src/librustdoc/html/static/storage.js @@ -1,8 +1,10 @@ // From rust: /* global resourcesSuffix */ +var darkThemes = ["dark", "ayu"]; var currentTheme = document.getElementById("themeStyle"); var mainTheme = document.getElementById("mainThemeStyle"); +var localStoredTheme = getCurrentValue("rustdoc-theme"); var savedHref = []; @@ -179,6 +181,14 @@ var updateSystemTheme = (function() { })(); if (getCurrentValue("rustdoc-use-system-theme") !== "false" && window.matchMedia) { + // 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 + if (getCurrentValue("rustdoc-use-system-theme") === null + && getCurrentValue("rustdoc-preferred-dark-theme") === null + && darkThemes.indexOf(localStoredTheme) >= 0) { + updateLocalStorage("rustdoc-preferred-dark-theme", localStoredTheme); + } + // call the function to initialize the theme at least once! updateSystemTheme(); } else {