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