51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
import { dialog, ipcMain, BrowserWindow } from "electron";
|
|
|
|
type AskOptions = {
|
|
title?: string;
|
|
okLabel?: string;
|
|
cancelLabel?: string;
|
|
kind?: "info" | "warning" | "error";
|
|
};
|
|
|
|
type OpenOptions = {
|
|
title?: string;
|
|
defaultPath?: string;
|
|
directory?: boolean;
|
|
multiple?: boolean;
|
|
filters?: Array<{ name: string; extensions: string[] }>;
|
|
};
|
|
|
|
export function registerDialogHandlers(): void {
|
|
ipcMain.handle("paseo:dialog:ask", async (event, message: string, options?: AskOptions) => {
|
|
const win = BrowserWindow.fromWebContents(event.sender);
|
|
const result = await dialog.showMessageBox(win ?? BrowserWindow.getFocusedWindow()!, {
|
|
type:
|
|
options?.kind === "warning" ? "warning" : options?.kind === "error" ? "error" : "question",
|
|
title: options?.title ?? "Confirm",
|
|
message,
|
|
buttons: [options?.cancelLabel ?? "Cancel", options?.okLabel ?? "OK"],
|
|
defaultId: 1,
|
|
cancelId: 0,
|
|
});
|
|
return result.response === 1;
|
|
});
|
|
|
|
ipcMain.handle("paseo:dialog:open", async (event, options?: OpenOptions) => {
|
|
const win = BrowserWindow.fromWebContents(event.sender);
|
|
const properties: Electron.OpenDialogOptions["properties"] = [];
|
|
if (options?.directory) properties.push("openDirectory");
|
|
if (options?.multiple) properties.push("multiSelections");
|
|
if (!options?.directory) properties.push("openFile");
|
|
|
|
const result = await dialog.showOpenDialog(win ?? BrowserWindow.getFocusedWindow()!, {
|
|
title: options?.title,
|
|
defaultPath: options?.defaultPath,
|
|
properties,
|
|
filters: options?.filters,
|
|
});
|
|
|
|
if (result.canceled) return null;
|
|
return options?.multiple ? result.filePaths : (result.filePaths[0] ?? null);
|
|
});
|
|
}
|