1
0
mirror of https://gitee.com/sui-feng-cb/AzurLaneAutoScript1 synced 2026-03-14 21:07:22 +08:00
Files
AzurLaneAutoScript/webapp/packages/main/src/index.ts

146 lines
3.8 KiB
TypeScript
Raw Normal View History

2021-10-08 18:10:41 +08:00
import {app, BrowserWindow, ipcMain, globalShortcut} from 'electron';
import {URL} from 'url';
2021-10-08 18:10:41 +08:00
import {PyShell} from '/@/pyshell';
import {webuiArgs, webuiPath} from '/@/config';
const path = require('path');
const isSingleInstance = app.requestSingleInstanceLock();
if (!isSingleInstance) {
app.quit();
process.exit(0);
}
app.disableHardwareAcceleration();
// Install "Vue.js devtools"
if (import.meta.env.MODE === 'development') {
app.whenReady()
.then(() => import('electron-devtools-installer'))
.then(({default: installExtension, VUEJS3_DEVTOOLS}) => installExtension(VUEJS3_DEVTOOLS, {
loadExtensionOptions: {
allowFileAccess: true,
},
}))
.catch(e => console.error('Failed install extension:', e));
}
2021-10-08 18:10:41 +08:00
/**
* Load deploy settings and start Alas web server.
*/
let alas = new PyShell(webuiPath, webuiArgs);
alas.end(function (err: string) {
// if (err) throw err;
});
let mainWindow: BrowserWindow | null = null;
const createWindow = async () => {
mainWindow = new BrowserWindow({
2021-10-08 18:10:41 +08:00
width: 1280,
height: 880,
show: false, // Use 'ready-to-show' event to show window
2021-10-08 18:10:41 +08:00
frame: false,
icon: path.join(__dirname, './buildResources/icon.ico'),
webPreferences: {
2021-10-08 18:10:41 +08:00
nodeIntegration: true,
contextIsolation: false, // Spectron tests can't work with contextIsolation: true
nativeWindowOpen: true,
2021-10-08 18:10:41 +08:00
// preload: join(__dirname, '../../preload/dist/index.cjs'),
},
});
/**
* If you install `show: true` then it can cause issues when trying to close the window.
* Use `show: false` and listener events `ready-to-show` to fix these issues.
*
* @see https://github.com/electron/electron/issues/25012
*/
mainWindow.on('ready-to-show', () => {
mainWindow?.show();
2021-10-08 18:10:41 +08:00
// Hide menu
const {Menu} = require('electron');
Menu.setApplicationMenu(null);
if (import.meta.env.MODE === 'development') {
mainWindow?.webContents.openDevTools();
}
});
2021-10-08 18:10:41 +08:00
// Dev tools
globalShortcut.register('Ctrl+Shift+I', function () {
if (mainWindow?.webContents.isDevToolsOpened()) {
mainWindow?.webContents.closeDevTools()
} else {
mainWindow?.webContents.openDevTools()
}
});
2021-10-23 14:50:44 +08:00
// Refresh
globalShortcut.register('Ctrl+R', function () {
mainWindow?.reload()
});
globalShortcut.register('Ctrl+Shift+R', function () {
mainWindow?.reload()
});
2021-10-08 18:10:41 +08:00
// Minimize, maximize, close window.
ipcMain.on('window-min', function () {
mainWindow?.minimize();
});
ipcMain.on('window-max', function () {
if (mainWindow?.isMaximized()) {
mainWindow?.restore();
} else {
mainWindow?.maximize();
}
});
ipcMain.on('window-close', function () {
alas.kill();
setTimeout(() => mainWindow?.close(), 500); // Wait taskkill to finish
});
/**
* URL for main window.
* Vite dev server for development.
* `file://../renderer/index.html` for production and test
*/
const pageUrl = import.meta.env.MODE === 'development' && import.meta.env.VITE_DEV_SERVER_URL !== undefined
? import.meta.env.VITE_DEV_SERVER_URL
: new URL('../renderer/dist/index.html', 'file://' + __dirname).toString();
await mainWindow.loadURL(pageUrl);
};
app.on('second-instance', () => {
// Someone tried to run a second instance, we should focus our window.
if (mainWindow) {
if (mainWindow.isMinimized()) mainWindow.restore();
mainWindow.focus();
}
});
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
}
});
app.whenReady()
.then(createWindow)
.catch((e) => console.error('Failed create window:', e));
// Auto-updates
if (import.meta.env.PROD) {
app.whenReady()
.then(() => import('electron-updater'))
.then(({autoUpdater}) => autoUpdater.checkForUpdatesAndNotify())
.catch((e) => console.error('Failed check updates:', e));
}