Skip to content

Commit c5b5964

Browse files
Support using the native file open dialog
Required for Electron apps on macOS. httptoolkit/httptoolkit#329
1 parent 7e5a86b commit c5b5964

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import * as path from 'path';
1111
import { promisify } from 'util';
1212
import * as querystring from 'querystring';
1313
import { URL } from 'url';
14-
import { app, BrowserWindow, shell, Menu, dialog, session } from 'electron';
14+
import { app, BrowserWindow, shell, Menu, dialog, session, ipcMain } from 'electron';
1515
import * as uuid from 'uuid/v4';
1616
import * as yargs from 'yargs';
1717
import * as semver from 'semver';
@@ -77,6 +77,7 @@ const createWindow = (logStream: WriteStream) => {
7777
height: windowState.height,
7878

7979
webPreferences: {
80+
preload: path.join(__dirname, 'preload.js'),
8081
contextIsolation: true,
8182
nodeIntegration: false
8283
},
@@ -583,4 +584,12 @@ if (!amMainInstance) {
583584
app.quit();
584585
}
585586
});
586-
}
587+
}
588+
589+
ipcMain.handle(
590+
'open-file',
591+
() =>
592+
dialog.showOpenDialogSync({
593+
properties: ['openFile', 'treatPackageAsDirectory'],
594+
})?.[0]
595+
);

src/preload.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {contextBridge, ipcRenderer} from 'electron';
2+
3+
contextBridge.exposeInMainWorld('nativeFile', {
4+
open: () => ipcRenderer.invoke('open-file'),
5+
});

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"include": [
2828
"./src/index.ts",
29+
"./src/preload.ts",
2930
"./custom-typings/**/*.d.ts"
3031
]
31-
}
32+
}

0 commit comments

Comments
 (0)