From f2ad3c3f6c1c8c2220127e3d38bcf095533a0e47 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 13 Apr 2018 22:54:09 +0200 Subject: [PATCH] Add rustdoc settings menu --- src/librustdoc/html/layout.rs | 8 ++- src/librustdoc/html/render.rs | 77 ++++++++++++++++++++- src/librustdoc/html/static/main.js | 26 +++---- src/librustdoc/html/static/rustdoc.css | 19 ++++- src/librustdoc/html/static/settings.css | 73 +++++++++++++++++++ src/librustdoc/html/static/settings.js | 50 +++++++++++++ src/librustdoc/html/static/themes/dark.css | 5 +- src/librustdoc/html/static/themes/light.css | 5 +- src/librustdoc/html/static/wheel.svg | 1 + 9 files changed, 238 insertions(+), 26 deletions(-) create mode 100644 src/librustdoc/html/static/settings.css create mode 100644 src/librustdoc/html/static/settings.js create mode 100644 src/librustdoc/html/static/wheel.svg diff --git a/src/librustdoc/html/layout.rs b/src/librustdoc/html/layout.rs index 583c9f2b67144..8e3989d33c13f 100644 --- a/src/librustdoc/html/layout.rs +++ b/src/librustdoc/html/layout.rs @@ -85,6 +85,9 @@ pub fn render( autocomplete=\"off\" \ placeholder=\"Click or press ‘S’ to search, ‘?’ for more options…\" \ type=\"search\">\ + \ + \"Change\ + \ \ \ \ @@ -181,9 +184,10 @@ pub fn render( themes = themes.iter() .filter_map(|t| t.file_stem()) .filter_map(|t| t.to_str()) - .map(|t| format!(r#""#, + .map(|t| format!(r#""#, page.root_path, - t.replace(".css", &format!("{}.css", page.resource_suffix)))) + t, + page.resource_suffix)) .collect::(), suffix=page.resource_suffix, ) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 651319743aadd..7a1ddd9fd2411 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::>() @@ -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, +"

\ + Rustdoc settings\ +

\ +
{}
\ +", + self.settings.iter() + .map(|(id, text, enabled)| { + format!("
\ + \ +
{}
\ +
", id, if *enabled { " checked" } else { "" }, text) + }) + .collect::(), + 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 = "

Settings