2021-08-03 10:40:37 +00:00
|
|
|
/*
|
2022-06-02 18:11:28 +00:00
|
|
|
Copyright 2021-2022 New Vector Ltd
|
2021-08-03 10:40:37 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2021-08-03 12:15:14 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import React, { createRef } from "react";
|
2021-10-22 22:23:32 +00:00
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
import UIStore, { UI_EVENTS } from "../../../stores/UIStore";
|
|
|
|
import { lerp } from "../../../utils/AnimationUtils";
|
|
|
|
import { MarkedExecution } from "../../../utils/MarkedExecution";
|
2021-08-03 10:40:37 +00:00
|
|
|
|
|
|
|
const PIP_VIEW_WIDTH = 336;
|
|
|
|
const PIP_VIEW_HEIGHT = 232;
|
|
|
|
|
|
|
|
const MOVING_AMT = 0.2;
|
2021-08-06 11:25:27 +00:00
|
|
|
const SNAPPING_AMT = 0.1;
|
2021-08-03 10:40:37 +00:00
|
|
|
|
|
|
|
const PADDING = {
|
2021-08-03 12:03:35 +00:00
|
|
|
top: 58,
|
|
|
|
bottom: 58,
|
|
|
|
left: 76,
|
|
|
|
right: 8,
|
2021-08-03 10:40:37 +00:00
|
|
|
};
|
|
|
|
|
2022-11-22 06:57:38 +00:00
|
|
|
/**
|
|
|
|
* The type of a callback which will create the pip content children.
|
|
|
|
*/
|
|
|
|
export type CreatePipChildren = (options: IChildrenOptions) => JSX.Element;
|
|
|
|
|
2021-08-06 14:29:23 +00:00
|
|
|
interface IChildrenOptions {
|
2022-11-22 06:57:38 +00:00
|
|
|
// a callback which is called when a mouse event (most likely mouse down) occurs at start of moving the pip around
|
2021-08-06 14:29:23 +00:00
|
|
|
onStartMoving: (event: React.MouseEvent<Element, MouseEvent>) => void;
|
2022-11-22 06:57:38 +00:00
|
|
|
// a callback which is called when the content fo the pip changes in a way that is likely to cause a resize
|
2021-08-06 14:43:20 +00:00
|
|
|
onResize: (event: Event) => void;
|
2021-08-06 14:29:23 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 10:40:37 +00:00
|
|
|
interface IProps {
|
2021-08-03 12:03:35 +00:00
|
|
|
className?: string;
|
2022-12-28 13:43:44 +00:00
|
|
|
children: Array<CreatePipChildren>;
|
2021-08-03 12:03:35 +00:00
|
|
|
draggable: boolean;
|
2021-11-17 12:49:43 +00:00
|
|
|
onDoubleClick?: () => void;
|
2022-06-02 18:11:28 +00:00
|
|
|
onMove?: () => void;
|
2021-08-03 10:40:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* PictureInPictureDragger shows a small version of CallView hovering over the UI in 'picture-in-picture'
|
|
|
|
* (PiP mode). It displays the call(s) which is *not* in the room the user is currently viewing.
|
|
|
|
*/
|
2022-06-02 18:11:28 +00:00
|
|
|
export default class PictureInPictureDragger extends React.Component<IProps> {
|
2021-08-03 12:03:35 +00:00
|
|
|
private callViewWrapper = createRef<HTMLDivElement>();
|
|
|
|
private initX = 0;
|
|
|
|
private initY = 0;
|
|
|
|
private desiredTranslationX = UIStore.instance.windowWidth - PADDING.right - PIP_VIEW_WIDTH;
|
2021-08-07 07:31:32 +00:00
|
|
|
private desiredTranslationY = UIStore.instance.windowHeight - PADDING.bottom - PIP_VIEW_HEIGHT;
|
2022-06-02 18:11:28 +00:00
|
|
|
private translationX = this.desiredTranslationX;
|
|
|
|
private translationY = this.desiredTranslationY;
|
2021-08-03 12:03:35 +00:00
|
|
|
private moving = false;
|
|
|
|
private scheduledUpdate = new MarkedExecution(
|
|
|
|
() => this.animationCallback(),
|
|
|
|
() => requestAnimationFrame(() => this.scheduledUpdate.trigger()),
|
|
|
|
);
|
|
|
|
|
|
|
|
public componentDidMount() {
|
|
|
|
document.addEventListener("mousemove", this.onMoving);
|
|
|
|
document.addEventListener("mouseup", this.onEndMoving);
|
2022-06-02 18:11:28 +00:00
|
|
|
UIStore.instance.on(UI_EVENTS.Resize, this.onResize);
|
2022-11-10 08:38:48 +00:00
|
|
|
// correctly position the PiP
|
|
|
|
this.snap();
|
2021-08-03 12:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
document.removeEventListener("mousemove", this.onMoving);
|
|
|
|
document.removeEventListener("mouseup", this.onEndMoving);
|
2022-06-02 18:11:28 +00:00
|
|
|
UIStore.instance.off(UI_EVENTS.Resize, this.onResize);
|
2021-08-03 12:03:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 13:06:26 +00:00
|
|
|
public componentDidUpdate(prevProps: Readonly<IProps>): void {
|
|
|
|
if (prevProps.children !== this.props.children) this.snap(true);
|
|
|
|
}
|
|
|
|
|
2021-08-03 12:03:35 +00:00
|
|
|
private animationCallback = () => {
|
|
|
|
if (
|
|
|
|
!this.moving &&
|
2022-06-02 18:11:28 +00:00
|
|
|
Math.abs(this.translationX - this.desiredTranslationX) <= 1 &&
|
|
|
|
Math.abs(this.translationY - this.desiredTranslationY) <= 1
|
|
|
|
) {
|
|
|
|
// Break the loop by settling the element into its final position
|
|
|
|
this.translationX = this.desiredTranslationX;
|
|
|
|
this.translationY = this.desiredTranslationY;
|
|
|
|
this.setStyle();
|
|
|
|
} else {
|
|
|
|
const amt = this.moving ? MOVING_AMT : SNAPPING_AMT;
|
|
|
|
this.translationX = lerp(this.translationX, this.desiredTranslationX, amt);
|
|
|
|
this.translationY = lerp(this.translationY, this.desiredTranslationY, amt);
|
|
|
|
|
|
|
|
this.setStyle();
|
|
|
|
this.scheduledUpdate.mark();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.onMove?.();
|
|
|
|
};
|
|
|
|
|
|
|
|
private setStyle = () => {
|
|
|
|
if (!this.callViewWrapper.current) return;
|
|
|
|
// Set the element's style directly, bypassing React for efficiency
|
2022-12-12 11:24:14 +00:00
|
|
|
this.callViewWrapper.current.style.transform = `translateX(${this.translationX}px) translateY(${this.translationY}px)`;
|
2021-08-03 12:03:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
private setTranslation(inTranslationX: number, inTranslationY: number) {
|
|
|
|
const width = this.callViewWrapper.current?.clientWidth || PIP_VIEW_WIDTH;
|
|
|
|
const height = this.callViewWrapper.current?.clientHeight || PIP_VIEW_HEIGHT;
|
|
|
|
|
|
|
|
// Avoid overflow on the x axis
|
|
|
|
if (inTranslationX + width >= UIStore.instance.windowWidth) {
|
|
|
|
this.desiredTranslationX = UIStore.instance.windowWidth - width;
|
|
|
|
} else if (inTranslationX <= 0) {
|
|
|
|
this.desiredTranslationX = 0;
|
|
|
|
} else {
|
|
|
|
this.desiredTranslationX = inTranslationX;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid overflow on the y axis
|
|
|
|
if (inTranslationY + height >= UIStore.instance.windowHeight) {
|
|
|
|
this.desiredTranslationY = UIStore.instance.windowHeight - height;
|
|
|
|
} else if (inTranslationY <= 0) {
|
|
|
|
this.desiredTranslationY = 0;
|
|
|
|
} else {
|
|
|
|
this.desiredTranslationY = inTranslationY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:29:23 +00:00
|
|
|
private onResize = (): void => {
|
|
|
|
this.snap(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
private snap = (animate = false) => {
|
2021-08-03 12:03:35 +00:00
|
|
|
const translationX = this.desiredTranslationX;
|
|
|
|
const translationY = this.desiredTranslationY;
|
|
|
|
// We subtract the PiP size from the window size in order to calculate
|
|
|
|
// the position to snap to from the PiP center and not its top-left
|
|
|
|
// corner
|
2022-12-12 11:24:14 +00:00
|
|
|
const windowWidth =
|
|
|
|
UIStore.instance.windowWidth - (this.callViewWrapper.current?.clientWidth || PIP_VIEW_WIDTH);
|
|
|
|
const windowHeight =
|
|
|
|
UIStore.instance.windowHeight - (this.callViewWrapper.current?.clientHeight || PIP_VIEW_HEIGHT);
|
2021-08-03 12:03:35 +00:00
|
|
|
|
|
|
|
if (translationX >= windowWidth / 2 && translationY >= windowHeight / 2) {
|
|
|
|
this.desiredTranslationX = windowWidth - PADDING.right;
|
|
|
|
this.desiredTranslationY = windowHeight - PADDING.bottom;
|
|
|
|
} else if (translationX >= windowWidth / 2 && translationY <= windowHeight / 2) {
|
|
|
|
this.desiredTranslationX = windowWidth - PADDING.right;
|
|
|
|
this.desiredTranslationY = PADDING.top;
|
|
|
|
} else if (translationX <= windowWidth / 2 && translationY >= windowHeight / 2) {
|
|
|
|
this.desiredTranslationX = PADDING.left;
|
|
|
|
this.desiredTranslationY = windowHeight - PADDING.bottom;
|
|
|
|
} else {
|
|
|
|
this.desiredTranslationX = PADDING.left;
|
|
|
|
this.desiredTranslationY = PADDING.top;
|
|
|
|
}
|
|
|
|
|
2022-06-02 18:11:28 +00:00
|
|
|
if (!animate) {
|
|
|
|
this.translationX = this.desiredTranslationX;
|
|
|
|
this.translationY = this.desiredTranslationY;
|
|
|
|
}
|
|
|
|
|
2021-08-03 12:03:35 +00:00
|
|
|
// We start animating here because we want the PiP to move when we're
|
|
|
|
// resizing the window
|
|
|
|
this.scheduledUpdate.mark();
|
|
|
|
};
|
|
|
|
|
|
|
|
private onStartMoving = (event: React.MouseEvent | MouseEvent) => {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
this.moving = true;
|
|
|
|
this.initX = event.pageX - this.desiredTranslationX;
|
|
|
|
this.initY = event.pageY - this.desiredTranslationY;
|
|
|
|
this.scheduledUpdate.mark();
|
|
|
|
};
|
|
|
|
|
|
|
|
private onMoving = (event: React.MouseEvent | MouseEvent) => {
|
|
|
|
if (!this.moving) return;
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
this.setTranslation(event.pageX - this.initX, event.pageY - this.initY);
|
|
|
|
};
|
|
|
|
|
|
|
|
private onEndMoving = () => {
|
|
|
|
this.moving = false;
|
2021-08-06 14:29:23 +00:00
|
|
|
this.snap(true);
|
2021-08-03 12:03:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
const style = {
|
2022-06-02 18:11:28 +00:00
|
|
|
transform: `translateX(${this.translationX}px) translateY(${this.translationY}px)`,
|
2021-08-03 12:03:35 +00:00
|
|
|
};
|
2022-06-02 18:11:28 +00:00
|
|
|
|
2022-12-28 13:43:44 +00:00
|
|
|
const children = this.props.children.map((create: CreatePipChildren) => {
|
|
|
|
return create({
|
|
|
|
onStartMoving: this.onStartMoving,
|
|
|
|
onResize: this.onResize,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-08-03 12:03:35 +00:00
|
|
|
return (
|
2022-10-07 02:27:28 +00:00
|
|
|
<aside
|
2021-08-03 12:03:35 +00:00
|
|
|
className={this.props.className}
|
2022-06-02 18:11:28 +00:00
|
|
|
style={style}
|
2021-08-03 12:03:35 +00:00
|
|
|
ref={this.callViewWrapper}
|
2021-11-17 12:49:43 +00:00
|
|
|
onDoubleClick={this.props.onDoubleClick}
|
2021-08-03 12:03:35 +00:00
|
|
|
>
|
2022-12-28 13:43:44 +00:00
|
|
|
{children}
|
2022-10-07 02:27:28 +00:00
|
|
|
</aside>
|
2021-08-03 12:03:35 +00:00
|
|
|
);
|
|
|
|
}
|
2021-08-03 10:40:37 +00:00
|
|
|
}
|