From a0cf8c73c9a8e8a447d1967969913a038c15fb0b Mon Sep 17 00:00:00 2001 From: DEV_DSW <562304744@qq.com> Date: Wed, 22 Apr 2026 08:58:16 +0800 Subject: [PATCH] refactor: remove tab management and related IPC events for cleaner architecture --- dist-electron/main/main.js | 2 +- dist-electron/preload/preload.js | 8 - electron/service/tab-manager/index.ts | 235 ----------------------- electron/service/task-operation/index.ts | 7 - electron/wins/index.ts | 9 - runtime-shared/lib/constants.ts | 8 - 6 files changed, 1 insertion(+), 268 deletions(-) delete mode 100644 electron/service/tab-manager/index.ts delete mode 100644 electron/service/task-operation/index.ts diff --git a/dist-electron/main/main.js b/dist-electron/main/main.js index 9203883..51d3af9 100644 --- a/dist-electron/main/main.js +++ b/dist-electron/main/main.js @@ -1,6 +1,6 @@ "use strict"; require("electron"); -require("./main-DXx4hjy_.js"); +require("./main-EkESqPgI.js"); require("electron-squirrel-startup"); require("electron-log"); require("bytenode"); diff --git a/dist-electron/preload/preload.js b/dist-electron/preload/preload.js index a8ecba1..817d1d3 100644 --- a/dist-electron/preload/preload.js +++ b/dist-electron/preload/preload.js @@ -5,14 +5,6 @@ var IPC_EVENTS = /* @__PURE__ */ ((IPC_EVENTS2) => { IPC_EVENTS2["EXTERNAL_OPEN"] = "external-open"; IPC_EVENTS2["APP_SET_FRAMELESS"] = "app:set-frameless"; IPC_EVENTS2["APP_LOAD_PAGE"] = "app:load-page"; - IPC_EVENTS2["TAB_CREATE"] = "tab:create"; - IPC_EVENTS2["TAB_LIST"] = "tab:list"; - IPC_EVENTS2["TAB_NAVIGATE"] = "tab:navigate"; - IPC_EVENTS2["TAB_RELOAD"] = "tab:reload"; - IPC_EVENTS2["TAB_BACK"] = "tab:back"; - IPC_EVENTS2["TAB_FORWARD"] = "tab:forward"; - IPC_EVENTS2["TAB_SWITCH"] = "tab:switch"; - IPC_EVENTS2["TAB_CLOSE"] = "tab:close"; IPC_EVENTS2["LOG_TO_MAIN"] = "log-to-main"; IPC_EVENTS2["READ_FILE"] = "read-file"; IPC_EVENTS2["INVOKE"] = "ipc:invoke"; diff --git a/electron/service/tab-manager/index.ts b/electron/service/tab-manager/index.ts deleted file mode 100644 index 1fd3392..0000000 --- a/electron/service/tab-manager/index.ts +++ /dev/null @@ -1,235 +0,0 @@ -import { BrowserView, BrowserWindow, ipcMain, IpcMainInvokeEvent } from 'electron' -import { randomUUID } from 'crypto' -import { IPC_EVENTS } from '@runtime/lib/constants' -import path from 'node:path' - -declare const MAIN_WINDOW_VITE_DEV_SERVER_URL: string | undefined; - -type TabId = string -type TabInfo = { id: TabId; url: string; title: string; isLoading: boolean; canGoBack: boolean; canGoForward: boolean } - -const UI_HEIGHT = 88 -const preloadEntryPath = MAIN_WINDOW_VITE_DEV_SERVER_URL - ? path.join(process.cwd(), 'dist-electron', 'preload', 'preload.js') - : path.join(__dirname, '..', 'preload', 'preload.js') - -export class TabManager { - private win: BrowserWindow - private views: Map = new Map() - private activeId: TabId | null = null - private skipNextNavigate: Map = new Map() - private enabled = false - - 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() { - 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) - } - - 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, active = true): TabInfo { - const id = randomUUID() - const view = new BrowserView({ - webPreferences: { - nodeIntegration: false, - contextIsolation: true, - sandbox: true, - preload: preloadEntryPath, - }, - }) - this.views.set(id, view) - if (this.enabled && active) 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 [winWidth, winHeight] = this.win.getContentSize() - - const HEADER_HEIGHT = 88 - const PADDING = 8 - const RIGHT_PANEL_WIDTH = 392 + 80 + 8 + 8 // TaskList + SideMenu + Gap + RightPadding - - 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 { - 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() - } - } -} diff --git a/electron/service/task-operation/index.ts b/electron/service/task-operation/index.ts deleted file mode 100644 index 958ac6d..0000000 --- a/electron/service/task-operation/index.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { ipcMain } from 'electron' -import { IPC_EVENTS } from '@runtime/lib/constants' -import { spawn } from 'child_process' - - - -export function runTaskOperationService() {} diff --git a/electron/wins/index.ts b/electron/wins/index.ts index 0db4f66..b894b48 100644 --- a/electron/wins/index.ts +++ b/electron/wins/index.ts @@ -1,11 +1,9 @@ import type { BrowserWindow } from 'electron' -import { ipcMain } from 'electron'; import { WINDOW_NAMES, MAIN_WIN_SIZE, IPC_EVENTS, MENU_IDS, CONVERSATION_ITEM_MENU_IDS, CONVERSATION_LIST_MENU_IDS, MESSAGE_ITEM_MENU_IDS, CONFIG_KEYS } from '@runtime/lib/constants' import { windowManager } from '@electron/service/window-service' import { menuManager } from '@electron/service/menu-service' import { logManager } from '@electron/service/logger' import { configManager } from '@electron/service/config-service' -import { TabManager } from '@service/tab-manager' import { registerWindowHandlers } from '@electron/ipc/window-handlers' import { createTray, destroyTray } from '@electron/main/tray' @@ -111,13 +109,6 @@ export function setupMainWindow() { handleTray(minimizeToTray, mainWindow); registerMenus(mainWindow); registerWindowHandlers(mainWindow); - - const tabManager = new TabManager(mainWindow) - tabManager.enable() - - mainWindow.on('closed', () => { - tabManager.destroy() - }) }); windowManager.create(WINDOW_NAMES.MAIN, MAIN_WIN_SIZE); diff --git a/runtime-shared/lib/constants.ts b/runtime-shared/lib/constants.ts index b2d7858..f8db9fc 100644 --- a/runtime-shared/lib/constants.ts +++ b/runtime-shared/lib/constants.ts @@ -3,14 +3,6 @@ export enum IPC_EVENTS { EXTERNAL_OPEN = 'external-open', APP_SET_FRAMELESS = 'app:set-frameless', APP_LOAD_PAGE = 'app:load-page', - TAB_CREATE = 'tab:create', - TAB_LIST = 'tab:list', - TAB_NAVIGATE = 'tab:navigate', - TAB_RELOAD = 'tab:reload', - TAB_BACK = 'tab:back', - TAB_FORWARD = 'tab:forward', - TAB_SWITCH = 'tab:switch', - TAB_CLOSE = 'tab:close', LOG_TO_MAIN = 'log-to-main', READ_FILE = 'read-file', INVOKE = 'ipc:invoke',