Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7
8import type * as Electron from "electron";
9
10import * as ElectronApp from "../electron/ElectronApp.ts";
11import * as ElectronDialog from "../electron/ElectronDialog.ts";
12import * as ElectronMenu from "../electron/ElectronMenu.ts";
13import * as DesktopApplicationMenu from "./DesktopApplicationMenu.ts";
14import * as DesktopConfig from "../app/DesktopConfig.ts";
15import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
16import * as DesktopUpdates from "../updates/DesktopUpdates.ts";
17import * as DesktopWindow from "./DesktopWindow.ts";
18
19const environmentInput = {
20 dirname: "/repo/apps/desktop/dist-electron",
21 homeDirectory: "/Users/alice",
22 platform: "linux",
23 processArch: "arm64",
24 appVersion: "1.2.3",
25 appPath: "/repo",
26 isPackaged: false,
27 resourcesPath: "/repo/resources",
28 runningUnderArm64Translation: false,
29} satisfies DesktopEnvironment.MakeDesktopEnvironmentInput;
30
31const electronAppLayer = Layer.succeed(ElectronApp.ElectronApp, {
32 metadata: Effect.die("unexpected metadata read"),
33 name: Effect.succeed("T3 Code"),
34 systemLocale: Effect.succeed("en-US"),
35 whenReady: Effect.void,
36 quit: Effect.void,
37 exit: () => Effect.void,
38 relaunch: () => Effect.void,
39 setPath: () => Effect.void,
40 setName: () => Effect.void,
41 setAboutPanelOptions: () => Effect.void,
42 setAppUserModelId: () => Effect.void,
43 getAppMetrics: Effect.succeed([]),
44 setAsDefaultProtocolClient: () => Effect.succeed(true),
45 setDesktopName: () => Effect.void,
46 setDockIcon: () => Effect.void,
47 appendCommandLineSwitch: () => Effect.void,
48 onBeforeQuitForUpdate: () => Effect.void,
49 removeCommandLineSwitch: () => Effect.void,
50 on: () => Effect.void,
51} satisfies ElectronApp.ElectronApp["Service"]);
52
53const electronDialogLayer = Layer.succeed(ElectronDialog.ElectronDialog, {
54 pickFolder: () => Effect.succeed(Option.none()),
55 pickFiles: () => Effect.succeed([]),
56 showMessageBox: () => Effect.succeed({ response: 0, checkboxChecked: false }),
57 showErrorBox: () => Effect.void,
58} satisfies ElectronDialog.ElectronDialog["Service"]);
59
60const desktopUpdatesLayer = Layer.succeed(DesktopUpdates.DesktopUpdates, {
61 getState: Effect.die("unexpected getState"),
62 isActionActive: Effect.succeed(false),
63 isInstallActive: Effect.succeed(false),
64 subscribe: Effect.die("unexpected subscribe"),
65 emitState: Effect.void,
66 disabledReason: Effect.succeed(Option.none()),
67 configure: Effect.void,
68 setChannel: () => Effect.die("unexpected setChannel"),
69 check: () => Effect.die("unexpected check"),
70 download: Effect.die("unexpected download"),
71 install: Effect.die("unexpected install"),
72 installPrepared: () => Effect.die("unexpected installPrepared"),
73} satisfies DesktopUpdates.DesktopUpdates["Service"]);
74
75const makeDesktopWindowLayer = (selectedAction: Deferred.Deferred<string>) =>
76 Layer.succeed(DesktopWindow.DesktopWindow, {
77 createMain: Effect.die("unexpected createMain"),
78 ensureMain: Effect.die("unexpected ensureMain"),
79 revealOrCreateMain: Effect.die("unexpected revealOrCreateMain"),
80 activate: Effect.void,
81 createMainIfBackendReady: Effect.void,
82 showConnectingSplash: Effect.void,
83 handleBackendReady: () => Effect.void,
84 handleBackendNotReady: Effect.void,
85 flushMainWindowBounds: Effect.void,
86 prepareCaptureReveal: Effect.void,
87 dispatchMenuAction: (action) => Deferred.succeed(selectedAction, action).pipe(Effect.asVoid),
88 dispatchSnapShotEvent: () => Effect.void,
89 zoomMain: (direction) =>
90 Deferred.succeed(selectedAction, `zoom-${direction}`).pipe(Effect.asVoid),
91 syncAppearance: Effect.void,
92 } satisfies DesktopWindow.DesktopWindow["Service"]);
93
94const makeElectronMenuLayer = (
95 applicationMenuTemplate: Deferred.Deferred<readonly Electron.MenuItemConstructorOptions[]>,
96) =>
97 Layer.succeed(ElectronMenu.ElectronMenu, {
98 setApplicationMenu: (template) =>
99 Deferred.succeed(applicationMenuTemplate, template).pipe(Effect.asVoid),
100 popupTemplate: () => Effect.void,
101 showContextMenu: () => Effect.succeed(Option.none()),
102 } satisfies ElectronMenu.ElectronMenu["Service"]);
103
104const configureMenu = (
105 selectedAction: Deferred.Deferred<string>,
106 applicationMenuTemplate: Deferred.Deferred<readonly Electron.MenuItemConstructorOptions[]>,
107) =>
108 Effect.gen(function* () {
109 const menu = yield* DesktopApplicationMenu.DesktopApplicationMenu;
110 yield* menu.configure;
111 }).pipe(
112 Effect.provide(
113 DesktopApplicationMenu.layer.pipe(
114 Layer.provideMerge(makeElectronMenuLayer(applicationMenuTemplate)),
115 Layer.provideMerge(makeDesktopWindowLayer(selectedAction)),
116 Layer.provideMerge(desktopUpdatesLayer),
117 Layer.provideMerge(electronDialogLayer),
118 Layer.provideMerge(electronAppLayer),
119 Layer.provideMerge(
120 DesktopEnvironment.layer(environmentInput).pipe(
121 Layer.provide(Layer.mergeAll(NodeServices.layer, DesktopConfig.layerTest({}))),
122 ),
123 ),
124 ),
125 ),
126 );
127
128describe("DesktopApplicationMenu", () => {
129 it.effect("installs the native menu and routes Settings through DesktopWindow", () =>
130 Effect.gen(function* () {
131 const selectedAction = yield* Deferred.make<string>();
132 const applicationMenuTemplate =
133 yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
134
135 yield* configureMenu(selectedAction, applicationMenuTemplate);
136
137 const template = yield* Deferred.await(applicationMenuTemplate);
138 const fileMenu = template.find((item) => item.label === "File");
139 assert.isDefined(fileMenu);
140 if (!Array.isArray(fileMenu.submenu)) {
141 throw new Error("Expected File menu submenu to be an array.");
142 }
143 const settingsItem = fileMenu.submenu.find((item) => item.label === "Settings...");
144 assert.isDefined(settingsItem);
145 const settingsClick = settingsItem.click;
146 if (typeof settingsClick !== "function") {
147 throw new Error("Expected Settings menu item to have a click handler.");
148 }
149
150 settingsClick({} as Electron.MenuItem, {} as Electron.BrowserWindow, {} as KeyboardEvent);
151 assert.equal(yield* Deferred.await(selectedAction), "open-settings");
152 }),
153 );
154
155 it.effect("owns Paste as Text and routes it through the renderer", () =>
156 Effect.gen(function* () {
157 const selectedAction = yield* Deferred.make<string>();
158 const applicationMenuTemplate =
159 yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
160
161 yield* configureMenu(selectedAction, applicationMenuTemplate);
162
163 const template = yield* Deferred.await(applicationMenuTemplate);
164 const editMenu = template.find((item) => item.label === "Edit");
165 assert.isDefined(editMenu);
166 if (!Array.isArray(editMenu.submenu)) {
167 throw new Error("Expected Edit menu submenu to be an array.");
168 }
169 const pasteAsTextItem = editMenu.submenu.find((item) => item.label === "Paste as Text");
170 assert.isDefined(pasteAsTextItem);
171 assert.equal(pasteAsTextItem.accelerator, "CmdOrCtrl+Shift+V");
172 if (typeof pasteAsTextItem.click !== "function") {
173 throw new Error("Expected Paste as Text menu item to have a click handler.");
174 }
175
176 pasteAsTextItem.click(
177 {} as Electron.MenuItem,
178 {} as Electron.BrowserWindow,
179 {} as KeyboardEvent,
180 );
181 assert.equal(yield* Deferred.await(selectedAction), "paste-as-text");
182 }),
183 );
184
185 // Chromium pastes as plain text for the accelerator on its own. Dispatching
186 // the action as well injects a second paste, which doubles the pasted text.
187 it.effect("leaves the accelerator to Chromium instead of injecting a paste", () =>
188 Effect.gen(function* () {
189 const selectedAction = yield* Deferred.make<string>();
190 const applicationMenuTemplate =
191 yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
192
193 yield* configureMenu(selectedAction, applicationMenuTemplate);
194
195 const template = yield* Deferred.await(applicationMenuTemplate);
196 const editMenu = template.find((item) => item.label === "Edit");
197 if (!Array.isArray(editMenu?.submenu)) {
198 throw new Error("Expected Edit menu submenu to be an array.");
199 }
200 const pasteAsTextItem = editMenu.submenu.find((item) => item.label === "Paste as Text");
201 if (typeof pasteAsTextItem?.click !== "function") {
202 throw new Error("Expected Paste as Text menu item to have a click handler.");
203 }
204
205 pasteAsTextItem.click(
206 {} as Electron.MenuItem,
207 {} as Electron.BrowserWindow,
208 {
209 triggeredByAccelerator: true,
210 } as unknown as KeyboardEvent,
211 );
212 assert.isFalse(yield* Deferred.isDone(selectedAction));
213 }),
214 );
215
216 // Zoom must route through DesktopWindow.zoomMain instead of the Electron
217 // zoom roles: the roles zoom whichever webContents has focus, which breaks
218 // app zoom while an embedded preview WebContentsView holds focus.
219 it.effect("routes View menu zoom to the main window instead of zoom roles", () =>
220 Effect.gen(function* () {
221 const selectedAction = yield* Deferred.make<string>();
222 const applicationMenuTemplate =
223 yield* Deferred.make<readonly Electron.MenuItemConstructorOptions[]>();
224
225 yield* configureMenu(selectedAction, applicationMenuTemplate);
226
227 const template = yield* Deferred.await(applicationMenuTemplate);
228 const viewMenu = template.find((item) => item.label === "View");
229 assert.isDefined(viewMenu);
230 if (!Array.isArray(viewMenu.submenu)) {
231 throw new Error("Expected View menu submenu to be an array.");
232 }
233
234 assert.isUndefined(
235 viewMenu.submenu.find((item) => item.role?.toLowerCase().includes("zoom")),
236 );
237
238 const zoomIn = viewMenu.submenu.find((item) => item.label === "Zoom In");
239 assert.isDefined(zoomIn);
240 assert.equal(zoomIn.accelerator, "CmdOrCtrl+=");
241 if (typeof zoomIn.click !== "function") {
242 throw new Error("Expected Zoom In menu item to have a click handler.");
243 }
244
245 zoomIn.click({} as Electron.MenuItem, {} as Electron.BrowserWindow, {} as KeyboardEvent);
246 assert.equal(yield* Deferred.await(selectedAction), "zoom-in");
247 }),
248 );
249});