theme-settings.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /** Theme preferences stored in the Host user-settings document. */
  2. import z from '@deepseek-ai/schemastery'
  3. /** Built-in preferences accepted at the registry and settings boundaries. */
  4. export const THEME_PREFERENCES = ['light', 'dark', 'system'] as const
  5. /** Settings namespace owned by the theme plugin. */
  6. export const THEME_SETTINGS_NAMESPACE = 'ui-theme'
  7. /** Field carrying the selected built-in theme preference. */
  8. export const THEME_PREFERENCE_FIELD = 'preference'
  9. /** Field carrying the conversation content font size. */
  10. export const FONT_SIZE_FIELD = 'fontSize'
  11. /** Theme preference persisted by the product Appearance row. */
  12. export type ThemePreference = typeof THEME_PREFERENCES[number]
  13. /** Default preference when the user-settings document has no override. */
  14. export const DEFAULT_PREFERENCE: ThemePreference = 'system'
  15. /** Smallest accepted content font size (px). */
  16. export const FONT_SIZE_MIN = 12
  17. /** Largest accepted content font size (px). */
  18. export const FONT_SIZE_MAX = 17
  19. /** Content font size when the user-settings document has no override (px). */
  20. export const DEFAULT_FONT_SIZE = 14
  21. /** Durable theme section shared by the Host schema and the browser scope. */
  22. export interface ThemeSettings {
  23. /** Selected built-in preference. */
  24. preference: ThemePreference
  25. /** Conversation content font size in px (integer within {@link FONT_SIZE_MIN}..{@link FONT_SIZE_MAX}). */
  26. fontSize: number
  27. }
  28. /** Durable theme schema; also the wire envelope the browser scope validates against. */
  29. export const ThemeSettingsSchema: z<ThemeSettings> = z.object({
  30. [THEME_PREFERENCE_FIELD]: z.union([...THEME_PREFERENCES]).default(DEFAULT_PREFERENCE),
  31. [FONT_SIZE_FIELD]: z.number().step(1).min(FONT_SIZE_MIN).max(FONT_SIZE_MAX).default(DEFAULT_FONT_SIZE),
  32. })
  33. /**
  34. * Narrow one wire or registry value to a persistable preference.
  35. * @param value - value crossing the settings or registry boundary.
  36. * @returns whether the value is a built-in preference.
  37. */
  38. export function isThemePreference(value: unknown): value is ThemePreference {
  39. return THEME_PREFERENCES.some(preference => preference === value)
  40. }