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

Fix supertab freezing the app #6965

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
39 changes: 16 additions & 23 deletions ts/services/addGlobalKeyboardShortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function addGlobalKeyboardShortcuts(): void {
return;
}

// Super tab :)
// Super tab :(
Copy link
Author

Choose a reason for hiding this comment

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

Signal Spirits haunt this codebase. Let the frown scare them away.

if (
(commandOrCtrl && key === 'F6') ||
(commandOrCtrl && !shiftKey && (key === 't' || key === 'T'))
Expand All @@ -55,10 +55,7 @@ export function addGlobalKeyboardShortcuts(): void {
const focusedIndexes: Array<number> = [];

targets.forEach((target, index) => {
if (
(focusedElement != null && target === focusedElement) ||
target.contains(focusedElement)
) {
if (target.contains(focusedElement)) {
Copy link
Author

Choose a reason for hiding this comment

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

target.contains(focusedElement) makes the preceding condition pointless because .contains() allows a null parameter and considers elements to also contain themselves.

focusedIndexes.push(index);
}
});
Expand All @@ -76,20 +73,13 @@ export function addGlobalKeyboardShortcuts(): void {
const focusedIndex = focusedIndexes.at(-1) ?? -1;

const lastIndex = targets.length - 1;
const increment = shiftKey ? -1 : 1;

let index;
if (focusedIndex < 0 || focusedIndex >= lastIndex) {
index = 0;
let index: number | undefined;
if (focusedIndex < 0) {
index = shiftKey ? lastIndex : 0;
} else {
index = focusedIndex + increment;
}

while (!targets[index]) {
index += increment;
if (index > lastIndex || index < 0) {
index = 0;
}
const increment = shiftKey ? lastIndex : 1;
index = (focusedIndex + increment) % targets.length;
}

const node = targets[index];
Expand All @@ -98,12 +88,15 @@ export function addGlobalKeyboardShortcuts(): void {
if (firstFocusableElement) {
firstFocusableElement.focus();
} else {
const nodeInfo = Array.from(node.attributes)
.map(attr => `${attr.name}=${attr.value}`)
.join(',');
log.warn(
`supertab: could not find focus for DOM node ${node.nodeName}<${nodeInfo}>`
);
if (node) {
const nodeInfo = Array.from(node.attributes)
.map(attr => `${attr.name}=${attr.value}`)
.join(',');
log.warn(
`supertab: could not find focus for DOM node ${node.nodeName}<${nodeInfo}>`
);
}

window.enterMouseMode();
const { activeElement } = document;
if (
Expand Down
8 changes: 4 additions & 4 deletions ts/util/focusableSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const focusableSelector = focusableSelectors.join(', ');
* is focusable.
*/
export function matchOrQueryFocusable(
element: HTMLElement
): HTMLElement | null {
return element.matches(focusableSelector)
element: HTMLElement | null | undefined
): HTMLElement | null | undefined {
return element?.matches(focusableSelector)
? element
: element.querySelector(focusableSelector);
: element?.querySelector(focusableSelector);
}