Skip to content

Commit 96ccba2

Browse files
Simplify "system theme" handling
1 parent 6e4a9ab commit 96ccba2

File tree

2 files changed

+23
-41
lines changed

2 files changed

+23
-41
lines changed

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

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Local js definitions:
22
/* global getSettingValue, getVirtualKey, updateLocalStorage, updateSystemTheme */
33
/* global addClass, removeClass, onEach, onEachLazy, blurHandler, elemIsInParent */
4-
/* global MAIN_ID, getVar, getSettingsButton */
4+
/* global MAIN_ID, getVar, getSettingsButton, isUsingSystemTheme */
55

66
"use strict";
77

@@ -15,7 +15,6 @@
1515
case "theme":
1616
case "preferred-dark-theme":
1717
case "preferred-light-theme":
18-
case "use-system-theme":
1918
updateSystemTheme();
2019
updateLightAndDark();
2120
break;
@@ -38,19 +37,17 @@
3837
}
3938

4039
function showLightAndDark() {
41-
addClass(document.getElementById("theme").parentElement, "hidden");
4240
removeClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
4341
removeClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
4442
}
4543

4644
function hideLightAndDark() {
4745
addClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
4846
addClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
49-
removeClass(document.getElementById("theme").parentElement, "hidden");
5047
}
5148

5249
function updateLightAndDark() {
53-
if (getSettingValue("use-system-theme") !== "false") {
50+
if (isUsingSystemTheme()) {
5451
showLightAndDark();
5552
} else {
5653
hideLightAndDark();
@@ -113,18 +110,19 @@
113110
const setting_name = setting["name"];
114111

115112
if (setting["options"] !== undefined) {
116-
// This is a select setting.
113+
// This is a <select> setting.
117114
output += `<div class="radio-line" id="${js_data_name}">\
118115
<span class="setting-name">${setting_name}</span>\
119116
<div class="choices">`;
120117
onEach(setting["options"], option => {
121-
const checked = option === setting["default"] ? " checked" : "";
118+
const optionAttr = option.split(" ").join("-");
119+
const checked = optionAttr === setting["default"] ? " checked" : "";
122120

123-
output += `<label for="${js_data_name}-${option}" class="choice">\
121+
output += `<label for="${js_data_name}-${optionAttr}" class="choice">\
124122
<input type="radio" name="${js_data_name}" \
125-
id="${js_data_name}-${option}" value="${option}"${checked}>\
123+
id="${js_data_name}-${optionAttr}" value="${optionAttr}"${checked}>\
126124
<span>${option}</span>\
127-
</label>`;
125+
</label>`;
128126
});
129127
output += "</div></div>";
130128
} else {
@@ -149,16 +147,11 @@
149147
function buildSettingsPage() {
150148
const themes = getVar("themes").split(",");
151149
const settings = [
152-
{
153-
"name": "Use system theme",
154-
"js_name": "use-system-theme",
155-
"default": true,
156-
},
157150
{
158151
"name": "Theme",
159152
"js_name": "theme",
160-
"default": "light",
161-
"options": themes,
153+
"default": "",
154+
"options": themes.concat("system preference"),
162155
},
163156
{
164157
"name": "Preferred light theme",

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

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ function getSettingValue(settingName) {
4242
return null;
4343
}
4444

45+
function isUsingSystemTheme() {
46+
const current = getSettingValue("theme");
47+
return current === null || current === "system-preference";
48+
}
49+
4550
const localStoredTheme = getSettingValue("theme");
4651

4752
const savedHref = [];
@@ -157,22 +162,6 @@ function switchTheme(styleElem, mainStyleElem, newTheme, saveTheme) {
157162
}
158163
}
159164

160-
// This function is called from "main.js".
161-
// eslint-disable-next-line no-unused-vars
162-
function useSystemTheme(value) {
163-
if (value === undefined) {
164-
value = true;
165-
}
166-
167-
updateLocalStorage("use-system-theme", value);
168-
169-
// update the toggle if we're on the settings page
170-
const toggle = document.getElementById("use-system-theme");
171-
if (toggle && toggle instanceof HTMLInputElement) {
172-
toggle.checked = value;
173-
}
174-
}
175-
176165
const updateSystemTheme = (function() {
177166
if (!window.matchMedia) {
178167
// fallback to the CSS computed value
@@ -193,25 +182,25 @@ const updateSystemTheme = (function() {
193182
const mql = window.matchMedia("(prefers-color-scheme: dark)");
194183

195184
function handlePreferenceChange(mql) {
196-
const use = theme => {
197-
switchTheme(window.currentTheme, window.mainTheme, theme, true);
185+
const use = (theme, saveIt) => {
186+
switchTheme(window.currentTheme, window.mainTheme, theme, saveIt);
198187
};
199188
// maybe the user has disabled the setting in the meantime!
200-
if (getSettingValue("use-system-theme") !== "false") {
189+
if (isUsingSystemTheme()) {
201190
const lightTheme = getSettingValue("preferred-light-theme") || "light";
202191
const darkTheme = getSettingValue("preferred-dark-theme") || "dark";
203192

204193
if (mql.matches) {
205-
use(darkTheme);
194+
use(darkTheme, false);
206195
} else {
207196
// prefers a light theme, or has no preference
208-
use(lightTheme);
197+
use(lightTheme, false);
209198
}
210199
// note: we save the theme so that it doesn't suddenly change when
211200
// the user disables "use-system-theme" and reloads the page or
212201
// navigates to another page
213202
} else {
214-
use(getSettingValue("theme"));
203+
use(getSettingValue("theme"), true);
215204
}
216205
}
217206

@@ -231,10 +220,10 @@ function switchToSavedTheme() {
231220
);
232221
}
233222

234-
if (getSettingValue("use-system-theme") !== "false" && window.matchMedia) {
223+
if (isUsingSystemTheme() && window.matchMedia) {
235224
// update the preferred dark theme if the user is already using a dark theme
236225
// See https://github.com/rust-lang/rust/pull/77809#issuecomment-707875732
237-
if (getSettingValue("use-system-theme") === null
226+
if (getSettingValue("theme") === null
238227
&& getSettingValue("preferred-dark-theme") === null
239228
&& darkThemes.indexOf(localStoredTheme) >= 0) {
240229
updateLocalStorage("preferred-dark-theme", localStoredTheme);

0 commit comments

Comments
 (0)