Open History to see the last commit
1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Option from "effect/Option";
5import * as Schema from "effect/Schema";
6
7import type * as Electron from "electron";
8
9import { makeComponentLogger } from "../app/DesktopObservability.ts";
10import * as ElectronApp from "../electron/ElectronApp.ts";
11import * as ElectronDialog from "../electron/ElectronDialog.ts";
12import * as ElectronMenu from "../electron/ElectronMenu.ts";
13import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
14import * as DesktopUpdates from "../updates/DesktopUpdates.ts";
15import * as DesktopWindow from "./DesktopWindow.ts";
16
17export class DesktopApplicationMenuActionError extends Schema.TaggedError<DesktopApplicationMenuActionError>()(
18 "DesktopApplicationMenuActionError",
19 {
20 action: Schema.String,
21 cause: Schema.Defect(),
22 },
23) {
24 override get message(): string {
25 return `Desktop menu action "${this.action}" failed.`;
26 }
27}
28
29export class DesktopApplicationMenu extends Context.Service<
30 DesktopApplicationMenu,
31 {
32 readonly configure: Effect.Effect<void>;
33 }
34>()("@t3tools/desktop/window/DesktopApplicationMenu") {}
35
36type DesktopApplicationMenuRuntimeServices =
37 | DesktopUpdates.DesktopUpdates
38 | DesktopWindow.DesktopWindow
39 | ElectronDialog.ElectronDialog;
40
41const { logInfo: logUpdaterInfo } = makeComponentLogger("desktop-updater");
42
43const { logError: logMenuError } = makeComponentLogger("desktop-menu");
44
45const dispatchMenuAction = Effect.fn("desktop.menu.dispatchMenuAction")(function* (
46 action: string,
47): Effect.fn.Return<void, DesktopWindow.DesktopWindowError, DesktopWindow.DesktopWindow> {
48 const desktopWindow = yield* DesktopWindow.DesktopWindow;
49 yield* desktopWindow.dispatchMenuAction(action, {
50 reveal: action !== "paste-as-text",
51 });
52});
53
54const zoomMainWindow = Effect.fn("desktop.menu.zoomMainWindow")(function* (
55 direction: DesktopWindow.MainWindowZoomDirection,
56): Effect.fn.Return<void, never, DesktopWindow.DesktopWindow> {
57 const desktopWindow = yield* DesktopWindow.DesktopWindow;
58 yield* desktopWindow.zoomMain(direction);
59});
60
61const checkForUpdatesFromMenu = Effect.gen(function* () {
62 const updates = yield* DesktopUpdates.DesktopUpdates;
63 const electronDialog = yield* ElectronDialog.ElectronDialog;
64 const result = yield* updates.check("menu");
65 const updateState = result.state;
66
67 if (updateState.status === "up-to-date") {
68 yield* electronDialog.showMessageBox({
69 type: "info",
70 title: "You're up to date!",
71 message: `T3 Code ${updateState.currentVersion} is currently the newest version available.`,
72 buttons: ["OK"],
73 });
74 } else if (updateState.status === "error") {
75 yield* electronDialog.showMessageBox({
76 type: "warning",
77 title: "Update check failed",
78 message: "Could not check for updates.",
79 detail: updateState.message ?? "An unknown error occurred. Please try again later.",
80 buttons: ["OK"],
81 });
82 }
83}).pipe(Effect.withSpan("desktop.menu.checkForUpdates"));
84
85const handleCheckForUpdatesMenuClick = Effect.gen(function* () {
86 const updates = yield* DesktopUpdates.DesktopUpdates;
87 const electronDialog = yield* ElectronDialog.ElectronDialog;
88 const disabledReason = yield* updates.disabledReason;
89 if (Option.isSome(disabledReason)) {
90 yield* logUpdaterInfo("manual update check requested, but updates are disabled", {
91 disabledReason: disabledReason.value,
92 });
93 yield* electronDialog.showMessageBox({
94 type: "info",
95 title: "Updates unavailable",
96 message: "Automatic updates are not available right now.",
97 detail: disabledReason.value,
98 buttons: ["OK"],
99 });
100 return;
101 }
102
103 const desktopWindow = yield* DesktopWindow.DesktopWindow;
104 yield* desktopWindow.ensureMain;
105 yield* checkForUpdatesFromMenu;
106}).pipe(Effect.withSpan("desktop.menu.handleCheckForUpdatesClick"));
107
108/** @public Service construction is part of the canonical Effect module API. */
109export const make = Effect.gen(function* () {
110 const electronApp = yield* ElectronApp.ElectronApp;
111 const electronMenu = yield* ElectronMenu.ElectronMenu;
112 const environment = yield* DesktopEnvironment.DesktopEnvironment;
113 const appName = yield* electronApp.name;
114 const context = yield* Effect.context<DesktopApplicationMenuRuntimeServices>();
115 const runPromise = Effect.runPromiseWith(context);
116
117 const runMenuEffect = <E>(
118 action: string,
119 effect: Effect.Effect<void, E, DesktopApplicationMenuRuntimeServices>,
120 ) => {
121 void runPromise(
122 effect.pipe(
123 Effect.annotateLogs({ action }),
124 Effect.withSpan("desktop.menu.action"),
125 Effect.catchCause((cause) => {
126 const error = new DesktopApplicationMenuActionError({ action, cause });
127 return logMenuError(error.message, { error });
128 }),
129 ),
130 );
131 };
132
133 const configure = Effect.gen(function* () {
134 const checkForUpdatesClick = () => {
135 runMenuEffect("check-for-updates", handleCheckForUpdatesMenuClick);
136 };
137 const settingsClick = () => {
138 runMenuEffect("open-settings", dispatchMenuAction("open-settings"));
139 };
140 const pasteAsTextClick = () => {
141 runMenuEffect("paste-as-text", dispatchMenuAction("paste-as-text"));
142 };
143 const zoomClick = (direction: DesktopWindow.MainWindowZoomDirection) => () => {
144 runMenuEffect(`zoom-${direction}`, zoomMainWindow(direction));
145 };
146 const template: Electron.MenuItemConstructorOptions[] = [];
147
148 if (environment.platform === "darwin") {
149 template.push({
150 label: appName,
151 submenu: [
152 { role: "about" },
153 {
154 label: "Check for Updates...",
155 click: checkForUpdatesClick,
156 },
157 { type: "separator" },
158 {
159 label: "Settings...",
160 accelerator: "CmdOrCtrl+,",
161 click: settingsClick,
162 },
163 { type: "separator" },
164 { role: "services" },
165 { type: "separator" },
166 { role: "hide" },
167 { role: "hideOthers" },
168 { role: "unhide" },
169 { type: "separator" },
170 { role: "quit" },
171 ],
172 });
173 }
174
175 template.push(
176 {
177 label: "File",
178 submenu: [
179 ...(environment.platform === "darwin"
180 ? []
181 : [
182 {
183 label: "Settings...",
184 accelerator: "CmdOrCtrl+,",
185 click: settingsClick,
186 },
187 { type: "separator" as const },
188 ]),
189 { role: environment.platform === "darwin" ? "close" : "quit" },
190 ],
191 },
192 {
193 label: "Edit",
194 submenu: [
195 { role: "undo" },
196 { role: "redo" },
197 { type: "separator" },
198 { role: "cut" },
199 { role: "copy" },
200 { role: "paste" },
201 {
202 label: "Paste as Text",
203 accelerator: "CmdOrCtrl+Shift+V",
204 click: pasteAsTextClick,
205 },
206 { role: "delete" },
207 { type: "separator" },
208 { role: "selectAll" },
209 ...(environment.platform === "darwin"
210 ? [
211 { type: "separator" as const },
212 {
213 label: "Speech",
214 submenu: [{ role: "startSpeaking" as const }, { role: "stopSpeaking" as const }],
215 },
216 ]
217 : []),
218 ],
219 },
220 {
221 label: "View",
222 submenu: [
223 { role: "reload" },
224 { role: "forceReload" },
225 { role: "toggleDevTools" },
226 { type: "separator" },
227 /*
228 Not the zoom roles: those act on the focused webContents, so with
229 an embedded preview WebContentsView focused they zoom the guest
230 page and the app UI appears stuck. These always zoom the main
231 window (see DesktopWindow.zoomMain).
232 */
233 { label: "Actual Size", accelerator: "CmdOrCtrl+0", click: zoomClick("reset") },
234 { label: "Zoom In", accelerator: "CmdOrCtrl+=", click: zoomClick("in") },
235 {
236 label: "Zoom In",
237 accelerator: "CmdOrCtrl+Plus",
238 visible: false,
239 click: zoomClick("in"),
240 },
241 { label: "Zoom Out", accelerator: "CmdOrCtrl+-", click: zoomClick("out") },
242 { type: "separator" },
243 { role: "togglefullscreen" },
244 ],
245 },
246 { role: "windowMenu" },
247 {
248 role: "help",
249 submenu: [
250 {
251 label: "Check for Updates...",
252 click: checkForUpdatesClick,
253 },
254 ],
255 },
256 );
257
258 yield* electronMenu.setApplicationMenu(template);
259 }).pipe(Effect.withSpan("desktop.menu.configure"));
260
261 return DesktopApplicationMenu.of({
262 configure,
263 });
264});
265
266export const layer = Layer.effect(DesktopApplicationMenu, make);