index.ts 2.8 KB

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