Skip to content

Commit b4580d5

Browse files
Simplify settings UI by merging system theme with the theme choices
1 parent 928d14b commit b4580d5

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

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

+36-24
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@
99
const isSettingsPage = window.location.pathname.endsWith("/settings.html");
1010

1111
function changeSetting(settingName, value) {
12+
if (settingName === "theme") {
13+
const useSystem = value === "system preference" ? "true" : "false";
14+
updateLocalStorage("use-system-theme", useSystem);
15+
}
1216
updateLocalStorage(settingName, value);
1317

1418
switch (settingName) {
1519
case "theme":
1620
case "preferred-dark-theme":
1721
case "preferred-light-theme":
18-
case "use-system-theme":
1922
updateSystemTheme();
2023
updateLightAndDark();
2124
break;
@@ -45,19 +48,18 @@
4548
}
4649

4750
function showLightAndDark() {
48-
addClass(document.getElementById("theme").parentElement, "hidden");
4951
removeClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
5052
removeClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
5153
}
5254

5355
function hideLightAndDark() {
5456
addClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
5557
addClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
56-
removeClass(document.getElementById("theme").parentElement, "hidden");
5758
}
5859

5960
function updateLightAndDark() {
60-
if (getSettingValue("use-system-theme") !== "false") {
61+
const useSystem = getSettingValue("use-system-theme");
62+
if (useSystem === "true" || (useSystem === null && getSettingValue("theme") === null)) {
6163
showLightAndDark();
6264
} else {
6365
hideLightAndDark();
@@ -91,7 +93,18 @@
9193
});
9294
onEachLazy(settingsElement.querySelectorAll("input[type=\"radio\"]"), elem => {
9395
const settingId = elem.name;
94-
const settingValue = getSettingValue(settingId);
96+
let settingValue = getSettingValue(settingId);
97+
if (settingId === "theme") {
98+
const useSystem = getSettingValue("use-system-theme");
99+
if (useSystem === "true" || settingValue === null) {
100+
if (useSystem !== "false") {
101+
settingValue = "system preference";
102+
} else {
103+
// This is the default theme.
104+
settingValue = "light";
105+
}
106+
}
107+
}
95108
if (settingValue !== null && settingValue !== "null") {
96109
elem.checked = settingValue === elem.value;
97110
}
@@ -120,26 +133,30 @@
120133

121134
if (setting["options"] !== undefined) {
122135
// This is a select setting.
123-
output += `<div class="radio-line" id="${js_data_name}">\
124-
<span class="setting-name">${setting_name}</span>\
125-
<div class="choices">`;
136+
output += `\
137+
<div class="radio-line" id="${js_data_name}">
138+
<span class="setting-name">${setting_name}</span>
139+
<div class="choices">`;
126140
onEach(setting["options"], option => {
127141
const checked = option === setting["default"] ? " checked" : "";
142+
const full = `${js_data_name}-${option.replace(/ /g,"-")}`;
128143

129-
output += `<label for="${js_data_name}-${option}" class="choice">\
130-
<input type="radio" name="${js_data_name}" \
131-
id="${js_data_name}-${option}" value="${option}"${checked}>\
132-
<span>${option}</span>\
133-
</label>`;
144+
output += `\
145+
<label for="${full}" class="choice">
146+
<input type="radio" name="${js_data_name}"
147+
id="${full}" value="${option}"${checked}>
148+
<span>${option}</span>
149+
</label>`;
134150
});
135151
output += "</div></div>";
136152
} else {
137153
// This is a toggle.
138154
const checked = setting["default"] === true ? " checked" : "";
139-
output += `<label class="toggle">\
140-
<input type="checkbox" id="${js_data_name}"${checked}>\
141-
<span class="label">${setting_name}</span>\
142-
</label>`;
155+
output += `\
156+
<label class="toggle">\
157+
<input type="checkbox" id="${js_data_name}"${checked}>\
158+
<span class="label">${setting_name}</span>\
159+
</label>`;
143160
}
144161
output += "</div>";
145162
}
@@ -156,16 +173,11 @@
156173
theme_names.push("light", "dark", "ayu");
157174

158175
const settings = [
159-
{
160-
"name": "Use system theme",
161-
"js_name": "use-system-theme",
162-
"default": true,
163-
},
164176
{
165177
"name": "Theme",
166178
"js_name": "theme",
167-
"default": "light",
168-
"options": theme_names,
179+
"default": "system preference",
180+
"options": theme_names.concat("system preference"),
169181
},
170182
{
171183
"name": "Preferred light theme",

0 commit comments

Comments
 (0)