Skip to content

Consolidate keyboard shortcuts into a single registry#7314

Closed
donkawechico wants to merge 3 commits intohome-assistant:devfrom
donkawechico:keyboard_shortcut_mixin
Closed

Consolidate keyboard shortcuts into a single registry#7314
donkawechico wants to merge 3 commits intohome-assistant:devfrom
donkawechico:keyboard_shortcut_mixin

Conversation

@donkawechico
Copy link
Contributor

@donkawechico donkawechico commented Oct 11, 2020

Proposed change

Now that other folks are starting to introduce shortcuts into Home Assistant (#7207, #7230), it might be a good idea to consolidate that logic into a single place and a standardized API.

Eventually this could be used to both a) warn about, or ensure we don't overwrite existing hotkeys, and b) surface all existing application hotkeys to the user (say, in a helpful shortcut overview launched with the question mark).

This doesn't (yet) have any protections against folks overwriting shortcuts by accident, but it does make it easier to see what components are registering hotkeys, and which keys are already taken.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

@donkawechico donkawechico force-pushed the keyboard_shortcut_mixin branch from 433944e to 73fdf5d Compare October 12, 2020 23:56
"@typescript-eslint/no-use-before-define": 0,
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-shadow": ["error"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed due to issue with making enum types in typescript with default eslint.

@donkawechico donkawechico marked this pull request as ready for review October 13, 2020 02:00
@KevinCathcart
Copy link
Contributor

One question to consider is the approach for supporting keybindings. The approach shown here works, but if we get to 10-20 shortcuts, the pattern used here will begin to expand to quite a bit of code (the switch style decoding of the keypresses in particular).

Might it be better to make use of a more traditional approach to avoid code bloat? Perhaps it makes sense to use a small keybinding library like the ~400B when minified tinykeys library, or borrow some code from such a library.

Separately, it might be better to have shortcut actions registered without a specific keybinding specified in the registering code. Instead have the registry itself have a static mapping of actions to bindings. That approach would make it rather straightforward to eventually add customizable keybindings, if demand should arise (the existing map becomes the defaults).

I'm suggesting the above only as things that might be worth thinking about. I am not objecting to the current proposed code, since it is unquestionably an improvement over the current situation.

@bramkragten
Copy link
Member

tinykeys does look nice :-)

@donkawechico
Copy link
Contributor Author

Tinykeys does look nice, but I think I'd want it to be used by keyboard_shortcut_mixin rather than replace it (not sure which you were suggesting). If devs use tinykeys directly in their components we could still end up in a situation where we're stomping on each others hotkeys.

But I 100% agree with what you're saying. This could get super messy if we don't keep future refactors in mind.

@donkawechico donkawechico force-pushed the keyboard_shortcut_mixin branch from afd4574 to 50bb51c Compare October 13, 2020 16:25
@KevinCathcart
Copy link
Contributor

KevinCathcart commented Oct 13, 2020

Yeah, I was certainly trying to suggest that if it were used, it would be used as part of the implementation of the mixin. Having every component set up shortcuts themselves with no coordination is exactly what we don't want.

@bramkragten
Copy link
Member

Still, this would not be able to tell what elements use what keyboard shortcuts, we would need a global registry for that. This mixin will only know the keys that the current element registered.

@donkawechico
Copy link
Contributor Author

Still, this would not be able to tell what elements use what keyboard shortcuts, we would need a global registry for that. This mixin will only know the keys that the current element registered.

Agreed. As I mention in the description, having the logic consolidated into one place should passively help avoid shortcut clobbering for now. But eventually I agree we need a more global dictionary.

private _registeredShortcuts: RegisteredShortcuts = {};

public disconnectedCallback() {
this.removeEventListener("keydown", this._keydownEvent);
Copy link
Member

@bramkragten bramkragten Oct 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we remove the listener on disconnected, we should add it again on connected. The firstupdated will only run once, so after the element is disconnected the listener will never be added again on connect

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this will not remove it now, as the callbacks for the add and remove are different.

callback: () => void,
element: HTMLElement | Document = this
) {
element.addEventListener("keydown", (event) => this._keydownEvent(event));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not do this in registerShortcut, we would get multiple listeners if you register multiple shortcuts.

@bramkragten
Copy link
Member

Do we need to update this, or just close?

@github-actions
Copy link

github-actions bot commented Feb 7, 2021

There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days.
Thank you for your contributions.

@github-actions github-actions bot added the stale label Feb 7, 2021
@github-actions github-actions bot closed this Feb 14, 2021
@github-actions github-actions bot locked and limited conversation to collaborators Jul 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants