diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 472192a646492..a9ea39dce8dac 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -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, +} + +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> From<(&'static str, &'static str, bool, Vec)> for Setting { + fn from(values: (&'static str, &'static str, bool, Vec)) -> 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::>(), + } + } +} + +impl<'a, T: Into> From<(Vec, &'a str, &'a str)> for Settings<'a> { + fn from(v: (Vec, &'a str, &'a str)) -> Settings<'a> { + let (settings, root_path, suffix) = v; + Settings { + settings: settings.into_iter().map(|v| v.into()).collect::>(), + root_path, + suffix, + } + } +} + +impl fmt::Display for Setting { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "
\ + \ +
{}
{}\ +
", + self.js_data_name, + if self.default_value { " checked" } else { "" }, + self.description, + if self.sub_settings.is_empty() { + String::new() + } else { + format!("
{}
", + self.sub_settings.iter().map(|s| s.to_string()).collect::()) + }) + } +} + #[derive(Debug)] struct Settings<'a> { - // (id, explanation, default value) - settings: Vec<(&'static str, &'static str, bool)>, + settings: Vec, 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, - } + )) } } @@ -1986,15 +2052,7 @@ impl<'a> fmt::Display for Settings<'a> {
{}
\ ", self.settings.iter() - .map(|(id, text, enabled)| { - format!("
\ - \ -
{}
\ -
", id, if *enabled { " checked" } else { "" }, text) - }) + .map(|s| s.to_string()) .collect::(), self.root_path, self.suffix) diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 85ab0855f05e9..afeb69615d419 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -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") { var impl_list = document.getElementById("implementations-list"); if (impl_list !== null) { @@ -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) { @@ -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); + 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; @@ -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"); }; diff --git a/src/librustdoc/html/static/settings.css b/src/librustdoc/html/static/settings.css index b31ad96fa545f..e44a3e2981029 100644 --- a/src/librustdoc/html/static/settings.css +++ b/src/librustdoc/html/static/settings.css @@ -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; +} diff --git a/src/librustdoc/html/static/settings.js b/src/librustdoc/html/static/settings.js index c21db7371f3c3..313edacf930aa 100644 --- a/src/librustdoc/html/static/settings.js +++ b/src/librustdoc/html/static/settings.js @@ -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); } @@ -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); }; } }