index.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { app, shell, BrowserWindow, protocol, net, ipcMain } 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. import useWinProcess from './useWinProcess';
  8. import log from './logger';
  9. const ULR_SCHEME_PROTOCOL = 'trd://';
  10. let mainWindow: BrowserWindow | null = null;
  11. function main() {
  12. const gotTheLock = app.requestSingleInstanceLock();
  13. log.info(`gotTheLock:${gotTheLock}`);
  14. if (!gotTheLock) {
  15. app.quit();
  16. return;
  17. }
  18. function createWindow(): void {
  19. // Create the browser window.
  20. mainWindow = new BrowserWindow({
  21. width: is.dev ? 1428 : 1024,
  22. height: 700,
  23. minWidth: 1024,
  24. minHeight: 600,
  25. show: false,
  26. ...(process.platform === 'linux' ? { icon } : {}),
  27. webPreferences: {
  28. preload: join(__dirname, '../preload/index.js'),
  29. sandbox: false,
  30. webSecurity: false,
  31. },
  32. });
  33. if (!mainWindow) return;
  34. mainWindow.on('ready-to-show', () => {
  35. mainWindow?.show();
  36. });
  37. mainWindow.webContents.setWindowOpenHandler((details) => {
  38. shell.openExternal(details.url);
  39. return { action: 'deny' };
  40. });
  41. // HMR for renderer base on electron-vite cli.
  42. // Load the remote URL for development or the local html file for production.
  43. if (is.dev && process.env.ELECTRON_RENDERER_URL) {
  44. mainWindow.loadURL(process.env.ELECTRON_RENDERER_URL);
  45. mainWindow.webContents.openDevTools();
  46. } else {
  47. mainWindow.loadFile(join(__dirname, '../renderer/index.html'));
  48. }
  49. }
  50. // This method will be called when Electron has finished
  51. // initialization and is ready to create browser windows.
  52. // Some APIs can only be used after this event occurs.
  53. app.whenReady().then(() => {
  54. // Set app user model id for windows
  55. electronApp.setAppUserModelId('com.electron');
  56. // Default open or close DevTools by F12 in development
  57. // and ignore CommandOrControl + R in production.
  58. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  59. app.on('browser-window-created', (_, window) => {
  60. optimizer.watchWindowShortcuts(window);
  61. });
  62. // use electron
  63. useElectron();
  64. // use multiple window porcess
  65. useWinProcess();
  66. // 渲染进程加载好之后,会发起check-silence-authority
  67. ipcMain.handleOnce('check-silence-authority', () => {
  68. const argv = process.argv;
  69. log.info(`check-silence-authority process.argv:${JSON.stringify(argv)}`);
  70. const urlScheme = argv.find((arg) => arg.startsWith(ULR_SCHEME_PROTOCOL));
  71. return urlScheme || '';
  72. });
  73. createWindow();
  74. // protocol
  75. protocol.handle('local', (request) => {
  76. const filePath = request.url.slice('local://'.length);
  77. return net.fetch(
  78. url.pathToFileURL(resolve(__dirname, filePath)).toString()
  79. );
  80. });
  81. app.on('activate', () => {
  82. // On macOS it's common to re-create a window in the app when the
  83. // dock icon is clicked and there are no other windows open.
  84. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  85. });
  86. });
  87. // Quit when all windows are closed, except on macOS. There, it's common
  88. // for applications and their menu bar to stay active until the user quits
  89. // explicitly with Cmd + Q.
  90. app.on('window-all-closed', () => {
  91. if (process.platform !== 'darwin') {
  92. app.quit();
  93. }
  94. });
  95. // In this file you can include the rest of your app"s specific main process
  96. // code. You can also put them in separate files and require them here.
  97. // simple instance
  98. app.on('second-instance', (event, argv) => {
  99. silenceAuthorityHandle(argv);
  100. mainWindow?.show();
  101. });
  102. // 免登录流程
  103. function silenceAuthorityHandle(argv: string[]) {
  104. log.info(`second-instance process.argv:${JSON.stringify(argv)}`);
  105. const urlScheme = argv.find((arg) => arg.startsWith(ULR_SCHEME_PROTOCOL));
  106. if (urlScheme && mainWindow) {
  107. mainWindow.webContents.send('silence-authority', urlScheme);
  108. }
  109. }
  110. }
  111. main();