apiElectron.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { ipcRenderer } from 'electron';
  2. function dialogSelectFile(
  3. config: Electron.OpenDialogOptions
  4. ): Promise<Electron.OpenDialogReturnValue> {
  5. return ipcRenderer.invoke('dialog:selectFile', config);
  6. }
  7. function dialogSaveFile(
  8. config: Electron.SaveDialogOptions
  9. ): Promise<Electron.SaveDialogReturnValue> {
  10. return ipcRenderer.invoke('dialog:saveFile', config);
  11. }
  12. // win prcess
  13. function startWinProcess(processCount: number, loadPageUrl: string) {
  14. return ipcRenderer.send('start-win-process', processCount, loadPageUrl);
  15. }
  16. function stopWinProcess() {
  17. return ipcRenderer.send('stop-win-process');
  18. }
  19. function closeProcessWindow(winId: number) {
  20. return ipcRenderer.send('close-process-window', winId);
  21. }
  22. function onStopRunning(callback: () => void) {
  23. // return ipcRenderer.on('stop-running', callback);
  24. return ipcRenderer.on('stop-running', () => callback());
  25. }
  26. // silence-authority
  27. function onSilenceAuthority(callback: (authKey: string) => void) {
  28. return ipcRenderer.on(
  29. 'silence-authority',
  30. (event: Electron.IpcRendererEvent, authKey: string) => callback(authKey)
  31. );
  32. }
  33. function checkSilenceAuthority(): Promise<string> {
  34. return ipcRenderer.invoke('check-silence-authority');
  35. }
  36. const electronApi = {
  37. dialogSelectFile,
  38. dialogSaveFile,
  39. // win prcess
  40. startWinProcess,
  41. stopWinProcess,
  42. closeProcessWindow,
  43. onStopRunning,
  44. // silence-authority
  45. onSilenceAuthority,
  46. checkSilenceAuthority,
  47. };
  48. export type ElectronApi = typeof electronApi;
  49. export default electronApi;