171 lines
3.7 KiB
TypeScript
171 lines
3.7 KiB
TypeScript
/**
|
||
* electron-store 配置和管理
|
||
*
|
||
* 重要说明:
|
||
* electron-store 必须在主进程中初始化和使用!
|
||
*
|
||
* 配置陷阱:
|
||
* 1. electron-store 使用 Node.js 的 fs 模块,只能在主进程中运行
|
||
* 2. 渲染进程无法直接使用 electron-store(contextIsolation: true)
|
||
* 3. 必须通过 IPC 通信让主进程代理所有 store 操作
|
||
* 4. 不要尝试在 preload 中导入 electron-store
|
||
*/
|
||
import Store from 'electron-store'
|
||
|
||
// 用户设置的类型定义
|
||
interface UserSettings {
|
||
// 计数器值
|
||
count: number
|
||
// 主题设置
|
||
theme: 'light' | 'dark' | 'system'
|
||
// 其他可扩展的设置...
|
||
[key: string]: unknown
|
||
}
|
||
|
||
// 窗口边界的类型定义
|
||
interface WindowBounds {
|
||
width: number
|
||
height: number
|
||
x?: number
|
||
y?: number
|
||
}
|
||
|
||
// Store 的 Schema 定义
|
||
interface StoreSchema {
|
||
// 用户设置
|
||
settings: UserSettings
|
||
// 窗口位置和大小
|
||
windowBounds: WindowBounds
|
||
}
|
||
|
||
// Store 实例(延迟初始化)
|
||
let store: Store<StoreSchema> | null = null
|
||
|
||
/**
|
||
* 初始化 Store
|
||
* 必须在 app.whenReady() 之后调用
|
||
*/
|
||
export function initStore(): void {
|
||
if (store) {
|
||
console.warn('Store 已经初始化过了')
|
||
return
|
||
}
|
||
|
||
store = new Store<StoreSchema>({
|
||
// 存储文件名
|
||
name: 'asset-pro-config',
|
||
|
||
// 默认值
|
||
defaults: {
|
||
settings: {
|
||
count: 0,
|
||
theme: 'system'
|
||
},
|
||
windowBounds: {
|
||
width: 1600,
|
||
height: 900
|
||
}
|
||
},
|
||
|
||
// Schema 验证(可选但推荐)
|
||
schema: {
|
||
settings: {
|
||
type: 'object',
|
||
properties: {
|
||
count: { type: 'number', default: 0 },
|
||
theme: {
|
||
type: 'string',
|
||
enum: ['light', 'dark', 'system'],
|
||
default: 'system'
|
||
}
|
||
},
|
||
default: {}
|
||
},
|
||
windowBounds: {
|
||
type: 'object',
|
||
properties: {
|
||
width: { type: 'number', default: 1600 },
|
||
height: { type: 'number', default: 900 },
|
||
x: { type: 'number' },
|
||
y: { type: 'number' }
|
||
},
|
||
default: {}
|
||
}
|
||
}
|
||
})
|
||
|
||
console.log('Store 初始化完成,存储路径:', store.path)
|
||
}
|
||
|
||
/**
|
||
* 获取 Store 实例
|
||
* @throws 如果 Store 未初始化
|
||
*/
|
||
function getStore(): Store<StoreSchema> {
|
||
if (!store) {
|
||
throw new Error('Store 未初始化,请先调用 initStore()')
|
||
}
|
||
return store
|
||
}
|
||
|
||
/**
|
||
* 获取用户设置
|
||
* @param key - 可选的设置键名
|
||
* @returns 设置值或所有设置
|
||
*/
|
||
export function getSettings(key?: string): unknown {
|
||
const s = getStore()
|
||
if (key) {
|
||
return s.get(`settings.${key}`)
|
||
}
|
||
return s.get('settings')
|
||
}
|
||
|
||
/**
|
||
* 保存用户设置
|
||
* @param key - 设置键名
|
||
* @param value - 设置值
|
||
*/
|
||
export function saveSettings(key: string, value: unknown): void {
|
||
const s = getStore()
|
||
s.set(`settings.${key}`, value)
|
||
}
|
||
|
||
/**
|
||
* 获取窗口边界设置
|
||
* @returns 窗口边界
|
||
*/
|
||
export function getWindowBounds(): WindowBounds {
|
||
const s = getStore()
|
||
const bounds = s.get('windowBounds')
|
||
|
||
// 强制使用新尺寸 1600x900
|
||
const newBounds: WindowBounds = {
|
||
width: 1600,
|
||
height: 900
|
||
}
|
||
|
||
// 如果之前保存了位置且位置有效,保留位置信息
|
||
// 否则不设置 x 和 y,让窗口居中显示
|
||
if (bounds.x !== undefined && bounds.y !== undefined) {
|
||
newBounds.x = bounds.x
|
||
newBounds.y = bounds.y
|
||
}
|
||
|
||
// 如果尺寸不是 1600x900,则更新并保存
|
||
if (bounds.width !== 1600 || bounds.height !== 900) {
|
||
s.set('windowBounds', newBounds)
|
||
}
|
||
|
||
return newBounds
|
||
}
|
||
|
||
/**
|
||
* 保存窗口边界设置
|
||
* @param bounds - 窗口边界
|
||
*/
|
||
export function saveWindowBounds(bounds: WindowBounds): void {
|
||
const s = getStore()
|
||
s.set('windowBounds', bounds)
|
||
}
|