2025-11-06 16:37:01 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 主题 Hooks
|
2025-11-12 00:13:26 +08:00
|
|
|
|
*
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* 提供统一的主题访问接口
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
|
import { useColorScheme as useSystemColorScheme } from 'react-native';
|
|
|
|
|
|
import { useTheme as useThemeStore } from '@/stores';
|
2025-11-12 00:13:26 +08:00
|
|
|
|
import { Colors } from '@/theme';
|
|
|
|
|
|
import { ThemeEnum } from '@/constants/theme';
|
2025-11-06 16:37:01 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2025-11-12 00:13:26 +08:00
|
|
|
|
* 获取当前颜色方案(light | dark | orange)
|
|
|
|
|
|
*
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* 从 settingsStore 读取用户设置的主题
|
2025-11-12 00:13:26 +08:00
|
|
|
|
* 支持 'light' | 'dark' | 'orange' | 'auto' 四种模式
|
2025-11-06 16:37:01 +08:00
|
|
|
|
*/
|
2025-11-12 00:13:26 +08:00
|
|
|
|
export function useColorScheme(): ThemeEnum {
|
2025-11-06 16:37:01 +08:00
|
|
|
|
const userTheme = useThemeStore();
|
|
|
|
|
|
const systemTheme = useSystemColorScheme();
|
|
|
|
|
|
|
|
|
|
|
|
// 如果用户选择了 'auto',则使用系统主题
|
|
|
|
|
|
if (userTheme === 'auto') {
|
2025-11-12 00:13:26 +08:00
|
|
|
|
return systemTheme === 'dark' ? ThemeEnum.DARK : ThemeEnum.LIGHT;
|
2025-11-06 16:37:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 否则使用用户选择的主题
|
2025-11-12 00:13:26 +08:00
|
|
|
|
return userTheme as ThemeEnum;
|
2025-11-06 16:37:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取主题颜色
|
2025-11-12 00:13:26 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @param props - 可选的自定义颜色 { light?: string; dark?: string; orange?: string }
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* @param colorName - Colors 中定义的颜色名称
|
|
|
|
|
|
* @returns 当前主题对应的颜色值
|
2025-11-12 00:13:26 +08:00
|
|
|
|
*
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* @example
|
|
|
|
|
|
* ```tsx
|
|
|
|
|
|
* const textColor = useThemeColor({}, 'text');
|
2025-11-12 00:13:26 +08:00
|
|
|
|
* const customColor = useThemeColor({ light: '#000', dark: '#fff', orange: '#f90' }, 'text');
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useThemeColor(
|
2025-11-12 00:13:26 +08:00
|
|
|
|
props: { light?: string; dark?: string; orange?: string },
|
|
|
|
|
|
colorName: keyof typeof Colors.light & keyof typeof Colors.dark & keyof typeof Colors.orange
|
2025-11-06 16:37:01 +08:00
|
|
|
|
): string {
|
|
|
|
|
|
const theme = useColorScheme();
|
|
|
|
|
|
const colorFromProps = props[theme];
|
|
|
|
|
|
|
|
|
|
|
|
if (colorFromProps) {
|
|
|
|
|
|
return colorFromProps;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
return Colors[theme][colorName];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取完整的主题颜色对象
|
2025-11-12 00:13:26 +08:00
|
|
|
|
*
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* @returns 当前主题的所有颜色配置
|
2025-11-12 00:13:26 +08:00
|
|
|
|
*
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* @example
|
|
|
|
|
|
* ```tsx
|
|
|
|
|
|
* const colors = useThemeColors();
|
|
|
|
|
|
* <View style={{ backgroundColor: colors.background }}>
|
|
|
|
|
|
* <Text style={{ color: colors.text }}>Hello</Text>
|
|
|
|
|
|
* </View>
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useThemeColors() {
|
|
|
|
|
|
const theme = useColorScheme();
|
2025-11-12 00:13:26 +08:00
|
|
|
|
|
2025-11-06 16:37:01 +08:00
|
|
|
|
return useMemo(() => {
|
|
|
|
|
|
return Colors[theme];
|
|
|
|
|
|
}, [theme]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取主题相关的所有信息
|
2025-11-12 00:13:26 +08:00
|
|
|
|
*
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* @returns 主题信息对象
|
2025-11-12 00:13:26 +08:00
|
|
|
|
*
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* @example
|
|
|
|
|
|
* ```tsx
|
2025-11-12 00:13:26 +08:00
|
|
|
|
* const { theme, colors, isDark, isLight, isOrange } = useThemeInfo();
|
2025-11-06 16:37:01 +08:00
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function useThemeInfo() {
|
|
|
|
|
|
const theme = useColorScheme();
|
|
|
|
|
|
const colors = useThemeColors();
|
2025-11-12 00:13:26 +08:00
|
|
|
|
|
2025-11-06 16:37:01 +08:00
|
|
|
|
return useMemo(() => ({
|
|
|
|
|
|
theme,
|
|
|
|
|
|
colors,
|
2025-11-12 00:13:26 +08:00
|
|
|
|
isDark: theme === ThemeEnum.DARK,
|
|
|
|
|
|
isLight: theme === ThemeEnum.LIGHT,
|
|
|
|
|
|
isOrange: theme === ThemeEnum.ORANGE,
|
2025-11-06 16:37:01 +08:00
|
|
|
|
}), [theme, colors]);
|
|
|
|
|
|
}
|
|
|
|
|
|
|