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
23/** Pure geometry shared by the conversation, composer, and floating preview. */
24export function resolveChatCanvasLayout({
25 container,
26 preview,
27 padding = 20,
28 maxChatWidth = 768,
29 minChatWidth = 640,
30 composerHeight = 0,
31 detailsCard = null,
32}: {
33 container: PreviewMiniPlayerSize;
34 preview: ChatCanvasPreview | null;
35 padding?: number;
36 maxChatWidth?: number;
37 minChatWidth?: number;
38 composerHeight?: number;
39 detailsCard?: PreviewMiniPlayerObstacles["detailsCard"];
40}) {
41 const normalWidth = Math.max(0, Math.min(maxChatWidth, container.width - padding * 2));
42 const normalLeft = (container.width - normalWidth) / 2;
43 let chat = { left: normalLeft, width: normalWidth, insetStart: 0, insetEnd: 0 };
44 let frame: PreviewMiniPlayerFrame | null = null;
45 let overlapsChat = false;
46 if (preview && container.width > 0 && container.height > 0) {
47 frame = resolvePreviewMiniPlayerFrame({ ...preview, container });
48 // Dragging stops at a readable chat lane on the left. Resizing can still
49 // consume that space when the container requires message overlap.
50 const minimumPreviewX =
51 preview.lastInteraction === "resize" ? GAP : padding + minChatWidth + GAP;
52 // New players start beside the composer, with the workspace card above them.
53 if (preview.position === null) frame = { ...frame, y: container.height - frame.height - GAP };
54 if (preview.lastInteraction === "resize" && composerHeight > 0) {
55 // Lift the preview while chat uses its remaining shrink room, reaching
56 // the composer's top before the preview enters the readable chat lane.
57 const laneWidth = Math.min(normalWidth, minChatWidth);
58 const transitionWidth = Math.max(GAP, normalWidth - laneWidth);
59 const lift = Math.min(
60 1,
61 Math.max(0, (padding + normalWidth + GAP - frame.x) / transitionWidth),
62 );
63 if (lift > 0) {
64 frame = resolvePreviewMiniPlayerFrame({
65 width: frame.width,
66 position: frame,
67 source: preview.source,
68 container: {
69 ...container,
70 height: Math.max(GAP * 2 + 1, container.height - composerHeight * lift),
71 },
72 });
73 }
74 }
75 const preferredFrame = {
76 ...frame,
77 ...clampPreviewMiniPlayerPosition(frame, container, frame, undefined, minimumPreviewX),
78 };
79 // A resize keeps its anchored edge and consumes card height first. A drag
80 // clears the full card whenever it can, so moving alone never folds it.
81 const cardObstacle = preview.lastInteraction === "resize" ? null : detailsCard;
82 frame = {
83 ...frame,
84 ...clampPreviewMiniPlayerPosition(
85 frame,
86 container,
87 frame,
88 { detailsCard: cardObstacle, composer: null },
89 minimumPreviewX,
90 ),
91 };
92 const chatBeside = (player: PreviewMiniPlayerFrame) => {
93 const normalRight = normalLeft + normalWidth;
94 if (player.x >= normalRight + GAP) return chat;
95 const width = Math.min(maxChatWidth, player.x - GAP - padding);
96 if (width < minChatWidth) return null;
97 const left = Math.min(normalLeft, player.x - GAP - width);
98 return {
99 left,
100 width,
101 insetStart: 0,
102 insetEnd: Math.max(0, container.width - left * 2 - width),
103 };
104 };
105 let nextChat = chatBeside(frame);
106 // Clearing the full card must also leave a readable chat. If only the
107 // preferred position does, keep the resized player and let the card fold.
108 if (!nextChat && detailsCard) {
109 nextChat = chatBeside(preferredFrame);
110 if (nextChat) frame = preferredFrame;
111 }
112 if (nextChat) chat = nextChat;
113 else overlapsChat = true;
114 if (overlapsChat && preview.lastInteraction === "resize") {
115 const width = Math.min(normalWidth, minChatWidth);
116 chat = {
117 left: padding,
118 width,
119 insetStart: 0,
120 insetEnd: Math.max(0, container.width - padding * 2 - width),
121 };
122 } else if (overlapsChat) {
123 const obstacles = {
124 detailsCard: cardObstacle,
125 composer: { left: chat.left, right: chat.left + chat.width, height: composerHeight },
126 };
127 frame = resolvePreviewMiniPlayerFrame({ ...preview, container, obstacles });
128 frame = {
129 ...frame,
130 ...clampPreviewMiniPlayerPosition(
131 preview.position ?? { x: frame.x, y: container.height - frame.height - GAP },
132 container,
133 frame,
134 obstacles,
135 minimumPreviewX,
136 ),
137 };
138 // If the full card and composer leave no slot, keep typing usable while
139 // the card folds to the remaining height above this fallback frame.
140 if (
141 frame.x < obstacles.composer.right &&
142 frame.x + frame.width > obstacles.composer.left &&
143 frame.y + frame.height > container.height - composerHeight
144 ) {
145 frame = {
146 ...frame,
147 ...clampPreviewMiniPlayerPosition(
148 frame,
149 container,
150 frame,
151 { composer: obstacles.composer, detailsCard: null },
152 minimumPreviewX,
153 ),
154 };
155 }
156 }
157 }
158 const overlapsDetailsCard = Boolean(
159 frame &&
160 detailsCard &&
161 frame.x < detailsCard.right &&
162 frame.x + frame.width > detailsCard.left &&
163 frame.y < detailsCard.bottom,
164 );
165 return { chat, frame, overlapsChat, overlapsDetailsCard };
166}