Skip to content

Commit

Permalink
Make keyboard interactions in the settings menu more pleasant
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 12, 2020
1 parent 12f0dba commit 704050d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
45 changes: 23 additions & 22 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,29 @@ if (!DOMTokenList.prototype.remove) {
};
}


// Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev.
//
// This function is meant as a polyfill for KeyboardEvent#key,
// since it is not supported in Trident. We also test for
// KeyboardEvent#keyCode because the handleShortcut handler is
// also registered for the keydown event, because Blink doesn't fire
// keypress on hitting the Escape key.
//
// So I guess you could say things are getting pretty interoperable.
function getVirtualKey(ev) {
if ("key" in ev && typeof ev.key != "undefined") {
return ev.key;
}

var c = ev.charCode || ev.keyCode;
if (c == 27) {
return "Escape";
}
return String.fromCharCode(c);
}

function getSearchInput() {
return document.getElementsByClassName("search-input")[0];
}
Expand Down Expand Up @@ -323,28 +346,6 @@ function defocusSearchBar() {
}
}

// Gets the human-readable string for the virtual-key code of the
// given KeyboardEvent, ev.
//
// This function is meant as a polyfill for KeyboardEvent#key,
// since it is not supported in Trident. We also test for
// KeyboardEvent#keyCode because the handleShortcut handler is
// also registered for the keydown event, because Blink doesn't fire
// keypress on hitting the Escape key.
//
// So I guess you could say things are getting pretty interoperable.
function getVirtualKey(ev) {
if ("key" in ev && typeof ev.key != "undefined") {
return ev.key;
}

var c = ev.charCode || ev.keyCode;
if (c == 27) {
return "Escape";
}
return String.fromCharCode(c);
}

function getHelpElement() {
buildHelperPopup();
return document.getElementById("help");
Expand Down
25 changes: 21 additions & 4 deletions src/librustdoc/html/static/settings.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Local js definitions:
/* global getCurrentValue, updateLocalStorage, updateSystemTheme */
/* global getCurrentValue, getVirtualKey, updateLocalStorage, updateSystemTheme */

(function () {
function changeSetting(settingName, value) {
Expand All @@ -14,10 +14,25 @@
}
}

function handleKey(ev) {
// Don't interfere with browser shortcuts
if (ev.ctrlKey || ev.altKey || ev.metaKey) {
return;
}
switch (getVirtualKey(ev)) {
case "Enter":
case "Return":
case "Space":
ev.target.checked = !ev.target.checked;
ev.preventDefault();
break;
}
}

function setEvents() {
var elems = {
toggles: document.getElementsByClassName("slider"),
selects: document.getElementsByClassName("select-wrapper")
toggles: Array.prototype.slice.call(document.getElementsByClassName("slider")),
selects: Array.prototype.slice.call(document.getElementsByClassName("select-wrapper")),
};
var i;

Expand All @@ -32,6 +47,8 @@
toggle.onchange = function() {
changeSetting(this.id, this.checked);
};
toggle.onkeyup = handleKey;
toggle.onkeyrelease = handleKey;
}
}

Expand All @@ -50,5 +67,5 @@
}
}

setEvents();
window.addEventListener("DOMContentLoaded", setEvents);
})();

0 comments on commit 704050d

Please sign in to comment.