Skip to content

Commit 389d3ca

Browse files
committed
fix(preview): recover PDF popups and close stale picture in picture
1 parent cd8c24e commit 389d3ca

7 files changed

Lines changed: 223 additions & 50 deletions

File tree

‎apps/mobile/src/features/browser/BrowserPreviewRouteScreen.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ function BrowserPreviewScreen({
122122
options={{ headerBackVisible: false }}
123123
actions={
124124
// Android WebView cannot play video in picture in picture.
125-
Platform.OS === "ios" && pictureInPicture.supported && pdfUri === null
125+
Platform.OS === "ios" &&
126+
pictureInPicture.supported &&
127+
(pictureInPicture.active || pdfUri === null)
126128
? [
127129
{
128130
accessibilityLabel: pictureInPicture.active

‎apps/server/src/preview/ServerBrowser.test.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,44 @@ it.live("a popup becomes the agent's own tab and keeps its opener page", () =>
908908
).pipe(Effect.provide(layer)),
909909
);
910910

911+
it.live.each([
912+
{ url: "https://environment.test/api/assets/token/guide.pdf", pdf: true },
913+
{ url: "https://example.com/sign-in", pdf: false },
914+
])("an adopted popup at $url recovers only PDF asset navigation", ({ url, pdf }) =>
915+
Effect.scoped(
916+
Effect.gen(function* () {
917+
const browser = yield* ServerBrowser.ServerBrowser;
918+
const manager = yield* Manager.PreviewManager;
919+
const events = yield* manager.subscribeEvents;
920+
const opened = yield* manager.open({ threadId: scope.thread.threadId, runtime: "server" });
921+
const viewer = yield* browser.attachViewer(viewerInput(opened.tabId, true));
922+
yield* viewer.input({ type: "takeControl" });
923+
const popup = makeContext();
924+
yield* Effect.promise(() => popup.page.goto(url));
925+
popup.page.goto.mockClear();
926+
popup.page.reload.mockImplementationOnce(async () => {
927+
popup.page.emit("load");
928+
});
929+
contexts[0]!.page.emit("popup", popup.page);
930+
let shown = yield* Queue.take(viewer.output);
931+
while (shown._tag !== "popup") shown = yield* Queue.take(viewer.output);
932+
yield* browser.attachViewer(viewerInput(shown.tabId, true));
933+
expect(popup.page.goto).not.toHaveBeenCalled();
934+
expect(popup.page.reload).toHaveBeenCalledTimes(pdf ? 1 : 0);
935+
if (pdf) {
936+
let event = yield* PubSub.take(events);
937+
while (event.type !== "navigated" || event.tabId !== shown.tabId) {
938+
event = yield* PubSub.take(events);
939+
}
940+
const session = (yield* manager.list({ threadId: scope.thread.threadId })).sessions.find(
941+
(entry) => entry.tabId === shown.tabId,
942+
);
943+
expect(session?.navStatus).toMatchObject({ _tag: "Success", url });
944+
}
945+
}),
946+
).pipe(Effect.provide(layer)),
947+
);
948+
911949
it.live("a tab opened to a file the browser cannot show reports the file to download", () =>
912950
Effect.scoped(
913951
Effect.gen(function* () {

‎apps/server/src/preview/ServerBrowser.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -887,16 +887,14 @@ const make = Effect.gen(function* () {
887887
page.on("crash", () => dropTab(tab, true));
888888
tabs.set(key, tab);
889889
reportLiveTabs();
890-
// Desktop PDFs may start loading before interception attaches. Other desktop
891-
// pages and popups keep their existing navigation.
890+
// Existing desktop and popup PDFs can start loading before interception attaches.
892891
if (
893-
!adopted &&
894892
snapshot.navStatus._tag === "Loading" &&
895-
(!desktop || isPdfAssetUrl(snapshot.navStatus.url))
893+
((!adopted && !desktop) || isPdfAssetUrl(snapshot.navStatus.url))
896894
) {
897895
const options = { waitUntil: "commit", timeout: NAVIGATION_TIMEOUT_MS } as const;
898896
tab.initialNavigation = (
899-
desktop && page.url() === snapshot.navStatus.url
897+
(desktop || adopted) && page.url() === snapshot.navStatus.url
900898
? page.reload(options)
901899
: page.goto(snapshot.navStatus.url, options)
902900
).then(constVoid);
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2+
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
3+
4+
import { applyPreviewServerSnapshot, resetPreviewStateForTests } from "~/previewStateStore";
5+
import { closeServerPictureInPicture, openServerPictureInPicture } from "./serverPictureInPicture";
6+
7+
vi.mock("~/state/previewStream", () => ({
8+
readPreviewStreamAccess: async () => ({}),
9+
}));
10+
vi.mock("@t3tools/client-runtime/preview/server-browser-stream", () => ({
11+
createPreviewFramePainter: () => ({ paint: vi.fn(), stop: vi.fn() }),
12+
createPreviewStreamClient: () => ({ stop: vi.fn() }),
13+
}));
14+
15+
const ref = {
16+
environmentId: EnvironmentId.make("environment"),
17+
threadId: ThreadId.make("thread"),
18+
};
19+
const stopTrack = vi.fn();
20+
const exitPictureInPicture = vi.fn(async () => {});
21+
22+
function navigate(url: string, tabId = "tab") {
23+
applyPreviewServerSnapshot(ref, {
24+
threadId: ref.threadId,
25+
tabId,
26+
navStatus: { _tag: "Success", url, title: "Document" },
27+
canGoBack: true,
28+
canGoForward: false,
29+
updatedAt: new Date().toISOString(),
30+
});
31+
}
32+
33+
beforeEach(() => {
34+
vi.clearAllMocks();
35+
resetPreviewStateForTests();
36+
const video = {
37+
style: { cssText: "" },
38+
readyState: 4,
39+
play: async () => {},
40+
addEventListener: vi.fn(),
41+
removeEventListener: vi.fn(),
42+
remove: vi.fn(),
43+
requestPictureInPicture: vi.fn(async () => {}),
44+
};
45+
vi.stubGlobal("HTMLMediaElement", { HAVE_METADATA: 1 });
46+
vi.stubGlobal("document", {
47+
pictureInPictureEnabled: true,
48+
pictureInPictureElement: video,
49+
exitPictureInPicture,
50+
body: { append: vi.fn() },
51+
createElement: (tag: string) =>
52+
tag === "video"
53+
? video
54+
: {
55+
getContext: () => ({ fillRect: vi.fn() }),
56+
captureStream: () => ({ getTracks: () => [{ stop: stopTrack }] }),
57+
},
58+
});
59+
navigate("https://environment.test/api/assets/token/index.html");
60+
});
61+
62+
afterEach(() => {
63+
closeServerPictureInPicture();
64+
resetPreviewStateForTests();
65+
vi.unstubAllGlobals();
66+
});
67+
68+
it("closes the video window and stream when its tab becomes a client-rendered PDF", async () => {
69+
await openServerPictureInPicture({ ...ref, tabId: "tab", seed: null });
70+
navigate("https://environment.test/api/assets/token/guide.PDF#page=2");
71+
expect(exitPictureInPicture).toHaveBeenCalledOnce();
72+
expect(stopTrack).toHaveBeenCalledOnce();
73+
});
74+
75+
it("keeps PiP for ordinary navigation and PDFs in another tab", async () => {
76+
await openServerPictureInPicture({ ...ref, tabId: "tab", seed: null });
77+
navigate("https://environment.test/api/assets/token/guide.pdf", "another-tab");
78+
navigate("https://example.com/paper.pdf");
79+
navigate("https://environment.test/api/assets/token/next.html");
80+
expect(exitPictureInPicture).not.toHaveBeenCalled();
81+
expect(stopTrack).not.toHaveBeenCalled();
82+
});
83+
84+
it("stops observing navigation when a PiP session closes", async () => {
85+
await openServerPictureInPicture({ ...ref, tabId: "tab", seed: null });
86+
closeServerPictureInPicture();
87+
navigate("https://environment.test/api/assets/token/guide.pdf");
88+
expect(exitPictureInPicture).toHaveBeenCalledOnce();
89+
expect(stopTrack).toHaveBeenCalledOnce();
90+
});

‎apps/web/src/browser/serverPictureInPicture.ts‎

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import {
33
createPreviewStreamClient,
44
type PreviewStreamClient,
55
} from "@t3tools/client-runtime/preview/server-browser-stream";
6-
import type { EnvironmentId } from "@t3tools/contracts";
6+
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
7+
import { scopedThreadKey } from "@t3tools/client-runtime/environment";
8+
import { previewPdfAssetUrl } from "@t3tools/shared/preview";
79
import { useSyncExternalStore } from "react";
810

911
import { readPreviewStreamAccess } from "~/state/previewStream";
12+
import { previewStateAtom, type ThreadPreviewState } from "~/previewStateStore";
13+
import { appAtomRegistry } from "~/rpc/atomRegistry";
1014

1115
interface WebKitVideo {
1216
webkitSetPresentationMode?: (mode: string) => void;
@@ -62,7 +66,7 @@ export function closeServerPictureInPicture(): void {
6266
/** Open during user activation, seeding the video with the currently visible frame. */
6367
export async function openServerPictureInPicture(input: {
6468
readonly environmentId: EnvironmentId;
65-
readonly threadId: string;
69+
readonly threadId: ThreadId;
6670
readonly tabId: string;
6771
readonly seed: HTMLCanvasElement | null;
6872
}): Promise<void> {
@@ -90,6 +94,7 @@ export async function openServerPictureInPicture(input: {
9094
let stopped = false;
9195
let refusals = 0;
9296
let retryTimer: ReturnType<typeof setTimeout> | null = null;
97+
let stopWatchingPreview = () => {};
9398
const onLeave = () => {
9499
if (active === current) closeServerPictureInPicture();
95100
};
@@ -136,6 +141,7 @@ export async function openServerPictureInPicture(input: {
136141
key: serverPictureInPictureKey(input.threadId, input.tabId),
137142
stop: () => {
138143
stopped = true;
144+
stopWatchingPreview();
139145
if (retryTimer !== null) clearTimeout(retryTimer);
140146
client?.stop();
141147
painter.stop();
@@ -172,5 +178,20 @@ export async function openServerPictureInPicture(input: {
172178
onLeave();
173179
throw error;
174180
}
175-
if (active === current) void connect(false);
181+
if (active === current) {
182+
const atom = previewStateAtom(scopedThreadKey(input));
183+
const closeForPdf = (state: ThreadPreviewState) => {
184+
const navigation = state.sessions[input.tabId]?.navStatus;
185+
if (
186+
navigation?._tag === "Success" &&
187+
previewPdfAssetUrl(navigation.url, navigation.url) !== null
188+
) {
189+
onLeave();
190+
}
191+
};
192+
// PDFs render in the client and cannot appear in the server's video stream.
193+
stopWatchingPreview = appAtomRegistry.subscribe(atom, closeForPdf);
194+
closeForPdf(appAtomRegistry.get(atom));
195+
if (active === current) void connect(false);
196+
}
176197
}

‎patches/@react-navigation%[email protected]‎

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
11
diff --git a/lib/module/views/NativeStackView.native.js b/lib/module/views/NativeStackView.native.js
2+
index 6882090..011ad0e 100644
23
--- a/lib/module/views/NativeStackView.native.js
34
+++ b/lib/module/views/NativeStackView.native.js
4-
@@ -3,3 +3,3 @@
5-
import { getDefaultHeaderHeight, getHeaderTitle, HeaderBackContext, HeaderHeightContext, HeaderShownContext, SafeAreaProviderCompat, useFrameSize } from '@react-navigation/elements';
6-
-import { NavigationProvider, StackActions, usePreventRemoveContext, useTheme } from '@react-navigation/native';
7-
+import { NavigationContext, NavigationRouteContext, StackActions, usePreventRemoveContext, useTheme } from '@react-navigation/native';
8-
import * as React from 'react';
9-
@@ -19,4 +19,14 @@
5+
@@ -17,8 +17,12 @@ const ANDROID_DEFAULT_HEADER_HEIGHT = 56;
6+
function isFabric() {
7+
return 'nativeFabricUIManager' in global;
108
}
119
+function SceneNavigationProvider({ presentationEnvelope, ...props }) {
12-
+ return presentationEnvelope ? props.children : /*#__PURE__*/_jsx(NavigationContext.Provider, {
13-
+ value: props.navigation,
14-
+ children: /*#__PURE__*/_jsx(NavigationRouteContext.Provider, {
15-
+ value: props.route,
16-
+ children: props.children
17-
+ })
18-
+ });
10+
+ return presentationEnvelope ? props.children : /*#__PURE__*/_jsx(NavigationProvider, props);
1911
+}
2012
const useNativeDriver = Platform.OS !== 'web';
2113
const SceneView = /*#__PURE__*/React.memo(function SceneView({
2214
+ presentationEnvelope,
2315
index,
24-
@@ -307,3 +317,4 @@
16+
focused,
17+
shouldFreeze,
18+
@@ -305,7 +309,8 @@ const SceneView = /*#__PURE__*/React.memo(function SceneView({
19+
}
20+
}
2521
});
2622
- return /*#__PURE__*/_jsx(NavigationProvider, {
2723
+ return /*#__PURE__*/_jsx(SceneNavigationProvider, {
2824
+ presentationEnvelope,
2925
route: route,
30-
@@ -429,2 +440,3 @@
26+
navigation: navigation,
27+
children: /*#__PURE__*/_jsx(ScreenStackItem, {
28+
@@ -427,6 +432,7 @@ const SceneView = /*#__PURE__*/React.memo(function SceneView({
29+
});
30+
});
3131
export function NativeStackView({
3232
+ presentationEnvelope,
3333
state,
34-
@@ -446,2 +458,13 @@
34+
navigation,
35+
descriptors,
36+
@@ -444,6 +450,17 @@ export function NativeStackView({
37+
return /*#__PURE__*/_jsx(SafeAreaProviderCompat, {
38+
children: /*#__PURE__*/_jsx(ScreenStack, {
3539
style: styles.container,
3640
+ onFinishTransitioning: () => {
3741
+ // Surface UIKit's transition-completion callback to every route of
@@ -45,10 +49,16 @@ diff --git a/lib/module/views/NativeStackView.native.js b/lib/module/views/Nativ
4549
+ });
4650
+ },
4751
children: state.routes.concat(state.preloadedRoutes).map((route, index) => {
48-
@@ -462,2 +485,3 @@
52+
const descriptor = descriptors[route.key] ?? preloadedDescriptors[route.key];
53+
const isFocused = state.index === index;
54+
@@ -460,6 +477,7 @@ export function NativeStackView({
55+
// due to component being unmounted. To avoid this, we don't freeze the previous screen there
56+
const shouldFreeze = isFabric() ? !isPreloaded && !isFocused && !isBelowFocused && !isModalOnIos : !isPreloaded && !isFocused && !isModalOnIos;
4957
return /*#__PURE__*/_jsx(SceneView, {
5058
+ presentationEnvelope,
5159
index: index,
60+
focused: isFocused,
61+
shouldFreeze: shouldFreeze,
5262
diff --git a/lib/module/views/useHeaderConfigProps.js b/lib/module/views/useHeaderConfigProps.js
5363
index 08055b2..8718847 100644
5464
--- a/lib/module/views/useHeaderConfigProps.js
@@ -249,57 +259,71 @@ index 5d63f41..e9c53ee 100644
249259
* A React Element to display as the item.
250260
*/
251261
diff --git a/src/views/NativeStackView.native.tsx b/src/views/NativeStackView.native.tsx
262+
index aa13bc7..ab4a9d1 100644
252263
--- a/src/views/NativeStackView.native.tsx
253264
+++ b/src/views/NativeStackView.native.tsx
254-
@@ -10,3 +10,4 @@
255-
import {
256-
- NavigationProvider,
257-
+ NavigationContext,
258-
+ NavigationRouteContext,
259-
type ParamListBase,
260-
@@ -54,3 +55,16 @@
265+
@@ -52,7 +52,14 @@ function isFabric() {
266+
return 'nativeFabricUIManager' in global;
267+
}
261268

262269
+// A presentation envelope contains descriptors from its owning navigator.
263270
+// Its synthetic route must not override those descriptors' focus state.
264-
+function SceneNavigationProvider({ presentationEnvelope, ...props }: { presentationEnvelope?: boolean; route: NativeStackDescriptor["route"]; navigation: NativeStackDescriptor["navigation"]; children: React.ReactNode }) {
265-
+ return presentationEnvelope ? props.children : (
266-
+ <NavigationContext.Provider value={props.navigation}>
267-
+ <NavigationRouteContext.Provider value={props.route}>
268-
+ {props.children}
269-
+ </NavigationRouteContext.Provider>
270-
+ </NavigationContext.Provider>
271-
+ );
271+
+function SceneNavigationProvider({ presentationEnvelope, ...props }: React.ComponentProps<typeof NavigationProvider> & { presentationEnvelope?: boolean }) {
272+
+ return presentationEnvelope ? props.children : <NavigationProvider {...props} />;
272273
+}
273274
+
274275
type SceneViewProps = {
275276
+ presentationEnvelope?: boolean;
276277
index: number;
277-
@@ -73,2 +87,3 @@
278+
focused: boolean;
279+
shouldFreeze: boolean;
280+
@@ -71,6 +78,7 @@ type SceneViewProps = {
281+
const useNativeDriver = Platform.OS !== 'web';
282+
278283
const SceneView = React.memo(function SceneView({
279284
+ presentationEnvelope,
280285
index,
281-
@@ -436,3 +451,3 @@
286+
focused,
287+
shouldFreeze,
288+
@@ -434,7 +442,7 @@ const SceneView = React.memo(function SceneView({
289+
);
290+
282291
return (
283292
- <NavigationProvider route={route} navigation={navigation}>
284293
+ <SceneNavigationProvider presentationEnvelope={presentationEnvelope} route={route} navigation={navigation}>
285294
<ScreenStackItem
286-
@@ -573,3 +588,3 @@
295+
screenId={route.key}
296+
activityState={isPreloaded ? 0 : 2}
297+
@@ -571,11 +579,12 @@ const SceneView = React.memo(function SceneView({
298+
</HeaderHeightContext.Provider>
299+
</AnimatedHeaderHeightContext.Provider>
287300
</ScreenStackItem>
288301
- </NavigationProvider>
289302
+ </SceneNavigationProvider>
290303
);
291-
@@ -578,2 +593,3 @@
304+
});
305+
292306
type Props = {
293307
+ presentationEnvelope?: boolean;
294308
state: StackNavigationState<ParamListBase>;
295-
@@ -588,2 +604,3 @@
309+
navigation: NativeStackNavigationHelpers;
310+
descriptors: NativeStackDescriptorMap;
311+
@@ -586,6 +595,7 @@ type Props = {
312+
};
313+
296314
export function NativeStackView({
297315
+ presentationEnvelope,
298316
state,
299-
@@ -635,2 +652,3 @@
317+
navigation,
318+
descriptors,
319+
@@ -633,6 +643,7 @@ export function NativeStackView({
320+
321+
return (
300322
<SceneView
301323
+ presentationEnvelope={presentationEnvelope}
302324
key={route.key}
325+
index={index}
326+
focused={isFocused}
303327
diff --git a/src/views/NativeStackView.tsx b/src/views/NativeStackView.tsx
304328
index c50a3fa..6305ed5 100644
305329
--- a/src/views/NativeStackView.tsx

0 commit comments

Comments
 (0)