settingsCodePreview.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. import type { Theme } from "@/useTheme.js";
  2. import { useState } from "react";
  3. import { resolveTheme } from "@/useTheme.js";
  4. import { Card, CardContent } from "@/components/ui/card.js";
  5. import { Input } from "@/components/ui/input.js";
  6. import { Switch } from "@/components/ui/switch.js";
  7. import {
  8. Select,
  9. SelectContent,
  10. SelectItem,
  11. SelectTrigger,
  12. SelectValue,
  13. } from "@/components/ui/select.js";
  14. import {
  15. getThemeOptionLabel,
  16. SettingsRow,
  17. ThemePreviewCard,
  18. ThemeSelect,
  19. } from "@/settings/SettingsPageParts.js";
  20. import { getCodePreviewTheme } from "@/lib/codePreviewPreferences.js";
  21. import { useZCodeIntl } from "@/i18n/IntlProvider.js";
  22. import type { CodePreviewSettings } from "@/store/index.js";
  23. import { THEME_MODES } from "@/settings/settingsPageConfig.js";
  24. import { BUILTIN_SKIN_MANIFESTS, getSkinManifest } from "@/skins/registry.js";
  25. import { MAX_UI_FONT_SIZE_PX, MIN_UI_FONT_SIZE_PX } from "@/lib/uiFontSize.js";
  26. function FontSizeInput({
  27. value,
  28. min,
  29. max,
  30. ariaLabel,
  31. onChange,
  32. disabled = false,
  33. }: {
  34. value: number;
  35. min: number;
  36. max: number;
  37. ariaLabel: string;
  38. onChange: (value: number) => void;
  39. disabled?: boolean;
  40. }) {
  41. const [draft, setDraft] = useState(String(value));
  42. const commit = () => {
  43. const parsed = draft.trim() === "" ? Number.NaN : Number(draft);
  44. const nextValue = Number.isFinite(parsed)
  45. ? Math.min(max, Math.max(min, Math.round(parsed)))
  46. : value;
  47. setDraft(String(nextValue));
  48. if (nextValue !== value) {
  49. onChange(nextValue);
  50. }
  51. };
  52. return (
  53. <div className="relative w-28">
  54. <Input
  55. type="number"
  56. inputMode="numeric"
  57. min={min}
  58. max={max}
  59. step={1}
  60. value={draft}
  61. disabled={disabled}
  62. aria-label={ariaLabel}
  63. onChange={(event) => setDraft(event.currentTarget.value)}
  64. onBlur={commit}
  65. onKeyDown={(event) => {
  66. if (event.key === "Enter") {
  67. event.currentTarget.blur();
  68. } else if (event.key === "Escape") {
  69. event.preventDefault();
  70. setDraft(String(value));
  71. }
  72. }}
  73. className="pr-8 text-right tabular-nums [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
  74. />
  75. <span className="pointer-events-none absolute inset-y-0 right-2 flex items-center text-ui-lg text-foreground-subtle">
  76. px
  77. </span>
  78. </div>
  79. );
  80. }
  81. export function AppearanceSectionContent({
  82. codePreviewSettings,
  83. setCodePreviewSettings,
  84. theme,
  85. setTheme,
  86. skinId,
  87. setSkin,
  88. uiFontSizePx,
  89. setUiFontSizePx,
  90. isMacDesktop,
  91. fontSmoothingEnabled,
  92. setFontSmoothingEnabled,
  93. }: {
  94. codePreviewSettings: CodePreviewSettings;
  95. setCodePreviewSettings: (settings: Partial<CodePreviewSettings>) => void;
  96. theme: Theme;
  97. setTheme: (theme: Theme) => void;
  98. skinId: string | null;
  99. setSkin: (skinId: string | null) => void;
  100. isMacDesktop: boolean;
  101. fontSmoothingEnabled: boolean;
  102. setFontSmoothingEnabled: (enabled: boolean) => void;
  103. uiFontSizePx: number;
  104. setUiFontSizePx: (fontSizePx: number) => void;
  105. }) {
  106. const { intl } = useZCodeIntl();
  107. const activePreviewMode = resolveTheme(theme);
  108. // fontGrid 锁定(A4):激活皮肤声明 12px 像素网格时,字号控件禁用并说明原因。
  109. // 运行时的 inline 值钳制在 applySkin 内,这里只负责 UI 禁用与解释。
  110. const fontGridLock =
  111. getSkinManifest(skinId)?.tokens.fontGrid?.lockUiFontSize ?? null;
  112. return (
  113. <>
  114. <div className="min-w-0 space-y-3">
  115. <div>
  116. <h3 className="text-ui-lg font-semibold text-foreground">
  117. {intl.formatMessage({ id: "settings.appearance.interfaceTitle" })}
  118. </h3>
  119. <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
  120. {intl.formatMessage({
  121. id: "settings.appearance.interfaceDescription",
  122. })}
  123. </p>
  124. </div>
  125. <Card className="border border-border bg-card py-0 shadow-none">
  126. <CardContent className="space-y-0 px-0">
  127. <SettingsRow
  128. searchId="settings.themeMode"
  129. label={intl.formatMessage({ id: "settings.themeMode" })}
  130. description={intl.formatMessage({
  131. id: "settings.themeModeDescription",
  132. })}
  133. control={
  134. <Select value={theme} onValueChange={(value) => setTheme(value as Theme)}>
  135. <SelectTrigger size="lg" className="w-[260px] min-w-0 justify-between">
  136. <SelectValue />
  137. </SelectTrigger>
  138. <SelectContent>
  139. {THEME_MODES.map(({ mode, icon: Icon }) => (
  140. <SelectItem key={mode} value={mode}>
  141. <div className="flex items-center gap-2">
  142. <Icon className="size-4" />
  143. {intl.formatMessage({
  144. id: `settings.themeMode.${mode}`,
  145. })}
  146. </div>
  147. </SelectItem>
  148. ))}
  149. </SelectContent>
  150. </Select>
  151. }
  152. />
  153. {BUILTIN_SKIN_MANIFESTS.length > 0 ? (
  154. <SettingsRow
  155. label={intl.formatMessage({ id: "settings.skin.label" })}
  156. description={intl.formatMessage({
  157. id: "settings.skin.description",
  158. })}
  159. control={
  160. <Select
  161. value={skinId ?? "none"}
  162. onValueChange={(value) => setSkin(value === "none" ? null : value)}
  163. >
  164. <SelectTrigger size="lg" className="w-[260px] min-w-0 justify-between">
  165. <SelectValue />
  166. </SelectTrigger>
  167. <SelectContent>
  168. {/* Radix SelectItem 禁止空字符串 value(会直接抛错),用 "none" 哨兵表示无皮肤。 */}
  169. <SelectItem value="none">
  170. {intl.formatMessage({ id: "settings.skin.default" })}
  171. </SelectItem>
  172. {BUILTIN_SKIN_MANIFESTS.map((manifest) => (
  173. <SelectItem key={manifest.id} value={manifest.id}>
  174. {intl.formatMessage({ id: manifest.i18nName })}
  175. </SelectItem>
  176. ))}
  177. </SelectContent>
  178. </Select>
  179. }
  180. />
  181. ) : null}
  182. <SettingsRow
  183. searchId="settings.uiFontSize"
  184. label={intl.formatMessage({ id: "settings.uiFontSize" })}
  185. description={intl.formatMessage(
  186. { id: fontGridLock !== null ? "settings.skin.fontGridLock" : "settings.uiFontSizeDescription" },
  187. fontGridLock !== null ? { size: fontGridLock } : undefined,
  188. )}
  189. control={
  190. <FontSizeInput
  191. key={uiFontSizePx}
  192. disabled={fontGridLock !== null}
  193. min={MIN_UI_FONT_SIZE_PX}
  194. max={MAX_UI_FONT_SIZE_PX}
  195. value={uiFontSizePx}
  196. onChange={setUiFontSizePx}
  197. ariaLabel={intl.formatMessage({ id: "settings.uiFontSize" })}
  198. />
  199. }
  200. />
  201. {isMacDesktop ? (
  202. <SettingsRow
  203. searchId="settings.fontSmoothing"
  204. label={intl.formatMessage({ id: "settings.fontSmoothing" })}
  205. description={intl.formatMessage({ id: "settings.fontSmoothingDescription" })}
  206. control={
  207. <Switch
  208. data-testid="font-smoothing-switch"
  209. aria-label={intl.formatMessage({ id: "settings.fontSmoothing" })}
  210. checked={fontSmoothingEnabled}
  211. onCheckedChange={setFontSmoothingEnabled}
  212. />
  213. }
  214. />
  215. ) : null}
  216. </CardContent>
  217. </Card>
  218. </div>
  219. <div className="space-y-6">
  220. <div className="min-w-0 space-y-3">
  221. <div>
  222. <h3 className="text-ui-lg font-semibold text-foreground">
  223. {intl.formatMessage({ id: "settings.appearance.codeTitle" })}
  224. </h3>
  225. <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
  226. {intl.formatMessage({
  227. id: "settings.appearance.codeDescription",
  228. })}
  229. </p>
  230. </div>
  231. <Card className="border border-border bg-card py-0 shadow-none [&_[data-slot=select-trigger]]:w-full">
  232. <CardContent className="space-y-0 px-0">
  233. <SettingsRow
  234. searchId="settings.lightTheme"
  235. label={intl.formatMessage({ id: "settings.lightTheme" })}
  236. description={intl.formatMessage({
  237. id: "settings.lightThemeDescription",
  238. })}
  239. control={
  240. <ThemeSelect
  241. value={codePreviewSettings.lightTheme}
  242. onValueChange={(value) => setCodePreviewSettings({ lightTheme: value })}
  243. />
  244. }
  245. />
  246. <SettingsRow
  247. searchId="settings.darkTheme"
  248. label={intl.formatMessage({ id: "settings.darkTheme" })}
  249. description={intl.formatMessage({
  250. id: "settings.darkThemeDescription",
  251. })}
  252. control={
  253. <ThemeSelect
  254. value={codePreviewSettings.darkTheme}
  255. onValueChange={(value) => setCodePreviewSettings({ darkTheme: value })}
  256. />
  257. }
  258. />
  259. <SettingsRow
  260. searchId="settings.showLineNumbers"
  261. label={intl.formatMessage({ id: "settings.showLineNumbers" })}
  262. description={intl.formatMessage({
  263. id: "settings.showLineNumbersDescription",
  264. })}
  265. control={
  266. <Switch
  267. checked={codePreviewSettings.showLineNumbers}
  268. onCheckedChange={(checked) =>
  269. setCodePreviewSettings({ showLineNumbers: checked })
  270. }
  271. />
  272. }
  273. />
  274. <SettingsRow
  275. searchId="settings.wrapLongLines"
  276. label={intl.formatMessage({ id: "settings.wrapLongLines" })}
  277. description={intl.formatMessage({
  278. id: "settings.wrapLongLinesDescription",
  279. })}
  280. control={
  281. <Switch
  282. checked={codePreviewSettings.wrapLongLines}
  283. onCheckedChange={(checked) =>
  284. setCodePreviewSettings({ wrapLongLines: checked })
  285. }
  286. />
  287. }
  288. />
  289. <SettingsRow
  290. searchId="settings.fontSize"
  291. label={intl.formatMessage({ id: "settings.fontSize" })}
  292. description={intl.formatMessage({
  293. id: "settings.fontSizeDescription",
  294. })}
  295. control={
  296. <FontSizeInput
  297. key={codePreviewSettings.fontSizePx}
  298. min={12}
  299. max={20}
  300. value={codePreviewSettings.fontSizePx}
  301. onChange={(fontSizePx) => setCodePreviewSettings({ fontSizePx })}
  302. ariaLabel={intl.formatMessage({ id: "settings.fontSize" })}
  303. />
  304. }
  305. />
  306. </CardContent>
  307. </Card>
  308. </div>
  309. <div className="min-w-0 space-y-4">
  310. <div>
  311. <h3 className="text-ui-base font-semibold text-foreground">
  312. {intl.formatMessage({ id: "settings.previewSectionTitle" })}
  313. </h3>
  314. <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
  315. {intl.formatMessage({ id: "settings.previewDescription" })}
  316. </p>
  317. </div>
  318. <div className="grid gap-4 sm:grid-cols-2">
  319. <ThemePreviewCard
  320. mode="light"
  321. title={intl.formatMessage({ id: "settings.previewLight" })}
  322. themeName={getThemeOptionLabel(codePreviewSettings.lightTheme)}
  323. theme={getCodePreviewTheme("light", codePreviewSettings)}
  324. isActive={activePreviewMode === "light"}
  325. showLineNumbers={codePreviewSettings.showLineNumbers}
  326. wrapLongLines={codePreviewSettings.wrapLongLines}
  327. fontSizePx={codePreviewSettings.fontSizePx}
  328. />
  329. <ThemePreviewCard
  330. mode="dark"
  331. title={intl.formatMessage({ id: "settings.previewDark" })}
  332. themeName={getThemeOptionLabel(codePreviewSettings.darkTheme)}
  333. theme={getCodePreviewTheme("dark", codePreviewSettings)}
  334. isActive={activePreviewMode === "dark"}
  335. showLineNumbers={codePreviewSettings.showLineNumbers}
  336. wrapLongLines={codePreviewSettings.wrapLongLines}
  337. fontSizePx={codePreviewSettings.fontSizePx}
  338. />
  339. </div>
  340. </div>
  341. </div>
  342. </>
  343. );
  344. }