From 56c33032d030a54f04216612ef7e708a9ebfde70 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sun, 30 Jul 2023 00:14:20 +0200 Subject: [PATCH] Add dialogs --- src/main.ts | 22 +++++++++++++++++++++- src/preload.ts | 4 ++++ src/scripts/renderer.ts | 35 ++++++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/main.ts b/src/main.ts index cb04677..ca3bd4c 100644 --- a/src/main.ts +++ b/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(); diff --git a/src/preload.ts b/src/preload.ts index 7c19264..688d8dd 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -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"), }); diff --git a/src/scripts/renderer.ts b/src/scripts/renderer.ts index 5595e91..bbe3fdb 100644 --- a/src/scripts/renderer.ts +++ b/src/scripts/renderer.ts @@ -1,17 +1,30 @@ -/* Context bridge */ +/* Context bridge types */ let internals: { ffmpeg: () => Promise; - argv: () => Promise; + argv: () => Promise; + allowedExtensions: () => Promise<{ + extensions: string[]; + }>; + askFile: () => Promise; + 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));