Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ pub fn render<T: Print, S: Print>(
{sidebar}\
</nav>\
<div class=\"theme-picker\">\
<button id=\"theme-picker\" aria-label=\"Pick another theme!\">\
<button id=\"theme-picker\" aria-label=\"Pick another theme!\" aria-haspopup=\"menu\">\
<img src=\"{static_root_path}brush{suffix}.svg\" \
width=\"18\" \
alt=\"Pick another theme!\">\
</button>\
<div id=\"theme-choices\"></div>\
<div id=\"theme-choices\" role=\"menu\"></div>\
</div>\
<script src=\"{static_root_path}theme{suffix}.js\"></script>\
<nav class=\"sub\">\
Expand Down
54 changes: 52 additions & 2 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,17 +798,67 @@ function handleThemeButtonsBlur(e) {{
var active = document.activeElement;
var related = e.relatedTarget;

if (active.id !== "themePicker" &&
if (active.id !== "theme-picker" &&
(!active.parentNode || active.parentNode.id !== "theme-choices") &&
(!related ||
(related.id !== "themePicker" &&
(related.id !== "theme-picker" &&
(!related.parentNode || related.parentNode.id !== "theme-choices")))) {{
hideThemeButtonState();
}}
}}

function handleThemeKeyDown(e) {{
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {{ return; }}
if (!themePicker.parentNode.contains(e.target)) {{ return; }}
var active = document.activeElement;
switch (e.key) {{
case "ArrowUp":
e.preventDefault();
if (active.previousElementSibling && e.target.id !== "theme-picker") {{
active.previousElementSibling.focus();
}} else {{
showThemeButtonState();
themes.lastElementChild.focus();
}}
break;
case "ArrowDown":
e.preventDefault();
if (active.nextElementSibling && e.target.id !== "theme-picker") {{
active.nextElementSibling.focus();
}} else {{
showThemeButtonState();
themes.firstElementChild.focus();
}}
break;
case "Enter":
case "Return":
case "Space":
if (e.target.id === "theme-picker" && themes.style.display === "none") {{
e.preventDefault();
showThemeButtonState();
themes.firstElementChild.focus();
}}
break;
case "Home":
e.preventDefault();
themes.firstElementChild.focus();
break;
case "End":
e.preventDefault();
themes.lastElementChild.focus();
break;
// The escape key is handled in main.js, instead of here, for two reasons:
//
// 1 Escape should close the menu, even if it's not focused.
// 2 The escape event handler is bound to both keydown and keypress, to work
// around browser inconsistencies. That sort of logic doesn't apply to the
// rest of these keybindings.
}}
}};

themePicker.onclick = switchThemeButtonState;
themePicker.onblur = handleThemeButtonsBlur;
document.addEventListener("keydown", handleThemeKeyDown);
{}.forEach(function(item) {{
var but = document.createElement("button");
but.textContent = item;
Expand Down
10 changes: 2 additions & 8 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Local js definitions:
/* global addClass, getCurrentValue, hasClass */
/* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
/* global hideThemeButtonState */

if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
Expand Down Expand Up @@ -137,10 +138,6 @@ function defocusSearchBar() {
sidebar.appendChild(div);
}
}
var themePickers = document.getElementsByClassName("theme-picker");
if (themePickers && themePickers.length > 0) {
themePickers[0].style.display = "none";
}
}

function hideSidebar() {
Expand All @@ -155,10 +152,6 @@ function defocusSearchBar() {
filler.remove();
}
document.getElementsByTagName("body")[0].style.marginTop = "";
var themePickers = document.getElementsByClassName("theme-picker");
if (themePickers && themePickers.length > 0) {
themePickers[0].style.display = null;
}
}

function showSearchResults(search) {
Expand Down Expand Up @@ -376,6 +369,7 @@ function defocusSearchBar() {
document.title = titleBeforeSearch;
}
defocusSearchBar();
hideThemeButtonState();
}

function handleShortcut(ev) {
Expand Down