index.ts 2.8 KB

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