Skip to content

Commit

Permalink
Also get previous "system-preference" setting
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Sep 5, 2022
1 parent 87e9dc3 commit eb277fb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/librustdoc/html/static/js/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* global getSettingValue, getVirtualKey, updateLocalStorage, updateSystemTheme */
/* global addClass, removeClass, onEach, onEachLazy, blurHandler, elemIsInParent */
/* global MAIN_ID, getVar, getSettingsButton, isUsingSystemTheme, CURRENT_THEME_SETTING_VERSION */
/* global getTheme, getPreferredDarkTheme, getPreferredLightTheme */

"use strict";

Expand Down Expand Up @@ -159,19 +160,19 @@
"name": "Theme",
"js_name": "theme",
"js_name_version": CURRENT_THEME_SETTING_VERSION,
"default": "system-preference",
"default": getTheme(),
"options": themes.concat("system preference"),
},
{
"name": "Preferred light theme",
"js_name": "preferred-light-theme",
"default": "light",
"default": getPreferredLightTheme(),
"options": themes,
},
{
"name": "Preferred dark theme",
"js_name": "preferred-dark-theme",
"default": "dark",
"default": getPreferredDarkTheme(),
"options": themes,
},
{
Expand Down
25 changes: 22 additions & 3 deletions src/librustdoc/html/static/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,34 @@ function isUsingSystemTheme() {
return current === null || current === "system-preference";
}

function valueOr(value, or) {
if (value !== null) {
return value;
}
return or;
}

function getTheme() {
const current = getSettingValue(`theme${CURRENT_THEME_SETTING_VERSION}`);
if (current === null) {
// We try to get what's being used in the previous version.
return getSettingValue("theme");
const isUsingSystemTheme = getSettingValue("use-system-theme");
if (isUsingSystemTheme === "true") {
return "system-preference";
}
return valueOr(getSettingValue("theme"), "system-preference");
}
return current;
}

function getPreferredDarkTheme() {
return valueOr(getSettingValue("preferred-dark-theme"), "dark");
}

function getPreferredLightTheme() {
return valueOr(getSettingValue("preferred-light-theme"), "light");
}

const savedHref = [];

// eslint-disable-next-line no-unused-vars
Expand Down Expand Up @@ -194,8 +213,8 @@ const updateSystemTheme = (function() {
};
// maybe the user has disabled the setting in the meantime!
if (isUsingSystemTheme()) {
const lightTheme = getSettingValue("preferred-light-theme") || "light";
const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
const lightTheme = getPreferredLightTheme();
const darkTheme = getPreferredDarkTheme();

if (mql.matches) {
use(darkTheme, false);
Expand Down
10 changes: 10 additions & 0 deletions src/test/rustdoc-gui/settings.goml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ wait-for-css: ("#settings", {"display": "block"})
wait-for: "#alternative-display #search"
assert: "#main-content.hidden"

// Checking the default values of the themes:
// * default: system
// * default preferred dark: dark
// * default preferred light: light
assert-property: ("#theme-system-preference", {"checked": "true"})
assert-property: ("#preferred-dark-theme-dark", {"checked": "true"})
assert-property: ("#preferred-light-theme-light", {"checked": "true"})
assert: ".setting-line:not(.hidden) #preferred-dark-theme"
assert: ".setting-line:not(.hidden) #preferred-light-theme"

// Now let's check the content of the settings menu.
local-storage: {"rustdoc-theme2": "dark"}
reload:
Expand Down

0 comments on commit eb277fb

Please sign in to comment.