Skip to content

Commit 704050d

Browse files
Make keyboard interactions in the settings menu more pleasant
1 parent 12f0dba commit 704050d

File tree

2 files changed

+44
-26
lines changed

2 files changed

+44
-26
lines changed

src/librustdoc/html/static/main.js

+23-22
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,29 @@ if (!DOMTokenList.prototype.remove) {
4040
};
4141
}
4242

43+
44+
// Gets the human-readable string for the virtual-key code of the
45+
// given KeyboardEvent, ev.
46+
//
47+
// This function is meant as a polyfill for KeyboardEvent#key,
48+
// since it is not supported in Trident. We also test for
49+
// KeyboardEvent#keyCode because the handleShortcut handler is
50+
// also registered for the keydown event, because Blink doesn't fire
51+
// keypress on hitting the Escape key.
52+
//
53+
// So I guess you could say things are getting pretty interoperable.
54+
function getVirtualKey(ev) {
55+
if ("key" in ev && typeof ev.key != "undefined") {
56+
return ev.key;
57+
}
58+
59+
var c = ev.charCode || ev.keyCode;
60+
if (c == 27) {
61+
return "Escape";
62+
}
63+
return String.fromCharCode(c);
64+
}
65+
4366
function getSearchInput() {
4467
return document.getElementsByClassName("search-input")[0];
4568
}
@@ -323,28 +346,6 @@ function defocusSearchBar() {
323346
}
324347
}
325348

326-
// Gets the human-readable string for the virtual-key code of the
327-
// given KeyboardEvent, ev.
328-
//
329-
// This function is meant as a polyfill for KeyboardEvent#key,
330-
// since it is not supported in Trident. We also test for
331-
// KeyboardEvent#keyCode because the handleShortcut handler is
332-
// also registered for the keydown event, because Blink doesn't fire
333-
// keypress on hitting the Escape key.
334-
//
335-
// So I guess you could say things are getting pretty interoperable.
336-
function getVirtualKey(ev) {
337-
if ("key" in ev && typeof ev.key != "undefined") {
338-
return ev.key;
339-
}
340-
341-
var c = ev.charCode || ev.keyCode;
342-
if (c == 27) {
343-
return "Escape";
344-
}
345-
return String.fromCharCode(c);
346-
}
347-
348349
function getHelpElement() {
349350
buildHelperPopup();
350351
return document.getElementById("help");

src/librustdoc/html/static/settings.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Local js definitions:
2-
/* global getCurrentValue, updateLocalStorage, updateSystemTheme */
2+
/* global getCurrentValue, getVirtualKey, updateLocalStorage, updateSystemTheme */
33

44
(function () {
55
function changeSetting(settingName, value) {
@@ -14,10 +14,25 @@
1414
}
1515
}
1616

17+
function handleKey(ev) {
18+
// Don't interfere with browser shortcuts
19+
if (ev.ctrlKey || ev.altKey || ev.metaKey) {
20+
return;
21+
}
22+
switch (getVirtualKey(ev)) {
23+
case "Enter":
24+
case "Return":
25+
case "Space":
26+
ev.target.checked = !ev.target.checked;
27+
ev.preventDefault();
28+
break;
29+
}
30+
}
31+
1732
function setEvents() {
1833
var elems = {
19-
toggles: document.getElementsByClassName("slider"),
20-
selects: document.getElementsByClassName("select-wrapper")
34+
toggles: Array.prototype.slice.call(document.getElementsByClassName("slider")),
35+
selects: Array.prototype.slice.call(document.getElementsByClassName("select-wrapper")),
2136
};
2237
var i;
2338

@@ -32,6 +47,8 @@
3247
toggle.onchange = function() {
3348
changeSetting(this.id, this.checked);
3449
};
50+
toggle.onkeyup = handleKey;
51+
toggle.onkeyrelease = handleKey;
3552
}
3653
}
3754

@@ -50,5 +67,5 @@
5067
}
5168
}
5269

53-
setEvents();
70+
window.addEventListener("DOMContentLoaded", setEvents);
5471
})();

0 commit comments

Comments
 (0)