94 lines
2.9 KiB
TypeScript
94 lines
2.9 KiB
TypeScript
import { app as electronApp, screen } from 'electron';
|
|
import { logger } from 'ee-core/log';
|
|
import { getConfig } from 'ee-core/config';
|
|
import { getMainWindow } from 'ee-core/electron';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
function writeRuntimeLog(message: string): void {
|
|
try {
|
|
const baseDir = electronApp.isPackaged ? path.dirname(process.execPath) : process.cwd();
|
|
const logFile = path.join(baseDir, 'runtime.log');
|
|
const line = `[${new Date().toISOString()}] ${message}\n`;
|
|
fs.appendFileSync(logFile, line, 'utf8');
|
|
} catch (error) {
|
|
console.error('[runtime-log] write failed:', error);
|
|
}
|
|
}
|
|
|
|
class Lifecycle {
|
|
/**
|
|
* Core app has been loaded
|
|
*/
|
|
async ready(): Promise<void> {
|
|
logger.info('[lifecycle] ready');
|
|
}
|
|
|
|
/**
|
|
* Electron app is ready
|
|
*/
|
|
async electronAppReady(): Promise<void> {
|
|
logger.info('[lifecycle] electron-app-ready');
|
|
|
|
// When double clicking the icon, display the opened window
|
|
electronApp.on('second-instance', () => {
|
|
const win = getMainWindow();
|
|
if (win.isMinimized()) {
|
|
win.restore();
|
|
}
|
|
win.show();
|
|
win.focus();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Main window has been loaded
|
|
*/
|
|
async windowReady(): Promise<void> {
|
|
logger.info('[lifecycle] window-ready');
|
|
|
|
const win = getMainWindow();
|
|
|
|
win.webContents.on('did-finish-load', () => {
|
|
logger.info('[window] did-finish-load');
|
|
writeRuntimeLog('window did-finish-load');
|
|
});
|
|
|
|
win.webContents.on('did-fail-load', (_event, errorCode, errorDescription, validatedURL) => {
|
|
logger.error('[window] did-fail-load errorCode:', errorCode, 'errorDescription:', errorDescription, 'url:', validatedURL);
|
|
writeRuntimeLog(`window did-fail-load errorCode=${errorCode} errorDescription=${errorDescription} url=${validatedURL}`);
|
|
});
|
|
|
|
// The window is centered and scaled proportionally
|
|
// Obtain the size information of the main screen, calculate the width and height of the window as a percentage of the screen,
|
|
// and calculate the coordinates of the upper left corner when the window is centered
|
|
const mainScreen = screen.getPrimaryDisplay();
|
|
const { width, height } = mainScreen.workAreaSize;
|
|
const windowWidth = Math.floor(width * 0.6);
|
|
const windowHeight = Math.floor(height * 0.8);
|
|
const x = Math.floor((width - windowWidth) / 2);
|
|
const y = Math.floor((height - windowHeight) / 2);
|
|
win.setBounds({ x, y, width: windowWidth, height: windowHeight });
|
|
|
|
// Delay loading, no white screen
|
|
const config = getConfig();
|
|
const { windowsOption } = config;
|
|
if (windowsOption?.show == false) {
|
|
win.once('ready-to-show', () => {
|
|
win.show();
|
|
win.focus();
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Before app close
|
|
*/
|
|
async beforeClose(): Promise<void> {
|
|
logger.info('[lifecycle] before-close');
|
|
}
|
|
}
|
|
Lifecycle.toString = () => '[class Lifecycle]';
|
|
|
|
export { Lifecycle };
|