Skip to content

Commit 2bebe5a

Browse files
committed
fix(desktop): improve tray reveal reliability and destroy lifecycle
- Invoke DesktopWindow.revealOrCreateMain on tray click and open actions so the window is revealed even during backend startup. - Retain ElectronTray reference when native destroy fails, allowing cleanup to be retried safely. - Reuse unified destroy helper during partial tray creation error handling. - Add and update unit tests for tray callbacks and error recovery.
1 parent 3187bc1 commit 2bebe5a

4 files changed

Lines changed: 163 additions & 117 deletions

File tree

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

Lines changed: 90 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -44,78 +44,99 @@ const makeElectronTrayLayer = (
4444
destroy: Effect.void,
4545
} satisfies ElectronTray.ElectronTray["Service"]);
4646

47-
const electronAppLayer = Layer.succeed(ElectronApp.ElectronApp, {
48-
metadata: Effect.die("unexpected metadata read"),
49-
name: Effect.succeed("T3 Code"),
50-
systemLocale: Effect.succeed("en-US"),
51-
whenReady: Effect.void,
52-
quit: Effect.void,
53-
exit: () => Effect.void,
54-
relaunch: () => Effect.void,
55-
setPath: () => Effect.void,
56-
setName: () => Effect.void,
57-
setAboutPanelOptions: () => Effect.void,
58-
setAppUserModelId: () => Effect.void,
59-
getAppMetrics: Effect.succeed([]),
60-
isDefaultProtocolClient: () => Effect.succeed(false),
61-
setAsDefaultProtocolClient: () => Effect.succeed(true),
62-
setDesktopName: () => Effect.void,
63-
setDockIcon: () => Effect.void,
64-
appendCommandLineSwitch: () => Effect.void,
65-
onBeforeQuitForUpdate: () => Effect.void,
66-
removeCommandLineSwitch: () => Effect.void,
67-
on: () => Effect.void,
68-
} satisfies ElectronApp.ElectronApp["Service"]);
69-
70-
const desktopWindowLayer = Layer.succeed(DesktopWindow.DesktopWindow, {
71-
createMain: Effect.die("unexpected createMain"),
72-
ensureMain: Effect.die("unexpected ensureMain"),
73-
revealOrCreateMain: Effect.die("unexpected revealOrCreateMain"),
74-
activate: Effect.void,
75-
createMainIfBackendReady: Effect.void,
76-
showConnectingSplash: Effect.void,
77-
handleBackendReady: () => Effect.void,
78-
handleBackendNotReady: Effect.void,
79-
flushMainWindowBounds: Effect.void,
80-
prepareCaptureReveal: Effect.void,
81-
dispatchMenuAction: () => Effect.void,
82-
dispatchSnapShotEvent: () => Effect.void,
83-
zoomMain: () => Effect.void,
84-
syncAppearance: Effect.void,
85-
} satisfies DesktopWindow.DesktopWindow["Service"]);
47+
const makeElectronAppLayer = (onQuit?: () => Effect.Effect<void>) =>
48+
Layer.succeed(ElectronApp.ElectronApp, {
49+
metadata: Effect.die("unexpected metadata read"),
50+
name: Effect.succeed("T3 Code"),
51+
systemLocale: Effect.succeed("en-US"),
52+
whenReady: Effect.void,
53+
quit: onQuit ? onQuit() : Effect.void,
54+
exit: () => Effect.void,
55+
relaunch: () => Effect.void,
56+
setPath: () => Effect.void,
57+
setName: () => Effect.void,
58+
setAboutPanelOptions: () => Effect.void,
59+
setAppUserModelId: () => Effect.void,
60+
getAppMetrics: Effect.succeed([]),
61+
isDefaultProtocolClient: () => Effect.succeed(false),
62+
setAsDefaultProtocolClient: () => Effect.succeed(true),
63+
setDesktopName: () => Effect.void,
64+
setDockIcon: () => Effect.void,
65+
appendCommandLineSwitch: () => Effect.void,
66+
onBeforeQuitForUpdate: () => Effect.void,
67+
removeCommandLineSwitch: () => Effect.void,
68+
on: () => Effect.void,
69+
} satisfies ElectronApp.ElectronApp["Service"]);
70+
71+
const makeDesktopWindowLayer = (onRevealOrCreateMain?: () => Effect.Effect<void>) =>
72+
Layer.succeed(DesktopWindow.DesktopWindow, {
73+
createMain: Effect.die("unexpected createMain"),
74+
ensureMain: Effect.die("unexpected ensureMain"),
75+
revealOrCreateMain: onRevealOrCreateMain ? onRevealOrCreateMain() : Effect.succeed({} as any),
76+
activate: Effect.void,
77+
createMainIfBackendReady: Effect.void,
78+
showConnectingSplash: Effect.void,
79+
handleBackendReady: () => Effect.void,
80+
handleBackendNotReady: Effect.void,
81+
flushMainWindowBounds: Effect.void,
82+
prepareCaptureReveal: Effect.void,
83+
dispatchMenuAction: () => Effect.void,
84+
dispatchSnapShotEvent: () => Effect.void,
85+
zoomMain: () => Effect.void,
86+
syncAppearance: Effect.void,
87+
} satisfies DesktopWindow.DesktopWindow["Service"]);
8688

8789
describe("DesktopTray", () => {
88-
it.effect("configures tray icon and menu on win32", () =>
89-
Effect.gen(function* () {
90-
const createdOptions = yield* Deferred.make<ElectronTray.ElectronTrayCreateOptions>();
91-
const testLayer = DesktopTray.layer.pipe(
92-
Layer.provideMerge(makeElectronTrayLayer(createdOptions)),
93-
Layer.provideMerge(desktopAssetsLayer),
94-
Layer.provideMerge(makeEnvironmentLayer("win32")),
95-
Layer.provideMerge(electronAppLayer),
96-
Layer.provideMerge(desktopWindowLayer),
97-
Layer.provideMerge(NodeServices.layer),
98-
);
90+
it.effect(
91+
"configures tray icon and menu on win32 and invokes revealOrCreateMain on open actions",
92+
() =>
93+
Effect.gen(function* () {
94+
const createdOptions = yield* Deferred.make<ElectronTray.ElectronTrayCreateOptions>();
95+
const clickDeferred = yield* Deferred.make<void>();
96+
const quitDeferred = yield* Deferred.make<void>();
9997

100-
yield* Effect.scoped(
101-
Effect.gen(function* () {
102-
const tray = yield* DesktopTray.DesktopTray;
103-
assert.isFalse(yield* tray.isAvailable);
98+
const testLayer = DesktopTray.layer.pipe(
99+
Layer.provideMerge(makeElectronTrayLayer(createdOptions)),
100+
Layer.provideMerge(desktopAssetsLayer),
101+
Layer.provideMerge(makeEnvironmentLayer("win32")),
102+
Layer.provideMerge(
103+
makeElectronAppLayer(() => Deferred.succeed(quitDeferred, void 0).pipe(Effect.asVoid)),
104+
),
105+
Layer.provideMerge(
106+
makeDesktopWindowLayer(() =>
107+
Deferred.succeed(clickDeferred, void 0).pipe(Effect.asVoid),
108+
),
109+
),
110+
Layer.provideMerge(NodeServices.layer),
111+
);
104112

105-
yield* tray.configure;
113+
yield* Effect.scoped(
114+
Effect.gen(function* () {
115+
const tray = yield* DesktopTray.DesktopTray;
116+
assert.isFalse(yield* tray.isAvailable);
106117

107-
const options = yield* Deferred.await(createdOptions);
108-
assert.equal(options.iconPath, "C:/path/to/icon.ico");
109-
assert.equal(options.tooltip, "T3 Code");
110-
assert.isDefined(options.menuItems);
111-
const menuItems = options.menuItems ?? [];
112-
assert.equal(menuItems.length, 3);
113-
assert.equal(menuItems[0]?.label, "Open T3 Code");
114-
assert.equal(menuItems[2]?.label, "Quit T3 Code");
115-
assert.isTrue(yield* tray.isAvailable);
116-
}),
117-
).pipe(Effect.provide(testLayer));
118-
}),
118+
yield* tray.configure;
119+
120+
const options = yield* Deferred.await(createdOptions);
121+
assert.equal(options.iconPath, "C:/path/to/icon.ico");
122+
assert.equal(options.tooltip, "T3 Code");
123+
assert.isDefined(options.menuItems);
124+
const menuItems = options.menuItems ?? [];
125+
assert.equal(menuItems.length, 3);
126+
assert.equal(menuItems[0]?.label, "Open T3 Code");
127+
assert.equal(menuItems[2]?.label, "Quit T3 Code");
128+
assert.isTrue(yield* tray.isAvailable);
129+
130+
// Click on tray invokes revealOrCreateMain
131+
options.onClick?.();
132+
yield* Deferred.await(clickDeferred);
133+
134+
// Click on Quit invokes electronApp.quit
135+
menuItems[2]?.click?.();
136+
yield* Deferred.await(quitDeferred);
137+
}),
138+
).pipe(Effect.provide(testLayer));
139+
}),
119140
);
120141

121142
it.effect("skips configuring tray on darwin", () =>
@@ -125,8 +146,8 @@ describe("DesktopTray", () => {
125146
Layer.provideMerge(makeElectronTrayLayer(createdOptions)),
126147
Layer.provideMerge(desktopAssetsLayer),
127148
Layer.provideMerge(makeEnvironmentLayer("darwin")),
128-
Layer.provideMerge(electronAppLayer),
129-
Layer.provideMerge(desktopWindowLayer),
149+
Layer.provideMerge(makeElectronAppLayer()),
150+
Layer.provideMerge(makeDesktopWindowLayer()),
130151
Layer.provideMerge(NodeServices.layer),
131152
);
132153

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,16 @@ export const make = Effect.gen(function* () {
8585
iconPath,
8686
tooltip: environment.displayName,
8787
onClick: () => {
88-
void runPromise(desktopWindow.activate);
88+
void runPromise(desktopWindow.revealOrCreateMain);
8989
},
9090
onDoubleClick: () => {
91-
void runPromise(desktopWindow.activate);
91+
void runPromise(desktopWindow.revealOrCreateMain);
9292
},
9393
menuItems: [
9494
{
9595
label: `Open ${environment.displayName}`,
9696
click: () => {
97-
void runPromise(desktopWindow.activate);
97+
void runPromise(desktopWindow.revealOrCreateMain);
9898
},
9999
},
100100
{

‎apps/desktop/src/electron/ElectronTray.test.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,4 +174,32 @@ describe("ElectronTray", () => {
174174
assert.isTrue(trayInstances[0]?.destroyed);
175175
}).pipe(Effect.provide(TestLayer)),
176176
);
177+
178+
it.effect("retains tray reference on failed destruction so cleanup can be retried", () =>
179+
Effect.gen(function* () {
180+
const electronTray = yield* ElectronTray.ElectronTray;
181+
182+
yield* electronTray.create({ iconPath: "icon1.ico" });
183+
const instance = trayInstances[0];
184+
assert.isDefined(instance);
185+
186+
let shouldThrow = true;
187+
const originalDestroy = instance.destroy;
188+
instance.destroy = () => {
189+
if (shouldThrow) {
190+
throw new Error("Native destruction failure");
191+
}
192+
originalDestroy.call(instance);
193+
};
194+
195+
const failure = yield* electronTray.destroy.pipe(Effect.exit);
196+
assert.isTrue(failure._tag === "Failure");
197+
assert.isFalse(instance.destroyed);
198+
199+
// Subsequent destroy retry succeeds
200+
shouldThrow = false;
201+
yield* electronTray.destroy;
202+
assert.isTrue(instance.destroyed);
203+
}).pipe(Effect.provide(TestLayer)),
204+
);
177205
});

‎apps/desktop/src/electron/ElectronTray.ts‎

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -97,70 +97,67 @@ export const make = Effect.gen(function* () {
9797
const platform = yield* HostProcessPlatform;
9898
const currentTrayRef = yield* Ref.make<Option.Option<Electron.Tray>>(Option.none());
9999

100+
const destroyTray = (tray: Electron.Tray) =>
101+
Effect.try({
102+
try: () => {
103+
if (!tray.isDestroyed()) {
104+
tray.destroy();
105+
}
106+
},
107+
catch: (cause) =>
108+
new ElectronTrayOperationError({
109+
operation: "destroy-tray",
110+
platform,
111+
cause,
112+
}),
113+
});
114+
100115
const destroy = Effect.gen(function* () {
101-
const currentTray = yield* Ref.getAndSet(currentTrayRef, Option.none());
116+
const currentTray = yield* Ref.get(currentTrayRef);
102117
if (Option.isSome(currentTray)) {
103-
yield* Effect.try({
104-
try: () => {
105-
if (!currentTray.value.isDestroyed()) {
106-
currentTray.value.destroy();
107-
}
108-
},
109-
catch: (cause) =>
110-
new ElectronTrayOperationError({
111-
operation: "destroy-tray",
112-
platform,
113-
cause,
114-
}),
115-
}).pipe(Effect.orDie);
118+
yield* destroyTray(currentTray.value).pipe(Effect.orDie);
119+
yield* Ref.set(currentTrayRef, Option.none());
116120
}
117121
});
118122

119123
const create = (options: ElectronTrayCreateOptions) =>
120124
Effect.gen(function* () {
121125
yield* destroy;
122126

127+
let partialTray: Electron.Tray | undefined;
123128
const tray = yield* Effect.try({
124129
try: () => {
125-
let partialTray: Electron.Tray | undefined;
126-
try {
127-
const icon = Electron.nativeImage.createFromPath(options.iconPath);
128-
const newTray = new Electron.Tray(icon.isEmpty() ? options.iconPath : icon);
129-
partialTray = newTray;
130-
if (options.tooltip) {
131-
newTray.setToolTip(options.tooltip);
132-
}
133-
if (options.menuItems && options.menuItems.length > 0) {
134-
const template = mapMenuItems(options.menuItems);
135-
const contextMenu = Electron.Menu.buildFromTemplate(template);
136-
newTray.setContextMenu(contextMenu);
137-
}
138-
if (options.onClick) {
139-
newTray.on("click", options.onClick);
140-
}
141-
if (options.onDoubleClick) {
142-
newTray.on("double-click", options.onDoubleClick);
143-
}
144-
return newTray;
145-
} catch (cause) {
146-
if (partialTray && !partialTray.isDestroyed()) {
147-
try {
148-
partialTray.destroy();
149-
} catch {
150-
// ignore
151-
}
152-
}
153-
throw cause;
130+
const icon = Electron.nativeImage.createFromPath(options.iconPath);
131+
const newTray = new Electron.Tray(icon.isEmpty() ? options.iconPath : icon);
132+
partialTray = newTray;
133+
if (options.tooltip) {
134+
newTray.setToolTip(options.tooltip);
135+
}
136+
if (options.menuItems && options.menuItems.length > 0) {
137+
const template = mapMenuItems(options.menuItems);
138+
const contextMenu = Electron.Menu.buildFromTemplate(template);
139+
newTray.setContextMenu(contextMenu);
140+
}
141+
if (options.onClick) {
142+
newTray.on("click", options.onClick);
143+
}
144+
if (options.onDoubleClick) {
145+
newTray.on("double-click", options.onDoubleClick);
154146
}
147+
return newTray;
155148
},
156149
catch: (cause) =>
157150
new ElectronTrayCreateError({
158151
iconPath: options.iconPath,
159152
cause,
160153
}),
161-
});
154+
}).pipe(
155+
Effect.tap((createdTray) => Ref.set(currentTrayRef, Option.some(createdTray))),
156+
Effect.tapError(() =>
157+
partialTray ? destroyTray(partialTray).pipe(Effect.orDie) : Effect.void,
158+
),
159+
);
162160

163-
yield* Ref.set(currentTrayRef, Option.some(tray));
164161
return tray;
165162
});
166163

0 commit comments

Comments
 (0)