| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- import type { Theme } from "@/useTheme.js";
- import { useState } from "react";
- import { resolveTheme } from "@/useTheme.js";
- import { Card, CardContent } from "@/components/ui/card.js";
- import { Input } from "@/components/ui/input.js";
- import { Switch } from "@/components/ui/switch.js";
- import {
- Select,
- SelectContent,
- SelectItem,
- SelectTrigger,
- SelectValue,
- } from "@/components/ui/select.js";
- import {
- getThemeOptionLabel,
- SettingsRow,
- ThemePreviewCard,
- ThemeSelect,
- } from "@/settings/SettingsPageParts.js";
- import { getCodePreviewTheme } from "@/lib/codePreviewPreferences.js";
- import { useZCodeIntl } from "@/i18n/IntlProvider.js";
- import type { CodePreviewSettings } from "@/store/index.js";
- import { THEME_MODES } from "@/settings/settingsPageConfig.js";
- import { BUILTIN_SKIN_MANIFESTS, getSkinManifest } from "@/skins/registry.js";
- import { MAX_UI_FONT_SIZE_PX, MIN_UI_FONT_SIZE_PX } from "@/lib/uiFontSize.js";
- function FontSizeInput({
- value,
- min,
- max,
- ariaLabel,
- onChange,
- disabled = false,
- }: {
- value: number;
- min: number;
- max: number;
- ariaLabel: string;
- onChange: (value: number) => void;
- disabled?: boolean;
- }) {
- const [draft, setDraft] = useState(String(value));
- const commit = () => {
- const parsed = draft.trim() === "" ? Number.NaN : Number(draft);
- const nextValue = Number.isFinite(parsed)
- ? Math.min(max, Math.max(min, Math.round(parsed)))
- : value;
- setDraft(String(nextValue));
- if (nextValue !== value) {
- onChange(nextValue);
- }
- };
- return (
- <div className="relative w-28">
- <Input
- type="number"
- inputMode="numeric"
- min={min}
- max={max}
- step={1}
- value={draft}
- disabled={disabled}
- aria-label={ariaLabel}
- onChange={(event) => setDraft(event.currentTarget.value)}
- onBlur={commit}
- onKeyDown={(event) => {
- if (event.key === "Enter") {
- event.currentTarget.blur();
- } else if (event.key === "Escape") {
- event.preventDefault();
- setDraft(String(value));
- }
- }}
- className="pr-8 text-right tabular-nums [appearance:textfield] [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:appearance-none"
- />
- <span className="pointer-events-none absolute inset-y-0 right-2 flex items-center text-ui-lg text-foreground-subtle">
- px
- </span>
- </div>
- );
- }
- export function AppearanceSectionContent({
- codePreviewSettings,
- setCodePreviewSettings,
- theme,
- setTheme,
- skinId,
- setSkin,
- uiFontSizePx,
- setUiFontSizePx,
- }: {
- codePreviewSettings: CodePreviewSettings;
- setCodePreviewSettings: (settings: Partial<CodePreviewSettings>) => void;
- theme: Theme;
- setTheme: (theme: Theme) => void;
- skinId: string | null;
- setSkin: (skinId: string | null) => void;
- uiFontSizePx: number;
- setUiFontSizePx: (fontSizePx: number) => void;
- }) {
- const { intl } = useZCodeIntl();
- const activePreviewMode = resolveTheme(theme);
- // fontGrid 锁定(A4):激活皮肤声明 12px 像素网格时,字号控件禁用并说明原因。
- // 运行时的 inline 值钳制在 applySkin 内,这里只负责 UI 禁用与解释。
- const fontGridLock =
- getSkinManifest(skinId)?.tokens.fontGrid?.lockUiFontSize ?? null;
- return (
- <>
- <div className="min-w-0 space-y-3">
- <div>
- <h3 className="text-ui-lg font-semibold text-foreground">
- {intl.formatMessage({ id: "settings.appearance.interfaceTitle" })}
- </h3>
- <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
- {intl.formatMessage({
- id: "settings.appearance.interfaceDescription",
- })}
- </p>
- </div>
- <Card className="border border-border bg-card py-0 shadow-none">
- <CardContent className="space-y-0 px-0">
- <SettingsRow
- label={intl.formatMessage({ id: "settings.themeMode" })}
- description={intl.formatMessage({
- id: "settings.themeModeDescription",
- })}
- control={
- <Select value={theme} onValueChange={(value) => setTheme(value as Theme)}>
- <SelectTrigger size="lg" className="w-[260px] min-w-0 justify-between">
- <SelectValue />
- </SelectTrigger>
- <SelectContent>
- {THEME_MODES.map(({ mode, icon: Icon }) => (
- <SelectItem key={mode} value={mode}>
- <div className="flex items-center gap-2">
- <Icon className="size-4" />
- {intl.formatMessage({
- id: `settings.themeMode.${mode}`,
- })}
- </div>
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- }
- />
- {BUILTIN_SKIN_MANIFESTS.length > 0 ? (
- <SettingsRow
- label={intl.formatMessage({ id: "settings.skin.label" })}
- description={intl.formatMessage({
- id: "settings.skin.description",
- })}
- control={
- <Select
- value={skinId ?? "none"}
- onValueChange={(value) => setSkin(value === "none" ? null : value)}
- >
- <SelectTrigger size="lg" className="w-[260px] min-w-0 justify-between">
- <SelectValue />
- </SelectTrigger>
- <SelectContent>
- {/* Radix SelectItem 禁止空字符串 value(会直接抛错),用 "none" 哨兵表示无皮肤。 */}
- <SelectItem value="none">
- {intl.formatMessage({ id: "settings.skin.default" })}
- </SelectItem>
- {BUILTIN_SKIN_MANIFESTS.map((manifest) => (
- <SelectItem key={manifest.id} value={manifest.id}>
- {intl.formatMessage({ id: manifest.i18nName })}
- </SelectItem>
- ))}
- </SelectContent>
- </Select>
- }
- />
- ) : null}
- <SettingsRow
- label={intl.formatMessage({ id: "settings.uiFontSize" })}
- description={intl.formatMessage(
- { id: fontGridLock !== null ? "settings.skin.fontGridLock" : "settings.uiFontSizeDescription" },
- fontGridLock !== null ? { size: fontGridLock } : undefined,
- )}
- control={
- <FontSizeInput
- key={uiFontSizePx}
- disabled={fontGridLock !== null}
- min={MIN_UI_FONT_SIZE_PX}
- max={MAX_UI_FONT_SIZE_PX}
- value={uiFontSizePx}
- onChange={setUiFontSizePx}
- ariaLabel={intl.formatMessage({ id: "settings.uiFontSize" })}
- />
- }
- />
- </CardContent>
- </Card>
- </div>
- <div className="space-y-6">
- <div className="min-w-0 space-y-3">
- <div>
- <h3 className="text-ui-lg font-semibold text-foreground">
- {intl.formatMessage({ id: "settings.appearance.codeTitle" })}
- </h3>
- <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
- {intl.formatMessage({
- id: "settings.appearance.codeDescription",
- })}
- </p>
- </div>
- <Card className="border border-border bg-card py-0 shadow-none [&_[data-slot=select-trigger]]:w-full">
- <CardContent className="space-y-0 px-0">
- <SettingsRow
- label={intl.formatMessage({ id: "settings.lightTheme" })}
- description={intl.formatMessage({
- id: "settings.lightThemeDescription",
- })}
- control={
- <ThemeSelect
- value={codePreviewSettings.lightTheme}
- onValueChange={(value) => setCodePreviewSettings({ lightTheme: value })}
- />
- }
- />
- <SettingsRow
- label={intl.formatMessage({ id: "settings.darkTheme" })}
- description={intl.formatMessage({
- id: "settings.darkThemeDescription",
- })}
- control={
- <ThemeSelect
- value={codePreviewSettings.darkTheme}
- onValueChange={(value) => setCodePreviewSettings({ darkTheme: value })}
- />
- }
- />
- <SettingsRow
- label={intl.formatMessage({ id: "settings.showLineNumbers" })}
- description={intl.formatMessage({
- id: "settings.showLineNumbersDescription",
- })}
- control={
- <Switch
- checked={codePreviewSettings.showLineNumbers}
- onCheckedChange={(checked) =>
- setCodePreviewSettings({ showLineNumbers: checked })
- }
- />
- }
- />
- <SettingsRow
- label={intl.formatMessage({ id: "settings.wrapLongLines" })}
- description={intl.formatMessage({
- id: "settings.wrapLongLinesDescription",
- })}
- control={
- <Switch
- checked={codePreviewSettings.wrapLongLines}
- onCheckedChange={(checked) =>
- setCodePreviewSettings({ wrapLongLines: checked })
- }
- />
- }
- />
- <SettingsRow
- label={intl.formatMessage({ id: "settings.fontSize" })}
- description={intl.formatMessage({
- id: "settings.fontSizeDescription",
- })}
- control={
- <FontSizeInput
- key={codePreviewSettings.fontSizePx}
- min={12}
- max={20}
- value={codePreviewSettings.fontSizePx}
- onChange={(fontSizePx) => setCodePreviewSettings({ fontSizePx })}
- ariaLabel={intl.formatMessage({ id: "settings.fontSize" })}
- />
- }
- />
- </CardContent>
- </Card>
- </div>
- <div className="min-w-0 space-y-4">
- <div>
- <h3 className="text-ui-base font-semibold text-foreground">
- {intl.formatMessage({ id: "settings.previewSectionTitle" })}
- </h3>
- <p className="mt-1 text-ui-base leading-6 text-foreground-subtle">
- {intl.formatMessage({ id: "settings.previewDescription" })}
- </p>
- </div>
- <div className="grid gap-4 sm:grid-cols-2">
- <ThemePreviewCard
- mode="light"
- title={intl.formatMessage({ id: "settings.previewLight" })}
- themeName={getThemeOptionLabel(codePreviewSettings.lightTheme)}
- theme={getCodePreviewTheme("light", codePreviewSettings)}
- isActive={activePreviewMode === "light"}
- showLineNumbers={codePreviewSettings.showLineNumbers}
- wrapLongLines={codePreviewSettings.wrapLongLines}
- fontSizePx={codePreviewSettings.fontSizePx}
- />
- <ThemePreviewCard
- mode="dark"
- title={intl.formatMessage({ id: "settings.previewDark" })}
- themeName={getThemeOptionLabel(codePreviewSettings.darkTheme)}
- theme={getCodePreviewTheme("dark", codePreviewSettings)}
- isActive={activePreviewMode === "dark"}
- showLineNumbers={codePreviewSettings.showLineNumbers}
- wrapLongLines={codePreviewSettings.wrapLongLines}
- fontSizePx={codePreviewSettings.fontSizePx}
- />
- </div>
- </div>
- </div>
- </>
- );
- }
|