全部模块化

This commit is contained in:
gsx
2023-05-28 11:31:56 +08:00
parent f0a76ebf63
commit 6011bcb47e
11 changed files with 95 additions and 107 deletions

View File

@@ -3,7 +3,7 @@ const { autoUpdater } = require("electron-updater");
const is = require('ee-core/utils/is'); const is = require('ee-core/utils/is');
const Log = require('ee-core/log'); const Log = require('ee-core/log');
const Conf = require('ee-core/config'); const Conf = require('ee-core/config');
const Electron = require('ee-core/electron'); const CoreWindow = require('ee-core/electron/window');
/** /**
* 自动升级插件 * 自动升级插件
@@ -11,8 +11,7 @@ const Electron = require('ee-core/electron');
*/ */
class AutoUpdaterAddon { class AutoUpdaterAddon {
constructor(app) { constructor() {
this.app = app;
} }
/** /**
@@ -20,8 +19,6 @@ class AutoUpdaterAddon {
*/ */
create () { create () {
Log.info('[addon:autoUpdater] load'); Log.info('[addon:autoUpdater] load');
// const app = this.app;
const cfg = Conf.getValue('addons.autoUpdater'); const cfg = Conf.getValue('addons.autoUpdater');
if ((is.windows() && cfg.windows) if ((is.windows() && cfg.windows)
|| (is.macOS() && cfg.macOS) || (is.macOS() && cfg.macOS)
@@ -131,7 +128,8 @@ class AutoUpdaterAddon {
sendStatusToWindow(content = {}) { sendStatusToWindow(content = {}) {
const textJson = JSON.stringify(content); const textJson = JSON.stringify(content);
const channel = 'app.updater'; const channel = 'app.updater';
Electron.mainWindow.webContents.send(channel, textJson); const win = CoreWindow.getMainWindow();
win.webContents.send(channel, textJson);
} }
/** /**

View File

@@ -8,8 +8,7 @@ const Conf = require('ee-core/config');
*/ */
class AwakenAddon { class AwakenAddon {
constructor(app) { constructor() {
this.app = app;
this.protocol = ''; this.protocol = '';
} }
@@ -24,17 +23,16 @@ class AwakenAddon {
electronApp.setAsDefaultProtocolClient(this.protocol); electronApp.setAsDefaultProtocolClient(this.protocol);
const self = this;
this.handleArgv(process.argv); this.handleArgv(process.argv);
electronApp.on('second-instance', (event, argv) => { electronApp.on('second-instance', (event, argv) => {
if (process.platform === 'win32') { if (process.platform === 'win32') {
self.handleArgv(argv) this.handleArgv(argv)
} }
}) })
// 仅用于macOS // 仅用于macOS
electronApp.on('open-url', (event, urlStr) => { electronApp.on('open-url', (event, urlStr) => {
self.handleUrl(urlStr) this.handleUrl(urlStr)
}) })
} }

View File

@@ -5,13 +5,12 @@ const path = require('path');
const Log = require('ee-core/log'); const Log = require('ee-core/log');
/** /**
* 安全插件 * 扩展插件 electron自身对该功能并不完全支持官方也不建议使用
* @class * @class
*/ */
class ChromeExtensionAddon { class ChromeExtensionAddon {
constructor(app) { constructor() {
this.app = app;
} }
/** /**

View File

@@ -10,8 +10,7 @@ const GetPort = require('ee-core/utils/get-port');
*/ */
class JavaServerAddon { class JavaServerAddon {
constructor(app) { constructor() {
this.app = app;
this.cfg; this.cfg;
this.javaServer; this.javaServer;
} }

View File

@@ -1,4 +1,5 @@
const Log = require('ee-core/log'); const Log = require('ee-core/log');
const EE = require('ee-core/ee');
/** /**
* 安全插件 * 安全插件
@@ -6,8 +7,7 @@ const Log = require('ee-core/log');
*/ */
class SecurityAddon { class SecurityAddon {
constructor(app) { constructor() {
this.app = app;
} }
/** /**
@@ -15,6 +15,7 @@ class SecurityAddon {
*/ */
create () { create () {
Log.info('[addon:security] load'); Log.info('[addon:security] load');
const { CoreApp } = EE;
const runWithDebug = process.argv.find(function(e){ const runWithDebug = process.argv.find(function(e){
let isHasDebug = e.includes("--inspect") || e.includes("--inspect-brk") || e.includes("--remote-debugging-port"); let isHasDebug = e.includes("--inspect") || e.includes("--inspect-brk") || e.includes("--remote-debugging-port");
return isHasDebug; return isHasDebug;
@@ -23,7 +24,7 @@ class SecurityAddon {
// 不允许远程调试 // 不允许远程调试
if (runWithDebug) { if (runWithDebug) {
Log.error('[error] Remote debugging is not allowed, runWithDebug:', runWithDebug); Log.error('[error] Remote debugging is not allowed, runWithDebug:', runWithDebug);
this.app.appQuit(); CoreApp.appQuit();
} }
} }
} }

View File

@@ -1,9 +1,11 @@
const { Tray, Menu, shell } = require('electron'); const { Tray, Menu } = require('electron');
const path = require('path'); const path = require('path');
const Ps = require('ee-core/ps'); const Ps = require('ee-core/ps');
const Log = require('ee-core/log'); const Log = require('ee-core/log');
const Electron = require('ee-core/electron'); const Electron = require('ee-core/electron');
const CoreWindow = require('ee-core/electron/window');
const Conf = require('ee-core/config'); const Conf = require('ee-core/config');
const EE = require('ee-core/ee');
/** /**
* 托盘插件 * 托盘插件
@@ -11,8 +13,7 @@ const Conf = require('ee-core/config');
*/ */
class TrayAddon { class TrayAddon {
constructor(app) { constructor() {
this.app = app;
this.tray = null; this.tray = null;
} }
@@ -24,10 +25,9 @@ class TrayAddon {
if (Ps.isDev() && Ps.isHotReload()) return; if (Ps.isDev() && Ps.isHotReload()) return;
Log.info('[addon:tray] load'); Log.info('[addon:tray] load');
const { CoreApp } = EE;
const app = this.app;
const cfg = Conf.getValue('addons.tray'); const cfg = Conf.getValue('addons.tray');
const { mainWindow } = Electron; const mainWindow = CoreWindow.getMainWindow();
// 托盘图标 // 托盘图标
let iconPath = path.join(Ps.getHomeDir(), cfg.icon); let iconPath = path.join(Ps.getHomeDir(), cfg.icon);
@@ -43,15 +43,14 @@ class TrayAddon {
{ {
label: '退出', label: '退出',
click: function () { click: function () {
app.appQuit(); CoreApp.appQuit();
} }
} }
] ]
// 点击关闭,最小化到托盘 // 点击关闭,最小化到托盘
mainWindow.on('close', (event) => { mainWindow.on('close', (event) => {
const extraObj = Electron.extra; if (Electron.extra.closeWindow == true) {
if (extraObj.closeWindow == true) {
return; return;
} }
mainWindow.hide(); mainWindow.hide();
@@ -63,12 +62,6 @@ class TrayAddon {
this.tray.setToolTip(cfg.title); this.tray.setToolTip(cfg.title);
const contextMenu = Menu.buildFromTemplate(trayMenuTemplate); const contextMenu = Menu.buildFromTemplate(trayMenuTemplate);
this.tray.setContextMenu(contextMenu); this.tray.setContextMenu(contextMenu);
// 使用默认浏览器打开链接
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
shell.openExternal(url);
return { action: 'deny' }
})
} }
} }

View File

@@ -2,6 +2,7 @@
const { Controller } = require('ee-core'); const { Controller } = require('ee-core');
const Log = require('ee-core/log'); const Log = require('ee-core/log');
const Services = require('ee-core/services');
/** /**
* example * example
@@ -24,12 +25,10 @@ class ExampleController extends Controller {
* test * test
*/ */
async test () { async test () {
const result = await this.service.example.test('electron'); const result = await Services.get('example').test('electron');
Log.info('service result:', result);
let tmpDir = Ps.getLogDir(); return 'hello electron-egg';
Log.info('tmpDir:', tmpDir);
return result;
} }
} }

49
electron/index.js Normal file
View File

@@ -0,0 +1,49 @@
const { Application } = require('ee-core');
class Index extends Application {
constructor() {
super();
// this === eeApp;
}
/**
* core app have been loaded
*/
async ready () {
// do some things
}
/**
* electron app ready
*/
async electronAppReady () {
// do some things
}
/**
* main window have been loaded
*/
async windowReady () {
// do some things
// 延迟加载,无白屏
const winOpt = this.config.windowsOption;
if (winOpt.show == false) {
const win = this.electron.mainWindow;
win.once('ready-to-show', () => {
win.show();
})
}
}
/**
* before app close
*/
async beforeClose () {
// do some things
}
}
Index.toString = () => '[class Index]';
module.exports = Index;

View File

@@ -1,20 +1,16 @@
/************************************************* /*************************************************
** preload为预加载模块该文件将会在程序启动时加载 ** ** preload为预加载模块该文件将会在程序启动时加载 **
*************************************************/ *************************************************/
const Addon = require('ee-core/addon');
/** /**
* 预加载模块入口 * 预加载模块入口
*/ */
module.exports = async (app) => { module.exports = async () => {
//已实现的功能模块,可选择性使用和修改 // 示例功能模块,可选择性使用和修改
const trayAddon = app.addon.tray; Addon.get('tray').create();
const securityAddon = app.addon.security; Addon.get('security').create();
const awakenAddon = app.addon.awaken; Addon.get('awaken').create();
const autoUpdaterAddon = app.addon.autoUpdater; Addon.get('autoUpdater').create();
}
trayAddon.create();
securityAddon.create();
awakenAddon.create();
autoUpdaterAddon.create();
}

52
main.js
View File

@@ -1,50 +1,2 @@
const { Application } = require('ee-core'); const { ElectronEgg } = require('ee-core');
const EE = require('ee-core/ee'); new ElectronEgg();
class Main extends Application {
constructor() {
super();
// this === eeApp;
}
/**
* core app have been loaded
*/
async ready () {
// do some things
}
/**
* electron app ready
*/
async electronAppReady () {
// do some things
}
/**
* main window have been loaded
*/
async windowReady () {
// do some things
// 延迟加载,无白屏
const winOpt = this.config.windowsOption;
if (winOpt.show == false) {
const win = this.electron.mainWindow;
win.once('ready-to-show', () => {
win.show();
})
}
}
/**
* before app close
*/
async beforeClose () {
// do some things
}
}
// Instantiate an app object
EE.app = new Main();

View File

@@ -1,6 +1,6 @@
{ {
"name": "ee", "name": "ee",
"version": "3.2.0", "version": "3.3.0",
"description": "A fast, desktop software development framework", "description": "A fast, desktop software development framework",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {
@@ -107,10 +107,14 @@
"electron": "^13.6.9", "electron": "^13.6.9",
"electron-builder": "^23.6.0", "electron-builder": "^23.6.0",
"electron-rebuild": "^3.2.8", "electron-rebuild": "^3.2.8",
"eslint": "^5.13.0",
"eslint-plugin-prettier": "^3.0.1",
"nodemon": "^2.0.16" "nodemon": "^2.0.16"
}, },
"dependencies": { "dependencies": {
"ee-core": "^2.1.1", "better-sqlite3": "^7.6.2",
"dayjs": "^1.10.7",
"ee-core": "^2.2.1",
"electron-updater": "^5.3.0", "electron-updater": "^5.3.0",
"lodash": "^4.17.21" "lodash": "^4.17.21"
} }