Files
element-call/src/grid/OneOnOneLayout.tsx

72 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-06-07 16:59:56 -04:00
/*
Copyright 2024 New Vector Ltd.
2024-06-07 16:59:56 -04:00
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
2024-06-07 16:59:56 -04:00
*/
import { type ReactNode, useCallback, useMemo } from "react";
2024-06-07 16:59:56 -04:00
import { useObservableEagerState } from "observable-hooks";
import classNames from "classnames";
import { type OneOnOneLayout as OneOnOneLayoutModel } from "../state/layout-types.ts";
import { type CallLayout, arrangeTiles } from "./CallLayout";
2024-06-07 16:59:56 -04:00
import styles from "./OneOnOneLayout.module.css";
import { type DragCallback, useUpdateLayout } from "./Grid";
import { useBehavior } from "../useBehavior";
2024-06-07 16:59:56 -04:00
2024-07-18 11:33:20 -04:00
/**
* An implementation of the "one-on-one" layout, in which the remote participant
* is shown at maximum size, overlaid by a small view of the local participant.
*/
2024-06-07 16:59:56 -04:00
export const makeOneOnOneLayout: CallLayout<OneOnOneLayoutModel> = ({
minBounds$,
pipAlignment$,
2024-06-07 16:59:56 -04:00
}) => ({
scrollingOnTop: false,
2024-06-07 16:59:56 -04:00
fixed: function OneOnOneLayoutFixed({ ref }): ReactNode {
2024-07-25 12:50:28 -04:00
useUpdateLayout();
return <div ref={ref} />;
},
2024-06-07 16:59:56 -04:00
scrolling: function OneOnOneLayoutScrolling({ ref, model, Slot }): ReactNode {
2024-07-25 12:50:28 -04:00
useUpdateLayout();
const { width, height } = useObservableEagerState(minBounds$);
const pipAlignmentValue = useBehavior(pipAlignment$);
2024-06-07 16:59:56 -04:00
const { tileWidth, tileHeight } = useMemo(
() => arrangeTiles(width, height, 1),
[width, height],
);
const onDragLocalTile: DragCallback = useCallback(
({ xRatio, yRatio }) =>
pipAlignment$.next({
2024-06-07 16:59:56 -04:00
block: yRatio < 0.5 ? "start" : "end",
inline: xRatio < 0.5 ? "start" : "end",
}),
[],
);
return (
<div ref={ref} className={styles.layer}>
2024-06-07 16:59:56 -04:00
<Slot
id={model.spotlight.id}
model={model.spotlight}
2024-06-07 16:59:56 -04:00
className={styles.container}
style={{ width: tileWidth, height: tileHeight }}
>
<Slot
className={classNames(styles.slot, styles.local)}
id={model.pip.id}
model={model.pip}
2024-06-07 16:59:56 -04:00
onDrag={onDragLocalTile}
data-block-alignment={pipAlignmentValue.block}
data-inline-alignment={pipAlignmentValue.inline}
/>
</Slot>
</div>
);
},
2024-06-07 16:59:56 -04:00
});