feat(electron): add file and directory selection dialogs; implement open path functionality

This commit is contained in:
Ivan committed 2026-04-13 18:26:53 -05:00
1 parent c77629dc10
commit bd56b884ba
3 files changed
+95

No files matched your search

+43
View File
@@ -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);
+43
View File
@@ -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);
+9
View File
@@ -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");