Skip to content

Commit 8143910

Browse files
committed
fix(desktop): honor Windows close intent and reopen on launch
1 parent 1cde241 commit 8143910

4 files changed

Lines changed: 146 additions & 13 deletions

File tree

‎apps/desktop/src/app/DesktopClerk.test.ts‎

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import * as FileSystem from "effect/FileSystem";
3434
import * as ElectronApp from "../electron/ElectronApp.ts";
3535
import * as ElectronShell from "../electron/ElectronShell.ts";
3636
import * as ElectronWindow from "../electron/ElectronWindow.ts";
37+
import * as DesktopWindow from "../window/DesktopWindow.ts";
3738
import * as DesktopClerk from "./DesktopClerk.ts";
3839
import * as DesktopWebLinks from "./DesktopWebLinks.ts";
3940
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
@@ -229,7 +230,12 @@ describe("DesktopClerk", () => {
229230
assert.equal(quit.mock.calls.length, 0);
230231
assert.deepEqual(registeredEvents, ["open-url", "open-file", "second-instance"]);
231232
}).pipe(
232-
Effect.provide(layerDesktopClerk()),
233+
Effect.provide(
234+
Layer.mergeAll(
235+
Layer.mock(DesktopWindow.DesktopWindow)({ activate: Effect.void }),
236+
layerDesktopClerk(),
237+
),
238+
),
233239
Effect.provideService(ElectronApp.ElectronApp, electronApp),
234240
Effect.provideService(ElectronWindow.ElectronWindow, electronWindow),
235241
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
@@ -258,7 +264,12 @@ describe("DesktopClerk", () => {
258264
assert.equal(quit.mock.calls.length, 1);
259265
assert.deepEqual(registeredEvents, []);
260266
}).pipe(
261-
Effect.provide(layerDesktopClerk()),
267+
Effect.provide(
268+
Layer.mergeAll(
269+
Layer.mock(DesktopWindow.DesktopWindow)({ activate: Effect.void }),
270+
layerDesktopClerk(),
271+
),
272+
),
262273
Effect.provideService(ElectronApp.ElectronApp, electronApp),
263274
Effect.provideService(ElectronWindow.ElectronWindow, electronWindow),
264275
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
@@ -305,7 +316,12 @@ it.effect(
305316
assert.equal(event.preventDefault.mock.calls.length, 1);
306317
}).pipe(
307318
Effect.scoped,
308-
Effect.provide(layerDesktopClerk()),
319+
Effect.provide(
320+
Layer.mergeAll(
321+
Layer.mock(DesktopWindow.DesktopWindow)({ activate: Effect.void }),
322+
layerDesktopClerk(),
323+
),
324+
),
309325
Effect.provideService(ElectronApp.ElectronApp, electronApp),
310326
Effect.provideService(ElectronWindow.ElectronWindow, electronWindow),
311327
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
@@ -384,7 +400,12 @@ it.effect.each(["startup", "open-url"] as const)(
384400
assert.strictEqual(delivery?.flowId, request.flowId);
385401
assert.strictEqual(delivery?.returnUrl, request.returnUrl);
386402
}).pipe(
387-
Effect.provide(layerDesktopClerk(true, [], "darwin", undefined, shell)),
403+
Effect.provide(
404+
Layer.mergeAll(
405+
Layer.mock(DesktopWindow.DesktopWindow)({ activate: Effect.void }),
406+
layerDesktopClerk(true, [], "darwin", undefined, shell),
407+
),
408+
),
388409
Effect.provideService(HostProcess.Arguments, entry === "startup" ? ["t3", link] : ["t3"]),
389410
Effect.provideService(ElectronApp.ElectronApp, electronApp),
390411
Effect.provideService(
@@ -434,7 +455,12 @@ it.effect("hands web and app links to the renderer and leaves other links alone"
434455
"t3code://threads/env-1/thread-1",
435456
]);
436457
}).pipe(
437-
Effect.provide(layerDesktopClerk(true, [], "darwin")),
458+
Effect.provide(
459+
Layer.mergeAll(
460+
Layer.mock(DesktopWindow.DesktopWindow)({ activate: Effect.void }),
461+
layerDesktopClerk(true, [], "darwin"),
462+
),
463+
),
438464
Effect.provideService(HostProcess.Arguments, ["t3"]),
439465
Effect.provideService(ElectronApp.ElectronApp, electronApp),
440466
Effect.provideService(
@@ -451,3 +477,35 @@ it.effect("hands web and app links to the renderer and leaves other links alone"
451477
);
452478
}).pipe(Effect.scoped),
453479
);
480+
481+
it.effect("activates the desktop for an ordinary second launch with no existing window", () => {
482+
storageMock.mockReturnValue(storageAdapter);
483+
createClerkBridgeMock.mockReturnValue({ cleanup: vi.fn(), isPrimaryInstance: true });
484+
const listeners = new Map<string, (...args: unknown[]) => void>();
485+
const activated = Promise.withResolvers<void>();
486+
return Effect.gen(function* () {
487+
const clerk = yield* DesktopClerk.DesktopClerk;
488+
yield* clerk.configure;
489+
listeners.get("second-instance")!({}, ["t3"]);
490+
yield* Effect.promise(() => activated.promise);
491+
}).pipe(
492+
Effect.scoped,
493+
494+
Effect.provideService(ElectronApp.ElectronApp, {
495+
on: (name: string, listener: (...args: unknown[]) => void) =>
496+
Effect.sync(() => {
497+
listeners.set(name, listener);
498+
}),
499+
} as unknown as ElectronApp.ElectronApp["Service"]),
500+
Effect.provide(
501+
Layer.mergeAll(
502+
layerDesktopClerk(),
503+
Layer.mock(ElectronWindow.ElectronWindow)({ currentMainOrFirst: Effect.succeedNone }),
504+
Layer.mock(DesktopWindow.DesktopWindow)({
505+
activate: Effect.sync(() => activated.resolve()),
506+
}),
507+
),
508+
),
509+
Effect.provideService(DesktopWebLinks.DesktopWebLinks, ignoreWebLinks),
510+
);
511+
});

‎apps/desktop/src/app/DesktopClerk.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
2020
import * as ElectronWindow from "../electron/ElectronWindow.ts";
2121
import * as DesktopUserData from "./DesktopUserData.ts";
2222
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
23+
import * as DesktopWindow from "../window/DesktopWindow.ts";
2324
import * as DesktopWebLinks from "./DesktopWebLinks.ts";
2425

2526
declare const __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__: string | undefined;
@@ -59,6 +60,7 @@ export class DesktopClerk extends Context.Service<
5960
| ElectronApp.ElectronApp
6061
| ElectronWindow.ElectronWindow
6162
| DesktopWebLinks.DesktopWebLinks
63+
| DesktopWindow.DesktopWindow
6264
| Scope.Scope
6365
>;
6466
}
@@ -132,7 +134,9 @@ export const make = Effect.gen(function* () {
132134
const electronApp = yield* ElectronApp.ElectronApp;
133135
const electronWindow = yield* ElectronWindow.ElectronWindow;
134136
const webLinks = yield* DesktopWebLinks.DesktopWebLinks;
135-
const context = yield* Effect.context<ElectronWindow.ElectronWindow>();
137+
const context = yield* Effect.context<
138+
ElectronWindow.ElectronWindow | DesktopWindow.DesktopWindow
139+
>();
136140
const runPromise = Effect.runPromiseWith(context);
137141

138142
// The SDK bridge holds Electron's single-instance lock (acquired at
@@ -229,9 +233,11 @@ export const make = Effect.gen(function* () {
229233
return;
230234
void runPromise(
231235
Effect.gen(function* () {
232-
const mainWindow = yield* electronWindow.currentMainOrFirst;
233-
if (Option.isSome(mainWindow)) yield* electronWindow.reveal(mainWindow.value);
234-
}),
236+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
237+
yield* desktopWindow.activate;
238+
}).pipe(
239+
Effect.catchCause((cause) => Effect.logWarning("Could not activate desktop", cause)),
240+
),
235241
);
236242
});
237243
}).pipe(Effect.withSpan("desktop.clerk.configure")),

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

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,10 @@ function layerTest(input: {
352352
// currentMainOrFirst mirrors the real fallback to the first live window (the
353353
// splash, before any main is registered). Reveal targets are recorded so tests
354354
// can assert what activation actually surfaced.
355-
const makeSplashScenario = (createOutcomes: readonly (Electron.BrowserWindow | null)[]) =>
355+
const makeSplashScenario = (
356+
createOutcomes: readonly (Electron.BrowserWindow | null)[],
357+
platform: NodeJS.Platform = "darwin",
358+
) =>
356359
Effect.gen(function* () {
357360
const createdWindows = yield* Ref.make<Electron.BrowserWindow[]>([]);
358361
const createCalls = yield* Ref.make(0);
@@ -368,7 +371,7 @@ const makeSplashScenario = (createOutcomes: readonly (Electron.BrowserWindow | n
368371
return registered;
369372
}
370373
const created = yield* Ref.get(createdWindows);
371-
return Option.fromNullishOr(created[0] ?? null);
374+
return Option.fromNullishOr(created.find((window) => !window.isDestroyed()) ?? null);
372375
});
373376

374377
const electronWindowShape = {
@@ -426,7 +429,17 @@ const makeSplashScenario = (createOutcomes: readonly (Electron.BrowserWindow | n
426429
recordMetrics: () => Effect.void,
427430
shutdown: Effect.void,
428431
}),
429-
layerDesktopEnvironment,
432+
DesktopEnvironment.layer({ ...environmentInput, platform }).pipe(
433+
Layer.provide(
434+
Layer.mergeAll(
435+
NodeServices.layer,
436+
DesktopConfig.layerTest({
437+
T3CODE_PORT: "3773",
438+
VITE_DEV_SERVER_URL: "http://127.0.0.1:5733",
439+
}),
440+
),
441+
),
442+
),
430443
DesktopAppSettings.layerTest(),
431444
layerDesktopClientSettings,
432445
layerDesktopServerExposure,
@@ -1455,6 +1468,49 @@ describe("DesktopWindow", () => {
14551468
}),
14561469
);
14571470

1471+
it.effect.each(["splash", "main"] as const)(
1472+
"keeps a closed Windows %s closed on backend readiness until activation",
1473+
(kind) =>
1474+
Effect.gen(function* () {
1475+
const closed = makeFakeBrowserWindow();
1476+
const reopened = makeFakeBrowserWindow();
1477+
const scenario = yield* makeSplashScenario([closed.window, reopened.window], "win32");
1478+
yield* Effect.gen(function* () {
1479+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
1480+
if (kind === "splash") yield* desktopWindow.showConnectingSplash;
1481+
else yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
1482+
closed.isDestroyed.mockReturnValue(true);
1483+
closed.windowListeners.get("closed")!();
1484+
yield* Effect.yieldNow;
1485+
yield* desktopWindow.handleBackendNotReady;
1486+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
1487+
assert.equal(yield* Ref.get(scenario.createCalls), 1);
1488+
yield* desktopWindow.activate;
1489+
assert.equal(yield* Ref.get(scenario.createCalls), 2);
1490+
assert.equal(Option.getOrThrow(yield* Ref.get(scenario.mainWindow)), reopened.window);
1491+
}).pipe(Effect.provide(scenario.layer));
1492+
}),
1493+
);
1494+
1495+
it.effect("remembers activation while the Windows backend is still starting", () =>
1496+
Effect.gen(function* () {
1497+
const splash = makeFakeBrowserWindow();
1498+
const main = makeFakeBrowserWindow();
1499+
const scenario = yield* makeSplashScenario([splash.window, main.window], "win32");
1500+
yield* Effect.gen(function* () {
1501+
const desktopWindow = yield* DesktopWindow.DesktopWindow;
1502+
yield* desktopWindow.showConnectingSplash;
1503+
splash.isDestroyed.mockReturnValue(true);
1504+
splash.windowListeners.get("closed")!();
1505+
yield* Effect.yieldNow;
1506+
yield* desktopWindow.activate;
1507+
assert.equal(yield* Ref.get(scenario.createCalls), 1);
1508+
yield* desktopWindow.handleBackendReady(new URL("http://127.0.0.1:3773"));
1509+
assert.equal(yield* Ref.get(scenario.createCalls), 2);
1510+
}).pipe(Effect.provide(scenario.layer));
1511+
}),
1512+
);
1513+
14581514
it.effect(
14591515
"retries opening the real main on activate when a failed post-readiness open left only the splash",
14601516
() =>

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,8 @@ export const make = Effect.gen(function* () {
340340
// createMainIfBackendReady, which gates the post-readiness window
341341
// open in development and the macOS "activate without windows" path.
342342
const backendReadyRef = yield* Ref.make(false);
343+
// Windows close-to-tray must survive backend restarts without showing a window.
344+
let windowRequested = true;
343345
// The transient "Connecting to WSL" splash window, tracked separately so it
344346
// is never mistaken for the real main window.
345347
const splashWindowRef = yield* Ref.make<Option.Option<Electron.BrowserWindow>>(Option.none());
@@ -863,6 +865,7 @@ export const make = Effect.gen(function* () {
863865
}
864866

865867
window.on("closed", () => {
868+
if (environment.platform === "win32") windowRequested = false;
866869
clearDevelopmentLoadRetry();
867870
clearBoundsPersist();
868871
void runPromise(electronWindow.clearMain(Option.some(window)));
@@ -900,6 +903,7 @@ export const make = Effect.gen(function* () {
900903
});
901904

902905
const createMainIfBackendReady = Effect.gen(function* () {
906+
if (!windowRequested) return;
903907
if (yield* waitingForBackend) return;
904908
const existingWindow = yield* currentMainWindow;
905909
if (Option.isSome(existingWindow)) return;
@@ -936,7 +940,15 @@ export const make = Effect.gen(function* () {
936940
yield* rendererHistory.register(splash.webContents, { surface: "splash" });
937941
yield* Ref.set(splashWindowRef, Option.some(splash));
938942
splash.once("closed", () => {
939-
void runPromise(Ref.set(splashWindowRef, Option.none()));
943+
void runPromise(
944+
Effect.gen(function* () {
945+
// Programmatic dismissal clears the reference before closing the splash.
946+
if (Option.isSome(yield* Ref.get(splashWindowRef)) && environment.platform === "win32") {
947+
windowRequested = false;
948+
}
949+
yield* Ref.set(splashWindowRef, Option.none());
950+
}),
951+
);
940952
});
941953
splash.once("ready-to-show", () => {
942954
if (!splash.isDestroyed()) {
@@ -988,6 +1000,7 @@ export const make = Effect.gen(function* () {
9881000
}
9891001
}),
9901002
activate: Effect.gen(function* () {
1003+
windowRequested = true;
9911004
const existingWindow = yield* currentMainWindow;
9921005
if (Option.isSome(existingWindow)) {
9931006
yield* electronWindow.reveal(existingWindow.value);

0 commit comments

Comments
 (0)