2023-01-13 01:31:19 +08:00
|
|
|
/*
|
|
|
|
Copyright 2022-2023 New Vector Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-04-20 03:51:44 +08:00
|
|
|
import { RefObject, useCallback, 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
|
|
|
|
* element (specifically, if an ancestor or descendant 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) || e.contains(focusedElement))
|
|
|
|
);
|
|
|
|
};
|
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,
|
|
|
|
setMicrophoneMuted: (muted: boolean) => 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") {
|
|
|
|
toggleMicrophoneMuted();
|
|
|
|
} else if (event.key == "v") {
|
|
|
|
toggleLocalVideoMuted();
|
2023-04-20 03:17:32 +08:00
|
|
|
} else if (event.key === " " && !spacebarHeld.current) {
|
|
|
|
spacebarHeld.current = true;
|
2023-01-13 01:31:19 +08:00
|
|
|
setMicrophoneMuted(false);
|
|
|
|
}
|
|
|
|
},
|
2023-04-20 03:55:55 +08:00
|
|
|
[
|
|
|
|
focusElement,
|
|
|
|
toggleLocalVideoMuted,
|
|
|
|
toggleMicrophoneMuted,
|
|
|
|
setMicrophoneMuted,
|
|
|
|
]
|
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-04-20 03:55:55 +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-04-20 03:17:32 +08:00
|
|
|
}, [setMicrophoneMuted, spacebarHeld])
|
2023-01-13 01:31:19 +08:00
|
|
|
);
|
|
|
|
}
|