index.ts 2.4 KB

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