Skip to content

Commit f8dc37d

Browse files
fix(web): floating preview resize holds its shape and ends when interrupted (#36315)
1 parent eb81855 commit f8dc37d

4 files changed

Lines changed: 388 additions & 45 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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‎

Lines changed: 97 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type PointerEvent as ReactPointerEvent,
77
type ReactNode,
88
useEffect,
9+
useEffectEvent,
910
useRef,
1011
useState,
1112
} from "react";
@@ -59,28 +60,19 @@ import { previewBridge } from "./previewBridge";
5960
import { PreviewUnreachable } from "./PreviewUnreachable";
6061
import { showPreviewPopup } from "./showPreviewPopup";
6162
import {
62-
clampPreviewMiniPlayerPosition,
63+
type PreviewMiniPlayerGesture,
64+
settlePreviewMiniPlayerResize,
65+
stepPreviewMiniPlayerGesture,
66+
} from "./previewMiniPlayerGesture";
67+
import {
6368
PREVIEW_MINI_PLAYER_CORNER_RADIUS,
6469
PREVIEW_MINI_PLAYER_WEBVIEW_Z_INDEX,
6570
type PreviewMiniPlayerFrame,
66-
resizePreviewMiniPlayer,
6771
resolveDeviceMiniPlayerCornerRadius,
6872
resolveDeviceMiniPlayerSourceSize,
6973
resolvePreviewMiniPlayerSourceSize,
7074
} from "./previewMiniPlayerLayout";
7175

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-
8476
interface Props {
8577
readonly threadRef: ScopedThreadRef;
8678
readonly miniPlayer: PreviewMiniPlayerState;
@@ -111,6 +103,13 @@ const useStreamedSourceSize = create<{
111103
readonly viewport: PreviewStreamViewport;
112104
} | null>(() => null);
113105

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+
114113
const NO_DEVICE_SCREEN_ATOM = Atom.make<DeviceScreenSize | null>(null).pipe(
115114
Atom.withLabel("device-stream-screen:none"),
116115
);
@@ -178,6 +177,11 @@ export function useThreadPreviewCanvasSource(
178177
? deviceStreamScreenAtom(miniPlayerDeviceViewId(threadRef, source))
179178
: NO_DEVICE_SCREEN_ATOM,
180179
);
180+
const heldSource = useHeldSourceSize((state) =>
181+
threadRef && sourceKey !== null && state?.key === streamedSourceKey(threadRef, sourceKey)
182+
? state.source
183+
: null,
184+
);
181185
if (!miniPlayer || !source || sourceKey === null) return null;
182186
if (source.kind === "browser" && !browser.snapshot) return null;
183187
return {
@@ -186,9 +190,10 @@ export function useThreadPreviewCanvasSource(
186190
position: miniPlayer.position,
187191
lastInteraction: miniPlayer.lastInteraction,
188192
source:
189-
source.kind === "browser"
193+
heldSource ??
194+
(source.kind === "browser"
190195
? browser.sourceSize
191-
: resolveDeviceMiniPlayerSourceSize(source.platform, screen),
196+
: resolveDeviceMiniPlayerSourceSize(source.platform, screen)),
192197
};
193198
}
194199

@@ -446,7 +451,8 @@ function MiniPlayerShell({
446451
readonly children: (frame: PreviewMiniPlayerFrame) => ReactNode;
447452
}) {
448453
const canvas = useChatCanvas();
449-
const gestureRef = useRef<PointerGesture | null>(null);
454+
const gestureRef = useRef<PreviewMiniPlayerGesture | null>(null);
455+
const captureRef = useRef<HTMLElement | null>(null);
450456
// Touch toggles the pill after the tap's click so that click cannot land on the newly opened pill.
451457
const [pillOpen, setPillOpen] = useState(false);
452458
const handleTappedRef = useRef(false);
@@ -463,6 +469,7 @@ function MiniPlayerShell({
463469
}, [pillOpen]);
464470
const container = canvas?.container ?? null;
465471
const sourceKey = previewMiniPlayerSourceKey(miniPlayer.source);
472+
const heldSourceKey = streamedSourceKey(threadRef, sourceKey);
466473
const frame = canvas?.previewKey === sourceKey ? canvas.layout.frame : null;
467474

468475
const radius = frame ? cornerRadius(frame) : PREVIEW_MINI_PLAYER_CORNER_RADIUS;
@@ -472,65 +479,108 @@ function MiniPlayerShell({
472479
usePreviewMiniPlayerStore.getState().close(threadRef);
473480
};
474481

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+
475524
const beginGesture = (
476525
event: ReactPointerEvent<HTMLElement>,
477526
direction: BrowserViewportResizeDirection | null,
478527
) => {
479528
if (event.button !== 0 || !frame) return;
529+
finishGesture();
480530
gestureRef.current = {
481531
pointerId: event.pointerId,
482532
pointerType: event.pointerType,
483533
pointerX: event.clientX,
484534
pointerY: event.clientY,
485535
frame,
486536
direction,
537+
source: sourceSize,
487538
moved: false,
488539
};
540+
captureRef.current = event.currentTarget;
541+
if (direction !== null) {
542+
useHeldSourceSize.setState({ key: heldSourceKey, source: sourceSize }, true);
543+
}
489544
event.currentTarget.setPointerCapture(event.pointerId);
490545
event.preventDefault();
491546
event.stopPropagation();
492547
};
493548

494549
const handlePointerMove = (event: ReactPointerEvent<HTMLElement>) => {
495550
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 };
500559
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);
511562
return;
512563
}
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 });
521565
};
522566

523567
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();
527570
handleTappedRef.current =
528571
event.type === "pointerup" &&
529-
gesture.direction === null &&
572+
gesture?.direction === null &&
530573
gesture.pointerType === "touch" &&
531574
!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();
534584
}
535585
};
536586

@@ -558,6 +608,7 @@ function MiniPlayerShell({
558608
onPointerMove={handlePointerMove}
559609
onPointerUp={endGesture}
560610
onPointerCancel={endGesture}
611+
onLostPointerCapture={handleLostCapture}
561612
onClick={() => {
562613
if (!handleTappedRef.current) return;
563614
handleTappedRef.current = false;
@@ -634,6 +685,7 @@ function MiniPlayerShell({
634685
onPointerMove={handlePointerMove}
635686
onPointerUp={endGesture}
636687
onPointerCancel={endGesture}
688+
onLostPointerCapture={handleLostCapture}
637689
/>
638690
))}
639691
</section>

0 commit comments

Comments
 (0)