index.ts 2.6 KB

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