index.ts 2.7 KB

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