* refactor(main): 使用枚举管理 IPC 通道 - 新增 IpcChannel 枚举,用于统一管理所有的 IPC 通道 - 修改相关代码,使用 IpcChannel 枚举替代硬编码的字符串通道名称 - 此改动有助于提高代码的可维护性和可读性,避免因通道名称变更导致的错误 * refactor(ipc): 将字符串通道名称替换为 IpcChannel 枚举 - 在多个文件中将硬编码的字符串通道名称替换为 IpcChannel 枚举值 - 更新了相关文件的导入,增加了对 IpcChannel 的引用 - 通过使用枚举来管理 IPC 通道名称,提高了代码的可维护性和可读性 * refactor(ipc): 调整 IPC 通道枚举和预加载脚本 - 移除了 IpcChannel 枚举中的未使用注释 - 更新了预加载脚本中 IpcChannel 的导入路径 * refactor(ipc): 更新 IpcChannel导入路径 - 将 IpcChannel 的导入路径从 @main/enum/IpcChannel 修改为 @shared/IpcChannel - 此修改涉及多个文件,包括 AppUpdater、BackupManager、EditMcpJsonPopup 等 - 同时移除了 tsconfig.web.json 中对 src/main/**/* 的引用 * refactor(ipc): 添加 ReduxStoreReady 事件并更新事件监听 - 在 IpcChannel 枚举中添加 ReduxStoreReady 事件 - 更新 ReduxService 中的事件监听,使用新的枚举值 * refactor(main): 重构 ReduxService 中的状态变化事件处理 - 将状态变化事件名称定义为常量 STATUS_CHANGE_EVENT - 更新事件监听和触发使用新的常量 - 优化了代码结构,提高了可维护性 * refactor(i18n): 优化国际化配置和语言选择逻辑 - 在多个文件中引入 defaultLanguage 常量,统一默认语言设置 - 调整 i18n 初始化和语言变更逻辑,使用新配置 - 更新相关组件和 Hook 中的语言选择逻辑 * refactor(ConfigManager): 重构配置管理器 - 添加 ConfigKeys 枚举,用于统一配置项的键名 - 引入 defaultLanguage,作为默认语言设置 - 重构 get 和 set 方法,使用 ConfigKeys 枚举作为键名 - 优化类型定义和方法签名,提高代码可读性和可维护性 * refactor(ConfigManager): 重命名配置键 ZoomFactor 将配置键 zoomFactor 重命名为 ZoomFactor,以符合命名规范。 更新了相关方法和属性以反映这一变更。 * refactor(shared): 重构常量定义并优化文件大小格式化逻辑 - 在 constant.ts 中添加 KB、MB、GB 常量定义 - 将 defaultLanguage 移至 constant.ts - 更新 ConfigManager、useAppInit、i18n、GeneralSettings 等文件中的导入路径 - 优化 formatFileSize 函数,使用新定义的常量 * refactor(FileSize): 使用 GB/MB/KB 等常量处理文件大小计算 * refactor(ipc): 将字符串通道名称替换为 IpcChannel 枚举 - 在多个文件中将硬编码的字符串通道名称替换为 IpcChannel 枚举值 - 更新了相关文件的导入,增加了对 IpcChannel 的引用 - 通过使用枚举来管理 IPC 通道名称,提高了代码的可维护性和可读性 * refactor(ipc): 更新 IpcChannel导入路径 - 将 IpcChannel 的导入路径从 @main/enum/IpcChannel 修改为 @shared/IpcChannel - 此修改涉及多个文件,包括 AppUpdater、BackupManager、EditMcpJsonPopup 等 - 同时移除了 tsconfig.web.json 中对 src/main/**/* 的引用 * refactor(i18n): 优化国际化配置和语言选择逻辑 - 在多个文件中引入 defaultLanguage 常量,统一默认语言设置 - 调整 i18n 初始化和语言变更逻辑,使用新配置 - 更新相关组件和 Hook 中的语言选择逻辑 * refactor(shared): 重构常量定义并优化文件大小格式化逻辑 - 在 constant.ts 中添加 KB、MB、GB 常量定义 - 将 defaultLanguage 移至 constant.ts - 更新 ConfigManager、useAppInit、i18n、GeneralSettings 等文件中的导入路径 - 优化 formatFileSize 函数,使用新定义的常量 * refactor: 移除重复的导入语句 - 在 HomeWindow.tsx 和 useAppInit.ts 文件中移除了重复的 defaultLanguage导入语句 - 这个改动简化了代码结构,提高了代码的可读性和维护性
141 lines
3.5 KiB
TypeScript
141 lines
3.5 KiB
TypeScript
import { defaultLanguage, ZOOM_SHORTCUTS } from '@shared/config/constant'
|
|
import { LanguageVarious, Shortcut, ThemeMode } from '@types'
|
|
import { app } from 'electron'
|
|
import Store from 'electron-store'
|
|
|
|
import { locales } from '../utils/locales'
|
|
|
|
enum ConfigKeys {
|
|
Language = 'language',
|
|
Theme = 'theme',
|
|
LaunchToTray = 'launchToTray',
|
|
Tray = 'tray',
|
|
TrayOnClose = 'trayOnClose',
|
|
ZoomFactor = 'ZoomFactor',
|
|
Shortcuts = 'shortcuts',
|
|
ClickTrayToShowQuickAssistant = 'clickTrayToShowQuickAssistant',
|
|
EnableQuickAssistant = 'enableQuickAssistant'
|
|
}
|
|
|
|
export class ConfigManager {
|
|
private store: Store
|
|
private subscribers: Map<string, Array<(newValue: any) => void>> = new Map()
|
|
|
|
constructor() {
|
|
this.store = new Store()
|
|
}
|
|
|
|
getLanguage(): LanguageVarious {
|
|
const locale = Object.keys(locales).includes(app.getLocale()) ? app.getLocale() : defaultLanguage
|
|
return this.get(ConfigKeys.Language, locale) as LanguageVarious
|
|
}
|
|
|
|
setLanguage(theme: LanguageVarious) {
|
|
this.set(ConfigKeys.Language, theme)
|
|
}
|
|
|
|
getTheme(): ThemeMode {
|
|
return this.get(ConfigKeys.Theme, ThemeMode.light)
|
|
}
|
|
|
|
setTheme(theme: ThemeMode) {
|
|
this.set(ConfigKeys.Theme, theme)
|
|
}
|
|
|
|
getLaunchToTray(): boolean {
|
|
return !!this.get(ConfigKeys.LaunchToTray, false)
|
|
}
|
|
|
|
setLaunchToTray(value: boolean) {
|
|
this.set(ConfigKeys.LaunchToTray, value)
|
|
}
|
|
|
|
getTray(): boolean {
|
|
return !!this.get(ConfigKeys.Tray, true)
|
|
}
|
|
|
|
setTray(value: boolean) {
|
|
this.set(ConfigKeys.Tray, value)
|
|
this.notifySubscribers(ConfigKeys.Tray, value)
|
|
}
|
|
|
|
getTrayOnClose(): boolean {
|
|
return !!this.get(ConfigKeys.TrayOnClose, true)
|
|
}
|
|
|
|
setTrayOnClose(value: boolean) {
|
|
this.set(ConfigKeys.TrayOnClose, value)
|
|
}
|
|
|
|
getZoomFactor(): number {
|
|
return this.get<number>(ConfigKeys.ZoomFactor, 1)
|
|
}
|
|
|
|
setZoomFactor(factor: number) {
|
|
this.set(ConfigKeys.ZoomFactor, factor)
|
|
this.notifySubscribers(ConfigKeys.ZoomFactor, factor)
|
|
}
|
|
|
|
subscribe<T>(key: string, callback: (newValue: T) => void) {
|
|
if (!this.subscribers.has(key)) {
|
|
this.subscribers.set(key, [])
|
|
}
|
|
this.subscribers.get(key)!.push(callback)
|
|
}
|
|
|
|
unsubscribe<T>(key: string, callback: (newValue: T) => void) {
|
|
const subscribers = this.subscribers.get(key)
|
|
if (subscribers) {
|
|
this.subscribers.set(
|
|
key,
|
|
subscribers.filter((subscriber) => subscriber !== callback)
|
|
)
|
|
}
|
|
}
|
|
|
|
private notifySubscribers<T>(key: string, newValue: T) {
|
|
const subscribers = this.subscribers.get(key)
|
|
if (subscribers) {
|
|
subscribers.forEach((subscriber) => subscriber(newValue))
|
|
}
|
|
}
|
|
|
|
getShortcuts() {
|
|
return this.get(ConfigKeys.Shortcuts, ZOOM_SHORTCUTS) as Shortcut[] | []
|
|
}
|
|
|
|
setShortcuts(shortcuts: Shortcut[]) {
|
|
this.set(
|
|
ConfigKeys.Shortcuts,
|
|
shortcuts.filter((shortcut) => shortcut.system)
|
|
)
|
|
this.notifySubscribers(ConfigKeys.Shortcuts, shortcuts)
|
|
}
|
|
|
|
getClickTrayToShowQuickAssistant(): boolean {
|
|
return this.get<boolean>(ConfigKeys.ClickTrayToShowQuickAssistant, false)
|
|
}
|
|
|
|
setClickTrayToShowQuickAssistant(value: boolean) {
|
|
this.set(ConfigKeys.ClickTrayToShowQuickAssistant, value)
|
|
}
|
|
|
|
getEnableQuickAssistant(): boolean {
|
|
return this.get(ConfigKeys.EnableQuickAssistant, false)
|
|
}
|
|
|
|
setEnableQuickAssistant(value: boolean) {
|
|
this.set(ConfigKeys.EnableQuickAssistant, value)
|
|
}
|
|
|
|
set(key: string, value: unknown) {
|
|
this.store.set(key, value)
|
|
}
|
|
|
|
get<T>(key: string, defaultValue?: T) {
|
|
return this.store.get(key, defaultValue) as T
|
|
}
|
|
}
|
|
|
|
export const configManager = new ConfigManager()
|