Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronDialog.ts

192 lines6.8 KB

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 }
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 }
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 }
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 }
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>;
82​
83export interface ElectronDialogPickFilesInput {
84 readonly owner: Option.Option<Electron.BrowserWindow>;
85 readonly defaultPath: Option.Option<string>;
86 readonly filters: readonly Electron.FileFilter[];
87 readonly multiple: boolean;
89​
90export class ElectronDialog extends Context.Service<
91 ElectronDialog,
92 {
93 readonly pickFolder: (
94 input: ElectronDialogPickFolderInput,
95 ) => Effect.Effect<Option.Option<string>, ElectronDialogPickFolderError>;
96 readonly pickFiles: (
97 input: ElectronDialogPickFilesInput,
98 ) => Effect.Effect<readonly string[], ElectronDialogPickFilesError>;
99 readonly showMessageBox: (
100 options: Electron.MessageBoxOptions,
101 ) => Effect.Effect<Electron.MessageBoxReturnValue, ElectronDialogShowMessageBoxError>;
102 readonly showErrorBox: (title: string, content: string) => Effect.Effect<void>;
103 }
104>()("@t3tools/desktop/electron/ElectronDialog") {}
105​
106export const make = ElectronDialog.of({
107 pickFolder: Effect.fn("desktop.electron.dialog.pickFolder")(function* (input) {
108 const ownerWindowId = Option.match(input.owner, {
109 onNone: () => null,
110 onSome: (owner) => owner.id,
111 });
112 const defaultPath = Option.getOrNull(input.defaultPath);
113 const openDialogOptions: Electron.OpenDialogOptions = Option.match(input.defaultPath, {
114 onNone: () => ({
115 properties: ["openDirectory", "createDirectory"],
116 }),
117 onSome: (defaultPath) => ({
118 properties: ["openDirectory", "createDirectory"],
119 defaultPath,
120 }),
121 });
122 const result = yield* Effect.tryPromise({
123 try: () =>
124 Option.match(input.owner, {
125 onNone: () => Electron.dialog.showOpenDialog(openDialogOptions),
126 onSome: (owner) => Electron.dialog.showOpenDialog(owner, openDialogOptions),
127 }),
128 catch: (cause) =>
129 new ElectronDialogPickFolderError({
130 ownerWindowId,
131 defaultPath,
132 cause,
133 }),
134 });
135​
136 if (result.canceled) {
137 return Option.none();
138 }
139 return Option.fromNullishOr(result.filePaths[0]);
140 }),
141 pickFiles: Effect.fn("desktop.electron.dialog.pickFiles")(function* (input) {
142 const ownerWindowId = Option.match(input.owner, {
143 onNone: () => null,
144 onSome: (owner) => owner.id,
145 });
146 const defaultPath = Option.getOrNull(input.defaultPath);
147 const openDialogOptions: Electron.OpenDialogOptions = {
148 properties: input.multiple ? ["openFile", "multiSelections"] : ["openFile"],
149 filters: [...input.filters],
150 ...(defaultPath === null ? {} : { defaultPath }),
151 };
152 const result = yield* Effect.tryPromise({
153 try: () =>
154 Option.match(input.owner, {
155 onNone: () => Electron.dialog.showOpenDialog(openDialogOptions),
156 onSome: (owner) => Electron.dialog.showOpenDialog(owner, openDialogOptions),
157 }),
158 catch: (cause) =>
159 new ElectronDialogPickFilesError({
160 ownerWindowId,
161 defaultPath,
162 cause,
163 }),
164 });
165 return result.canceled ? [] : result.filePaths;
166 }),
167 showMessageBox: (options) =>
168 Effect.tryPromise({
169 try: () => Electron.dialog.showMessageBox(options),
170 catch: (cause) =>
171 new ElectronDialogShowMessageBoxError({
172 type: options.type ?? null,
173 titleLength: options.title?.length ?? null,
174 messageLength: options.message.length,
175 detailLength: options.detail?.length ?? null,
176 buttonCount: options.buttons?.length ?? 0,
177 cause,
178 }),
179 }),
180 showErrorBox: (title, content) =>
181 Effect.try({
182 try: () => Electron.dialog.showErrorBox(title, content),
183 catch: (cause) =>
184 new ElectronDialogShowErrorBoxError({
185 titleLength: title.length,
186 contentLength: content.length,
187 cause,
188 }),
189 }).pipe(Effect.orDie),
190});
191​
192export const layer = Layer.succeed(ElectronDialog, make);