2026-05-01 20:39:09 +08:00
|
|
|
import { readFileSync, writeFileSync } from 'node:fs'
|
|
|
|
|
import { join } from 'node:path'
|
2026-04-30 22:00:01 +08:00
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
import { app } from 'electron/main'
|
2026-04-30 22:00:01 +08:00
|
|
|
|
|
|
|
|
export interface DesktopAppSettings {
|
2026-05-01 20:39:09 +08:00
|
|
|
openAtLogin: boolean
|
|
|
|
|
keepRunningInBackground: boolean
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
export type DesktopAppSettingKey = keyof DesktopAppSettings
|
2026-04-30 22:00:01 +08:00
|
|
|
|
|
|
|
|
const DEFAULT_DESKTOP_APP_SETTINGS: DesktopAppSettings = {
|
|
|
|
|
openAtLogin: false,
|
|
|
|
|
keepRunningInBackground: true,
|
2026-05-01 20:39:09 +08:00
|
|
|
}
|
2026-04-30 22:00:01 +08:00
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
let cachedSettings: DesktopAppSettings = { ...DEFAULT_DESKTOP_APP_SETTINGS }
|
2026-04-30 22:00:01 +08:00
|
|
|
|
|
|
|
|
function settingsPath(): string {
|
2026-05-01 20:39:09 +08:00
|
|
|
return join(app.getPath('userData'), 'desktop-settings.json')
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeSettings(input: unknown): DesktopAppSettings {
|
2026-05-01 20:39:09 +08:00
|
|
|
if (!input || typeof input !== 'object') {
|
|
|
|
|
return { ...DEFAULT_DESKTOP_APP_SETTINGS }
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
const candidate = input as Partial<Record<DesktopAppSettingKey, unknown>>
|
2026-04-30 22:00:01 +08:00
|
|
|
return {
|
|
|
|
|
openAtLogin:
|
2026-05-01 20:39:09 +08:00
|
|
|
typeof candidate.openAtLogin === 'boolean'
|
2026-04-30 22:00:01 +08:00
|
|
|
? candidate.openAtLogin
|
|
|
|
|
: DEFAULT_DESKTOP_APP_SETTINGS.openAtLogin,
|
|
|
|
|
keepRunningInBackground:
|
2026-05-01 20:39:09 +08:00
|
|
|
typeof candidate.keepRunningInBackground === 'boolean'
|
2026-04-30 22:00:01 +08:00
|
|
|
? candidate.keepRunningInBackground
|
|
|
|
|
: DEFAULT_DESKTOP_APP_SETTINGS.keepRunningInBackground,
|
2026-05-01 20:39:09 +08:00
|
|
|
}
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readPersistedSettings(): DesktopAppSettings {
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
return normalizeSettings(JSON.parse(readFileSync(settingsPath(), 'utf8')))
|
2026-04-30 22:00:01 +08:00
|
|
|
} catch {
|
2026-05-01 20:39:09 +08:00
|
|
|
return { ...DEFAULT_DESKTOP_APP_SETTINGS }
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function persistSettings(settings: DesktopAppSettings): void {
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
writeFileSync(settingsPath(), JSON.stringify(settings), 'utf8')
|
2026-04-30 22:00:01 +08:00
|
|
|
} catch (error) {
|
2026-05-01 20:39:09 +08:00
|
|
|
console.warn('[desktop-settings] persist failed', error)
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function readSystemOpenAtLogin(fallback: boolean): boolean {
|
|
|
|
|
try {
|
2026-05-01 20:39:09 +08:00
|
|
|
return app.getLoginItemSettings().openAtLogin
|
2026-04-30 22:00:01 +08:00
|
|
|
} catch (error) {
|
2026-05-01 20:39:09 +08:00
|
|
|
console.warn('[desktop-settings] read login item failed', error)
|
|
|
|
|
return fallback
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyOpenAtLogin(openAtLogin: boolean): boolean {
|
|
|
|
|
try {
|
|
|
|
|
app.setLoginItemSettings({
|
|
|
|
|
openAtLogin,
|
|
|
|
|
openAsHidden: false,
|
2026-05-01 20:39:09 +08:00
|
|
|
})
|
|
|
|
|
return readSystemOpenAtLogin(openAtLogin)
|
2026-04-30 22:00:01 +08:00
|
|
|
} catch (error) {
|
2026-05-01 20:39:09 +08:00
|
|
|
console.warn('[desktop-settings] apply login item failed', error)
|
|
|
|
|
return openAtLogin
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function initDesktopAppSettings(): DesktopAppSettings {
|
2026-05-01 20:39:09 +08:00
|
|
|
cachedSettings = readPersistedSettings()
|
2026-04-30 22:00:01 +08:00
|
|
|
cachedSettings = {
|
|
|
|
|
...cachedSettings,
|
|
|
|
|
openAtLogin: applyOpenAtLogin(cachedSettings.openAtLogin),
|
2026-05-01 20:39:09 +08:00
|
|
|
}
|
|
|
|
|
persistSettings(cachedSettings)
|
|
|
|
|
return getDesktopAppSettings()
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function getDesktopAppSettings(): DesktopAppSettings {
|
|
|
|
|
cachedSettings = {
|
|
|
|
|
...cachedSettings,
|
|
|
|
|
openAtLogin: readSystemOpenAtLogin(cachedSettings.openAtLogin),
|
2026-05-01 20:39:09 +08:00
|
|
|
}
|
|
|
|
|
return { ...cachedSettings }
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setDesktopAppSetting(
|
|
|
|
|
key: DesktopAppSettingKey,
|
|
|
|
|
value: boolean,
|
|
|
|
|
): DesktopAppSettings {
|
|
|
|
|
const next: DesktopAppSettings = {
|
|
|
|
|
...cachedSettings,
|
|
|
|
|
[key]: value,
|
2026-05-01 20:39:09 +08:00
|
|
|
}
|
2026-04-30 22:00:01 +08:00
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
if (key === 'openAtLogin') {
|
|
|
|
|
next.openAtLogin = applyOpenAtLogin(value)
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-01 20:39:09 +08:00
|
|
|
cachedSettings = next
|
|
|
|
|
persistSettings(cachedSettings)
|
|
|
|
|
return getDesktopAppSettings()
|
2026-04-30 22:00:01 +08:00
|
|
|
}
|