signal-desktop/ts/components/CallingPip.tsx

304 lines
8.4 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2020-10-01 00:43:05 +00:00
import React from 'react';
import { minBy, debounce, noop } from 'lodash';
2020-11-17 15:07:53 +00:00
import { CallingPipRemoteVideo } from './CallingPipRemoteVideo';
import { LocalizerType } from '../types/Util';
import { GroupCallVideoRequest, VideoFrameSource } from '../types/Calling';
2020-10-01 00:43:05 +00:00
import {
ActiveCallType,
2020-10-01 00:43:05 +00:00
HangUpType,
SetLocalPreviewType,
SetRendererCanvasType,
} from '../state/ducks/calling';
import { missingCaseError } from '../util/missingCaseError';
enum PositionMode {
BeingDragged,
SnapToBottom,
SnapToLeft,
SnapToRight,
SnapToTop,
}
type PositionState =
| {
mode: PositionMode.BeingDragged;
mouseX: number;
mouseY: number;
dragOffsetX: number;
dragOffsetY: number;
}
| {
mode: PositionMode.SnapToLeft | PositionMode.SnapToRight;
offsetY: number;
}
| {
mode: PositionMode.SnapToTop | PositionMode.SnapToBottom;
offsetX: number;
};
interface SnapCandidate {
mode:
| PositionMode.SnapToBottom
| PositionMode.SnapToLeft
| PositionMode.SnapToRight
| PositionMode.SnapToTop;
distanceToEdge: number;
}
2020-10-01 00:43:05 +00:00
export type PropsType = {
activeCall: ActiveCallType;
2020-11-17 15:07:53 +00:00
getGroupCallVideoFrameSource: (demuxId: number) => VideoFrameSource;
2020-10-01 00:43:05 +00:00
hangUp: (_: HangUpType) => void;
hasLocalVideo: boolean;
i18n: LocalizerType;
setGroupCallVideoRequest: (_: Array<GroupCallVideoRequest>) => void;
2020-10-01 00:43:05 +00:00
setLocalPreview: (_: SetLocalPreviewType) => void;
setRendererCanvas: (_: SetRendererCanvasType) => void;
togglePip: () => void;
};
const PIP_HEIGHT = 156;
const PIP_WIDTH = 120;
const PIP_TOP_MARGIN = 56;
2020-10-01 00:43:05 +00:00
const PIP_PADDING = 8;
export const CallingPip = ({
activeCall,
2020-11-17 15:07:53 +00:00
getGroupCallVideoFrameSource,
2020-10-01 00:43:05 +00:00
hangUp,
hasLocalVideo,
i18n,
setGroupCallVideoRequest,
2020-10-01 00:43:05 +00:00
setLocalPreview,
setRendererCanvas,
togglePip,
}: PropsType): JSX.Element | null => {
const videoContainerRef = React.useRef<null | HTMLDivElement>(null);
2020-10-01 00:43:05 +00:00
const localVideoRef = React.useRef(null);
const [windowWidth, setWindowWidth] = React.useState(window.innerWidth);
const [windowHeight, setWindowHeight] = React.useState(window.innerHeight);
const [positionState, setPositionState] = React.useState<PositionState>({
mode: PositionMode.SnapToRight,
2020-10-01 00:43:05 +00:00
offsetY: 0,
});
React.useEffect(() => {
setLocalPreview({ element: localVideoRef });
2020-11-17 15:07:53 +00:00
}, [setLocalPreview]);
2020-10-01 00:43:05 +00:00
const handleMouseMove = React.useCallback(
(ev: MouseEvent) => {
if (positionState.mode === PositionMode.BeingDragged) {
setPositionState(oldState => ({
...oldState,
mouseX: ev.screenX,
mouseY: ev.screenY,
}));
2020-10-01 00:43:05 +00:00
}
},
[positionState]
2020-10-01 00:43:05 +00:00
);
const handleMouseUp = React.useCallback(() => {
if (positionState.mode === PositionMode.BeingDragged) {
const { mouseX, mouseY, dragOffsetX, dragOffsetY } = positionState;
2020-10-01 00:43:05 +00:00
const { innerHeight, innerWidth } = window;
const offsetX = mouseX - dragOffsetX;
const offsetY = mouseY - dragOffsetY;
2020-10-01 00:43:05 +00:00
const snapCandidates: Array<SnapCandidate> = [
{
mode: PositionMode.SnapToLeft,
distanceToEdge: offsetX,
},
{
mode: PositionMode.SnapToRight,
distanceToEdge: innerWidth - (offsetX + PIP_WIDTH),
},
{
mode: PositionMode.SnapToTop,
distanceToEdge: offsetY - PIP_TOP_MARGIN,
},
{
mode: PositionMode.SnapToBottom,
distanceToEdge: innerHeight - (offsetY + PIP_HEIGHT),
},
];
2020-10-01 00:43:05 +00:00
// This fallback is mostly for TypeScript, because `minBy` says it can return
// `undefined`.
const snapTo =
minBy(snapCandidates, candidate => candidate.distanceToEdge) ||
snapCandidates[0];
switch (snapTo.mode) {
case PositionMode.SnapToLeft:
case PositionMode.SnapToRight:
setPositionState({
mode: snapTo.mode,
offsetY,
});
break;
case PositionMode.SnapToTop:
case PositionMode.SnapToBottom:
setPositionState({
mode: snapTo.mode,
offsetX,
});
break;
default:
throw missingCaseError(snapTo.mode);
}
2020-10-01 00:43:05 +00:00
}
}, [positionState, setPositionState]);
2020-10-01 00:43:05 +00:00
React.useEffect(() => {
if (positionState.mode === PositionMode.BeingDragged) {
2020-10-01 00:43:05 +00:00
document.addEventListener('mousemove', handleMouseMove, false);
document.addEventListener('mouseup', handleMouseUp, false);
return () => {
document.removeEventListener('mouseup', handleMouseUp, false);
document.removeEventListener('mousemove', handleMouseMove, false);
};
2020-10-01 00:43:05 +00:00
}
return noop;
}, [positionState.mode, handleMouseMove, handleMouseUp]);
React.useEffect(() => {
const handleWindowResize = debounce(
() => {
setWindowWidth(window.innerWidth);
setWindowHeight(window.innerHeight);
},
100,
{
maxWait: 3000,
}
);
2020-10-01 00:43:05 +00:00
window.addEventListener('resize', handleWindowResize, false);
2020-10-01 00:43:05 +00:00
return () => {
window.removeEventListener('resize', handleWindowResize, false);
2020-10-01 00:43:05 +00:00
};
}, []);
const [translateX, translateY] = React.useMemo<[number, number]>(() => {
switch (positionState.mode) {
case PositionMode.BeingDragged:
return [
positionState.mouseX - positionState.dragOffsetX,
positionState.mouseY - positionState.dragOffsetY,
];
case PositionMode.SnapToLeft:
return [
PIP_PADDING,
Math.min(
PIP_TOP_MARGIN + positionState.offsetY,
windowHeight - PIP_PADDING - PIP_HEIGHT
),
];
case PositionMode.SnapToRight:
return [
windowWidth - PIP_PADDING - PIP_WIDTH,
Math.min(
PIP_TOP_MARGIN + positionState.offsetY,
windowHeight - PIP_PADDING - PIP_HEIGHT
),
];
case PositionMode.SnapToTop:
return [
Math.min(
positionState.offsetX,
windowWidth - PIP_PADDING - PIP_WIDTH
),
PIP_TOP_MARGIN + PIP_PADDING,
];
case PositionMode.SnapToBottom:
return [
Math.min(
positionState.offsetX,
windowWidth - PIP_PADDING - PIP_WIDTH
),
windowHeight - PIP_PADDING - PIP_HEIGHT,
];
default:
throw missingCaseError(positionState);
}
}, [windowWidth, windowHeight, positionState]);
2020-10-01 00:43:05 +00:00
return (
// eslint-disable-next-line jsx-a11y/no-static-element-interactions
<div
className="module-calling-pip"
onMouseDown={ev => {
const node = videoContainerRef.current;
if (!node) {
return;
}
const rect = node.getBoundingClientRect();
const dragOffsetX = ev.screenX - rect.left;
const dragOffsetY = ev.screenY - rect.top;
2020-10-01 00:43:05 +00:00
setPositionState({
mode: PositionMode.BeingDragged,
mouseX: ev.screenX,
mouseY: ev.screenY,
dragOffsetX,
dragOffsetY,
2020-10-01 00:43:05 +00:00
});
}}
ref={videoContainerRef}
style={{
cursor:
positionState.mode === PositionMode.BeingDragged
? '-webkit-grabbing'
: '-webkit-grab',
transform: `translate3d(${translateX}px,${translateY}px, 0)`,
transition:
positionState.mode === PositionMode.BeingDragged
? 'none'
: 'transform ease-out 300ms',
2020-10-01 00:43:05 +00:00
}}
>
2020-11-17 15:07:53 +00:00
<CallingPipRemoteVideo
activeCall={activeCall}
2020-11-17 15:07:53 +00:00
getGroupCallVideoFrameSource={getGroupCallVideoFrameSource}
i18n={i18n}
setRendererCanvas={setRendererCanvas}
setGroupCallVideoRequest={setGroupCallVideoRequest}
2020-11-17 15:07:53 +00:00
/>
2020-10-01 00:43:05 +00:00
{hasLocalVideo ? (
<video
className="module-calling-pip__video--local"
ref={localVideoRef}
autoPlay
/>
) : null}
<div className="module-calling-pip__actions">
<button
aria-label={i18n('calling__hangup')}
className="module-calling-pip__button--hangup"
onClick={() => {
hangUp({ conversationId: activeCall.conversation.id });
2020-10-01 00:43:05 +00:00
}}
type="button"
2020-11-19 18:11:35 +00:00
/>
<button
aria-label={i18n('calling__pip--off')}
className="module-calling-pip__button--pip"
onClick={togglePip}
type="button"
>
<div />
</button>
2020-10-01 00:43:05 +00:00
</div>
</div>
);
};