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

chatCanvasLayout.ts

195 lines6.5 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 canvasCenterLeft = (container.width - normalWidth) / 2;
49 // Beside a card it would crowd, the chat centers between the card and the canvas edge. The shift
50 // eases to zero as clearance grows, so very wide windows keep the chat still and a resize
51 // never jumps it.
52 const besideShift = detailsCard
53 ? Math.min(
54 (container.width - detailsCard.left) / 2,
55 Math.max(
56 0,
57 (container.width - detailsCard.left) / 2 - (laneRight - canvasCenterLeft - normalWidth),
58 ),
59 )
60 : 0;
61 const normalLeft = Math.max(
62 padding,
63 Math.min(canvasCenterLeft - besideShift, laneRight - normalWidth),
64 );
65 let chat = {
66 left: normalLeft,
67 width: normalWidth,
68 insetStart: 0,
69 insetEnd: Math.max(0, container.width - normalLeft * 2 - normalWidth),
70 };
71 let frame: PreviewMiniPlayerFrame | null = null;
72 let overlapsChat = false;
73 if (preview && container.width > 0 && container.height > 0) {
74 frame = resolvePreviewMiniPlayerFrame({ ...preview, container });
75 // Reserve a chat lane only if the preview still has room to drag.
76 const minimumPreviewX =
77 preview.lastInteraction === "resize" ||
78 container.width <= padding + minChatWidth + frame.width + GAP * 2
79 ? GAP
80 : padding + minChatWidth + GAP;
81 if (preview.position === null) frame = { ...frame, y: container.height - frame.height - GAP };
82 if (preview.lastInteraction === "resize" && composerHeight > 0) {
83 const laneWidth = Math.min(normalWidth, minChatWidth);
84 const transitionWidth = Math.max(GAP, normalWidth - laneWidth);
85 const lift = Math.min(
86 1,
87 Math.max(0, (padding + normalWidth + GAP - frame.x) / transitionWidth),
88 );
89 if (lift > 0) {
90 frame = resolvePreviewMiniPlayerFrame({
91 width: frame.width,
92 position: frame,
93 source: preview.source,
94 container: {
95 ...container,
96 height: Math.max(GAP * 2 + 1, container.height - composerHeight * lift),
97 },
98 });
99 }
100 }
101 const findBarObstacles = findBar ? { detailsCard: findBar, composer: null } : undefined;
102 const preferredFrame = {
103 ...frame,
104 ...clampPreviewMiniPlayerPosition(frame, container, frame, findBarObstacles, minimumPreviewX),
105 };
106 const dragObstacle =
107 detailsCard && findBar
108 ? {
109 left: Math.min(detailsCard.left, findBar.left),
110 right: Math.max(detailsCard.right, findBar.right),
111 bottom: Math.max(detailsCard.bottom, findBar.bottom),
112 }
113 : (detailsCard ?? findBar);
114 const cardObstacle = preview.lastInteraction === "resize" ? findBar : dragObstacle;
115 frame = {
116 ...frame,
117 ...clampPreviewMiniPlayerPosition(
118 frame,
119 container,
120 frame,
121 { detailsCard: cardObstacle, composer: null },
122 minimumPreviewX,
123 ),
124 };
125 const chatBeside = (player: PreviewMiniPlayerFrame) => {
126 const normalRight = normalLeft + normalWidth;
127 if (player.x >= normalRight + GAP) return chat;
128 const width = Math.min(maxChatWidth, player.x - GAP - padding);
129 if (width < minChatWidth) return null;
130 const left = Math.min(normalLeft, player.x - GAP - width);
131 return {
132 left,
133 width,
134 insetStart: 0,
135 insetEnd: Math.max(0, container.width - left * 2 - width),
136 };
137 };
138 let nextChat = chatBeside(frame);
139 if (!nextChat && detailsCard) {
140 nextChat = chatBeside(preferredFrame);
141 if (nextChat) frame = preferredFrame;
142 }
143 if (nextChat) chat = nextChat;
144 else overlapsChat = true;
145 if (overlapsChat && preview.lastInteraction === "resize") {
146 const width = Math.min(normalWidth, minChatWidth);
147 chat = {
148 left: padding,
149 width,
150 insetStart: 0,
151 insetEnd: Math.max(0, container.width - padding * 2 - width),
152 };
153 } else if (overlapsChat) {
154 const obstacles = {
155 detailsCard: cardObstacle,
156 composer: { left: chat.left, right: chat.left + chat.width, height: composerHeight },
157 };
158 frame = resolvePreviewMiniPlayerFrame({ ...preview, container, obstacles });
159 frame = {
160 ...frame,
161 ...clampPreviewMiniPlayerPosition(
162 preview.position ?? { x: frame.x, y: container.height - frame.height - GAP },
163 container,
164 frame,
165 obstacles,
166 minimumPreviewX,
167 ),
168 };
169 if (
170 frame.x < obstacles.composer.right &&
171 frame.x + frame.width > obstacles.composer.left &&
172 frame.y + frame.height > container.height - composerHeight
173 ) {
174 frame = {
175 ...frame,
176 ...clampPreviewMiniPlayerPosition(
177 frame,
178 container,
179 frame,
180 { composer: obstacles.composer, detailsCard: findBar },
181 minimumPreviewX,
182 ),
183 };
184 }
185 }
186 }
187 const overlapsDetailsCard = Boolean(
188 frame &&
189 detailsCard &&
190 frame.x < detailsCard.right &&
191 frame.x + frame.width > detailsCard.left &&
192 frame.y < detailsCard.bottom,
193 );
194 return { chat, frame, overlapsChat, overlapsDetailsCard };