Skip to content

Commit d2003ea

Browse files
donjor-agent[bot]rsi-pr-agent
authored andcommitted
refactor(multi-window): createWindow seams become marked insertions (#1811)
The part of the createWindow reshape that stays in fork-owned files; its DesktopWindow.ts hooks folded into their owners. Squashes: - 312c64e refactor(multi-window): createWindow seams become marked insertions (#1811) (#1811) - fb7d900 fixup! refactor(multi-window): createWindow seams become marked insertions (#1811) (#1811) - faa53bd refactor(multi-window): createWindow seams become marked insertions (#1811) (#1811) Fork-Domain: multi-window Fork-Tier: core
1 parent ee8c9fe commit d2003ea

4 files changed

Lines changed: 72 additions & 31 deletions

File tree

‎apps/desktop/src/window/DesktopWindow.fork.suite.ts‎

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,20 @@ export const previewOwnedForwardingFork = {
5252
subscribeOwnedOpenLinks: () => Effect.void,
5353
} satisfies Partial<PreviewManager.PreviewManager["Service"]>;
5454

55-
/**
56-
* Preview session overrides that record calls when a fork case passes the
57-
* matching recorder; without one, upstream's mock stands.
58-
*/
55+
/** Records shared-session requests when a fork case passes a recorder; else upstream's mock stands. */
5956
export const previewSessionCountersFork = (input: {
6057
readonly previewBrowserSessionRequests?: number[];
61-
readonly previewMainWindowSets?: Electron.BrowserWindow[];
6258
}): Partial<PreviewManager.PreviewManager["Service"]> => {
63-
const { previewBrowserSessionRequests: sessionRequests, previewMainWindowSets: mainWindowSets } =
64-
input;
65-
return {
66-
...(sessionRequests && {
67-
getBrowserSession: () =>
68-
Effect.sync(() => {
69-
sessionRequests.push(1);
70-
return {} as Electron.Session;
71-
}),
72-
}),
73-
...(mainWindowSets && {
74-
setMainWindow: (window) =>
75-
Effect.sync(() => {
76-
mainWindowSets.push(window);
77-
}),
78-
}),
79-
};
59+
const requests = input.previewBrowserSessionRequests;
60+
return requests
61+
? {
62+
getBrowserSession: () =>
63+
Effect.sync(() => {
64+
requests.push(1);
65+
return {} as Electron.Session;
66+
}),
67+
}
68+
: {};
8069
};
8170

8271
/**
@@ -374,17 +363,22 @@ export const registerDesktopWindowForkTests = (harness: DesktopWindowHarnessFork
374363
assert.deepEqual(previewOwners, [testWindowId(1)]);
375364
assert.deepEqual(previewBrowserSessionRequests, []);
376365
assert.deepEqual(fakeWindow.loadURL.mock.calls[0], ["t3code-dev://app/"]);
377-
assert.isFalse(fakeWindow.windowListeners.has("resize"));
378366

379367
fakeWindow.windowListeners.get("ready-to-show")?.();
380368
yield* Effect.yieldNow;
381369
assert.deepEqual(
382370
placementClaims.map((claim) => claim.key),
383371
[testWindowId(1)],
384372
);
385-
const pageTitleUpdated = fakeWindow.windowListeners.get("page-title-updated");
386373
const preventDefault = vi.fn();
387-
pageTitleUpdated?.({ preventDefault }, "Project One");
374+
// The fake keeps one listener per event; the project title needs every one.
375+
const listeners = vi.mocked(fakeWindow.window.on).mock.calls as unknown as [
376+
string,
377+
(...args: readonly unknown[]) => void,
378+
][];
379+
for (const [event, listener] of listeners) {
380+
if (event === "page-title-updated") listener({ preventDefault }, "Project One");
381+
}
388382
assert.equal(preventDefault.mock.calls.length, 1);
389383
assert.deepEqual(fakeWindow.setTitle.mock.calls, [["Project One"]]);
390384

‎apps/desktop/src/window/DesktopWindow.fork.test.ts‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,9 +257,6 @@ describe("DesktopWindow (fork)", () => {
257257
yield* Effect.gen(function* () {
258258
const desktopWindow = yield* DesktopWindow.DesktopWindow;
259259
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
260-
const resizeListeners = () =>
261-
vi.mocked(window.on).mock.calls.filter(([event]) => String(event) === "resize").length;
262-
assert.equal(resizeListeners(), 1);
263260

264261
yield* desktopWindow.requestWindow({ kind: "new-window" });
265262
yield* desktopWindow.requestWindow({ kind: "new-window" });
@@ -280,8 +277,6 @@ describe("DesktopWindow (fork)", () => {
280277
options[3]?.webPreferences?.additionalArguments ?? [],
281278
windowIdPreloadArgument(testWindowId(4)),
282279
);
283-
// Every all-projects window tracks its bounds (see the current-main test).
284-
assert.equal(resizeListeners(), 3);
285280
}).pipe(Effect.provide(makeLayer(window, options)));
286281
}),
287282
);

‎apps/desktop/src/window/DesktopWindowDispatch.fork.ts‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ScopedProjectRef } from "@t3tools/contracts";
2+
import * as Context from "effect/Context";
23
import * as Effect from "effect/Effect";
34
import * as Option from "effect/Option";
45
import type * as Electron from "electron";
@@ -23,12 +24,38 @@ import type { WindowIdentity } from "./WindowIdentity.ts";
2324

2425
const { logInfo: logWindowInfo } = makeComponentLogger("desktop-window");
2526

27+
interface WindowCreateTarget {
28+
readonly identity: WindowIdentity;
29+
readonly windowId: WindowId;
30+
readonly dispatched?: WindowCreateRequest | undefined;
31+
}
32+
33+
/**
34+
* The window `createWindow` builds. Upstream's `createWindow` takes no
35+
* arguments, so its callers provide the target here instead of widening it.
36+
*/
37+
const WindowCreateTargetFork = Context.Reference<Option.Option<WindowCreateTarget>>(
38+
"t3/desktop/WindowCreateTarget.fork",
39+
{ defaultValue: () => Option.none() },
40+
);
41+
42+
export const requireWindowCreateTargetFork = Effect.gen(function* () {
43+
const target = yield* WindowCreateTargetFork;
44+
if (Option.isNone(target)) return yield* Effect.die("createWindow ran without a create target");
45+
return target.value;
46+
});
47+
48+
export const withWindowCreateTargetFork =
49+
(target: WindowCreateTarget) =>
50+
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
51+
Effect.provideService(effect, WindowCreateTargetFork, Option.some(target));
52+
2653
/**
2754
* Whether a window is the primary one the app opened itself. A dispatched
2855
* window (New Window, Open in New Window) is never primary: it leaves the one
2956
* saved bounds slot to the primary window.
3057
*/
31-
export const isPrimaryWindowFork = (
58+
const isPrimaryWindowFork = (
3259
identity: WindowIdentity,
3360
dispatched: WindowCreateRequest | undefined,
3461
): boolean => identity.kind === "hub" && dispatched === undefined;

‎apps/desktop/src/window/WindowPlacement.fork.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,31 @@ export const reserveClaimTitleFork = (input: {
8585
? null
8686
: windowClaimTitle(input.windowId);
8787

88+
/** The title a window is built with, as an option that overrides upstream's app name. */
89+
export const titleOptionFork = (
90+
title: string,
91+
): Pick<Electron.BrowserWindowConstructorOptions, "title"> => ({ title });
92+
93+
/**
94+
* Rewrites upstream's app-name title from the latest page title (`resolve`
95+
* returns null to keep it). Install before the title hold, which restores the
96+
* `setTitle` it found, and before upstream's `page-title-updated` listener, so
97+
* the page title is current when that listener sets the title.
98+
*/
99+
export const retitleWindowFork = (
100+
window: Electron.BrowserWindow,
101+
appTitle: string,
102+
resolve: (pageTitle: string) => string | null,
103+
): void => {
104+
let pageTitle = "";
105+
window.on("page-title-updated", (_event, title) => {
106+
pageTitle = title;
107+
});
108+
const setTitle = window.setTitle;
109+
window.setTitle = (title: string) =>
110+
setTitle.call(window, title === appTitle ? (resolve(pageTitle) ?? title) : title);
111+
};
112+
88113
export interface TitleHold {
89114
/** Shows the held title again, over anything set since the window was built. */
90115
readonly pin: () => void;

0 commit comments

Comments
 (0)