The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 17:15 UTC
fix(web): floating preview resize holds its shape and ends when interrupted (#36315)
Julius Marminge committed
Nov 13, 2026 at 12:28 UTC
f8dc37d0b74adb3c4e6e42ab9eeb82c88d42ef0d
4 files changed
+388
-45
apps/web/src/components/preview/ThreadPreviewMiniPlayer.browser.test.tsx
new
+121
@@ -0,0 +1,121 @@
1
+import "~/index.css";
2
+
3
+import { RegistryContext } from "@effect/atom-react";
4
+import { scopeThreadRef } from "@t3tools/client-runtime/environment";
5
+import { type DeviceServiceState, type EnvironmentId, ThreadId } from "@t3tools/contracts";
6
+import { AsyncResult, Atom } from "effect/reactivity";
7
+import { useState } from "react";
8
+import { expect, it, vi } from "vite-plus/test";
9
+
10
+import { act, render } from "../../../test/render";
11
+import {
12
+ type PreviewMiniPlayerSource,
13
+ selectThreadPreviewMiniPlayer,
14
+ usePreviewMiniPlayerStore,
15
+} from "../../previewMiniPlayerStore";
16
+import { appAtomRegistry } from "../../rpc/atomRegistry";
17
+import { deviceEnvironment } from "../../state/device";
18
+import { ChatCanvas } from "../chat/ChatCanvas";
19
+import { createChatCanvasStore } from "../chat/canvasStore";
20
+import { deviceStreamScreenAtom } from "../device/deviceStreamSession";
21
+import { ThreadPreviewMiniPlayer, useThreadPreviewCanvasSource } from "./ThreadPreviewMiniPlayer";
22
+
23
+vi.mock(import("../device/DeviceStreamView"), () => ({
24
+ DeviceStreamView: (({ viewId }: { viewId: string }) => (
25
+ <div data-testid="stream" data-view-id={viewId} />
26
+ )) as never,
27
+}));
28
+
29
+const threadRef = scopeThreadRef("env-1" as EnvironmentId, ThreadId.make("thread-1"));
30
+const phone: PreviewMiniPlayerSource = {
31
+ kind: "device",
32
+ hostId: "local",
33
+ deviceId: "phone",
34
+ platform: "ios",
35
+ name: "Phone",
36
+};
37
+
38
+function Harness() {
39
+ const [store] = useState(createChatCanvasStore);
40
+ const miniPlayer = usePreviewMiniPlayerStore((state) =>
41
+ selectThreadPreviewMiniPlayer(state.byThreadKey, threadRef),
42
+ );
43
+ const preview = useThreadPreviewCanvasSource(threadRef, miniPlayer);
44
+ return (
45
+ <ChatCanvas store={store} threadRef={threadRef} preview={preview} composerOverlayElement={null}>
46
+ {miniPlayer ? (
47
+ <ThreadPreviewMiniPlayer threadRef={threadRef} miniPlayer={miniPlayer} />
48
+ ) : null}
49
+ </ChatCanvas>
50
+ );
51
+}
52
+
53
+it("holds its frame through a source change and stops when the gesture is interrupted", async () => {
54
+ using _state = vi
55
+ .spyOn(deviceEnvironment, "state")
56
+ .mockReturnValue(Atom.make<AsyncResult.AsyncResult<DeviceServiceState>>(AsyncResult.initial()));
57
+ using _store = {
58
+ [Symbol.dispose]: () => usePreviewMiniPlayerStore.getState().removeThread(threadRef),
59
+ };
60
+ usePreviewMiniPlayerStore.getState().open(threadRef, phone);
61
+ const container = document.createElement("div");
62
+ container.style.cssText = "display:flex;position:fixed;inset:0;width:1200px;height:800px";
63
+ await using screen = await render(
64
+ <RegistryContext.Provider value={appAtomRegistry}>
65
+ <Harness />
66
+ </RegistryContext.Provider>,
67
+ { container },
68
+ );
69
+ const player = screen.container.querySelector<HTMLElement>("[data-preview-mini-player]")!;
70
+ const handle = (direction: string) =>
71
+ player.querySelector<HTMLElement>(`[data-preview-mini-player-resize="${direction}"]`)!;
72
+ const rect = () => {
73
+ const { left, top, right, bottom } = player.getBoundingClientRect();
74
+ return { left, top, right, bottom };
75
+ };
76
+ const point = (target: HTMLElement, type: string, x: number, y: number, buttons: number) =>
77
+ act(() =>
78
+ target.dispatchEvent(
79
+ new PointerEvent(type, {
80
+ pointerId: 1,
81
+ pointerType: "mouse",
82
+ isPrimary: true,
83
+ button: type === "pointermove" ? -1 : 0,
84
+ buttons,
85
+ clientX: x,
86
+ clientY: y,
87
+ bubbles: true,
88
+ cancelable: true,
89
+ }),
90
+ ),
91
+ );
92
+ const viewId = screen.getByTestId("stream").element().getAttribute("data-view-id")!;
93
+ const portrait = rect();
94
+
95
+ // Held without moving while the device rotates: the frame waits for the release.
96
+ await point(handle("northwest"), "pointerdown", portrait.left, portrait.top, 1);
97
+ await act(() =>
98
+ appAtomRegistry.set(deviceStreamScreenAtom(viewId), {
99
+ width: 1_000,
100
+ height: 2_000,
101
+ orientation: "landscape_left",
102
+ }),
103
+ );
104
+ expect(rect()).toEqual(portrait);
105
+
106
+ // The release never arrived, so the next buttonless move ends the gesture where it was held.
107
+ await point(handle("northwest"), "pointermove", portrait.left - 200, portrait.top - 200, 0);
108
+ const landscape = rect();
109
+ expect({ right: landscape.right, bottom: landscape.bottom }).toEqual({
110
+ right: portrait.right,
111
+ bottom: portrait.bottom,
112
+ });
113
+ expect(landscape.right - landscape.left).toBe(2 * (landscape.bottom - landscape.top));
114
+ await point(handle("northwest"), "pointermove", portrait.left - 300, portrait.top - 300, 1);
115
+ expect(rect()).toEqual(landscape);
116
+
117
+ await point(handle("west"), "pointerdown", landscape.left, landscape.top + 10, 1);
118
+ await act(() => window.dispatchEvent(new Event("blur")));
119
+ await point(handle("west"), "pointermove", landscape.left - 100, landscape.top + 10, 1);
120
+ expect(rect()).toEqual(landscape);
121
+});
apps/web/src/components/preview/ThreadPreviewMiniPlayer.tsx
+97
-45
@@ -6,6 +6,7 @@ import {
6
type PointerEvent as ReactPointerEvent,
7
type ReactNode,
8
useEffect,
9
+ useEffectEvent,
10
useRef,
11
useState,
12
} from "react";
@@ -59,28 +60,19 @@ import { previewBridge } from "./previewBridge";
60
import { PreviewUnreachable } from "./PreviewUnreachable";
61
import { showPreviewPopup } from "./showPreviewPopup";
62
import {
62
- clampPreviewMiniPlayerPosition,
63
+ type PreviewMiniPlayerGesture,
64
+ settlePreviewMiniPlayerResize,
65
+ stepPreviewMiniPlayerGesture,
66
+} from "./previewMiniPlayerGesture";
67
+import {
68
PREVIEW_MINI_PLAYER_CORNER_RADIUS,
69
PREVIEW_MINI_PLAYER_WEBVIEW_Z_INDEX,
70
type PreviewMiniPlayerFrame,
66
- resizePreviewMiniPlayer,
71
resolveDeviceMiniPlayerCornerRadius,
72
resolveDeviceMiniPlayerSourceSize,
73
resolvePreviewMiniPlayerSourceSize,
74
} from "./previewMiniPlayerLayout";
75
72
-interface PointerGesture {
73
- readonly pointerId: number;
74
- readonly pointerType: string;
75
- readonly pointerX: number;
76
- readonly pointerY: number;
77
- readonly frame: PreviewMiniPlayerFrame;
78
- readonly direction: BrowserViewportResizeDirection | null;
79
- moved: boolean;
80
-}
81
-
82
-const HANDLE_TAP_SLOP_PX = 6;
83
-
76
interface Props {
77
readonly threadRef: ScopedThreadRef;
78
readonly miniPlayer: PreviewMiniPlayerState;
@@ -111,6 +103,13 @@ const useStreamedSourceSize = create<{
103
readonly viewport: PreviewStreamViewport;
104
} | null>(() => null);
105
106
+// The source size a resize in progress lays the frame out from, so a source that changes shape
107
+// under a held handle cannot resize the player by itself. Same single slot as above.
108
+const useHeldSourceSize = create<{
109
+ readonly key: string;
110
+ readonly source: PreviewMiniPlayerSize;
111
+} | null>(() => null);
112
+
113
const NO_DEVICE_SCREEN_ATOM = Atom.make<DeviceScreenSize | null>(null).pipe(
114
Atom.withLabel("device-stream-screen:none"),
115
);
@@ -178,6 +177,11 @@ export function useThreadPreviewCanvasSource(
177
? deviceStreamScreenAtom(miniPlayerDeviceViewId(threadRef, source))
178
: NO_DEVICE_SCREEN_ATOM,
179
);
180
+ const heldSource = useHeldSourceSize((state) =>
181
+ threadRef && sourceKey !== null && state?.key === streamedSourceKey(threadRef, sourceKey)
182
+ ? state.source
183
+ : null,
184
+ );
185
if (!miniPlayer || !source || sourceKey === null) return null;
186
if (source.kind === "browser" && !browser.snapshot) return null;
187
return {
@@ -186,9 +190,10 @@ export function useThreadPreviewCanvasSource(
190
position: miniPlayer.position,
191
lastInteraction: miniPlayer.lastInteraction,
192
source:
189
- source.kind === "browser"
193
+ heldSource ??
194
+ (source.kind === "browser"
195
? browser.sourceSize
191
- : resolveDeviceMiniPlayerSourceSize(source.platform, screen),
196
+ : resolveDeviceMiniPlayerSourceSize(source.platform, screen)),
197
};
198
}
199
@@ -446,7 +451,8 @@ function MiniPlayerShell({
451
readonly children: (frame: PreviewMiniPlayerFrame) => ReactNode;
452
}) {
453
const canvas = useChatCanvas();
449
- const gestureRef = useRef<PointerGesture | null>(null);
454
+ const gestureRef = useRef<PreviewMiniPlayerGesture | null>(null);
455
+ const captureRef = useRef<HTMLElement | null>(null);
456
// Touch toggles the pill after the tap's click so that click cannot land on the newly opened pill.
457
const [pillOpen, setPillOpen] = useState(false);
458
const handleTappedRef = useRef(false);
@@ -463,6 +469,7 @@ function MiniPlayerShell({
469
}, [pillOpen]);
470
const container = canvas?.container ?? null;
471
const sourceKey = previewMiniPlayerSourceKey(miniPlayer.source);
472
+ const heldSourceKey = streamedSourceKey(threadRef, sourceKey);
473
const frame = canvas?.previewKey === sourceKey ? canvas.layout.frame : null;
474
475
const radius = frame ? cornerRadius(frame) : PREVIEW_MINI_PLAYER_CORNER_RADIUS;
@@ -472,11 +479,54 @@ function MiniPlayerShell({
479
usePreviewMiniPlayerStore.getState().close(threadRef);
480
};
481
482
+ /** Every way a gesture stops comes through here; returns the gesture it ended. */
483
+ const finishGesture = () => {
484
+ const gesture = gestureRef.current;
485
+ if (!gesture) return null;
486
+ gestureRef.current = null;
487
+ handleTappedRef.current = false;
488
+ const handle = captureRef.current;
489
+ captureRef.current = null;
490
+ if (handle?.hasPointerCapture(gesture.pointerId)) {
491
+ handle.releasePointerCapture(gesture.pointerId);
492
+ }
493
+ if (gesture.direction === null) return gesture;
494
+ useHeldSourceSize.setState((state) => (state?.key === heldSourceKey ? null : state), true);
495
+ const settled =
496
+ frame && container
497
+ ? settlePreviewMiniPlayerResize({
498
+ frame,
499
+ direction: gesture.direction,
500
+ held: gesture.source,
501
+ source: sourceSize,
502
+ container,
503
+ })
504
+ : null;
505
+ if (settled) {
506
+ usePreviewMiniPlayerStore
507
+ .getState()
508
+ .resize(threadRef, sourceKey, settled.width, { x: settled.x, y: settled.y });
509
+ }
510
+ return gesture;
511
+ };
512
+ const endInterruptedGesture = useEffectEvent(() => {
513
+ finishGesture();
514
+ });
515
+ // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- window blur ends a gesture whose release went to another window, and unmount ends one in progress
516
+ useEffect(() => {
517
+ window.addEventListener("blur", endInterruptedGesture);
518
+ return () => {
519
+ window.removeEventListener("blur", endInterruptedGesture);
520
+ useHeldSourceSize.setState((state) => (state?.key === heldSourceKey ? null : state), true);
521
+ };
522
+ }, [heldSourceKey]);
523
+
524
const beginGesture = (
525
event: ReactPointerEvent<HTMLElement>,
526
direction: BrowserViewportResizeDirection | null,
527
) => {
528
if (event.button !== 0 || !frame) return;
529
+ finishGesture();
530
gestureRef.current = {
531
pointerId: event.pointerId,
532
pointerType: event.pointerType,
@@ -484,8 +534,13 @@ function MiniPlayerShell({
534
pointerY: event.clientY,
535
frame,
536
direction,
537
+ source: sourceSize,
538
moved: false,
539
};
540
+ captureRef.current = event.currentTarget;
541
+ if (direction !== null) {
542
+ useHeldSourceSize.setState({ key: heldSourceKey, source: sourceSize }, true);
543
+ }
544
event.currentTarget.setPointerCapture(event.pointerId);
545
event.preventDefault();
546
event.stopPropagation();
@@ -493,44 +548,39 @@ function MiniPlayerShell({
548
549
const handlePointerMove = (event: ReactPointerEvent<HTMLElement>) => {
550
const gesture = gestureRef.current;
496
- if (!gesture || gesture.pointerId !== event.pointerId || !container) return;
497
- const delta = { x: event.clientX - gesture.pointerX, y: event.clientY - gesture.pointerY };
498
- if (!gesture.moved && Math.hypot(delta.x, delta.y) < HANDLE_TAP_SLOP_PX) return;
499
- gesture.moved = true;
551
+ if (!gesture || !container) return;
552
+ const step = stepPreviewMiniPlayerGesture(gesture, event, container);
553
+ if (step.kind === "ignore") return;
554
+ if (step.kind === "end") {
555
+ finishGesture();
556
+ return;
557
+ }
558
+ if (!gesture.moved) gestureRef.current = { ...gesture, moved: true };
559
const store = usePreviewMiniPlayerStore.getState();
501
- if (gesture.direction === null) {
502
- store.move(
503
- threadRef,
504
- sourceKey,
505
- clampPreviewMiniPlayerPosition(
506
- { x: gesture.frame.x + delta.x, y: gesture.frame.y + delta.y },
507
- container,
508
- gesture.frame,
509
- ),
510
- );
560
+ if (step.kind === "move") {
561
+ store.move(threadRef, sourceKey, step.position);
562
return;
563
}
513
- const next = resizePreviewMiniPlayer({
514
- start: gesture.frame,
515
- direction: gesture.direction,
516
- delta,
517
- source: sourceSize,
518
- container,
519
- });
520
- store.resize(threadRef, sourceKey, next.width, { x: next.x, y: next.y });
564
+ store.resize(threadRef, sourceKey, step.frame.width, { x: step.frame.x, y: step.frame.y });
565
};
566
567
const endGesture = (event: ReactPointerEvent<HTMLElement>) => {
524
- const gesture = gestureRef.current;
525
- if (gesture?.pointerId !== event.pointerId) return;
526
- gestureRef.current = null;
568
+ if (gestureRef.current?.pointerId !== event.pointerId) return;
569
+ const gesture = finishGesture();
570
handleTappedRef.current =
571
event.type === "pointerup" &&
529
- gesture.direction === null &&
572
+ gesture?.direction === null &&
573
gesture.pointerType === "touch" &&
574
!gesture.moved;
532
- if (event.currentTarget.hasPointerCapture(event.pointerId)) {
533
- event.currentTarget.releasePointerCapture(event.pointerId);
575
+ };
576
+
577
+ // Capture can go without a release or cancel: the handle is replaced, or the platform takes it.
578
+ const handleLostCapture = (event: ReactPointerEvent<HTMLElement>) => {
579
+ if (
580
+ gestureRef.current?.pointerId === event.pointerId &&
581
+ captureRef.current === event.currentTarget
582
+ ) {
583
+ finishGesture();
584
}
585
};
586
@@ -558,6 +608,7 @@ function MiniPlayerShell({
608
onPointerMove={handlePointerMove}
609
onPointerUp={endGesture}
610
onPointerCancel={endGesture}
611
+ onLostPointerCapture={handleLostCapture}
612
onClick={() => {
613
if (!handleTappedRef.current) return;
614
handleTappedRef.current = false;
@@ -634,6 +685,7 @@ function MiniPlayerShell({
685
onPointerMove={handlePointerMove}
686
onPointerUp={endGesture}
687
onPointerCancel={endGesture}
688
+ onLostPointerCapture={handleLostCapture}
689
/>
690
))}
691
</section>
apps/web/src/components/preview/previewMiniPlayerGesture.test.ts
new
+77
@@ -0,0 +1,77 @@
1
+import { describe, expect, it } from "vite-plus/test";
2
+
3
+import { resolveChatCanvasLayout } from "../chat/chatCanvasLayout";
4
+import {
5
+ type PreviewMiniPlayerGesture,
6
+ settlePreviewMiniPlayerResize,
7
+ stepPreviewMiniPlayerGesture,
8
+} from "./previewMiniPlayerGesture";
9
+
10
+const container = { width: 1_440, height: 900 };
11
+const frame = { x: 996, y: 433, width: 268, height: 179 };
12
+const gesture: PreviewMiniPlayerGesture = {
13
+ pointerId: 1,
14
+ pointerType: "mouse",
15
+ pointerX: 996,
16
+ pointerY: 433,
17
+ frame,
18
+ direction: "northwest",
19
+ source: { width: 1_200, height: 800 },
20
+ moved: true,
21
+};
22
+const pointer = { pointerId: 1, buttons: 1, clientX: 936, clientY: 393 };
23
+
24
+describe("stepPreviewMiniPlayerGesture", () => {
25
+ it("ends on a move without the primary button instead of following the hover", () => {
26
+ expect(stepPreviewMiniPlayerGesture(gesture, pointer, container).kind).toBe("resize");
27
+ expect(stepPreviewMiniPlayerGesture(gesture, { ...pointer, buttons: 0 }, container)).toEqual({
28
+ kind: "end",
29
+ });
30
+ expect(stepPreviewMiniPlayerGesture(gesture, { ...pointer, buttons: 2 }, container)).toEqual({
31
+ kind: "end",
32
+ });
33
+ expect(stepPreviewMiniPlayerGesture(gesture, { ...pointer, pointerId: 2 }, container)).toEqual({
34
+ kind: "ignore",
35
+ });
36
+ });
37
+
38
+ it("resizes at the shape the gesture started with", () => {
39
+ const step = stepPreviewMiniPlayerGesture(gesture, pointer, container);
40
+ expect(step).toEqual({ kind: "resize", frame: { x: 936, y: 393, width: 328, height: 219 } });
41
+ });
42
+});
43
+
44
+describe("settlePreviewMiniPlayerResize", () => {
45
+ it("keeps the corner opposite the handle when the source changed shape mid-resize", () => {
46
+ const source = { width: 1_200, height: 1_000 };
47
+ const settled = settlePreviewMiniPlayerResize({
48
+ frame,
49
+ direction: "northwest",
50
+ held: gesture.source,
51
+ source,
52
+ container,
53
+ });
54
+ expect(settled).toEqual({ x: 1_018, y: 407, width: 246, height: 205 });
55
+ expect(
56
+ resolveChatCanvasLayout({
57
+ container,
58
+ preview: {
59
+ key: "browser:tab",
60
+ width: settled!.width,
61
+ position: settled,
62
+ source,
63
+ lastInteraction: "resize",
64
+ },
65
+ }).frame,
66
+ ).toEqual(settled);
67
+ expect(
68
+ settlePreviewMiniPlayerResize({
69
+ frame,
70
+ direction: "northwest",
71
+ held: gesture.source,
72
+ source: { width: 600, height: 400 },
73
+ container,
74
+ }),
75
+ ).toBeNull();
76
+ });
77
+});
apps/web/src/components/preview/previewMiniPlayerGesture.ts
new
+93
@@ -0,0 +1,93 @@
1
+import type { BrowserViewportResizeDirection } from "~/browser/browserViewportLayout";
2
+import type { PreviewMiniPlayerPosition, PreviewMiniPlayerSize } from "~/previewMiniPlayerStore";
3
+
4
+import {
5
+ clampPreviewMiniPlayerPosition,
6
+ type PreviewMiniPlayerFrame,
7
+ resizePreviewMiniPlayer,
8
+} from "./previewMiniPlayerLayout";
9
+
10
+const HANDLE_TAP_SLOP_PX = 6;
11
+
12
+export interface PreviewMiniPlayerGesture {
13
+ readonly pointerId: number;
14
+ readonly pointerType: string;
15
+ readonly pointerX: number;
16
+ readonly pointerY: number;
17
+ readonly frame: PreviewMiniPlayerFrame;
18
+ /** Null drags the player; a direction resizes it from that handle. */
19
+ readonly direction: BrowserViewportResizeDirection | null;
20
+ /** The source size a resize keeps the frame's shape to until it ends. */
21
+ readonly source: PreviewMiniPlayerSize;
22
+ readonly moved: boolean;
23
+}
24
+
25
+interface GesturePointer {
26
+ readonly pointerId: number;
27
+ readonly buttons: number;
28
+ readonly clientX: number;
29
+ readonly clientY: number;
30
+}
31
+
32
+export type PreviewMiniPlayerGestureStep =
33
+ | { readonly kind: "ignore" }
34
+ | { readonly kind: "end" }
35
+ | { readonly kind: "move"; readonly position: PreviewMiniPlayerPosition }
36
+ | { readonly kind: "resize"; readonly frame: PreviewMiniPlayerFrame };
37
+
38
+/** What a pointer move does to the gesture in progress. */
39
+export function stepPreviewMiniPlayerGesture(
40
+ gesture: PreviewMiniPlayerGesture,
41
+ pointer: GesturePointer,
42
+ container: PreviewMiniPlayerSize,
43
+): PreviewMiniPlayerGestureStep {
44
+ if (pointer.pointerId !== gesture.pointerId) return { kind: "ignore" };
45
+ // A release the handle never saw (over a native view, another window) leaves only this trace.
46
+ if ((pointer.buttons & 1) === 0) return { kind: "end" };
47
+ const delta = { x: pointer.clientX - gesture.pointerX, y: pointer.clientY - gesture.pointerY };
48
+ if (!gesture.moved && Math.hypot(delta.x, delta.y) < HANDLE_TAP_SLOP_PX) {
49
+ return { kind: "ignore" };
50
+ }
51
+ if (gesture.direction === null) {
52
+ return {
53
+ kind: "move",
54
+ position: clampPreviewMiniPlayerPosition(
55
+ { x: gesture.frame.x + delta.x, y: gesture.frame.y + delta.y },
56
+ container,
57
+ gesture.frame,
58
+ ),
59
+ };
60
+ }
61
+ return {
62
+ kind: "resize",
63
+ frame: resizePreviewMiniPlayer({
64
+ start: gesture.frame,
65
+ direction: gesture.direction,
66
+ delta,
67
+ source: gesture.source,
68
+ container,
69
+ }),
70
+ };
71
+}
72
+
73
+/**
74
+ * The frame a finished resize leaves when the source changed shape under it: the new shape, with
75
+ * the edges opposite the handle where the gesture left them. Null when the shape is unchanged.
76
+ */
77
+export function settlePreviewMiniPlayerResize(input: {
78
+ readonly frame: PreviewMiniPlayerFrame;
79
+ readonly direction: BrowserViewportResizeDirection;
80
+ readonly held: PreviewMiniPlayerSize;
81
+ readonly source: PreviewMiniPlayerSize;
82
+ readonly container: PreviewMiniPlayerSize;
83
+}): PreviewMiniPlayerFrame | null {
84
+ const { frame, direction, held, source, container } = input;
85
+ if (held.width * source.height === source.width * held.height) return null;
86
+ return resizePreviewMiniPlayer({
87
+ start: frame,
88
+ direction,
89
+ delta: { x: 0, y: 0 },
90
+ source,
91
+ container,
92
+ });
93
+}