platform.mjs 1.7 KB

12345678910111213141516171819202122232425262728293031
  1. /** Platform policy is shared by startup, native menus and the renderer bridge. */
  2. export function desktopPlatform(platform = process.platform) {
  3. return Object.freeze({ platform, server: platform === 'darwin',
  4. supportedModes: platform === 'darwin' ? ['client', 'server'] : ['client'],
  5. computer: ['darwin', 'win32'].includes(platform), windows: platform === 'win32' })
  6. }
  7. export function loginItemOptions(platform = process.platform, executable = process.execPath) {
  8. return platform === 'win32' ? { path: executable, args: ['--yaoyao-login'] } : {}
  9. }
  10. export function desktopMenu(template, platform = process.platform) {
  11. if (platform === 'darwin') return template
  12. const excluded = new Set(['desktop-local-vm', 'desktop-sync-notice', 'desktop-sync-retry',
  13. 'desktop-sync-force', 'desktop-stop-and-quit', 'desktop-host-use-local', 'desktop-host-ask'])
  14. const result = []
  15. for (const item of template) {
  16. if (excluded.has(item.id) || item.label === '执行节点' || ['hide', 'hideOthers', 'unhide'].includes(item.role)) continue
  17. const next = { ...item }
  18. if (next.submenu) next.submenu = desktopMenu(next.submenu, platform)
  19. if (next.id === 'desktop-login') next.label = '登录 Windows 时启动'
  20. if (next.id === 'desktop-background') next.label = '登录启动时仅驻留托盘'
  21. if (next.id === 'desktop-service-status') next.label = '尚未连接服务器'
  22. if (next.label === '退出夭夭(后台继续运行)') next.label = '退出夭夭'
  23. if (next.label === '停止后台服务并退出') continue
  24. if (next.type === 'separator' && (!result.length || result.at(-1).type === 'separator')) continue
  25. result.push(next)
  26. }
  27. if (result.at(-1)?.type === 'separator') result.pop()
  28. return result
  29. }