Skip to content

Commit 27d7615

Browse files
authored
Rollup merge of #93097 - GuillaumeGomez:settings-js, r=jsha
Switch settings menu to full js Since the settings can only be set when the JS is enabled, it's not really a problem. It also fixes a debate we had around the themes not being accessible easily before. ![Screenshot from 2022-01-19 23-06-59](https://user-images.githubusercontent.com/3050060/150221936-fd1a1e76-06b6-4416-a653-dbae111979ed.png) You can test it [here](https://rustdoc.crud.net/imperio/settings-js/doc/foo/index.html). r? ``@jsha``
2 parents c110cfa + 73688e4 commit 27d7615

File tree

12 files changed

+439
-206
lines changed

12 files changed

+439
-206
lines changed

src/librustdoc/html/markdown.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,10 @@ fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
14401440
let mut map = FxHashMap::default();
14411441
// This is the list of IDs used in Javascript.
14421442
map.insert("help".into(), 1);
1443+
map.insert("settings".into(), 1);
1444+
map.insert("not-displayed".into(), 1);
1445+
map.insert("alternative-display".into(), 1);
1446+
map.insert("search".into(), 1);
14431447
// This is the list of IDs used in HTML generated in Rust (including the ones
14441448
// used in tera template files).
14451449
map.insert("mainThemeStyle".into(), 1);
@@ -1449,7 +1453,6 @@ fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
14491453
map.insert("settings-menu".into(), 1);
14501454
map.insert("help-button".into(), 1);
14511455
map.insert("main-content".into(), 1);
1452-
map.insert("search".into(), 1);
14531456
map.insert("crate-search".into(), 1);
14541457
map.insert("render-detail".into(), 1);
14551458
map.insert("toggle-all-docs".into(), 1);

src/librustdoc/html/render/context.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ use super::print_item::{full_path, item_path, print_item};
1717
use super::search_index::build_index;
1818
use super::write_shared::write_shared;
1919
use super::{
20-
collect_spans_and_sources, print_sidebar, scrape_examples_help, settings, AllTypes,
21-
LinkFromSrc, NameDoc, StylePath, BASIC_KEYWORDS,
20+
collect_spans_and_sources, print_sidebar, scrape_examples_help, AllTypes, LinkFromSrc, NameDoc,
21+
StylePath, BASIC_KEYWORDS,
2222
};
2323

2424
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
@@ -589,21 +589,18 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
589589
page.root_path = "./";
590590

591591
let sidebar = "<h2 class=\"location\">Settings</h2><div class=\"sidebar-elems\"></div>";
592-
let theme_names: Vec<String> = self
593-
.shared
594-
.style_files
595-
.iter()
596-
.map(StylePath::basename)
597-
.collect::<Result<_, Error>>()?;
598592
let v = layout::render(
599593
&self.shared.layout,
600594
&page,
601595
sidebar,
602-
settings(
603-
self.shared.static_root_path.as_deref().unwrap_or("./"),
604-
&self.shared.resource_suffix,
605-
theme_names,
606-
)?,
596+
|buf: &mut Buffer| {
597+
write!(
598+
buf,
599+
"<script defer src=\"{}settings{}.js\"></script>",
600+
page.static_root_path.unwrap_or(""),
601+
page.resource_suffix
602+
)
603+
},
607604
&self.shared.style_files,
608605
);
609606
self.shared.fs.write(settings_file, v)?;

src/librustdoc/html/render/mod.rs

-128
Original file line numberDiff line numberDiff line change
@@ -334,134 +334,6 @@ impl AllTypes {
334334
}
335335
}
336336

337-
#[derive(Debug)]
338-
enum Setting {
339-
Section {
340-
description: &'static str,
341-
sub_settings: Vec<Setting>,
342-
},
343-
Toggle {
344-
js_data_name: &'static str,
345-
description: &'static str,
346-
default_value: bool,
347-
},
348-
Select {
349-
js_data_name: &'static str,
350-
description: &'static str,
351-
default_value: &'static str,
352-
options: Vec<String>,
353-
},
354-
}
355-
356-
impl Setting {
357-
fn display(&self, root_path: &str, suffix: &str) -> String {
358-
match *self {
359-
Setting::Section { description, ref sub_settings } => format!(
360-
"<div class=\"setting-line\">\
361-
<div class=\"title\">{}</div>\
362-
<div class=\"sub-settings\">{}</div>
363-
</div>",
364-
description,
365-
sub_settings.iter().map(|s| s.display(root_path, suffix)).collect::<String>()
366-
),
367-
Setting::Toggle { js_data_name, description, default_value } => format!(
368-
"<div class=\"setting-line\">\
369-
<label class=\"toggle\">\
370-
<input type=\"checkbox\" id=\"{}\" {}>\
371-
<span class=\"slider\"></span>\
372-
</label>\
373-
<div>{}</div>\
374-
</div>",
375-
js_data_name,
376-
if default_value { " checked" } else { "" },
377-
description,
378-
),
379-
Setting::Select { js_data_name, description, default_value, ref options } => format!(
380-
"<div class=\"setting-line\"><div class=\"radio-line\" id=\"{}\"><span class=\"setting-name\">{}</span><div class=\"choices\">{}</div></div></div>",
381-
js_data_name,
382-
description,
383-
options
384-
.iter()
385-
.map(|opt| format!(
386-
"<label for=\"{js_data_name}-{name}\" class=\"choice\">
387-
<input type=\"radio\" name=\"{js_data_name}\" id=\"{js_data_name}-{name}\" value=\"{name}\" {checked}>\
388-
{name}\
389-
</label>",
390-
js_data_name = js_data_name,
391-
name = opt,
392-
checked = if opt == default_value { "checked" } else { "" },
393-
))
394-
.collect::<String>(),
395-
),
396-
}
397-
}
398-
}
399-
400-
impl From<(&'static str, &'static str, bool)> for Setting {
401-
fn from(values: (&'static str, &'static str, bool)) -> Setting {
402-
Setting::Toggle { js_data_name: values.0, description: values.1, default_value: values.2 }
403-
}
404-
}
405-
406-
impl<T: Into<Setting>> From<(&'static str, Vec<T>)> for Setting {
407-
fn from(values: (&'static str, Vec<T>)) -> Setting {
408-
Setting::Section {
409-
description: values.0,
410-
sub_settings: values.1.into_iter().map(|v| v.into()).collect::<Vec<_>>(),
411-
}
412-
}
413-
}
414-
415-
fn settings(root_path: &str, suffix: &str, theme_names: Vec<String>) -> Result<String, Error> {
416-
// (id, explanation, default value)
417-
let settings: &[Setting] = &[
418-
Setting::from(("use-system-theme", "Use system theme", true)),
419-
Setting::Select {
420-
js_data_name: "theme",
421-
description: "Theme",
422-
default_value: "light",
423-
options: theme_names.clone(),
424-
},
425-
Setting::Select {
426-
js_data_name: "preferred-light-theme",
427-
description: "Preferred light theme",
428-
default_value: "light",
429-
options: theme_names.clone(),
430-
},
431-
Setting::Select {
432-
js_data_name: "preferred-dark-theme",
433-
description: "Preferred dark theme",
434-
default_value: "dark",
435-
options: theme_names,
436-
},
437-
("auto-hide-large-items", "Auto-hide item contents for large items.", true).into(),
438-
("auto-hide-method-docs", "Auto-hide item methods' documentation", false).into(),
439-
("auto-hide-trait-implementations", "Auto-hide trait implementation documentation", false)
440-
.into(),
441-
("go-to-only-result", "Directly go to item in search if there is only one result", false)
442-
.into(),
443-
("line-numbers", "Show line numbers on code examples", false).into(),
444-
("disable-shortcuts", "Disable keyboard shortcuts", false).into(),
445-
];
446-
447-
Ok(format!(
448-
"<div class=\"main-heading\">
449-
<h1 class=\"fqn\">\
450-
<span class=\"in-band\">Rustdoc settings</span>\
451-
</h1>\
452-
<span class=\"out-of-band\">\
453-
<a id=\"back\" href=\"javascript:void(0)\">Back</a>\
454-
</span>\
455-
</div>\
456-
<div class=\"settings\">{}</div>\
457-
<link rel=\"stylesheet\" href=\"{root_path}settings{suffix}.css\">\
458-
<script src=\"{root_path}settings{suffix}.js\"></script>",
459-
settings.iter().map(|s| s.display(root_path, suffix)).collect::<String>(),
460-
root_path = root_path,
461-
suffix = suffix
462-
))
463-
}
464-
465337
fn scrape_examples_help(shared: &SharedContext<'_>) -> String {
466338
let mut content = SCRAPE_EXAMPLES_HELP_MD.to_owned();
467339
content.push_str(&format!(

0 commit comments

Comments
 (0)