2022-08-07 19:05:49 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 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.
|
|
|
|
*/
|
|
|
|
|
2022-08-19 17:16:57 +02:00
|
|
|
import { useCallback, useEffect, useState } from "react";
|
2022-08-07 19:05:49 +02:00
|
|
|
|
2022-10-21 17:24:56 +01:00
|
|
|
import { TileDescriptor } from "../room/InCallView";
|
2022-08-07 19:05:49 +02:00
|
|
|
import { useEventTarget } from "../useEvents";
|
2022-08-19 17:16:57 +02:00
|
|
|
import { useCallFeed } from "./useCallFeed";
|
2022-08-07 19:05:49 +02:00
|
|
|
|
|
|
|
export function useFullscreen(ref: React.RefObject<HTMLElement>): {
|
2022-10-21 17:24:56 +01:00
|
|
|
toggleFullscreen: (participant: TileDescriptor) => void;
|
|
|
|
fullscreenParticipant: TileDescriptor | null;
|
2022-08-07 19:05:49 +02:00
|
|
|
} {
|
|
|
|
const [fullscreenParticipant, setFullscreenParticipant] =
|
2022-10-21 17:24:56 +01:00
|
|
|
useState<TileDescriptor | null>(null);
|
2022-08-19 17:16:57 +02:00
|
|
|
const { disposed } = useCallFeed(fullscreenParticipant?.callFeed);
|
2022-08-07 19:05:49 +02:00
|
|
|
|
|
|
|
const toggleFullscreen = useCallback(
|
2022-10-21 17:24:56 +01:00
|
|
|
(tileDes: TileDescriptor) => {
|
2022-08-07 19:05:49 +02:00
|
|
|
if (fullscreenParticipant) {
|
|
|
|
document.exitFullscreen();
|
|
|
|
setFullscreenParticipant(null);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
ref.current.requestFullscreen();
|
2022-10-21 17:24:56 +01:00
|
|
|
setFullscreenParticipant(tileDes);
|
2022-08-07 19:05:49 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.warn("Failed to fullscreen:", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[fullscreenParticipant, setFullscreenParticipant, ref]
|
|
|
|
);
|
|
|
|
|
|
|
|
const onFullscreenChanged = useCallback(() => {
|
|
|
|
if (!document.fullscreenElement) {
|
|
|
|
setFullscreenParticipant(null);
|
|
|
|
}
|
|
|
|
}, [setFullscreenParticipant]);
|
|
|
|
|
|
|
|
useEventTarget(ref.current, "fullscreenchange", onFullscreenChanged);
|
|
|
|
|
2022-08-19 17:16:57 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (disposed) {
|
|
|
|
document.exitFullscreen();
|
|
|
|
setFullscreenParticipant(null);
|
|
|
|
}
|
|
|
|
}, [disposed]);
|
|
|
|
|
2022-08-07 19:05:49 +02:00
|
|
|
return { toggleFullscreen, fullscreenParticipant };
|
|
|
|
}
|