Skip to content

Commit 28193e8

Browse files
committed
Auto merge of #49954 - GuillaumeGomez:doc-settings, r=ollie27,QuietMisdreavus
Add rustdoc settings menu Fixes #18167. r? @QuietMisdreavus
2 parents 0cbd310 + 1f7892f commit 28193e8

File tree

9 files changed

+231
-26
lines changed

9 files changed

+231
-26
lines changed

src/librustdoc/html/layout.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
8585
autocomplete=\"off\" \
8686
placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \
8787
type=\"search\">\
88+
<a id=\"settings-menu\" href=\"{root_path}settings.html\">\
89+
<img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\
90+
</a>\
8891
</div>\
8992
</form>\
9093
</nav>\
@@ -182,9 +185,10 @@ pub fn render<T: fmt::Display, S: fmt::Display>(
182185
themes = themes.iter()
183186
.filter_map(|t| t.file_stem())
184187
.filter_map(|t| t.to_str())
185-
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}">"#,
188+
.map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}{}.css">"#,
186189
page.root_path,
187-
t.replace(".css", &format!("{}.css", page.resource_suffix))))
190+
t,
191+
page.resource_suffix))
188192
.collect::<String>(),
189193
suffix=page.resource_suffix,
190194
)

src/librustdoc/html/render.rs

+76-3
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ fn write_shared(cx: &Context,
750750

751751
write(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)),
752752
include_bytes!("static/rustdoc.css"))?;
753+
write(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)),
754+
include_bytes!("static/settings.css"))?;
753755

754756
// To avoid "light.css" to be overwritten, we'll first run over the received themes and only
755757
// then we'll run over the "official" styles.
@@ -769,6 +771,8 @@ fn write_shared(cx: &Context,
769771

770772
write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)),
771773
include_bytes!("static/brush.svg"))?;
774+
write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)),
775+
include_bytes!("static/wheel.svg"))?;
772776
write(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)),
773777
include_bytes!("static/themes/light.css"))?;
774778
themes.insert("light".to_owned());
@@ -802,8 +806,7 @@ themePicker.onclick = function() {{
802806
switchTheme(currentTheme, mainTheme, item);
803807
}};
804808
themes.appendChild(but);
805-
}});
806-
"#,
809+
}});"#,
807810
themes.iter()
808811
.map(|s| format!("\"{}\"", s))
809812
.collect::<Vec<String>>()
@@ -812,6 +815,8 @@ themePicker.onclick = function() {{
812815

813816
write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)),
814817
include_bytes!("static/main.js"))?;
818+
write(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)),
819+
include_bytes!("static/settings.js"))?;
815820

816821
{
817822
let mut data = format!("var resourcesSuffix = \"{}\";\n",
@@ -1575,6 +1580,51 @@ impl fmt::Display for AllTypes {
15751580
}
15761581
}
15771582

1583+
#[derive(Debug)]
1584+
struct Settings<'a> {
1585+
// (id, explanation, default value)
1586+
settings: Vec<(&'static str, &'static str, bool)>,
1587+
root_path: &'a str,
1588+
suffix: &'a str,
1589+
}
1590+
1591+
impl<'a> Settings<'a> {
1592+
pub fn new(root_path: &'a str, suffix: &'a str) -> Settings<'a> {
1593+
Settings {
1594+
settings: vec![
1595+
("item-declarations", "Auto-hide item declarations.", true),
1596+
("item-attributes", "Auto-hide item attributes.", true),
1597+
],
1598+
root_path,
1599+
suffix,
1600+
}
1601+
}
1602+
}
1603+
1604+
impl<'a> fmt::Display for Settings<'a> {
1605+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1606+
write!(f,
1607+
"<h1 class='fqn'>\
1608+
<span class='in-band'>Rustdoc settings</span>\
1609+
</h1>\
1610+
<div class='settings'>{}</div>\
1611+
<script src='{}settings{}.js'></script>",
1612+
self.settings.iter()
1613+
.map(|(id, text, enabled)| {
1614+
format!("<div class='setting-line'>\
1615+
<label class='toggle'>\
1616+
<input type='checkbox' id='{}' {}>\
1617+
<span class='slider'></span>\
1618+
</label>\
1619+
<div>{}</div>\
1620+
</div>", id, if *enabled { " checked" } else { "" }, text)
1621+
})
1622+
.collect::<String>(),
1623+
self.root_path,
1624+
self.suffix)
1625+
}
1626+
}
1627+
15781628
impl Context {
15791629
/// String representation of how to get back to the root path of the 'doc/'
15801630
/// folder in terms of a relative URL.
@@ -1618,6 +1668,8 @@ impl Context {
16181668
};
16191669
let final_file = self.dst.join(&krate.name)
16201670
.join("all.html");
1671+
let settings_file = self.dst.join("settings.html");
1672+
16211673
let crate_name = krate.name.clone();
16221674
item.name = Some(krate.name);
16231675

@@ -1639,7 +1691,7 @@ impl Context {
16391691
if !root_path.ends_with('/') {
16401692
root_path.push('/');
16411693
}
1642-
let page = layout::Page {
1694+
let mut page = layout::Page {
16431695
title: "List of all items in this crate",
16441696
css_class: "mod",
16451697
root_path: "../",
@@ -1662,6 +1714,27 @@ impl Context {
16621714
self.shared.css_file_extension.is_some(),
16631715
&self.shared.themes),
16641716
&final_file);
1717+
1718+
// Generating settings page.
1719+
let settings = Settings::new("./", &self.shared.resource_suffix);
1720+
page.title = "Rustdoc settings";
1721+
page.description = "Settings of Rustdoc";
1722+
page.root_path = "./";
1723+
1724+
let mut w = BufWriter::new(try_err!(File::create(&settings_file), &settings_file));
1725+
let mut themes = self.shared.themes.clone();
1726+
let sidebar = "<p class='location'>Settings</p><div class='sidebar-elems'></div>";
1727+
themes.push(PathBuf::from("settings.css"));
1728+
let mut layout = self.shared.layout.clone();
1729+
layout.krate = String::new();
1730+
layout.logo = String::new();
1731+
layout.favicon = String::new();
1732+
try_err!(layout::render(&mut w, &layout,
1733+
&page, &sidebar, &settings,
1734+
self.shared.css_file_extension.is_some(),
1735+
&themes),
1736+
&settings_file);
1737+
16651738
Ok(())
16661739
}
16671740

src/librustdoc/html/static/main.js

+11-15
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,13 @@
7777
return false;
7878
}
7979
var end = start + className.length;
80-
if (end < elemClass.length && elemClass[end] !== ' ') {
81-
return false;
82-
}
83-
return true;
80+
return !(end < elemClass.length && elemClass[end] !== ' ');
8481
}
8582
if (start > 0 && elemClass[start - 1] !== ' ') {
8683
return false;
8784
}
8885
var end = start + className.length;
89-
if (end < elemClass.length && elemClass[end] !== ' ') {
90-
return false;
91-
}
92-
return true;
86+
return !(end < elemClass.length && elemClass[end] !== ' ');
9387
}
9488
return false;
9589
}
@@ -320,7 +314,7 @@
320314
} else if (ev.target.tagName === 'SPAN' && hasClass(ev.target.parentNode, 'line-numbers')) {
321315
var prev_id = 0;
322316

323-
var set_fragment = function (name) {
317+
var set_fragment = function(name) {
324318
if (browserSupportsHistoryApi()) {
325319
history.replaceState(null, null, '#' + name);
326320
window.hashchange();
@@ -835,7 +829,7 @@
835829
query.search = val;
836830
// searching by type
837831
} else if (val.search("->") > -1) {
838-
var trimmer = function (s) { return s.trim(); };
832+
var trimmer = function(s) { return s.trim(); };
839833
var parts = val.split("->").map(trimmer);
840834
var input = parts[0];
841835
// sort inputs so that order does not matter
@@ -1559,7 +1553,7 @@
15591553
startSearch();
15601554

15611555
// Draw a convenient sidebar of known crates if we have a listing
1562-
if (rootPath === '../') {
1556+
if (rootPath === '../' || rootPath === "./") {
15631557
var sidebar = document.getElementsByClassName('sidebar-elems')[0];
15641558
if (sidebar) {
15651559
var div = document.createElement('div');
@@ -1578,11 +1572,11 @@
15781572
crates.sort();
15791573
for (var i = 0; i < crates.length; ++i) {
15801574
var klass = 'crate';
1581-
if (crates[i] === window.currentCrate) {
1575+
if (rootPath !== "./" && crates[i] === window.currentCrate) {
15821576
klass += ' current';
15831577
}
15841578
var link = document.createElement('a');
1585-
link.href = '../' + crates[i] + '/index.html';
1579+
link.href = rootPath + crates[i] + '/index.html';
15861580
link.title = rawSearchIndex[crates[i]].doc;
15871581
link.className = klass;
15881582
link.textContent = crates[i];
@@ -1959,7 +1953,7 @@
19591953
otherMessage = '&nbsp;Show&nbsp;type&nbsp;declaration';
19601954
}
19611955
e.parentNode.insertBefore(createToggle(otherMessage), e);
1962-
if (otherMessage) {
1956+
if (otherMessage && getCurrentValue('rustdoc-item-declarations') !== "false") {
19631957
collapseDocs(e.previousSibling.childNodes[0], "toggle");
19641958
}
19651959
}
@@ -2029,7 +2023,9 @@
20292023
onEach(document.getElementById('main').getElementsByTagName('pre'), function(e) {
20302024
onEach(e.getElementsByClassName('attributes'), function(i_e) {
20312025
i_e.parentNode.insertBefore(createToggleWrapper(), i_e);
2032-
collapseDocs(i_e.previousSibling.childNodes[0], "toggle");
2026+
if (getCurrentValue("rustdoc-item-attributes") !== "false") {
2027+
collapseDocs(i_e.previousSibling.childNodes[0], "toggle");
2028+
}
20332029
});
20342030
});
20352031

src/librustdoc/html/static/rustdoc.css

+17-2
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,16 @@ a {
548548

549549
.block a.current.crate { font-weight: 500; }
550550

551+
.search-container {
552+
position: relative;
553+
}
554+
.search-container > .top-button {
555+
position: absolute;
556+
right: 0;
557+
top: 10px;
558+
}
551559
.search-input {
552-
width: 100%;
560+
width: calc(100% - 34px);
553561
/* Override Normalize.css: we have margins and do
554562
not want to overflow - the `moz` attribute is necessary
555563
until Firefox 29, too early to drop at this point */
@@ -1224,7 +1232,14 @@ kbd {
12241232
outline: none;
12251233
}
12261234

1227-
#theme-picker {
1235+
#settings-menu {
1236+
position: absolute;
1237+
right: 0;
1238+
top: 10px;
1239+
outline: none;
1240+
}
1241+
1242+
#theme-picker, #settings-menu {
12281243
padding: 4px;
12291244
width: 27px;
12301245
height: 29px;
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2018 The Rust Project Developers. See the COPYRIGHT
3+
* file at the top-level directory of this distribution and at
4+
* http://rust-lang.org/COPYRIGHT.
5+
*
6+
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
* option. This file may not be copied, modified, or distributed
10+
* except according to those terms.
11+
*/
12+
13+
.setting-line {
14+
padding: 5px;
15+
}
16+
17+
.setting-line > div {
18+
max-width: calc(100% - 74px);
19+
display: inline-block;
20+
vertical-align: top;
21+
font-size: 17px;
22+
padding-top: 2px;
23+
}
24+
25+
.toggle {
26+
position: relative;
27+
display: inline-block;
28+
width: 45px;
29+
height: 27px;
30+
margin-right: 20px;
31+
}
32+
33+
.toggle input {
34+
display: none;
35+
}
36+
37+
.slider {
38+
position: absolute;
39+
cursor: pointer;
40+
top: 0;
41+
left: 0;
42+
right: 0;
43+
bottom: 0;
44+
background-color: #ccc;
45+
-webkit-transition: .3s;
46+
transition: .3s;
47+
}
48+
49+
.slider:before {
50+
position: absolute;
51+
content: "";
52+
height: 19px;
53+
width: 19px;
54+
left: 4px;
55+
bottom: 4px;
56+
background-color: white;
57+
-webkit-transition: .3s;
58+
transition: .3s;
59+
}
60+
61+
input:checked + .slider {
62+
background-color: #2196F3;
63+
}
64+
65+
input:focus + .slider {
66+
box-shadow: 0 0 1px #2196F3;
67+
}
68+
69+
input:checked + .slider:before {
70+
-webkit-transform: translateX(19px);
71+
-ms-transform: translateX(19px);
72+
transform: translateX(19px);
73+
}
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*!
2+
* Copyright 2018 The Rust Project Developers. See the COPYRIGHT
3+
* file at the top-level directory of this distribution and at
4+
* http://rust-lang.org/COPYRIGHT.
5+
*
6+
* Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
* http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
* <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
* option. This file may not be copied, modified, or distributed
10+
* except according to those terms.
11+
*/
12+
13+
(function () {
14+
function changeSetting(settingName, isEnabled) {
15+
updateLocalStorage('rustdoc-' + settingName, isEnabled);
16+
}
17+
18+
function getSettingValue(settingName) {
19+
return getCurrentValue('rustdoc-' + settingName);
20+
}
21+
22+
function setEvents() {
23+
var elems = document.getElementsByClassName("slider");
24+
if (!elems || elems.length === 0) {
25+
return;
26+
}
27+
for (var i = 0; i < elems.length; ++i) {
28+
var toggle = elems[i].previousElementSibling;
29+
var settingId = toggle.id;
30+
var settingValue = getSettingValue(settingId);
31+
if (settingValue !== null) {
32+
toggle.checked = settingValue === "true";
33+
}
34+
toggle.onchange = function() {
35+
changeSetting(this.id, this.checked);
36+
};
37+
}
38+
}
39+
40+
setEvents();
41+
})();

0 commit comments

Comments
 (0)