index.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { app, BrowserWindow, screen } from 'electron';
  2. import is_dev from 'electron-is-dev';
  3. import { join } from 'path';
  4. let mainWindow: BrowserWindow | null = null;
  5. class createWin {
  6. constructor() {
  7. const displayWorkAreaSize = screen.getAllDisplays()[0].workArea;
  8. mainWindow = new BrowserWindow({
  9. width: parseInt(`${displayWorkAreaSize.width * 0.85}`, 10),
  10. height: parseInt(`${displayWorkAreaSize.height * 0.85}`, 10),
  11. movable: true,
  12. // frame: false,
  13. show: false,
  14. center: true,
  15. resizable: true,
  16. // transparent: true,
  17. titleBarStyle: 'default',
  18. webPreferences: {
  19. devTools: true,
  20. contextIsolation: false,
  21. nodeIntegration: true,
  22. enableRemoteModule: true,
  23. },
  24. backgroundColor: '#fff',
  25. });
  26. const URL = is_dev
  27. ? `https://localhost:${process.env.PORT}` // vite 启动的服务器地址
  28. : `file://${join(__dirname, '../index.html')}`; // vite 构建后的静态文件地址
  29. mainWindow.loadURL(URL);
  30. mainWindow.on('ready-to-show', () => {
  31. mainWindow.show();
  32. });
  33. }
  34. }
  35. app.whenReady().then(() => new createWin());
  36. const isFirstInstance = app.requestSingleInstanceLock();
  37. if (!isFirstInstance) {
  38. app.quit();
  39. } else {
  40. app.on('second-instance', () => {
  41. if (mainWindow) {
  42. mainWindow.focus();
  43. }
  44. });
  45. }
  46. app.on('window-all-closed', () => {
  47. if (process.platform !== 'darwin') {
  48. app.quit();
  49. }
  50. });
  51. app.on('activate', () => {
  52. if (BrowserWindow.getAllWindows().length === 0) {
  53. new createWin();
  54. }
  55. });