Skip to content

Commit 17b8ca9

Browse files
committed
Pull theme picker keyboard code into main.js
Most of the code in mod.rs should be code that really needs to have the list of available themes inlined into it.
1 parent 90c7c63 commit 17b8ca9

File tree

2 files changed

+59
-51
lines changed

2 files changed

+59
-51
lines changed

Diff for: src/librustdoc/html/render/mod.rs

-50
Original file line numberDiff line numberDiff line change
@@ -807,58 +807,8 @@ function handleThemeButtonsBlur(e) {{
807807
}}
808808
}}
809809
810-
function handleThemeKeyDown(e) {{
811-
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {{ return; }}
812-
if (!themePicker.parentNode.contains(e.target)) {{ return; }}
813-
var active = document.activeElement;
814-
switch (e.key) {{
815-
case "ArrowUp":
816-
e.preventDefault();
817-
if (active.previousElementSibling && e.target.id !== "theme-picker") {{
818-
active.previousElementSibling.focus();
819-
}} else {{
820-
showThemeButtonState();
821-
themes.lastElementChild.focus();
822-
}}
823-
break;
824-
case "ArrowDown":
825-
e.preventDefault();
826-
if (active.nextElementSibling && e.target.id !== "theme-picker") {{
827-
active.nextElementSibling.focus();
828-
}} else {{
829-
showThemeButtonState();
830-
themes.firstElementChild.focus();
831-
}}
832-
break;
833-
case "Enter":
834-
case "Return":
835-
case "Space":
836-
if (e.target.id === "theme-picker" && themes.style.display === "none") {{
837-
e.preventDefault();
838-
showThemeButtonState();
839-
themes.firstElementChild.focus();
840-
}}
841-
break;
842-
case "Home":
843-
e.preventDefault();
844-
themes.firstElementChild.focus();
845-
break;
846-
case "End":
847-
e.preventDefault();
848-
themes.lastElementChild.focus();
849-
break;
850-
// The escape key is handled in main.js, instead of here, for two reasons:
851-
//
852-
// 1 Escape should close the menu, even if it's not focused.
853-
// 2 The escape event handler is bound to both keydown and keypress, to work
854-
// around browser inconsistencies. That sort of logic doesn't apply to the
855-
// rest of these keybindings.
856-
}}
857-
}};
858-
859810
themePicker.onclick = switchThemeButtonState;
860811
themePicker.onblur = handleThemeButtonsBlur;
861-
document.addEventListener("keydown", handleThemeKeyDown);
862812
{}.forEach(function(item) {{
863813
var but = document.createElement("button");
864814
but.textContent = item;

Diff for: src/librustdoc/html/static/main.js

+59-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Local js definitions:
55
/* global addClass, getCurrentValue, hasClass */
66
/* global onEachLazy, hasOwnProperty, removeClass, updateLocalStorage */
7-
/* global hideThemeButtonState */
7+
/* global hideThemeButtonState, showThemeButtonState */
88

99
if (!String.prototype.startsWith) {
1010
String.prototype.startsWith = function(searchString, position) {
@@ -48,6 +48,14 @@ function getSearchElement() {
4848
return document.getElementById("search");
4949
}
5050

51+
function getThemesElement() {
52+
return document.getElementById("theme-choices");
53+
}
54+
55+
function getThemePickerElement() {
56+
return document.getElementById("theme-picker");
57+
}
58+
5159
// Sets the focus on the search bar at the top of the page
5260
function focusSearchBar() {
5361
getSearchInput().focus();
@@ -406,7 +414,57 @@ function defocusSearchBar() {
406414
case "?":
407415
displayHelp(true, ev);
408416
break;
417+
418+
default:
419+
var themePicker = getThemePickerElement();
420+
if (themePicker.parentNode.contains(ev.target)) {
421+
handleThemeKeyDown(ev);
422+
}
423+
}
424+
}
425+
}
426+
427+
function handleThemeKeyDown(ev) {
428+
var active = document.activeElement;
429+
var themes = getThemesElement();
430+
switch (getVirtualKey(ev)) {
431+
case "ArrowUp":
432+
ev.preventDefault();
433+
if (active.previousElementSibling && ev.target.id !== "theme-picker") {
434+
active.previousElementSibling.focus();
435+
} else {
436+
showThemeButtonState();
437+
themes.lastElementChild.focus();
409438
}
439+
break;
440+
case "ArrowDown":
441+
ev.preventDefault();
442+
if (active.nextElementSibling && ev.target.id !== "theme-picker") {
443+
active.nextElementSibling.focus();
444+
} else {
445+
showThemeButtonState();
446+
themes.firstElementChild.focus();
447+
}
448+
break;
449+
case "Enter":
450+
case "Return":
451+
case "Space":
452+
if (ev.target.id === "theme-picker" && themes.style.display === "none") {
453+
ev.preventDefault();
454+
showThemeButtonState();
455+
themes.firstElementChild.focus();
456+
}
457+
break;
458+
case "Home":
459+
ev.preventDefault();
460+
themes.firstElementChild.focus();
461+
break;
462+
case "End":
463+
ev.preventDefault();
464+
themes.lastElementChild.focus();
465+
break;
466+
// The escape key is handled in handleEscape, not here,
467+
// so that pressing escape will close the menu even if it isn't focused
410468
}
411469
}
412470

0 commit comments

Comments
 (0)