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;
22// Minimum space between chat and the workspace card. Chat stays centered while
23// the card fits beside it with this much room.
24export const DETAILS_CARD_CLEARANCE = 32;
25
26/** Pure geometry shared by the conversation, composer, workspace card, and floating preview. */
27export function resolveChatCanvasLayout({
28 container,
29 preview,
30 padding = 48,
31 maxChatWidth = 768,
32 minChatWidth = 640,
33 composerHeight = 0,
34 detailsCard = null,
35}: {
36 container: PreviewMiniPlayerSize;
37 preview: ChatCanvasPreview | null;
38 padding?: number;
39 maxChatWidth?: number;
40 minChatWidth?: number;
41 composerHeight?: number;
42 detailsCard?: PreviewMiniPlayerObstacles["detailsCard"];
43}) {
44 const centeredWidth = Math.max(0, Math.min(maxChatWidth, container.width - padding * 2));
45 // A workspace card that does not fit beside the centered chat first moves
46 // chat left, only as far as it needs. Chat narrows only after it reaches the
47 // left padding.
48 const laneRight = detailsCard
49 ? detailsCard.left - DETAILS_CARD_CLEARANCE
50 : container.width - padding;
51 const normalWidth = Math.max(0, Math.min(centeredWidth, laneRight - padding));
52 const normalLeft = Math.max(
53 padding,
54 Math.min((container.width - normalWidth) / 2, laneRight - normalWidth),
55 );
56 let chat = {
57 left: normalLeft,
58 width: normalWidth,
59 insetStart: 0,
60 insetEnd: Math.max(0, container.width - normalLeft * 2 - normalWidth),
61 };
62 let frame: PreviewMiniPlayerFrame | null = null;
63 let overlapsChat = false;
64 if (preview && container.width > 0 && container.height > 0) {
65 frame = resolvePreviewMiniPlayerFrame({ ...preview, container });
66 // Dragging stops at a readable chat lane on the left. Resizing can still
67 // consume that space when the container requires message overlap.
68 const minimumPreviewX =
69 preview.lastInteraction === "resize" ? GAP : padding + minChatWidth + GAP;
70 // New players start beside the composer, with the workspace card above them.
71 if (preview.position === null) frame = { ...frame, y: container.height - frame.height - GAP };
72 if (preview.lastInteraction === "resize" && composerHeight > 0) {
73 // Lift the preview while chat uses its remaining shrink room, reaching
74 // the composer's top before the preview enters the readable chat lane.
75 const laneWidth = Math.min(normalWidth, minChatWidth);
76 const transitionWidth = Math.max(GAP, normalWidth - laneWidth);
77 const lift = Math.min(
78 1,
79 Math.max(0, (padding + normalWidth + GAP - frame.x) / transitionWidth),
80 );
81 if (lift > 0) {
82 frame = resolvePreviewMiniPlayerFrame({
83 width: frame.width,
84 position: frame,
85 source: preview.source,
86 container: {
87 ...container,
88 height: Math.max(GAP * 2 + 1, container.height - composerHeight * lift),
89 },
90 });
91 }
92 }
93 const preferredFrame = {
94 ...frame,
95 ...clampPreviewMiniPlayerPosition(frame, container, frame, undefined, minimumPreviewX),
96 };
97 // A resize keeps its anchored edge and consumes card height first. A drag
98 // clears the full card whenever it can, so moving alone never folds it.
99 const cardObstacle = preview.lastInteraction === "resize" ? null : detailsCard;
100 frame = {
101 ...frame,
102 ...clampPreviewMiniPlayerPosition(
103 frame,
104 container,
105 frame,
106 { detailsCard: cardObstacle, composer: null },
107 minimumPreviewX,
108 ),
109 };
110 const chatBeside = (player: PreviewMiniPlayerFrame) => {
111 const normalRight = normalLeft + normalWidth;
112 if (player.x >= normalRight + GAP) return chat;
113 const width = Math.min(maxChatWidth, player.x - GAP - padding);
114 if (width < minChatWidth) return null;
115 const left = Math.min(normalLeft, player.x - GAP - width);
116 return {
117 left,
118 width,
119 insetStart: 0,
120 insetEnd: Math.max(0, container.width - left * 2 - width),
121 };
122 };
123 let nextChat = chatBeside(frame);
124 // Clearing the full card must also leave a readable chat. If only the
125 // preferred position does, keep the resized player and let the card fold.
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 the full card and composer leave no slot, keep typing usable while
157 // the card folds to the remaining height above this fallback frame.
158 if (
159 frame.x < obstacles.composer.right &&
160 frame.x + frame.width > obstacles.composer.left &&
161 frame.y + frame.height > container.height - composerHeight
162 ) {
163 frame = {
164 ...frame,
165 ...clampPreviewMiniPlayerPosition(
166 frame,
167 container,
168 frame,
169 { composer: obstacles.composer, detailsCard: null },
170 minimumPreviewX,
171 ),
172 };
173 }
174 }
175 }
176 const overlapsDetailsCard = Boolean(
177 frame &&
178 detailsCard &&
179 frame.x < detailsCard.right &&
180 frame.x + frame.width > detailsCard.left &&
181 frame.y < detailsCard.bottom,
182 );
183 return { chat, frame, overlapsChat, overlapsDetailsCard };
184}