feat: enhance theme management and image caching functionality

This commit is contained in:
DEV_DSW
2026-04-16 15:43:35 +08:00
parent 411f4f3421
commit b1f589a674
13 changed files with 74 additions and 183 deletions

View File

@@ -3,6 +3,7 @@ import { CONFIG_KEYS } from '@lib/constants'
import { setupMainWindow } from './wins';
import started from 'electron-squirrel-startup'
import configManager from '@electron/service/config-service'
import themeManager from '@electron/service/theme-service'
import { runTaskOperationService } from '@electron/process/runTaskOperationService'
import { initScriptStoreService } from '@electron/service/script-store-service'
import log from 'electron-log';
@@ -154,6 +155,7 @@ if (started) {
app.whenReady().then(async () => {
await configManager.init();
await themeManager.init();
gatewayManager.init();

View File

@@ -14,6 +14,7 @@ const DEFAULT_CONFIG: IConfig = {
[CONFIG_KEYS.PROVIDER]: '',
[CONFIG_KEYS.DEFAULT_MODEL]: null,
[CONFIG_KEYS.SELECTED_CHANNELS]: [],
[CONFIG_KEYS.IMAGE_CACHE]: [],
}
export class ConfigService {

View File

@@ -8,15 +8,17 @@ class ThemeService {
private _isDark: boolean = nativeTheme.shouldUseDarkColors;
constructor() {
const themeMode = configManager.get(CONFIG_KEYS.THEME_MODE);
if (themeMode) {
nativeTheme.themeSource = themeMode;
this._isDark = nativeTheme.shouldUseDarkColors;
}
this._setupIpcEvent();
logManager.info('ThemeService initialized successfully.');
}
async init() {
const themeMode = configManager.get(CONFIG_KEYS.THEME_MODE);
nativeTheme.themeSource = themeMode;
this._isDark = nativeTheme.shouldUseDarkColors;
logManager.info('ThemeService async init completed.');
}
private _setupIpcEvent() {
ipcMain.handle(IPC_EVENTS.SET_THEME_MODE, (_e, mode: ThemeMode) => {
nativeTheme.themeSource = mode;
@@ -25,7 +27,7 @@ class ThemeService {
});
ipcMain.handle(IPC_EVENTS.GET_THEME_MODE, () => {
return nativeTheme.themeSource;
return configManager.get(CONFIG_KEYS.THEME_MODE);
});
ipcMain.handle(IPC_EVENTS.IS_DARK_THEME, () => {

View File

@@ -29,24 +29,26 @@ const isMac = process.platform === 'darwin';
const isWindows = process.platform === 'win32';
const useCustomTitleBar = isWindows;
const SHARED_WINDOW_OPTIONS = {
frame: isMac || !useCustomTitleBar,
titleBarStyle: isMac ? 'hiddenInset' : useCustomTitleBar ? 'hidden' : 'default',
trafficLightPosition: isMac ? { x: 16, y: 16 } : undefined,
show: false,
title: 'NIANXX',
darkTheme: themeManager.isDark,
backgroundColor: themeManager.isDark ? '#2C2C2C' : '#FFFFFF',
webPreferences: {
nodeIntegration: false, // 禁用 Node.js 集成,提高安全性
contextIsolation: true, // 启用上下文隔离,防止渲染进程访问主进程 API
sandbox: true, // 启用沙箱模式,进一步增强安全性
backgroundThrottling: false,
preload: MAIN_WINDOW_VITE_DEV_SERVER_URL
? path.join(process.cwd(), 'dist-electron/preload/preload.js')
: path.join(__dirname, 'preload.js'),
},
} as BrowserWindowConstructorOptions;
function getSharedWindowOptions(): BrowserWindowConstructorOptions {
return {
frame: isMac || !useCustomTitleBar,
titleBarStyle: isMac ? 'hiddenInset' : useCustomTitleBar ? 'hidden' : 'default',
trafficLightPosition: isMac ? { x: 16, y: 16 } : undefined,
show: false,
title: 'NIANXX',
darkTheme: themeManager.isDark,
backgroundColor: themeManager.isDark ? '#2C2C2C' : '#FFFFFF',
webPreferences: {
nodeIntegration: false, // 禁用 Node.js 集成,提高安全性
contextIsolation: true, // 启用上下文隔离,防止渲染进程访问主进程 API
sandbox: true, // 启用沙箱模式,进一步增强安全性
backgroundThrottling: false,
preload: MAIN_WINDOW_VITE_DEV_SERVER_URL
? path.join(process.cwd(), 'dist-electron/preload/preload.js')
: path.join(__dirname, 'preload.js'),
},
};
}
class WindowService {
private static _instance: WindowService;
@@ -218,7 +220,7 @@ class WindowService {
return this._isHiddenWin(name)
? this._winStates[name].instance as BrowserWindow
: new BrowserWindow({
...SHARED_WINDOW_OPTIONS,
...getSharedWindowOptions(),
icon: this._logo,
...opts,
});