diff --git a/apps/desktop/src/ipc/DesktopIpc.test.ts b/apps/desktop/src/ipc/DesktopIpc.test.ts index 1973ab49cefeb..91c73ad45d665 100644 --- a/apps/desktop/src/ipc/DesktopIpc.test.ts +++ b/apps/desktop/src/ipc/DesktopIpc.test.ts @@ -9,7 +9,15 @@ vi.mock("electron", () => ({})); import * as ElectronWindow from "../electron/ElectronWindow.ts"; import * as DesktopIpc from "./DesktopIpc.ts"; -it.effect("answers only the main window's top frame", () => +const popout = (id: number, url: string) => { + const mainFrame = {}; + return { + sender: { id, getType: () => "window", getURL: () => url, mainFrame }, + senderFrame: mainFrame, + }; +}; + +it.effect("answers only the app's own windows' top frames", () => Effect.gen(function* () { const mainFrame = {}; const invoke = new Map(); @@ -25,7 +33,7 @@ it.effect("answers only the main window's top frame", () => Layer.provide( Layer.mock(ElectronWindow.ElectronWindow, { main: Effect.succeedSome({ - webContents: { id: 1, mainFrame }, + webContents: { id: 1, mainFrame, getURL: () => "t3://app/index.html#/" }, } as unknown as Electron.BrowserWindow), }), ), @@ -46,6 +54,20 @@ it.effect("answers only the main window's top frame", () => app: { sender: { id: 1 }, senderFrame: mainFrame }, appSubframe: { sender: { id: 1 }, senderFrame: {} }, previewGuest: { sender: { id: 2 }, senderFrame: {} }, + popout: popout(3, "t3://app/index.html#/browser-window/tab-1"), + popoutSubframe: { + ...popout(3, "t3://app/index.html#/browser-window/tab-1"), + senderFrame: {}, + }, + popoutOnAppRoute: popout(3, "t3://app/index.html#/settings"), + foreignWindow: popout(4, "https://example.com/#/browser-window/tab-1"), + blankWindow: popout(6, "about:blank#/browser-window/tab-1"), + otherHost: popout(7, "t3://other/index.html#/browser-window/tab-1"), + fileWindow: popout(8, "file:///index.html#/browser-window/tab-1"), + guestOnAppUrl: { + ...popout(5, "t3://app/index.html#/browser-window/tab-1"), + sender: { ...popout(5, "").sender, getType: () => "webview" }, + }, }; const results: Record = {}; for (const [name, sender] of Object.entries(senders)) { @@ -62,6 +84,14 @@ it.effect("answers only the main window's top frame", () => app: ["secret", "secret"], appSubframe: ["rejected", undefined], previewGuest: ["rejected", undefined], + popout: ["secret", "secret"], + popoutSubframe: ["rejected", undefined], + popoutOnAppRoute: ["rejected", undefined], + foreignWindow: ["rejected", undefined], + blankWindow: ["rejected", undefined], + otherHost: ["rejected", undefined], + fileWindow: ["rejected", undefined], + guestOnAppUrl: ["rejected", undefined], }); }), ); diff --git a/apps/desktop/src/ipc/DesktopIpc.ts b/apps/desktop/src/ipc/DesktopIpc.ts index 69d4f70c15a0e..2c543412da2f6 100644 --- a/apps/desktop/src/ipc/DesktopIpc.ts +++ b/apps/desktop/src/ipc/DesktopIpc.ts @@ -6,9 +6,15 @@ import * as Schema from "effect/Schema"; import * as Scope from "effect/Scope"; import * as ElectronWindow from "../electron/ElectronWindow.ts"; +import { isSameOriginRendererNavigation } from "../window/RendererNavigation.ts"; export interface DesktopIpcInvokeEvent { - readonly sender: { readonly id: number }; + readonly sender: { + readonly id: number; + readonly getType?: () => string; + readonly getURL?: () => string; + readonly mainFrame?: unknown; + }; readonly senderFrame?: unknown; } @@ -118,6 +124,7 @@ export const make = ( Effect.gen(function* () { yield* Effect.annotateCurrentSpan({ channel }); if (!(yield* isTrustedSender(event))) { + yield* Effect.logWarning("Rejected IPC from an untrusted sender."); return yield* new DesktopIpcUntrustedSenderError({ channel }); } return yield* handler(raw, event); @@ -185,17 +192,33 @@ export const layer = (ipcMain: DesktopIpcMain) => Effect.gen(function* () { const electronWindow = yield* ElectronWindow.ElectronWindow; return make(ipcMain, (event) => - Effect.map( - electronWindow.main, - (main) => - Option.isSome(main) && - main.value.webContents.id === event.sender.id && - main.value.webContents.mainFrame === event.senderFrame, - ), + Effect.map(electronWindow.main, (main) => { + if (Option.isNone(main)) return false; + const app = main.value.webContents; + if (app.id === event.sender.id) return app.mainFrame === event.senderFrame; + return isPopoutWindow(event, app.getURL()); + }), ); }), ); +// A pop-out browser window runs the app itself, so it is as trusted as the main window. +// Preview guests are "webview" contents, and their pop-ups never load the app origin. +// The hash is routing, not a secret: any app-origin page is already as trusted as the main window. +const isPopoutWindow = (event: DesktopIpcInvokeEvent, appUrl: string): boolean => { + const sender = event.sender; + if (sender.getType?.() !== "window" || sender.mainFrame !== event.senderFrame) return false; + try { + const url = new URL(sender.getURL?.() ?? ""); + return ( + isSameOriginRendererNavigation({ applicationUrl: appUrl, navigationUrl: url.href }) && + url.hash.startsWith("#/browser-window/") + ); + } catch { + return false; + } +}; + export const layerTest = (ipcMain: DesktopIpcMain) => Layer.succeed( DesktopIpc, diff --git a/apps/desktop/src/ipc/channels.ts b/apps/desktop/src/ipc/channels.ts index 4a99d52b0e79d..a7f8d38597d7e 100644 --- a/apps/desktop/src/ipc/channels.ts +++ b/apps/desktop/src/ipc/channels.ts @@ -119,6 +119,8 @@ export const PREVIEW_COPY_ARTIFACT_CHANNEL = "desktop:preview-copy-artifact"; export const PREVIEW_PICTURE_IN_PICTURE_OPEN_CHANNEL = "desktop:preview-pip-open"; export const PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL = "desktop:preview-pip-close"; export const PREVIEW_PICTURE_IN_PICTURE_FRAME_CHANNEL = "desktop:preview-pip-frame"; +export const PREVIEW_TAB_WINDOW_OPEN_CHANNEL = "desktop:preview-tab-window-open"; +export const PREVIEW_TAB_WINDOW_CLOSE_CHANNEL = "desktop:preview-tab-window-close"; export const PREVIEW_RECORDING_START_CHANNEL = "desktop:preview-recording-start"; export const PREVIEW_RECORDING_STOP_CHANNEL = "desktop:preview-recording-stop"; export const PREVIEW_RECORDING_SAVE_CHANNEL = "desktop:preview-recording-save"; diff --git a/apps/desktop/src/ipc/methods/preview.test.ts b/apps/desktop/src/ipc/methods/preview.test.ts index bd7e44bec8307..3577374898039 100644 --- a/apps/desktop/src/ipc/methods/preview.test.ts +++ b/apps/desktop/src/ipc/methods/preview.test.ts @@ -9,6 +9,7 @@ import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import { beforeEach, describe, expect, it, vi } from "vite-plus/test"; +import * as ElectronWindow from "../../electron/ElectronWindow.ts"; import * as PreviewManager from "../../preview/Manager.ts"; import * as PreviewIpc from "./preview.ts"; import * as DesktopIpc from "../DesktopIpc.ts"; @@ -110,6 +111,52 @@ describe("preview IPC methods", () => { }), ); + effectIt.effect.each([ + { name: "sends opened links only to the main window, never a pop-out", mainAlive: true }, + { name: "opens the link in the same tab when the main window is gone", mainAlive: false }, + ])("$name", ({ mainAlive }) => + Effect.gen(function* () { + let openLink: ((event: unknown) => Effect.Effect) | undefined; + const mainSend = vi.fn(); + const navigate = vi.fn(() => Effect.void); + const sendAll = vi.fn(() => Effect.void); + const listen = () => Effect.void; + yield* PreviewIpc.installPreviewEventForwarding().pipe( + Effect.provideService(ElectronWindow.ElectronWindow, { + main: mainAlive + ? Effect.succeedSome({ + isDestroyed: () => false, + webContents: { send: mainSend }, + } as unknown as Electron.BrowserWindow) + : Effect.succeedNone, + sendAll, + } as unknown as ElectronWindow.ElectronWindow["Service"]), + Effect.provideService(PreviewManager.PreviewManager, { + subscribeStateChanges: listen, + subscribeRecordingInputs: listen, + subscribePointerEvents: listen, + subscribeFindResults: listen, + subscribeOpenLinks: (listener: (event: unknown) => Effect.Effect) => { + openLink = listener; + return Effect.void; + }, + navigate, + } as unknown as PreviewManager.PreviewManager["Service"]), + ); + yield* openLink!({ tabId: "tab-1", url: "https://example.com" }); + if (mainAlive) { + expect(mainSend).toHaveBeenCalledOnce(); + expect(navigate).not.toHaveBeenCalled(); + } else { + expect(navigate).toHaveBeenCalledWith("tab-1", "https://example.com"); + } + expect(sendAll).not.toHaveBeenCalledWith( + expect.stringContaining("open-link"), + expect.anything(), + ); + }), + ); + beforeEach(() => { fromPartition.mockClear(); }); diff --git a/apps/desktop/src/ipc/methods/preview.ts b/apps/desktop/src/ipc/methods/preview.ts index 9dc0465c1e55a..16aec5f5bcb1c 100644 --- a/apps/desktop/src/ipc/methods/preview.ts +++ b/apps/desktop/src/ipc/methods/preview.ts @@ -7,6 +7,7 @@ import { DesktopPreviewConfigInputSchema, DesktopPreviewFindInPageInputSchema, DesktopPreviewNavigateInputSchema, + DesktopPreviewOpenTabWindowInputSchema, DesktopPreviewRecordingArtifactSchema, DesktopPreviewRecordingSaveInputSchema, DesktopPreviewRegisterWebviewInputSchema, @@ -63,8 +64,16 @@ export const installPreviewEventForwarding = Effect.fn( yield* manager.subscribePointerEvents((event) => electronWindow.sendAll(IpcChannels.PREVIEW_POINTER_EVENT_CHANNEL, event), ); + // Only the main window opens tabs; pop-out windows would open a duplicate. + // With the main window closed (macOS keeps pop-outs alive), open the link in place. yield* manager.subscribeOpenLinks((event) => - electronWindow.sendAll(IpcChannels.PREVIEW_OPEN_LINK_CHANNEL, event), + Effect.flatMap(electronWindow.main, (main) => + Option.isSome(main) && !main.value.isDestroyed() + ? Effect.sync(() => + main.value.webContents.send(IpcChannels.PREVIEW_OPEN_LINK_CHANNEL, event), + ) + : manager.navigate(event.tabId, event.url).pipe(Effect.ignore), + ), ); yield* manager.subscribeFindResults((event) => electronWindow.sendAll(IpcChannels.PREVIEW_FIND_RESULT_CHANNEL, event), @@ -293,6 +302,20 @@ const openPictureInPicture = tabMethod( "desktop.ipc.preview.openPictureInPicture", (manager, tabId) => manager.openPictureInPicture(tabId), ); +const openTabWindow = DesktopIpc.makeIpcMethod({ + channel: IpcChannels.PREVIEW_TAB_WINDOW_OPEN_CHANNEL, + payload: DesktopPreviewOpenTabWindowInputSchema, + result: Schema.Void, + handler: Effect.fn("desktop.ipc.preview.openTabWindow")(function* ({ tabId, partition }) { + const manager = yield* PreviewManager.PreviewManager; + yield* manager.openTabWindow(tabId, partition); + }), +}); +const closeTabWindow = tabMethod( + IpcChannels.PREVIEW_TAB_WINDOW_CLOSE_CHANNEL, + "desktop.ipc.preview.closeTabWindow", + (manager, tabId) => manager.closeTabWindow(tabId), +); const closePictureInPicture = tabMethod( IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL, "desktop.ipc.preview.closePictureInPicture", @@ -583,6 +606,8 @@ export const methods = [ copyArtifactToClipboard, openPictureInPicture, closePictureInPicture, + openTabWindow, + closeTabWindow, startRecording, stopRecording, saveRecording, diff --git a/apps/desktop/src/preload.ts b/apps/desktop/src/preload.ts index 280c5ad6c6716..a5efdb8696727 100644 --- a/apps/desktop/src/preload.ts +++ b/apps/desktop/src/preload.ts @@ -500,6 +500,11 @@ contextBridge.exposeInMainWorld("desktopBridge", { close: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_PICTURE_IN_PICTURE_CLOSE_CHANNEL, { tabId }), }, + tabWindow: { + open: (tabId, partition) => + ipcRenderer.invoke(IpcChannels.PREVIEW_TAB_WINDOW_OPEN_CHANNEL, { tabId, partition }), + close: (tabId) => ipcRenderer.invoke(IpcChannels.PREVIEW_TAB_WINDOW_CLOSE_CHANNEL, { tabId }), + }, recording: { onInput: (listener) => { const wrappedListener = (_event: Electron.IpcRendererEvent, event: unknown) => { diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts index 2091ff3c7f8f2..725a1036ac075 100644 --- a/apps/desktop/src/preview/Manager.test.ts +++ b/apps/desktop/src/preview/Manager.test.ts @@ -33,6 +33,8 @@ import * as BrowserSession from "./BrowserSession.ts"; import * as DesktopBrowserHost from "./DesktopBrowserHost.ts"; import * as PreviewManager from "./Manager.ts"; import * as PreviewPasskeys from "./Passkeys.ts"; +import * as DesktopPrimaryPaste from "../app/DesktopPrimaryPaste.ts"; +import { PRIMARY_PASTE_DISABLED_ARGUMENT } from "./GuestProtocol.ts"; describe("fitPictureInPictureContentSize", () => { it("does not collapse toward the minimum size when orientation changes repeatedly", () => { @@ -250,6 +252,7 @@ const managerLayer = (platform: NodeJS.Platform = "darwin") => attachGuest: () => () => {}, }), ), + Layer.provideMerge(Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: false })), Layer.provideMerge(layerEnvironment), Layer.provideMerge(layerFileSystem), Layer.provideMerge(Path.layer), @@ -679,6 +682,7 @@ describe("PreviewManager", () => { Effect.provide( Layer.mergeAll( layerEnvironment, + Layer.succeed(DesktopPrimaryPaste.DesktopPrimaryPaste, { enabled: true }), layerBrowserSession, Layer.mock(DesktopBrowserHost.DesktopBrowserHost)({ attach: () => {}, @@ -1868,12 +1872,15 @@ describe("PreviewManager", () => { expect(replacementListenerSpies.on).not.toHaveBeenCalled(); yield* manager.closeTab("tab_close_register_race"); const recordingExit = yield* Effect.exit(manager.startRecording("tab_close_register_race")); + const tabWindowExit = yield* Effect.exit( + manager.openTabWindow("tab_close_register_race", "persist:test"), + ); yield* Deferred.succeed(continueCloseCleanup, undefined); yield* Fiber.join(closeFiber); const recreated = yield* Fiber.join(recreateFiber); const registrationExit = yield* Fiber.await(registrationFiber); - for (const exit of [registrationExit, recordingExit] as ReadonlyArray< + for (const exit of [registrationExit, recordingExit, tabWindowExit] as ReadonlyArray< Exit.Exit >) { expect(Exit.isFailure(exit)).toBe(true); @@ -1892,6 +1899,86 @@ describe("PreviewManager", () => { ), ); + effectIt.effect("keeps a stale tab window from attaching to a recreated tab", () => + withManager((manager) => + Effect.gen(function* () { + const hostHandlers = new Map) => void>(); + const tabWindow = { + webContents: { + id: 900, + on: (event: string, handler: (...args: ReadonlyArray) => void) => + hostHandlers.set(event, handler), + setWindowOpenHandler: vi.fn(), + }, + isDestroyed: () => false, + once: vi.fn(), + close: vi.fn(), + loadURL: vi.fn(async () => undefined), + }; + browserWindowConstructor.mockImplementation(function () { + return tabWindow; + }); + yield* manager.setMainWindow({ + isDestroyed: () => false, + once: vi.fn(), + webContents: { getURL: () => "t3code://app/" }, + } as never); + + yield* manager.createTab("tab_stale_window"); + yield* manager.openTabWindow("tab_stale_window", "persist:t3code-preview-test"); + yield* manager.closeTab("tab_stale_window"); + yield* manager.createTab("tab_stale_window"); + const guest = { id: 77, on: vi.fn() }; + tabWindow.close.mockClear(); + hostHandlers.get("did-attach-webview")!({}, guest); + + expect(tabWindow.close).toHaveBeenCalledOnce(); + expect(guest.on).not.toHaveBeenCalled(); + }), + ), + ); + + effectIt.effect("detaches a tab with no page as soon as its window opens", () => + withManager((manager) => + Effect.gen(function* () { + const hostHandlers = new Map) => void>(); + browserWindowConstructor.mockImplementation(function () { + return { + webContents: { + id: 901, + on: (event: string, handler: (...args: ReadonlyArray) => void) => + hostHandlers.set(event, handler), + setWindowOpenHandler: vi.fn(), + }, + isDestroyed: () => false, + once: vi.fn(), + close: vi.fn(), + loadURL: vi.fn(async () => undefined), + }; + }); + yield* manager.setMainWindow({ + isDestroyed: () => false, + once: vi.fn(), + webContents: { getURL: () => "t3code://app/" }, + } as never); + const detached: Array = []; + yield* manager.subscribeStateChanges((_tabId, state) => + Effect.sync(() => detached.push(state.detached)), + ); + + yield* manager.createTab("tab_empty_window"); + yield* manager.openTabWindow("tab_empty_window", "persist:t3code-preview-test"); + + expect(detached.at(-1)).toBe(true); + const preferences: Electron.WebPreferences = {}; + hostHandlers.get("will-attach-webview")!({}, preferences, { + partition: "persist:t3code-preview-test", + }); + expect(preferences.additionalArguments).toContain(PRIMARY_PASTE_DISABLED_ARGUMENT); + }), + ), + ); + effectIt.effect("captures final pixels after warmup fails and paint readiness settles", () => withManager((manager) => Effect.gen(function* () { diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts index 1f28826bcc196..559b2e001cff7 100644 --- a/apps/desktop/src/preview/Manager.ts +++ b/apps/desktop/src/preview/Manager.ts @@ -64,6 +64,7 @@ import * as SynchronizedRef from "effect/SynchronizedRef"; import { msg } from "gt-i18n"; import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; +import * as DesktopPrimaryPaste from "../app/DesktopPrimaryPaste.ts"; import { m } from "../i18n/messages.ts"; import * as DesktopRendererHistory from "../telemetry/DesktopRendererHistory.ts"; import { @@ -74,6 +75,8 @@ import { import * as DesktopBrowserHost from "./DesktopBrowserHost.ts"; import * as PreviewPasskeys from "./Passkeys.ts"; import * as BrowserSession from "./BrowserSession.ts"; +import { hardenPreviewWebviewPreferences } from "./WebviewPreferences.ts"; +import { isSameOriginRendererNavigation } from "../window/RendererNavigation.ts"; import { ANNOTATION_CAPTURED_CHANNEL, ANNOTATION_SEND_ENABLED_CHANNEL, @@ -115,6 +118,7 @@ export interface PreviewTabState { canGoForward: boolean; zoomFactor: number; pictureInPicture: boolean; + detached: boolean; colorScheme: DesktopPreviewColorScheme; audioMuted: boolean; audible: boolean; @@ -570,8 +574,10 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function const random = yield* SecureRandom.SecureRandom; const parentScope = yield* Scope.Scope; const browserHost = yield* DesktopBrowserHost.DesktopBrowserHost; + const primaryPaste = yield* DesktopPrimaryPaste.DesktopPrimaryPaste; const browserSession = yield* BrowserSession.BrowserSession; const passkeys = yield* PreviewPasskeys.PreviewPasskeys; + const { isPartition: isBrowserPartition } = yield* BrowserSession.BrowserSession; const context = yield* Effect.context(); const runFork = Effect.runForkWith(context); const runPromise = Effect.runPromiseWith(context); @@ -1738,7 +1744,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function currentMainWindow = undefined; frameCaptureWindowOpen = false; mainWindowCleanupFiber = runFork( - Effect.all([closeAllPictureInPicture(), stopAllRecordings()], { + Effect.all([closeAllPictureInPicture(), stopAllRecordings(), closeAllTabWindows()], { concurrency: "unbounded", discard: true, }).pipe(Effect.ignore), @@ -1772,6 +1778,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function canGoForward: false, zoomFactor: normalizeZoomFactor(defaults?.zoomFactor), pictureInPicture: false, + detached: false, colorScheme: defaults?.colorScheme ?? "system", ...(defaults?.serverTab === undefined ? {} : { serverTab: defaults.serverTab }), audioMuted: false, @@ -1810,10 +1817,11 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function [ cancelPickElement(tabId), closePictureInPicture(tabId), + closeTabWindow(tabId), stopFrameCapture(tabId, "recording"), ], { - concurrency: 3, + concurrency: 4, discard: true, }, ); @@ -1851,6 +1859,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function canGoForward: false, zoomFactor: DEFAULT_ZOOM_FACTOR, pictureInPicture: false, + detached: false, colorScheme: "system", audioMuted: false, audible: false, @@ -1898,7 +1907,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function !wc || wc.isDestroyed() || wc.getType() !== "webview" || - (Option.isSome(mainWindow) && wc.hostWebContents !== mainWindow.value.webContents) + (Option.isSome(mainWindow) && + wc.hostWebContents !== mainWindow.value.webContents && + !tabWindowHostIds.has(wc.hostWebContents?.id ?? -1)) ) { return yield* new PreviewWebContentsNotFoundError({ tabId, webContentsId }); } @@ -2113,6 +2124,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function canGoForward: current?.canGoForward ?? false, zoomFactor: current?.zoomFactor ?? DEFAULT_ZOOM_FACTOR, pictureInPicture: current?.pictureInPicture ?? false, + detached: current?.detached ?? false, colorScheme: current?.colorScheme ?? "system", // Keep audibility until Chromium replaces the document; clearing it here can lose the final transition. audioMuted: current?.audioMuted ?? false, @@ -3126,6 +3138,160 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function }, ); + const tabWindows = new Map(); + const tabWindowHostIds = new Set(); + + const closeTabWindow = Effect.fn("PreviewManager.closeTabWindow")(function* (tabId: string) { + const window = tabWindows.get(tabId); + if (!window || window.isDestroyed()) return yield* update(tabId, { detached: false }); + yield* attempt({ operation: "tabWindow.close", tabId }, () => window.close()); + }); + + const closeAllTabWindows = Effect.fn("PreviewManager.closeAllTabWindows")(function* () { + yield* Effect.forEach( + [...tabWindows.keys()], + (tabId) => closeTabWindow(tabId).pipe(Effect.ignore), + { + discard: true, + }, + ); + }); + + // The guest cannot move between hosts, so the pop-out loads the current URL in a fresh webview. + const openTabWindow = Effect.fn("PreviewManager.openTabWindow")(function* ( + tabId: string, + partition: string, + ) { + // Fail fast instead of queueing behind an in-flight close. + if ((yield* Ref.get(closingTabIdsRef)).has(tabId)) { + return yield* new PreviewTabNotFoundError({ tabId }); + } + const created = yield* withTabLifecycleLock(tabId, createTabWindowUnlocked(tabId, partition)); + if (!created) return; + const { window, appUrl } = created; + yield* attemptPromise({ operation: "tabWindow.load", tabId }, () => + window.loadURL(appUrl.href), + ).pipe( + Effect.onError(() => + attempt({ operation: "tabWindow.close", tabId }, () => window.close()).pipe(Effect.ignore), + ), + ); + }); + + const createTabWindowUnlocked = Effect.fn("PreviewManager.createTabWindowUnlocked")(function* ( + tabId: string, + partition: string, + ) { + const tab = (yield* SynchronizedRef.get(tabsRef)).get(tabId); + if (!tab || (yield* Ref.get(closingTabIdsRef)).has(tabId)) { + return yield* new PreviewTabNotFoundError({ tabId }); + } + const generation = tabLifecycleGenerations.get(tabId); + const existing = tabWindows.get(tabId); + if (existing && !existing.isDestroyed()) { + yield* attempt({ operation: "tabWindow.focus", tabId }, () => existing.focus()); + return null; + } + if (!isBrowserPartition(partition)) { + return yield* new PreviewOperationError({ + operation: "tabWindow.partition", + tabId, + cause: new Error("Not a browser partition."), + }); + } + const mainWindow = yield* Ref.get(mainWindowRef); + if (Option.isNone(mainWindow) || mainWindow.value.isDestroyed()) { + return yield* new PreviewOperationError({ + operation: "tabWindow.appUrl", + tabId, + cause: new Error("Main window is not available."), + }); + } + const appUrl = new URL(mainWindow.value.webContents.getURL()); + appUrl.hash = `#/browser-window/${encodeURIComponent(tabId)}`; + const window = yield* attempt( + { operation: "tabWindow.create", tabId }, + () => + new BrowserWindow({ + width: 1200, + height: 800, + title: "Browser", + autoHideMenuBar: true, + // The toolbar row (40px) doubles as the title bar; the lights sit centered in it. + ...(hostPlatform === "darwin" && { + titleBarStyle: "hidden" as const, + trafficLightPosition: { x: 14, y: 13 }, + }), + webPreferences: { + preload: path.join(path.dirname(pictureInPicturePreloadPath), "preload.cjs"), + contextIsolation: true, + nodeIntegration: false, + sandbox: true, + webviewTag: true, + }, + }), + ); + const host = window.webContents; + const hostId = host.id; + tabWindows.set(tabId, window); + tabWindowHostIds.add(hostId); + // A tab with no page never attaches a webview here, so nothing else would hand it to this window. + if (tab.webContentsId === null) yield* update(tabId, { detached: true }); + host.on("will-attach-webview", (event, webPreferences, params) => { + if (typeof params.partition !== "string" || !isBrowserPartition(params.partition)) { + event.preventDefault(); + return; + } + hardenPreviewWebviewPreferences(webPreferences, passkeys.bridgeEnabled, primaryPaste.enabled); + }); + host.on("did-attach-webview", (_event, contents) => { + // The tab was closed or recreated since this window opened; never bind to the replacement. + if (tabLifecycleGenerations.get(tabId) !== generation) { + window.close(); + return; + } + contents.on("page-title-updated", (_titleEvent, nextTitle) => { + if (!window.isDestroyed()) window.setTitle(nextTitle); + }); + runFork( + prepareWebview(contents).pipe( + Effect.andThen( + withTabLifecycleLock( + tabId, + registerWebviewUnlocked(tabId, contents.id, generation).pipe( + // Under the lock, so a close or recreate can't land between registering and this write. + Effect.andThen( + Effect.suspend(() => + window.isDestroyed() || tabWindows.get(tabId) !== window + ? Effect.void + : update(tabId, { detached: true }), + ), + ), + ), + ), + ), + Effect.catchCause((cause) => + Effect.logWarning("Preview tab window failed to register.", { tabId, cause }), + ), + ), + ); + }); + host.setWindowOpenHandler(() => ({ action: "deny" })); + // The preload exposes desktopBridge, so the window must never leave the app origin. + host.on("will-navigate", (event, url) => { + if (!isSameOriginRendererNavigation({ applicationUrl: appUrl.href, navigationUrl: url })) { + event.preventDefault(); + } + }); + window.once("closed", () => { + tabWindowHostIds.delete(hostId); + if (tabWindows.get(tabId) === window) tabWindows.delete(tabId); + if (tabLifecycleGenerations.get(tabId) !== generation) return; + runFork(update(tabId, { detached: false }).pipe(Effect.ignore)); + }); + return { window, appUrl }; + }); + const openPictureInPicture = Effect.fn("PreviewManager.openPictureInPicture")(function* ( tabId: string, ) { @@ -3716,6 +3882,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function hardReload, navigate, openPictureInPicture, + openTabWindow, + closeTabWindow, openDevTools, pickElement, updateAnnotationVoice, @@ -3984,6 +4152,11 @@ export class PreviewManager extends Context.Service< readonly revealArtifact: (path: string) => Effect.Effect; readonly copyArtifactToClipboard: (path: string) => Effect.Effect; readonly openPictureInPicture: (tabId: string) => Effect.Effect; + readonly openTabWindow: ( + tabId: string, + partition: string, + ) => Effect.Effect; + readonly closeTabWindow: (tabId: string) => Effect.Effect; readonly closePictureInPicture: (tabId: string) => Effect.Effect; readonly startRecording: ( tabId: string, @@ -4151,6 +4324,8 @@ export const make = Effect.gen(function* PreviewManagerMake() { revealArtifact: operations.revealArtifact, copyArtifactToClipboard: operations.copyArtifactToClipboard, openPictureInPicture: operations.openPictureInPicture, + openTabWindow: operations.openTabWindow, + closeTabWindow: operations.closeTabWindow, closePictureInPicture: operations.closePictureInPicture, startRecording: operations.startRecording, stopRecording: operations.stopRecording, diff --git a/apps/desktop/src/preview/WebviewPreferences.ts b/apps/desktop/src/preview/WebviewPreferences.ts index 4dc9d9aa6b732..8e21954f7c8f3 100644 --- a/apps/desktop/src/preview/WebviewPreferences.ts +++ b/apps/desktop/src/preview/WebviewPreferences.ts @@ -1,3 +1,29 @@ +import { PASSKEY_BRIDGE_ARGUMENT, PRIMARY_PASTE_DISABLED_ARGUMENT } from "./GuestProtocol.ts"; + /** sandbox:true is required with contextIsolation:false or the shared page global would expose Node and IPC. */ export const PREVIEW_WEBVIEW_PREFERENCES = "contextIsolation=false,sandbox=true,nodeIntegration=false"; + +export const hardenPreviewWebviewPreferences = ( + webPreferences: Electron.WebPreferences, + passkeyBridgeEnabled: boolean, + primaryPasteEnabled: boolean, +): void => { + webPreferences.sandbox = true; + webPreferences.nodeIntegration = false; + webPreferences.nodeIntegrationInSubFrames = false; + webPreferences.contextIsolation = false; + webPreferences.disableHtmlFullscreenWindowResize = true; + if (passkeyBridgeEnabled) { + webPreferences.additionalArguments = [ + ...(webPreferences.additionalArguments ?? []), + PASSKEY_BRIDGE_ARGUMENT, + ]; + } + if (!primaryPasteEnabled) { + webPreferences.additionalArguments = [ + ...(webPreferences.additionalArguments ?? []), + PRIMARY_PASTE_DISABLED_ARGUMENT, + ]; + } +}; diff --git a/apps/desktop/src/window/DesktopWindow.test.ts b/apps/desktop/src/window/DesktopWindow.test.ts index 46311dbba8122..63f5e8815c4a7 100644 --- a/apps/desktop/src/window/DesktopWindow.test.ts +++ b/apps/desktop/src/window/DesktopWindow.test.ts @@ -17,6 +17,7 @@ import { vi } from "vite-plus/test"; vi.mock("electron", async (importOriginal) => ({ ...(await importOriginal()), + app: { on: vi.fn(), off: vi.fn() }, session: { fromPartition: vi.fn(() => ({ getUserAgent: vi.fn(() => "Mozilla/5.0 Electron/41.5.0 t3code/1.2.3"), @@ -534,45 +535,6 @@ describe("DesktopWindow", () => { } }); - it("rejects renderer navigations whose origin only serializes as null", () => { - for (const applicationUrl of ["t3code://app/", "t3code-dev://app/"]) { - assert.isTrue( - DesktopWindow.isSameOriginRendererNavigation({ - applicationUrl, - navigationUrl: `${applicationUrl}threads?id=1#latest`, - }), - applicationUrl, - ); - } - for (const navigationUrl of [ - "t3code://other/", - "t3code://app.example/", - "t3code://app:8080/", - "t3code://user@other/", - "t3code:app", - "t3code-dev://app/", - "file:///etc/passwd", - "data:text/html,

hi

", - "javascript:alert(1)", - "about:blank", - "blob:t3code://app/0b5e2c4a", - ]) { - assert.isFalse( - DesktopWindow.isSameOriginRendererNavigation({ - applicationUrl: "t3code://app/", - navigationUrl, - }), - navigationUrl, - ); - } - assert.isFalse( - DesktopWindow.isSameOriginRendererNavigation({ - applicationUrl: "data:text/html,app", - navigationUrl: "data:text/html,other", - }), - ); - }); - it.effect( "opens and reopens the window without backend readiness when local execution is disabled", () => diff --git a/apps/desktop/src/window/DesktopWindow.ts b/apps/desktop/src/window/DesktopWindow.ts index 741e03b79b3c2..f2a8780c69379 100644 --- a/apps/desktop/src/window/DesktopWindow.ts +++ b/apps/desktop/src/window/DesktopWindow.ts @@ -31,10 +31,7 @@ import { TRACKPAD_SCROLL_END_CHANNEL, WINDOW_FULLSCREEN_STATE_CHANNEL, } from "../ipc/channels.ts"; -import { - PASSKEY_BRIDGE_ARGUMENT, - PRIMARY_PASTE_DISABLED_ARGUMENT, -} from "../preview/GuestProtocol.ts"; +import { hardenPreviewWebviewPreferences } from "../preview/WebviewPreferences.ts"; import * as PreviewManager from "../preview/Manager.ts"; import * as PreviewPasskeys from "../preview/Passkeys.ts"; import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts"; @@ -43,6 +40,8 @@ import * as ElectronApp from "../electron/ElectronApp.ts"; import { m } from "../i18n/messages.ts"; import * as DesktopRendererHistory from "../telemetry/DesktopRendererHistory.ts"; import { makeQuitShortcutHandler } from "./QuitHold.ts"; +import { isSameOriginRendererNavigation } from "./RendererNavigation.ts"; + import { copyContextMenuImage } from "./ContextMenuImage.ts"; const TITLEBAR_HEIGHT = 40; @@ -271,24 +270,6 @@ function buildConnectingSplashDataUrl(shouldUseDarkColors: boolean): string { return `data:text/html;charset=utf-8,${encodeURIComponent(html)}`; } -export function isSameOriginRendererNavigation(input: { - readonly applicationUrl: string; - readonly navigationUrl: string; -}): boolean { - try { - const applicationUrl = new URL(input.applicationUrl); - const navigationUrl = new URL(input.navigationUrl); - // Node serializes custom-scheme origins as "null", even for different hosts and schemes. - return ( - applicationUrl.host !== "" && - navigationUrl.protocol === applicationUrl.protocol && - navigationUrl.host === applicationUrl.host - ); - } catch { - return false; - } -} - function isRetryableDevelopmentRendererLoadFailure(input: { readonly applicationUrl: string; readonly errorCode: number; @@ -627,23 +608,11 @@ export const make = Effect.gen(function* () { event.preventDefault(); return; } - webPreferences.sandbox = true; - webPreferences.nodeIntegration = false; - webPreferences.nodeIntegrationInSubFrames = false; - webPreferences.contextIsolation = false; - webPreferences.disableHtmlFullscreenWindowResize = true; - if (previewPasskeys.bridgeEnabled) { - webPreferences.additionalArguments = [ - ...(webPreferences.additionalArguments ?? []), - PASSKEY_BRIDGE_ARGUMENT, - ]; - } - if (!primaryPaste.enabled) { - webPreferences.additionalArguments = [ - ...(webPreferences.additionalArguments ?? []), - PRIMARY_PASTE_DISABLED_ARGUMENT, - ]; - } + hardenPreviewWebviewPreferences( + webPreferences, + previewPasskeys.bridgeEnabled, + primaryPaste.enabled, + ); }); const contextMenuContents = new WeakSet(); @@ -756,6 +725,14 @@ export const make = Effect.gen(function* () { installContextMenu(window, contents); void runPromise(previewManager.prepareWebview(contents)); }); + // Pop-out browser windows host their own guest, which needs the same menu. + const onWindowCreated = (_event: Electron.Event, created: Electron.BrowserWindow) => { + created.webContents.on("did-attach-webview", (_attachEvent, contents) => { + installContextMenu(created, contents); + }); + }; + Electron.app.on("browser-window-created", onWindowCreated); + window.once("closed", () => Electron.app.off("browser-window-created", onWindowCreated)); window.webContents.setWindowOpenHandler(({ url }) => { if (Option.isSome(ElectronShell.parseSafeExternalUrl(url))) { diff --git a/apps/desktop/src/window/RendererNavigation.test.ts b/apps/desktop/src/window/RendererNavigation.test.ts new file mode 100644 index 0000000000000..4b511166fcb9d --- /dev/null +++ b/apps/desktop/src/window/RendererNavigation.test.ts @@ -0,0 +1,45 @@ +import { assert, describe, it } from "@effect/vitest"; + +import { isSameOriginRendererNavigation } from "./RendererNavigation.ts"; + +describe("isSameOriginRendererNavigation", () => { + it("compares protocol and host instead of origin", () => { + for (const applicationUrl of ["t3code://app/", "t3code-dev://app/"]) { + assert.isTrue( + isSameOriginRendererNavigation({ + applicationUrl, + navigationUrl: `${applicationUrl}threads?id=1#latest`, + }), + applicationUrl, + ); + } + for (const navigationUrl of [ + "t3code://other/", + "t3code://app.example/", + "t3code://app:8080/", + "t3code://user@other/", + "t3code:app", + "t3code-dev://app/", + "file:///etc/passwd", + "data:text/html,

hi

", + "javascript:alert(1)", + "about:blank", + "blob:t3code://app/0b5e2c4a", + "not a url", + ]) { + assert.isFalse( + isSameOriginRendererNavigation({ + applicationUrl: "t3code://app/", + navigationUrl, + }), + navigationUrl, + ); + } + assert.isFalse( + isSameOriginRendererNavigation({ + applicationUrl: "data:text/html,app", + navigationUrl: "data:text/html,other", + }), + ); + }); +}); diff --git a/apps/desktop/src/window/RendererNavigation.ts b/apps/desktop/src/window/RendererNavigation.ts new file mode 100644 index 0000000000000..74d64ae31079f --- /dev/null +++ b/apps/desktop/src/window/RendererNavigation.ts @@ -0,0 +1,17 @@ +export function isSameOriginRendererNavigation(input: { + readonly applicationUrl: string; + readonly navigationUrl: string; +}): boolean { + try { + const applicationUrl = new URL(input.applicationUrl); + const navigationUrl = new URL(input.navigationUrl); + // Node serializes custom-scheme origins as "null", even for different hosts and schemes. + return ( + applicationUrl.host !== "" && + navigationUrl.protocol === applicationUrl.protocol && + navigationUrl.host === applicationUrl.host + ); + } catch { + return false; + } +} diff --git a/apps/web/src/appInit.tsx b/apps/web/src/appInit.tsx index ae8e685c5c9ec..b7891163c8139 100644 --- a/apps/web/src/appInit.tsx +++ b/apps/web/src/appInit.tsx @@ -309,8 +309,10 @@ export function startAppServices(): AppRouter { startHostFreezeToasts(); startLegacyThreadMigrationToast(); startPermissionUpdateNotices(router); - startThreadNotifications(appAtomRegistry, router); - startGuideRunNotifications(appAtomRegistry, router); + if (!window.location.hash.startsWith("#/browser-window/")) { + startThreadNotifications(appAtomRegistry, router); + startGuideRunNotifications(appAtomRegistry, router); + } if (isElectron) startDesktopUpdateLaunchNotice(); return router; diff --git a/apps/web/src/browser/ElectronBrowserHost.tsx b/apps/web/src/browser/ElectronBrowserHost.tsx index e818b4b400dc1..a96f38e538215 100644 --- a/apps/web/src/browser/ElectronBrowserHost.tsx +++ b/apps/web/src/browser/ElectronBrowserHost.tsx @@ -53,6 +53,7 @@ function hostedBrowserSessions( runtimeTabId: previewRuntimeTabId(threadRef, previewState.serverEpoch, snapshot.tabId), pictureInPicture: previewState.desktopByTabId[snapshot.tabId]?.pictureInPicture ?? false, + detached: previewState.desktopByTabId[snapshot.tabId]?.detached ?? false, zoomFactor: previewState.desktopByTabId[snapshot.tabId]?.zoomFactor ?? 1, })) : []; @@ -119,7 +120,8 @@ export function startElectronBrowserHost(): () => void { preview.onPointerEvent((event) => { useBrowserPointerStore.getState().apply(event); }), - preview.onOpenLink(openLinkInPreview), + // Open-link events reach every window; only the main window acts, so one link opens one tab. + ...(popoutTabId ? [] : [preview.onOpenLink(openLinkInPreview)]), preview.onTunnelAccessRequest(({ requestId, environmentId }) => { void readPreviewTunnelUrl(environmentId).then((url) => preview.resolveTunnelAccess(requestId, url), @@ -131,40 +133,49 @@ export function startElectronBrowserHost(): () => void { }; } +// A pop-out window hosts exactly one tab; the main window shows a placeholder for it. +const popoutMatch = /^#\/browser-window\/([^/?]+)/.exec(window.location.hash); +const popoutTabId = popoutMatch ? decodeURIComponent(popoutMatch[1]!) : null; + export function ElectronBrowserHost() { const previewByThreadKey = useActivePreviewSessions(); const primaryEnvironmentId = useAtomValue(primaryEnvironmentIdAtom); useDesktopHostedPreviewSessions(primaryEnvironmentId); - const sessions = hostedBrowserSessions(previewByThreadKey, primaryEnvironmentId); + const sessions = hostedBrowserSessions(previewByThreadKey, primaryEnvironmentId).filter( + (session) => !popoutTabId || session.runtimeTabId === popoutTabId, + ); if (!isElectron) return null; return (
- {sessions.map(({ threadRef, snapshot, runtimeTabId, pictureInPicture, zoomFactor }) => { - const url = snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url; - return ( - - ); - })} + {sessions.map( + ({ threadRef, snapshot, runtimeTabId, pictureInPicture, detached, zoomFactor }) => { + const url = snapshot.navStatus._tag === "Idle" ? null : snapshot.navStatus.url; + return ( + + ); + }, + )}
); } diff --git a/apps/web/src/browser/HostedBrowserWebview.tsx b/apps/web/src/browser/HostedBrowserWebview.tsx index 023fb03c3aa17..5cbbdfd4d1b39 100644 --- a/apps/web/src/browser/HostedBrowserWebview.tsx +++ b/apps/web/src/browser/HostedBrowserWebview.tsx @@ -7,6 +7,9 @@ import { useShallow } from "zustand/react/shallow"; import { useEffect, useEffectEvent, useRef, useState } from "react"; import { previewBridge } from "~/components/preview/previewBridge"; +import { Button } from "~/components/ui/button"; +import { toastManager } from "~/components/ui/toast"; +import { takeTabWindowRequest } from "./pendingTabWindows"; import { preventThumbButtonFocus, takeThumbButtonNavigation, @@ -58,6 +61,7 @@ export function HostedBrowserWebview(props: { readonly initialUrl: string | null; readonly viewport: PreviewViewportSetting; readonly pictureInPicture: boolean; + readonly detached: boolean; /** Fixed for the tab lifetime; Electron reads partition before guest attachment. */ readonly profileId: string | undefined; readonly zoomFactor: number; @@ -74,6 +78,7 @@ export function HostedBrowserWebview(props: { initialUrl, viewport, pictureInPicture, + detached, zoomFactor, profileId, serverDriven = false, @@ -141,8 +146,17 @@ export function HostedBrowserWebview(props: { const [webviewGeneration, setWebviewGeneration] = useState(0); const [recoverySrc, setRecoverySrc] = useState(initialSrc); const active = presentation.visible && presentation.rect !== null; - const shown = active || pictureInPicture; + const shown = active || pictureInPicture || detached; const reportedShown = useEffectEvent(() => serverDriven && shown); + // The pop-out window's guest replaced ours; coming back needs a fresh guest at the current URL. + const [renderedDetached, setRenderedDetached] = useState(detached); + if (renderedDetached !== detached) { + setRenderedDetached(detached); + if (!detached) { + setRecoverySrc(initialUrl ?? initialSrc); + setWebviewGeneration((generation) => generation + 1); + } + } const remountCrashedWebview = useEffectEvent(() => { setRecoverySrc(initialUrl ?? initialSrc); setWebviewGeneration((generation) => generation + 1); @@ -171,6 +185,15 @@ export function HostedBrowserWebview(props: { if (Number.isInteger(webContentsId) && webContentsId > 0) { await bridge.registerWebview(runtimeTabId, webContentsId); withDesktopTab(runtimeTabId, () => bridge.setShown(runtimeTabId, reportedShown())); + if (takeTabWindowRequest(runtimeTabId)) { + await bridge.tabWindow.open(runtimeTabId, config.partition).catch((error) => { + toastManager.add({ + type: "error", + title: "Unable to open browser window", + description: error instanceof Error ? error.message : "An error occurred.", + }); + }); + } } } catch {} })(); @@ -216,8 +239,9 @@ export function HostedBrowserWebview(props: { }, [ clientSettingsHydrated, config, + // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Detaching unmounts the webview and a crash recovery remounts it under a new key, so the listeners attach to the new element. + detached, runtimeTabId, - // oxlint-disable-next-line react/exhaustive-effect-dependencies -- A crash recovery remounts the webview under a new key, so the listeners attach to the new element. webviewGeneration, ]); @@ -373,56 +397,77 @@ export function HostedBrowserWebview(props: { onChange={commitViewportChange} /> ) : null} -
- + This tab is open in its own window. + +
+ ) : ( +
-
+ > + + + )} {active && effectiveViewport._tag !== "fill" && !fittedSourceViewport ? ( <> /\.(?:html?|pdf)$/i.test(path.split(/[?#]/, 1)[0] ?? ""); @@ -64,6 +66,8 @@ export async function openUrlInPreview(input: { readonly openPreview: OpenPreviewMutation; readonly profileId?: PreviewOpenInput["profileId"]; readonly background?: boolean; + /** Opens in the background and pops the tab out into its own desktop window. */ + readonly tabWindow?: boolean; }): Promise> { // Read before any await: a side chat pane that unmounts meanwhile drops its host mapping. const threadRef = rightPanelHostRef(input.threadRef); @@ -94,8 +98,12 @@ export async function openUrlInPreview(input: { }); return mapAtomCommandResult(result, (snapshot) => { rememberPreviewUrl(threadRef, input.url); - if (input.background) { + if (input.background || input.tabWindow) { updatePreviewServerSnapshot(threadRef, snapshot); + if (input.tabWindow) { + const { serverEpoch } = readThreadPreviewState(threadRef); + requestTabWindow(previewRuntimeTabId(threadRef, serverEpoch, snapshot.tabId)); + } // Restore focus unless the user picked a tab while opening this one. if ( previousActiveTabId && diff --git a/apps/web/src/browser/pendingTabWindows.ts b/apps/web/src/browser/pendingTabWindows.ts new file mode 100644 index 0000000000000..aebd0439f44e9 --- /dev/null +++ b/apps/web/src/browser/pendingTabWindows.ts @@ -0,0 +1,9 @@ +// Runtime tab ids to pop out as soon as their hosted webview registers with the desktop bridge. +// ponytail: an id whose tab never gets a hosted webview (server-rendered tab) stays here until reload. +const pending = new Set(); + +export const requestTabWindow = (runtimeTabId: string): void => { + pending.add(runtimeTabId); +}; + +export const takeTabWindowRequest = (runtimeTabId: string): boolean => pending.delete(runtimeTabId); diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index f6062475ac25b..e9738e86327bc 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -131,6 +131,7 @@ import { resolveExternalWebLinkHost, showExternalLinkContextMenu, } from "./chat/externalLinkContextMenu"; +import { previewBridge } from "./preview/previewBridge"; import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip"; import { Button } from "./ui/button"; import { stackedThreadToast, toastManager } from "./ui/toast"; @@ -769,7 +770,7 @@ function useChatMarkdownState({ if (!readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)) return; await pullRequestLinking.changeLink(threadRef, href, linked); }; - const openExternalLinkInPreview = (url: string) => { + const openExternalLinkInPreview = (url: string, tabWindow = false) => { if (!threadRef || !canOperatePreview) { return Promise.resolve( AsyncResult.failure( @@ -781,7 +782,7 @@ function useChatMarkdownState({ ), ); } - return openUrlInPreview({ threadRef, url, openPreview }).then((result) => { + return openUrlInPreview({ threadRef, url, openPreview, tabWindow }).then((result) => { if (result._tag === "Success") recordVisitForThread(threadRef, url); else if (!isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); @@ -1405,6 +1406,7 @@ const CHAT_MARKDOWN_COMPONENTS = { threadLinkAction, position: { x: event.clientX, y: event.clientY }, showContextMenu: (items, position) => api.contextMenu.show(items, position, source), + canOpenInWindow: previewBridge !== undefined, openInPreview: async (target) => { const result = await openExternalLinkInPreview(target); if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { @@ -1414,6 +1416,15 @@ const CHAT_MARKDOWN_COMPONENTS = { ); } }, + openInWindow: async (target) => { + const result = await openExternalLinkInPreview(target, true); + if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { + reportMarkdownActionFailure( + { operation: "open-link-in-window", target }, + result.cause, + ); + } + }, openExternal: (target) => api.shell.openExternal(target), copyLink: (target) => writeTextToClipboard(target, { diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx index 128f3b16d1dde..6d6cf4a8c543e 100644 --- a/apps/web/src/components/ChatView.tsx +++ b/apps/web/src/components/ChatView.tsx @@ -3942,7 +3942,7 @@ export default function ChatView(props: ChatViewProps) { {canOperatePreview && activeThreadRef && activeEnvironmentServerBrowser && !embedded ? ( ) : null} - {activeThreadRef && shownPreviewMiniPlayer && !embedded ? ( + {activeThreadRef && shownPreviewMiniPlayer && canvasPreview && !embedded ? ( { it.each([ @@ -26,4 +29,14 @@ describe("external chat link context menu", () => { ])("rejects unsafe external web-link href %s", (href) => { expect(resolveExternalWebLinkHref(href)).toBeNull(); }); + + it.each([ + [true, true, ["open-in-preview", "open-in-window", "open-external", "copy-link"]], + [true, false, ["open-in-preview", "open-external", "copy-link"]], + [false, true, ["open-external", "copy-link"]], + ])("with preview %s and window %s offers %j", (canOpenInPreview, canOpenInWindow, ids) => { + expect( + externalLinkContextMenuItems({ canOpenInPreview, canOpenInWindow }).map((item) => item.id), + ).toEqual(ids); + }); }); diff --git a/apps/web/src/components/chat/externalLinkContextMenu.ts b/apps/web/src/components/chat/externalLinkContextMenu.ts index ed65545552fb3..73ba16c8cc364 100644 --- a/apps/web/src/components/chat/externalLinkContextMenu.ts +++ b/apps/web/src/components/chat/externalLinkContextMenu.ts @@ -5,6 +5,7 @@ import { m } from "../../i18n/messages"; type ExternalLinkContextMenuAction = | "open-in-preview" + | "open-in-window" | "open-external" | "copy-link" | "link-to-thread" @@ -13,6 +14,7 @@ type ExternalLinkContextMenuAction = type ExternalLinkContextMenuFailureOperation = | "show-link-context-menu" | "open-link-in-preview" + | "open-link-in-window" | "open-link-external" | "copy-link" | "link-pull-request-to-thread" @@ -20,6 +22,7 @@ type ExternalLinkContextMenuFailureOperation = const FAILURE_OPERATION_BY_ACTION = { "open-in-preview": "open-link-in-preview", + "open-in-window": "open-link-in-window", "open-external": "open-link-external", "copy-link": "copy-link", "link-to-thread": "link-pull-request-to-thread", @@ -28,18 +31,20 @@ const FAILURE_OPERATION_BY_ACTION = { const EXTERNAL_LINK_CONTEXT_MENU_ITEMS = [ { id: "open-in-preview", label: msg("Open in integrated browser") }, + { id: "open-in-window", label: msg("Open in new integrated browser window") }, { id: "open-external", label: msg("Open in system browser") }, { id: "copy-link", label: msg("Copy Link") }, ] as const satisfies readonly ContextMenuItem[]; -function externalLinkContextMenuItems(options: { +export function externalLinkContextMenuItems(options: { readonly canOpenInPreview: boolean; + readonly canOpenInWindow: boolean; readonly threadLinkAction?: "link-to-thread" | "unlink-from-thread" | undefined; }): readonly ContextMenuItem[] { - const items = ( - options.canOpenInPreview - ? EXTERNAL_LINK_CONTEXT_MENU_ITEMS - : EXTERNAL_LINK_CONTEXT_MENU_ITEMS.filter((item) => item.id !== "open-in-preview") + const items = EXTERNAL_LINK_CONTEXT_MENU_ITEMS.filter( + (item) => + (item.id !== "open-in-preview" || options.canOpenInPreview) && + (item.id !== "open-in-window" || (options.canOpenInPreview && options.canOpenInWindow)), ).map((item) => ({ id: item.id, label: m(item.label) })); if (options.threadLinkAction === undefined) return items; return [ @@ -58,12 +63,14 @@ interface ShowExternalLinkContextMenuOptions { readonly href: string; readonly position: { readonly x: number; readonly y: number }; readonly canOpenInPreview?: boolean; + readonly canOpenInWindow?: boolean; readonly threadLinkAction?: "link-to-thread" | "unlink-from-thread" | undefined; readonly showContextMenu: ( items: readonly ContextMenuItem[], position: { readonly x: number; readonly y: number }, ) => Promise; readonly openInPreview: (href: string) => Promise; + readonly openInWindow?: (href: string) => Promise; readonly openExternal: (href: string) => Promise; readonly copyLink: (href: string) => Promise; readonly updateThreadLink?: (href: string, linked: boolean) => Promise; @@ -96,9 +103,11 @@ export async function showExternalLinkContextMenu({ href, position, canOpenInPreview = true, + canOpenInWindow = false, threadLinkAction, showContextMenu, openInPreview, + openInWindow, openExternal, copyLink, updateThreadLink, @@ -107,7 +116,7 @@ export async function showExternalLinkContextMenu({ let action: ExternalLinkContextMenuAction | null; try { action = await showContextMenu( - externalLinkContextMenuItems({ canOpenInPreview, threadLinkAction }), + externalLinkContextMenuItems({ canOpenInPreview, canOpenInWindow, threadLinkAction }), position, ); } catch (cause) { @@ -118,6 +127,8 @@ export async function showExternalLinkContextMenu({ try { if (action === "open-in-preview") { await openInPreview(href); + } else if (action === "open-in-window") { + await openInWindow?.(href); } else if (action === "open-external") { await openExternal(href); } else if (action === "copy-link") { diff --git a/apps/web/src/components/preview/PreviewChromeRow.tsx b/apps/web/src/components/preview/PreviewChromeRow.tsx index 26f190569e377..75eb4e77fa17e 100644 --- a/apps/web/src/components/preview/PreviewChromeRow.tsx +++ b/apps/web/src/components/preview/PreviewChromeRow.tsx @@ -41,7 +41,9 @@ interface Props { recording?: boolean | undefined; onPictureInPicture?: (() => void) | undefined; pictureInPicture?: boolean | undefined; + pictureInPictureLabel?: string | undefined; pictureInPictureDisabled?: boolean | undefined; + windowTitleBar?: boolean | undefined; onPickElement?: (() => void) | undefined; pickActive?: boolean | undefined; pickDisabled?: boolean | undefined; @@ -71,7 +73,9 @@ export function PreviewChromeRow({ recording, onPictureInPicture, pictureInPicture, + pictureInPictureLabel, pictureInPictureDisabled, + windowTitleBar, onPickElement, pickActive, pickDisabled, @@ -80,6 +84,9 @@ export function PreviewChromeRow({ leadingActions, }: Props) { const gt = useGT(); + const pipLabel = + pictureInPictureLabel ?? + (pictureInPicture ? gt("Close floating preview") : gt("Float preview over chat")); const [draft, setDraft] = useState(url); const [inputFocused, setInputFocused] = useState(false); @@ -95,7 +102,11 @@ export function PreviewChromeRow({
@@ -288,9 +299,7 @@ export function PreviewChromeRow({ variant={pictureInPicture ? "secondary" : "ghost"} size="icon-xs" onClick={onPictureInPicture} - aria-label={ - pictureInPicture ? gt("Close floating preview") : gt("Float preview over chat") - } + aria-label={pipLabel} aria-pressed={pictureInPicture ? "true" : "false"} type="button" disabled={pictureInPictureDisabled} @@ -299,9 +308,7 @@ export function PreviewChromeRow({ > - - {pictureInPicture ? gt("Close floating preview") : gt("Float preview over chat")} - + {pipLabel} ) : null} {trailingActions} diff --git a/apps/web/src/components/preview/PreviewMoreMenu.tsx b/apps/web/src/components/preview/PreviewMoreMenu.tsx index c4fabfa97d6df..638137fdf0290 100644 --- a/apps/web/src/components/preview/PreviewMoreMenu.tsx +++ b/apps/web/src/components/preview/PreviewMoreMenu.tsx @@ -38,7 +38,7 @@ export interface PreviewMoreMenuActions { readonly clearCookies: () => void; readonly clearCache: () => void; readonly openDevTools?: () => void; - readonly toggleNativePictureInPicture?: () => void; + readonly toggleTabWindow?: () => void; readonly find?: () => void; } @@ -49,7 +49,7 @@ interface Props { colorScheme: DesktopPreviewColorScheme; deviceToolbarVisible: boolean; onToggleDeviceToolbar: () => void; - nativePictureInPicture: boolean; + tabWindow: boolean; profileName: string | undefined; findShortcutLabel?: string | null; move?: { readonly label: string; readonly onMove: () => void }; @@ -89,7 +89,7 @@ export function PreviewMoreMenu({ colorScheme, deviceToolbarVisible, onToggleDeviceToolbar, - nativePictureInPicture, + tabWindow, profileName, findShortcutLabel, move, @@ -116,11 +116,9 @@ export function PreviewMoreMenu({ Open DevTools ) : null} - {actions.toggleNativePictureInPicture ? ( - - {nativePictureInPicture - ? gt("Close separate preview window") - : gt("Open separate preview window")} + {actions.toggleTabWindow ? ( + + {tabWindow ? gt("Bring back to panel") : gt("Open in separate window")} ) : null} {move ? {move.label} : null} diff --git a/apps/web/src/components/preview/PreviewView.browser.test.tsx b/apps/web/src/components/preview/PreviewView.browser.test.tsx index 115aec44c5ab3..f86eaa522add7 100644 --- a/apps/web/src/components/preview/PreviewView.browser.test.tsx +++ b/apps/web/src/components/preview/PreviewView.browser.test.tsx @@ -189,6 +189,11 @@ import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId"; import { usePreviewMiniPlayerStore } from "~/previewMiniPlayerStore"; import { dispatchPreviewAction } from "./previewActionBus"; import { PreviewView } from "./PreviewView"; +import { + PoppedOutAnnotationReceiver, + sendPreviewAnnotationToChatWindow, +} from "./previewAnnotationDelivery"; +import { applyPreviewDesktopState } from "~/previewStateStore"; import { ToastProvider } from "~/components/ui/toast"; const TEST_THREAD_REF = { @@ -348,6 +353,52 @@ describe("PreviewView", () => { expect(mocks.setAnnotationSendEnabled).toHaveBeenLastCalledWith(TEST_RUNTIME_TAB_ID, true); }); + it("accepts pop-out annotations only for an authorized current detached tab", async () => { + setup(); + applyPreviewDesktopState(TEST_THREAD_REF, "tab-1", { + hasWebContents: true, + navStatus: { _tag: "Idle" }, + canGoBack: false, + canGoForward: false, + loading: false, + zoomFactor: 1, + pictureInPicture: false, + detached: true, + colorScheme: "system", + audioMuted: false, + audible: false, + controller: "none", + favicon: null, + }); + using cleanup = new DisposableStack(); + cleanup.defer(() => applyPreviewDesktopState(TEST_THREAD_REF, "tab-1", null)); + await using _screen = await render(); + const message = { + threadRef: TEST_THREAD_REF, + tabId: "tab-1", + runtimeTabId: TEST_RUNTIME_TAB_ID, + picked: { ...annotation("popout-annotation"), screenshot: null }, + screenshotFailed: false, + }; + mocks.readEnvironmentScope.mockReturnValueOnce(false).mockReturnValue(true); + const channel = new BroadcastChannel("t3code:preview-annotations"); + cleanup.defer(() => channel.close()); + for (const data of [ + message, + { ...message, picked: null }, + { ...message, runtimeTabId: "stale-runtime" }, + { ...message, tabId: "closed-tab" }, + { ...message, threadRef: { ...TEST_THREAD_REF, environmentId: "other-environment" } }, + ]) { + // oxlint-disable-next-line unicorn/require-post-message-target-origin -- BroadcastChannel has no target origin + channel.postMessage(data); + } + sendPreviewAnnotationToChatWindow(message); + await vi.waitFor(() => expect(mocks.addPreviewAnnotation).toHaveBeenCalledOnce()); + expect(mocks.addPreviewAnnotation).toHaveBeenCalledWith(TEST_THREAD_REF, message.picked); + expect(mocks.addImage).not.toHaveBeenCalled(); + }); + it("still sends annotation text when the picked element's crop is malformed", async () => { setup(); const picked = annotation("annotation-2", { diff --git a/apps/web/src/components/preview/PreviewView.tsx b/apps/web/src/components/preview/PreviewView.tsx index bb62228b93736..82ccdcd4173f2 100644 --- a/apps/web/src/components/preview/PreviewView.tsx +++ b/apps/web/src/components/preview/PreviewView.tsx @@ -36,9 +36,7 @@ import { removeUrlForThread, useThreadRecentHistory, } from "~/browserHistoryStore"; -import { type ComposerImageAttachment, useComposerDraftStore } from "~/composerDraftStore"; -import { capturePreviewAnnotationScreenshot } from "~/lib/previewAnnotation"; -import { readComposerImageBytes } from "../chat/composerAttachmentFiles"; +import type { ComposerImageAttachment } from "~/composerDraftStore"; import { ensureLocalApi } from "~/localApi"; import { getClientSettings } from "~/hooks/useSettings"; import { @@ -50,6 +48,7 @@ import { resolveDiscoveredServerUrl, resolveExternalServerUrl, } from "~/browser/browserTargetResolver"; +import { usePreviewWebviewConfig } from "~/browser/previewWebviewConfigState"; import { useEnvironmentSupportsServerBrowser } from "~/state/entities"; import { useEnvironment, @@ -72,6 +71,10 @@ import { randomUUID } from "~/lib/utils"; import { primaryServerKeybindingsAtom } from "~/state/server"; import { PreviewAnnotationSpeech } from "./PreviewAnnotationSpeech"; import { previewBridge } from "./previewBridge"; +import { + addPreviewAnnotationToDraft, + sendPreviewAnnotationToChatWindow, +} from "./previewAnnotationDelivery"; import { subscribePreviewAction } from "./previewActionBus"; import { closePreviewSession } from "./closePreviewSession"; import { openPreviewSession } from "./openPreviewSession"; @@ -91,13 +94,15 @@ import type { MessageResolver } from "~/i18n/messages"; import { revealInFileExplorerLabel } from "./fileExplorerLabel"; import { Badge } from "~/components/ui/badge"; import { BrowserSurfaceSlot } from "~/browser/BrowserSurfaceSlot"; +import { useMediaQuery } from "~/hooks/useMediaQuery"; +import { RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY } from "~/rightPanelLayout"; import { alternatePreviewRuntime, useDesktopLoadsLoopback, useRendersServerTabNatively, } from "~/browser/previewRuntime"; import { ServerBrowserSurface, type ServerBrowserHandle } from "~/browser/ServerBrowserSurface"; -import { cn } from "~/lib/utils"; +import { cn, isMacPlatform } from "~/lib/utils"; import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore"; import { usePreviewSession } from "./usePreviewSession"; import { ZoomIndicator } from "./ZoomIndicator"; @@ -183,6 +188,8 @@ export function PreviewView({ const gt = useGT(); const messages = useMessages(); const urlInputRef = useRef(null); + // Narrow windows show the panel as a sheet (--z-sheet: 46); the surface must sit above it. + const surfaceZIndex = useMediaQuery(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY) ? 47 : 30; const [find, setFind] = useState<{ tabId: string; requestId: number } | null>(null); const [pickActive, setPickActive] = useState(false); const canSendAnnotation = @@ -201,8 +208,6 @@ export function PreviewView({ const miniPlayerTabId = usePreviewMiniPlayerStore((state) => selectThreadPreviewMiniPlayerTabId(state.byThreadKey, threadRef), ); - const addPreviewAnnotation = useComposerDraftStore((store) => store.addPreviewAnnotation); - const addImage = useComposerDraftStore((store) => store.addImage); const environmentHttpBaseUrl = useEnvironmentHttpBaseUrl(threadRef.environmentId); const open = useAtomCommand(previewEnvironment.open); const closePreview = useAtomCommand(previewEnvironment.close, "preview close"); @@ -269,6 +274,11 @@ export function PreviewView({ const controller = desktopOverlay?.controller ?? "none"; const viewport = snapshot?.viewport ?? FILL_PREVIEW_VIEWPORT; const browserDefaults = useBrowserDefaults(); + // Same lookup as ElectronBrowserHost so the pop-out joins the webview's partition. + const webviewPartition = usePreviewWebviewConfig( + threadRef.environmentId, + snapshot?.profileId, + )?.partition; // IPC interprets undefined profile ids as all profiles. const activeProfileId = snapshot?.profileId ?? DEFAULT_BROWSER_PROFILE_ID; const activeProfileName = previewProfileName(browserDefaults.profiles, activeProfileId, messages); @@ -533,7 +543,9 @@ export function PreviewView({ ...(nativeServerTab && previewBridge ? { openDevTools: desktopCall(previewBridge.openDevTools), - toggleNativePictureInPicture: () => handleNativePictureInPicture(), + ...(webviewPartition + ? { toggleTabWindow: () => handleTabWindow(webviewPartition) } + : {}), ...(canFindInPage ? { find: handleFind } : {}), } : {}), @@ -557,20 +569,21 @@ export function PreviewView({ ?.clearCache(threadRef.environmentId, activeProfileId) .catch(() => undefined), openDevTools: desktopCall(previewBridge.openDevTools), - toggleNativePictureInPicture: () => handleNativePictureInPicture(), + ...(webviewPartition ? { toggleTabWindow: () => handleTabWindow(webviewPartition) } : {}), ...(canFindInPage ? { find: handleFind } : {}), } : null; - const handleNativePictureInPicture = () => { + const isPopoutWindow = window.location.hash.startsWith("#/browser-window/"); + const handleTabWindow = (partition: string) => { if (!previewBridge || !runtimeTabId) return; - const operation = desktopOverlay?.pictureInPicture - ? previewBridge.pictureInPicture.close - : previewBridge.pictureInPicture.open; - void operation(runtimeTabId).catch((error) => { + const request = desktopOverlay?.detached + ? previewBridge.tabWindow.close(runtimeTabId) + : previewBridge.tabWindow.open(runtimeTabId, partition); + void request.catch((error) => { toastManager.add({ type: "error", - title: gt("Unable to update popped-out preview"), + title: gt("Unable to update browser window"), description: thrownErrorMessage(error, m) ?? gt("An error occurred."), }); }); @@ -875,38 +888,21 @@ export function PreviewView({ submitted = true; } const { annotation: picked, submission, screenshotFailed = false } = result; - // Send annotations even if the optional crop fails or stalls; drop failed screenshots so prompts cannot - // claim an attachment. - const capture = capturePreviewAnnotationScreenshot(picked); - const cropDropped = screenshotFailed || capture.status === "failed"; - const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked; - if (capture.status === "captured") await readComposerImageBytes(capture.file); - addPreviewAnnotation(threadRef, annotation); - if (cropDropped) { - toastManager.add( - stackedThreadToast({ - type: "error", - title: gt("Could not capture the picked element"), - description: gt("The annotation was kept without the screenshot."), - }), - ); - } - const screenshotFile = capture.status === "captured" ? capture.file : null; - const image = - screenshotFile && annotation.screenshot - ? ({ - type: "image", - id: annotation.id, - name: screenshotFile.name, - mimeType: screenshotFile.type, - sizeBytes: screenshotFile.size, - previewUrl: annotation.screenshot.dataUrl, - file: screenshotFile, - } satisfies ComposerImageAttachment) - : null; - if (image) { - addImage(threadRef, image); + if (isPopoutWindow && tabId) { + sendPreviewAnnotationToChatWindow({ + threadRef, + tabId, + runtimeTabId, + picked, + screenshotFailed, + }); + return; } + const { annotation, image } = await addPreviewAnnotationToDraft( + threadRef, + picked, + screenshotFailed, + ); if ( submission === "send" && readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope) @@ -1023,11 +1019,23 @@ export function PreviewView({ captureDisabled={!desktopOverlay || isUnreachable} recording={recordingRuntimeTabId !== null} onPictureInPicture={ - tabId && (isServerTab || previewBridge) ? handlePictureInPicture : undefined + isPopoutWindow + ? () => { + if (previewBridge && runtimeTabId) + void previewBridge.tabWindow.close(runtimeTabId).catch(() => undefined); + } + : tabId && (isServerTab || previewBridge) + ? handlePictureInPicture + : undefined } - pictureInPicture={miniPlayerTabId === tabId} + pictureInPictureLabel={isPopoutWindow ? gt("Bring back to chat") : undefined} + windowTitleBar={isPopoutWindow && isMacPlatform(navigator.platform)} + pictureInPicture={!isPopoutWindow && miniPlayerTabId === tabId} pictureInPictureDisabled={ - isUnreachable || (!isServerTab && !desktopOverlay?.hasWebContents) + !isPopoutWindow && + (isUnreachable || + Boolean(desktopOverlay?.detached) || + (!isServerTab && !desktopOverlay?.hasWebContents)) } onPickElement={previewBridge && tabId && !isServerTab ? handlePickElement : undefined} pickActive={pickActive} @@ -1072,7 +1080,7 @@ export function PreviewView({ } deviceToolbarVisible={viewport._tag !== "fill"} onToggleDeviceToolbar={handleToggleDeviceToolbar} - nativePictureInPicture={desktopOverlay?.pictureInPicture ?? false} + tabWindow={desktopOverlay?.detached ?? false} /> ) : null } @@ -1134,6 +1142,7 @@ export function PreviewView({ key={runtimeTabId} tabId={runtimeTabId} visible={visible && !isUnreachable} + zIndex={surfaceZIndex} className="absolute inset-0 h-full w-full" /> ) : ( diff --git a/apps/web/src/components/preview/ThreadPreviewMiniPlayer.tsx b/apps/web/src/components/preview/ThreadPreviewMiniPlayer.tsx index 378bcdd88408f..df989479769fa 100644 --- a/apps/web/src/components/preview/ThreadPreviewMiniPlayer.tsx +++ b/apps/web/src/components/preview/ThreadPreviewMiniPlayer.tsx @@ -43,6 +43,7 @@ import { toastManager } from "~/components/ui/toast"; import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip"; import { cn } from "~/lib/utils"; import { useThreadPreviewState } from "~/previewStateStore"; +import { usePreviewWebviewConfig } from "~/browser/previewWebviewConfigState"; import { type PreviewMiniPlayerSize, type PreviewMiniPlayerSource, @@ -186,7 +187,9 @@ export function useThreadPreviewCanvasSource( : null, ); if (!miniPlayer || !source || sourceKey === null) return null; - if (source.kind === "browser" && !browser.snapshot) return null; + if (source.kind === "browser" && (!browser.snapshot || browser.desktopOverlay?.detached)) { + return null; + } return { key: sourceKey, width: miniPlayer.width, @@ -230,6 +233,10 @@ function BrowserMiniPlayer({ threadRef, tabId, miniPlayer }: Props & { readonly recordingTabIds.has(runtimeTabId) || findActiveBrowserRecordingRuntimeTabId(threadRef, tabId) !== null; const serverSurfaceRef = useRef(null); + const webviewPartition = usePreviewWebviewConfig( + threadRef.environmentId, + snapshot?.profileId, + )?.partition; const serverPictureInPicture = useServerPictureInPictureKey() === serverPictureInPictureKey(threadRef.threadId, tabId); const viewportKey = streamedSourceKey( @@ -252,11 +259,8 @@ function BrowserMiniPlayer({ threadRef, tabId, miniPlayer }: Props & { readonly tabId, seed: serverSurfaceRef.current?.canvas() ?? null, }); - } else if (previewBridge) { - const operation = desktopOverlay?.pictureInPicture - ? previewBridge.pictureInPicture.close - : previewBridge.pictureInPicture.open; - await operation(runtimeTabId); + } else if (previewBridge && webviewPartition) { + await previewBridge.tabWindow.open(runtimeTabId, webviewPartition); } } catch (error) { toastManager.add({ @@ -275,7 +279,7 @@ function BrowserMiniPlayer({ threadRef, tabId, miniPlayer }: Props & { readonly ? previewPdfAssetUrl(snapshot.navStatus.url, httpBaseUrl) : null; const loadFailed = snapshot.navStatus._tag === "LoadFailed" ? snapshot.navStatus : null; - const poppedOut = serverTab ? serverPictureInPicture : Boolean(desktopOverlay?.pictureInPicture); + const poppedOut = serverTab && serverPictureInPicture; const canPopOut = serverTab ? poppedOut || (pdfUrl === null && supportsServerPictureInPicture()) : true; diff --git a/apps/web/src/components/preview/previewAnnotationDelivery.ts b/apps/web/src/components/preview/previewAnnotationDelivery.ts new file mode 100644 index 0000000000000..c7e6eaa3cc883 --- /dev/null +++ b/apps/web/src/components/preview/previewAnnotationDelivery.ts @@ -0,0 +1,96 @@ +import { + AuthOrchestrationOperateScope, + PreviewAnnotationPayloadSchema, + ScopedThreadRef, + type PreviewAnnotationPayload, +} from "@t3tools/contracts"; +import * as Schema from "effect/Schema"; +import { msg } from "gt-react"; +import { useEffect } from "react"; + +import { stackedThreadToast, toastManager } from "~/components/ui/toast"; +import { type ComposerImageAttachment, useComposerDraftStore } from "~/composerDraftStore"; +import { capturePreviewAnnotationScreenshot } from "~/lib/previewAnnotation"; +import { previewRuntimeTabId } from "~/browser/previewRuntimeTabId"; +import { readThreadPreviewState } from "~/previewStateStore"; +import { readEnvironmentScope } from "~/state/session"; +import { m } from "~/i18n/messages"; +import { readComposerImageBytes } from "../chat/composerAttachmentFiles"; + +const PoppedOutAnnotation = Schema.Struct({ + threadRef: ScopedThreadRef, + tabId: Schema.String, + runtimeTabId: Schema.String, + picked: PreviewAnnotationPayloadSchema, + screenshotFailed: Schema.Boolean, +}); +type PoppedOutAnnotation = typeof PoppedOutAnnotation.Type; +const isPoppedOutAnnotation = Schema.is(PoppedOutAnnotation); + +// The pop-out window has its own composer store, so its picks are handed to the chat window. +const CHANNEL_NAME = "t3code:preview-annotations"; + +export async function addPreviewAnnotationToDraft( + threadRef: ScopedThreadRef, + picked: PreviewAnnotationPayload, + screenshotFailed: boolean, +) { + // Keep the annotation even if the crop fails; drop the screenshot so prompts cannot claim an attachment. + const capture = capturePreviewAnnotationScreenshot(picked); + const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked; + if (capture.status === "captured") await readComposerImageBytes(capture.file); + const store = useComposerDraftStore.getState(); + store.addPreviewAnnotation(threadRef, annotation); + if (screenshotFailed || capture.status === "failed") { + toastManager.add( + stackedThreadToast({ + type: "error", + title: m(msg("Could not capture the picked element")), + description: m(msg("The annotation was kept without the screenshot.")), + }), + ); + } + const file = capture.status === "captured" ? capture.file : null; + const image = + file && annotation.screenshot + ? ({ + type: "image", + id: annotation.id, + name: file.name, + mimeType: file.type, + sizeBytes: file.size, + previewUrl: annotation.screenshot.dataUrl, + file, + } satisfies ComposerImageAttachment) + : null; + if (image) store.addImage(threadRef, image); + return { annotation, image }; +} + +export function sendPreviewAnnotationToChatWindow(message: PoppedOutAnnotation) { + const channel = new BroadcastChannel(CHANNEL_NAME); + // oxlint-disable-next-line unicorn/require-post-message-target-origin -- BroadcastChannel has no target origin + channel.postMessage(message); + channel.close(); +} + +export function PoppedOutAnnotationReceiver() { + // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to picks posted by pop-out preview windows + useEffect(() => { + const channel = new BroadcastChannel(CHANNEL_NAME); + channel.onmessage = (event: MessageEvent) => { + if (!isPoppedOutAnnotation(event.data)) return; + const { threadRef, tabId, runtimeTabId, picked, screenshotFailed } = event.data; + if (!readEnvironmentScope(threadRef.environmentId, AuthOrchestrationOperateScope)) return; + const state = readThreadPreviewState(threadRef); + if ( + !state.desktopByTabId[tabId]?.detached || + runtimeTabId !== previewRuntimeTabId(threadRef, state.serverEpoch, tabId) + ) + return; + void addPreviewAnnotationToDraft(threadRef, picked, screenshotFailed); + }; + return () => channel.close(); + }, []); + return null; +} diff --git a/apps/web/src/components/preview/usePreviewBridge.test.ts b/apps/web/src/components/preview/usePreviewBridge.test.ts index 29858518cfb11..e1f8fa164896b 100644 --- a/apps/web/src/components/preview/usePreviewBridge.test.ts +++ b/apps/web/src/components/preview/usePreviewBridge.test.ts @@ -20,6 +20,7 @@ function state(navStatus: DesktopPreviewTabState["navStatus"]): DesktopPreviewTa canGoForward: false, zoomFactor: 1, pictureInPicture: false, + detached: false, colorScheme: "system", audioMuted: false, audible: false, diff --git a/apps/web/src/components/preview/usePreviewBridge.ts b/apps/web/src/components/preview/usePreviewBridge.ts index 294e93147a9c8..2be598a8fb38c 100644 --- a/apps/web/src/components/preview/usePreviewBridge.ts +++ b/apps/web/src/components/preview/usePreviewBridge.ts @@ -151,6 +151,7 @@ export function projectDesktopState(state: DesktopPreviewTabState): DesktopPrevi loading: state.navStatus.kind === "Loading", zoomFactor: state.zoomFactor, pictureInPicture: state.pictureInPicture, + detached: state.detached, colorScheme: state.colorScheme, audioMuted: state.audioMuted, audible: state.audible, diff --git a/apps/web/src/previewStateStore.test.ts b/apps/web/src/previewStateStore.test.ts index 6554dde5f1c25..02f184ffc1222 100644 --- a/apps/web/src/previewStateStore.test.ts +++ b/apps/web/src/previewStateStore.test.ts @@ -178,6 +178,7 @@ describe("previewStateStore (single-tab)", () => { loading: false, zoomFactor: 1, pictureInPicture: false, + detached: false, colorScheme: "system" as const, audioMuted: false, audible: false, @@ -226,6 +227,7 @@ describe("previewStateStore (single-tab)", () => { loading: false, zoomFactor: 1, pictureInPicture: false, + detached: false, colorScheme: "system", audioMuted: false, audible: false, @@ -256,6 +258,7 @@ describe("previewStateStore (single-tab)", () => { loading: false, zoomFactor: 1, pictureInPicture: false, + detached: false, colorScheme: "system", audioMuted: false, audible: false, @@ -388,6 +391,7 @@ describe("previewStateStore (single-tab)", () => { loading: false, zoomFactor: 1, pictureInPicture: false, + detached: false, colorScheme: "system", audioMuted: false, audible: false, diff --git a/apps/web/src/previewStateStore.ts b/apps/web/src/previewStateStore.ts index 6e190e8069ee5..b7aaae215706a 100644 --- a/apps/web/src/previewStateStore.ts +++ b/apps/web/src/previewStateStore.ts @@ -21,6 +21,7 @@ export interface DesktopPreviewOverlay { loading: boolean; zoomFactor: number; pictureInPicture: boolean; + detached: boolean; colorScheme: DesktopPreviewColorScheme; audioMuted: boolean; audible: boolean; @@ -406,6 +407,7 @@ function isPreviewStateEqual( previous.loading === next.loading && previous.zoomFactor === next.zoomFactor && previous.pictureInPicture === next.pictureInPicture && + previous.detached === next.detached && previous.colorScheme === next.colorScheme && previous.audioMuted === next.audioMuted && previous.audible === next.audible && diff --git a/apps/web/src/routeTree.gen.ts b/apps/web/src/routeTree.gen.ts index 0392f92459406..8648893064685 100644 --- a/apps/web/src/routeTree.gen.ts +++ b/apps/web/src/routeTree.gen.ts @@ -18,6 +18,7 @@ import { Route as ApproveAgentRouteImport } from './routes/approve-agent' import { Route as ChatRouteImport } from './routes/_chat' import { Route as ChatIndexRouteImport } from './routes/_chat.index' import { Route as ProjectsProjectKeyRouteImport } from './routes/projects.$projectKey' +import { Route as BrowserWindowRuntimeTabIdRouteImport } from './routes/browser-window.$runtimeTabId' import { Route as ChatUsageRouteImport } from './routes/_chat.usage' import { Route as ChatSettingsRouteImport } from './routes/_chat.settings' import { Route as ChatPullRequestsRouteImport } from './routes/_chat.pull-requests' @@ -98,6 +99,12 @@ const ProjectsProjectKeyRoute = ProjectsProjectKeyRouteImport.update({ path: '/projects/$projectKey', getParentRoute: () => rootRouteImport, } as any) +const BrowserWindowRuntimeTabIdRoute = + BrowserWindowRuntimeTabIdRouteImport.update({ + id: '/browser-window/$runtimeTabId', + path: '/browser-window/$runtimeTabId', + getParentRoute: () => rootRouteImport, + } as any) const ChatUsageRoute = ChatUsageRouteImport.update({ id: '/usage', path: '/usage', @@ -293,6 +300,7 @@ export interface FileRoutesByFullPath { '/pull-requests': typeof ChatPullRequestsRoute '/settings': typeof ChatSettingsRouteWithChildren '/usage': typeof ChatUsageRoute + '/browser-window/$runtimeTabId': typeof BrowserWindowRuntimeTabIdRoute '/projects/$projectKey': typeof ProjectsProjectKeyRoute '/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute '/draft/$draftId': typeof ChatDraftDraftIdRoute @@ -337,6 +345,7 @@ export interface FileRoutesByTo { '/pull-requests': typeof ChatPullRequestsRoute '/settings': typeof ChatSettingsRouteWithChildren '/usage': typeof ChatUsageRoute + '/browser-window/$runtimeTabId': typeof BrowserWindowRuntimeTabIdRoute '/projects/$projectKey': typeof ProjectsProjectKeyRoute '/': typeof ChatIndexRoute '/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute @@ -384,6 +393,7 @@ export interface FileRoutesById { '/_chat/pull-requests': typeof ChatPullRequestsRoute '/_chat/settings': typeof ChatSettingsRouteWithChildren '/_chat/usage': typeof ChatUsageRoute + '/browser-window/$runtimeTabId': typeof BrowserWindowRuntimeTabIdRoute '/projects/$projectKey': typeof ProjectsProjectKeyRoute '/_chat/': typeof ChatIndexRoute '/_chat/$environmentId/$threadId': typeof ChatEnvironmentIdThreadIdRoute @@ -432,6 +442,7 @@ export interface FileRouteTypes { | '/pull-requests' | '/settings' | '/usage' + | '/browser-window/$runtimeTabId' | '/projects/$projectKey' | '/$environmentId/$threadId' | '/draft/$draftId' @@ -476,6 +487,7 @@ export interface FileRouteTypes { | '/pull-requests' | '/settings' | '/usage' + | '/browser-window/$runtimeTabId' | '/projects/$projectKey' | '/' | '/$environmentId/$threadId' @@ -522,6 +534,7 @@ export interface FileRouteTypes { | '/_chat/pull-requests' | '/_chat/settings' | '/_chat/usage' + | '/browser-window/$runtimeTabId' | '/projects/$projectKey' | '/_chat/' | '/_chat/$environmentId/$threadId' @@ -564,6 +577,7 @@ export interface RootRouteChildren { GuestRoute: typeof GuestRoute PairRoute: typeof PairRoute WelcomeRoute: typeof WelcomeRoute + BrowserWindowRuntimeTabIdRoute: typeof BrowserWindowRuntimeTabIdRoute ProjectsProjectKeyRoute: typeof ProjectsProjectKeyRoute ThreadsEnvironmentIdThreadIdRoute: typeof ThreadsEnvironmentIdThreadIdRoute } @@ -633,6 +647,13 @@ declare module '@tanstack/react-router' { preLoaderRoute: typeof ProjectsProjectKeyRouteImport parentRoute: typeof rootRouteImport } + '/browser-window/$runtimeTabId': { + id: '/browser-window/$runtimeTabId' + path: '/browser-window/$runtimeTabId' + fullPath: '/browser-window/$runtimeTabId' + preLoaderRoute: typeof BrowserWindowRuntimeTabIdRouteImport + parentRoute: typeof rootRouteImport + } '/_chat/usage': { id: '/_chat/usage' path: '/usage' @@ -977,6 +998,7 @@ const rootRouteChildren: RootRouteChildren = { GuestRoute: GuestRoute, PairRoute: PairRoute, WelcomeRoute: WelcomeRoute, + BrowserWindowRuntimeTabIdRoute: BrowserWindowRuntimeTabIdRoute, ProjectsProjectKeyRoute: ProjectsProjectKeyRoute, ThreadsEnvironmentIdThreadIdRoute: ThreadsEnvironmentIdThreadIdRoute, } diff --git a/apps/web/src/routes/__root.tsx b/apps/web/src/routes/__root.tsx index 3cbef77306c8e..e4eef54acff9f 100644 --- a/apps/web/src/routes/__root.tsx +++ b/apps/web/src/routes/__root.tsx @@ -13,6 +13,7 @@ import { import { msg, T, useGT, Var } from "gt-react"; import { CheckIcon, CopyIcon } from "lucide-react"; import { useEffect, useEffectEvent, useRef, useState } from "react"; +import { PoppedOutAnnotationReceiver } from "~/components/preview/previewAnnotationDelivery"; import { APP_DISPLAY_NAME, APP_VERSION } from "../branding"; import { AppSidebarLayout } from "../components/AppSidebarLayout"; @@ -182,6 +183,8 @@ function RootRouteBranch() { return ; } + if (pathname.startsWith("/browser-window/")) return ; + // Settings can reopen /welcome mid-session, so it mounts everything the app does except the // first-run gate and the event router, whose first navigation would leave the page. const appHosts = ( @@ -220,6 +223,7 @@ function RootRouteBranch() { return ( <> + {isWelcome ? ( <> {appHosts} diff --git a/apps/web/src/routes/browser-window.$runtimeTabId.tsx b/apps/web/src/routes/browser-window.$runtimeTabId.tsx new file mode 100644 index 0000000000000..0b4bea1afd1c7 --- /dev/null +++ b/apps/web/src/routes/browser-window.$runtimeTabId.tsx @@ -0,0 +1,27 @@ +import { scopeThreadRef } from "@t3tools/client-runtime/environment"; +import { type EnvironmentId, ThreadId } from "@t3tools/contracts"; +import { createFileRoute } from "@tanstack/react-router"; + +import { PreviewPanel } from "~/components/preview/PreviewPanel"; +import { usePreviewSession } from "~/components/preview/usePreviewSession"; + +export const Route = createFileRoute("/browser-window/$runtimeTabId")({ + component: BrowserWindowRoute, +}); + +function BrowserWindowRoute() { + const { runtimeTabId } = Route.useParams(); + const [environmentId, threadId, , tabId] = JSON.parse(runtimeTabId) as [ + string, + string, + string | null, + string, + ]; + const threadRef = scopeThreadRef(environmentId as EnvironmentId, ThreadId.make(threadId)); + usePreviewSession(threadRef); + return ( +
+ +
+ ); +} diff --git a/packages/contracts/src/ipc.ts b/packages/contracts/src/ipc.ts index 77a523c03608f..4014b83a8bc81 100644 --- a/packages/contracts/src/ipc.ts +++ b/packages/contracts/src/ipc.ts @@ -611,6 +611,8 @@ export interface DesktopPreviewTabState { canGoForward: boolean; zoomFactor: number; pictureInPicture: boolean; + /** Rendered in its own window; the main window shows a placeholder. */ + detached: boolean; colorScheme: DesktopPreviewColorScheme; audioMuted: boolean; /** Chromium reports playback even when audioMuted is true. */ @@ -978,6 +980,11 @@ export const DesktopPreviewRegisterWebviewInputSchema = Schema.Struct({ webContentsId: Schema.Int.check(Schema.isGreaterThan(0)), }); +export const DesktopPreviewOpenTabWindowInputSchema = Schema.Struct({ + tabId: DesktopPreviewTabIdSchema, + partition: Schema.String, +}); + export const DesktopPreviewNavigateInputSchema = Schema.Struct({ tabId: DesktopPreviewTabIdSchema, url: Schema.String, @@ -1377,6 +1384,10 @@ export interface DesktopPreviewBridge { open: (tabId: string) => Promise; close: (tabId: string) => Promise; }; + tabWindow: { + open: (tabId: string, partition: string) => Promise; + close: (tabId: string) => Promise; + }; recording: { onInput: (listener: (event: DesktopPreviewRecordingInputEvent) => void) => () => void; startScreencast: (tabId: string) => Promise;