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