// 主题与外观:通过 CSS 变量实现主题、强调色、密度、字号、行高、内容宽度
import type { AppSettings, AppearanceSettings } from './types';
export interface AccentOption {
id: string;
color: string;
strong: string;
soft: string;
contrast: string;
}
export const ACCENTS: AccentOption[] = [
{ id: 'blue', color: '#5b6ef2', strong: '#4a5ce0', soft: 'rgba(91, 110, 242, 0.14)', contrast: '#ffffff' },
{ id: 'teal', color: '#0f9d8f', strong: '#0c877b', soft: 'rgba(15, 157, 143, 0.14)', contrast: '#ffffff' },
{ id: 'green', color: '#3f9e5a', strong: '#34874c', soft: 'rgba(63, 158, 90, 0.14)', contrast: '#ffffff' },
{ id: 'violet', color: '#8a5cf6', strong: '#7747e6', soft: 'rgba(138, 92, 246, 0.14)', contrast: '#ffffff' },
{ id: 'rose', color: '#e05572', strong: '#c94461', soft: 'rgba(224, 85, 114, 0.14)', contrast: '#ffffff' },
{ id: 'amber', color: '#b4771c', strong: '#9c6514', soft: 'rgba(180, 119, 28, 0.16)', contrast: '#ffffff' },
{ id: 'slate', color: '#64748b', strong: '#556274', soft: 'rgba(100, 116, 139, 0.16)', contrast: '#ffffff' },
];
export const FALLBACK_SETTINGS: AppSettings = {
appearance: {
language: 'zh',
theme: 'system',
accent: 'blue',
density: 'comfortable',
fontFamily: 'sans',
fontSize: 15,
lineHeight: 1.6,
contentWidth: 'standard',
},
sync: { rangeDays: 30, folderLimit: 300, autoEnabled: true, intervalMinutes: 15 },
editor: { signature: '', fontFamily: 'sans', fontSize: 15, lineHeight: 1.7, defaultAccountId: null },
backup: {
webdavUrl: '',
webdavUsername: '',
remotePath: 'xmail',
autoEnabled: false,
intervalHours: 24,
includeBodies: false,
includeAttachments: false,
days: 90,
accountIds: [],
lastBackupAt: null,
},
notifications: {
enabled: false,
hasBotToken: false,
hasChatId: false,
},
};
export function accentOf(id: string): AccentOption {
return ACCENTS.find((item) => item.id === id) || ACCENTS[0];
}
let mediaQuery: MediaQueryList | null = null;
let mediaHandler: (() => void) | null = null;
function applyTheme(mode: AppearanceSettings['theme']) {
const root = document.documentElement;
if (mediaQuery && mediaHandler) {
mediaQuery.removeEventListener('change', mediaHandler);
mediaQuery = null;
mediaHandler = null;
}
if (mode === 'system') {
mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
mediaHandler = () => {
root.dataset.theme = mediaQuery?.matches ? 'dark' : 'light';
};
mediaQuery.addEventListener('change', mediaHandler);
root.dataset.theme = mediaQuery.matches ? 'dark' : 'light';
} else {
root.dataset.theme = mode;
}
}
const CONTENT_WIDTHS: Record<string, string> = {
narrow: '680px',
standard: '900px',
wide: '1120px',
full: '100%',
};
export function applyAppearance(appearance: AppearanceSettings) {
const root = document.documentElement;
const accent = accentOf(appearance.accent);
root.style.setProperty('--accent', accent.color);
root.style.setProperty('--accent-strong', accent.strong);
root.style.setProperty('--accent-soft', accent.soft);
root.style.setProperty('--accent-contrast', accent.contrast);
root.dataset.density = appearance.density;
root.dataset.font = appearance.fontFamily;
root.dataset.width = appearance.contentWidth;
root.style.fontSize = `${appearance.fontSize}px`;
root.style.setProperty('--line-height', String(appearance.lineHeight));
root.style.setProperty('--content-max', CONTENT_WIDTHS[appearance.contentWidth] || CONTENT_WIDTHS.standard);
applyTheme(appearance.theme);
}