index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { app, shell, BrowserWindow, protocol, net } from 'electron';
  2. import { join, resolve } from 'path';
  3. import url from 'url';
  4. import { electronApp, optimizer, is } from '@electron-toolkit/utils';
  5. import icon from '../../resources/icon.png?asset';
  6. import useElectron from './useElectron';
  7. import useWinProcess from './useWinProcess';
  8. function createWindow(): void {
  9. // Create the browser window.
  10. const mainWindow = new BrowserWindow({
  11. width: is.dev ? 1428 : 1024,
  12. height: 700,
  13. minWidth: 1024,
  14. minHeight: 600,
  15. show: false,
  16. ...(process.platform === 'linux' ? { icon } : {}),
  17. webPreferences: {
  18. preload: join(__dirname, '../preload/index.js'),
  19. sandbox: false,
  20. webSecurity: false,
  21. },
  22. });
  23. mainWindow.on('ready-to-show', () => {
  24. mainWindow.show();
  25. });
  26. mainWindow.webContents.setWindowOpenHandler((details) => {
  27. shell.openExternal(details.url);
  28. return { action: 'deny' };
  29. });
  30. // HMR for renderer base on electron-vite cli.
  31. // Load the remote URL for development or the local html file for production.
  32. if (is.dev && process.env.ELECTRON_RENDERER_URL) {
  33. mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL);
  34. mainWindow.webContents.openDevTools();
  35. } else {
  36. mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
  37. }
  38. }
  39. // This method will be called when Electron has finished
  40. // initialization and is ready to create browser windows.
  41. // Some APIs can only be used after this event occurs.
  42. app.whenReady().then(() => {
  43. // Set app user model id for windows
  44. electronApp.setAppUserModelId('com.electron');
  45. // Default open or close DevTools by F12 in development
  46. // and ignore CommandOrControl + R in production.
  47. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  48. app.on('browser-window-created', (_, window) => {
  49. optimizer.watchWindowShortcuts(window);
  50. });
  51. // use electron
  52. useElectron();
  53. // use multiple window porcess
  54. useWinProcess();
  55. createWindow();
  56. // protocol
  57. protocol.handle('local', (request) => {
  58. const filePath = request.url.slice('local://'.length);
  59. return net.fetch(
  60. url.pathToFileURL(resolve(__dirname, filePath)).toString()
  61. );
  62. });
  63. app.on('activate', () => {
  64. // On macOS it's common to re-create a window in the app when the
  65. // dock icon is clicked and there are no other windows open.
  66. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  67. });
  68. });
  69. // Quit when all windows are closed, except on macOS. There, it's common
  70. // for applications and their menu bar to stay active until the user quits
  71. // explicitly with Cmd + Q.
  72. app.on('window-all-closed', () => {
  73. if (process.platform !== 'darwin') {
  74. app.quit();
  75. }
  76. });
  77. // In this file you can include the rest of your app"s specific main process
  78. // code. You can also put them in separate files and require them here.