AppearancePanel.tsx

4.8 KB v1.0
// 外观设置:语言、主题、强调色、密度、字体、字号、行高、内容宽度
import { useI18n } from '../../i18n';
import { Card, ColorSwatches, Segmented, SettingRow, Slider } from '../../components/ui';
import { Icon } from '../../components/Icon';
import { PanelHeader, type PanelProps } from './common';
import { accentOf } from '../../theme';
import type { ContentWidth, Density, FontFamily, Language, ThemeMode } from '../../types';

export function AppearancePanel({ settings, updateSettings }: PanelProps) {
  const { t } = useI18n();
  const appearance = settings.appearance;
  const patch = (value: Record<string, unknown>) =>
    updateSettings({ appearance: { ...appearance, ...value } });

  const previewFont =
    appearance.fontFamily === 'serif'
      ? 'Georgia, "Songti SC", "SimSun", serif'
      : '-apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif';
  const accent = accentOf(appearance.accent);

  return (
    <>
      <PanelHeader title={t('appearanceTitle')} desc={t('appearanceDesc')} />

      <Card title={t('language')}>
        <Segmented<Language>
          value={appearance.language}
          options={[
            { value: 'zh', label: t('languageZh') },
            { value: 'en', label: t('languageEn') },
          ]}
          onChange={(value) => patch({ language: value })}
        />
      </Card>

      <Card title={t('theme')}>
        <div className="choice-grid">
          {(
            [
              { value: 'light', label: t('themeLight'), icon: 'sun' },
              { value: 'dark', label: t('themeDark'), icon: 'moon' },
              { value: 'system', label: t('themeSystem'), icon: 'monitor' },
            ] as Array<{ value: ThemeMode; label: string; icon: 'sun' | 'moon' | 'monitor' }>
          ).map((option) => (
            <button
              type="button"
              key={option.value}
              className={`choice ${appearance.theme === option.value ? 'active' : ''}`}
              onClick={() => patch({ theme: option.value })}
            >
              <Icon name={option.icon} size={17} />
              {option.label}
            </button>
          ))}
        </div>
      </Card>

      <Card title={t('accentColor')}>
        <ColorSwatches value={appearance.accent} onChange={(value) => patch({ accent: value })} />
      </Card>

      <Card title={t('density')}>
        <Segmented<Density>
          value={appearance.density}
          options={[
            { value: 'comfortable', label: t('densityComfortable') },
            { value: 'compact', label: t('densityCompact') },
          ]}
          onChange={(value) => patch({ density: value })}
        />
      </Card>

      <Card>
        <SettingRow label={t('bodyFont')}>
          <Segmented<FontFamily>
            value={appearance.fontFamily}
            options={[
              { value: 'sans', label: t('fontSans') },
              { value: 'serif', label: t('fontSerif') },
            ]}
            onChange={(value) => patch({ fontFamily: value })}
          />
        </SettingRow>
        <SettingRow label={t('fontSize')}>
          <Slider
            value={appearance.fontSize}
            min={12}
            max={22}
            step={1}
            format={(value) => `${value}px`}
            onChange={(value) => patch({ fontSize: value })}
          />
        </SettingRow>
        <SettingRow label={t('lineHeight')}>
          <Slider
            value={appearance.lineHeight}
            min={1.2}
            max={2.2}
            step={0.05}
            format={(value) => value.toFixed(2)}
            onChange={(value) => patch({ lineHeight: value })}
          />
        </SettingRow>
        <SettingRow label={t('contentWidth')}>
          <Segmented<ContentWidth>
            value={appearance.contentWidth}
            options={[
              { value: 'narrow', label: t('widthNarrow') },
              { value: 'standard', label: t('widthStandard') },
              { value: 'wide', label: t('widthWide') },
              { value: 'full', label: t('widthFull') },
            ]}
            onChange={(value) => patch({ contentWidth: value })}
          />
        </SettingRow>
      </Card>

      <Card title={t('appearancePreview')}>
        <div
          className="preview-mail"
          style={{ fontFamily: previewFont, fontSize: `${appearance.fontSize}px`, lineHeight: appearance.lineHeight }}
        >
          <div className="preview-subject">{t('previewSubject')}</div>
          <div className="preview-from">
            <span className="preview-avatar" style={{ background: accent.soft, color: accent.strong }}>
              X
            </span>
            <span>{t('previewSender')}</span>
          </div>
          <p className="preview-body">{t('previewBody')}</p>
        </div>
      </Card>
    </>
  );
}