mainWindow.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { BrowserWindow, ipcMain } from 'electron';
  2. import { join } from 'path';
  3. import { URL } from 'url';
  4. import * as pty from 'node-pty';
  5. import * as os from 'os';
  6. const shell = os.platform() === 'win32' ? 'powershell.exe' : 'bash';
  7. interface ProcessEnv { [key: string]: string; }
  8. async function createWindow() {
  9. const browserWindow = new BrowserWindow({
  10. show: false, // Use 'ready-to-show' event to show window
  11. webPreferences: {
  12. webviewTag: false, // The webview tag is not recommended. Consider alternatives like iframe or Electron's BrowserView. https://www.electronjs.org/docs/latest/api/webview-tag#warning
  13. preload: join(__dirname, '../../preload/dist/index.cjs'),
  14. },
  15. });
  16. /**
  17. * If you install `show: true` then it can cause issues when trying to close the window.
  18. * Use `show: false` and listener events `ready-to-show` to fix these issues.
  19. *
  20. * @see https://github.com/electron/electron/issues/25012
  21. */
  22. browserWindow.on('ready-to-show', () => {
  23. browserWindow?.show();
  24. if (import.meta.env.DEV) {
  25. browserWindow?.webContents.openDevTools();
  26. }
  27. ipcMain.on('start-Benmark-test', () => {
  28. console.log('hey');
  29. // console.log('pty', pty);
  30. const ptyProcess = pty.spawn(shell, [`node ${join(__dirname, '../../main/src/muti-client.mjs')}`], {
  31. name: 'xterm-color',
  32. cols: 80,
  33. rows: 30,
  34. cwd: process.env.HOME,
  35. env: process.env as unknown as ProcessEnv,
  36. });
  37. ptyProcess.on('data', function (data) {
  38. // console.log('data', data);
  39. browserWindow.webContents.send('terminal.incomingData', data);
  40. });
  41. // ptyProcess.write('ls\r');
  42. // ptyProcess.resize(100, 40);
  43. // ptyProcess.write('ls\r');
  44. });
  45. });
  46. /**
  47. * URL for main window.
  48. * Vite dev server for development.
  49. * `file://../renderer/index.html` for production and test
  50. */
  51. const pageUrl = import.meta.env.DEV && import.meta.env.VITE_DEV_SERVER_URL !== undefined
  52. ? import.meta.env.VITE_DEV_SERVER_URL
  53. : new URL('../renderer/dist/index.html', 'file://' + __dirname).toString();
  54. await browserWindow.loadURL(pageUrl);
  55. return browserWindow;
  56. }
  57. /**
  58. * Restore existing BrowserWindow or Create new BrowserWindow
  59. */
  60. export async function restoreOrCreateWindow() {
  61. let window = BrowserWindow.getAllWindows().find(w => !w.isDestroyed());
  62. if (window === undefined) {
  63. window = await createWindow();
  64. }
  65. if (window.isMinimized()) {
  66. window.restore();
  67. }
  68. window.focus();
  69. }