element-call-Github/src/useCallViewKeyboardShortcuts.ts

95 lines
2.6 KiB
TypeScript
Raw Normal View History

/*
Copyright 2022-2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
*/
import { RefObject, useCallback, useMemo, useRef } from "react";
import { useEventTarget } from "./useEvents";
/**
* Determines whether focus is in the same part of the tree as the given
* element (specifically, if the element or an ancestor of it is focused).
*/
const mayReceiveKeyEvents = (e: HTMLElement): boolean => {
2023-04-20 03:54:39 +08:00
const focusedElement = document.activeElement;
return focusedElement !== null && focusedElement.contains(e);
2023-04-20 03:54:39 +08:00
};
2023-01-13 19:52:40 +08:00
export function useCallViewKeyboardShortcuts(
focusElement: RefObject<HTMLElement | null>,
toggleMicrophoneMuted: () => void,
toggleLocalVideoMuted: () => void,
2023-10-11 22:42:04 +08:00
setMicrophoneMuted: (muted: boolean) => void,
): void {
const spacebarHeld = useRef(false);
// These event handlers are set on the window because we want users to be able
// to trigger them without going to the trouble of focusing something
useEventTarget(
window,
"keydown",
useCallback(
(event: KeyboardEvent) => {
2023-04-20 03:54:39 +08:00
if (focusElement.current === null) return;
if (!mayReceiveKeyEvents(focusElement.current)) return;
if (event.key === "m") {
event.preventDefault();
toggleMicrophoneMuted();
} else if (event.key == "v") {
event.preventDefault();
toggleLocalVideoMuted();
} else if (event.key === " ") {
event.preventDefault();
if (!spacebarHeld.current) {
spacebarHeld.current = true;
setMicrophoneMuted(false);
}
}
},
2023-04-20 03:55:55 +08:00
[
focusElement,
toggleLocalVideoMuted,
toggleMicrophoneMuted,
setMicrophoneMuted,
2023-10-11 22:42:04 +08:00
],
),
// Because this is set on the window, to prevent shortcuts from activating
// another event callback at the same time, we need to preventDefault
// *before* child elements receive the event by using capture mode
useMemo(() => ({ capture: true }), []),
);
useEventTarget(
window,
"keyup",
useCallback(
(event: KeyboardEvent) => {
2023-04-20 03:54:39 +08:00
if (focusElement.current === null) return;
if (!mayReceiveKeyEvents(focusElement.current)) return;
if (event.key === " ") {
spacebarHeld.current = false;
setMicrophoneMuted(true);
}
},
2023-10-11 22:42:04 +08:00
[focusElement, setMicrophoneMuted],
),
);
useEventTarget(
window,
"blur",
useCallback(() => {
if (spacebarHeld.current) {
spacebarHeld.current = false;
setMicrophoneMuted(true);
}
2023-10-11 22:42:04 +08:00
}, [setMicrophoneMuted, spacebarHeld]),
);
}