Files
TamaASR/src/main/auto-start-service.ts
T
MeloriseandCodex 5a3aa9cfcb feat: 完成托盘设置页和悬浮球交互
实现隐藏启动、就绪状态托盘、登录提醒、后端切换、快捷键与自启动设置、九宫格和坐标微调,以及常驻悬浮球点击录音。

AI-assisted-by: Codex
AI-model: gpt-5.6-sol
AI-reasoning-effort: medium
Co-authored-by: Codex <codex@openai.com>
2026-08-23 02:34:38 +08:00

41 lines
1.2 KiB
TypeScript

import { app } from 'electron';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
function desktopEscape(value: string): string {
return `"${value.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('`', '\\`').replaceAll('$', '\\$')}"`;
}
export class AutoStartService {
private readonly filePath = path.join(os.homedir(), '.config', 'autostart', 'meloasr.desktop');
apply(enabled: boolean): void {
if (!enabled) {
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
fs.writeFileSync(this.filePath, [
'[Desktop Entry]',
'Type=Application',
'Name=MeloASR',
'Hidden=true',
''
].join('\n'), { mode: 0o644 });
return;
}
const args = app.isPackaged ? [] : [app.getAppPath()];
const command = [process.execPath, ...args].map(desktopEscape).join(' ');
const contents = [
'[Desktop Entry]',
'Type=Application',
'Name=MeloASR',
`Exec=${command}`,
'Terminal=false',
'X-GNOME-Autostart-enabled=true',
'Comment=MeloASR',
''
].join('\n');
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
fs.writeFileSync(this.filePath, contents, { mode: 0o644 });
}
}