diff --git a/electron/main-legacy.js b/electron/main-legacy.js index 86d44e7..af61fe2 100644 --- a/electron/main-legacy.js +++ b/electron/main-legacy.js @@ -17,6 +17,17 @@ const crypto = require("crypto"); // remember main window var mainWindow = null; +function getDialogParentWindow() { + const focused = BrowserWindow.getFocusedWindow(); + if (focused && !focused.isDestroyed()) { + return focused; + } + if (mainWindow && !mainWindow.isDestroyed()) { + return mainWindow; + } + return null; +} + // power save blocker id var activePowerSaveBlockerId = null; @@ -190,6 +201,38 @@ ipcMain.handle("showPathInFolder", (event, path) => { shell.showItemInFolder(path); }); +ipcMain.handle("open-path", (event, p) => { + return shell.openPath(p); +}); + +ipcMain.handle("pick-file", async () => { + const win = getDialogParentWindow(); + if (!win) { + return null; + } + const { canceled, filePaths } = await dialog.showOpenDialog(win, { + properties: ["openFile"], + }); + if (canceled || !filePaths || filePaths.length === 0) { + return null; + } + return filePaths[0]; +}); + +ipcMain.handle("pick-directory", async () => { + const win = getDialogParentWindow(); + if (!win) { + return null; + } + const { canceled, filePaths } = await dialog.showOpenDialog(win, { + properties: ["openDirectory"], + }); + if (canceled || !filePaths || filePaths.length === 0) { + return null; + } + return filePaths[0]; +}); + function log(message) { // log to stdout of this process console.log(message); diff --git a/electron/main.js b/electron/main.js index 576b496..9543ed3 100644 --- a/electron/main.js +++ b/electron/main.js @@ -21,6 +21,17 @@ const crypto = require("crypto"); // remember main window var mainWindow = null; +function getDialogParentWindow() { + const focused = BrowserWindow.getFocusedWindow(); + if (focused && !focused.isDestroyed()) { + return focused; + } + if (mainWindow && !mainWindow.isDestroyed()) { + return mainWindow; + } + return null; +} + // tray instance var tray = null; @@ -265,6 +276,38 @@ ipcMain.handle("showPathInFolder", (event, path) => { shell.showItemInFolder(path); }); +ipcMain.handle("open-path", (event, p) => { + return shell.openPath(p); +}); + +ipcMain.handle("pick-file", async () => { + const win = getDialogParentWindow(); + if (!win) { + return null; + } + const { canceled, filePaths } = await dialog.showOpenDialog(win, { + properties: ["openFile"], + }); + if (canceled || !filePaths || filePaths.length === 0) { + return null; + } + return filePaths[0]; +}); + +ipcMain.handle("pick-directory", async () => { + const win = getDialogParentWindow(); + if (!win) { + return null; + } + const { canceled, filePaths } = await dialog.showOpenDialog(win, { + properties: ["openDirectory"], + }); + if (canceled || !filePaths || filePaths.length === 0) { + return null; + } + return filePaths[0]; +}); + function log(message) { // log to stdout of this process console.log(message); diff --git a/electron/preload.js b/electron/preload.js index 8846598..19dc2e2 100644 --- a/electron/preload.js +++ b/electron/preload.js @@ -63,6 +63,15 @@ contextBridge.exposeInMainWorld("electron", { showPathInFolder: async function (path) { return await ipcRenderer.invoke("showPathInFolder", path); }, + openPath: async function (path) { + return await ipcRenderer.invoke("open-path", path); + }, + pickFile: async function () { + return await ipcRenderer.invoke("pick-file"); + }, + pickDirectory: async function () { + return await ipcRenderer.invoke("pick-directory"); + }, // allow checking hardware acceleration status isHardwareAccelerationEnabled: async function () { return await ipcRenderer.invoke("is-hardware-acceleration-enabled");