EngineFlagsSection.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { useState } from 'react'
  2. import { Gauge, TriangleAlert } from 'lucide-react'
  3. import type { EngineFlags, FullscreenPauseMode, WallpaperMeta } from '@shared/types'
  4. import { hasEngineFlags } from '@shared/engineFlags'
  5. import { BOOLEAN_FLAGS, CHOICE_FLAGS, FULLSCREEN_PAUSE_OPTIONS, optionLabel, presetName } from '../../../constants/engineFlags'
  6. import { useConfig } from '../../../hooks/queries'
  7. import { t } from '../../../i18n'
  8. import SidebarSection, { CustomBadge } from './SidebarSection'
  9. interface Props {
  10. flags?: EngineFlags
  11. onChange: (patch: Pick<WallpaperMeta, 'engineFlags' | 'fpsOverride'>) => Promise<void>
  12. }
  13. const selectClass = 'rounded bg-fg/5 px-1.5 py-1 text-xs text-gray-200 outline-none disabled:opacity-50'
  14. const onOff = (value: boolean | undefined) => (value ? 'on' : 'off')
  15. const onOffLabel = (value: boolean | undefined) => (value ? t('common.on') : t('common.off'))
  16. export default function EngineFlagsSection({ flags = {}, onChange }: Props) {
  17. const [busy, setBusy] = useState(false)
  18. const config = useConfig()
  19. const global = config?.engineFlags ?? {}
  20. const presets = config?.engineFlagPresets ?? []
  21. async function commit(patch: Pick<WallpaperMeta, 'engineFlags' | 'fpsOverride'>) {
  22. setBusy(true)
  23. try {
  24. await onChange(patch)
  25. } finally {
  26. setBusy(false)
  27. }
  28. }
  29. function setFlag<K extends keyof EngineFlags>(key: K, value: EngineFlags[K] | undefined) {
  30. const next = { ...flags, [key]: value }
  31. if (value === undefined) delete next[key]
  32. void commit({ engineFlags: hasEngineFlags(next) ? next : undefined })
  33. }
  34. function applyPreset(id: string) {
  35. const preset = presets.find((p) => p.id === id)
  36. if (!preset) return
  37. const patch: Pick<WallpaperMeta, 'engineFlags' | 'fpsOverride'> = { engineFlags: { ...flags, ...preset.flags } }
  38. if (preset.fps !== undefined) patch.fpsOverride = preset.fps ?? undefined
  39. void commit(patch)
  40. }
  41. return (
  42. <SidebarSection icon={Gauge} title={t('perf.title')} busy={busy} badge={hasEngineFlags(flags) && <CustomBadge label={t('thumb.customBadge')} />}>
  43. <div className="mt-2 flex flex-col gap-2">
  44. <p className="text-[11px] text-gray-600">{t('perf.overrideHint')}</p>
  45. <div className="flex items-center gap-2">
  46. <select
  47. value=""
  48. disabled={busy}
  49. onChange={(e) => { applyPreset(e.target.value) }}
  50. className={`${selectClass} flex-1`}
  51. >
  52. <option value="" disabled>
  53. {t('perf.applyPreset')}
  54. </option>
  55. {presets.map((p) => (
  56. <option key={p.id} value={p.id}>
  57. {presetName(p)}
  58. </option>
  59. ))}
  60. </select>
  61. {hasEngineFlags(flags) && (
  62. <button
  63. onClick={() => void commit({ engineFlags: undefined })}
  64. disabled={busy}
  65. className="rounded bg-fg/5 px-2 py-1 text-xs text-gray-400 hover:bg-fg/10 disabled:opacity-50"
  66. >
  67. {t('common.reset')}
  68. </button>
  69. )}
  70. </div>
  71. {BOOLEAN_FLAGS.map(({ key, label, hint }) => (
  72. <label key={key} className="flex items-center justify-between gap-2 text-xs text-gray-300" title={hint && t(hint)}>
  73. <span>{t(label)}</span>
  74. <select
  75. value={flags[key] === undefined ? 'inherit' : onOff(flags[key])}
  76. disabled={busy}
  77. onChange={(e) => { setFlag(key, e.target.value === 'inherit' ? undefined : e.target.value === 'on') }}
  78. className={selectClass}
  79. >
  80. <option value="inherit">{t('common.defaultValue', { value: onOffLabel(global[key]) })}</option>
  81. <option value="on">{t('common.on')}</option>
  82. <option value="off">{t('common.off')}</option>
  83. </select>
  84. </label>
  85. ))}
  86. {CHOICE_FLAGS.map(({ key, label, hint, wallpaperNote, fallback, options }) => (
  87. <div key={key}>
  88. <label className="flex items-center justify-between gap-2 text-xs text-gray-300" title={t(hint)}>
  89. <span>{t(label)}</span>
  90. <select
  91. value={flags[key] ?? 'inherit'}
  92. disabled={busy}
  93. onChange={(e) => { setFlag(key, e.target.value === 'inherit' ? undefined : (e.target.value as EngineFlags[typeof key])) }
  94. }
  95. className={selectClass}
  96. >
  97. <option value="inherit">
  98. {t('common.defaultValue', {
  99. value: optionLabel(options.find((o) => o.value === (global[key] ?? fallback))?.label ?? '')
  100. })}
  101. </option>
  102. {options.map((o) => (
  103. <option key={o.value} value={o.value}>
  104. {optionLabel(o.label)}
  105. </option>
  106. ))}
  107. </select>
  108. </label>
  109. {wallpaperNote && flags[key] !== undefined && flags[key] !== fallback && (
  110. <p className="mt-1 flex gap-1.5 text-[11px] leading-snug text-amber-300">
  111. <TriangleAlert size={12} className="mt-px shrink-0" />
  112. {t(wallpaperNote)}
  113. </p>
  114. )}
  115. </div>
  116. ))}
  117. <label className="flex flex-col gap-1 text-xs text-gray-300">
  118. <span>{t('perf.fullscreenApps')}</span>
  119. <select
  120. value={flags.fullscreenPause ?? 'inherit'}
  121. disabled={busy}
  122. onChange={(e) => { setFlag('fullscreenPause', e.target.value === 'inherit' ? undefined : (e.target.value as FullscreenPauseMode)) }
  123. }
  124. className={selectClass}
  125. >
  126. <option value="inherit">
  127. {t('common.defaultValue', {
  128. value: t(FULLSCREEN_PAUSE_OPTIONS.find((o) => o.value === (global.fullscreenPause ?? 'default'))!.label)
  129. })}
  130. </option>
  131. {FULLSCREEN_PAUSE_OPTIONS.map((o) => (
  132. <option key={o.value} value={o.value}>
  133. {t(o.label)}
  134. </option>
  135. ))}
  136. </select>
  137. </label>
  138. </div>
  139. </SidebarSection>
  140. )
  141. }