|
|
@@ -3,7 +3,7 @@ import { basename, extname, join } from 'path'
|
|
|
import { fileURLToPath } from 'url'
|
|
|
import { autoUpdater } from 'electron-updater'
|
|
|
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
|
|
|
-import { mkdirSync, appendFileSync, existsSync, writeFileSync } from 'fs'
|
|
|
+import { mkdirSync, appendFileSync, existsSync, readFileSync, writeFileSync } from 'fs'
|
|
|
import { writeFile } from 'fs/promises'
|
|
|
// --- 日志管理 ---
|
|
|
let logDir: string = ''
|
|
|
@@ -61,6 +61,60 @@ function isHttpUrl(url: string): boolean {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+const STARTUP_PREFERENCE_FILE = 'startup-preference.json'
|
|
|
+
|
|
|
+interface StartupPreference {
|
|
|
+ openAtLogin: boolean
|
|
|
+}
|
|
|
+
|
|
|
+function getStartupPreferencePath(): string {
|
|
|
+ return join(app.getPath('userData'), STARTUP_PREFERENCE_FILE)
|
|
|
+}
|
|
|
+
|
|
|
+function readStartupPreference(): { openAtLogin: boolean; fileExists: boolean } {
|
|
|
+ const p = getStartupPreferencePath()
|
|
|
+ if (!existsSync(p)) {
|
|
|
+ return { openAtLogin: true, fileExists: false }
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ const raw = readFileSync(p, 'utf-8')
|
|
|
+ const j = JSON.parse(raw) as Partial<StartupPreference>
|
|
|
+ return {
|
|
|
+ openAtLogin: typeof j.openAtLogin === 'boolean' ? j.openAtLogin : true,
|
|
|
+ fileExists: true
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ return { openAtLogin: true, fileExists: true }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function writeStartupPreference(openAtLogin: boolean): void {
|
|
|
+ try {
|
|
|
+ const payload: StartupPreference = { openAtLogin }
|
|
|
+ writeFileSync(getStartupPreferencePath(), JSON.stringify(payload, null, 0), 'utf-8')
|
|
|
+ } catch (e) {
|
|
|
+ console.error('writeStartupPreference', e)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function applyOpenAtLoginToOS(openAtLogin: boolean): void {
|
|
|
+ if (!app.isPackaged) return
|
|
|
+ app.setLoginItemSettings({
|
|
|
+ openAtLogin,
|
|
|
+ path: app.getPath('exe')
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function initStartupFromPreference(): void {
|
|
|
+ const { openAtLogin, fileExists } = readStartupPreference()
|
|
|
+ if (!fileExists) {
|
|
|
+ writeStartupPreference(true)
|
|
|
+ applyOpenAtLoginToOS(true)
|
|
|
+ } else {
|
|
|
+ applyOpenAtLoginToOS(openAtLogin)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
function sanitizeFilename(name: string): string {
|
|
|
return name.replace(/[<>:"/\\|?*\x00-\x1f]/g, '_').slice(0, 120) || 'file'
|
|
|
}
|
|
|
@@ -877,6 +931,7 @@ app.on('second-instance', () => {
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
electronApp.setAppUserModelId('com.hnyunzhu.im')
|
|
|
+ initStartupFromPreference()
|
|
|
app.on('browser-window-created', (_, window) => {
|
|
|
optimizer.watchWindowShortcuts(window)
|
|
|
})
|
|
|
@@ -885,6 +940,24 @@ app.whenReady().then(() => {
|
|
|
|
|
|
ipcMain.handle('get-app-version', () => app.getVersion())
|
|
|
|
|
|
+ ipcMain.handle('get-open-at-login', () => {
|
|
|
+ const isPackaged = app.isPackaged
|
|
|
+ if (isPackaged) {
|
|
|
+ return {
|
|
|
+ openAtLogin: app.getLoginItemSettings().openAtLogin,
|
|
|
+ isPackaged: true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return { openAtLogin: readStartupPreference().openAtLogin, isPackaged: false }
|
|
|
+ })
|
|
|
+
|
|
|
+ ipcMain.handle('set-open-at-login', (_, enabled: unknown) => {
|
|
|
+ const on = enabled === true
|
|
|
+ writeStartupPreference(on)
|
|
|
+ applyOpenAtLoginToOS(on)
|
|
|
+ return on
|
|
|
+ })
|
|
|
+
|
|
|
// 生产环境自动更新(自建服务器)
|
|
|
if (!is.dev) {
|
|
|
autoUpdater.checkForUpdatesAndNotify()
|