theme-manager.uts 6.88 KB
// utils/theme-manager.uts
// 主题管理器 - 类似 i18n,管理多套主题的切换和获取

import { get } from "./common.uts";
import { getScreenSize } from "./screen-adapt.uts";

/**
 * 主题名称类型
 */
export type ThemeName = "mobile-large" | "mobile-medium" | "mobile-small" | "tablet" | "tablet-large"

/**
 * 主题变量接口
 */
export interface ThemeVariables {
	spacing: {
		[key: string]: number
	}
	borderRadius: {
		[key: string]: number
	}
	fontSize: {
		[key: string]: number
	}
	size: {
		[key: string]: number
	}
	lineHeight: {
		[key: string]: number
	}
}

/**
 * 主题配置类型
 */
export type ThemeConfig = {
	[key in ThemeName]: ThemeVariables
}

/**
 * 默认主题配置(内联 JSON)
 * 实际项目中可以改为从 themes/themes.json 文件读取
 */
const defaultThemeConfig: ThemeConfig = {
	"mobile-small": {
		spacing: { "0": 0, xs: 4, sm: 6, md: 10, base: 12, lg: 16, xl: 20, "2xl": 24, "3xl": 32, "4xl": 40 },
		borderRadius: { none: 0, sm: 2, base: 4, md: 6, lg: 8, xl: 10, "2xl": 12, full: 9999 },
		fontSize: { xs: 10, sm: 12, base: 14, md: 16, lg: 18, xl: 20, "2xl": 24, "3xl": 28, "4xl": 32 },
		size: { xs: 16, sm: 24, md: 32, base: 40, lg: 48, xl: 64, "2xl": 80, "3xl": 96 },
		lineHeight: { tight: 1.2, normal: 1.5, relaxed: 1.75, loose: 2 }
	},
	"mobile-medium": {
		spacing: { "0": 0, xs: 4, sm: 8, md: 12, base: 16, lg: 20, xl: 24, "2xl": 32, "3xl": 40, "4xl": 48 },
		borderRadius: { none: 0, sm: 2, base: 4, md: 6, lg: 8, xl: 12, "2xl": 16, full: 9999 },
		fontSize: { xs: 10, sm: 12, base: 14, md: 16, lg: 18, xl: 20, "2xl": 24, "3xl": 30, "4xl": 36 },
		size: { xs: 16, sm: 24, md: 32, base: 40, lg: 48, xl: 64, "2xl": 80, "3xl": 96 },
		lineHeight: { tight: 1.2, normal: 1.5, relaxed: 1.75, loose: 2 }
	},
	"mobile-large": {
		spacing: { "0": 0, xs: 6, sm: 10, md: 14, base: 18, lg: 24, xl: 28, "2xl": 36, "3xl": 44, "4xl": 56 },
		borderRadius: { none: 0, sm: 3, base: 6, md: 8, lg: 10, xl: 14, "2xl": 18, full: 9999 },
		fontSize: { xs: 11, sm: 13, base: 15, md: 17, lg: 19, xl: 22, "2xl": 26, "3xl": 32, "4xl": 40 },
		size: { xs: 20, sm: 28, md: 36, base: 44, lg: 52, xl: 68, "2xl": 84, "3xl": 100 },
		lineHeight: { tight: 1.2, normal: 1.5, relaxed: 1.75, loose: 2 }
	},
	"tablet": {
		spacing: { "0": 0, xs: 6, sm: 10, md: 16, base: 20, lg: 24, xl: 32, "2xl": 40, "3xl": 48, "4xl": 64 },
		borderRadius: { none: 0, sm: 4, base: 6, md: 8, lg: 12, xl: 16, "2xl": 20, full: 9999 },
		fontSize: { xs: 12, sm: 14, base: 16, md: 18, lg: 20, xl: 24, "2xl": 28, "3xl": 36, "4xl": 44 },
		size: { xs: 24, sm: 32, md: 40, base: 48, lg: 56, xl: 72, "2xl": 88, "3xl": 104 },
		lineHeight: { tight: 1.2, normal: 1.5, relaxed: 1.75, loose: 2 }
	},
	"tablet-large": {
		spacing: { "0": 0, xs: 8, sm: 12, md: 20, base: 24, lg: 32, xl: 40, "2xl": 48, "3xl": 64, "4xl": 80 },
		borderRadius: { none: 0, sm: 6, base: 8, md: 10, lg: 14, xl: 18, "2xl": 24, full: 9999 },
		fontSize: { xs: 14, sm: 16, base: 18, md: 20, lg: 24, xl: 28, "2xl": 32, "3xl": 40, "4xl": 48 },
		size: { xs: 28, sm: 36, md: 44, base: 52, lg: 64, xl: 80, "2xl": 96, "3xl": 112 },
		lineHeight: { tight: 1.2, normal: 1.5, relaxed: 1.75, loose: 2 }
	}
};

/**
 * 加载主题配置
 * 优先从文件读取,如果失败则使用默认配置
 */
function loadThemeConfig(): ThemeConfig {
	// TODO: 实现从 themes/themes.json 文件读取
	// 当前使用内联默认配置
	// 后续可以改为:
	// 1. 使用 uni.request 从服务器加载
	// 2. 使用 uni.getFileSystemManager() 读取本地文件
	// 3. 使用静态 import(如果支持)
	
	return defaultThemeConfig;
}

/**
 * 根据屏幕尺寸自动选择主题
 */
export function getThemeByScreenSize(): ThemeName {
	const screenSize = getScreenSize();
	
	switch (screenSize) {
		case "large": {
			return "mobile-large";
		}
		case "medium": {
			return "mobile-medium";
		}
		case "small": {
			return "mobile-small";
		}
		case "xlarge": {
			// 根据屏幕宽度进一步判断是 tablet 还是 tablet-large
			const windowInfo = uni.getWindowInfo();
			if (windowInfo.windowWidth >= 1920) {
				return "tablet-large";
			}
			return "tablet";
		}
		default: {
			return "mobile-medium";
		}
	}
}

/**
 * 主题管理器类
 */
export class ThemeManager {
	private currentThemeName: ThemeName = "mobile-medium";
	private themes: null | ThemeConfig = null;
	
	constructor() {
		this.themes = loadThemeConfig();
		this.currentThemeName = getThemeByScreenSize();
	}
	
	/**
	 * 自动选择主题(根据屏幕尺寸)
	 */
	autoSelectTheme(): void {
		this.currentThemeName = getThemeByScreenSize();
	}
	
	/**
	 * 获取圆角值
	 */
	getBorderRadius(key: string): number {
		return this.getValue("borderRadius", key, 0);
	}
	
	/**
	 * 获取当前主题变量
	 */
	getCurrentTheme(): null | ThemeVariables {
		if (this.themes == null) {
			return null;
		}
		
		return get(this.themes, this.currentThemeName) as null | ThemeVariables;
	}
	
	/**
	 * 获取当前主题名称
	 */
	getCurrentThemeName(): ThemeName {
		return this.currentThemeName;
	}
	
	/**
	 * 获取字体大小
	 */
	getFontSize(key: string): number {
		return this.getValue("fontSize", key, 14);
	}
	
	/**
	 * 获取行高值
	 */
	getLineHeight(key: string): number {
		return this.getValue("lineHeight", key, 1.5);
	}
	
	/**
	 * 获取尺寸值
	 */
	getSize(key: string): number {
		return this.getValue("size", key, 0);
	}
	
	/**
	 * 获取间距值
	 */
	getSpacing(key: string): number {
		return this.getValue("spacing", key, 0);
	}
	
	/**
	 * 获取指定主题变量
	 */
	getTheme(themeName: ThemeName): null | ThemeVariables {
		if (this.themes == null) {
			return null;
		}
		
		return get(this.themes, themeName) as null | ThemeVariables;
	}
	
	/**
	 * 获取主题变量值
	 * @param category 变量类别(spacing, borderRadius, fontSize, size, lineHeight)
	 * @param key 变量键名(如 'base', 'lg', 'xl')
	 * @param defaultValue 默认值
	 */
	getValue(category: string, key: string, defaultValue: number = 0): number {
		const theme = this.getCurrentTheme();
		if (theme == null) {
			return defaultValue;
		}
		
		const categoryObj = get(theme, category) as any;
		if (categoryObj == null) {
			return defaultValue;
		}
		
		const value = get(categoryObj, key);
		if (value == null || typeof value !== "number") {
			return defaultValue;
		}
		
		return value;
	}
	
	/**
	 * 设置当前主题
	 */
	setTheme(themeName: ThemeName): boolean {
		if (this.themes == null) {
			console.warn("[theme-manager] Theme config not loaded");
			return false;
		}
		
		if (get(this.themes, themeName) == null) {
			console.warn(`[theme-manager] Theme "${themeName}" not found`);
			return false;
		}
		
		this.currentThemeName = themeName;
		return true;
	}
}

// 全局主题管理器实例
let globalThemeManager: null | ThemeManager = null;

/**
 * 获取全局主题管理器实例
 */
export function getThemeManager(): ThemeManager {
	if (globalThemeManager == null) {
		globalThemeManager = new ThemeManager();
	}
	return globalThemeManager;
}