Skip to content
40 changes: 40 additions & 0 deletions apps/desktop/src/preview/DesktopBrowserHost.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,20 @@ const watchPainting = (host: DesktopBrowserHost.DesktopBrowserHost["Service"]) =
return painting;
});

/** The viewer sizes the host hands the renderer, each time one changes. */
const watchViewerSizes = (host: DesktopBrowserHost.DesktopBrowserHost["Service"]) =>
Effect.gen(function* () {
const sizes: Array<string | null> = [];
yield* host.viewerSizes.pipe(
Stream.runForEach(({ size }) =>
Effect.sync(() => sizes.push(size && `${size.width}x${size.height}`)),
),
Effect.forkScoped,
);
yield* Effect.yieldNow;
return sizes;
});

describe("DesktopBrowserHost", () => {
it.effect("announces tabs already attached to a backend that starts later", () =>
Effect.gen(function* () {
Expand Down Expand Up @@ -288,6 +302,32 @@ describe("DesktopBrowserHost", () => {
}),
);

it.effect(
"hands the renderer the viewer size until the server lets go, exits, or the guest goes",
() =>
Effect.gen(function* () {
const host = yield* DesktopBrowserHost.make;
const sizes = yield* watchViewerSizes(host);
const backend = yield* host.events.pipe(Stream.runDrain, Effect.forkScoped);
yield* Effect.yieldNow;
host.attach(key, makeDebuggee().tab);
const viewerSize = (size: { width: number; height: number } | null) =>
host.handleCommandLine(encodeJson({ type: "viewerSize", ...key, size }));

yield* viewerSize({ width: 1200, height: 900 });
yield* viewerSize({ width: 1200, height: 900 });
yield* viewerSize({ width: 1000, height: 700 });
yield* host.handleCommandLine(encodeJson({ type: "release", ...key }));
yield* viewerSize({ width: 800, height: 600 });
yield* Fiber.interrupt(backend);
yield* viewerSize({ width: 700, height: 500 });
host.detach(key);
yield* Effect.yieldNow;

expect(sizes).toEqual(["1200x900", "1000x700", null, "800x600", null, "700x500", null]);
}),
);

it.effect("drops replies from a relay the server released", () =>
Effect.gen(function* () {
const host = yield* makeDrawnHost;
Expand Down
39 changes: 38 additions & 1 deletion apps/desktop/src/preview/DesktopBrowserHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ interface AttachedTab {
agentLedAt: number;
focusEmulated: boolean;
focusChange: Promise<void> | null;
/** The server's streamed viewer size, which the renderer gives the webview while it hides it. */
viewerSize: { readonly width: number; readonly height: number } | null;
focusUses: number;
/** Each pending command, by the extra session waiting on it; the main connection's have none. */
readonly focusPending: Map<object, string | undefined>;
Expand Down Expand Up @@ -120,6 +122,10 @@ export class DesktopBrowserHost extends Context.Service<
readonly key: DesktopBrowserTabKey;
readonly active: boolean;
}>;
readonly viewerSizes: Stream.Stream<{
readonly key: DesktopBrowserTabKey;
readonly size: { readonly width: number; readonly height: number } | null;
}>;
}
>()("@t3tools/desktop/preview/DesktopBrowserHost") {}

Expand All @@ -135,6 +141,10 @@ export const make = Effect.gen(function* () {
readonly key: DesktopBrowserTabKey;
readonly active: boolean;
}>();
const viewerSizes = yield* PubSub.unbounded<{
readonly key: DesktopBrowserTabKey;
readonly size: { readonly width: number; readonly height: number } | null;
}>();
const runFork = Effect.runForkWith(yield* Effect.context<never>());
const tabs = new Map<string, AttachedTab>();
/** Threads whose agent turn runs, by the server's account. */
Expand Down Expand Up @@ -216,6 +226,17 @@ export const make = Effect.gen(function* () {
tab.focusChange = change;
};

// Emulating the size through the debugger lays the page out but leaves the guest's surface, and
// so every screencast frame, at the webview's own box. The renderer has to resize that box.
const setViewerSize = (
tab: AttachedTab,
size: { readonly width: number; readonly height: number } | null,
) => {
if (size?.width === tab.viewerSize?.width && size?.height === tab.viewerSize?.height) return;
tab.viewerSize = size;
runFork(PubSub.publish(viewerSizes, { key: tab.key, size }));
};

const waitingHolds = (tab: AttachedTab) =>
tab.waitingScreenshots.some((waiting) => waiting.held && waiting.live());

Expand Down Expand Up @@ -318,6 +339,7 @@ export const make = Effect.gen(function* () {
tab.relay = null;
tab.screencasts.clear();
tab.agentUsed = false;
setViewerSize(tab, null);
dropFocusHolds(tab);
};

Expand Down Expand Up @@ -515,6 +537,7 @@ export const make = Effect.gen(function* () {
tab.debuggee.debugger.off("message", tab.onMessage);
tab.debuggee.webContents.off("did-navigate", tab.onNewDocument);
syncPainting(tab);
setViewerSize(tab, null);
if (announced) emit({ type: "detached", ...key });
};

Expand All @@ -534,6 +557,7 @@ export const make = Effect.gen(function* () {
agentLedAt: Number.NEGATIVE_INFINITY,
focusEmulated: false,
focusChange: null,
viewerSize: null,
focusUses: 0,
focusPending: new Map(),
capturing: new Set(),
Expand Down Expand Up @@ -651,6 +675,10 @@ export const make = Effect.gen(function* () {
dropRelay(tab);
return;
}
if (command.value.type === "viewerSize") {
setViewerSize(tab, command.value.size);
return;
}
if (AGENT_INPUT_COMMAND.test(command.value.message)) tab.agentInputAt = performance.now();
relayFor(tab).receive(command.value.message);
});
Expand Down Expand Up @@ -690,14 +718,23 @@ export const make = Effect.gen(function* () {
return DesktopBrowserHost.of({
pointers: Stream.fromPubSub(pointers),
painting: Stream.fromPubSub(painting),
viewerSizes: Stream.fromPubSub(viewerSizes),
// Subscribes before announcing, so no attach falls between the two.
events: Stream.unwrap(
Effect.gen(function* () {
const subscription = yield* PubSub.subscribe(outbox);
yield* announceAll;
return Stream.fromSubscription(subscription);
}),
).pipe(Stream.map((event) => lineEncoder.encode(`${encodeEvent(event)}\n`))),
).pipe(
Stream.map((event) => lineEncoder.encode(`${encodeEvent(event)}\n`)),
// The backend that asked for these sizes has gone.
Stream.ensuring(
Effect.sync(() => {
for (const tab of tabs.values()) setViewerSize(tab, null);
}),
),
),
handleCommandLine,
attach,
detach,
Expand Down
18 changes: 16 additions & 2 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@ describe("PreviewManager", () => {
);

effectIt.effect(
"asks the client to paint a navigated server tab while the agent captures it",
"asks the client to paint a navigated server tab while the agent captures it, and to size it for a viewer",
() =>
Effect.gen(function* () {
const host = yield* DesktopBrowserHost.DesktopBrowserHost;
Expand All @@ -2432,9 +2432,16 @@ describe("PreviewManager", () => {
fromId.mockReturnValue(contents);
const manager = yield* PreviewManager.PreviewManager;
const capturing = yield* Queue.unbounded<boolean>();
const sizes = yield* Queue.unbounded<PreviewManager.PreviewTabState["viewerSize"]>();
let size: PreviewManager.PreviewTabState["viewerSize"] = null;
yield* manager.subscribeStateChanges((_tabId, state) =>
Queue.offer(capturing, state.capturing).pipe(Effect.asVoid),
Effect.gen(function* () {
yield* Queue.offer(capturing, state.capturing);
if (state.viewerSize !== size) yield* Queue.offer(sizes, (size = state.viewerSize));
}),
);
const viewerSize = (next: PreviewManager.PreviewTabState["viewerSize"]) =>
host.handleCommandLine(JSON.stringify({ type: "viewerSize", ...serverTab, size: next }));
const attached = yield* Deferred.make<void>();
yield* host.events.pipe(
Stream.runForEach((line) =>
Expand All @@ -2447,6 +2454,8 @@ describe("PreviewManager", () => {
yield* manager.createTab("tab_1", { serverTab });
yield* manager.registerWebview("tab_1", 42);
yield* Deferred.await(attached);
yield* viewerSize({ width: 1200, height: 900 });
expect(yield* Queue.take(sizes)).toEqual({ width: 1200, height: 900 });
// Navigating rebuilds the tab's state, which must keep the server tab it answers for.
yield* manager.navigate("tab_1", "https://example.com/next");
yield* Queue.clear(capturing);
Expand All @@ -2466,6 +2475,11 @@ describe("PreviewManager", () => {
yield* manager.setCaptureDrawn("tab_1", true);
screenshot.resolve({ data: "" });
expect(yield* Queue.take(capturing)).toBe(false);
// Navigating kept the size, so the next change is the next size.
yield* viewerSize({ width: 1000, height: 700 });
expect(yield* Queue.take(sizes)).toEqual({ width: 1000, height: 700 });
yield* host.handleCommandLine(JSON.stringify({ type: "release", ...serverTab }));
expect(yield* Queue.take(sizes)).toBeNull();
}).pipe(Effect.provide(managerLayer())),
);

Expand Down
18 changes: 18 additions & 0 deletions apps/desktop/src/preview/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export interface PreviewTabState {
audible: boolean;
controller: "human" | "agent" | "none";
capturing: boolean;
viewerSize: { readonly width: number; readonly height: number } | null;
favicon?: DesktopPreviewFavicon;
serverTab?: { readonly threadId: string; readonly tabId: string };
updatedAt: string;
Expand Down Expand Up @@ -1776,6 +1777,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
audible: false,
controller: "none",
capturing: false,
viewerSize: null,
updatedAt,
};
return [
Expand Down Expand Up @@ -1854,6 +1856,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
audible: false,
controller: "none",
capturing: false,
viewerSize: null,
updatedAt,
};
yield* emit(tabId, closed);
Expand Down Expand Up @@ -2117,6 +2120,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
audible: current?.audible ?? false,
controller: current?.controller ?? "none",
capturing: current?.capturing ?? false,
viewerSize: current?.viewerSize ?? null,
...(current?.favicon ? { favicon: current.favicon } : {}),
...(current?.serverTab ? { serverTab: current.serverTab } : {}),
updatedAt,
Expand Down Expand Up @@ -3633,6 +3637,19 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
}
},
);
const updateServerTabViewerSize = Effect.fn("PreviewManager.updateServerTabViewerSize")(
function* (viewer: {
readonly key: { readonly threadId: string; readonly tabId: string };
readonly size: { readonly width: number; readonly height: number } | null;
}) {
const tab = [...(yield* SynchronizedRef.get(tabsRef)).values()].find(
(candidate) =>
candidate.serverTab?.threadId === viewer.key.threadId &&
candidate.serverTab.tabId === viewer.key.tabId,
);
if (tab) yield* update(tab.tabId, { viewerSize: viewer.size });
},
);
let browserHostStarted = false;
// Subscribes before the attach that follows, so no event falls between the two.
const listenToBrowserHost = Effect.suspend(() => {
Expand All @@ -3643,6 +3660,7 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
[
browserHost.pointers.pipe(Stream.runForEach(emitAgentPointer), listen),
browserHost.painting.pipe(Stream.runForEach(updateServerTabCapturing), listen),
browserHost.viewerSizes.pipe(Stream.runForEach(updateServerTabViewerSize), listen),
],
{ discard: true },
);
Expand Down
10 changes: 10 additions & 0 deletions apps/server/src/preview/DesktopBrowserChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ export class DesktopBrowserChannel extends Context.Service<
key: DesktopTabKey,
pointer: { readonly phase: "move" | "click"; readonly x: number; readonly y: number },
) => Effect.Effect<void>;
readonly viewerSize: (
key: DesktopTabKey,
size: { readonly width: number; readonly height: number } | null,
) => Effect.Effect<void>;
/** Never fails. A failed write means a broken channel; the next backend starts with no turns. */
readonly turn: (threadId: string, active: boolean) => Effect.Effect<void>;
}
Expand Down Expand Up @@ -94,6 +98,9 @@ const make = Effect.gen(function* () {
pointer: Effect.fn("DesktopBrowserChannel.pointer")(function () {
return Effect.void;
}),
viewerSize: Effect.fn("DesktopBrowserChannel.viewerSize")(function () {
return Effect.void;
}),
isShown: () => false,
awaitShown: Effect.fn("DesktopBrowserChannel.awaitShown")(function () {
return Effect.succeed(false);
Expand Down Expand Up @@ -284,6 +291,9 @@ const make = Effect.gen(function* () {
pointer: Effect.fn("DesktopBrowserChannel.pointer")(function (key, pointer) {
return command({ type: "pointer", ...key, ...pointer });
}),
viewerSize: Effect.fn("DesktopBrowserChannel.viewerSize")(function (key, size) {
return command({ type: "viewerSize", ...key, size });
}),
});
});

Expand Down
Loading
Loading