123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 'use strict'
- import { app, BrowserWindow, ipcMain } from 'electron';
- import * as path from 'path';
- import { format as formatUrl } from 'url';
- import { exec } from 'child_process';
- app.allowRendererProcessReuse = false;
- const isDevelopment = process.env.NODE_ENV !== 'production';
- const platform = process.platform;
- let mainWindow = null, subWindow1 = null, serWindow1;
- let random = null;
- const version = app.getVersion();
- let win1 = null, win2 = null, about = null, esptools = null;
- let tmp = null, appIcon = null, menu = null, trayImg = null;
- // ------------ Keep only one instance of the app running ------------ //
- // const singleLock = app.requestSingleInstanceLock();
- // !singleLock ? app.quit() : '';
- const gotTheLock = app.requestSingleInstanceLock()
- if (!gotTheLock) {
- app.quit()
- } else {
- app.on('second-instance', (event, commandLine, workingDirectory) => {
- if (mainWindow) {
- if (mainWindow.isMinimized()) mainWindow.restore()
- mainWindow.focus()
- }
- })
- // Create myWindow, load the rest of the app, etc...
- app.whenReady().then(() => {
- })
- }
- // ------------ run auto launch & add ca cert in MacOS ------------ //
- if (platform === 'darwin') {
- app.dock.hide();
- const sudo = require('sudo-prompt');
- const AutoLaunch = require('auto-launch');
- const opts = {
- name: "XGOEdu",
- isHidden: false,
- mac: { useLaunchAgent: true }
- };
- autoLaunch(opts);
- exec(`"${path.join(__dirname, '..', 'verifyCert.bash')}"`, (err, stdout) => {
- if (!err && stdout.indexOf('No') > -1)
- sudo.exec(
- `security add-trusted-cert -d -r trustRoot -k "/Library/Keychains/System.keychain" "${path.join(__dirname, '..', 'ca.crt')}"`,
- { name: 'CocoRobo X Uploader' },
- err => console.log(err)
- );
- else if (err)
- console.error(err);
- });
- /**
- * Auto launch when computer boot.
- * @opts {object} Auto lanuch's options
- * @returns {void} void
- */
- function autoLaunch(opts) {
- const autoLaunch = new AutoLaunch(opts);
- autoLaunch
- .isEnabled()
- .then(enabled => enabled ? '' : autoLaunch.enable())
- .catch(err => console.error(err));
- return;
- }
- }
- /**
- * Reboot the app.
- * @returns {void} void
- */
- function reboot() {
- app.relaunch();
- app.exit();
- return;
- }
- /**
- * Get windows id & return a list.
- * @param {string} data Incoming message
- * @returns {Array<number>} An id list
- */
- function getWindowsId(data) {
- const result = [];
- if (data === "already") {
- const windows = BrowserWindow.getAllWindows();
- for (let i = windows.length - 1; i >= 0; i--)
- result.push(windows[i].id);
- platform === 'darwin' ? result.reverse() : '';
- }
- return result;
- }
- // ------------ IpcMain eventlisteners ------------ //
- ipcMain.on('tmp', (e, path) => tmp = path);
- ipcMain.on('version', (e, data) => e.returnValue = data === "already" ? version : '');
- ipcMain.on('relaunch', reboot);
- ipcMain.on('windowsid', (e, data) => e.returnValue = getWindowsId(data));
- // ------------ When app is ready ------------ //
- app.on('ready', () => {
- createSubWindow1();
- createSubWindow2();
- mainWindow = createMainWindow();
- });
- // when user acitvate the app, it's useful in mac only
- app.on('activate', () => {
- // on macOS it is common to re-create a window even after all windows have been closed
- mainWindow === null ? mainWindow = createMainWindow() : null;
- });
- function createMainWindow() {
- let window = new BrowserWindow({ autoHideMenuBar: true, show: false, webPreferences: { nodeIntegration: true }, width: 1280, height: 920, minWidth: 601, minHeight: 670 });
- window.loadURL(formatUrl({
- pathname: path.join(__dirname, '..', '..', 'cocoblockly-x/index.html'),
- protocol: 'file',
- slashes: true,
- search: "lang=zh-hans",
- }));
- // if (isDevelopment) {
- // window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}`);
- // } else {
- // window.loadURL(formatUrl({
- // pathname: path.join(__dirname, 'cocoblockly-x/index.html'),
- // protocol: 'file',
- // slashes: true,
- // search: "lang=zh-hans",
- // }));
- // }
- window.once('ready-to-show', () => window.show());
- window.on('closed', () => { app.quit() });
- //window.webContents.on('devtools-opened', () => window.focus());
- window.webContents.openDevTools({ mode: 'undocked' });
- return window;
- }
- function createSubWindow1() {
- let window = new BrowserWindow({ autoHideMenuBar: true, webPreferences: { nodeIntegration: true }, width: 0, height: 0, show: false });
- // window.webContents.openDevTools({ mode: "undocked" });
- if (isDevelopment) {
- window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}/index.html`);
- }
- else {
- window.loadURL(formatUrl({
- pathname: path.join(__dirname, 'index.html'),
- protocol: 'file',
- slashes: true
- }));
- }
- window.on('closed', () => window = null);
- //window.webContents.on('devtools-opened', () => setImmediate(() => window.focus()));
- //window.webContents.openDevTools({ mode: 'undocked' });
- return window;
- }
- function createSubWindow2() {
- let window = new BrowserWindow({ autoHideMenuBar: true, webPreferences: { nodeIntegration: true }, width: 0, height: 0, show: false });
- // window.webContents.openDevTools({ mode: "undocked" });
- if (isDevelopment) {
- window.loadURL(`http://localhost:${process.env.ELECTRON_WEBPACK_WDS_PORT}/serialport.html`);
- } else
- window.loadURL(formatUrl({
- pathname: path.join(__dirname, 'serialport.html'),
- protocol: 'file',
- slashes: true
- }));
- window.on('closed', () => window = null);
- //window.webContents.on('devtools-opened', () => setImmediate(() => window.focus()));
- //window.webContents.openDevTools({ mode: 'undocked' });
- return window;
- }
- // ------------ When quit the app ------------ //
- app.once('quit', () => {
- if (tmp) {
- let cmd = platform === 'win32' ? `rd /s /q "${tmp}"` : `rm -rf "${tmp}"`;
- exec(cmd);
- }
- });
|