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 * as Electron from "electron";
8
9const CONFIRM_BUTTON_INDEX = 1;
10
11export class ElectronDialogPickFolderError extends Schema.TaggedErrorClass<ElectronDialogPickFolderError>()(
12 "ElectronDialogPickFolderError",
13 {
14 ownerWindowId: Schema.NullOr(Schema.Number),
15 defaultPath: Schema.NullOr(Schema.String),
16 cause: Schema.Defect(),
17 },
18) {
19 override get message(): string {
20 const owner = this.ownerWindowId === null ? "the application" : `window ${this.ownerWindowId}`;
21 const defaultPath = this.defaultPath === null ? "no default path" : this.defaultPath;
22 return `Failed to open the Electron folder picker for ${owner} with ${defaultPath}.`;
23 }
24}
25
26export class ElectronDialogPickFilesError extends Schema.TaggedErrorClass<ElectronDialogPickFilesError>()(
27 "ElectronDialogPickFilesError",
28 {
29 ownerWindowId: Schema.NullOr(Schema.Number),
30 defaultPath: Schema.NullOr(Schema.String),
31 cause: Schema.Defect(),
32 },
33) {
34 override get message(): string {
35 const owner = this.ownerWindowId === null ? "the application" : `window ${this.ownerWindowId}`;
36 const defaultPath = this.defaultPath === null ? "no default path" : this.defaultPath;
37 return `Failed to open the Electron file picker for ${owner} with ${defaultPath}.`;
38 }
39}
40
41export class ElectronDialogConfirmError extends Schema.TaggedErrorClass<ElectronDialogConfirmError>()(
42 "ElectronDialogConfirmError",
43 {
44 ownerWindowId: Schema.NullOr(Schema.Number),
45 promptLength: Schema.Number,
46 cause: Schema.Defect(),
47 },
48) {
49 override get message(): string {
50 const owner = this.ownerWindowId === null ? "the application" : `window ${this.ownerWindowId}`;
51 return `Failed to open an Electron confirmation dialog for ${owner} with a ${this.promptLength}-character prompt.`;
52 }
53}
54
55export class ElectronDialogShowMessageBoxError extends Schema.TaggedErrorClass<ElectronDialogShowMessageBoxError>()(
56 "ElectronDialogShowMessageBoxError",
57 {
58 type: Schema.NullOr(Schema.Literals(["none", "info", "error", "question", "warning"])),
59 titleLength: Schema.NullOr(Schema.Number),
60 messageLength: Schema.Number,
61 detailLength: Schema.NullOr(Schema.Number),
62 buttonCount: Schema.Number,
63 cause: Schema.Defect(),
64 },
65) {
66 override get message(): string {
67 const type = this.type === null ? "untyped" : this.type;
68 return `Failed to show the Electron ${type} message box with ${this.buttonCount} buttons.`;
69 }
70}
71
72export class ElectronDialogShowErrorBoxError extends Schema.TaggedErrorClass<ElectronDialogShowErrorBoxError>()(
73 "ElectronDialogShowErrorBoxError",
74 {
75 titleLength: Schema.Number,
76 contentLength: Schema.Number,
77 cause: Schema.Defect(),
78 },
79) {
80 override get message(): string {
81 return `Failed to show the Electron error box with a ${this.titleLength}-character title and ${this.contentLength}-character content.`;
82 }
83}
84
85export const ElectronDialogError = Schema.Union([
86 ElectronDialogPickFolderError,
87 ElectronDialogPickFilesError,
88 ElectronDialogConfirmError,
89 ElectronDialogShowMessageBoxError,
90 ElectronDialogShowErrorBoxError,
91]);
92export type ElectronDialogError = typeof ElectronDialogError.Type;
93export const isElectronDialogError = Schema.is(ElectronDialogError);
94
95export interface ElectronDialogPickFolderInput {
96 readonly owner: Option.Option<Electron.BrowserWindow>;
97 readonly defaultPath: Option.Option<string>;
98}
99
100export interface ElectronDialogPickFilesInput {
101 readonly owner: Option.Option<Electron.BrowserWindow>;
102 readonly defaultPath: Option.Option<string>;
103 readonly filters: readonly Electron.FileFilter[];
104}
105
106export interface ElectronDialogConfirmInput {
107 readonly owner: Option.Option<Electron.BrowserWindow>;
108 readonly message: string;
109}
110
111export class ElectronDialog extends Context.Service<
112 ElectronDialog,
113 {
114 readonly pickFolder: (
115 input: ElectronDialogPickFolderInput,
116 ) => Effect.Effect<Option.Option<string>, ElectronDialogPickFolderError>;
117 readonly pickFiles: (
118 input: ElectronDialogPickFilesInput,
119 ) => Effect.Effect<readonly string[], ElectronDialogPickFilesError>;
120 readonly confirm: (
121 input: ElectronDialogConfirmInput,
122 ) => Effect.Effect<boolean, ElectronDialogConfirmError>;
123 readonly showMessageBox: (
124 options: Electron.MessageBoxOptions,
125 ) => Effect.Effect<Electron.MessageBoxReturnValue, ElectronDialogShowMessageBoxError>;
126 readonly showErrorBox: (title: string, content: string) => Effect.Effect<void>;
127 }
128>()("@t3tools/desktop/electron/ElectronDialog") {}
129
130export const make = ElectronDialog.of({
131 pickFolder: Effect.fn("desktop.electron.dialog.pickFolder")(function* (input) {
132 const ownerWindowId = Option.match(input.owner, {
133 onNone: () => null,
134 onSome: (owner) => owner.id,
135 });
136 const defaultPath = Option.getOrNull(input.defaultPath);
137 const openDialogOptions: Electron.OpenDialogOptions = Option.match(input.defaultPath, {
138 onNone: () => ({
139 properties: ["openDirectory", "createDirectory"],
140 }),
141 onSome: (defaultPath) => ({
142 properties: ["openDirectory", "createDirectory"],
143 defaultPath,
144 }),
145 });
146 const result = yield* Effect.tryPromise({
147 try: () =>
148 Option.match(input.owner, {
149 onNone: () => Electron.dialog.showOpenDialog(openDialogOptions),
150 onSome: (owner) => Electron.dialog.showOpenDialog(owner, openDialogOptions),
151 }),
152 catch: (cause) =>
153 new ElectronDialogPickFolderError({
154 ownerWindowId,
155 defaultPath,
156 cause,
157 }),
158 });
159
160 if (result.canceled) {
161 return Option.none();
162 }
163 return Option.fromNullishOr(result.filePaths[0]);
164 }),
165 pickFiles: Effect.fn("desktop.electron.dialog.pickFiles")(function* (input) {
166 const ownerWindowId = Option.match(input.owner, {
167 onNone: () => null,
168 onSome: (owner) => owner.id,
169 });
170 const defaultPath = Option.getOrNull(input.defaultPath);
171 const openDialogOptions: Electron.OpenDialogOptions = {
172 properties: ["openFile", "multiSelections"],
173 filters: [...input.filters],
174 ...(defaultPath === null ? {} : { defaultPath }),
175 };
176 const result = yield* Effect.tryPromise({
177 try: () =>
178 Option.match(input.owner, {
179 onNone: () => Electron.dialog.showOpenDialog(openDialogOptions),
180 onSome: (owner) => Electron.dialog.showOpenDialog(owner, openDialogOptions),
181 }),
182 catch: (cause) =>
183 new ElectronDialogPickFilesError({
184 ownerWindowId,
185 defaultPath,
186 cause,
187 }),
188 });
189 return result.canceled ? [] : result.filePaths;
190 }),
191 confirm: Effect.fn("desktop.electron.dialog.confirm")(function* (input) {
192 const normalizedMessage = input.message.trim();
193 if (normalizedMessage.length === 0) {
194 return false;
195 }
196
197 const options = {
198 type: "question" as const,
199 buttons: ["No", "Yes"],
200 defaultId: 0,
201 cancelId: 0,
202 noLink: true,
203 message: normalizedMessage,
204 };
205 const ownerWindowId = Option.match(input.owner, {
206 onNone: () => null,
207 onSome: (owner) => owner.id,
208 });
209 const result = yield* Effect.tryPromise({
210 try: () =>
211 Option.match(input.owner, {
212 onNone: () => Electron.dialog.showMessageBox(options),
213 onSome: (owner) => Electron.dialog.showMessageBox(owner, options),
214 }),
215 catch: (cause) =>
216 new ElectronDialogConfirmError({
217 ownerWindowId,
218 promptLength: normalizedMessage.length,
219 cause,
220 }),
221 });
222 return result.response === CONFIRM_BUTTON_INDEX;
223 }),
224 showMessageBox: (options) =>
225 Effect.tryPromise({
226 try: () => Electron.dialog.showMessageBox(options),
227 catch: (cause) =>
228 new ElectronDialogShowMessageBoxError({
229 type: options.type ?? null,
230 titleLength: options.title?.length ?? null,
231 messageLength: options.message.length,
232 detailLength: options.detail?.length ?? null,
233 buttonCount: options.buttons?.length ?? 0,
234 cause,
235 }),
236 }),
237 showErrorBox: (title, content) =>
238 Effect.try({
239 try: () => Electron.dialog.showErrorBox(title, content),
240 catch: (cause) =>
241 new ElectronDialogShowErrorBoxError({
242 titleLength: title.length,
243 contentLength: content.length,
244 cause,
245 }),
246 }).pipe(Effect.orDie),
247});
248
249export const layer = Layer.succeed(ElectronDialog, make);