react-native-live-widgets 1.2.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +40 -1
- package/android/src/main/java/com/reactnativeliveactivity/renderer/DynamicWidgetCanvasRenderer.kt +443 -46
- package/android/src/main/java/com/reactnativeliveactivity/storage/DynamicWidgetStateStore.kt +86 -28
- package/android/src/main/java/com/reactnativeliveactivity/widgets/DynamicHomeScreenWidgetProvider.kt +126 -56
- package/android/src/main/res/layout/rn_live_widget_hit_zone.xml +16 -0
- package/android/src/main/res/layout/rn_live_widget_host.xml +16 -87
- package/docs/GUIDE-EN.md +241 -0
- package/docs/GUIDE-VI.md +266 -0
- package/docs/index.html +1140 -259
- package/docs/llms-full.txt +26 -0
- package/docs/llms.txt +8 -0
- package/ios/Models/DynamicLayoutModels.swift +6 -1
- package/ios/Renderer/DynamicNodeView.swift +169 -14
- package/ios/Widgets/DynamicHomeScreenWidget.swift +5 -0
- package/lib/builder.d.ts.map +1 -1
- package/lib/builder.js +65 -11
- package/lib/builder.js.map +1 -1
- package/lib/dispatcher.js +16 -10
- package/lib/dispatcher.js.map +1 -1
- package/lib/hooks.js +36 -31
- package/lib/hooks.js.map +1 -1
- package/lib/index.d.ts +17 -2
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +124 -50
- package/lib/index.js.map +1 -1
- package/lib/ir.d.ts +15 -0
- package/lib/ir.d.ts.map +1 -0
- package/lib/ir.js +379 -0
- package/lib/ir.js.map +1 -0
- package/lib/jsx.d.ts +9 -1
- package/lib/jsx.d.ts.map +1 -1
- package/lib/jsx.js +131 -84
- package/lib/jsx.js.map +1 -1
- package/lib/preview.d.ts +0 -23
- package/lib/preview.d.ts.map +1 -1
- package/lib/preview.js +373 -104
- package/lib/preview.js.map +1 -1
- package/lib/types.d.ts +204 -12
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js +2 -1
- package/lib/types.js.map +1 -1
- package/package.json +2 -1
- package/src/builder.ts +57 -2
- package/src/index.ts +66 -1
- package/src/ir.ts +430 -0
- package/src/jsx.tsx +30 -0
- package/src/preview.tsx +356 -32
- package/src/react-native-env.d.ts +1 -0
- package/src/types.ts +253 -13
package/src/index.ts
CHANGED
|
@@ -6,11 +6,14 @@ import {
|
|
|
6
6
|
LiveActivityPushTokens,
|
|
7
7
|
StartActivityOptions,
|
|
8
8
|
UpdateActivityOptions,
|
|
9
|
+
WidgetCapabilities,
|
|
9
10
|
WidgetConfigOption,
|
|
10
11
|
WidgetLayoutConfig,
|
|
11
12
|
WidgetRemoteConfig,
|
|
12
13
|
WidgetTimelineConfig,
|
|
14
|
+
WidgetIR,
|
|
13
15
|
} from './types';
|
|
16
|
+
import { compileToIR, inspectIR } from './ir';
|
|
14
17
|
|
|
15
18
|
declare const __DEV__: boolean | undefined;
|
|
16
19
|
|
|
@@ -43,7 +46,14 @@ import {
|
|
|
43
46
|
} from './dispatcher';
|
|
44
47
|
export type { ActionHandler, GlobalActionHandler };
|
|
45
48
|
export { registerAction, registerActionHandler, unregisterAction };
|
|
46
|
-
export
|
|
49
|
+
export type {
|
|
50
|
+
WidgetCapabilities,
|
|
51
|
+
GradientProps,
|
|
52
|
+
ShadowProps,
|
|
53
|
+
BorderStyle,
|
|
54
|
+
TypographyStyle,
|
|
55
|
+
FontDesign,
|
|
56
|
+
} from './types';
|
|
47
57
|
export * from './jsx';
|
|
48
58
|
export * from './preview';
|
|
49
59
|
import { renderToWidget } from './jsx';
|
|
@@ -659,6 +669,59 @@ export const Widget = {
|
|
|
659
669
|
return false;
|
|
660
670
|
}
|
|
661
671
|
},
|
|
672
|
+
|
|
673
|
+
/**
|
|
674
|
+
* 📊 Bảng tra cứu năng lực nền tảng (Platform Capability & Fallback Matrix).
|
|
675
|
+
* Giúp ứng dụng xác định chính xác tính năng nào được hỗ trợ thực tế trên thiết bị đang chạy.
|
|
676
|
+
*/
|
|
677
|
+
getCapabilities: async (): Promise<WidgetCapabilities> => {
|
|
678
|
+
const isIOS = Platform.OS === 'ios';
|
|
679
|
+
const isAndroid = Platform.OS === 'android';
|
|
680
|
+
const osVerNum = parseFloat(String(Platform.Version)) || 0;
|
|
681
|
+
|
|
682
|
+
let hasIsland = false;
|
|
683
|
+
if (isIOS) {
|
|
684
|
+
try {
|
|
685
|
+
hasIsland = LiveActivityModule?.isDynamicIslandSupported
|
|
686
|
+
? await LiveActivityModule.isDynamicIslandSupported()
|
|
687
|
+
: osVerNum >= 16.1;
|
|
688
|
+
} catch {
|
|
689
|
+
hasIsland = osVerNum >= 16.1;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
return {
|
|
694
|
+
platform: isIOS ? 'ios' : 'android',
|
|
695
|
+
osVersion: Platform.Version,
|
|
696
|
+
supportsLiveActivities: isIOS ? osVerNum >= 16.1 : osVerNum >= 26,
|
|
697
|
+
supportsDynamicIsland: hasIsland,
|
|
698
|
+
supportsAndroidLiveUpdates: isAndroid && osVerNum >= 36,
|
|
699
|
+
supportsHomeWidgets: isIOS ? osVerNum >= 14.0 : osVerNum >= 26,
|
|
700
|
+
supportsInteractiveIntents: isIOS ? osVerNum >= 17.0 : true,
|
|
701
|
+
supportsGradients: true,
|
|
702
|
+
supportsShadows: true,
|
|
703
|
+
supportsDashedBorders: true,
|
|
704
|
+
supportsDynamicType: true,
|
|
705
|
+
supportsRTL: true,
|
|
706
|
+
supportsTintedMode: isIOS ? osVerNum >= 18.0 : isAndroid,
|
|
707
|
+
supportsNumericAnimation: isIOS && osVerNum >= 16.0,
|
|
708
|
+
supportsAccessibility: true,
|
|
709
|
+
};
|
|
710
|
+
},
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* 🌲 Biên dịch cây DSL sang UI Intermediate Representation (IR & Semantic Tree - Phase 2).
|
|
714
|
+
*/
|
|
715
|
+
compileIR: (layout: any): WidgetIR => {
|
|
716
|
+
return compileToIR(layout);
|
|
717
|
+
},
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* 🔍 Phân tích và in ra bản đồ kiến trúc IR, Semantic Tree và Kế hoạch phân bổ Renderer Planner.
|
|
721
|
+
*/
|
|
722
|
+
inspectIR: (layout: any): string => {
|
|
723
|
+
return inspectIR(layout);
|
|
724
|
+
},
|
|
662
725
|
};
|
|
663
726
|
|
|
664
727
|
// ─── 3. SHARED STORAGE (App Groups & SharedPreferences) ─────────────────────
|
|
@@ -810,4 +873,6 @@ export const AudioPlayer = {
|
|
|
810
873
|
|
|
811
874
|
export * from './types';
|
|
812
875
|
export * from './builder';
|
|
876
|
+
export * from './ir';
|
|
813
877
|
export { StyleSheet } from './builder';
|
|
878
|
+
export * from './hooks';
|
package/src/ir.ts
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LayoutNode,
|
|
3
|
+
WidgetSinglePageConfig,
|
|
4
|
+
WidgetIR,
|
|
5
|
+
SemanticNode,
|
|
6
|
+
NodeRenderPlan,
|
|
7
|
+
RenderPlanTarget,
|
|
8
|
+
StateBinding,
|
|
9
|
+
AccessibilityRole,
|
|
10
|
+
} from './types';
|
|
11
|
+
|
|
12
|
+
let nodeCounter = 0;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 🌲 Trình biên dịch UI Intermediate Representation (IR).
|
|
16
|
+
* Chuyển hóa cây DSL thành cấu trúc dữ liệu trung gian chuẩn gồm:
|
|
17
|
+
* 1. Root LayoutNode (đã chuẩn hóa thuộc tính & định danh).
|
|
18
|
+
* 2. Semantic Tree độc lập (Tách biệt hoàn toàn khỏi việc vẽ hình của Canvas).
|
|
19
|
+
* 3. Render Plan (Renderer Planner phân loại Native RemoteViews vs Canvas Bitmap).
|
|
20
|
+
* 4. State Bindings (Danh sách các liên kết tương tác & đồng bộ trạng thái).
|
|
21
|
+
*/
|
|
22
|
+
export function compileToIR(input: LayoutNode | WidgetSinglePageConfig): WidgetIR {
|
|
23
|
+
nodeCounter = 0;
|
|
24
|
+
const rootNode = resolveRootLayoutNode(input);
|
|
25
|
+
|
|
26
|
+
// 1. Gán ID duy nhất và lập bản đồ cây
|
|
27
|
+
const indexedRoot = assignNodeIds(rootNode, '0');
|
|
28
|
+
|
|
29
|
+
// 2. Xây dựng Semantic Tree độc lập
|
|
30
|
+
const semanticTree: SemanticNode[] = [];
|
|
31
|
+
buildSemanticNodes(indexedRoot, '0', semanticTree);
|
|
32
|
+
const semanticSummary = synthesizeSemanticSummary(semanticTree);
|
|
33
|
+
|
|
34
|
+
// 3. Renderer Planner: Phân loại node theo khả năng native
|
|
35
|
+
const planNodes: Record<string, NodeRenderPlan> = {};
|
|
36
|
+
let nativeCount = 0;
|
|
37
|
+
let canvasCount = 0;
|
|
38
|
+
classifyRenderTargets(indexedRoot, planNodes, () => {
|
|
39
|
+
nativeCount++;
|
|
40
|
+
}, () => {
|
|
41
|
+
canvasCount++;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// 4. Trích xuất State Bindings
|
|
45
|
+
const stateBindings: StateBinding[] = [];
|
|
46
|
+
extractBindings(indexedRoot, stateBindings);
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
version: '2.0',
|
|
50
|
+
timestamp: Date.now(),
|
|
51
|
+
root: indexedRoot,
|
|
52
|
+
semanticTree,
|
|
53
|
+
semanticSummary,
|
|
54
|
+
renderPlan: {
|
|
55
|
+
overallStrategy: nativeCount >= canvasCount ? 'native_first_hybrid' : 'canvas_fallback',
|
|
56
|
+
nativeNodeCount: nativeCount,
|
|
57
|
+
canvasNodeCount: canvasCount,
|
|
58
|
+
nodes: planNodes,
|
|
59
|
+
},
|
|
60
|
+
stateBindings,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function resolveRootLayoutNode(input: LayoutNode | WidgetSinglePageConfig): LayoutNode {
|
|
65
|
+
if ('type' in input) {
|
|
66
|
+
return input;
|
|
67
|
+
}
|
|
68
|
+
return input.medium || input.large || input.small || { type: 'VStack', children: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function assignNodeIds(node: any, path: string): LayoutNode {
|
|
72
|
+
if (!node || typeof node !== 'object') return node;
|
|
73
|
+
|
|
74
|
+
const cloned = { ...node };
|
|
75
|
+
if (!cloned.id) {
|
|
76
|
+
cloned.id = `node_${path}_${++nodeCounter}`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (Array.isArray(cloned.children)) {
|
|
80
|
+
cloned.children = cloned.children.map((child: any, idx: number) =>
|
|
81
|
+
assignNodeIds(child, `${path}.${idx}`)
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return cloned as LayoutNode;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function buildSemanticNodes(node: any, path: string, result: SemanticNode[]): void {
|
|
89
|
+
if (!node || typeof node !== 'object') return;
|
|
90
|
+
|
|
91
|
+
const props = node.props || {};
|
|
92
|
+
const accessible = props.accessible !== false;
|
|
93
|
+
const type = node.type;
|
|
94
|
+
|
|
95
|
+
let role: AccessibilityRole = props.accessibilityRole || 'none';
|
|
96
|
+
let label = props.accessibilityLabel || '';
|
|
97
|
+
let hint = props.accessibilityHint;
|
|
98
|
+
let value = props.accessibilityValue;
|
|
99
|
+
let interactive = false;
|
|
100
|
+
let actionId = props.actionId;
|
|
101
|
+
let deepLink = props.deepLink;
|
|
102
|
+
|
|
103
|
+
switch (type) {
|
|
104
|
+
case 'Text': {
|
|
105
|
+
if (role === 'none') {
|
|
106
|
+
const typo = props.typography || '';
|
|
107
|
+
role = typo.startsWith('title') || typo === 'largeTitle' || typo === 'headline'
|
|
108
|
+
? 'header'
|
|
109
|
+
: 'text';
|
|
110
|
+
}
|
|
111
|
+
if (!label && props.text !== undefined) {
|
|
112
|
+
label = String(props.text);
|
|
113
|
+
}
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
case 'Button': {
|
|
118
|
+
role = 'button';
|
|
119
|
+
interactive = true;
|
|
120
|
+
if (!label && props.title !== undefined) {
|
|
121
|
+
label = String(props.title);
|
|
122
|
+
}
|
|
123
|
+
if (!hint) {
|
|
124
|
+
hint = actionId ? `Kích hoạt hành động ${actionId}` : 'Chạm để thực hiện';
|
|
125
|
+
}
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
case 'Progress': {
|
|
130
|
+
role = 'progressbar';
|
|
131
|
+
if (!label) label = 'Tiến độ';
|
|
132
|
+
if (value === undefined && props.value !== undefined) {
|
|
133
|
+
value = `${Math.round(props.value * 100)}%`;
|
|
134
|
+
}
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
case 'Gauge': {
|
|
139
|
+
role = 'progressbar';
|
|
140
|
+
label = props.label || label || 'Chỉ số';
|
|
141
|
+
if (value === undefined) {
|
|
142
|
+
value = props.valueText || (props.value !== undefined ? `${Math.round(props.value * 100)}%` : undefined);
|
|
143
|
+
}
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
case 'Timer': {
|
|
148
|
+
role = 'timer';
|
|
149
|
+
if (!label) label = 'Đồng hồ đếm';
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
case 'Toggle': {
|
|
154
|
+
role = 'switch';
|
|
155
|
+
interactive = true;
|
|
156
|
+
if (!label && props.label) label = String(props.label);
|
|
157
|
+
value = props.isChecked ? 'Đã chọn' : 'Chưa chọn';
|
|
158
|
+
hint = 'Chạm để thay đổi trạng thái';
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
case 'Badge': {
|
|
163
|
+
role = 'summary';
|
|
164
|
+
if (!label && props.text) label = String(props.text);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
case 'Image': {
|
|
169
|
+
role = props.systemName || props.url ? 'image' : 'none';
|
|
170
|
+
if (!label && props.systemName) {
|
|
171
|
+
label = `Biểu tượng ${props.systemName}`;
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
default:
|
|
177
|
+
break;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (accessible && (label || value || interactive)) {
|
|
181
|
+
result.push({
|
|
182
|
+
id: node.id || `semantic_${path}`,
|
|
183
|
+
path,
|
|
184
|
+
type,
|
|
185
|
+
role,
|
|
186
|
+
label: label.trim(),
|
|
187
|
+
hint,
|
|
188
|
+
value,
|
|
189
|
+
interactive,
|
|
190
|
+
actionId,
|
|
191
|
+
deepLink,
|
|
192
|
+
isDecorative: !accessible,
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (Array.isArray(node.children)) {
|
|
197
|
+
node.children.forEach((child: any, idx: number) => {
|
|
198
|
+
buildSemanticNodes(child, `${path}.${idx}`, result);
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
function synthesizeSemanticSummary(tree: SemanticNode[]): string {
|
|
204
|
+
const parts: string[] = [];
|
|
205
|
+
for (const node of tree) {
|
|
206
|
+
if (node.isDecorative) continue;
|
|
207
|
+
|
|
208
|
+
let phrase = '';
|
|
209
|
+
if (node.role === 'header' || node.role === 'text') {
|
|
210
|
+
phrase = node.label;
|
|
211
|
+
} else if (node.role === 'button') {
|
|
212
|
+
phrase = `Nút ${node.label}`;
|
|
213
|
+
if (node.hint) phrase += ` (${node.hint})`;
|
|
214
|
+
} else if (node.role === 'progressbar' || node.role === 'timer') {
|
|
215
|
+
phrase = `${node.label} ${node.value ?? ''}`.trim();
|
|
216
|
+
} else if (node.role === 'switch') {
|
|
217
|
+
phrase = `${node.label}: ${node.value ?? ''}`.trim();
|
|
218
|
+
} else if (node.label) {
|
|
219
|
+
phrase = node.label;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
if (phrase && !parts.includes(phrase)) {
|
|
223
|
+
parts.push(phrase);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return parts.join('. ');
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 🎯 Phân loại Render Target (Native RemoteViews vs Canvas Bitmap).
|
|
232
|
+
* Áp dụng nguyên tắc "Native-first, Canvas-when-needed".
|
|
233
|
+
*/
|
|
234
|
+
function classifyRenderTargets(
|
|
235
|
+
node: any,
|
|
236
|
+
registry: Record<string, NodeRenderPlan>,
|
|
237
|
+
onNative: () => void,
|
|
238
|
+
onCanvas: () => void
|
|
239
|
+
): void {
|
|
240
|
+
if (!node || typeof node !== 'object') return;
|
|
241
|
+
|
|
242
|
+
const type = node.type;
|
|
243
|
+
const props = node.props || {};
|
|
244
|
+
const id = node.id || 'anonymous';
|
|
245
|
+
|
|
246
|
+
let target: RenderPlanTarget = 'native_remote_view';
|
|
247
|
+
let reason = '';
|
|
248
|
+
let hasNative = true;
|
|
249
|
+
let requiresCanvas = false;
|
|
250
|
+
|
|
251
|
+
switch (type) {
|
|
252
|
+
case 'Text':
|
|
253
|
+
target = 'native_remote_view';
|
|
254
|
+
reason = 'Ánh xạ trực tiếp sang native TextView với đầy đủ font scaling';
|
|
255
|
+
hasNative = true;
|
|
256
|
+
requiresCanvas = false;
|
|
257
|
+
onNative();
|
|
258
|
+
break;
|
|
259
|
+
|
|
260
|
+
case 'Button':
|
|
261
|
+
target = 'native_remote_view';
|
|
262
|
+
reason = 'Ánh xạ trực tiếp sang native Button với PendingIntent';
|
|
263
|
+
hasNative = true;
|
|
264
|
+
requiresCanvas = false;
|
|
265
|
+
onNative();
|
|
266
|
+
break;
|
|
267
|
+
|
|
268
|
+
case 'Progress':
|
|
269
|
+
if (props.style === 'circular') {
|
|
270
|
+
target = 'canvas_bitmap';
|
|
271
|
+
reason = 'Vòng tròn tiến độ 360° đòi hỏi vẽ đường cong Canvas 2D';
|
|
272
|
+
hasNative = false;
|
|
273
|
+
requiresCanvas = true;
|
|
274
|
+
onCanvas();
|
|
275
|
+
} else {
|
|
276
|
+
target = 'native_remote_view';
|
|
277
|
+
reason = 'Thanh tiến độ ngang ánh xạ trực tiếp sang native ProgressBar';
|
|
278
|
+
hasNative = true;
|
|
279
|
+
requiresCanvas = false;
|
|
280
|
+
onNative();
|
|
281
|
+
}
|
|
282
|
+
break;
|
|
283
|
+
|
|
284
|
+
case 'Gauge':
|
|
285
|
+
target = 'canvas_bitmap';
|
|
286
|
+
reason = 'Đồng hồ cung tròn 240° đòi hỏi tính toán góc quét và bóng kim bằng Canvas';
|
|
287
|
+
hasNative = false;
|
|
288
|
+
requiresCanvas = true;
|
|
289
|
+
onCanvas();
|
|
290
|
+
break;
|
|
291
|
+
|
|
292
|
+
case 'Equalizer':
|
|
293
|
+
target = 'canvas_bitmap';
|
|
294
|
+
reason = 'Dải sóng âm thanh nhiều cột đòi hỏi hoạt họa pha sóng Canvas';
|
|
295
|
+
hasNative = false;
|
|
296
|
+
requiresCanvas = true;
|
|
297
|
+
onCanvas();
|
|
298
|
+
break;
|
|
299
|
+
|
|
300
|
+
case 'Divider':
|
|
301
|
+
target = 'native_remote_view';
|
|
302
|
+
reason = 'Đường phân cách ánh xạ trực tiếp sang native View với background/height';
|
|
303
|
+
hasNative = true;
|
|
304
|
+
requiresCanvas = false;
|
|
305
|
+
onNative();
|
|
306
|
+
break;
|
|
307
|
+
|
|
308
|
+
case 'Image':
|
|
309
|
+
target = 'native_remote_view';
|
|
310
|
+
reason = 'Ảnh ánh xạ trực tiếp sang native ImageView';
|
|
311
|
+
hasNative = true;
|
|
312
|
+
requiresCanvas = false;
|
|
313
|
+
onNative();
|
|
314
|
+
break;
|
|
315
|
+
|
|
316
|
+
case 'VStack':
|
|
317
|
+
case 'HStack':
|
|
318
|
+
case 'ZStack':
|
|
319
|
+
case 'Grid': {
|
|
320
|
+
const hasDashed = props.borderStyle === 'dashed' || props.borderStyle === 'dotted';
|
|
321
|
+
const hasComplexGradient = props.gradient && props.gradient.colors?.length > 2;
|
|
322
|
+
|
|
323
|
+
if (hasDashed || hasComplexGradient) {
|
|
324
|
+
target = 'composite';
|
|
325
|
+
reason = 'Vùng chứa có viền đứt nét hoặc gradient đa điểm cần nền Canvas kèm con Native';
|
|
326
|
+
hasNative = false;
|
|
327
|
+
requiresCanvas = true;
|
|
328
|
+
onCanvas();
|
|
329
|
+
} else {
|
|
330
|
+
target = 'native_remote_view';
|
|
331
|
+
reason = 'Khung bố cục chuẩn ánh xạ sang LinearLayout hoặc FrameLayout';
|
|
332
|
+
hasNative = true;
|
|
333
|
+
requiresCanvas = false;
|
|
334
|
+
onNative();
|
|
335
|
+
}
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
default:
|
|
340
|
+
target = 'native_remote_view';
|
|
341
|
+
reason = 'Thành phần tiêu chuẩn';
|
|
342
|
+
hasNative = true;
|
|
343
|
+
onNative();
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const childrenTargets: RenderPlanTarget[] = [];
|
|
348
|
+
if (Array.isArray(node.children)) {
|
|
349
|
+
node.children.forEach((child: any) => {
|
|
350
|
+
classifyRenderTargets(child, registry, onNative, onCanvas);
|
|
351
|
+
const childPlan = registry[child.id];
|
|
352
|
+
if (childPlan) {
|
|
353
|
+
childrenTargets.push(childPlan.target);
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
registry[id] = {
|
|
359
|
+
id,
|
|
360
|
+
type,
|
|
361
|
+
target,
|
|
362
|
+
reason,
|
|
363
|
+
hasNativeEquivalent: hasNative,
|
|
364
|
+
requiresCanvas,
|
|
365
|
+
childrenTargets: childrenTargets.length > 0 ? childrenTargets : undefined,
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function extractBindings(node: any, bindings: StateBinding[]): void {
|
|
370
|
+
if (!node || typeof node !== 'object') return;
|
|
371
|
+
|
|
372
|
+
const props = node.props || {};
|
|
373
|
+
const actionId = props.actionId;
|
|
374
|
+
|
|
375
|
+
if (actionId) {
|
|
376
|
+
let bindingType: StateBinding['bindingType'] = 'custom';
|
|
377
|
+
let targetKey: string | undefined = undefined;
|
|
378
|
+
|
|
379
|
+
if (actionId.startsWith('toggle:')) {
|
|
380
|
+
bindingType = 'toggle';
|
|
381
|
+
targetKey = actionId.replace('toggle:', '');
|
|
382
|
+
} else if (actionId.startsWith('page:')) {
|
|
383
|
+
bindingType = 'page';
|
|
384
|
+
targetKey = actionId.replace('page:', '');
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
bindings.push({
|
|
388
|
+
id: node.id || `bind_${bindings.length}`,
|
|
389
|
+
actionId,
|
|
390
|
+
bindingType,
|
|
391
|
+
targetKey,
|
|
392
|
+
initialValue: props.isChecked,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
if (Array.isArray(node.children)) {
|
|
397
|
+
node.children.forEach((child: any) => extractBindings(child, bindings));
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* 🔍 Tiện ích gỡ lỗi: In ra cây kiểm tra trực quan của Intermediate Representation.
|
|
403
|
+
*/
|
|
404
|
+
export function inspectIR(input: LayoutNode | WidgetSinglePageConfig): string {
|
|
405
|
+
const ir = compileToIR(input);
|
|
406
|
+
const lines: string[] = [];
|
|
407
|
+
|
|
408
|
+
lines.push(`🌲 Widget IR v${ir.version} [${new Date(ir.timestamp).toISOString()}]`);
|
|
409
|
+
lines.push(`📊 Chiến lược kết xuất: ${ir.renderPlan.overallStrategy.toUpperCase()}`);
|
|
410
|
+
lines.push(` - Native RemoteViews nodes: ${ir.renderPlan.nativeNodeCount}`);
|
|
411
|
+
lines.push(` - Canvas Bitmap nodes: ${ir.renderPlan.canvasNodeCount}`);
|
|
412
|
+
lines.push(`\n♿ Tóm tắt âm thanh Trợ năng (Synthesized A11y Summary):`);
|
|
413
|
+
lines.push(` "${ir.semanticSummary}"`);
|
|
414
|
+
|
|
415
|
+
lines.push(`\n🎯 Kế hoạch phân bổ (Render Plan Tree):`);
|
|
416
|
+
for (const node of ir.semanticTree) {
|
|
417
|
+
const plan = ir.renderPlan.nodes[node.id];
|
|
418
|
+
const targetTag = plan?.target === 'native_remote_view' ? '[NATIVE]' : (plan?.target === 'canvas_bitmap' ? '[CANVAS]' : '[COMPOSITE]');
|
|
419
|
+
lines.push(` ${targetTag} ${node.path} <${node.type}> role=${node.role} label="${node.label}" ${node.interactive ? `(Action: ${node.actionId})` : ''}`);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (ir.stateBindings.length > 0) {
|
|
423
|
+
lines.push(`\n⚡ Liên kết trạng thái (State Bindings):`);
|
|
424
|
+
for (const b of ir.stateBindings) {
|
|
425
|
+
lines.push(` - [${b.bindingType.toUpperCase()}] action=${b.actionId} target=${b.targetKey ?? 'N/A'}`);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
return lines.join('\n');
|
|
430
|
+
}
|
package/src/jsx.tsx
CHANGED
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
LinkNode,
|
|
18
18
|
GaugeNode,
|
|
19
19
|
ChartNode,
|
|
20
|
+
ToggleNode,
|
|
20
21
|
ContainerProps,
|
|
21
22
|
TextStyle,
|
|
22
23
|
ZStackProps,
|
|
@@ -127,6 +128,21 @@ Gauge.displayName = 'Gauge';
|
|
|
127
128
|
export const Chart: React.FC<ChartNode['props']> = () => null;
|
|
128
129
|
Chart.displayName = 'Chart';
|
|
129
130
|
|
|
131
|
+
export type ToggleJSXProps = Partial<ToggleNode['props']> & {
|
|
132
|
+
children?: React.ReactNode;
|
|
133
|
+
actionId?: string;
|
|
134
|
+
key?: string;
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
export const Toggle: React.FC<ToggleJSXProps> = () => null;
|
|
138
|
+
Toggle.displayName = 'Toggle';
|
|
139
|
+
|
|
140
|
+
export const Switch: React.FC<ToggleJSXProps> = () => null;
|
|
141
|
+
Switch.displayName = 'Switch';
|
|
142
|
+
|
|
143
|
+
export const Checkbox: React.FC<ToggleJSXProps> = () => null;
|
|
144
|
+
Checkbox.displayName = 'Checkbox';
|
|
145
|
+
|
|
130
146
|
// ─── 2. RECURSIVE JSX -> LAYOUTNODE CONVERTER ────────────────────────────────
|
|
131
147
|
|
|
132
148
|
/**
|
|
@@ -243,6 +259,12 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
243
259
|
if (isComp(Chart, 'Chart')) {
|
|
244
260
|
return UI.Chart(restProps.values ?? [], restProps);
|
|
245
261
|
}
|
|
262
|
+
if (isComp(Toggle, 'Toggle') || isComp(Switch, 'Switch') || isComp(Checkbox, 'Checkbox')) {
|
|
263
|
+
const label = restProps.label || (typeof children === 'string' ? children : '');
|
|
264
|
+
const actionId = restProps.actionId || (restProps.key ? `toggle:${restProps.key}` : 'toggle:item');
|
|
265
|
+
const style = isComp(Switch, 'Switch') ? 'switch' : (isComp(Checkbox, 'Checkbox') ? 'checkbox' : restProps.style);
|
|
266
|
+
return UI.Toggle(label, { ...restProps, actionId, style });
|
|
267
|
+
}
|
|
246
268
|
|
|
247
269
|
// Component do người dùng tự viết: gọi hàm render để đệ quy phân giải
|
|
248
270
|
try {
|
|
@@ -287,6 +309,14 @@ export function renderToWidget(element: any): LayoutNode | null {
|
|
|
287
309
|
return UI.Spacer(restProps.minLength);
|
|
288
310
|
case 'Divider':
|
|
289
311
|
return UI.Divider(restProps);
|
|
312
|
+
case 'Toggle':
|
|
313
|
+
case 'Switch':
|
|
314
|
+
case 'Checkbox': {
|
|
315
|
+
const label = restProps.label || (typeof children === 'string' ? children : '');
|
|
316
|
+
const actionId = restProps.actionId || 'toggle:item';
|
|
317
|
+
const style = type === 'Switch' ? 'switch' : (type === 'Checkbox' ? 'checkbox' : restProps.style);
|
|
318
|
+
return UI.Toggle(label, { ...restProps, actionId, style });
|
|
319
|
+
}
|
|
290
320
|
default:
|
|
291
321
|
return null;
|
|
292
322
|
}
|