Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

chatCanvasLayout.ts

183 lines6.0 KB

1import {
2 clampPreviewMiniPlayerPosition,
3 resolvePreviewMiniPlayerFrame,
4 type PreviewMiniPlayerFrame,
5 type PreviewMiniPlayerObstacles,
6} from "../preview/previewMiniPlayerLayout";
7import type {
8 PreviewMiniPlayerPosition,
9 PreviewMiniPlayerSize,
10 PreviewMiniPlayerState,
11} from "~/previewMiniPlayerStore";
12​
13export interface ChatCanvasPreview {
14 readonly key: string;
15 readonly width: number | null;
16 readonly position: PreviewMiniPlayerPosition | null;
17 readonly source: PreviewMiniPlayerSize;
18 readonly lastInteraction?: PreviewMiniPlayerState["lastInteraction"];
20​
21const GAP = 12;
22export const DETAILS_CARD_CLEARANCE = 32;
23​
24export function resolveChatCanvasLayout({
25 container,
26 preview,
27 padding = 48,
28 maxChatWidth = 768,
29 minChatWidth = 640,
30 composerHeight = 0,
31 detailsCard = null,
32 findBar = null,
33}: {
34 container: PreviewMiniPlayerSize;
35 preview: ChatCanvasPreview | null;
36 padding?: number;
37 maxChatWidth?: number;
38 minChatWidth?: number;
39 composerHeight?: number;
40 detailsCard?: PreviewMiniPlayerObstacles["detailsCard"];
41 findBar?: PreviewMiniPlayerObstacles["detailsCard"];
42}) {
43 const centeredWidth = Math.max(0, Math.min(maxChatWidth, container.width - padding * 2));
44 const laneRight = detailsCard
45 ? detailsCard.left - DETAILS_CARD_CLEARANCE
46 : container.width - padding;
47 const normalWidth = Math.max(0, Math.min(centeredWidth, laneRight - padding));
48 const normalLeft = Math.max(
49 padding,
50 Math.min((container.width - normalWidth) / 2, laneRight - normalWidth),
51 );
52 let chat = {
53 left: normalLeft,
54 width: normalWidth,
55 insetStart: 0,
56 insetEnd: Math.max(0, container.width - normalLeft * 2 - normalWidth),
57 };
58 let frame: PreviewMiniPlayerFrame | null = null;
59 let overlapsChat = false;
60 if (preview && container.width > 0 && container.height > 0) {
61 frame = resolvePreviewMiniPlayerFrame({ ...preview, container });
62 // Reserve a readable chat lane only while it leaves room to drag. Otherwise
63 // the container's right edge would pin the preview at every pointer position.
64 const minimumPreviewX =
65 preview.lastInteraction === "resize" ||
66 container.width <= padding + minChatWidth + frame.width + GAP * 2
67 ? GAP
68 : padding + minChatWidth + GAP;
69 if (preview.position === null) frame = { ...frame, y: container.height - frame.height - GAP };
70 if (preview.lastInteraction === "resize" && composerHeight > 0) {
71 const laneWidth = Math.min(normalWidth, minChatWidth);
72 const transitionWidth = Math.max(GAP, normalWidth - laneWidth);
73 const lift = Math.min(
74 1,
75 Math.max(0, (padding + normalWidth + GAP - frame.x) / transitionWidth),
76 );
77 if (lift > 0) {
78 frame = resolvePreviewMiniPlayerFrame({
79 width: frame.width,
80 position: frame,
81 source: preview.source,
82 container: {
83 ...container,
84 height: Math.max(GAP * 2 + 1, container.height - composerHeight * lift),
85 },
86 });
87 }
88 }
89 const findBarObstacles = findBar ? { detailsCard: findBar, composer: null } : undefined;
90 const preferredFrame = {
91 ...frame,
92 ...clampPreviewMiniPlayerPosition(frame, container, frame, findBarObstacles, minimumPreviewX),
93 };
94 const dragObstacle =
95 detailsCard && findBar
96 ? {
97 left: Math.min(detailsCard.left, findBar.left),
98 right: Math.max(detailsCard.right, findBar.right),
99 bottom: Math.max(detailsCard.bottom, findBar.bottom),
100 }
101 : (detailsCard ?? findBar);
102 const cardObstacle = preview.lastInteraction === "resize" ? findBar : dragObstacle;
103 frame = {
104 ...frame,
105 ...clampPreviewMiniPlayerPosition(
106 frame,
107 container,
108 frame,
109 { detailsCard: cardObstacle, composer: null },
110 minimumPreviewX,
111 ),
112 };
113 const chatBeside = (player: PreviewMiniPlayerFrame) => {
114 const normalRight = normalLeft + normalWidth;
115 if (player.x >= normalRight + GAP) return chat;
116 const width = Math.min(maxChatWidth, player.x - GAP - padding);
117 if (width < minChatWidth) return null;
118 const left = Math.min(normalLeft, player.x - GAP - width);
119 return {
120 left,
121 width,
122 insetStart: 0,
123 insetEnd: Math.max(0, container.width - left * 2 - width),
124 };
125 };
126 let nextChat = chatBeside(frame);
127 if (!nextChat && detailsCard) {
128 nextChat = chatBeside(preferredFrame);
129 if (nextChat) frame = preferredFrame;
130 }
131 if (nextChat) chat = nextChat;
132 else overlapsChat = true;
133 if (overlapsChat && preview.lastInteraction === "resize") {
134 const width = Math.min(normalWidth, minChatWidth);
135 chat = {
136 left: padding,
137 width,
138 insetStart: 0,
139 insetEnd: Math.max(0, container.width - padding * 2 - width),
140 };
141 } else if (overlapsChat) {
142 const obstacles = {
143 detailsCard: cardObstacle,
144 composer: { left: chat.left, right: chat.left + chat.width, height: composerHeight },
145 };
146 frame = resolvePreviewMiniPlayerFrame({ ...preview, container, obstacles });
147 frame = {
148 ...frame,
149 ...clampPreviewMiniPlayerPosition(
150 preview.position ?? { x: frame.x, y: container.height - frame.height - GAP },
151 container,
152 frame,
153 obstacles,
154 minimumPreviewX,
155 ),
156 };
157 if (
158 frame.x < obstacles.composer.right &&
159 frame.x + frame.width > obstacles.composer.left &&
160 frame.y + frame.height > container.height - composerHeight
161 ) {
162 frame = {
163 ...frame,
164 ...clampPreviewMiniPlayerPosition(
165 frame,
166 container,
167 frame,
168 { composer: obstacles.composer, detailsCard: findBar },
169 minimumPreviewX,
170 ),
171 };
172 }
173 }
174 }
175 const overlapsDetailsCard = Boolean(
176 frame &&
177 detailsCard &&
178 frame.x < detailsCard.right &&
179 frame.x + frame.width > detailsCard.left &&
180 frame.y < detailsCard.bottom,
181 );
182 return { chat, frame, overlapsChat, overlapsDetailsCard };