2024-05-18 04:38:00 +08:00
|
|
|
/*
|
2024-09-06 16:22:13 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2024-05-18 04:38:00 +08:00
|
|
|
|
2024-09-06 16:22:13 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
Please see LICENSE in the repository root for full details.
|
2024-05-18 04:38:00 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
import { BehaviorSubject, Observable } from "rxjs";
|
2024-05-31 01:06:24 +08:00
|
|
|
import { ComponentType } from "react";
|
2024-05-18 04:38:00 +08:00
|
|
|
|
2024-06-08 05:29:48 +08:00
|
|
|
import { MediaViewModel, UserMediaViewModel } from "../state/MediaViewModel";
|
2024-05-31 01:06:24 +08:00
|
|
|
import { LayoutProps } from "./Grid";
|
2024-05-18 04:38:00 +08:00
|
|
|
|
|
|
|
export interface Bounds {
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
}
|
|
|
|
|
2024-06-08 04:59:56 +08:00
|
|
|
export interface Alignment {
|
|
|
|
inline: "start" | "end";
|
|
|
|
block: "start" | "end";
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultSpotlightAlignment: Alignment = {
|
|
|
|
inline: "end",
|
|
|
|
block: "end",
|
|
|
|
};
|
|
|
|
export const defaultPipAlignment: Alignment = { inline: "end", block: "start" };
|
|
|
|
|
2024-05-18 04:38:00 +08:00
|
|
|
export interface CallLayoutInputs {
|
|
|
|
/**
|
|
|
|
* The minimum bounds of the layout area.
|
|
|
|
*/
|
|
|
|
minBounds: Observable<Bounds>;
|
|
|
|
/**
|
2024-06-08 04:59:56 +08:00
|
|
|
* The alignment of the floating spotlight tile, if present.
|
|
|
|
*/
|
|
|
|
spotlightAlignment: BehaviorSubject<Alignment>;
|
|
|
|
/**
|
|
|
|
* The alignment of the small picture-in-picture tile, if present.
|
2024-05-18 04:38:00 +08:00
|
|
|
*/
|
2024-06-08 04:59:56 +08:00
|
|
|
pipAlignment: BehaviorSubject<Alignment>;
|
2024-05-18 04:38:00 +08:00
|
|
|
}
|
|
|
|
|
2024-05-31 01:06:24 +08:00
|
|
|
export interface GridTileModel {
|
|
|
|
type: "grid";
|
2024-06-08 05:29:48 +08:00
|
|
|
vm: UserMediaViewModel;
|
2024-05-31 01:06:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface SpotlightTileModel {
|
|
|
|
type: "spotlight";
|
|
|
|
vms: MediaViewModel[];
|
|
|
|
maximised: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type TileModel = GridTileModel | SpotlightTileModel;
|
|
|
|
|
2024-05-18 04:38:00 +08:00
|
|
|
export interface CallLayoutOutputs<Model> {
|
2024-07-04 03:08:30 +08:00
|
|
|
/**
|
|
|
|
* Whether the scrolling layer of the layout should appear on top.
|
|
|
|
*/
|
|
|
|
scrollingOnTop: boolean;
|
2024-05-18 04:38:00 +08:00
|
|
|
/**
|
|
|
|
* The visually fixed (non-scrolling) layer of the layout.
|
|
|
|
*/
|
2024-05-31 01:06:24 +08:00
|
|
|
fixed: ComponentType<LayoutProps<Model, TileModel, HTMLDivElement>>;
|
2024-05-18 04:38:00 +08:00
|
|
|
/**
|
|
|
|
* The layer of the layout that can overflow and be scrolled.
|
|
|
|
*/
|
2024-05-31 01:06:24 +08:00
|
|
|
scrolling: ComponentType<LayoutProps<Model, TileModel, HTMLDivElement>>;
|
2024-05-18 04:38:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A layout system for media tiles.
|
|
|
|
*/
|
|
|
|
export type CallLayout<Model> = (
|
|
|
|
inputs: CallLayoutInputs,
|
|
|
|
) => CallLayoutOutputs<Model>;
|
2024-06-08 04:59:56 +08:00
|
|
|
|
|
|
|
export interface GridArrangement {
|
|
|
|
tileWidth: number;
|
|
|
|
tileHeight: number;
|
|
|
|
gap: number;
|
|
|
|
columns: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const tileMaxAspectRatio = 17 / 9;
|
|
|
|
const tileMinAspectRatio = 4 / 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine the ideal arrangement of tiles into a grid of a particular size.
|
|
|
|
*/
|
|
|
|
export function arrangeTiles(
|
|
|
|
width: number,
|
|
|
|
minHeight: number,
|
|
|
|
tileCount: number,
|
|
|
|
): GridArrangement {
|
|
|
|
// The goal here is to determine the grid size and padding that maximizes
|
|
|
|
// use of screen space for n tiles without making those tiles too small or
|
|
|
|
// too cropped (having an extreme aspect ratio)
|
|
|
|
const gap = width < 800 ? 16 : 20;
|
2024-07-26 18:50:44 +08:00
|
|
|
const area = width * minHeight;
|
|
|
|
// Magic numbers that make tiles scale up nicely as the window gets larger
|
|
|
|
const tileArea = Math.pow(Math.sqrt(area) / 8 + 125, 2);
|
|
|
|
const tilesPerPage = Math.min(tileCount, area / tileArea);
|
2024-06-08 04:59:56 +08:00
|
|
|
|
2024-08-09 00:46:38 +08:00
|
|
|
let columns = Math.min(
|
2024-06-08 04:59:56 +08:00
|
|
|
// Don't create more columns than we have items for
|
2024-07-26 18:50:44 +08:00
|
|
|
tilesPerPage,
|
2024-06-08 04:59:56 +08:00
|
|
|
// The ideal number of columns is given by a packing of equally-sized
|
|
|
|
// squares into a grid.
|
|
|
|
// width / column = height / row.
|
|
|
|
// columns * rows = number of squares.
|
|
|
|
// ∴ columns = sqrt(width / height * number of squares).
|
|
|
|
// Except we actually want 16:9-ish tiles rather than squares, so we
|
|
|
|
// divide the width-to-height ratio by the target aspect ratio.
|
2024-07-26 18:50:44 +08:00
|
|
|
Math.round(
|
|
|
|
Math.sqrt((width / minHeight / tileMinAspectRatio) * tilesPerPage),
|
|
|
|
),
|
2024-06-08 04:59:56 +08:00
|
|
|
);
|
2024-07-26 18:50:44 +08:00
|
|
|
let rows = tilesPerPage / columns;
|
|
|
|
// If all the tiles could fit on one page, we want to ensure that they do by
|
|
|
|
// not leaving fractional rows hanging off the bottom
|
2024-08-09 00:46:38 +08:00
|
|
|
if (tilesPerPage === tileCount) {
|
|
|
|
rows = Math.ceil(rows);
|
|
|
|
// We may now be able to fit the tiles into fewer columns
|
|
|
|
columns = Math.ceil(tileCount / rows);
|
|
|
|
}
|
2024-06-08 04:59:56 +08:00
|
|
|
|
2024-07-04 03:08:30 +08:00
|
|
|
let tileWidth = (width - (columns + 1) * gap) / columns;
|
2024-06-08 04:59:56 +08:00
|
|
|
let tileHeight = (minHeight - (rows - 1) * gap) / rows;
|
|
|
|
|
|
|
|
// Impose a minimum and maximum aspect ratio on the tiles
|
|
|
|
const tileAspectRatio = tileWidth / tileHeight;
|
|
|
|
if (tileAspectRatio > tileMaxAspectRatio)
|
|
|
|
tileWidth = tileHeight * tileMaxAspectRatio;
|
2024-08-02 01:49:09 +08:00
|
|
|
else if (tileAspectRatio < tileMinAspectRatio)
|
|
|
|
tileHeight = tileWidth / tileMinAspectRatio;
|
2024-06-08 04:59:56 +08:00
|
|
|
|
|
|
|
return { tileWidth, tileHeight, gap, columns };
|
|
|
|
}
|