index.ts 2.4 KB

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