feat: 调整项目结构

This commit is contained in:
DEV_DSW
2025-12-25 08:47:59 +08:00
parent 63592b2c3b
commit 50c7282ff2
9 changed files with 236 additions and 33 deletions

View File

@@ -1,5 +1,7 @@
import { BrowserView, BrowserWindow } from 'electron'
import { BrowserView, BrowserWindow, ipcMain, IpcMainInvokeEvent } from 'electron'
import { randomUUID } from 'crypto'
import { IPC_EVENTS } from '@common/constants'
import path from 'node:path'
type TabId = string
type TabInfo = { id: TabId; url: string; title: string; isLoading: boolean; canGoBack: boolean; canGoForward: boolean }
@@ -16,6 +18,42 @@ export class TabManager {
constructor(win: BrowserWindow) {
this.win = win
this.win.on('resize', () => this.updateActiveBounds())
this._setupIpcEvents()
}
private _setupIpcEvents() {
ipcMain.handle(IPC_EVENTS.TAB_CREATE, (_e, url?: string) => {
const info = this.create(url)
return info
})
ipcMain.handle(IPC_EVENTS.TAB_LIST, () => {
return this.list()
})
ipcMain.handle(IPC_EVENTS.TAB_NAVIGATE, (_e, { tabId, url }: { tabId: TabId; url: string }) => {
this.navigate(tabId, url)
})
ipcMain.handle(IPC_EVENTS.TAB_RELOAD, (_e, tabId: TabId) => {
this.reload(tabId)
})
ipcMain.handle(IPC_EVENTS.TAB_BACK, (_e, tabId: TabId) => {
this.goBack(tabId)
})
ipcMain.handle(IPC_EVENTS.TAB_FORWARD, (_e, tabId: TabId) => {
this.goForward(tabId)
})
ipcMain.handle(IPC_EVENTS.TAB_SWITCH, (_e, tabId: TabId) => {
this.switch(tabId)
})
ipcMain.handle(IPC_EVENTS.TAB_CLOSE, (_e, tabId: TabId) => {
this.close(tabId)
})
}
enable() {
@@ -30,15 +68,40 @@ export class TabManager {
if (view) this.win.removeBrowserView(view)
}
destroy() {
this.disable()
this.views.forEach(view => {
// @ts-ignore
view.webContents.destroy()
})
this.views.clear()
ipcMain.removeHandler(IPC_EVENTS.TAB_CREATE)
ipcMain.removeHandler(IPC_EVENTS.TAB_LIST)
ipcMain.removeHandler(IPC_EVENTS.TAB_NAVIGATE)
ipcMain.removeHandler(IPC_EVENTS.TAB_RELOAD)
ipcMain.removeHandler(IPC_EVENTS.TAB_BACK)
ipcMain.removeHandler(IPC_EVENTS.TAB_FORWARD)
ipcMain.removeHandler(IPC_EVENTS.TAB_SWITCH)
ipcMain.removeHandler(IPC_EVENTS.TAB_CLOSE)
}
list(): TabInfo[] {
return Array.from(this.views.entries()).map(([id, view]) => this.info(id, view))
}
create(url?: string): TabInfo {
create(url?: string, active = true): TabInfo {
const id = randomUUID()
const view = new BrowserView({ webPreferences: { sandbox: true } })
const view = new BrowserView({
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
sandbox: true,
preload: path.join(__dirname, 'preload.js'),
},
})
this.views.set(id, view)
if (this.enabled) this.attach(id)
if (this.enabled && active) this.attach(id)
const target = url && url.length > 0 ? url : 'about:blank'
view.webContents.loadURL(target)
this.bindEvents(id, view)
@@ -110,8 +173,23 @@ export class TabManager {
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) })
const [winWidth, winHeight] = this.win.getContentSize()
const HEADER_HEIGHT = 88
const PADDING = 8
const RIGHT_PANEL_WIDTH = 392 + 80 + 8 // TaskList + SideMenu + Gap
const x = PADDING
const y = HEADER_HEIGHT + PADDING
const width = winWidth - RIGHT_PANEL_WIDTH - PADDING
const height = winHeight - HEADER_HEIGHT - (PADDING * 2)
view.setBounds({
x,
y,
width: Math.max(0, width),
height: Math.max(0, height)
})
}
private bindEvents(id: TabId, view: BrowserView): void {

View File

@@ -7,6 +7,8 @@ import { menuManager } from '@main/service/menu-service'
import { logManager } from '@main/service/logger'
import { configManager } from '@main/service/config-service'
import { trayManager } from '@main/service/tray-service'
import { TabManager } from '@service/tab-manager'
import path from 'node:path'
const handleTray = (minimizeToTray: boolean) => {
if (minimizeToTray) {
@@ -107,6 +109,37 @@ export function setupMainWindow() {
});
handleTray(minimizeToTray);
registerMenus(mainWindow);
const tabManager = new TabManager(mainWindow)
tabManager.enable()
let tabsInitialized = false
mainWindow.on('closed', () => {
tabManager.destroy()
})
const getPageUrl = (page: string) => {
if (MAIN_WINDOW_VITE_DEV_SERVER_URL) {
return `${MAIN_WINDOW_VITE_DEV_SERVER_URL}/html/${page}.html`
}
return `file://${path.join(__dirname, `../renderer/${MAIN_WINDOW_VITE_NAME}/html/${page}.html`)}`
}
const initTabs = () => {
if (tabsInitialized) return
tabsInitialized = true
tabManager.create(getPageUrl('home'), true)
tabManager.create(getPageUrl('knowledge'), false)
tabManager.create(getPageUrl('task'), false)
tabManager.create(getPageUrl('setting'), false)
}
mainWindow.webContents.on('did-finish-load', () => {
const url = mainWindow.webContents.getURL()
if (url.includes('/html/index.html') || url.endsWith('index.html')) {
initTabs()
}
})
});
windowManager.create(WINDOW_NAMES.MAIN, MAIN_WIN_SIZE);