feat: 项目结构调整|新增依赖
This commit is contained in:
158
src/electron/main/main.ts
Normal file
158
src/electron/main/main.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
import { app, BrowserWindow, ipcMain, shell } from "electron";
|
||||
import path from "node:path";
|
||||
import started from "electron-squirrel-startup";
|
||||
import { TabManager } from '@modules/tab-manager'
|
||||
import { logger } from '@modules/logger'
|
||||
import "@modules/window-size";
|
||||
|
||||
if (started) {
|
||||
app.quit();
|
||||
}
|
||||
|
||||
class AppMain {
|
||||
private mainWindow: BrowserWindow | null = null
|
||||
private tabs: TabManager | null = null
|
||||
private readonly isDev = !!MAIN_WINDOW_VITE_DEV_SERVER_URL
|
||||
|
||||
init() {
|
||||
this.registerLifecycle()
|
||||
this.registerCommonIPC()
|
||||
this.registerAppIPC()
|
||||
this.registerLogIPC()
|
||||
}
|
||||
|
||||
private createWindow(options?: { frameless?: boolean; route?: string }): BrowserWindow {
|
||||
const frameless = !!options?.frameless
|
||||
const win = new BrowserWindow({
|
||||
width: 900,
|
||||
height: 670,
|
||||
autoHideMenuBar: true,
|
||||
frame: frameless ? false : true,
|
||||
// @ts-ignore
|
||||
windowButtonVisibility: frameless ? false : true,
|
||||
resizable: true,
|
||||
maximizable: true,
|
||||
minimizable: true,
|
||||
webPreferences: {
|
||||
devTools: this.isDev,
|
||||
nodeIntegration: false,
|
||||
contextIsolation: true, // 同时启动上下文隔离
|
||||
sandbox: true, // 启动沙箱模式
|
||||
preload: path.join(__dirname, "preload.js"),
|
||||
},
|
||||
})
|
||||
|
||||
this.loadEntry(win, options?.route)
|
||||
if (this.isDev) win.webContents.openDevTools()
|
||||
this.mainWindow = win
|
||||
this.initTabsIfNeeded(options)
|
||||
return win
|
||||
}
|
||||
|
||||
private loadEntry(win: BrowserWindow, route?: string) {
|
||||
// @ts-ignore
|
||||
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
|
||||
const target = route ? `${MAIN_WINDOW_VITE_DEV_SERVER_URL}${route}` : MAIN_WINDOW_VITE_DEV_SERVER_URL
|
||||
win.loadURL(target)
|
||||
} else {
|
||||
win.loadFile(path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/index.html`))
|
||||
}
|
||||
|
||||
// 暴露安全 API 示例:通过 IPC 处理文件读取
|
||||
ipcMain.handle('read-file', async (event, filePath) => {
|
||||
const fs = require('fs');
|
||||
return fs.promises.readFile(filePath, 'utf-8'); // 主进程处理敏感操作
|
||||
});
|
||||
}
|
||||
|
||||
private initTabsIfNeeded(options?: { frameless?: boolean; route?: string }) {
|
||||
if (!this.mainWindow) return
|
||||
const route = options?.route || ''
|
||||
const shouldInit = !!options?.frameless || route.startsWith('/browser')
|
||||
if (!shouldInit) return
|
||||
this.tabs = new TabManager(this.mainWindow)
|
||||
this.registerTabIPC()
|
||||
this.tabs.enable?.()
|
||||
this.tabs.create('about:blank')
|
||||
}
|
||||
|
||||
private registerLifecycle() {
|
||||
app.on("ready", () => {
|
||||
this.createWindow({ frameless: false, route: '/login' })
|
||||
})
|
||||
app.on("window-all-closed", () => {
|
||||
if (process.platform !== "darwin") app.quit()
|
||||
})
|
||||
app.on("activate", () => {
|
||||
if (!BrowserWindow.getAllWindows().length) this.createWindow({ frameless: false, route: '/login' })
|
||||
})
|
||||
}
|
||||
|
||||
private registerCommonIPC() {
|
||||
ipcMain.handle('open-baidu', () => {
|
||||
this.mainWindow?.loadURL("https://www.baidu.com")
|
||||
})
|
||||
ipcMain.handle("external-open", (_event, url: string) => {
|
||||
try {
|
||||
const allowed = /^https:\/\/(www\.)?(baidu\.com|\w+[\.-]?\w+\.[a-z]{2,})(\/.*)?$/i;
|
||||
if (typeof url === "string" && allowed.test(url)) {
|
||||
return shell.openExternal(url);
|
||||
}
|
||||
throw new Error("URL not allowed");
|
||||
} catch (e) {
|
||||
return Promise.reject(e);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private registerAppIPC() {
|
||||
ipcMain.handle('app:set-frameless', async (event, route?: string) => {
|
||||
const old = BrowserWindow.fromWebContents(event.sender)
|
||||
const win = this.createWindow({ frameless: true, route })
|
||||
if (old && !old.isDestroyed()) old.close()
|
||||
return true
|
||||
})
|
||||
}
|
||||
|
||||
private registerTabIPC() {
|
||||
if (!this.tabs) return
|
||||
const tabs = this.tabs
|
||||
ipcMain.handle('tab:create', (_e, url?: string) => tabs.create(url))
|
||||
ipcMain.handle('tab:list', () => tabs.list())
|
||||
ipcMain.handle('tab:navigate', (_e, payload: { tabId: string; url: string }) => tabs.navigate(payload.tabId, payload.url))
|
||||
ipcMain.handle('tab:reload', (_e, tabId: string) => tabs.reload(tabId))
|
||||
ipcMain.handle('tab:back', (_e, tabId: string) => tabs.goBack(tabId))
|
||||
ipcMain.handle('tab:forward', (_e, tabId: string) => tabs.goForward(tabId))
|
||||
ipcMain.handle('tab:switch', (_e, tabId: string) => tabs.switch(tabId))
|
||||
ipcMain.handle('tab:close', (_e, tabId: string) => tabs.close(tabId))
|
||||
}
|
||||
|
||||
private registerLogIPC() {
|
||||
ipcMain.handle('log-to-main', (_e, logLevel: string, message: string) => {
|
||||
switch(logLevel) {
|
||||
case 'trace':
|
||||
logger.trace(message)
|
||||
break
|
||||
case 'debug':
|
||||
logger.debug(message)
|
||||
break
|
||||
case 'info':
|
||||
logger.info(message)
|
||||
break
|
||||
case 'warn':
|
||||
logger.warn(message)
|
||||
break
|
||||
case 'error':
|
||||
logger.error(message)
|
||||
break
|
||||
default:
|
||||
logger.info(message)
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
new AppMain().init()
|
||||
|
||||
|
||||
95
src/electron/main/modules/ipc/index.ts
Normal file
95
src/electron/main/modules/ipc/index.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { ipcMain, BaseWindow } from 'electron'
|
||||
|
||||
type Handler = (...args:any[]) => any
|
||||
type AsyncHandler = (...args:any[]) => Promise<any>
|
||||
|
||||
export class IPCManager {
|
||||
private static instance: IPCManager
|
||||
private handlers: Map<string, Handler>
|
||||
private asyncHandlers: Map<string,AsyncHandler>
|
||||
private constructor() {
|
||||
this.handlers = new Map()
|
||||
this.asyncHandlers =new Map()
|
||||
this.initialize()
|
||||
}
|
||||
|
||||
public static getInstance(): IPCManager {
|
||||
if (!IPCManager.instance) {
|
||||
IPCManager.instance =new IPCManager()
|
||||
}
|
||||
|
||||
return IPCManager.instance
|
||||
}
|
||||
|
||||
private initialize(): void {
|
||||
//注册同步处理器
|
||||
ipcMain.on('ipc:invoke',(event, channel,...args) => {
|
||||
try {
|
||||
const handler = this.handlers.get(channel)
|
||||
|
||||
if(handler){
|
||||
event.returnValue = handler(...args)
|
||||
} else {
|
||||
event.returnValue = { success: false, error: `No handler for channel: ${channel}`}
|
||||
}
|
||||
} catch (error) {
|
||||
event.returnValue = { success: false, error:(error as Error).message}
|
||||
}
|
||||
})
|
||||
|
||||
// 注册异步处理器
|
||||
ipcMain.handle('ipc:invokeAsync', async (_event, channel, ...args) => {
|
||||
try {
|
||||
const handler = this.asyncHandlers.get(channel)
|
||||
|
||||
if(handler){
|
||||
return await handler(...args)
|
||||
}
|
||||
|
||||
throw new Error(`No async handler for channel: ${channel}`)
|
||||
} catch (error) {
|
||||
throw error
|
||||
}
|
||||
})
|
||||
|
||||
ipcMain.handle('get-window-id',(event) => {
|
||||
event.returnValue = event.sender.id
|
||||
})
|
||||
}
|
||||
|
||||
// 注册同步处理器
|
||||
public register(channel:string, handler:Handler):void {
|
||||
this.handlers.set(channel, handler)
|
||||
}
|
||||
|
||||
// 注册异步处理器
|
||||
public registerAsync(channel:string, handler:AsyncHandler):void {
|
||||
this.asyncHandlers.set(channel, handler)
|
||||
}
|
||||
|
||||
// 广播消息给所有窗口
|
||||
public broadcast(channel:string, ...args:any[]):void {
|
||||
BaseWindow.getAllWindows().forEach(window => {
|
||||
if (!window.isDestroyed()) {
|
||||
window.webContents.send(channel, ...args)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 发送消息给指定窗口
|
||||
public sendToWindow(windowId: string, channel:string, ...args:any[]):void {
|
||||
const window = BaseWindow.fromId(windowId)
|
||||
|
||||
if (window && !window.isDestroyed()) {
|
||||
window.webContents.send(channel, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
// 清理所有处理器
|
||||
public clear():void {
|
||||
this.handlers.clear()
|
||||
this.asyncHandlers.clear()
|
||||
}
|
||||
}
|
||||
|
||||
export const ipcManager = IPCManager.getInstance()
|
||||
29
src/electron/main/modules/logger/index.ts
Normal file
29
src/electron/main/modules/logger/index.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import * as log4js from 'log4js';
|
||||
|
||||
log4js.configure({
|
||||
appenders: {
|
||||
out: {
|
||||
type: 'stdout'
|
||||
},
|
||||
app: {
|
||||
type: 'file',
|
||||
filename: 'logs/app.log',
|
||||
backups: 3,
|
||||
compress: false,
|
||||
encoding: 'utf-8',
|
||||
layout: {
|
||||
type: 'pattern',
|
||||
pattern: '[%d{yyyy-MM-dd hh:mm:ss.SSS}] [%p] %m'
|
||||
},
|
||||
keepFileExt: true
|
||||
}
|
||||
},
|
||||
categories: {
|
||||
default: {
|
||||
appenders: ['out', 'app'],
|
||||
level: 'debug'
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const logger = log4js.getLogger();
|
||||
152
src/electron/main/modules/tab-manager/index.ts
Normal file
152
src/electron/main/modules/tab-manager/index.ts
Normal file
@@ -0,0 +1,152 @@
|
||||
import { BrowserView, BrowserWindow } from 'electron'
|
||||
import { randomUUID } from 'crypto'
|
||||
|
||||
type TabId = string
|
||||
type TabInfo = { id: TabId; url: string; title: string; isLoading: boolean; canGoBack: boolean; canGoForward: boolean }
|
||||
|
||||
const UI_HEIGHT = 88
|
||||
|
||||
export class TabManager {
|
||||
private win: BrowserWindow
|
||||
private views: Map<TabId, BrowserView> = new Map()
|
||||
private activeId: TabId | null = null
|
||||
private skipNextNavigate: Map<TabId, boolean> = new Map()
|
||||
private enabled = false
|
||||
|
||||
constructor(win: BrowserWindow) {
|
||||
this.win = win
|
||||
this.win.on('resize', () => this.updateActiveBounds())
|
||||
}
|
||||
|
||||
enable() {
|
||||
this.enabled = true
|
||||
this.updateActiveBounds()
|
||||
if (this.activeId) this.attach(this.activeId)
|
||||
}
|
||||
|
||||
disable() {
|
||||
this.enabled = false
|
||||
const view = this.activeId ? this.views.get(this.activeId) : null
|
||||
if (view) this.win.removeBrowserView(view)
|
||||
}
|
||||
|
||||
list(): TabInfo[] {
|
||||
return Array.from(this.views.entries()).map(([id, view]) => this.info(id, view))
|
||||
}
|
||||
|
||||
create(url?: string): TabInfo {
|
||||
const id = randomUUID()
|
||||
const view = new BrowserView({ webPreferences: { sandbox: true } })
|
||||
this.views.set(id, view)
|
||||
if (this.enabled) this.attach(id)
|
||||
const target = url && url.length > 0 ? url : 'about:blank'
|
||||
view.webContents.loadURL(target)
|
||||
this.bindEvents(id, view)
|
||||
const info = this.info(id, view)
|
||||
this.win.webContents.send('tab-created', info)
|
||||
return info
|
||||
}
|
||||
|
||||
switch(tabId: TabId): void {
|
||||
if (!this.views.has(tabId)) return
|
||||
if (this.enabled) this.attach(tabId)
|
||||
this.win.webContents.send('tab-switched', { tabId })
|
||||
}
|
||||
|
||||
close(tabId: TabId): void {
|
||||
const view = this.views.get(tabId)
|
||||
if (!view) return
|
||||
if (this.activeId === tabId) {
|
||||
this.win.removeBrowserView(view)
|
||||
this.activeId = null
|
||||
}
|
||||
// @ts-ignore
|
||||
view.webContents.destroy()
|
||||
this.views.delete(tabId)
|
||||
this.win.webContents.send('tab-closed', { tabId })
|
||||
const next = this.views.keys().next().value as TabId | undefined
|
||||
if (next) this.switch(next)
|
||||
}
|
||||
|
||||
navigate(tabId: TabId, url: string): void {
|
||||
const view = this.views.get(tabId)
|
||||
if (!view) return
|
||||
this.skipNextNavigate.set(tabId, true)
|
||||
view.webContents.loadURL(url)
|
||||
}
|
||||
|
||||
reload(tabId: TabId): void {
|
||||
const view = this.views.get(tabId)
|
||||
if (!view) return
|
||||
view.webContents.reload()
|
||||
}
|
||||
|
||||
goBack(tabId: TabId): void {
|
||||
const view = this.views.get(tabId)
|
||||
if (!view) return
|
||||
if (view.webContents.canGoBack()) view.webContents.goBack()
|
||||
}
|
||||
|
||||
goForward(tabId: TabId): void {
|
||||
const view = this.views.get(tabId)
|
||||
if (!view) return
|
||||
if (view.webContents.canGoForward()) view.webContents.goForward()
|
||||
}
|
||||
|
||||
private attach(tabId: TabId): void {
|
||||
if (!this.enabled) return
|
||||
const view = this.views.get(tabId)
|
||||
if (!view) return
|
||||
if (this.activeId && this.views.get(this.activeId)) {
|
||||
const prev = this.views.get(this.activeId)!
|
||||
this.win.removeBrowserView(prev)
|
||||
}
|
||||
this.activeId = tabId
|
||||
this.win.addBrowserView(view)
|
||||
this.updateActiveBounds()
|
||||
}
|
||||
|
||||
private updateActiveBounds(): void {
|
||||
if (!this.enabled || !this.activeId) return
|
||||
const view = this.views.get(this.activeId)
|
||||
if (!view) return
|
||||
const [width, height] = this.win.getContentSize()
|
||||
view.setBounds({ x: 0, y: UI_HEIGHT, width, height: Math.max(0, height - UI_HEIGHT) })
|
||||
}
|
||||
|
||||
private bindEvents(id: TabId, view: BrowserView): void {
|
||||
const send = () => this.win.webContents.send('tab-updated', this.info(id, view))
|
||||
view.webContents.on('did-start-loading', send)
|
||||
view.webContents.on('did-stop-loading', send)
|
||||
view.webContents.on('did-finish-load', send)
|
||||
view.webContents.on('page-title-updated', send)
|
||||
view.webContents.on('did-navigate', send)
|
||||
view.webContents.on('did-navigate-in-page', send)
|
||||
|
||||
view.webContents.on('will-navigate', (event, url) => {
|
||||
if (this.skipNextNavigate.get(id)) {
|
||||
this.skipNextNavigate.set(id, false)
|
||||
return
|
||||
}
|
||||
event.preventDefault()
|
||||
this.create(url)
|
||||
})
|
||||
|
||||
view.webContents.setWindowOpenHandler(({ url }) => {
|
||||
this.create(url)
|
||||
return { action: 'deny' }
|
||||
})
|
||||
}
|
||||
|
||||
private info(id: TabId, view: BrowserView): TabInfo {
|
||||
const wc = view.webContents
|
||||
return {
|
||||
id,
|
||||
url: wc.getURL(),
|
||||
title: wc.getTitle(),
|
||||
isLoading: wc.isLoading(),
|
||||
canGoBack: wc.canGoBack(),
|
||||
canGoForward: wc.canGoForward()
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/electron/main/modules/tray/index.ts
Normal file
31
src/electron/main/modules/tray/index.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
// 创建系统托盘
|
||||
import { Tray, Menu } from 'electron'
|
||||
import path from 'path'
|
||||
|
||||
const createTray = (app: Electron.App, win: Electron.BrowserWindow) => {
|
||||
let tray = new Tray(path.join(__dirname, '../public/favicon.ico'))
|
||||
|
||||
tray.setToolTip('示例平台') // 鼠标放在托盘图标上的提示信息
|
||||
|
||||
tray.on('click', (e) => {
|
||||
if (e.shiftKey) {
|
||||
app.quit()
|
||||
} else {
|
||||
win.show()
|
||||
}
|
||||
})
|
||||
|
||||
tray.setContextMenu(
|
||||
Menu.buildFromTemplate([
|
||||
{
|
||||
label: '退出',
|
||||
click: () => {
|
||||
// 先把用户的登录状态和用户的登录信息给清楚掉,再退出
|
||||
app.quit()
|
||||
}
|
||||
}
|
||||
])
|
||||
)
|
||||
}
|
||||
|
||||
module.exports = createTray
|
||||
26
src/electron/main/modules/window-size/index.ts
Normal file
26
src/electron/main/modules/window-size/index.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { ipcMain, BrowserWindow } from 'electron'
|
||||
|
||||
// 最小化
|
||||
ipcMain.on('window-min', (event) => {
|
||||
const webContent = event.sender
|
||||
const win = BrowserWindow.fromWebContents(webContent)
|
||||
win?.minimize()
|
||||
})
|
||||
|
||||
// 最大化
|
||||
ipcMain.on('window-max', (event) => {
|
||||
const webContent = event.sender
|
||||
const win = BrowserWindow.fromWebContents(webContent)
|
||||
if (win?.isMaximized()) {
|
||||
win.unmaximize()
|
||||
} else {
|
||||
win?.maximize()
|
||||
}
|
||||
})
|
||||
|
||||
// 关闭
|
||||
ipcMain.on('window-close', (event) => {
|
||||
const webContent = event.sender
|
||||
const win = BrowserWindow.fromWebContents(webContent)
|
||||
win?.close()
|
||||
})
|
||||
Reference in New Issue
Block a user