diff options
| author | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2018-04-13 22:54:09 +0200 |
|---|---|---|
| committer | Guillaume Gomez <guillaume1.gomez@gmail.com> | 2018-04-17 23:35:43 +0200 |
| commit | f2ad3c3f6c1c8c2220127e3d38bcf095533a0e47 (patch) | |
| tree | 8cf0210b7f728961dd44e28847a385e4c11617ce | |
| parent | 9379bcdd0b4ef06303e8c4e3e7c3fd79882372cc (diff) | |
| download | rust-f2ad3c3f6c1c8c2220127e3d38bcf095533a0e47.tar.gz rust-f2ad3c3f6c1c8c2220127e3d38bcf095533a0e47.zip | |
Add rustdoc settings menu
| -rw-r--r-- | src/librustdoc/html/layout.rs | 8 | ||||
| -rw-r--r-- | src/librustdoc/html/render.rs | 77 | ||||
| -rw-r--r-- | src/librustdoc/html/static/main.js | 26 | ||||
| -rw-r--r-- | src/librustdoc/html/static/rustdoc.css | 19 | ||||
| -rw-r--r-- | src/librustdoc/html/static/settings.css | 73 | ||||
| -rw-r--r-- | src/librustdoc/html/static/settings.js | 50 | ||||
| -rw-r--r-- | src/librustdoc/html/static/themes/dark.css | 5 | ||||
| -rw-r--r-- | src/librustdoc/html/static/themes/light.css | 5 | ||||
| -rw-r--r-- | src/librustdoc/html/static/wheel.svg | 1 |
9 files changed, 238 insertions, 26 deletions
diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 583c9f2b671..8e3989d33c1 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -85,6 +85,9 @@ pub fn render<T: fmt::Display, S: fmt::Display>( autocomplete=\"off\" \ placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \ type=\"search\">\ + <a id=\"settings-menu\" href=\"{root_path}settings.html\">\ + <img src=\"{root_path}wheel{suffix}.svg\" width=\"18\" alt=\"Change settings\">\ + </a>\ </div>\ </form>\ </nav>\ @@ -181,9 +184,10 @@ pub fn render<T: fmt::Display, S: fmt::Display>( themes = themes.iter() .filter_map(|t| t.file_stem()) .filter_map(|t| t.to_str()) - .map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}">"#, + .map(|t| format!(r#"<link rel="stylesheet" type="text/css" href="{}{}{}.css">"#, page.root_path, - t.replace(".css", &format!("{}.css", page.resource_suffix)))) + t, + page.resource_suffix)) .collect::<String>(), suffix=page.resource_suffix, ) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 651319743aa..7a1ddd9fd24 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -742,6 +742,8 @@ fn write_shared(cx: &Context, write(cx.dst.join(&format!("rustdoc{}.css", cx.shared.resource_suffix)), include_bytes!("static/rustdoc.css"))?; + write(cx.dst.join(&format!("settings{}.css", cx.shared.resource_suffix)), + include_bytes!("static/settings.css"))?; // To avoid "light.css" to be overwritten, we'll first run over the received themes and only // then we'll run over the "official" styles. @@ -761,6 +763,8 @@ fn write_shared(cx: &Context, write(cx.dst.join(&format!("brush{}.svg", cx.shared.resource_suffix)), include_bytes!("static/brush.svg"))?; + write(cx.dst.join(&format!("wheel{}.svg", cx.shared.resource_suffix)), + include_bytes!("static/wheel.svg"))?; write(cx.dst.join(&format!("light{}.css", cx.shared.resource_suffix)), include_bytes!("static/themes/light.css"))?; themes.insert("light".to_owned()); @@ -794,8 +798,7 @@ themePicker.onclick = function() {{ switchTheme(currentTheme, mainTheme, item); }}; themes.appendChild(but); -}}); -"#, +}});"#, themes.iter() .map(|s| format!("\"{}\"", s)) .collect::<Vec<String>>() @@ -804,6 +807,8 @@ themePicker.onclick = function() {{ write(cx.dst.join(&format!("main{}.js", cx.shared.resource_suffix)), include_bytes!("static/main.js"))?; + write(cx.dst.join(&format!("settings{}.js", cx.shared.resource_suffix)), + include_bytes!("static/settings.js"))?; { let mut data = format!("var resourcesSuffix = \"{}\";\n", @@ -1503,6 +1508,51 @@ impl fmt::Display for AllTypes { } } +#[derive(Debug)] +struct Settings<'a> { + // (id, explanation, default value) + settings: Vec<(&'static str, &'static str, bool)>, + 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), + ], + root_path, + suffix, + } + } +} + +impl<'a> fmt::Display for Settings<'a> { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, +"<h1 class='fqn'>\ + <span class='in-band'>Rustdoc settings</span>\ +</h1>\ +<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) + }) + .collect::<String>(), + self.root_path, + self.suffix) + } +} + impl Context { /// String representation of how to get back to the root path of the 'doc/' /// folder in terms of a relative URL. @@ -1546,6 +1596,8 @@ impl Context { }; let final_file = self.dst.join(&krate.name) .join("all.html"); + let settings_file = self.dst.join("settings.html"); + let crate_name = krate.name.clone(); item.name = Some(krate.name); @@ -1567,7 +1619,7 @@ impl Context { if !root_path.ends_with('/') { root_path.push('/'); } - let page = layout::Page { + let mut page = layout::Page { title: "List of all items in this crate", css_class: "mod", root_path: "../", @@ -1590,6 +1642,25 @@ impl Context { self.shared.css_file_extension.is_some(), &self.shared.themes), &final_file); + + // If the file already exists, no need to generate it again... + if !settings_file.is_file() { + let settings = Settings::new("./", &self.shared.resource_suffix); + page.title = "Rustdoc settings"; + page.description = "Settings of Rustdoc"; + page.root_path = "./"; + + let mut w = BufWriter::new(try_err!(File::create(&settings_file), &settings_file)); + let mut themes = self.shared.themes.clone(); + let sidebar = "<p class='location'>Settings</p><div class='sidebar-elems'>".to_owned(); + themes.push(PathBuf::from("settings.css")); + try_err!(layout::render(&mut w, &self.shared.layout, + &page, &sidebar, &settings, + self.shared.css_file_extension.is_some(), + &themes), + &settings_file); + } + Ok(()) } diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 2546a9410a9..1b47ec0ec02 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -77,19 +77,13 @@ return false; } var end = start + className.length; - if (end < elemClass.length && elemClass[end] !== ' ') { - return false; - } - return true; + return !(end < elemClass.length && elemClass[end] !== ' '); } if (start > 0 && elemClass[start - 1] !== ' ') { return false; } var end = start + className.length; - if (end < elemClass.length && elemClass[end] !== ' ') { - return false; - } - return true; + return !(end < elemClass.length && elemClass[end] !== ' '); } return false; } @@ -320,7 +314,7 @@ } else if (ev.target.tagName === 'SPAN' && hasClass(ev.target.parentNode, 'line-numbers')) { var prev_id = 0; - var set_fragment = function (name) { + var set_fragment = function(name) { if (browserSupportsHistoryApi()) { history.replaceState(null, null, '#' + name); window.hashchange(); @@ -835,7 +829,7 @@ query.search = val; // searching by type } else if (val.search("->") > -1) { - var trimmer = function (s) { return s.trim(); }; + var trimmer = function(s) { return s.trim(); }; var parts = val.split("->").map(trimmer); var input = parts[0]; // sort inputs so that order does not matter @@ -1547,7 +1541,7 @@ startSearch(); // Draw a convenient sidebar of known crates if we have a listing - if (rootPath === '../') { + if (rootPath === '../' || rootPath === "./") { var sidebar = document.getElementsByClassName('sidebar-elems')[0]; if (sidebar) { var div = document.createElement('div'); @@ -1566,11 +1560,11 @@ crates.sort(); for (var i = 0; i < crates.length; ++i) { var klass = 'crate'; - if (crates[i] === window.currentCrate) { + if (rootPath !== "./" && crates[i] === window.currentCrate) { klass += ' current'; } var link = document.createElement('a'); - link.href = '../' + crates[i] + '/index.html'; + link.href = rootPath + crates[i] + '/index.html'; link.title = rawSearchIndex[crates[i]].doc; link.className = klass; link.textContent = crates[i]; @@ -1947,7 +1941,7 @@ otherMessage = ' Show type declaration'; } e.parentNode.insertBefore(createToggle(otherMessage), e); - if (otherMessage) { + if (otherMessage && getCurrentValue('rustdoc-item-declarations') !== "false") { collapseDocs(e.previousSibling.childNodes[0], "toggle"); } } @@ -2017,7 +2011,9 @@ onEach(document.getElementById('main').getElementsByTagName('pre'), function(e) { onEach(e.getElementsByClassName('attributes'), function(i_e) { i_e.parentNode.insertBefore(createToggleWrapper(), i_e); - collapseDocs(i_e.previousSibling.childNodes[0], "toggle"); + if (getCurrentValue("rustdoc-item-attributes") !== "false") { + collapseDocs(i_e.previousSibling.childNodes[0], "toggle"); + } }); }); diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index 4c6c8dcfdda..d6b3ab26af8 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -548,8 +548,16 @@ a { .block a.current.crate { font-weight: 500; } +.search-container { + position: relative; +} +.search-container > .top-button { + position: absolute; + right: 0; + top: 10px; +} .search-input { - width: 100%; + width: calc(100% - 34px); /* Override Normalize.css: we have margins and do not want to overflow - the `moz` attribute is necessary until Firefox 29, too early to drop at this point */ @@ -1224,7 +1232,14 @@ kbd { outline: none; } -#theme-picker { +#settings-menu { + position: absolute; + right: 0; + top: 10px; + outline: none; +} + +#theme-picker, #settings-menu { padding: 4px; width: 27px; height: 29px; diff --git a/src/librustdoc/html/static/settings.css b/src/librustdoc/html/static/settings.css new file mode 100644 index 00000000000..34835f3f22d --- /dev/null +++ b/src/librustdoc/html/static/settings.css @@ -0,0 +1,73 @@ +/** + * Copyright 2018 The Rust Project Developers. See the COPYRIGHT + * file at the top-level directory of this distribution and at + * http://rust-lang.org/COPYRIGHT. + * + * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or + * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license + * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your + * option. This file may not be copied, modified, or distributed + * except according to those terms. + */ + +.setting-line { + padding: 5px; +} + +.setting-line > div { + max-width: calc(100% - 74px); + display: inline-block; + vertical-align: top; + font-size: 17px; + padding-top: 2px; +} + +.toggle { + position: relative; + display: inline-block; + width: 45px; + height: 27px; + margin-right: 20px; +} + +.toggle input { + display: none; +} + +.slider { + position: absolute; + cursor: pointer; + top: 0; + left: 0; + right: 0; + bottom: 0; + background-color: #ccc; + -webkit-transition: .3s; + transition: .3s; +} + +.slider:before { + position: absolute; + content: ""; + height: 19px; + width: 19px; + left: 4px; + bottom: 4px; + background-color: white; + -webkit-transition: .3s; + transition: .3s; +} + +input:checked + .slider { + background-color: #2196F3; +} + +input:focus + .slider { + box-shadow: 0 0 1px #2196F3; +} + +input:checked + .slider:before { + -webkit-transform: translateX(19px); + -ms-transform: translateX(19px); + transform: translateX(19px); +} \ No newline at end of file diff --git a/src/librustdoc/html/static/settings.js b/src/librustdoc/html/static/settings.js new file mode 100644 index 00000000000..ac7bc610520 --- /dev/null +++ b/src/librustdoc/html/static/settings.js @@ -0,0 +1,50 @@ +/*! + * Copyright 2018 The Rust Project Developers. See the COPYRIGHT + * file at the top-level directory of this distribution and at + * http://rust-lang.org/COPYRIGHT. + * + * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or + * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license + * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your + * option. This file may not be copied, modified, or distributed + * except according to those terms. + */ + +(function () { + function changeSetting(settingName, isEnabled) { + updateLocalStorage('rustdoc-' + settingName, isEnabled); + } + + function getSettingValue(settingName) { + return getCurrentValue('rustdoc-' + settingName); + } + + function replaceLogoHref() { + var elem = document.getElementsByClassName("sidebar-menu"); + if (elem && elem.length > 0) { + elem[0].nextElementSibling.setAttribute('href', 'javascript:void'); + } + } + + function setEvents() { + var elems = document.getElementsByClassName("slider"); + if (!elems || elems.length === 0) { + return; + } + for (var 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"; + } + toggle.onchange = function() { + changeSetting(this.id, this.checked); + }; + } + } + + // To remove possibility to come back to "something" by clicking on the logo. + replaceLogoHref(); + setEvents(); +})(); diff --git a/src/librustdoc/html/static/themes/dark.css b/src/librustdoc/html/static/themes/dark.css index 93971a205bf..da4be7db5aa 100644 --- a/src/librustdoc/html/static/themes/dark.css +++ b/src/librustdoc/html/static/themes/dark.css @@ -362,12 +362,13 @@ kbd { box-shadow-color: #c6cbd1; } -#theme-picker { +#theme-picker, #settings-menu { border-color: #e0e0e0; background: #f0f0f0; } -#theme-picker:hover, #theme-picker:focus { +#theme-picker:hover, #theme-picker:focus, +#settings-menu:hover, #settings-menu:focus { border-color: #ffb900; } diff --git a/src/librustdoc/html/static/themes/light.css b/src/librustdoc/html/static/themes/light.css index e13818b4bd2..12af01d2e24 100644 --- a/src/librustdoc/html/static/themes/light.css +++ b/src/librustdoc/html/static/themes/light.css @@ -356,12 +356,13 @@ kbd { box-shadow-color: #c6cbd1; } -#theme-picker { +#theme-picker, #settings-menu { border-color: #e0e0e0; background-color: #fff; } -#theme-picker:hover, #theme-picker:focus { +#theme-picker:hover, #theme-picker:focus, +#settings-menu:hover, #settings-menu:focus { border-color: #717171; } diff --git a/src/librustdoc/html/static/wheel.svg b/src/librustdoc/html/static/wheel.svg new file mode 100644 index 00000000000..44381a401a8 --- /dev/null +++ b/src/librustdoc/html/static/wheel.svg @@ -0,0 +1 @@ +<?xml version="1.0" ?><!DOCTYPE svg PUBLIC '-//W3C//DTD SVG 1.1//EN' 'http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd'><svg enable-background="new 0 0 27.434 29.5" height="29.5px" id="Capa_1" version="1.1" viewBox="0 0 27.434 29.5" width="27.434px" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"><g><path d="M27.315,18.389c-0.165-0.604-0.509-1.113-0.981-1.459c-0.042-0.144-0.083-0.429-0.015-0.761l0.037-0.177v-0.182V14.8 c0-1.247-0.006-1.277-0.048-1.472c-0.076-0.354-0.035-0.653,0.007-0.803c0.477-0.346,0.828-0.861,0.996-1.476 c0.261-0.956,0.076-2.091-0.508-3.114l-0.591-1.032c-0.746-1.307-1.965-2.119-3.182-2.119c-0.378,0-0.75,0.081-1.085,0.235 c-0.198-0.025-0.554-0.15-0.855-0.389l-0.103-0.082l-0.114-0.065l-1.857-1.067L18.92,3.36l-0.105-0.044 c-0.376-0.154-0.658-0.41-0.768-0.556C17.918,1.172,16.349,0,14.296,0H13.14c-2.043,0-3.608,1.154-3.749,2.721 C9.277,2.862,8.999,3.104,8.633,3.25l-0.1,0.039L8.439,3.341L6.495,4.406L6.363,4.479L6.245,4.573 C5.936,4.82,5.596,4.944,5.416,4.977c-0.314-0.139-0.66-0.21-1.011-0.21c-1.198,0-2.411,0.819-3.165,2.139L0.65,7.938 c-0.412,0.72-0.642,1.521-0.644,2.258c-0.003,0.952,0.362,1.756,1.013,2.256c0.034,0.155,0.061,0.448-0.016,0.786 c-0.038,0.168-0.062,0.28-0.062,1.563c0,1.148,0,1.148,0.015,1.262l0.009,0.073l0.017,0.073c0.073,0.346,0.045,0.643,0.011,0.802 C0.348,17.512-0.01,18.314,0,19.268c0.008,0.729,0.238,1.523,0.648,2.242l0.589,1.031c0.761,1.331,1.967,2.159,3.15,2.159 c0.324,0,0.645-0.064,0.938-0.187c0.167,0.038,0.492,0.156,0.813,0.416l0.11,0.088l0.124,0.07l2.045,1.156l0.102,0.057l0.107,0.043 c0.364,0.147,0.646,0.381,0.766,0.521c0.164,1.52,1.719,2.634,3.745,2.634h1.155c2.037,0,3.598-1.134,3.747-2.675 c0.117-0.145,0.401-0.393,0.774-0.549l0.111-0.047l0.105-0.062l1.96-1.159l0.105-0.062l0.097-0.075 c0.309-0.246,0.651-0.371,0.832-0.402c0.313,0.138,0.662,0.212,1.016,0.212c1.199,0,2.412-0.82,3.166-2.139l0.59-1.032 C27.387,20.48,27.575,19.342,27.315,18.389z M25.274,20.635l-0.59,1.032c-0.438,0.765-1.104,1.251-1.639,1.251 c-0.133,0-0.258-0.029-0.369-0.094c-0.15-0.086-0.346-0.127-0.566-0.127c-0.596,0-1.383,0.295-2.01,0.796l-1.96,1.157 c-1.016,0.425-1.846,1.291-1.846,1.929s-0.898,1.159-1.998,1.159H13.14c-1.1,0-1.998-0.514-1.998-1.141s-0.834-1.477-1.854-1.888 l-2.046-1.157c-0.636-0.511-1.425-0.814-2.006-0.814c-0.202,0-0.379,0.037-0.516,0.115c-0.101,0.057-0.214,0.084-0.333,0.084 c-0.518,0-1.179-0.498-1.62-1.271l-0.591-1.032c-0.545-0.954-0.556-1.983-0.024-2.286c0.532-0.305,0.78-1.432,0.551-2.506 c0,0,0-0.003,0-1.042c0-1.088,0.021-1.18,0.021-1.18c0.238-1.072-0.01-2.203-0.552-2.513C1.631,10.8,1.634,9.765,2.18,8.812 L2.769,7.78c0.438-0.766,1.103-1.251,1.636-1.251c0.131,0,0.255,0.029,0.365,0.092C4.92,6.707,5.114,6.747,5.334,6.747 c0.596,0,1.38-0.296,2.007-0.795l1.944-1.065c1.021-0.407,1.856-1.277,1.856-1.933c0-0.656,0.898-1.192,1.998-1.192h1.156V1.761 c1.1,0,1.998,0.545,1.998,1.211c0,0.667,0.832,1.554,1.849,1.973L20,6.013c0.618,0.489,1.401,0.775,2.012,0.775 c0.24,0,0.454-0.045,0.62-0.139c0.122-0.069,0.259-0.102,0.403-0.102c0.551,0,1.221,0.476,1.653,1.231l0.59,1.032 c0.544,0.953,0.518,2.004-0.062,2.334c-0.577,0.331-0.859,1.48-0.627,2.554c0,0,0.01,0.042,0.01,1.103c0,1.012,0,1.012,0,1.012 c-0.218,1.049,0.068,2.174,0.636,2.498C25.802,18.635,25.819,19.68,25.274,20.635z"/><path d="M13.61,7.611c-3.913,0-7.084,3.173-7.084,7.085c0,3.914,3.171,7.085,7.084,7.085s7.085-3.172,7.085-7.085 C20.695,10.784,17.523,7.611,13.61,7.611z M13.61,20.02c-2.936,0-5.323-2.388-5.323-5.323c0-2.935,2.388-5.323,5.323-5.323 s5.324,2.388,5.324,5.323C18.934,17.632,16.546,20.02,13.61,20.02z"/><path d="M13.682,9.908c-2.602,0-4.718,2.116-4.718,4.718c0,2.601,2.116,4.716,4.718,4.716c2.601,0,4.717-2.115,4.717-4.716 C18.399,12.024,16.283,9.908,13.682,9.908z M13.682,17.581c-1.633,0-2.956-1.323-2.956-2.955s1.323-2.956,2.956-2.956 c1.632,0,2.956,1.324,2.956,2.956S15.314,17.581,13.682,17.581z"/></g></svg> \ No newline at end of file |
