Skip to content

Commit c3ddca6

Browse files
authored
Rollup merge of #93251 - jsha:theme-radio, r=GuillaumeGomez
rustdoc settings: use radio buttons for theme This reduces the number of clicks required to change theme. Also, simplify the UI a bit (remove setting grouping), and add a "Back" link close to the settings icon. Demo: https://rustdoc.crud.net/jsha/theme-radio/settings.html r? ``@GuillaumeGomez`` New: ![image](https://user-images.githubusercontent.com/220205/150702647-4826d525-54fa-439a-b24c-6d5bca6f95bf.png) Old: ![image](https://user-images.githubusercontent.com/220205/150702669-6a4214ed-1dab-4fee-b1aa-59acfce3dbca.png)
2 parents 3d6f276 + 11b17c6 commit c3ddca6

File tree

3 files changed

+78
-50
lines changed

3 files changed

+78
-50
lines changed

src/librustdoc/html/render/mod.rs

+35-40
Original file line numberDiff line numberDiff line change
@@ -376,25 +376,21 @@ impl Setting {
376376
description,
377377
),
378378
Setting::Select { js_data_name, description, default_value, ref options } => format!(
379-
"<div class=\"setting-line\">\
380-
<div>{}</div>\
381-
<label class=\"select-wrapper\">\
382-
<select id=\"{}\" autocomplete=\"off\">{}</select>\
383-
<img src=\"{}down-arrow{}.svg\" alt=\"Select item\">\
384-
</label>\
385-
</div>",
386-
description,
379+
"<div class=\"setting-line\"><div class=\"radio-line\" id=\"{}\"><span class=\"setting-name\">{}</span>{}</div></div>",
387380
js_data_name,
381+
description,
388382
options
389383
.iter()
390384
.map(|opt| format!(
391-
"<option value=\"{name}\" {}>{name}</option>",
392-
if opt == default_value { "selected" } else { "" },
385+
"<label for=\"{js_data_name}-{name}\" class=\"choice\">
386+
<input type=\"radio\" name=\"{js_data_name}\" id=\"{js_data_name}-{name}\" value=\"{name}\" {checked}>\
387+
{name}\
388+
</label>",
389+
js_data_name = js_data_name,
393390
name = opt,
391+
checked = if opt == default_value { "checked" } else { "" },
394392
))
395393
.collect::<String>(),
396-
root_path,
397-
suffix,
398394
),
399395
}
400396
}
@@ -418,31 +414,25 @@ impl<T: Into<Setting>> From<(&'static str, Vec<T>)> for Setting {
418414
fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<String, Error> {
419415
// (id, explanation, default value)
420416
let settings: &[Setting] = &[
421-
(
422-
"Theme preferences",
423-
vec![
424-
Setting::from(("use-system-theme", "Use system theme", true)),
425-
Setting::Select {
426-
js_data_name: "theme",
427-
description: "Theme",
428-
default_value: "light",
429-
options: theme_names.clone(),
430-
},
431-
Setting::Select {
432-
js_data_name: "preferred-dark-theme",
433-
description: "Preferred dark theme",
434-
default_value: "dark",
435-
options: theme_names.clone(),
436-
},
437-
Setting::Select {
438-
js_data_name: "preferred-light-theme",
439-
description: "Preferred light theme",
440-
default_value: "light",
441-
options: theme_names,
442-
},
443-
],
444-
)
445-
.into(),
417+
Setting::from(("use-system-theme", "Use system theme", true)),
418+
Setting::Select {
419+
js_data_name: "theme",
420+
description: "Theme",
421+
default_value: "light",
422+
options: theme_names.clone(),
423+
},
424+
Setting::Select {
425+
js_data_name: "preferred-light-theme",
426+
description: "Preferred light theme",
427+
default_value: "light",
428+
options: theme_names.clone(),
429+
},
430+
Setting::Select {
431+
js_data_name: "preferred-dark-theme",
432+
description: "Preferred dark theme",
433+
default_value: "dark",
434+
options: theme_names,
435+
},
446436
("auto-hide-large-items", "Auto-hide item contents for large items.", true).into(),
447437
("auto-hide-method-docs", "Auto-hide item methods' documentation", false).into(),
448438
("auto-hide-trait-implementations", "Auto-hide trait implementation documentation", false)
@@ -454,9 +444,14 @@ fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<S
454444
];
455445

456446
Ok(format!(
457-
"<h1 class=\"fqn\">\
458-
<span class=\"in-band\">Rustdoc settings</span>\
459-
</h1>\
447+
"<div class=\"main-heading\">
448+
<h1 class=\"fqn\">\
449+
<span class=\"in-band\">Rustdoc settings</span>\
450+
</h1>\
451+
<span class=\"out-of-band\">\
452+
<a id=\"back\" href=\"javascript:void(0)\">Back</a>\
453+
</span>\
454+
</div>\
460455
<div class=\"settings\">{}</div>\
461456
<link rel=\"stylesheet\" href=\"{root_path}settings{suffix}.css\">\
462457
<script src=\"{root_path}settings{suffix}.js\"></script>",

src/librustdoc/html/static/css/settings.css

+24
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,30 @@
1717
border-bottom: 1px solid;
1818
}
1919

20+
.setting-line .radio-line {
21+
display: flex;
22+
flex-wrap: wrap;
23+
}
24+
25+
.setting-line .radio-line > * {
26+
padding: 0.3em;
27+
}
28+
29+
.setting-line .radio-line .setting-name {
30+
flex-grow: 1;
31+
}
32+
33+
.setting-line .radio-line input {
34+
margin-right: 0.3em;
35+
}
36+
37+
.radio-line .choice {
38+
border-radius: 0.1em;
39+
border: 1px solid;
40+
margin-left: 0.5em;
41+
min-width: 3.5em;
42+
}
43+
2044
.toggle {
2145
position: relative;
2246
display: inline-block;

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

+19-10
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,15 @@
3333
}
3434

3535
function showLightAndDark() {
36-
addClass(document.getElementById("theme").parentElement.parentElement, "hidden");
37-
removeClass(document.getElementById("preferred-light-theme").parentElement.parentElement,
38-
"hidden");
39-
removeClass(document.getElementById("preferred-dark-theme").parentElement.parentElement,
40-
"hidden");
36+
addClass(document.getElementById("theme").parentElement, "hidden");
37+
removeClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
38+
removeClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
4139
}
4240

4341
function hideLightAndDark() {
44-
addClass(document.getElementById("preferred-light-theme").parentElement.parentElement,
45-
"hidden");
46-
addClass(document.getElementById("preferred-dark-theme").parentElement.parentElement,
47-
"hidden");
48-
removeClass(document.getElementById("theme").parentElement.parentElement, "hidden");
42+
addClass(document.getElementById("preferred-light-theme").parentElement, "hidden");
43+
addClass(document.getElementById("preferred-dark-theme").parentElement, "hidden");
44+
removeClass(document.getElementById("theme").parentElement, "hidden");
4945
}
5046

5147
function updateLightAndDark() {
@@ -82,6 +78,19 @@
8278
changeSetting(this.id, this.value);
8379
};
8480
});
81+
onEachLazy(document.querySelectorAll("input[type=\"radio\"]"), function(elem) {
82+
const settingId = elem.name;
83+
const settingValue = getSettingValue(settingId);
84+
if (settingValue !== null && settingValue !== "null") {
85+
elem.checked = settingValue === elem.value;
86+
}
87+
elem.addEventListener("change", function(ev) {
88+
changeSetting(ev.target.name, ev.target.value);
89+
});
90+
});
91+
document.getElementById("back").addEventListener("click", function() {
92+
history.back();
93+
});
8594
}
8695

8796
window.addEventListener("DOMContentLoaded", setEvents);

0 commit comments

Comments
 (0)