Skip to content

Commit eb277fb

Browse files
Also get previous "system-preference" setting
1 parent 87e9dc3 commit eb277fb

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/librustdoc/html/static/js/settings.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/* global getSettingValue, getVirtualKey, updateLocalStorage, updateSystemTheme */
33
/* global addClass, removeClass, onEach, onEachLazy, blurHandler, elemIsInParent */
44
/* global MAIN_ID, getVar, getSettingsButton, isUsingSystemTheme, CURRENT_THEME_SETTING_VERSION */
5+
/* global getTheme, getPreferredDarkTheme, getPreferredLightTheme */
56

67
"use strict";
78

@@ -159,19 +160,19 @@
159160
"name": "Theme",
160161
"js_name": "theme",
161162
"js_name_version": CURRENT_THEME_SETTING_VERSION,
162-
"default": "system-preference",
163+
"default": getTheme(),
163164
"options": themes.concat("system preference"),
164165
},
165166
{
166167
"name": "Preferred light theme",
167168
"js_name": "preferred-light-theme",
168-
"default": "light",
169+
"default": getPreferredLightTheme(),
169170
"options": themes,
170171
},
171172
{
172173
"name": "Preferred dark theme",
173174
"js_name": "preferred-dark-theme",
174-
"default": "dark",
175+
"default": getPreferredDarkTheme(),
175176
"options": themes,
176177
},
177178
{

src/librustdoc/html/static/js/storage.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,34 @@ function isUsingSystemTheme() {
4747
return current === null || current === "system-preference";
4848
}
4949

50+
function valueOr(value, or) {
51+
if (value !== null) {
52+
return value;
53+
}
54+
return or;
55+
}
56+
5057
function getTheme() {
5158
const current = getSettingValue(`theme${CURRENT_THEME_SETTING_VERSION}`);
5259
if (current === null) {
5360
// We try to get what's being used in the previous version.
54-
return getSettingValue("theme");
61+
const isUsingSystemTheme = getSettingValue("use-system-theme");
62+
if (isUsingSystemTheme === "true") {
63+
return "system-preference";
64+
}
65+
return valueOr(getSettingValue("theme"), "system-preference");
5566
}
5667
return current;
5768
}
5869

70+
function getPreferredDarkTheme() {
71+
return valueOr(getSettingValue("preferred-dark-theme"), "dark");
72+
}
73+
74+
function getPreferredLightTheme() {
75+
return valueOr(getSettingValue("preferred-light-theme"), "light");
76+
}
77+
5978
const savedHref = [];
6079

6180
// eslint-disable-next-line no-unused-vars
@@ -194,8 +213,8 @@ const updateSystemTheme = (function() {
194213
};
195214
// maybe the user has disabled the setting in the meantime!
196215
if (isUsingSystemTheme()) {
197-
const lightTheme = getSettingValue("preferred-light-theme") || "light";
198-
const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
216+
const lightTheme = getPreferredLightTheme();
217+
const darkTheme = getPreferredDarkTheme();
199218

200219
if (mql.matches) {
201220
use(darkTheme, false);

src/test/rustdoc-gui/settings.goml

+10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ wait-for-css: ("#settings", {"display": "block"})
2929
wait-for: "#alternative-display #search"
3030
assert: "#main-content.hidden"
3131

32+
// Checking the default values of the themes:
33+
// * default: system
34+
// * default preferred dark: dark
35+
// * default preferred light: light
36+
assert-property: ("#theme-system-preference", {"checked": "true"})
37+
assert-property: ("#preferred-dark-theme-dark", {"checked": "true"})
38+
assert-property: ("#preferred-light-theme-light", {"checked": "true"})
39+
assert: ".setting-line:not(.hidden) #preferred-dark-theme"
40+
assert: ".setting-line:not(.hidden) #preferred-light-theme"
41+
3242
// Now let's check the content of the settings menu.
3343
local-storage: {"rustdoc-theme2": "dark"}
3444
reload:

0 commit comments

Comments
 (0)