adapter.uts
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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)
];