The best way to control Claude Code, Codex and more from any computer or phone
fix(mobile): browser picture in picture opens from the header button (#17731)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 9, 2026 at 23:13 UTC
57b3780770a829ff81e62d8dc658683bf51d5155
1 file changed
+49
-31
apps/mobile/src/features/browser/preview-stream.browser.ts
+49
-31
@@ -50,7 +50,7 @@ const AGENT_CURSOR_SVG =
50
interface Viewer {
51
readonly stop: () => void;
52
readonly command: (input: PreviewStreamInput) => void;
53
- readonly togglePictureInPicture: () => Promise<void>;
53
+ readonly togglePictureInPicture: () => void;
54
}
55
56
let activeViewer: Viewer | null = null;
@@ -66,7 +66,7 @@ export function command(input: PreviewStreamInput) {
66
}
67
68
export function pictureInPicture() {
69
- void activeViewer?.togglePictureInPicture();
69
+ activeViewer?.togglePictureInPicture();
70
}
71
72
export function start(configuration: PreviewStreamConfiguration) {
@@ -147,6 +147,7 @@ export function start(configuration: PreviewStreamConfiguration) {
147
};
148
const painter = createPreviewFramePainter(canvas, () => {
149
if (!streaming && !stopped) reportStatus("streaming");
150
+ sendPictureInPictureFrame();
151
});
152
const send = (message: PreviewStreamInput) => client?.send(message) ?? false;
153
const clearInput = () => {
@@ -620,10 +621,16 @@ export function start(configuration: PreviewStreamConfiguration) {
621
const pictureInPictureSupported =
622
interactive &&
623
typeof canvas.captureStream === "function" &&
624
+ typeof CanvasCaptureMediaStreamTrack === "function" &&
625
((document.pictureInPictureEnabled === true &&
626
typeof videoPrototype.requestPictureInPicture === "function") ||
627
typeof videoPrototype.webkitSetPresentationMode === "function");
628
+ // Since the iOS 17 SDK, WebKit revokes the activation it grants an injected script when
629
+ // the script returns, so the native button's request must start before any await. The
630
+ // video is ready ahead of the tap: paused, and fed a frame only when it has none or
631
+ // floats, so the inline view captures nothing.
632
let video: PresentationVideo | null = null;
633
+ let track: CanvasCaptureMediaStreamTrack | null = null;
634
const pictureInPictureActive = () =>
635
video !== null &&
636
(document.pictureInPictureElement === video ||
@@ -635,16 +642,20 @@ export function start(configuration: PreviewStreamConfiguration) {
642
active: pictureInPictureActive(),
643
...(detail ? { detail } : {}),
644
});
638
- const ensureVideo = () => {
639
- if (video) return video;
645
+ // Called after each paint. A frame drawn before the player attached is lost, so retry.
646
+ const sendPictureInPictureFrame = () => {
647
+ if (video && (video.readyState === HTMLMediaElement.HAVE_NOTHING || pictureInPictureActive())) {
648
+ track?.requestFrame();
649
+ }
650
+ };
651
+ if (pictureInPictureSupported) {
652
+ const stream = canvas.captureStream(0);
653
+ const [captured] = stream.getVideoTracks();
654
const element: PresentationVideo = document.createElement("video");
655
element.muted = true;
656
element.playsInline = true;
643
- element.autoplay = true;
657
// Under the canvas at full size: WebKit pauses muted video it considers off screen.
645
- element.srcObject = canvas.captureStream();
646
- // A static page sends no new frames; repaint once so the stream has one.
647
- if (canvas.width > 0 && canvas.height > 0) canvas.getContext("2d")?.drawImage(canvas, 0, 0);
658
+ element.srcObject = stream;
659
for (const name of [
660
"enterpictureinpicture",
661
"leavepictureinpicture",
@@ -656,25 +667,38 @@ export function start(configuration: PreviewStreamConfiguration) {
667
});
668
}
669
container.prepend(element);
670
+ // Covers the letterbox, where a stale frame of another shape would show through.
671
+ canvas.style.background = configuration.background;
672
video = element;
660
- return element;
661
- };
662
- const togglePictureInPicture = async () => {
663
- if (!pictureInPictureSupported) return;
664
- try {
665
- if (pictureInPictureActive()) {
666
- if (document.pictureInPictureElement) await document.exitPictureInPicture();
667
- else video?.webkitSetPresentationMode?.("inline");
668
- return;
669
- }
670
- const element = ensureVideo();
671
- await element.play();
672
- if (document.pictureInPictureEnabled) await element.requestPictureInPicture();
673
- else element.webkitSetPresentationMode?.("picture-in-picture");
674
- } catch (error) {
673
+ track = captured instanceof CanvasCaptureMediaStreamTrack ? captured : null;
674
+ }
675
+ const togglePictureInPicture = () => {
676
+ const element = video;
677
+ if (!element) return;
678
+ if (pictureInPictureActive()) {
679
+ if (document.pictureInPictureElement) void document.exitPictureInPicture().catch(() => {});
680
+ else element.webkitSetPresentationMode?.("inline");
681
+ return;
682
+ }
683
+ if (element.readyState === HTMLMediaElement.HAVE_NOTHING) {
684
+ reportPictureInPicture("The page has not shown yet. Try again in a moment.");
685
+ return;
686
+ }
687
+ // The page may have changed since the video's last frame. WebKit captures only on a draw.
688
+ track?.requestFrame();
689
+ canvas.getContext("2d")?.drawImage(canvas, 0, 0);
690
+ const fail = (error: unknown) => {
691
+ element.pause();
692
reportPictureInPicture(
693
error instanceof Error ? error.message : "Picture in picture is unavailable.",
694
);
695
+ };
696
+ if (document.pictureInPictureEnabled) {
697
+ const entering = element.requestPictureInPicture();
698
+ void Promise.all([entering, element.play()]).catch(fail);
699
+ } else {
700
+ element.webkitSetPresentationMode?.("picture-in-picture");
701
+ void element.play().catch(fail);
702
}
703
};
704
@@ -691,14 +715,8 @@ export function start(configuration: PreviewStreamConfiguration) {
715
painter.stop();
716
client?.stop();
717
client = null;
694
- if (video) {
695
- for (const track of video.srcObject instanceof MediaStream
696
- ? video.srcObject.getTracks()
697
- : []) {
698
- track.stop();
699
- }
700
- video.srcObject = null;
701
- }
718
+ track?.stop();
719
+ if (video) video.srcObject = null;
720
},
721
command: (message) => {
722
send(message);