Add dialogs
This commit is contained in:
parent
b055aad397
commit
56c33032d0
3 changed files with 49 additions and 12 deletions
22
src/main.ts
22
src/main.ts
|
@ -1,7 +1,8 @@
|
|||
import { BrowserWindow, app, ipcMain } from "electron";
|
||||
import { FileFilter, BrowserWindow, app, dialog, ipcMain } from "electron";
|
||||
import path = require("path");
|
||||
import ffmpegPath = require("ffmpeg-static");
|
||||
|
||||
/** Create a new window */
|
||||
const createWindow = () => {
|
||||
const win = new BrowserWindow({
|
||||
width: 1280,
|
||||
|
@ -15,9 +16,28 @@ const createWindow = () => {
|
|||
win.webContents.openDevTools(); // debug
|
||||
};
|
||||
|
||||
const moviesFilter = {
|
||||
name: "Videos",
|
||||
extensions: ["mp4", "mkv"],
|
||||
} as FileFilter;
|
||||
|
||||
/** Ask user a file */
|
||||
const askFile = async () => {
|
||||
return (
|
||||
await dialog.showOpenDialog({
|
||||
filters: [moviesFilter],
|
||||
properties: ["openFile", "dontAddToRecent"],
|
||||
})
|
||||
).filePaths;
|
||||
};
|
||||
|
||||
app.whenReady().then(() => {
|
||||
/* Context bridge */
|
||||
ipcMain.handle("ffmpeg", () => ffmpegPath);
|
||||
ipcMain.handle("argv", () => process.argv);
|
||||
ipcMain.handle("allowedExtensions", () => moviesFilter);
|
||||
ipcMain.handle("askfile", () => askFile());
|
||||
ipcMain.handle("exit", () => app.quit());
|
||||
|
||||
createWindow();
|
||||
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
import { contextBridge, ipcRenderer } from "electron";
|
||||
|
||||
/* Context bridge */
|
||||
contextBridge.exposeInMainWorld("internals", {
|
||||
ffmpeg: () => ipcRenderer.invoke("ffmpeg"),
|
||||
argv: () => ipcRenderer.invoke("argv"),
|
||||
allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"),
|
||||
askFile: () => ipcRenderer.invoke("askfile"),
|
||||
exit: () => ipcRenderer.invoke("exit"),
|
||||
});
|
||||
|
|
|
@ -1,17 +1,30 @@
|
|||
/* Context bridge */
|
||||
/* Context bridge types */
|
||||
let internals: {
|
||||
ffmpeg: () => Promise<string>;
|
||||
argv: () => Promise<string>;
|
||||
argv: () => Promise<string[]>;
|
||||
allowedExtensions: () => Promise<{
|
||||
extensions: string[];
|
||||
}>;
|
||||
askFile: () => Promise<string[]>;
|
||||
exit: () => any;
|
||||
};
|
||||
|
||||
const get_ffmpeg = async () => {
|
||||
const response = await internals.ffmpeg();
|
||||
console.log(response);
|
||||
};
|
||||
get_ffmpeg();
|
||||
const get_file = async () => {
|
||||
const allowedExtensions = (await internals.allowedExtensions()).extensions;
|
||||
console.log(allowedExtensions);
|
||||
const argv = await internals.argv();
|
||||
if (argv.length === 2) {
|
||||
const file = argv.pop();
|
||||
if (allowedExtensions.some((ext) => file.endsWith(ext))) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
|
||||
const get_argv = async () => {
|
||||
const response = await internals.argv();
|
||||
console.log(response);
|
||||
const file = await internals.askFile();
|
||||
if (file.length === 0) {
|
||||
await internals.exit();
|
||||
}
|
||||
return file.join("");
|
||||
};
|
||||
get_argv();
|
||||
|
||||
get_file().then((file) => (document.getElementById("info").innerText = file));
|
||||
|
|
Loading…
Reference in a new issue