wallpaper-state.service.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { openStore } from '../utils/safeStore'
  2. import type { ScreenTarget } from '@shared/types'
  3. interface StoreSchema {
  4. /** Written by older versions, read once as the '*' assignment */
  5. activeWallpaperId?: string
  6. assignments?: Record<ScreenTarget, string>
  7. }
  8. const store = openStore<StoreSchema>()
  9. export function getAssignments(): Record<ScreenTarget, string> {
  10. const assignments = store.get('assignments')
  11. if (assignments) return assignments
  12. const legacy = store.get('activeWallpaperId')
  13. return legacy ? { '*': legacy } : {}
  14. }
  15. export function setAssignment(screen: ScreenTarget, wallpaperId: string): void {
  16. const next = screen === '*' ? {} : { ...getAssignments() }
  17. if (screen !== '*') delete next['*']
  18. next[screen] = wallpaperId
  19. store.set('assignments', next)
  20. }
  21. export function clearAssignment(screen?: ScreenTarget): void {
  22. if (screen === undefined) {
  23. store.set('assignments', {})
  24. return
  25. }
  26. const next = { ...getAssignments() }
  27. delete next[screen]
  28. store.set('assignments', next)
  29. }
  30. /** The wallpaper on the first screen, for callers that only care about one */
  31. export function getActiveWallpaperId(): string | undefined {
  32. return Object.values(getAssignments())[0]
  33. }