adapter.uts 1.7 KB
import { landscapeMap, portraitMap } from "./registry.uts";
import { AppResolutionConfig } from "./types/index.uts";

// ========================================================================
// 4. 适配器定义 (Adapter Definition)
// ========================================================================
export interface IResolutionAdapter {
    match(w: number, h: number): boolean
    getConfig(key: string): AppResolutionConfig | null
    getFallback(): AppResolutionConfig | null
    getPrefix(): string
    getCssClass(key: string): string
}

class ResolutionAdapterImpl implements IResolutionAdapter {
	private map: Map<string, AppResolutionConfig>;
	private matcher: (w: number, h: number) => boolean;
	private prefix: string;

	constructor(prefix: string, map: Map<string, AppResolutionConfig>, matcher: (w: number, h: number) => boolean) {
		this.prefix = prefix;
		this.map = map;
		this.matcher = matcher;
	}

	getConfig(key: string): AppResolutionConfig | null { return this.map.get(key); }
	getCssClass(key: string): string {
		// 生成 CSS 类名规则:layout-{prefix}-{width}x{height}
		// 例如:layout-landscape-1920x1080
		// 如果未匹配到具体分辨率,使用 layout-{prefix}-common
		if (this.map.has(key)) {
			return `layout-${this.prefix}-${key}`;
		}
		return `layout-${this.prefix}-common`;
	}
	getFallback(): AppResolutionConfig | null { return this.map.get("common"); }
	getPrefix(): string { return this.prefix; }
    
	match(w: number, h: number): boolean { return this.matcher(w, h); }
}

export const resolutionAdapters: IResolutionAdapter[] = [
	new ResolutionAdapterImpl("landscape", landscapeMap, (w, h) => w > h),
	new ResolutionAdapterImpl("portrait", portraitMap, (w, h) => w <= h)
];