|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type ThemePreset = 'ios' | 'md' | 'auto'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export type ThemeMode = 'light' | 'dark' | 'auto'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export interface ThemeConfig { |
|
|
preset: ThemePreset; |
|
|
mode: 'light' | 'dark'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function applyIonicTheme(config: ThemeConfig): void { |
|
|
const root = document.documentElement; |
|
|
|
|
|
|
|
|
if (config.mode === 'dark') { |
|
|
root.classList.add('ion-palette-dark'); |
|
|
} else { |
|
|
root.classList.remove('ion-palette-dark'); |
|
|
} |
|
|
|
|
|
|
|
|
root.style.setProperty('--ion-font-family', [ |
|
|
'-apple-system', |
|
|
'BlinkMacSystemFont', |
|
|
'"Segoe UI"', |
|
|
'Roboto', |
|
|
'"Helvetica Neue"', |
|
|
'Arial', |
|
|
'sans-serif', |
|
|
'"Apple Color Emoji"', |
|
|
'"Segoe UI Emoji"', |
|
|
'"Segoe UI Symbol"', |
|
|
'"Microsoft YaHei"', |
|
|
'"PingFang SC"', |
|
|
'"Hiragino Sans GB"', |
|
|
'"WenQuanYi Micro Hei"', |
|
|
].join(',')); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSystemPlatformPreference(): 'ios' | 'md' { |
|
|
if (typeof window !== 'undefined') { |
|
|
const platform = navigator.platform.toLowerCase(); |
|
|
const userAgent = navigator.userAgent.toLowerCase(); |
|
|
|
|
|
|
|
|
if (platform.includes('mac') || |
|
|
platform.includes('iphone') || |
|
|
platform.includes('ipad') || |
|
|
userAgent.includes('mac') || |
|
|
userAgent.includes('iphone') || |
|
|
userAgent.includes('ipad')) { |
|
|
return 'ios'; |
|
|
} |
|
|
} |
|
|
return 'md'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getSystemThemePreference(): 'light' | 'dark' { |
|
|
if (typeof window !== 'undefined' && window.matchMedia) { |
|
|
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
|
|
} |
|
|
return 'light'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getEffectiveThemePreset(themePreset: ThemePreset): 'ios' | 'md' { |
|
|
if (themePreset === 'auto') { |
|
|
return getSystemPlatformPreference(); |
|
|
} |
|
|
return themePreset as 'ios' | 'md'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getEffectiveThemeMode(themeMode: ThemeMode): 'light' | 'dark' { |
|
|
if (themeMode === 'auto') { |
|
|
return getSystemThemePreference(); |
|
|
} |
|
|
return themeMode as 'light' | 'dark'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const THEME_PRESET_LABELS: Record<ThemePreset, string> = { |
|
|
'ios': 'iOS', |
|
|
'md': 'Material Design', |
|
|
'auto': '自动识别', |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const THEME_MODE_LABELS: Record<ThemeMode, string> = { |
|
|
'light': '浅色', |
|
|
'dark': '深色', |
|
|
'auto': '跟随系统', |
|
|
}; |
|
|
|
|
|
|