Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions apps/desktop/src/preview/DesktopBrowserHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DesktopBrowserCommand,
DesktopBrowserEvent,
type DesktopBrowserEvent as DesktopBrowserEventType,
type DesktopPopupBlockReason,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
import * as NodePath from "node:path";
Expand Down Expand Up @@ -32,6 +33,10 @@ export interface DesktopBrowserTabKey {
readonly tabId: string;
}

export type DesktopPopupCommand =
| { readonly type: "adopt"; readonly key: DesktopBrowserTabKey; readonly popupId: string }
| { readonly type: "close"; readonly key: DesktopBrowserTabKey };

export interface DesktopBrowserTabDebugger {
readonly webContents: Electron.WebContents;
readonly debugger: Electron.Debugger;
Expand Down Expand Up @@ -103,6 +108,16 @@ export class DesktopBrowserHost extends Context.Service<
readonly key: DesktopBrowserTabKey;
readonly active: boolean;
}>;
/** Asks the server to adopt a popup window that `opener`'s page opened. */
readonly popupOpened: (opener: DesktopBrowserTabKey, popupId: string, url: string) => void;
readonly popupBlocked: (
opener: DesktopBrowserTabKey,
url: string,
reason: DesktopPopupBlockReason,
) => void;
/** A popup the server asked for is gone, so its tab will never attach. */
readonly popupGone: (key: DesktopBrowserTabKey) => void;
readonly popupCommands: Stream.Stream<DesktopPopupCommand>;
}
>()("@t3tools/desktop/preview/DesktopBrowserHost") {}

Expand All @@ -118,6 +133,7 @@ export const make = Effect.gen(function* () {
readonly key: DesktopBrowserTabKey;
readonly active: boolean;
}>();
const popupCommands = yield* PubSub.unbounded<DesktopPopupCommand>();
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 @@ -496,6 +512,16 @@ export const make = Effect.gen(function* () {
else endAgentTurn(command.value.threadId);
return;
}
// A popup's key attaches only once its window is adopted.
if (command.value.type === "adoptPopup" || command.value.type === "closePopup") {
const key = { threadId: command.value.threadId, tabId: command.value.tabId };
const popupCommand: DesktopPopupCommand =
command.value.type === "adoptPopup"
? { type: "adopt", key, popupId: command.value.popupId }
: { type: "close", key };
runFork(PubSub.publish(popupCommands, popupCommand));
return;
}
const tab = tabs.get(keyOf(command.value));
if (!tab) return;
if (command.value.type === "pointer") {
Expand Down Expand Up @@ -536,9 +562,20 @@ export const make = Effect.gen(function* () {
);
});

// Event URLs travel on every line of the channel; a data: URL can be megabytes.
const eventUrl = (url: string) => url.slice(0, 2048);

return DesktopBrowserHost.of({
pointers: Stream.fromPubSub(pointers),
painting: Stream.fromPubSub(painting),
popupCommands: Stream.fromPubSub(popupCommands),
popupOpened: (opener, popupId, url) =>
emit({ type: "popup", ...opener, popupId, url: eventUrl(url) }),
popupBlocked: (opener, url, reason) =>
emit({ type: "popupBlocked", ...opener, url: eventUrl(url), reason }),
popupGone: (key) => {
if (!tabs.has(keyOf(key))) emit({ type: "detached", ...key });
},
// Subscribes before announcing, so no attach falls between the two.
events: Stream.unwrap(
Effect.gen(function* () {
Expand Down
49 changes: 44 additions & 5 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,54 @@ describe("previewWindowOpenAction", () => {
}
});

it("denies a blank window instead of blanking the opener", () => {
for (const url of ["about:blank", ""]) {
for (const disposition of ["new-window", "foreground-tab"] as const) {
expect(PreviewManager.previewWindowOpenAction(details({ url, disposition }))).toBe("deny");
}
it("opens a blank window as a popup, so a page can point it at sign-in through the handle", () => {
for (const disposition of ["new-window", "foreground-tab", "background-tab"] as const) {
expect(
PreviewManager.previewWindowOpenAction(details({ url: "about:blank", disposition })),
).toBe("popup");
}
});
});

describe("popupWindowBounds", () => {
const display = { x: 1800, y: 25, width: 2560, height: 1415 };

it("centres the size the page asked for over the T3 window, not where the page placed it", () => {
expect(
PreviewManager.popupWindowBounds({
host: { x: 2010, y: 266, width: 1100, height: 780 },
workArea: display,
// Firebase's signInWithPopup features, positioned for the whole screen.
features: "location=yes,resizable=yes,toolbar=no,width=500,height=600,top=405,left=2310,",
}),
).toEqual({ x: 2310, y: 356, width: 500, height: 600 });
expect(
PreviewManager.popupWindowBounds({
host: { x: 2010, y: 266, width: 1100, height: 780 },
workArea: display,
features: "",
}),
).toEqual({ x: 2160, y: 356, width: 800, height: 600 });
});

it("keeps the popup on the T3 window's display", () => {
expect(
PreviewManager.popupWindowBounds({
host: { x: 1800, y: 25, width: 400, height: 300 },
workArea: display,
features: "width=500,height=660",
}),
).toEqual({ x: 1800, y: 25, width: 500, height: 660 });
expect(
PreviewManager.popupWindowBounds({
host: { x: 0, y: 25, width: 1440, height: 875 },
workArea: { x: 0, y: 25, width: 1440, height: 875 },
features: "width=3000,height=2000",
}),
).toEqual({ x: 0, y: 25, width: 1440, height: 875 });
});
});

const {
browserWindowConstructor,
clipboardItemConstructor,
Expand Down
Loading
Loading