feat: implement custom window controls and replace header bar with title bar
- Add window handlers for minimize, maximize, close, and check if maximized in ipcMain. - Update preload script to use new window control IPC events. - Refactor window service to remove old IPC event handlers and use new handlers. - Remove old HeaderBar and DragRegion components, replacing them with a new TitleBar component. - Update Layout component to use TitleBar instead of HeaderBar. - Remove useWinManager hook as its functionality is now integrated into TitleBar. - Update login page to remove HeaderBar and adjust layout accordingly. - Update constants to remove old window IPC events. - Update package dependencies to replace @iconify/vue with @lucide/vue.
This commit is contained in:
23
electron/ipc/window-handlers.ts
Normal file
23
electron/ipc/window-handlers.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ipcMain, BrowserWindow } from 'electron';
|
||||
|
||||
export function registerWindowHandlers(mainWindow: BrowserWindow): void {
|
||||
ipcMain.handle('window:minimize', () => {
|
||||
mainWindow.minimize();
|
||||
});
|
||||
|
||||
ipcMain.handle('window:maximize', () => {
|
||||
if (mainWindow.isMaximized()) {
|
||||
mainWindow.unmaximize();
|
||||
} else {
|
||||
mainWindow.maximize();
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle('window:close', () => {
|
||||
mainWindow.close();
|
||||
});
|
||||
|
||||
ipcMain.handle('window:isMaximized', () => {
|
||||
return mainWindow.isMaximized();
|
||||
});
|
||||
}
|
||||
@@ -8,11 +8,13 @@ const api: WindowApi = {
|
||||
open: (url: string) => ipcRenderer.invoke('external-open', url)
|
||||
},
|
||||
|
||||
closeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_CLOSE),
|
||||
minimizeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_MINIMIZE),
|
||||
maximizeWindow: () => ipcRenderer.send(IPC_EVENTS.WINDOW_MAXIMIZE),
|
||||
onWindowMaximized: (callback: (isMaximized: boolean) => void) => ipcRenderer.on(IPC_EVENTS.WINDOW_MAXIMIZE + 'back', (_, isMaximized) => callback(isMaximized)),
|
||||
isWindowMaximized: () => ipcRenderer.invoke(IPC_EVENTS.IS_WINDOW_MAXIMIZED),
|
||||
platform: process.platform,
|
||||
|
||||
windowMinimize: () => ipcRenderer.invoke('window:minimize'),
|
||||
windowMaximize: () => ipcRenderer.invoke('window:maximize'),
|
||||
windowClose: () => ipcRenderer.invoke('window:close'),
|
||||
windowIsMaximized: () => ipcRenderer.invoke('window:isMaximized'),
|
||||
|
||||
viewIsReady: () => ipcRenderer.send(IPC_EVENTS.RENDERER_IS_READY),
|
||||
|
||||
app: {
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { WindowNames } from '@lib/types'
|
||||
|
||||
import { CONFIG_KEYS, IPC_EVENTS, WINDOW_NAMES } from '@lib/constants'
|
||||
import { BrowserWindow, BrowserWindowConstructorOptions, ipcMain, IpcMainInvokeEvent, type IpcMainEvent } from 'electron'
|
||||
import { debounce } from '@lib/utils'
|
||||
import { createLogo } from '@electron/utils'
|
||||
|
||||
import logManager from '@electron/service/logger'
|
||||
@@ -26,10 +25,14 @@ interface SizeOptions {
|
||||
minHeight?: number; // 窗口最小高度,可选
|
||||
}
|
||||
|
||||
const isMac = process.platform === 'darwin';
|
||||
const isWindows = process.platform === 'win32';
|
||||
const useCustomTitleBar = isWindows;
|
||||
|
||||
const SHARED_WINDOW_OPTIONS = {
|
||||
frame: false,
|
||||
titleBarStyle: 'hidden',
|
||||
trafficLightPosition: { x: -100, y: -100 },
|
||||
frame: isMac || !useCustomTitleBar,
|
||||
titleBarStyle: isMac ? 'hiddenInset' : useCustomTitleBar ? 'hidden' : 'default',
|
||||
trafficLightPosition: isMac ? { x: 16, y: 16 } : undefined,
|
||||
show: false,
|
||||
title: 'NIANXX',
|
||||
darkTheme: themeManager.isDark,
|
||||
@@ -71,29 +74,6 @@ class WindowService {
|
||||
}
|
||||
|
||||
private _setupIpcEvents() {
|
||||
const handleCloseWindow = (e: IpcMainEvent) => {
|
||||
const target = BrowserWindow.fromWebContents(e.sender);
|
||||
const winName = this.getName(target);
|
||||
|
||||
this.close(target, this._isReallyClose(winName));
|
||||
}
|
||||
|
||||
const handleMinimizeWindow = (e: IpcMainEvent) => {
|
||||
BrowserWindow.fromWebContents(e.sender)?.minimize();
|
||||
}
|
||||
|
||||
const handleMaximizeWindow = (e: IpcMainEvent) => {
|
||||
this.toggleMax(BrowserWindow.fromWebContents(e.sender));
|
||||
}
|
||||
|
||||
const handleIsWindowMaximized = (e: IpcMainInvokeEvent) => {
|
||||
return BrowserWindow.fromWebContents(e.sender)?.isMaximized() ?? false;
|
||||
}
|
||||
|
||||
ipcMain.on(IPC_EVENTS.WINDOW_CLOSE, handleCloseWindow);
|
||||
ipcMain.on(IPC_EVENTS.WINDOW_MINIMIZE, handleMinimizeWindow);
|
||||
ipcMain.on(IPC_EVENTS.WINDOW_MAXIMIZE, handleMaximizeWindow);
|
||||
ipcMain.handle(IPC_EVENTS.IS_WINDOW_MAXIMIZED, handleIsWindowMaximized);
|
||||
ipcMain.handle(IPC_EVENTS.APP_LOAD_PAGE, (e: IpcMainInvokeEvent, page: string) => {
|
||||
const win = BrowserWindow.fromWebContents(e.sender);
|
||||
if (win) this._loadPage(win, page);
|
||||
@@ -139,17 +119,13 @@ class WindowService {
|
||||
}
|
||||
|
||||
private _setupWinLifecycle(window: BrowserWindow, name: WindowNames) {
|
||||
const updateWinStatus = debounce(() => !window?.isDestroyed()
|
||||
&& window?.webContents?.send(IPC_EVENTS.WINDOW_MAXIMIZE + 'back', window?.isMaximized()), 80);
|
||||
window.once('closed', () => {
|
||||
this._winStates[name].onClosed.forEach(callback => callback(window));
|
||||
window?.destroy();
|
||||
window?.removeListener('resize', updateWinStatus);
|
||||
this._winStates[name].instance = void 0;
|
||||
this._winStates[name].isHidden = false;
|
||||
logManager.info(`Window closed: ${name}`);
|
||||
});
|
||||
window.on('resize', updateWinStatus)
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ import { logManager } from '@electron/service/logger'
|
||||
import { configManager } from '@electron/service/config-service'
|
||||
import { trayManager } from '@electron/service/tray-service'
|
||||
import { TabManager } from '@service/tab-manager'
|
||||
import { registerWindowHandlers } from '@electron/ipc/window-handlers'
|
||||
|
||||
const handleTray = (minimizeToTray: boolean) => {
|
||||
if (minimizeToTray) {
|
||||
@@ -109,6 +110,7 @@ export function setupMainWindow() {
|
||||
|
||||
handleTray(minimizeToTray);
|
||||
registerMenus(mainWindow);
|
||||
registerWindowHandlers(mainWindow);
|
||||
|
||||
const tabManager = new TabManager(mainWindow)
|
||||
tabManager.enable()
|
||||
|
||||
Reference in New Issue
Block a user