2023-01-13 01:31:19 +08:00
|
|
|
/*
|
2024-09-06 16:22:13 +08:00
|
|
|
Copyright 2022-2024 New Vector Ltd.
|
2023-01-13 01:31:19 +08:00
|
|
|
|
2024-09-06 16:22:13 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2023-01-13 01:31:19 +08:00
|
|
|
*/
|
|
|
|
|
2024-07-25 06:42:21 +08:00
|
|
|
import { RefObject, useCallback, useMemo, useRef } from "react";
|
2023-01-13 01:31:19 +08:00
|
|
|
|
|
|
|
import { useEventTarget } from "./useEvents";
|
|
|
|
|
2023-04-20 03:51:44 +08:00
|
|
|
/**
|
|
|
|
* Determines whether focus is in the same part of the tree as the given
|
2024-07-25 06:42:21 +08:00
|
|
|
* element (specifically, if the element or an ancestor of it is focused).
|
2023-04-20 03:51:44 +08:00
|
|
|
*/
|
|
|
|
const mayReceiveKeyEvents = (e: HTMLElement): boolean => {
|
2023-04-20 03:54:39 +08:00
|
|
|
const focusedElement = document.activeElement;
|
2024-07-25 06:42:21 +08:00
|
|
|
return focusedElement !== null && focusedElement.contains(e);
|
2023-04-20 03:54:39 +08:00
|
|
|
};
|
2023-04-20 03:51:44 +08:00
|
|
|
|
2023-01-13 19:52:40 +08:00
|
|
|
export function useCallViewKeyboardShortcuts(
|
2023-04-20 03:51:44 +08:00
|
|
|
focusElement: RefObject<HTMLElement | null>,
|
2023-01-13 01:31:19 +08:00
|
|
|
toggleMicrophoneMuted: () => void,
|
|
|
|
toggleLocalVideoMuted: () => void,
|
2023-10-11 22:42:04 +08:00
|
|
|
setMicrophoneMuted: (muted: boolean) => void,
|
2023-09-23 06:05:13 +08:00
|
|
|
): void {
|
2023-04-20 03:17:32 +08:00
|
|
|
const spacebarHeld = useRef(false);
|
2023-01-13 01:31:19 +08:00
|
|
|
|
2023-04-20 03:51:44 +08:00
|
|
|
// 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
|
|
|
|
|
2023-01-13 01:31:19 +08:00
|
|
|
useEventTarget(
|
|
|
|
window,
|
|
|
|
"keydown",
|
|
|
|
useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
2023-04-20 03:54:39 +08:00
|
|
|
if (focusElement.current === null) return;
|
2023-04-20 03:51:44 +08:00
|
|
|
if (!mayReceiveKeyEvents(focusElement.current)) return;
|
2023-01-13 01:31:19 +08:00
|
|
|
|
|
|
|
if (event.key === "m") {
|
2024-07-25 06:42:21 +08:00
|
|
|
event.preventDefault();
|
2023-01-13 01:31:19 +08:00
|
|
|
toggleMicrophoneMuted();
|
|
|
|
} else if (event.key == "v") {
|
2024-07-25 06:42:21 +08:00
|
|
|
event.preventDefault();
|
2023-01-13 01:31:19 +08:00
|
|
|
toggleLocalVideoMuted();
|
2024-07-25 06:42:21 +08:00
|
|
|
} else if (event.key === " ") {
|
|
|
|
event.preventDefault();
|
|
|
|
if (!spacebarHeld.current) {
|
|
|
|
spacebarHeld.current = true;
|
|
|
|
setMicrophoneMuted(false);
|
|
|
|
}
|
2023-01-13 01:31:19 +08:00
|
|
|
}
|
|
|
|
},
|
2023-04-20 03:55:55 +08:00
|
|
|
[
|
|
|
|
focusElement,
|
|
|
|
toggleLocalVideoMuted,
|
|
|
|
toggleMicrophoneMuted,
|
|
|
|
setMicrophoneMuted,
|
2023-10-11 22:42:04 +08:00
|
|
|
],
|
|
|
|
),
|
2024-07-25 06:42:21 +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 }), []),
|
2023-01-13 01:31:19 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
useEventTarget(
|
|
|
|
window,
|
|
|
|
"keyup",
|
|
|
|
useCallback(
|
|
|
|
(event: KeyboardEvent) => {
|
2023-04-20 03:54:39 +08:00
|
|
|
if (focusElement.current === null) return;
|
2023-04-20 03:51:44 +08:00
|
|
|
if (!mayReceiveKeyEvents(focusElement.current)) return;
|
2023-01-13 01:31:19 +08:00
|
|
|
|
|
|
|
if (event.key === " ") {
|
2023-04-20 03:17:32 +08:00
|
|
|
spacebarHeld.current = false;
|
2023-01-13 01:31:19 +08:00
|
|
|
setMicrophoneMuted(true);
|
|
|
|
}
|
|
|
|
},
|
2023-10-11 22:42:04 +08:00
|
|
|
[focusElement, setMicrophoneMuted],
|
|
|
|
),
|
2023-01-13 01:31:19 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
useEventTarget(
|
|
|
|
window,
|
|
|
|
"blur",
|
|
|
|
useCallback(() => {
|
2023-04-21 17:18:43 +08:00
|
|
|
if (spacebarHeld.current) {
|
2023-04-20 03:17:32 +08:00
|
|
|
spacebarHeld.current = false;
|
2023-01-13 01:31:19 +08:00
|
|
|
setMicrophoneMuted(true);
|
|
|
|
}
|
2023-10-11 22:42:04 +08:00
|
|
|
}, [setMicrophoneMuted, spacebarHeld]),
|
2023-01-13 01:31:19 +08:00
|
|
|
);
|
|
|
|
}
|