mirror of
https://github.com/hicccc77/WeFlow.git
synced 2026-04-03 15:08:25 +00:00
134
electron/main.ts
134
electron/main.ts
@@ -171,6 +171,118 @@ const AUTO_UPDATE_ENABLED =
|
||||
process.env.AUTO_UPDATE_ENABLED === '1' ||
|
||||
(process.env.AUTO_UPDATE_ENABLED == null && !process.env.VITE_DEV_SERVER_URL)
|
||||
|
||||
const getLaunchAtStartupUnsupportedReason = (): string | null => {
|
||||
if (process.platform !== 'win32' && process.platform !== 'darwin') {
|
||||
return '当前平台暂不支持开机自启动'
|
||||
}
|
||||
if (!app.isPackaged) {
|
||||
return '仅安装后的 Windows / macOS 版本支持开机自启动'
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
const isLaunchAtStartupSupported = (): boolean => getLaunchAtStartupUnsupportedReason() == null
|
||||
|
||||
const getStoredLaunchAtStartupPreference = (): boolean | undefined => {
|
||||
const value = configService?.get('launchAtStartup')
|
||||
return typeof value === 'boolean' ? value : undefined
|
||||
}
|
||||
|
||||
const getSystemLaunchAtStartup = (): boolean => {
|
||||
if (!isLaunchAtStartupSupported()) return false
|
||||
try {
|
||||
return app.getLoginItemSettings().openAtLogin === true
|
||||
} catch (error) {
|
||||
console.error('[WeFlow] 读取开机自启动状态失败:', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
const buildLaunchAtStartupSettings = (enabled: boolean): Parameters<typeof app.setLoginItemSettings>[0] =>
|
||||
process.platform === 'win32'
|
||||
? { openAtLogin: enabled, path: process.execPath }
|
||||
: { openAtLogin: enabled }
|
||||
|
||||
const setSystemLaunchAtStartup = (enabled: boolean): { success: boolean; enabled: boolean; error?: string } => {
|
||||
try {
|
||||
app.setLoginItemSettings(buildLaunchAtStartupSettings(enabled))
|
||||
const effectiveEnabled = app.getLoginItemSettings().openAtLogin === true
|
||||
if (effectiveEnabled !== enabled) {
|
||||
return {
|
||||
success: false,
|
||||
enabled: effectiveEnabled,
|
||||
error: '系统未接受该开机自启动设置'
|
||||
}
|
||||
}
|
||||
return { success: true, enabled: effectiveEnabled }
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
enabled: getSystemLaunchAtStartup(),
|
||||
error: `设置开机自启动失败: ${String((error as Error)?.message || error)}`
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const getLaunchAtStartupStatus = (): { enabled: boolean; supported: boolean; reason?: string } => {
|
||||
const unsupportedReason = getLaunchAtStartupUnsupportedReason()
|
||||
if (unsupportedReason) {
|
||||
return {
|
||||
enabled: getStoredLaunchAtStartupPreference() === true,
|
||||
supported: false,
|
||||
reason: unsupportedReason
|
||||
}
|
||||
}
|
||||
return {
|
||||
enabled: getSystemLaunchAtStartup(),
|
||||
supported: true
|
||||
}
|
||||
}
|
||||
|
||||
const applyLaunchAtStartupPreference = (
|
||||
enabled: boolean
|
||||
): { success: boolean; enabled: boolean; supported: boolean; reason?: string; error?: string } => {
|
||||
const unsupportedReason = getLaunchAtStartupUnsupportedReason()
|
||||
if (unsupportedReason) {
|
||||
return {
|
||||
success: false,
|
||||
enabled: getStoredLaunchAtStartupPreference() === true,
|
||||
supported: false,
|
||||
reason: unsupportedReason
|
||||
}
|
||||
}
|
||||
|
||||
const result = setSystemLaunchAtStartup(enabled)
|
||||
configService?.set('launchAtStartup', result.enabled)
|
||||
return {
|
||||
...result,
|
||||
supported: true
|
||||
}
|
||||
}
|
||||
|
||||
const syncLaunchAtStartupPreference = () => {
|
||||
if (!configService) return
|
||||
|
||||
const unsupportedReason = getLaunchAtStartupUnsupportedReason()
|
||||
if (unsupportedReason) return
|
||||
|
||||
const storedPreference = getStoredLaunchAtStartupPreference()
|
||||
const systemEnabled = getSystemLaunchAtStartup()
|
||||
|
||||
if (typeof storedPreference !== 'boolean') {
|
||||
configService.set('launchAtStartup', systemEnabled)
|
||||
return
|
||||
}
|
||||
|
||||
if (storedPreference === systemEnabled) return
|
||||
|
||||
const result = setSystemLaunchAtStartup(storedPreference)
|
||||
configService.set('launchAtStartup', result.enabled)
|
||||
if (!result.success && result.error) {
|
||||
console.error('[WeFlow] 同步开机自启动设置失败:', result.error)
|
||||
}
|
||||
}
|
||||
|
||||
// 使用白名单过滤 PATH,避免被第三方目录中的旧版 VC++ 运行库劫持。
|
||||
// 仅保留系统目录(Windows/System32/SysWOW64)和应用自身目录(可执行目录、resources)。
|
||||
function sanitizePathEnv() {
|
||||
@@ -1250,7 +1362,12 @@ function registerIpcHandlers() {
|
||||
})
|
||||
|
||||
ipcMain.handle('config:set', async (_, key: string, value: any) => {
|
||||
const result = configService?.set(key as any, value)
|
||||
let result: unknown
|
||||
if (key === 'launchAtStartup') {
|
||||
result = applyLaunchAtStartupPreference(value === true)
|
||||
} else {
|
||||
result = configService?.set(key as any, value)
|
||||
}
|
||||
if (key === 'updateChannel') {
|
||||
applyAutoUpdateChannel('settings')
|
||||
}
|
||||
@@ -1259,6 +1376,12 @@ function registerIpcHandlers() {
|
||||
})
|
||||
|
||||
ipcMain.handle('config:clear', async () => {
|
||||
if (isLaunchAtStartupSupported() && getSystemLaunchAtStartup()) {
|
||||
const result = setSystemLaunchAtStartup(false)
|
||||
if (!result.success && result.error) {
|
||||
console.error('[WeFlow] 清空配置时关闭开机自启动失败:', result.error)
|
||||
}
|
||||
}
|
||||
configService?.clear()
|
||||
messagePushService.handleConfigCleared()
|
||||
return true
|
||||
@@ -1301,6 +1424,14 @@ function registerIpcHandlers() {
|
||||
return app.getVersion()
|
||||
})
|
||||
|
||||
ipcMain.handle('app:getLaunchAtStartupStatus', async () => {
|
||||
return getLaunchAtStartupStatus()
|
||||
})
|
||||
|
||||
ipcMain.handle('app:setLaunchAtStartup', async (_, enabled: boolean) => {
|
||||
return applyLaunchAtStartupPreference(enabled === true)
|
||||
})
|
||||
|
||||
ipcMain.handle('app:checkWayland', async () => {
|
||||
if (process.platform !== 'linux') return false;
|
||||
|
||||
@@ -2881,6 +3012,7 @@ app.whenReady().then(async () => {
|
||||
updateSplashProgress(5, '正在加载配置...')
|
||||
configService = new ConfigService()
|
||||
applyAutoUpdateChannel('startup')
|
||||
syncLaunchAtStartupPreference()
|
||||
|
||||
// 将用户主题配置推送给 Splash 窗口
|
||||
if (splashWindow && !splashWindow.isDestroyed()) {
|
||||
|
||||
Reference in New Issue
Block a user