From 7e4fee3fe511d4da252a75169fdcc15bcbbca99d Mon Sep 17 00:00:00 2001 From: MrKBear Date: Fri, 15 Apr 2022 17:42:29 +0800 Subject: [PATCH] Add electron minthread --- config/webpack.common.js | 3 +- config/webpack.electron.js | 1 + package.json | 4 ++- source/Component/HeaderBar/HeaderBar.scss | 6 ++++ source/Electron/Electron.ts | 44 ++++++++++++++++++----- source/Service/Service.ts | 2 ++ 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/config/webpack.common.js b/config/webpack.common.js index 8e9bcd0..036b1c3 100644 --- a/config/webpack.common.js +++ b/config/webpack.common.js @@ -83,7 +83,8 @@ const Entry = () => ({ }, Electron: { - import: source("./Electron/Electron.ts") + import: source("./Electron/Electron.ts"), + dependOn: ["Service"] }, }); diff --git a/config/webpack.electron.js b/config/webpack.electron.js index 0a79da0..de970fd 100644 --- a/config/webpack.electron.js +++ b/config/webpack.electron.js @@ -8,6 +8,7 @@ module.exports = (env) => { const config = { entry: { + Service: AllEntry.Service, Electron: AllEntry.Electron, }, diff --git a/package.json b/package.json index e3178c0..b5390c7 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,9 @@ "build-electron": "webpack --mode development --config ./config/webpack.electron.js", "release-electron": "webpack --mode production --no-devtool --config ./config/webpack.electron.js", "electron-cache": "set ELECTRON_SKIP_BINARY_DOWNLOAD=& set ELECTRON_MIRROR=https://npmmirror.com/mirrors/electron/& set ELECTRON_CUSTOM_DIR={{ version }}& node ./node_modules/electron/install.js", - "electron-test": "electron" + "electron": "set LIVING_TOGETHER_BASE_PATH=./build& npx electron ./build/Electron.js", + "build-run-electron": "npm run build-desktop-web & npm run copy-fluent-icon & npm run build-electron & npm run electron", + "release-run-electron": "npm run release-desktop-web & npm run copy-fluent-icon & npm run release-electron & npm run electron" }, "keywords": [ "artwork", diff --git a/source/Component/HeaderBar/HeaderBar.scss b/source/Component/HeaderBar/HeaderBar.scss index eb11584..5f284ed 100644 --- a/source/Component/HeaderBar/HeaderBar.scss +++ b/source/Component/HeaderBar/HeaderBar.scss @@ -7,6 +7,9 @@ div.header-bar { justify-content: space-between; user-select: none; + // 在 Electron 中用于拖拽窗口 + -webkit-app-region: drag; + div.title { padding-left: 3px; } @@ -36,6 +39,9 @@ div.header-bar { min-width: 135px; display: flex; + // 在 Electron 中用于拖拽窗口 + -webkit-app-region: no-drag; + div.action-button { width: 45px; height: 45px; diff --git a/source/Electron/Electron.ts b/source/Electron/Electron.ts index cf12dc6..e274c75 100644 --- a/source/Electron/Electron.ts +++ b/source/Electron/Electron.ts @@ -1,16 +1,44 @@ import { app, BrowserWindow } from "electron"; +import { Service } from "@Service/Service"; +const ENV = process.env ?? {}; -async function main() { +class ElectronApp { - await app.whenReady(); + public service: Service; - const win = new BrowserWindow({ - width: 800, - height: 600 - }); + public serviceUrl: string = "http://127.0.0.1"; - win.loadURL("https://www.baidu.com"); + public constructor() { + this.service = new Service(); + } + public async runService() { + + let defaultPort: number | undefined = parseInt(ENV.LIVING_TOGETHER_DEFAULT_PORT ?? ""); + if (isNaN(defaultPort)) defaultPort = undefined; + + this.serviceUrl = await this.service.run( + ENV.LIVING_TOGETHER_BASE_PATH, defaultPort + ); + } + + public mainWindows?: BrowserWindow; + + public async runMainThread() { + + await app.whenReady(); + + await this.runService(); + + this.mainWindows = new BrowserWindow({ + width: 800, + height: 600, + titleBarStyle: 'hidden', + frame: false, + }); + + this.mainWindows.loadURL(this.serviceUrl); + } } -main(); \ No newline at end of file +new ElectronApp().runMainThread(); \ No newline at end of file diff --git a/source/Service/Service.ts b/source/Service/Service.ts index c3bff0c..c79be00 100644 --- a/source/Service/Service.ts +++ b/source/Service/Service.ts @@ -41,6 +41,8 @@ class Service { this.app.listen(this.servicePort); console.log("Service: service run in port " + this.servicePort); + + return "http://127.0.0.1:" + this.servicePort + "/"; } }