141 lines
4.3 KiB
TypeScript
141 lines
4.3 KiB
TypeScript
import { app } from 'electron';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
export type AutoUpdateMode = 'auto' | 'off' | 'version';
|
|
|
|
export interface DesktopSettings {
|
|
autoUpdateMode: AutoUpdateMode;
|
|
autoStart: boolean;
|
|
closeToTray: boolean;
|
|
hardwareAcceleration: boolean;
|
|
manifestUrls: string[];
|
|
preferredVersion: string | null;
|
|
vaapiVideoEncode: boolean;
|
|
}
|
|
|
|
export interface DesktopSettingsSnapshot extends DesktopSettings {
|
|
runtimeHardwareAcceleration: boolean;
|
|
restartRequired: boolean;
|
|
}
|
|
|
|
const DEFAULT_DESKTOP_SETTINGS: DesktopSettings = {
|
|
autoUpdateMode: 'auto',
|
|
autoStart: true,
|
|
closeToTray: true,
|
|
hardwareAcceleration: true,
|
|
manifestUrls: [],
|
|
preferredVersion: null,
|
|
vaapiVideoEncode: false
|
|
};
|
|
|
|
function normalizeAutoUpdateMode(value: unknown): AutoUpdateMode {
|
|
return value === 'off' || value === 'version' ? value : 'auto';
|
|
}
|
|
|
|
function normalizePreferredVersion(value: unknown): string | null {
|
|
return typeof value === 'string' && value.trim().length > 0 ? value.trim() : null;
|
|
}
|
|
|
|
function normalizeManifestUrls(value: unknown): string[] {
|
|
if (!Array.isArray(value)) {
|
|
return [];
|
|
}
|
|
|
|
const manifestUrls: string[] = [];
|
|
|
|
for (const entry of value) {
|
|
if (typeof entry !== 'string') {
|
|
continue;
|
|
}
|
|
|
|
const nextUrl = entry.trim();
|
|
|
|
if (!nextUrl || manifestUrls.includes(nextUrl)) {
|
|
continue;
|
|
}
|
|
|
|
manifestUrls.push(nextUrl);
|
|
}
|
|
|
|
return manifestUrls;
|
|
}
|
|
|
|
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<DesktopSettings>;
|
|
|
|
return {
|
|
autoUpdateMode: normalizeAutoUpdateMode(parsed.autoUpdateMode),
|
|
autoStart: typeof parsed.autoStart === 'boolean'
|
|
? parsed.autoStart
|
|
: DEFAULT_DESKTOP_SETTINGS.autoStart,
|
|
closeToTray: typeof parsed.closeToTray === 'boolean'
|
|
? parsed.closeToTray
|
|
: DEFAULT_DESKTOP_SETTINGS.closeToTray,
|
|
vaapiVideoEncode: typeof parsed.vaapiVideoEncode === 'boolean'
|
|
? parsed.vaapiVideoEncode
|
|
: DEFAULT_DESKTOP_SETTINGS.vaapiVideoEncode,
|
|
hardwareAcceleration: typeof parsed.hardwareAcceleration === 'boolean'
|
|
? parsed.hardwareAcceleration
|
|
: DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration,
|
|
manifestUrls: normalizeManifestUrls(parsed.manifestUrls),
|
|
preferredVersion: normalizePreferredVersion(parsed.preferredVersion)
|
|
};
|
|
} catch {
|
|
return { ...DEFAULT_DESKTOP_SETTINGS };
|
|
}
|
|
}
|
|
|
|
export function updateDesktopSettings(patch: Partial<DesktopSettings>): DesktopSettingsSnapshot {
|
|
const mergedSettings = {
|
|
...readDesktopSettings(),
|
|
...patch
|
|
};
|
|
const nextSettings: DesktopSettings = {
|
|
autoUpdateMode: normalizeAutoUpdateMode(mergedSettings.autoUpdateMode),
|
|
autoStart: typeof mergedSettings.autoStart === 'boolean'
|
|
? mergedSettings.autoStart
|
|
: DEFAULT_DESKTOP_SETTINGS.autoStart,
|
|
closeToTray: typeof mergedSettings.closeToTray === 'boolean'
|
|
? mergedSettings.closeToTray
|
|
: DEFAULT_DESKTOP_SETTINGS.closeToTray,
|
|
hardwareAcceleration: typeof mergedSettings.hardwareAcceleration === 'boolean'
|
|
? mergedSettings.hardwareAcceleration
|
|
: DEFAULT_DESKTOP_SETTINGS.hardwareAcceleration,
|
|
manifestUrls: normalizeManifestUrls(mergedSettings.manifestUrls),
|
|
preferredVersion: normalizePreferredVersion(mergedSettings.preferredVersion),
|
|
vaapiVideoEncode: typeof mergedSettings.vaapiVideoEncode === 'boolean'
|
|
? mergedSettings.vaapiVideoEncode
|
|
: DEFAULT_DESKTOP_SETTINGS.vaapiVideoEncode
|
|
};
|
|
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');
|
|
}
|