feat: preload demo

This commit is contained in:
gaoshuaixing
2025-01-04 21:00:21 +08:00
parent 361c040c2d
commit 7cb799ad8f
7 changed files with 124 additions and 11 deletions

View File

@@ -2,7 +2,7 @@
const { dialog } = require('electron');
const _ = require('lodash');
const { getMainWindow } = require('ee-core/electron/window');
const { getMainWindow } = require('ee-core/electron');
/**
* effect - demo

View File

@@ -10,7 +10,7 @@ const { logger } = require('ee-core/log');
const { getConfig } = require('ee-core/config');
const { frameworkService } = require('../service/framework');
const { sqlitedbService } = require('../service/database/sqlitedb');
const { autoUpdater } = require('../service/os/auto_updater');
const { autoUpdaterService } = require('../service/os/auto_updater');
/**
* framework - demo
@@ -246,7 +246,7 @@ class FrameworkController {
* 检查是否有新版本
*/
checkForUpdater() {
autoUpdater.checkUpdate();
autoUpdaterService.checkUpdate();
return;
}
@@ -254,7 +254,7 @@ class FrameworkController {
* 下载新版本
*/
downloadApp() {
autoUpdater.download();
autoUpdaterService.download();
return;
}

View File

@@ -2,9 +2,16 @@
** preload为预加载模块该文件将会在程序启动时加载 **
*************************************************/
const { trayService } = require('../service/os/tray');
const { securityService } = require('../service/os/security');
const { autoUpdaterService } = require('../service/os/auto_updater');
function preload() {
// 示例功能模块,可选择性使用和修改
console.log('preload/index.js');
console.log('preload functions');
trayService.create();
securityService.create();
autoUpdaterService.create();
}
/**

View File

@@ -3,13 +3,13 @@ const { autoUpdater } = require("electron-updater");
const { is } = require('ee-core/utils');
const { logger } = require('ee-core/log');
const { getConfig } = require('ee-core/config');
const { getMainWindow, setCloseAndQuit } = require('ee-core/electron/window');
const { getMainWindow, setCloseAndQuit } = require('ee-core/electron');
/**
* 自动升级
* @class
*/
class AutoUpdater {
class AutoUpdaterService {
/**
* 创建
@@ -157,7 +157,7 @@ class AutoUpdater {
}
}
AutoUpdater.toString = () => '[class AutoUpdater]';
AutoUpdaterService.toString = () => '[class AutoUpdaterService]';
module.exports = {
autoUpdater: new AutoUpdater()
autoUpdaterService: new AutoUpdaterService()
};

View File

@@ -0,0 +1,33 @@
'use strict';
const { logger } = require('ee-core/log');
const { app: electronApp } = require('electron');
/**
* 安全
* @class
*/
class SecurityService {
/**
* 创建
*/
create () {
logger.info('[security] load');
const runWithDebug = process.argv.find(function(e){
let isHasDebug = e.includes("--inspect") || e.includes("--inspect-brk") || e.includes("--remote-debugging-port");
return isHasDebug;
})
// 不允许远程调试
if (runWithDebug) {
logger.error('[error] Remote debugging is not allowed, runWithDebug:', runWithDebug);
electronApp.quit();
}
}
}
SecurityService.toString = () => '[class SecurityService]';
module.exports = {
securityService: new SecurityService()
};

View File

@@ -0,0 +1,74 @@
const { Tray, Menu } = require('electron');
const path = require('path');
const { isDev, getBaseDir } = require('ee-core/ps');
const { logger } = require('ee-core/log');
const { app: electronApp } = require('electron');
const { getMainWindow, getCloseAndQuit, setCloseAndQuit } = require('ee-core/electron');
const { getConfig } = require('ee-core/config');
/**
* 托盘
* @class
*/
class TrayService {
constructor() {
this.tray = null;
}
/**
* 创建托盘
*/
create () {
// todo 开发环境,代码热更新开启时,会导致托盘中有残影
if (isDev()) return;
logger.info('[addon:tray] load');
const cfg = getConfig().customize.tray;
const mainWindow = getMainWindow();
// tray icon
const iconPath = path.join(getBaseDir(), cfg.icon);
// 托盘菜单功能列表
const trayMenuTemplate = [
{
label: '显示',
click: function () {
mainWindow.show();
}
},
{
label: '退出',
click: function () {
electronApp.quit();
}
}
]
// 设置一个标识,点击关闭,最小化到托盘
setCloseAndQuit(false);
mainWindow.on('close', (event) => {
if (getCloseAndQuit()) {
return;
}
mainWindow.hide();
event.preventDefault();
});
// 实例化托盘
this.tray = new Tray(iconPath);
this.tray.setToolTip(cfg.title);
const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
this.tray.setContextMenu(contextMenu);
// 左键单击的时候能够显示主窗口
this.tray.on('click', () => {
mainWindow.show()
})
}
}
TrayService.toString = () => '[class TrayService]';
module.exports = {
trayService: new TrayService()
};

View File

@@ -1,9 +1,8 @@
'use strict';
const path = require('path');
const { app: electronApp } = require('electron');
const { BrowserWindow, Notification } = require('electron');
const { getMainWindow } = require('ee-core/electron/window');
const { getMainWindow } = require('ee-core/electron');
const { isProd, getBaseDir } = require('ee-core/ps');
/**