Skip to content

Abstract keybinding to allow keyboard shortcut customization

Nathan Friend requested to merge nfriend-keyboard-customizations-step-1 into master

What does this MR do?

This is step 1 towards #251226, which allows keyboard shortcuts to be individually disabled or customized.

Before this MR, developers would directly bind a key sequence to a handler:

Mousetrap.bind('p b', handler);

After this MR, developers will instead define a new "command" in ~/behaviors/shortcuts/keybindings.js and bind to the result of keysFor(command):

import { keysFor, TOGGLE_PERFORMANCE_BAR } from '~/behaviors/shortcuts/keybindings'

Mousetrap.bind(keysFor(TOGGLE_PERFORMANCE_BAR), handler);

keybindings.js handles returning the appropriate key sequence for the command (or it returns [] if the command is disabled).

This MR also updates our developer documentation with some details about how keyboard shortcuts should be implemented going forward.

Technical details

At a high level, the keysFor looks in localStorage for any keyboard shortcut customizations and merges the customization object with the default keybindings. Then, it returns the appropriate keybinding for the provided command.

What doesn't this MR do?

This MR does not replace all instances of Mousetrap.bind('key+sequence') with Mousetrap.bind(keysFor(COMMAND)). Instead, only a single shortcut is replaced (pb: toggle performance bar), for demonstration/review purposes. A follow-up MR will replace all of the these keybindings once this framework is in place.

Screenshots

This MR makes no user-facing changes.

What's the big picture?

See !43348 (closed) for a better idea of how this MR fits into the larger picture.

Related to #251226

Edited by Nathan Friend

Merge request reports

Loading