config.handlers.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { ipcMain, dialog, BrowserWindow } from 'electron'
  2. import { IpcChannels } from '@shared/ipc-channels'
  3. import {
  4. getConfiguredWorkshopPath,
  5. setConfiguredWorkshopPath,
  6. isWorkshopPathConfigured,
  7. getDefaultFps,
  8. setDefaultFps,
  9. getLweRepoUrl,
  10. getLweRepoBranch,
  11. setLweRepo,
  12. getLweCmakeArgs,
  13. setLweCmakeArgs,
  14. importWEConfigFile,
  15. createFreshConfig,
  16. getConfiguredBackupPath,
  17. setConfiguredBackupPath,
  18. isBackupPathConfigured,
  19. getAutoUnsubscribeAfterBackup,
  20. setAutoUnsubscribeAfterBackup,
  21. getTrayEnabled,
  22. setTrayEnabled,
  23. getAutostartEnabled,
  24. getAutostartMinimized,
  25. setAutostart,
  26. getAutostartPlaylistId,
  27. setAutostartPlaylistId,
  28. getKillLweOnQuit,
  29. setKillLweOnQuit
  30. } from '../services/config.service'
  31. import { getDefaultWorkshopPath } from '../utils/paths'
  32. import { DEFAULT_LWE_REPO } from '@shared/constants'
  33. import { restartWatcher } from '../services/watcher.service'
  34. import { importWEConfig } from '../services/library.service'
  35. import { createTray, destroyTray } from '../services/tray.service'
  36. import { isAutostartSupported, setAutostartEnabled } from '../services/autostart.service'
  37. export function registerConfigHandlers(): void {
  38. ipcMain.handle(IpcChannels.CONFIG_GET, () => ({
  39. workshopPath: getConfiguredWorkshopPath(),
  40. defaultWorkshopPath: getDefaultWorkshopPath(),
  41. isConfigured: isWorkshopPathConfigured(),
  42. defaultFps: getDefaultFps(),
  43. lweRepoUrl: getLweRepoUrl(),
  44. lweRepoBranch: getLweRepoBranch(),
  45. lweCmakeArgs: getLweCmakeArgs(),
  46. defaultLweRepoUrl: DEFAULT_LWE_REPO,
  47. backupPath: getConfiguredBackupPath(),
  48. isBackupConfigured: isBackupPathConfigured(),
  49. autoUnsubscribeAfterBackup: getAutoUnsubscribeAfterBackup(),
  50. trayEnabled: getTrayEnabled(),
  51. autostartSupported: isAutostartSupported(),
  52. autostartEnabled: getAutostartEnabled(),
  53. autostartMinimized: getAutostartMinimized(),
  54. autostartPlaylistId: getAutostartPlaylistId(),
  55. killLweOnQuit: getKillLweOnQuit()
  56. }))
  57. ipcMain.handle(IpcChannels.CONFIG_GET_AUTOSTART_SUPPORTED, () => isAutostartSupported())
  58. ipcMain.handle(IpcChannels.CONFIG_SET_TRAY_ENABLED, (_e, enabled: boolean) => {
  59. setTrayEnabled(enabled)
  60. if (enabled) {
  61. const win = BrowserWindow.getAllWindows()[0]
  62. if (win) createTray(win)
  63. } else {
  64. destroyTray()
  65. }
  66. return { ok: true }
  67. })
  68. ipcMain.handle(
  69. IpcChannels.CONFIG_SET_AUTOSTART,
  70. (_e, enabled: boolean, minimized: boolean, playlistId: string | null) => {
  71. setAutostart(enabled, minimized)
  72. setAutostartPlaylistId(playlistId)
  73. setAutostartEnabled(enabled, minimized)
  74. return { ok: true }
  75. }
  76. )
  77. ipcMain.handle(IpcChannels.CONFIG_SET_KILL_LWE_ON_QUIT, (_e, enabled: boolean) => {
  78. setKillLweOnQuit(enabled)
  79. return { ok: true }
  80. })
  81. ipcMain.handle(IpcChannels.CONFIG_SET_BACKUP_PATH, (_e, newPath: string) => {
  82. setConfiguredBackupPath(newPath)
  83. return { ok: true }
  84. })
  85. ipcMain.handle(IpcChannels.CONFIG_SET_AUTO_UNSUBSCRIBE, (_e, enabled: boolean) => {
  86. setAutoUnsubscribeAfterBackup(enabled)
  87. return { ok: true }
  88. })
  89. ipcMain.handle(IpcChannels.CONFIG_SET_DEFAULT_FPS, (_e, fps: number | null) => {
  90. setDefaultFps(fps)
  91. return { ok: true }
  92. })
  93. ipcMain.handle(IpcChannels.CONFIG_SET_LWE_REPO, (_e, url: string | null, branch: string | null) => {
  94. setLweRepo(url, branch)
  95. return { ok: true }
  96. })
  97. ipcMain.handle(IpcChannels.CONFIG_SET_LWE_CMAKE_ARGS, (_e, args: string | null) => {
  98. setLweCmakeArgs(args)
  99. return { ok: true }
  100. })
  101. ipcMain.handle(IpcChannels.CONFIG_SET_WORKSHOP_PATH, (_e, newPath: string) => {
  102. setConfiguredWorkshopPath(newPath)
  103. restartWatcher()
  104. return { ok: true }
  105. })
  106. ipcMain.handle(IpcChannels.CONFIG_PICK_FOLDER, async (_e) => {
  107. const win = BrowserWindow.getFocusedWindow()
  108. const result = await dialog.showOpenDialog(win!, {
  109. properties: ['openDirectory'],
  110. title: 'Select Wallpaper Engine Workshop folder'
  111. })
  112. if (result.canceled || result.filePaths.length === 0) return null
  113. return result.filePaths[0]
  114. })
  115. ipcMain.handle(IpcChannels.CONFIG_PICK_FILE, async (_e) => {
  116. const win = BrowserWindow.getFocusedWindow()
  117. const result = await dialog.showOpenDialog(win!, {
  118. properties: ['openFile'],
  119. title: 'Select Wallpaper Engine config.json',
  120. filters: [{ name: 'JSON', extensions: ['json'] }]
  121. })
  122. if (result.canceled || result.filePaths.length === 0) return null
  123. return result.filePaths[0]
  124. })
  125. ipcMain.handle(IpcChannels.CONFIG_IMPORT_WE, (_e, sourcePath: string) => {
  126. // 1. Copy + fix paths
  127. const destPath = importWEConfigFile(sourcePath)
  128. // 2. Import folders/playlists into our library store
  129. const result = importWEConfig(destPath)
  130. return { ...result, configPath: destPath }
  131. })
  132. ipcMain.handle(IpcChannels.CONFIG_CREATE_FRESH, () => {
  133. const destPath = createFreshConfig()
  134. return { configPath: destPath }
  135. })
  136. }