Skip to content

Commit 90ee696

Browse files
donjor-agent[bot]rsi-pr-agent
authored andcommitted
feat(desktop): publish each window's project scope for external tools (#1621)
* feat(desktop): publish each window's project scope for external tools Fork-Domain: multi-window Fork-Tier: qol * fix(desktop): keep the window manifest version module-private Fork-Domain: multi-window Fork-Tier: qol * fix(desktop): publish each window's own filter and harden the manifest file Read the window's stored filter, not the sidebar chooser, so legacy-sidebar and sidebar-less routes publish too; hold until projects load. Serialize removal with writes, warn once on failure, narrow the directory mode, and test the IPC sender check. Fork-Domain: multi-window Fork-Tier: qol * fix(desktop): publish known windows past an unreachable environment Squashes: - 462e407 feat(desktop): publish each window's project scope for external tools (#1621) (#1621) - 34cfc10 fixup! feat(desktop): publish each window's project scope for external tools (#1621) (#1621) - a5edcca feat(desktop): publish each window's project scope for external tools (#1621) (#1621) - 16a2a09 fixup! feat(desktop): publish each window's project scope for external tools (#1621) (#1621) Fork-Domain: multi-window Fork-Tier: qol
1 parent 26889fd commit 90ee696

17 files changed

Lines changed: 891 additions & 162 deletions

‎apps/desktop/src/ipc/DesktopIpcHandlers.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
showContextMenu,
6161
} from "./methods/window.ts";
6262
import { requestWindow } from "./methods/window.fork.ts"; // fork-hook: multi-window/dispatch-request-ipc
63+
import { publishWindowProjects } from "./methods/window.fork.ts"; // fork-hook: multi-window/window-projects-ipc
6364
import {
6465
acknowledgeSnapShot,
6566
checkSnapShotShortcut,
@@ -166,6 +167,7 @@ export const installDesktopIpcHandlers = Effect.fn("desktop.ipc.installHandlers"
166167
yield* ipc.handle(pasteAsText);
167168
yield* ipc.handle(revealWindow);
168169
yield* ipc.handle(requestWindow); // fork-hook: multi-window/dispatch-request-ipc
170+
yield* ipc.handle(publishWindowProjects); // fork-hook: multi-window/window-projects-ipc
169171
yield* ipc.handle(probeRemoteEditors);
170172
yield* ipc.handle(getUpdateState);
171173
yield* ipc.handle(setUpdateChannel);

‎apps/desktop/src/ipc/channels.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const CONTEXT_MENU_CHANNEL = "desktop:context-menu";
77
export const OPEN_EXTERNAL_CHANNEL = "desktop:open-external";
88
export const OPEN_SYSTEM_SETTINGS_CHANNEL = "desktop:open-system-settings";
99
export const REQUEST_WINDOW_CHANNEL = "desktop:request-window"; // fork-hook: multi-window/dispatch-request-ipc
10+
export const PUBLISH_WINDOW_PROJECTS_CHANNEL = "desktop:publish-window-projects"; // fork-hook: multi-window/window-projects-ipc
1011
export const PROBE_REMOTE_EDITORS_CHANNEL = "desktop:probe-remote-editors";
1112
export const MENU_ACTION_CHANNEL = "desktop:menu-action";
1213
export const PASTE_AS_TEXT_CHANNEL = "desktop:paste-as-text";

‎apps/desktop/src/ipc/methods/window.fork.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { assert, describe, it } from "@effect/vitest";
22
import * as Effect from "effect/Effect";
33
import * as Layer from "effect/Layer";
44
import * as Option from "effect/Option";
5+
import type * as Electron from "electron";
56

67
import * as DesktopBackendManager from "../../backend/DesktopBackendManager.ts";
78
import * as DesktopBackendMode from "../../app/DesktopBackendMode.ts";
89
import * as DesktopBackendPool from "../../backend/DesktopBackendPool.ts";
10+
import * as ElectronWindow from "../../electron/ElectronWindow.ts";
11+
import { HyprlandPlacement } from "../../window/HyprlandPlacement.ts";
912
import { getLocalEnvironmentBootstraps } from "./window.ts";
13+
import { publishWindowProjects } from "./window.fork.ts";
1014

1115
const readyConfig: DesktopBackendManager.DesktopBackendStartConfig = {
1216
executablePath: "node",
@@ -65,3 +69,50 @@ describe("getLocalEnvironmentBootstraps in client-only mode", () => {
6569
),
6670
);
6771
});
72+
73+
describe("publishWindowProjects", () => {
74+
const windowId = "0f8fad5b-d9cb-469f-a165-70867728950e";
75+
const scope = { kind: "all" } as const;
76+
const window = { webContents: { id: 7 } } as unknown as Electron.BrowserWindow;
77+
78+
// Runs one report with the given sender, and returns what reached placement.
79+
const report = (payload: unknown, sender: { readonly id: number } | undefined) => {
80+
const published: Array<{ readonly windowId: string; readonly scope: unknown }> = [];
81+
return publishWindowProjects.handler(payload, sender && { sender }).pipe(
82+
Effect.as(published),
83+
Effect.provide(
84+
Layer.mergeAll(
85+
Layer.mock(ElectronWindow.ElectronWindow)({
86+
getById: (id) => Effect.succeed(id === windowId ? Option.some(window) : Option.none()),
87+
}),
88+
Layer.mock(HyprlandPlacement)({
89+
isAvailable: true,
90+
publishScope: (id, reported) =>
91+
Effect.sync(() => void published.push({ windowId: id, scope: reported })),
92+
}),
93+
),
94+
),
95+
);
96+
};
97+
98+
it.effect("records the scope a window reports for itself", () =>
99+
Effect.gen(function* () {
100+
assert.deepEqual(yield* report({ windowId, scope }, { id: 7 }), [{ windowId, scope }]);
101+
}),
102+
);
103+
104+
it.effect("ignores a report from another window's sender, or none", () =>
105+
Effect.gen(function* () {
106+
assert.deepEqual(yield* report({ windowId, scope }, { id: 8 }), []);
107+
assert.deepEqual(yield* report({ windowId, scope }, undefined), []);
108+
}),
109+
);
110+
111+
it.effect("ignores a report for an unknown or malformed window id", () =>
112+
Effect.gen(function* () {
113+
const unknown = "1f8fad5b-d9cb-469f-a165-70867728950e";
114+
assert.deepEqual(yield* report({ windowId: unknown, scope }, { id: 7 }), []);
115+
assert.deepEqual(yield* report({ windowId: "hub", scope }, { id: 7 }), []);
116+
}),
117+
);
118+
});

‎apps/desktop/src/ipc/methods/window.fork.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { DesktopWindowRequest } from "@t3tools/contracts";
1+
import { DesktopWindowProjects, DesktopWindowRequest } from "@t3tools/contracts";
22
import * as Effect from "effect/Effect";
3+
import * as Option from "effect/Option";
34
import * as Schema from "effect/Schema";
45

6+
import { ElectronWindow } from "../../electron/ElectronWindow.ts";
7+
import { IpcRequester } from "../../electron/WindowTargets.fork.ts";
58
import * as DesktopWindow from "../../window/DesktopWindow.ts";
9+
import { HyprlandPlacement } from "../../window/HyprlandPlacement.ts";
10+
import { isWindowId } from "../../window/WindowId.fork.ts";
611
import * as IpcChannels from "../channels.ts";
712
import * as DesktopIpc from "../DesktopIpc.ts";
813

@@ -16,3 +21,26 @@ export const requestWindow = DesktopIpc.makeIpcMethod({
1621
yield* desktopWindow.requestWindow(request);
1722
}),
1823
});
24+
25+
/**
26+
* A window reporting the projects its filter shows. Only the window itself may
27+
* report for its id: a sender whose webContents is not that window's is ignored.
28+
*/
29+
export const publishWindowProjects = DesktopIpc.makeIpcMethod({
30+
channel: IpcChannels.PUBLISH_WINDOW_PROJECTS_CHANNEL,
31+
payload: DesktopWindowProjects,
32+
result: Schema.Void,
33+
handler: Effect.fn("desktop.ipc.window.publishWindowProjects")(function* ({ windowId, scope }) {
34+
if (!isWindowId(windowId)) return;
35+
const requester = yield* IpcRequester;
36+
const window = yield* (yield* ElectronWindow).getById(windowId);
37+
if (
38+
Option.isNone(requester) ||
39+
Option.isNone(window) ||
40+
window.value.webContents.id !== requester.value
41+
) {
42+
return;
43+
}
44+
yield* (yield* HyprlandPlacement).publishScope(windowId, scope);
45+
}),
46+
});

‎apps/desktop/src/preview/WindowPolicy.preload.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ export type PreviewCapableDesktopBridge = DesktopBridge & {
1010
};
1111

1212
/**
13-
* The bridge members a desktop window needs on top of the upstream bridge:
14-
* which window this is, the project filter it was seeded with, whether it is
15-
* still demanded, and how to open another one. They are optional in the contract, so the upstream
16-
* bridge literal stays exactly as upstream wrote it and this is the only place
17-
* the fork adds to it.
13+
* The fork's desktop-window bridge members. They are optional in the contract,
14+
* so the upstream bridge literal stays as upstream wrote it and this is the
15+
* only place the fork adds to it.
1816
*/
1917
export type ProjectWindowCapabilities = Required<
20-
Pick<DesktopBridge, "requestWindow" | "getWindowDemandState" | "onWindowDemandStateChange">
18+
Pick<
19+
DesktopBridge,
20+
"requestWindow" | "publishWindowProjects" | "getWindowDemandState" | "onWindowDemandStateChange"
21+
>
2122
> &
2223
Pick<DesktopBridge, "windowId" | "windowScopeSeed">;
2324

@@ -42,6 +43,8 @@ ipcRenderer.on(IpcChannels.WINDOW_DEMAND_STATE_CHANNEL, (_event, demanded: unkno
4243

4344
const projectWindowCapabilities = (): ProjectWindowCapabilities => ({
4445
requestWindow: (request) => ipcRenderer.invoke(IpcChannels.REQUEST_WINDOW_CHANNEL, request),
46+
publishWindowProjects: (projects) =>
47+
ipcRenderer.invoke(IpcChannels.PUBLISH_WINDOW_PROJECTS_CHANNEL, projects),
4548
// Main passes every app window its id; a window main did not create has none.
4649
...windowIdCapability(),
4750
// Branded ids are plain strings at runtime; the preload cannot import the

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ function layerTest(input: {
297297
claim: (key, title) => Effect.sync(() => void input.placementClaims?.push({ key, title })), // fork-hook: multi-window/window-id-claim
298298
snapshotAddresses: Effect.succeed(new Set<string>()), // fork-hook: multi-window/claim-baseline
299299
forget: () => Effect.void,
300+
publishScope: () => Effect.void, // fork-hook: multi-window/window-projects-fake
300301
workspaceOf: () => Effect.succeed(Option.none()),
301302
stageWorkspaceRule: (title, workspace) =>
302303
Effect.sync(() => {
@@ -579,6 +580,7 @@ const makeSplashScenario = (createOutcomes: readonly (Electron.BrowserWindow | n
579580
claim: () => Effect.void,
580581
snapshotAddresses: Effect.succeed(new Set<string>()), // fork-hook: multi-window/claim-baseline
581582
forget: () => Effect.void,
583+
publishScope: () => Effect.void, // fork-hook: multi-window/window-projects-fake
582584
workspaceOf: () => Effect.succeed(Option.none()),
583585
stageWorkspaceRule: () => Effect.succeed(false),
584586
clearWorkspaceRule: () => Effect.void,

‎apps/desktop/src/window/DesktopWindowSession.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ function makeLayer(baseDir: string, workspaces: Record<string, HyprlandWorkspace
6969
claim: () => Effect.void,
7070
snapshotAddresses: Effect.succeed(new Set<string>()),
7171
forget: () => Effect.void,
72+
publishScope: () => Effect.void,
7273
workspaceOf: (key) => Effect.succeed(Option.fromNullishOr(workspaces[key])),
7374
stageWorkspaceRule: () => Effect.succeed(false),
7475
clearWorkspaceRule: () => Effect.void,

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

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1+
import * as NodeServices from "@effect/platform-node/NodeServices";
12
import { assert, describe, it } from "@effect/vitest";
3+
import { EnvironmentId, ProjectId } from "@t3tools/contracts";
24
import * as Effect from "effect/Effect";
5+
import * as FileSystem from "effect/FileSystem";
6+
import * as Layer from "effect/Layer";
37
import * as Option from "effect/Option";
48

59
import * as HyprlandPlacement from "./HyprlandPlacement.ts";
610
import type { WindowId } from "./WindowId.fork.ts";
711
import { windowClaimTitle } from "./WindowPlacement.fork.ts";
12+
import {
13+
type WindowProjectManifest,
14+
windowProjectManifestPath,
15+
} from "./WindowProjectManifest.fork.ts";
816

917
const PID = 4242;
1018
const first = "00000000-0000-4000-8000-000000000001" as WindowId;
@@ -60,8 +68,9 @@ describe("HyprlandPlacement", () => {
6068
});
6169
const knownAddresses = new Set<string>();
6270

63-
yield* Effect.all(
64-
mapped.map(({ windowId, title }) => placement.claim(windowId, title, { knownAddresses })),
71+
yield* Effect.forEach(
72+
mapped,
73+
({ windowId, title }) => placement.claim(windowId, title, { knownAddresses }),
6574
{ concurrency: "unbounded" },
6675
);
6776

@@ -148,4 +157,104 @@ describe("HyprlandPlacement", () => {
148157
assert.isTrue(Option.isNone(yield* placement.workspaceOf(first)));
149158
}),
150159
);
160+
161+
it.effect("writes each window's address and published scope, and drops it on forget", () =>
162+
Effect.gen(function* () {
163+
const writes: WindowProjectManifest[] = [];
164+
const placement = yield* HyprlandPlacement.make({
165+
...options,
166+
manifestPath: "/manifest.json",
167+
writeManifest: (path, manifest) =>
168+
Effect.sync(() => {
169+
assert.strictEqual(path, "/manifest.json");
170+
writes.push(manifest);
171+
}),
172+
requestHyprland: listClients(() => [client("0xaaa", "T3 Code")]),
173+
});
174+
const scope = {
175+
kind: "projects",
176+
projects: [
177+
{
178+
environmentId: EnvironmentId.make("env"),
179+
projectId: ProjectId.make("web"),
180+
workspaceRoot: "/src/web",
181+
},
182+
],
183+
} as const;
184+
185+
yield* placement.claim(first, "T3 Code");
186+
yield* placement.publishScope(first, scope);
187+
yield* placement.publishScope(second, { kind: "all" });
188+
yield* placement.forget(first);
189+
190+
assert.deepEqual(
191+
writes.map((manifest) => manifest.windows),
192+
[
193+
[{ windowId: first, address: "0xaaa", scope: null }],
194+
[{ windowId: first, address: "0xaaa", scope }],
195+
[
196+
{ windowId: first, address: "0xaaa", scope },
197+
{ windowId: second, address: null, scope: { kind: "all" } },
198+
],
199+
[{ windowId: second, address: null, scope: { kind: "all" } }],
200+
],
201+
);
202+
assert.isTrue(writes.every((manifest) => manifest.pid === PID && manifest.version === 1));
203+
}),
204+
);
205+
206+
it.effect("writes no manifest off Hyprland or without a path", () =>
207+
Effect.gen(function* () {
208+
const writes: unknown[] = [];
209+
const record = (_path: string, manifest: WindowProjectManifest) =>
210+
Effect.sync(() => void writes.push(manifest));
211+
const offHyprland = yield* HyprlandPlacement.make({
212+
...options,
213+
environment: { instanceSignature: undefined, runtimeDirectory: "/run/user/1000" },
214+
manifestPath: "/manifest.json",
215+
writeManifest: record,
216+
});
217+
const pathless = yield* HyprlandPlacement.make({ ...options, writeManifest: record });
218+
219+
yield* offHyprland.publishScope(first, { kind: "all" });
220+
yield* pathless.publishScope(first, { kind: "all" });
221+
222+
assert.deepEqual(writes, []);
223+
}),
224+
);
225+
226+
it.effect("removes its manifest on close and writes none after", () =>
227+
Effect.gen(function* () {
228+
const fileSystem = yield* FileSystem.FileSystem;
229+
const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "placement-layer-" });
230+
const keys = ["HYPRLAND_INSTANCE_SIGNATURE", "XDG_RUNTIME_DIR"] as const;
231+
yield* Effect.acquireRelease(
232+
Effect.sync(() => {
233+
const saved = keys.map((key) => [key, process.env[key]] as const);
234+
process.env.HYPRLAND_INSTANCE_SIGNATURE = "test";
235+
process.env.XDG_RUNTIME_DIR = root;
236+
return saved;
237+
}),
238+
(saved) =>
239+
Effect.sync(() => {
240+
for (const [key, value] of saved) {
241+
if (value === undefined) delete process.env[key];
242+
else process.env[key] = value;
243+
}
244+
}),
245+
);
246+
const path = windowProjectManifestPath(root, process.pid)!;
247+
248+
const placement = yield* Effect.gen(function* () {
249+
const open = yield* HyprlandPlacement.HyprlandPlacement;
250+
yield* open.publishScope(first, { kind: "all" });
251+
assert.isTrue(yield* fileSystem.exists(path));
252+
return open;
253+
}).pipe(Effect.provide(HyprlandPlacement.layer.pipe(Layer.provide(NodeServices.layer))));
254+
255+
assert.isFalse(yield* fileSystem.exists(path));
256+
yield* placement.publishScope(second, { kind: "all" });
257+
assert.isFalse(yield* fileSystem.exists(path));
258+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
259+
);
151260
});

0 commit comments

Comments
 (0)