Skip to content

[rustdoc] Add sub settings #60778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 79 additions & 21 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1950,30 +1950,96 @@ impl fmt::Display for AllTypes {
}
}

#[derive(Debug)]
struct Setting {
js_data_name: &'static str,
description: &'static str,
default_value: bool,
sub_settings: Vec<Setting>,
}

impl From<(&'static str, &'static str, bool)> for Setting {
fn from(values: (&'static str, &'static str, bool)) -> Setting {
Setting {
js_data_name: values.0,
description: values.1,
default_value: values.2,
sub_settings: Vec::new(),
}
}
}

impl<T: Into<Setting>> From<(&'static str, &'static str, bool, Vec<T>)> for Setting {
fn from(values: (&'static str, &'static str, bool, Vec<T>)) -> Setting {
Setting {
js_data_name: values.0,
description: values.1,
default_value: values.2,
sub_settings: values.3.into_iter().map(|v| v.into()).collect::<Vec<_>>(),
}
}
}

impl<'a, T: Into<Setting>> From<(Vec<T>, &'a str, &'a str)> for Settings<'a> {
fn from(v: (Vec<T>, &'a str, &'a str)) -> Settings<'a> {
let (settings, root_path, suffix) = v;
Settings {
settings: settings.into_iter().map(|v| v.into()).collect::<Vec<_>>(),
root_path,
suffix,
}
}
}

impl fmt::Display for Setting {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<div class='setting-line'>\
<label class='toggle'>\
<input type='checkbox' id='{}'{}>\
<span class='slider'></span>\
</label>\
<div>{}</div>{}\
</div>",
self.js_data_name,
if self.default_value { " checked" } else { "" },
self.description,
if self.sub_settings.is_empty() {
String::new()
} else {
format!("<div class='sub-setting'>{}</div>",
self.sub_settings.iter().map(|s| s.to_string()).collect::<String>())
})
}
}

#[derive(Debug)]
struct Settings<'a> {
// (id, explanation, default value)
settings: Vec<(&'static str, &'static str, bool)>,
settings: Vec<Setting>,
root_path: &'a str,
suffix: &'a str,
}

impl<'a> Settings<'a> {
pub fn new(root_path: &'a str, suffix: &'a str) -> Settings<'a> {
Settings {
settings: vec![
("item-declarations", "Auto-hide item declarations.", true),
("item-attributes", "Auto-hide item attributes.", true),
("trait-implementations", "Auto-hide trait implementations documentation",
true),
("method-docs", "Auto-hide item methods' documentation", false),
Settings::from((vec![
("auto-hide-declarations", "Auto-hide item declarations", true, vec![
("auto-hide-struct", "Auto-hide structs declaration", true),
("auto-hide-enum", "Auto-hide enums declaration", false),
("auto-hide-union", "Auto-hide unions declaration", true),
("auto-hide-trait", "Auto-hide traits declaration", true),
("auto-hide-macro", "Auto-hide macros declaration", false),
]),
("auto-hide-attributes", "Auto-hide item attributes", true, vec![]),
("auto-hide-trait-implementations", "Auto-hide trait implementations documentation",
true, vec![]),
("auto-hide-method-docs", "Auto-hide item methods' documentation", false, vec![]),
("go-to-only-result", "Directly go to item in search if there is only one result",
false),
("line-numbers", "Show line numbers on code examples", false),
false, vec![]),
("line-numbers", "Show line numbers on code examples", false, vec![]),
],
root_path,
suffix,
}
))
}
}

Expand All @@ -1986,15 +2052,7 @@ impl<'a> fmt::Display for Settings<'a> {
<div class='settings'>{}</div>\
<script src='{}settings{}.js'></script>",
self.settings.iter()
.map(|(id, text, enabled)| {
format!("<div class='setting-line'>\
<label class='toggle'>\
<input type='checkbox' id='{}' {}>\
<span class='slider'></span>\
</label>\
<div>{}</div>\
</div>", id, if *enabled { " checked" } else { "" }, text)
})
.map(|s| s.to_string())
.collect::<String>(),
self.root_path,
self.suffix)
Expand Down
32 changes: 28 additions & 4 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2067,7 +2067,7 @@ if (!DOMTokenList.prototype.remove) {
function autoCollapse(pageId, collapse) {
if (collapse) {
toggleAllDocs(pageId, true);
} else if (getCurrentValue("rustdoc-trait-implementations") !== "false") {
} else if (getCurrentValue("rustdoc-auto-hide-trait-implementations") !== "false") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware that by changing the local storage keys that we look up, we're creating a situation on docs.rs (and sites like it that host docs made by multiple versions of rustdoc) where a setting made on one set of docs won't affect the other. Since we're adding a bunch more at the same time, it might be an understandable/okay change, but i'm not sure it's totally worthwhile.

Another problem with renaming existing local storage keys is that if someone had changed the setting, now they will be stuck with the default again and will need to go back and change. Should we try to have a kind of compatibility check that loads in a value from the previous key if the new one isn't set?

var impl_list = document.getElementById("implementations-list");

if (impl_list !== null) {
Expand Down Expand Up @@ -2105,7 +2105,7 @@ if (!DOMTokenList.prototype.remove) {
}

var toggle = createSimpleToggle(false);
var hideMethodDocs = getCurrentValue("rustdoc-method-docs") === "true";
var hideMethodDocs = getCurrentValue("rustdoc-auto-hide-method-docs") === "true";
var pageId = getPageId();

var func = function(e) {
Expand Down Expand Up @@ -2235,7 +2235,31 @@ if (!DOMTokenList.prototype.remove) {
return wrapper;
}

var showItemDeclarations = getCurrentValue("rustdoc-item-declarations") === "false";
var currentType = document.getElementsByClassName("type-decl")[0];
var className = null;
if (currentType) {
currentType = currentType.getElementsByClassName("rust")[0];
if (currentType) {
currentType.classList.forEach(function(item) {
if (item !== "main") {
className = item;
return true;
}
});
}
}
var showItemDeclarations = getCurrentValue("rustdoc-auto-hide-" + className);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh, now i see why changing the names of the keys could be worthwhile. This might negate the previous comment.

if (showItemDeclarations === null) {
if (className === "enum" || className === "macro") {
showItemDeclarations = "false";
} else if (className === "struct" || className === "union" || className === "trait") {
showItemDeclarations = "true";
} else {
// In case we found an unknown type, we just use the "parent" value.
showItemDeclarations = getCurrentValue("rustdoc-auto-hide-declarations");
}
}
showItemDeclarations = showItemDeclarations === "false";
function buildToggleWrapper(e) {
if (hasClass(e, "autohide")) {
var wrap = e.previousElementSibling;
Expand Down Expand Up @@ -2318,7 +2342,7 @@ if (!DOMTokenList.prototype.remove) {

// To avoid checking on "rustdoc-item-attributes" value on every loop...
var itemAttributesFunc = function() {};
if (getCurrentValue("rustdoc-item-attributes") !== "false") {
if (getCurrentValue("rustdoc-auto-hide-attributes") !== "false") {
itemAttributesFunc = function(x) {
collapseDocs(x.previousSibling.childNodes[0], "toggle");
};
Expand Down
6 changes: 6 additions & 0 deletions src/librustdoc/html/static/settings.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ input:checked + .slider:before {
-ms-transform: translateX(19px);
transform: translateX(19px);
}

.setting-line > .sub-setting {
padding-left: 42px;
width: 100%;
display: block;
}
35 changes: 34 additions & 1 deletion src/librustdoc/html/static/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,35 @@
updateLocalStorage('rustdoc-' + settingName, isEnabled);
}

function updateChildren(elem) {
var state = elem.checked;

var parentLabel = elem.parentElement;
if (!parentLabel) {
return;
}
var parentSettingLine = parentLabel.parentElement;
if (!parentSettingLine) {
return;
}
var elems = parentSettingLine.getElementsByClassName("sub-setting")[0];
if (!elems) {
return;
}

var toggles = elems.getElementsByTagName("input");
if (!toggles || toggles.length < 1) {
return;
}
var ev = new Event("change");
for (var x = 0; x < toggles.length; ++x) {
if (toggles[x].checked !== state) {
toggles[x].checked = state;
toggles[x].dispatchEvent(ev);
}
}
}

function getSettingValue(settingName) {
return getCurrentValue('rustdoc-' + settingName);
}
Expand All @@ -12,15 +41,19 @@
if (!elems || elems.length === 0) {
return;
}
for (var i = 0; i < elems.length; ++i) {
var i;
for (i = 0; i < elems.length; ++i) {
var toggle = elems[i].previousElementSibling;
var settingId = toggle.id;
var settingValue = getSettingValue(settingId);
if (settingValue !== null) {
toggle.checked = settingValue === "true";
}
}
for (i = 0; i < elems.length; ++i) {
toggle.onchange = function() {
changeSetting(this.id, this.checked);
updateChildren(this);
};
}
}
Expand Down