main.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict'
  2. import { app, BrowserWindow, ipcMain } from 'electron';
  3. import * as path from 'path';
  4. import { format as formatUrl } from 'url';
  5. import { exec } from 'child_process';
  6. app.allowRendererProcessReuse = false;
  7. const isDevelopment = process.env.NODE_ENV !== 'production';
  8. const platform = process.platform;
  9. let mainWindow = null, subWindow1 = null, serWindow1;
  10. let random = null;
  11. const version = app.getVersion();
  12. let win1 = null, win2 = null, about = null, esptools = null;
  13. let tmp = null, appIcon = null, menu = null, trayImg = null;
  14. // ------------ Keep only one instance of the app running ------------ //
  15. // const singleLock = app.requestSingleInstanceLock();
  16. // !singleLock ? app.quit() : '';
  17. const gotTheLock = app.requestSingleInstanceLock()
  18. if (!gotTheLock) {
  19. app.quit()
  20. } else {
  21. app.on('second-instance', (event, commandLine, workingDirectory) => {
  22. if (mainWindow) {
  23. if (mainWindow.isMinimized()) mainWindow.restore()
  24. mainWindow.focus()
  25. }
  26. })
  27. // Create myWindow, load the rest of the app, etc...
  28. app.whenReady().then(() => {
  29. })
  30. }
  31. // ------------ run auto launch & add ca cert in MacOS ------------ //
  32. if (platform === 'darwin') {
  33. app.dock.hide();
  34. const sudo = require('sudo-prompt');
  35. const AutoLaunch = require('auto-launch');
  36. const opts = {
  37. name: "CocoRoboXDeskTop",
  38. isHidden: false,
  39. mac: { useLaunchAgent: true }
  40. };
  41. autoLaunch(opts);
  42. exec(`"${path.join(__dirname, '..', 'verifyCert.bash')}"`, (err, stdout) => {
  43. if (!err && stdout.indexOf('No') > -1)
  44. sudo.exec(
  45. `security add-trusted-cert -d -r trustRoot -k "/Library/Keychains/System.keychain" "${path.join(__dirname, '..', 'ca.crt')}"`,
  46. { name: 'CocoRobo X Uploader' },
  47. err => console.log(err)
  48. );
  49. else if (err)
  50. console.error(err);
  51. });
  52. /**
  53. * Auto launch when computer boot.
  54. * @opts {object} Auto lanuch's options
  55. * @returns {void} void
  56. */
  57. function autoLaunch(opts) {
  58. const autoLaunch = new AutoLaunch(opts);
  59. autoLaunch
  60. .isEnabled()
  61. .then(enabled => enabled ? '' : autoLaunch.enable())
  62. .catch(err => console.error(err));
  63. return;
  64. }
  65. }
  66. /**
  67. * Reboot the app.
  68. * @returns {void} void
  69. */
  70. function reboot() {
  71. app.relaunch();
  72. app.exit();
  73. return;
  74. }
  75. /**
  76. * Get windows id & return a list.
  77. * @param {string} data Incoming message
  78. * @returns {Array<number>} An id list
  79. */
  80. function getWindowsId(data) {
  81. const result = [];
  82. if (data === "already") {
  83. const windows = BrowserWindow.getAllWindows();
  84. for (let i = windows.length - 1; i >= 0; i--)
  85. result.push(windows[i].id);
  86. platform === 'darwin' ? result.reverse() : '';
  87. }
  88. return result;
  89. }
  90. // ------------ IpcMain eventlisteners ------------ //
  91. ipcMain.on('tmp', (e, path) => tmp = path);
  92. ipcMain.on('version', (e, data) => e.returnValue = data === "already" ? version : '');
  93. ipcMain.on('relaunch', reboot);
  94. ipcMain.on('windowsid', (e, data) => e.returnValue = getWindowsId(data));
  95. // ------------ When app is ready ------------ //
  96. app.on('ready', () => {
  97. createSubWindow1();
  98. createSubWindow2();
  99. mainWindow = createMainWindow();
  100. });
  101. // when user acitvate the app, it's useful in mac only
  102. app.on('activate', () => {
  103. // on macOS it is common to re-create a window even after all windows have been closed
  104. mainWindow === null ? mainWindow = createMainWindow() : null;
  105. });
  106. function createMainWindow() {
  107. let window = new BrowserWindow({ autoHideMenuBar: true, show: false, webPreferences: { nodeIntegration: true }, width: 1280, height: 920, minWidth: 601, minHeight: 670 });
  108. window.loadURL(formatUrl({
  109. pathname: path.join(__dirname, '..', '..', 'cocoblockly-x/index.html'),
  110. protocol: 'file',
  111. slashes: true,
  112. search: "lang=zh-hans",
  113. }));
  114. // if (isDevelopment) {
  115. // window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`);
  116. // } else {
  117. // window.loadURL(formatUrl({
  118. // pathname: path.join(__dirname, 'cocoblockly-x/index.html'),
  119. // protocol: 'file',
  120. // slashes: true,
  121. // search: "lang=zh-hans",
  122. // }));
  123. // }
  124. window.once('ready-to-show', () => window.show());
  125. window.on('closed', () => { app.quit() });
  126. //window.webContents.on('devtools-opened', () => window.focus());
  127. //window.webContents.openDevTools({ mode: 'undocked' });
  128. return window;
  129. }
  130. function createSubWindow1() {
  131. let window = new BrowserWindow({ autoHideMenuBar: true, webPreferences: { nodeIntegration: true }, width: 0, height: 0, show: false });
  132. // window.webContents.openDevTools({ mode: "undocked" });
  133. if (isDevelopment) {
  134. window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}/index.html`);
  135. }
  136. else {
  137. window.loadURL(formatUrl({
  138. pathname: path.join(__dirname, 'index.html'),
  139. protocol: 'file',
  140. slashes: true
  141. }));
  142. }
  143. window.on('closed', () => window = null);
  144. //window.webContents.on('devtools-opened', () => setImmediate(() => window.focus()));
  145. //window.webContents.openDevTools({ mode: 'undocked' });
  146. return window;
  147. }
  148. function createSubWindow2() {
  149. let window = new BrowserWindow({ autoHideMenuBar: true, webPreferences: { nodeIntegration: true }, width: 0, height: 0, show: false });
  150. // window.webContents.openDevTools({ mode: "undocked" });
  151. if (isDevelopment) {
  152. window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}/serialport.html`);
  153. } else
  154. window.loadURL(formatUrl({
  155. pathname: path.join(__dirname, 'serialport.html'),
  156. protocol: 'file',
  157. slashes: true
  158. }));
  159. window.on('closed', () => window = null);
  160. //window.webContents.on('devtools-opened', () => setImmediate(() => window.focus()));
  161. //window.webContents.openDevTools({ mode: 'undocked' });
  162. return window;
  163. }
  164. // ------------ When quit the app ------------ //
  165. app.once('quit', () => {
  166. if (tmp) {
  167. let cmd = platform === 'win32' ? `rd /s /q "${tmp}"` : `rm -rf "${tmp}"`;
  168. exec(cmd);
  169. }
  170. });