import { app } from 'electron'; import * as fs from 'fs'; import * as path from 'path'; export interface DesktopSettings { hardwareAcceleration: boolean; vaapiVideoEncode: boolean; } export interface DesktopSettingsSnapshot extends DesktopSettings { runtimeHardwareAcceleration: boolean; restartRequired: boolean; } const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = { hardwareAcceleration: true, vaapiVideoEncode: false }; export function getDesktopSettingsSnapshot(): DesktopSettingsSnapshot { const storedSettings = readDesktopSettings(); const runtimeHardwareAcceleration = app.isHardwareAccelerationEnabled(); return { ...storedSettings, runtimeHardwareAcceleration, restartRequired: storedSettings.hardwareAcceleration !== runtimeHardwareAcceleration }; } export function readDesktopSettings(): DesktopSettings { const filePath = getDesktopSettingsPath(); try { if (!fs.existsSync(filePath)) { return { ...DEFAULT_DESKTOP_SETTINGS }; } const raw = fs.readFileSync(filePath, 'utf8'); const parsed = JSON.parse(raw) as Partial; return { vaapiVideoEncode: typeof parsed.vaapiVideoEncode === 'boolean' ? parsed.vaapiVideoEncode : DEFAULT_DESKTOP_SETTINGS.vaapiVideoEncode, hardwareAcceleration: typeof parsed.hardwareAcceleration === 'boolean' ? parsed.hardwareAcceleration : DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration }; } catch { return { ...DEFAULT_DESKTOP_SETTINGS }; } } export function updateDesktopSettings(patch: Partial): DesktopSettingsSnapshot { const nextSettings: DesktopSettings = { ...readDesktopSettings(), ...patch }; const filePath = getDesktopSettingsPath(); fs.mkdirSync(path.dirname(filePath), { recursive: true }); fs.writeFileSync(filePath, JSON.stringify(nextSettings, null, 2), 'utf8'); return getDesktopSettingsSnapshot(); } function getDesktopSettingsPath(): string { return path.join(app.getPath('userData'), 'desktop-settings.json'); }