Skip to content

Commit

Permalink
重构代码,移除electron/remote
Browse files Browse the repository at this point in the history
  • Loading branch information
xianyunleo committed Sep 21, 2024
1 parent 5a7ef15 commit 37a3504
Show file tree
Hide file tree
Showing 37 changed files with 900 additions and 937 deletions.
13 changes: 4 additions & 9 deletions electron.vite.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, splitVendorChunkPlugin } from 'electron-vite'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import commonjsExternals from 'vite-plugin-commonjs-externals'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import { builtinModules } from 'module'

const externals = ['fix-path', 'electron-store', '@electron/remote', 'extract-zip',
const externals = ['fix-path', 'electron-store', 'extract-zip',
'hmc-win32', 'net-win32', ...builtinModules, ...builtinModules.map((m) => `node:${m}`)]

export default defineConfig({
Expand All @@ -22,20 +22,15 @@ export default defineConfig({
plugins: [externalizeDepsPlugin()]
},
renderer: {
optimizeDeps: {
exclude: externals
},
optimizeDeps: { exclude: externals },
resolve: {
alias: {
'@': resolve('src')
}
},
plugins: [
vue(),
splitVendorChunkPlugin(),
commonjsExternals({
externals
}),
commonjsExternals({ externals }),
Components({
resolvers: [
AntDesignVueResolver({
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"dependencies": {
"@electron-toolkit/preload": "^2.0.0",
"@electron-toolkit/utils": "^2.0.0",
"@electron/remote": "^2.0.11",
"@tiny-libs/nls": "^1.0.1",
"7zip-min-electron": "^1.4.4",
"electron-dl-downloader": "^1.0.5",
Expand Down
12 changes: 0 additions & 12 deletions src/main/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { electronRequire } from '@/main/utils/electron'
import { isDev, isMacOS, isWindows } from '@/main/utils/utils'
import path from 'path'
import { MAC_USER_CORE_DIR, InitFiles_DIR_NAME, TEMP_DIR_NAME } from '@/main/utils/constant'
Expand All @@ -13,13 +12,7 @@ import GetAppPath from '@/main/utils/GetAppPath'
import Shell from '@/main/utils/Shell'
import { extractZip } from '@/main/utils/extract'

const app = electronRequire('app')

export default class App {
static getVersion() {
return app.getVersion()
}

static async initFileExists() {
return await FileUtil.Exists(GetAppPath.getInitFilePath())
}
Expand Down Expand Up @@ -152,9 +145,4 @@ export default class App {
}
}
}

static exit() {
app.exit()
}

}
74 changes: 43 additions & 31 deletions src/main/MainWindow.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,43 @@
export default class MainWindow {

static _instance;
static forceQuit = false;

/**
*
* @param mainWindow {BrowserWindow}
*/
static init(mainWindow) {
if (this._instance) return
this._instance = mainWindow
mainWindow.on('close', (event) => {
if (!this.forceQuit) {
event.preventDefault();
if (mainWindow.isFullScreen()) {
mainWindow.once('leave-full-screen', () => mainWindow.hide())

mainWindow.setFullScreen(false)
} else {
mainWindow.hide()
}
}
})
}

static show() {
this._instance.isMinimized() ? this._instance.restore() : this._instance.show()
}

}
export default class MainWindow {

static _instance;
static forceQuit = false;

static getInstance(){
return this._instance
}

/**
*
* @param mainWindow {BrowserWindow}
*/
static init(mainWindow) {
if (this._instance) return
this._instance = mainWindow
mainWindow.on('close', (event) => {
if (!this.forceQuit) {
event.preventDefault();
if (mainWindow.isFullScreen()) {
mainWindow.once('leave-full-screen', () => mainWindow.hide())

mainWindow.setFullScreen(false)
} else {
mainWindow.hide()
}
}
})

mainWindow.on('maximize', () => {
mainWindow.webContents.send('mainWindowMaximize')
})

mainWindow.on('unmaximize', () => {
mainWindow.webContents.send('mainWindowUnmaximize')
})
}

static show() {
this._instance.isMinimized() ? this._instance.restore() : this._instance.show()
}

}
108 changes: 54 additions & 54 deletions src/main/TrayManage.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,54 @@
import { app, Tray, Menu, nativeImage } from 'electron'
import { APP_NAME } from '@/shared/utils/constant'
import { isMacOS, isWindows } from '@/main/utils/utils'
import { t } from '@/main/utils/i18n'
import Path from '@/main/utils/Path'
import GetAppPath from '@/main/utils/GetAppPath'
import MainWindow from '@/main/MainWindow'

export default class TrayManage {
static _instance

static init() {
if (this._instance) return
let iconPath = this.getIconPath()
let icon = nativeImage.createFromPath(iconPath).resize({ width: 18, height: 18 })
icon.setTemplateImage(true)
let tray = new Tray(icon)
const contextMenu = this.getContextMenu()
tray.setToolTip(APP_NAME)
tray.setContextMenu(contextMenu)
if (isWindows) {
tray.on('click', () => this.showMainWindow())
}
this._instance = tray
}

static getContextMenu() {
return Menu.buildFromTemplate([
{
label: t('Open'),
click: () => this.showMainWindow()
},
{
label: t('Exit'),
click: () => app.quit()
}
])
}

static showMainWindow() {
MainWindow.show()
}

static updateContextMenu() {
this._instance.setContextMenu(this.getContextMenu())
}

static getIconPath() {
if (isMacOS) {
return Path.Join(GetAppPath.getStaticDir(), 'img/icons/icon-tray-Mac.png')
}
return Path.Join(GetAppPath.getStaticDir(), 'img/icons/icon-tray.png')
}
}
import { app, Tray, Menu, nativeImage } from 'electron'
import { APP_NAME } from '@/shared/utils/constant'
import { isMacOS, isWindows } from '@/main/utils/utils'
import { t } from '@/main/utils/i18n'
import MainWindow from '@/main/MainWindow'
import trayIcon from '@/../resources/img/icons/icon-tray.png?asset'
import trayMacIcon from '@/../resources/img/icons/icon-tray-Mac.png?asset'

export default class TrayManage {
static _instance

static init() {
if (this._instance) return
let iconPath = this.getIconPath()
let icon = nativeImage.createFromPath(iconPath).resize({ width: 18, height: 18 })
icon.setTemplateImage(true)
let tray = new Tray(icon)
const contextMenu = this.getContextMenu()
tray.setToolTip(APP_NAME)
tray.setContextMenu(contextMenu)
if (isWindows) {
tray.on('click', () => this.showMainWindow())
}
this._instance = tray
}

static getContextMenu() {
return Menu.buildFromTemplate([
{
label: t('Open'),
click: () => this.showMainWindow()
},
{
label: t('Exit'),
click: () => app.quit()
}
])
}

static showMainWindow() {
MainWindow.show()
}

static updateContextMenu() {
this._instance.setContextMenu(this.getContextMenu())
}

static getIconPath() {
if (isMacOS) {
return trayMacIcon
}
return trayIcon
}
}
2 changes: 1 addition & 1 deletion src/main/common/call.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Worker } from 'node:worker_threads'
export async function callStatic(className, methodName, ...args) {
let result, importReturn

const utilsClassArr = ['ProcessLibrary']
const utilsClassArr = ['ProcessLibrary', 'FileDialog']
if (utilsClassArr.includes(className)) {
importReturn = await import(`@/main/utils/${className}.js`)
} else if (className === 'I18n') {
Expand Down
2 changes: 1 addition & 1 deletion src/main/core/Env/Env.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default class Env {
static async createOtherBinFile(targetPath, targetOtherFileName, otherBinName) {
let binDirPath = GetPath.getBinDir();
let path = Path.Join(binDirPath, otherBinName);
let targetOtherFilePath = Path.Join(Path.GetDirectoryName(targetPath), targetOtherFileName);
let targetOtherFilePath = Path.Join(Path.GetDirName(targetPath), targetOtherFileName);
await FsUtil.CreateSymbolicLink(path, targetOtherFilePath);
}

Expand Down
5 changes: 1 addition & 4 deletions src/main/core/Env/EnvMacOS.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import {electronRequire} from '@/main/utils/electron';
import FileUtil from "@/main/utils/FileUtil";
import Path from "@/main/utils/Path";
import OS from "@/main/utils/OS";
Expand All @@ -7,13 +6,11 @@ import {APP_NAME} from "@/shared/utils/constant";
import GetPath from "@/shared/utils/GetPath";
import FsUtil from '@/main/utils/FsUtil'

const app = electronRequire('app')

export default class EnvMacOS {
static _envFileName = '.zshrc';

static getEnvFilePath() {
return Path.Join(app.getPath('home'), this._envFileName);
return Path.Join(OS.getHomeDir(), this._envFileName);
}

static async switch(enable) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/core/Nginx.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ export default class Nginx {
access_log off;
}
access_log logs/${Path.GetFileNameWithoutExtension(confName)}.access.log;
error_log logs/${Path.GetFileNameWithoutExtension(confName)}.error.log;
access_log logs/${Path.GetFileNameWithoutExt(confName)}.access.log;
error_log logs/${Path.GetFileNameWithoutExt(confName)}.error.log;
}`;

confText = replaceLineBreak(confText)
Expand Down Expand Up @@ -190,7 +190,7 @@ export default class Nginx {
}
let files = await DirUtil.GetFiles(rewritePath, '.conf');
return files.map(name => {
return Path.GetFileNameWithoutExtension(name);
return Path.GetFileNameWithoutExt(name);
});
}

Expand All @@ -213,7 +213,7 @@ export default class Nginx {
* @returns {number}
*/
static getPortByConfPath(path){
return Number(Path.GetFileNameWithoutExtension(path).split('_')[1])
return Number(Path.GetFileNameWithoutExt(path).split('_')[1])
}

static getErrorLogOffValue(){
Expand Down
Loading

0 comments on commit 37a3504

Please sign in to comment.