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}: {
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 chat lane only if the preview still has room to drag.
63 const minimumPreviewX =
64 preview.lastInteraction === "resize" ||
65 container.width <= padding + minChatWidth + frame.width + GAP * 2
66 ? GAP
67 : padding + minChatWidth + GAP;
68 if (preview.position === null) frame = { ...frame, y: container.height - frame.height - GAP };
69 if (preview.lastInteraction === "resize" && composerHeight > 0) {
70 const laneWidth = Math.min(normalWidth, minChatWidth);
71 const transitionWidth = Math.max(GAP, normalWidth - laneWidth);
72 const lift = Math.min(
73 1,
74 Math.max(0, (padding + normalWidth + GAP - frame.x) / transitionWidth),
75 );
76 if (lift > 0) {
77 frame = resolvePreviewMiniPlayerFrame({
78 width: frame.width,
79 position: frame,
80 source: preview.source,
81 container: {
82 ...container,
83 height: Math.max(GAP * 2 + 1, container.height - composerHeight * lift),
84 },
85 });
86 }
87 }
88 const findBarObstacles = findBar ? { detailsCard: findBar, composer: null } : undefined;
89 const preferredFrame = {
90 ...frame,
91 ...clampPreviewMiniPlayerPosition(frame, container, frame, findBarObstacles, minimumPreviewX),
92 };
93 const dragObstacle =
94 detailsCard && findBar
95 ? {
96 left: Math.min(detailsCard.left, findBar.left),
97 right: Math.max(detailsCard.right, findBar.right),
98 bottom: Math.max(detailsCard.bottom, findBar.bottom),
99 }
100 : (detailsCard ?? findBar);
101 const cardObstacle = preview.lastInteraction === "resize" ? findBar : dragObstacle;
102 frame = {
103 ...frame,
104 ...clampPreviewMiniPlayerPosition(
105 frame,
106 container,
107 frame,
108 { detailsCard: cardObstacle, composer: null },
109 minimumPreviewX,
110 ),
111 };
112 const chatBeside = (player: PreviewMiniPlayerFrame) => {
113 const normalRight = normalLeft + normalWidth;
114 if (player.x >= normalRight + GAP) return chat;
115 const width = Math.min(maxChatWidth, player.x - GAP - padding);
116 if (width < minChatWidth) return null;
117 const left = Math.min(normalLeft, player.x - GAP - width);
118 return {
119 left,
120 width,
121 insetStart: 0,
122 insetEnd: Math.max(0, container.width - left * 2 - width),
123 };
124 };
125 let nextChat = chatBeside(frame);
126 if (!nextChat && detailsCard) {
127 nextChat = chatBeside(preferredFrame);
128 if (nextChat) frame = preferredFrame;
129 }
130 if (nextChat) chat = nextChat;
131 else overlapsChat = true;
132 if (overlapsChat && preview.lastInteraction === "resize") {
133 const width = Math.min(normalWidth, minChatWidth);
134 chat = {
135 left: padding,
136 width,
137 insetStart: 0,
138 insetEnd: Math.max(0, container.width - padding * 2 - width),
139 };
140 } else if (overlapsChat) {
141 const obstacles = {
142 detailsCard: cardObstacle,
143 composer: { left: chat.left, right: chat.left + chat.width, height: composerHeight },
144 };
145 frame = resolvePreviewMiniPlayerFrame({ ...preview, container, obstacles });
146 frame = {
147 ...frame,
148 ...clampPreviewMiniPlayerPosition(
149 preview.position ?? { x: frame.x, y: container.height - frame.height - GAP },
150 container,
151 frame,
152 obstacles,
153 minimumPreviewX,
154 ),
155 };
156 if (
157 frame.x < obstacles.composer.right &&
158 frame.x + frame.width > obstacles.composer.left &&
159 frame.y + frame.height > container.height - composerHeight
160 ) {
161 frame = {
162 ...frame,
163 ...clampPreviewMiniPlayerPosition(
164 frame,
165 container,
166 frame,
167 { composer: obstacles.composer, detailsCard: findBar },
168 minimumPreviewX,
169 ),
170 };
171 }
172 }
173 }
174 const overlapsDetailsCard = Boolean(
175 frame &&
176 detailsCard &&
177 frame.x < detailsCard.right &&
178 frame.x + frame.width > detailsCard.left &&
179 frame.y < detailsCard.bottom,
180 );
181 return { chat, frame, overlapsChat, overlapsDetailsCard };
182}