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