Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hotkeys] Hide hotkeys dialog on 'shift + ?' keypress #1301

Merged
merged 5 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/core/src/components/hotkeys/hotkeysDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class HotkeysDialog {
private container: HTMLElement;
private hotkeysQueue = [] as IHotkeyProps[][];
private isDialogShowing = false;
private timeoutToken = 0;
private showTimeoutToken = 0;
private hideTimeoutToken = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

feels more accurate to declare these as number rather than initialize to a likely invalid ID (although clearTimeout happily ignores invalid IDs)


public render() {
if (this.container == null) {
Expand Down Expand Up @@ -59,8 +60,13 @@ class HotkeysDialog {
this.hotkeysQueue.push(hotkeys);

// reset timeout for debounce
clearTimeout(this.timeoutToken);
this.timeoutToken = setTimeout(this.show, 10);
clearTimeout(this.showTimeoutToken);
this.showTimeoutToken = setTimeout(this.show, 10);
}

public hideAfterDelay() {
clearTimeout(this.hideTimeoutToken);
this.hideTimeoutToken = setTimeout(this.hide, 10);
}

public show = () => {
Expand Down Expand Up @@ -141,3 +147,7 @@ export function showHotkeysDialog(hotkeys: IHotkeyProps[]) {
export function hideHotkeysDialog() {
HOTKEYS_DIALOG.hide();
}

export function hideHotkeysDialogAfterDelay() {
HOTKEYS_DIALOG.hideAfterDelay();
}
12 changes: 9 additions & 3 deletions packages/core/src/components/hotkeys/hotkeysEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { safeInvoke } from "../../common/utils";
import { Hotkey, IHotkeyProps } from "./hotkey";
import { comboMatches, getKeyCombo, IKeyCombo, parseKeyCombo } from "./hotkeyParser";
import { IHotkeysProps } from "./hotkeys";
import { isHotkeysDialogShowing, showHotkeysDialog } from "./hotkeysDialog";
import { hideHotkeysDialogAfterDelay, isHotkeysDialogShowing, showHotkeysDialog } from "./hotkeysDialog";

const SHOW_DIALOG_KEY = "?";

Expand Down Expand Up @@ -53,14 +53,20 @@ export class HotkeysEvents {
}

public handleKeyDown = (e: KeyboardEvent) => {
if (this.isTextInput(e) || isHotkeysDialogShowing()) {
if (this.isTextInput(e)) {
return;
}

const combo = getKeyCombo(e);

if (comboMatches(parseKeyCombo(SHOW_DIALOG_KEY), combo)) {
showHotkeysDialog(this.actions.map((action) => action.props));
if (isHotkeysDialogShowing()) {
hideHotkeysDialogAfterDelay();
Copy link
Contributor

Choose a reason for hiding this comment

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

why is the delay necessary here? what's wrong with simply hideHotkeysDialog?

Copy link
Contributor Author

@cmslewis cmslewis Jul 10, 2017

Choose a reason for hiding this comment

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

Because a bunch of disjoint hotkey keydown listeners will fire in succession. If you synchronously call hideHotkeysDialog in any one of them, then immediate successive calls to isHotkeysDialogShowing would return false, causing the hotkeys dialog to reopen (because the same hotkey that opens that dialog is now overloaded to close it too).

Introducing a small delay is the only way to guarantee all those keydown listeners execute before the dialog actually gets hidden.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added a comment.

} else {
showHotkeysDialog(this.actions.map((action) => action.props));
}
return;
} else if (isHotkeysDialogShowing()) {
return;
}

Expand Down