feat: 项目结构调整|新增依赖

This commit is contained in:
duanshuwen
2025-11-22 21:17:40 +08:00
parent 38b6a4b4a3
commit 6013c38fe7
40 changed files with 535 additions and 115 deletions

View 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()

View 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();

View 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()
}
}
}

View 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

View 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()
})