Skip to content

Commit

Permalink
Fix supertab freezing the app
Browse files Browse the repository at this point in the history
Removes the infinite, app-freezing while-loop and handles the now-reachable errors.
  • Loading branch information
Wallunen committed Sep 23, 2024
1 parent 02e7a9e commit abc6e9c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
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 :(
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)) {
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);
}

0 comments on commit abc6e9c

Please sign in to comment.