@native-sdk/cli 0.0.0 → 0.4.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.
Files changed (373) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/assets/icon.icns +0 -0
  5. package/assets/icon.ico +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +15 -0
  8. package/assets/native-sdk.entitlements +10 -0
  9. package/assets/native-sdk.manifest +12 -0
  10. package/bin/native.js +156 -0
  11. package/build/app.zig +716 -0
  12. package/build.zig +2593 -0
  13. package/build.zig.zon +24 -0
  14. package/native-sdk.d.ts +615 -0
  15. package/package.json +45 -8
  16. package/skill-data/automation/SKILL.md +248 -0
  17. package/skill-data/core/SKILL.md +249 -0
  18. package/skill-data/core/references/app-model-runtime.md +141 -0
  19. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  20. package/skill-data/core/references/frontend-assets.md +124 -0
  21. package/skill-data/core/references/project-anatomy.md +108 -0
  22. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  23. package/skill-data/native-ui/SKILL.md +1080 -0
  24. package/skills/native-sdk/SKILL.md +33 -0
  25. package/src/app_runner/root.zig +740 -0
  26. package/src/assets/root.zig +20 -0
  27. package/src/automation/protocol.zig +285 -0
  28. package/src/automation/root.zig +13 -0
  29. package/src/automation/server.zig +506 -0
  30. package/src/automation/snapshot.zig +1202 -0
  31. package/src/automation/watcher.zig +165 -0
  32. package/src/bridge/root.zig +507 -0
  33. package/src/debug/root.zig +246 -0
  34. package/src/embed/app_exports.zig +11 -0
  35. package/src/embed/c_api.zig +793 -0
  36. package/src/embed/c_exports.zig +10 -0
  37. package/src/embed/chrome.zig +168 -0
  38. package/src/embed/conversions.zig +359 -0
  39. package/src/embed/host.zig +960 -0
  40. package/src/embed/root.zig +81 -0
  41. package/src/embed/tests.zig +2552 -0
  42. package/src/embed/types.zig +382 -0
  43. package/src/embed/ui_host.zig +289 -0
  44. package/src/extensions/root.zig +197 -0
  45. package/src/frontend/root.zig +48 -0
  46. package/src/js/root.zig +65 -0
  47. package/src/platform/android/NativeSdkActivity.java +1370 -0
  48. package/src/platform/android/android_host.c +696 -0
  49. package/src/platform/android/files.zig +9 -0
  50. package/src/platform/android/native_sdk_app.h +237 -0
  51. package/src/platform/ios/files.zig +8 -0
  52. package/src/platform/ios/native_sdk_app.h +289 -0
  53. package/src/platform/ios/uikit_host.m +2739 -0
  54. package/src/platform/linux/cef_host.cpp +599 -0
  55. package/src/platform/linux/gtk_host.c +5362 -0
  56. package/src/platform/linux/gtk_host.h +265 -0
  57. package/src/platform/linux/root.zig +1519 -0
  58. package/src/platform/macos/appkit_host.h +604 -0
  59. package/src/platform/macos/appkit_host.m +10392 -0
  60. package/src/platform/macos/automation_host.h +14 -0
  61. package/src/platform/macos/automation_host.m +12 -0
  62. package/src/platform/macos/cef_host.mm +3028 -0
  63. package/src/platform/macos/root.zig +2260 -0
  64. package/src/platform/null_platform.zig +2232 -0
  65. package/src/platform/null_platform_tests.zig +670 -0
  66. package/src/platform/policy_values.zig +14 -0
  67. package/src/platform/root.zig +181 -0
  68. package/src/platform/types.zig +2681 -0
  69. package/src/platform/windows/cef_host.cpp +3 -0
  70. package/src/platform/windows/root.zig +1521 -0
  71. package/src/platform/windows/webview2_host.cpp +6230 -0
  72. package/src/primitives/app_dirs/root.zig +440 -0
  73. package/src/primitives/app_manifest/root.zig +109 -0
  74. package/src/primitives/app_manifest/tests.zig +758 -0
  75. package/src/primitives/app_manifest/types.zig +587 -0
  76. package/src/primitives/app_manifest/validation.zig +828 -0
  77. package/src/primitives/assets/root.zig +520 -0
  78. package/src/primitives/canvas/a11y_audit.zig +390 -0
  79. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  80. package/src/primitives/canvas/app_icon.zig +1590 -0
  81. package/src/primitives/canvas/chart.zig +392 -0
  82. package/src/primitives/canvas/chart_tests.zig +751 -0
  83. package/src/primitives/canvas/commands.zig +446 -0
  84. package/src/primitives/canvas/drawing.zig +309 -0
  85. package/src/primitives/canvas/equality.zig +316 -0
  86. package/src/primitives/canvas/events.zig +696 -0
  87. package/src/primitives/canvas/font_coverage.zig +118 -0
  88. package/src/primitives/canvas/font_ttf.zig +510 -0
  89. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  90. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  91. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  92. package/src/primitives/canvas/frame.zig +679 -0
  93. package/src/primitives/canvas/frame_metrics.zig +462 -0
  94. package/src/primitives/canvas/gpu.zig +639 -0
  95. package/src/primitives/canvas/hash.zig +112 -0
  96. package/src/primitives/canvas/icons/alert.svg +1 -0
  97. package/src/primitives/canvas/icons/archive.svg +1 -0
  98. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  99. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  100. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  101. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  102. package/src/primitives/canvas/icons/check.svg +1 -0
  103. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  104. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  105. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  106. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  107. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  108. package/src/primitives/canvas/icons/clock.svg +1 -0
  109. package/src/primitives/canvas/icons/copy.svg +1 -0
  110. package/src/primitives/canvas/icons/download.svg +1 -0
  111. package/src/primitives/canvas/icons/edit.svg +1 -0
  112. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  113. package/src/primitives/canvas/icons/external-link.svg +1 -0
  114. package/src/primitives/canvas/icons/eye.svg +1 -0
  115. package/src/primitives/canvas/icons/file-text.svg +1 -0
  116. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  117. package/src/primitives/canvas/icons/folder.svg +1 -0
  118. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  119. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  120. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  121. package/src/primitives/canvas/icons/info.svg +1 -0
  122. package/src/primitives/canvas/icons/menu.svg +1 -0
  123. package/src/primitives/canvas/icons/missing.svg +1 -0
  124. package/src/primitives/canvas/icons/moon.svg +1 -0
  125. package/src/primitives/canvas/icons/music.svg +1 -0
  126. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  127. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  128. package/src/primitives/canvas/icons/pause.svg +1 -0
  129. package/src/primitives/canvas/icons/play.svg +1 -0
  130. package/src/primitives/canvas/icons/plus.svg +1 -0
  131. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  132. package/src/primitives/canvas/icons/repeat.svg +1 -0
  133. package/src/primitives/canvas/icons/save.svg +1 -0
  134. package/src/primitives/canvas/icons/search.svg +1 -0
  135. package/src/primitives/canvas/icons/send.svg +1 -0
  136. package/src/primitives/canvas/icons/settings.svg +1 -0
  137. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  138. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  139. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  140. package/src/primitives/canvas/icons/sun.svg +1 -0
  141. package/src/primitives/canvas/icons/terminal.svg +1 -0
  142. package/src/primitives/canvas/icons/trash.svg +1 -0
  143. package/src/primitives/canvas/icons/volume.svg +1 -0
  144. package/src/primitives/canvas/icons/wrench.svg +1 -0
  145. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  146. package/src/primitives/canvas/icons/x.svg +1 -0
  147. package/src/primitives/canvas/icons.zig +249 -0
  148. package/src/primitives/canvas/layout_audit.zig +744 -0
  149. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  150. package/src/primitives/canvas/markdown.zig +1031 -0
  151. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  152. package/src/primitives/canvas/markdown_tests.zig +538 -0
  153. package/src/primitives/canvas/plan_key_index.zig +120 -0
  154. package/src/primitives/canvas/png.zig +355 -0
  155. package/src/primitives/canvas/reference.zig +1457 -0
  156. package/src/primitives/canvas/reference_blur.zig +104 -0
  157. package/src/primitives/canvas/reference_memo.zig +322 -0
  158. package/src/primitives/canvas/reference_paths.zig +184 -0
  159. package/src/primitives/canvas/reference_tests.zig +1650 -0
  160. package/src/primitives/canvas/render.zig +747 -0
  161. package/src/primitives/canvas/render_effects.zig +255 -0
  162. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  163. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  164. package/src/primitives/canvas/render_images.zig +247 -0
  165. package/src/primitives/canvas/render_layers.zig +260 -0
  166. package/src/primitives/canvas/render_paths.zig +347 -0
  167. package/src/primitives/canvas/render_resources.zig +51 -0
  168. package/src/primitives/canvas/render_tests.zig +3186 -0
  169. package/src/primitives/canvas/root.zig +744 -0
  170. package/src/primitives/canvas/serialization.zig +1528 -0
  171. package/src/primitives/canvas/svg_icon.zig +636 -0
  172. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  173. package/src/primitives/canvas/test_support.zig +422 -0
  174. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  175. package/src/primitives/canvas/tests.zig +30 -0
  176. package/src/primitives/canvas/text.zig +82 -0
  177. package/src/primitives/canvas/text_atlas.zig +408 -0
  178. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  179. package/src/primitives/canvas/text_interaction.zig +615 -0
  180. package/src/primitives/canvas/text_layout.zig +1069 -0
  181. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  182. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  183. package/src/primitives/canvas/text_layout_types.zig +128 -0
  184. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  185. package/src/primitives/canvas/text_metrics.zig +309 -0
  186. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  187. package/src/primitives/canvas/text_span_tests.zig +608 -0
  188. package/src/primitives/canvas/text_spans.zig +1010 -0
  189. package/src/primitives/canvas/text_tests.zig +1949 -0
  190. package/src/primitives/canvas/themes/geist.zig +470 -0
  191. package/src/primitives/canvas/tokens.zig +1522 -0
  192. package/src/primitives/canvas/ui.zig +2686 -0
  193. package/src/primitives/canvas/ui_markup.zig +3791 -0
  194. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  195. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  196. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  197. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  198. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  199. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  200. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  201. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  202. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  203. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  204. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  205. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  206. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  207. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  208. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  209. package/src/primitives/canvas/ui_provenance.zig +96 -0
  210. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  211. package/src/primitives/canvas/ui_schema.zig +825 -0
  212. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  213. package/src/primitives/canvas/ui_tests.zig +1432 -0
  214. package/src/primitives/canvas/vector.zig +919 -0
  215. package/src/primitives/canvas/vector_tests.zig +547 -0
  216. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  217. package/src/primitives/canvas/virtual_extents.zig +540 -0
  218. package/src/primitives/canvas/widget_access.zig +208 -0
  219. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  220. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  221. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  222. package/src/primitives/canvas/widget_layout.zig +2055 -0
  223. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  224. package/src/primitives/canvas/widget_metrics.zig +228 -0
  225. package/src/primitives/canvas/widget_render.zig +2534 -0
  226. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  227. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  228. package/src/primitives/canvas/widget_render_style.zig +626 -0
  229. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  230. package/src/primitives/canvas/widget_routing.zig +475 -0
  231. package/src/primitives/canvas/widget_runtime.zig +307 -0
  232. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  233. package/src/primitives/canvas/widget_semantics.zig +480 -0
  234. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  235. package/src/primitives/canvas/widget_tests.zig +8 -0
  236. package/src/primitives/canvas/widget_text_input.zig +351 -0
  237. package/src/primitives/canvas/widget_text_select.zig +147 -0
  238. package/src/primitives/canvas/widget_tree.zig +232 -0
  239. package/src/primitives/canvas/widgets.zig +1319 -0
  240. package/src/primitives/diagnostics/root.zig +445 -0
  241. package/src/primitives/geometry/root.zig +889 -0
  242. package/src/primitives/json/root.zig +272 -0
  243. package/src/primitives/platform_info/root.zig +450 -0
  244. package/src/primitives/trace/root.zig +569 -0
  245. package/src/root.zig +165 -0
  246. package/src/runtime/api.zig +431 -0
  247. package/src/runtime/async_bridge.zig +49 -0
  248. package/src/runtime/automation_commands.zig +686 -0
  249. package/src/runtime/automation_liveness_tests.zig +140 -0
  250. package/src/runtime/automation_snapshot.zig +246 -0
  251. package/src/runtime/automation_widget_dispatch.zig +617 -0
  252. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  253. package/src/runtime/bridge_payload.zig +194 -0
  254. package/src/runtime/bridge_permission_tests.zig +551 -0
  255. package/src/runtime/bridge_responses.zig +414 -0
  256. package/src/runtime/builtin_bridge.zig +554 -0
  257. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  258. package/src/runtime/canvas_font_tests.zig +410 -0
  259. package/src/runtime/canvas_fonts.zig +229 -0
  260. package/src/runtime/canvas_frame.zig +1734 -0
  261. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  262. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  263. package/src/runtime/canvas_frame_helpers.zig +414 -0
  264. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  265. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  266. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  267. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  268. package/src/runtime/canvas_frame_tests.zig +9 -0
  269. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  270. package/src/runtime/canvas_image_tests.zig +362 -0
  271. package/src/runtime/canvas_images.zig +224 -0
  272. package/src/runtime/canvas_limits.zig +317 -0
  273. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  274. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  275. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  276. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  277. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  278. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  279. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  280. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  281. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  282. package/src/runtime/canvas_widget_display.zig +677 -0
  283. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  284. package/src/runtime/canvas_widget_events.zig +1098 -0
  285. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  286. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  287. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  288. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  289. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  290. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  291. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  292. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  293. package/src/runtime/canvas_widget_state.zig +798 -0
  294. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  295. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  296. package/src/runtime/clock.zig +231 -0
  297. package/src/runtime/core.zig +927 -0
  298. package/src/runtime/effects.zig +4197 -0
  299. package/src/runtime/effects_audio_tests.zig +690 -0
  300. package/src/runtime/effects_clipboard_tests.zig +317 -0
  301. package/src/runtime/effects_fetch_tests.zig +1031 -0
  302. package/src/runtime/effects_file_tests.zig +407 -0
  303. package/src/runtime/effects_tests.zig +1251 -0
  304. package/src/runtime/flow.zig +1175 -0
  305. package/src/runtime/frame_profile.zig +221 -0
  306. package/src/runtime/gpu_surface_events.zig +639 -0
  307. package/src/runtime/launch_timing.zig +59 -0
  308. package/src/runtime/markdown_app_tests.zig +224 -0
  309. package/src/runtime/native_command_event_tests.zig +431 -0
  310. package/src/runtime/platform_bridge_tests.zig +8 -0
  311. package/src/runtime/platform_config_tests.zig +392 -0
  312. package/src/runtime/root.zig +150 -0
  313. package/src/runtime/runtime_event_tests.zig +1079 -0
  314. package/src/runtime/session_journal.zig +1522 -0
  315. package/src/runtime/session_record.zig +332 -0
  316. package/src/runtime/session_replay.zig +274 -0
  317. package/src/runtime/session_state.zig +30 -0
  318. package/src/runtime/session_tests.zig +399 -0
  319. package/src/runtime/shell_layout.zig +472 -0
  320. package/src/runtime/shell_layout_tests.zig +1246 -0
  321. package/src/runtime/shell_view_tests.zig +604 -0
  322. package/src/runtime/state.zig +110 -0
  323. package/src/runtime/system_services.zig +392 -0
  324. package/src/runtime/test_support.zig +79 -0
  325. package/src/runtime/tests.zig +30 -0
  326. package/src/runtime/ui_app.zig +3290 -0
  327. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  328. package/src/runtime/ui_app_provenance.zig +370 -0
  329. package/src/runtime/ui_app_tests.zig +3254 -0
  330. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  331. package/src/runtime/ui_app_window_tests.zig +442 -0
  332. package/src/runtime/validation.zig +206 -0
  333. package/src/runtime/view.zig +908 -0
  334. package/src/runtime/view_canvas.zig +850 -0
  335. package/src/runtime/view_widget_control.zig +433 -0
  336. package/src/runtime/view_widget_scroll.zig +360 -0
  337. package/src/runtime/view_widget_text.zig +275 -0
  338. package/src/runtime/view_widget_tree.zig +843 -0
  339. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  340. package/src/runtime/widget_bridge.zig +231 -0
  341. package/src/runtime/window_command_bridge_tests.zig +403 -0
  342. package/src/runtime/window_storage.zig +255 -0
  343. package/src/runtime/window_view_runtime.zig +727 -0
  344. package/src/runtime/window_views.zig +334 -0
  345. package/src/security/root.zig +118 -0
  346. package/src/tooling/android.zig +1172 -0
  347. package/src/tooling/assets.zig +128 -0
  348. package/src/tooling/buildgraph.zig +379 -0
  349. package/src/tooling/cef.zig +671 -0
  350. package/src/tooling/codesign.zig +172 -0
  351. package/src/tooling/components/identity_tests.zig +185 -0
  352. package/src/tooling/components/stepper.zig +113 -0
  353. package/src/tooling/components/timeline.native +27 -0
  354. package/src/tooling/components/timeline_item.zig +131 -0
  355. package/src/tooling/default_icon.icns +0 -0
  356. package/src/tooling/default_icon.png +0 -0
  357. package/src/tooling/dev.zig +172 -0
  358. package/src/tooling/doctor.zig +349 -0
  359. package/src/tooling/eject_components.zig +170 -0
  360. package/src/tooling/embedlib.zig +290 -0
  361. package/src/tooling/ios.zig +528 -0
  362. package/src/tooling/manifest.zig +1939 -0
  363. package/src/tooling/package.zig +2388 -0
  364. package/src/tooling/process_tree.zig +114 -0
  365. package/src/tooling/raw_manifest.zig +193 -0
  366. package/src/tooling/root.zig +22 -0
  367. package/src/tooling/templates.zig +3191 -0
  368. package/src/tooling/toolchain.zig +371 -0
  369. package/src/tooling/verbs.zig +204 -0
  370. package/src/tooling/web_engine.zig +135 -0
  371. package/src/tooling/xcodeproj.zig +462 -0
  372. package/src/window_state/root.zig +507 -0
  373. package/index.js +0 -2
@@ -0,0 +1,2352 @@
1
+ //! Markup interpreter: turns a parsed markup document into `Ui(Msg)` nodes
2
+ //! against a concrete Model/Msg pair (grammar reference:
3
+ //! skill-data/native-ui/SKILL.md).
4
+ //!
5
+ //! The document is runtime data but binding and message resolution is
6
+ //! comptime-unrolled: loop item types are collected from the Model at
7
+ //! comptime, `for` scopes carry type-erased item pointers tagged into that
8
+ //! comptime list, and paths resolve through `inline for` field/method
9
+ //! matching. A markup view builds exactly what an equivalent hand-written
10
+ //! `view(ui, model)` would: same structural ids, same handler table.
11
+
12
+ const std = @import("std");
13
+ const canvas = @import("root.zig");
14
+ const markup = @import("ui_markup.zig");
15
+ const ui_provenance = @import("ui_provenance.zig");
16
+
17
+ pub const BuildError = error{ MarkupBuild, OutOfMemory };
18
+
19
+ pub const BuildDiagnostic = struct {
20
+ line: usize = 0,
21
+ column: usize = 0,
22
+ message: []const u8 = "",
23
+ /// Source file the position refers to (relative to the markup root),
24
+ /// stamped by import resolution; empty for single-file documents.
25
+ path: []const u8 = "",
26
+ };
27
+
28
+ /// A resolved binding value. Enums resolve to their tag name so equality
29
+ /// against enum-typed loop variables and literals works uniformly. Defined
30
+ /// in the expression core (ui_markup_expr.zig) and shared with the
31
+ /// comptime-compiled path so both engines convert, compare, and evaluate
32
+ /// values through the same code.
33
+ pub const Value = markup.expr.Value;
34
+
35
+ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
36
+ return struct {
37
+ const Self = @This();
38
+ pub const Ui = canvas.Ui(MsgT);
39
+
40
+ document: markup.MarkupDocument,
41
+ diagnostic: BuildDiagnostic = .{},
42
+
43
+ pub fn init(arena: std.mem.Allocator, source: []const u8) (markup.ParseError || error{OutOfMemory})!Self {
44
+ var parser = markup.Parser.init(arena, source);
45
+ const document = try parser.parse();
46
+ return .{ .document = try markup.canonicalize(arena, document) };
47
+ }
48
+
49
+ pub fn initDiagnostic(arena: std.mem.Allocator, source: []const u8, diagnostic: *markup.MarkupErrorInfo) (markup.ParseError || error{OutOfMemory})!Self {
50
+ var parser = markup.Parser.init(arena, source);
51
+ defer diagnostic.* = parser.diagnostic;
52
+ const document = try parser.parse();
53
+ return .{ .document = try markup.canonicalize(arena, document) };
54
+ }
55
+
56
+ /// Wrap an already-resolved document (the import resolver's
57
+ /// output, or a parsed single-file document). Canonicalize it
58
+ /// first (`markup.canonicalize`) so attribute expressions parse
59
+ /// once instead of per frame; an uncanonicalized document builds
60
+ /// identically through `attrTyped`'s fallback, just slower.
61
+ pub fn fromDocument(document: markup.MarkupDocument) Self {
62
+ return .{ .document = document };
63
+ }
64
+
65
+ /// Build the view for the current model. Compatible with the
66
+ /// hand-written `view(ui, model)` shape.
67
+ pub fn build(self: *Self, ui: *Ui, model: *const ModelT) BuildError!Ui.Node {
68
+ if (self.document.imports.len > 0) {
69
+ return self.failNode(self.document.imports[0], markup.import_unresolved_message);
70
+ }
71
+ const root = self.document.root orelse {
72
+ self.diagnostic = .{ .line = 1, .column = 1, .message = markup.component_file_view_message };
73
+ return error.MarkupBuild;
74
+ };
75
+ var scope = Scope{ .model = model, .arena = ui.arena };
76
+ return self.buildNode(ui, &scope, root);
77
+ }
78
+
79
+ // ------------------------------------------------------- scopes
80
+
81
+ /// Types a `for` loop can iterate: element types of Model slice or
82
+ /// array fields, public array/slice declarations, and public
83
+ /// functions returning slices (optionally taking an arena).
84
+ const item_types = collectItemTypes(ModelT);
85
+
86
+ /// Shared eval-branch budget for this view's Model/Msg
87
+ /// scaled comptime walks (`inline for` over model fields/decls,
88
+ /// msg variants, and `item_types`); see `typeScanQuota`.
89
+ const scan_quota = typeScanQuota(ModelT) + typeScanQuota(MsgT);
90
+
91
+ /// A named value in scope: a `for` loop item (typed pointer tagged
92
+ /// into `item_types`), a slice-valued template arg (iterable by a
93
+ /// `for each` inside the template), a scalar template arg (usable
94
+ /// in bindings, interpolation, and equality), or the anonymous
95
+ /// slot capture a `<use>` with children pushes for its body.
96
+ const ScopeEntry = struct {
97
+ name: []const u8,
98
+ payload: Payload,
99
+
100
+ const Payload = union(enum) {
101
+ item: struct { type_index: usize, ptr: *const anyopaque },
102
+ slice: struct { type_index: usize, ptr: *const anyopaque, len: usize },
103
+ value: Value,
104
+ slot: SlotCapture,
105
+ };
106
+ };
107
+
108
+ /// A `<use>` site's children plus the scope state they must build
109
+ /// under: the consumer's scope, restored when the template body
110
+ /// reaches its `<slot/>`. Pushed with an empty name (never a
111
+ /// binding head), so lookups skip it.
112
+ const SlotCapture = struct {
113
+ nodes: []const markup.MarkupNode,
114
+ len: usize,
115
+ floor: usize,
116
+ template_ctx: ?usize,
117
+ /// The use site's template chain: slot content is authored at
118
+ /// the use site, so its provenance restores to the consumer's
119
+ /// chain exactly like its scope does.
120
+ chain: []const ui_provenance.UseSite = &.{},
121
+ };
122
+
123
+ const max_scope_depth = 16;
124
+
125
+ const Scope = struct {
126
+ model: *const ModelT,
127
+ /// The build arena, threaded to arena-taking scalar binding fns
128
+ /// (`pub fn summary(m: *const Model, arena: std.mem.Allocator)
129
+ /// []const u8`). Strings they produce live exactly as long as
130
+ /// the built tree.
131
+ arena: std.mem.Allocator,
132
+ entries: [max_scope_depth]ScopeEntry = undefined,
133
+ len: usize = 0,
134
+ /// Bindings resolve entries[floor..len] then the model: a
135
+ /// template body sees its args and its own loop variables but
136
+ /// not the loop variables at the expansion site.
137
+ floor: usize = 0,
138
+ /// Template expansion depth: a hard cap on runtime recursion
139
+ /// for documents the validator never saw. Legit nesting is
140
+ /// bounded by define-before-use (checked per expansion via
141
+ /// `template_ctx`) plus lexical slot-content depth.
142
+ use_depth: usize = 0,
143
+ /// Index of the template whose body is currently building, or
144
+ /// null in root/consumer scope. A use inside a body may only
145
+ /// reference earlier templates (the validator's rule, enforced
146
+ /// again here so an unvalidated document cannot recurse).
147
+ template_ctx: ?usize = null,
148
+ /// Template instantiation chain for provenance stamping: the
149
+ /// `<use>` sites (outermost first) that put the node currently
150
+ /// building where it is. Arena-allocated snapshots — pushed by
151
+ /// `buildUse`, restored by `<slot/>` content — so every
152
+ /// stamped `NodeSource` shares one immutable slice.
153
+ source_chain: []const ui_provenance.UseSite = &.{},
154
+
155
+ fn lookup(self: *const Scope, head: []const u8) ?*const ScopeEntry {
156
+ var index = self.len;
157
+ while (index > self.floor) {
158
+ index -= 1;
159
+ if (std.mem.eql(u8, self.entries[index].name, head)) return &self.entries[index];
160
+ }
161
+ return null;
162
+ }
163
+
164
+ /// The innermost slot capture visible to the current template
165
+ /// body (never looks below the floor: an inner template with
166
+ /// no use-site children must not see an outer capture).
167
+ fn slotCapture(self: *const Scope) ?SlotCapture {
168
+ var index = self.len;
169
+ while (index > self.floor) {
170
+ index -= 1;
171
+ if (self.entries[index].payload == .slot) return self.entries[index].payload.slot;
172
+ }
173
+ return null;
174
+ }
175
+ };
176
+
177
+ // ------------------------------------------------------ building
178
+
179
+ fn buildNode(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
180
+ switch (node.kind) {
181
+ .element => return self.buildElement(ui, scope, node),
182
+ .use_block => return self.buildUse(ui, scope, node),
183
+ .template_block => return self.failNode(node, markup.template_top_level_message),
184
+ .import_block => return self.failNode(node, markup.import_top_level_message),
185
+ .slot_block => return self.failNode(node, markup.slot_outside_template_message),
186
+ .text => return self.failNode(node, "text content is only allowed inside text-bearing elements"),
187
+ .for_block, .if_block, .else_block => return self.failNode(node, "structure tags are only allowed inside an element"),
188
+ }
189
+ }
190
+
191
+ /// Build one element and, when the builder collects provenance,
192
+ /// stamp the node's authored source (file, span, template chain)
193
+ /// onto the built `Ui.Node`. Every element — composites included —
194
+ /// funnels through here, so the stamp covers the whole vocabulary
195
+ /// in one place. `finalize` pairs the stamp with the structural id.
196
+ fn buildElement(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
197
+ var built = try self.buildElementInner(ui, scope, node);
198
+ if (ui.provenance_sink != null) {
199
+ const source = try ui.arena.create(ui_provenance.NodeSource);
200
+ source.* = .{
201
+ .src_path = node.src_path,
202
+ .span = node.span,
203
+ .line = node.line,
204
+ .column = node.column,
205
+ .chain = scope.source_chain,
206
+ };
207
+ built.source = source;
208
+ }
209
+ return built;
210
+ }
211
+
212
+ fn buildElementInner(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
213
+ if (std.mem.eql(u8, node.name, "markdown")) {
214
+ return self.buildMarkdown(ui, scope, node);
215
+ }
216
+ if (std.mem.eql(u8, node.name, "stepper")) {
217
+ return self.buildStepper(ui, scope, node);
218
+ }
219
+ if (std.mem.eql(u8, node.name, "step")) {
220
+ // Steps inside a stepper are consumed by buildStepper.
221
+ return self.failNode(node, markup.step_parent_message);
222
+ }
223
+ if (std.mem.eql(u8, node.name, "timeline")) {
224
+ return self.buildTimeline(ui, scope, node);
225
+ }
226
+ if (std.mem.eql(u8, node.name, "timeline-item")) {
227
+ return self.buildTimelineItem(ui, scope, node);
228
+ }
229
+ if (std.mem.eql(u8, node.name, "chart")) {
230
+ return self.buildChart(ui, scope, node);
231
+ }
232
+ if (std.mem.eql(u8, node.name, "series")) {
233
+ // Series inside a chart are consumed by buildChart.
234
+ return self.failNode(node, markup.series_parent_message);
235
+ }
236
+ if (std.mem.eql(u8, node.name, "context-menu")) {
237
+ // Direct context-menu children are consumed by their host
238
+ // element below; one reaching here is misplaced.
239
+ return self.failNode(node, markup.context_menu_parent_message);
240
+ }
241
+ if (std.mem.eql(u8, node.name, "input-group")) {
242
+ return self.buildInputGroup(ui, scope, node);
243
+ }
244
+ if (std.mem.eql(u8, node.name, "input-group-actions")) {
245
+ // Actions rows inside an input-group are consumed by
246
+ // buildInputGroup.
247
+ return self.failNode(node, markup.input_group_actions_parent_message);
248
+ }
249
+ if (std.mem.eql(u8, node.name, "span")) {
250
+ // Spans inside a text paragraph are consumed by
251
+ // buildSpanParagraph; one reaching here has no text
252
+ // parent.
253
+ return self.failNode(node, markup.span_parent_message);
254
+ }
255
+ if (std.mem.eql(u8, node.name, "reactions")) {
256
+ // Reactions inside a bubble are consumed by the bubble's
257
+ // build below; one reaching here has no bubble parent.
258
+ return self.failNode(node, markup.reactions_parent_message);
259
+ }
260
+ const kind = elementKind(node.name) orelse {
261
+ return self.failNode(node, "unknown element");
262
+ };
263
+ // Extract a direct context-menu child: it is metadata on this
264
+ // element (lowered to the declared platform-menu items), not
265
+ // content, so every content rule below sees the remaining
266
+ // children only. Mirrors the validator and the compiled
267
+ // engine.
268
+ var inner = node;
269
+ var context_menu_child: ?markup.MarkupNode = null;
270
+ for (node.children) |child| {
271
+ if (!markup.nodeIsContextMenu(child)) continue;
272
+ if (context_menu_child != null) return self.failNode(child, markup.context_menu_single_message);
273
+ if (!markup.contextMenuHostEligible(node)) return self.failNode(child, markup.context_menu_host_message);
274
+ if (markup.contextMenuShapeError(child)) |info| {
275
+ self.diagnostic = .{ .line = info.line, .column = info.column, .message = info.message, .path = info.path };
276
+ return error.MarkupBuild;
277
+ }
278
+ context_menu_child = child;
279
+ }
280
+ // Extract a direct reactions child the same way: the pill is
281
+ // bubble CHROME (it lowers onto the bubble widget's
282
+ // chrome-text channel), not content, so every content rule
283
+ // below sees the remaining children only. Mirrors the
284
+ // validator and the compiled engine.
285
+ var reactions_child: ?markup.MarkupNode = null;
286
+ for (node.children) |child| {
287
+ if (!markup.nodeIsReactions(child)) continue;
288
+ if (kind != .bubble) return self.failNode(child, markup.reactions_parent_message);
289
+ if (reactions_child != null) return self.failNode(child, markup.reactions_single_message);
290
+ if (markup.reactionsShapeError(child)) |info| {
291
+ self.diagnostic = .{ .line = info.line, .column = info.column, .message = info.message, .path = info.path };
292
+ return error.MarkupBuild;
293
+ }
294
+ reactions_child = child;
295
+ }
296
+ // The bubble's chrome-text channel belongs to the reaction
297
+ // pill; a bare text attribute would silently do nothing, so
298
+ // it is a build error. Mirrors the validator and the
299
+ // compiled engine.
300
+ if (kind == .bubble) {
301
+ if (node.attrEntry("text")) |_| {
302
+ return self.failNode(node, markup.bubble_text_attr_message);
303
+ }
304
+ }
305
+ if (context_menu_child != null or reactions_child != null) {
306
+ const filtered = try ui.arena.alloc(markup.MarkupNode, node.children.len);
307
+ var filtered_len: usize = 0;
308
+ for (node.children) |child| {
309
+ if (markup.nodeIsContextMenu(child)) continue;
310
+ if (markup.nodeIsReactions(child)) continue;
311
+ filtered[filtered_len] = child;
312
+ filtered_len += 1;
313
+ }
314
+ inner.children = filtered[0..filtered_len];
315
+ }
316
+ // Value/text handlers on non-hit-target kinds can never fire
317
+ // (the element has no control or text behavior); reject
318
+ // instead of silently accepting a dead handler. on-press and
319
+ // on-toggle are exempt: a bound press handler makes any
320
+ // element a hit target, and presses on non-interactive
321
+ // content inside it fall through to it. Mirrors the validator
322
+ // and the compiled engine's compile error.
323
+ if (!canvas.widgetKindHitTarget(kind)) {
324
+ for (node.attrs) |attribute| {
325
+ if (std.mem.startsWith(u8, attribute.name, "on-") and markup.deadHandlerOnNonHitTarget(attribute.name)) {
326
+ return self.failNode(node, markup.non_hit_target_handler_message);
327
+ }
328
+ // Autofocus can never land here: nothing about this
329
+ // element is focusable. Mirrors the validator and
330
+ // the compiled engine's compile error.
331
+ if (std.mem.eql(u8, attribute.name, "autofocus")) {
332
+ return self.failNode(node, markup.autofocus_element_message);
333
+ }
334
+ }
335
+ }
336
+ // Stacking kinds give every child the full content box, so a
337
+ // gap can never space them; reject the dead layout data
338
+ // instead of silently stacking children on top of each other.
339
+ // Mirrors the validator and the compiled engine's compile
340
+ // error.
341
+ if (canvas.widgetKindStacksChildren(kind)) {
342
+ for (node.attrs) |attribute| {
343
+ if (std.mem.eql(u8, attribute.name, "gap")) {
344
+ return self.failNode(node, markup.stack_container_gap_message);
345
+ }
346
+ }
347
+ }
348
+ // Only plain text leaves word-wrap or elide; anywhere else
349
+ // the options are silently inert dead layout data. Mirrors
350
+ // the validator and the compiled engine's compile error.
351
+ if (kind != .text) {
352
+ for (node.attrs) |attribute| {
353
+ if (std.mem.eql(u8, attribute.name, "wrap")) {
354
+ return self.failNode(node, markup.wrap_element_message);
355
+ }
356
+ if (std.mem.eql(u8, attribute.name, "overflow")) {
357
+ return self.failNode(node, markup.overflow_element_message);
358
+ }
359
+ }
360
+ }
361
+ // The overflow policy's closed literal vocabulary. Mirrors
362
+ // the validator and the compiled engine's compile error;
363
+ // bindings resolve below like any enum option.
364
+ for (node.attrs) |attribute| {
365
+ if (!std.mem.eql(u8, attribute.name, "overflow")) continue;
366
+ const expression = markup.parseAttrExpression(attribute.value) orelse continue;
367
+ if (expression != .literal) continue;
368
+ if (!markup.nameInList(expression.literal, &markup.overflow_value_names)) {
369
+ return self.failNode(node, markup.overflow_value_message);
370
+ }
371
+ }
372
+ // The size register's closed literal vocabulary: the control
373
+ // scale everywhere, the typography rungs (heading/display) on
374
+ // text only. Mirrors the validator and the compiled engine's
375
+ // compile error; bindings resolve below like any enum option.
376
+ for (node.attrs) |attribute| {
377
+ if (!std.mem.eql(u8, attribute.name, "size")) continue;
378
+ const expression = markup.parseAttrExpression(attribute.value) orelse continue;
379
+ if (expression != .literal) continue;
380
+ if (markup.nameInList(expression.literal, &markup.known_text_size_value_names)) {
381
+ if (kind != .text) {
382
+ return self.failNode(node, markup.text_size_element_message);
383
+ }
384
+ } else if (!markup.nameInList(expression.literal, &markup.known_control_size_value_names)) {
385
+ return self.failNode(node, markup.size_value_message);
386
+ }
387
+ }
388
+ // Splits take exactly two static pane children (the divider
389
+ // sits between fixed panes). Mirrors the validator and the
390
+ // compiled engine's compile error.
391
+ if (kind == .split) {
392
+ var pane_count: usize = 0;
393
+ for (inner.children) |child| {
394
+ switch (child.kind) {
395
+ .element, .use_block => pane_count += 1,
396
+ else => return self.failNode(child, markup.split_children_message),
397
+ }
398
+ }
399
+ if (pane_count != 2) return self.failNode(node, markup.split_children_message);
400
+ }
401
+ // The a11y lint's error half: an unnamed interactive control
402
+ // or a misused role ships a view a screen reader user cannot
403
+ // operate, so it fails the build like any other markup
404
+ // mistake. Mirrors the validator and the compiled engine's
405
+ // compile error.
406
+ if (markup.a11yNameError(node)) |message| {
407
+ return self.failNode(node, message);
408
+ }
409
+ if (markup.a11yRoleError(node)) |message| {
410
+ return self.failNode(node, message);
411
+ }
412
+ var options: Ui.ElementOptions = .{};
413
+ try self.applyAttrs(scope, node, &options);
414
+ // The extracted context-menu lowers through the ordinary
415
+ // element path — its menu-items build like any element
416
+ // (structure tags, interpolation, and message typing all
417
+ // apply) — and the built nodes become the host's declared
418
+ // items. An empty runtime result (every if false, an empty
419
+ // for) simply declares no menu.
420
+ if (context_menu_child) |menu_node| {
421
+ var menu_children: std.ArrayListUnmanaged(Ui.Node) = .empty;
422
+ try self.buildChildList(ui, scope, menu_node.children, &menu_children);
423
+ options.context_menu = ui.contextMenuItemsFromNodes(menu_children.items);
424
+ }
425
+
426
+ if (kind == .icon) {
427
+ // The shared icon value grammar, no children. Mirrors the
428
+ // validator and the compiled engine: a built-in literal is
429
+ // proven here (a typo can never rot), while app: names and
430
+ // bound names ride the explicit icon channel and degrade
431
+ // at draw time to the missing-icon fallback plus a Debug
432
+ // warning naming the value.
433
+ const name_attr = node.attrEntry("name") orelse return self.failNode(node, markup.icon_missing_name_message);
434
+ if (inner.children.len > 0) return self.failNode(node, markup.icon_children_message);
435
+ switch (markup.iconValueOf(name_attr.value, markup.icon_name_message)) {
436
+ .builtin => |name| {
437
+ var built = ui.el(kind, options, .{});
438
+ built.widget.text = name;
439
+ return built;
440
+ },
441
+ .app => |spelled| options.icon = spelled,
442
+ .binding => options.icon = try self.stringAttr(scope, node, name_attr, markup.icon_name_message),
443
+ .invalid => |message| return self.failNode(node, message),
444
+ }
445
+ return ui.el(kind, options, .{});
446
+ }
447
+
448
+ // The span paragraph: a text element with inline <span>
449
+ // children lowers through Ui.paragraph, exactly like a
450
+ // builder span paragraph. Mirrors the validator and the
451
+ // compiled engine.
452
+ if (kind == .text and markup.nodeHasSpanChildren(inner)) {
453
+ return self.buildSpanParagraph(ui, scope, inner, options);
454
+ }
455
+
456
+ // The list-row composite: a text-taking element whose content
457
+ // is element children instead of the text run flows those
458
+ // children inside its own chrome (mixing text and elements is
459
+ // a teaching error; the text path below would silently drop
460
+ // the elements). Mirrors the validator and the compiled
461
+ // engine.
462
+ const composite_children = elementTakesChildren(kind) and markup.nodeHasElementContent(inner);
463
+ if (composite_children) {
464
+ for (inner.children) |child| {
465
+ if (child.kind == .text) return self.failNode(child, markup.text_or_children_content_message);
466
+ }
467
+ }
468
+ if (elementTakesText(kind) and !composite_children) {
469
+ const text = try self.interpolatedText(ui, scope, inner);
470
+ var built = ui.el(kind, options, .{});
471
+ built.widget.text = text;
472
+ // Avatars clip their runtime image to the avatar circle,
473
+ // exactly like `Ui.avatar` (a no-op while the id is 0 and
474
+ // the initials fallback renders).
475
+ if (kind == .avatar) built.widget.image_fit = .cover;
476
+ return built;
477
+ }
478
+
479
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
480
+ try self.buildChildren(ui, scope, inner, &children);
481
+ // Tab triggers ARE segmented controls: markup composes the
482
+ // strip from `<button>` children (segmented-control is a
483
+ // documented markup exclusion), and the engine lowers them to
484
+ // the widget kind tab strips are built on — so the active
485
+ // trigger lifts to the surface with a hairline exactly like
486
+ // the Zig builder's tabs. Handlers ride the widget id, so
487
+ // `selected=`/`on-press` bindings are untouched.
488
+ if (kind == .tabs) lowerTabsTriggers(children.items);
489
+ var built = ui.el(kind, options, @as([]const Ui.Node, children.items));
490
+ // The extracted reactions run lands on the bubble widget's
491
+ // chrome-text channel — the render pass draws it as the
492
+ // docked pill — and the dock rides text_alignment (end is
493
+ // the default: the trailing dock reactions conventionally
494
+ // hang from). Interpolation applies to the run like any
495
+ // text content.
496
+ if (reactions_child) |pill_node| {
497
+ built.widget.text = try self.interpolatedText(ui, scope, pill_node);
498
+ built.widget.text_alignment = if (pill_node.attr("text-alignment")) |raw|
499
+ std.meta.stringToEnum(canvas.TextAlign, raw) orelse
500
+ return self.failNode(pill_node, markup.reactions_alignment_value_message)
501
+ else
502
+ .end;
503
+ }
504
+ return built;
505
+ }
506
+
507
+ fn buildChildren(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
508
+ return self.buildChildList(ui, scope, node.children, out);
509
+ }
510
+
511
+ fn buildChildList(self: *Self, ui: *Ui, scope: *Scope, children: []const markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
512
+ var index: usize = 0;
513
+ while (index < children.len) : (index += 1) {
514
+ const child = children[index];
515
+ switch (child.kind) {
516
+ .element => try out.append(ui.arena, try self.buildElement(ui, scope, child)),
517
+ .use_block => try out.append(ui.arena, try self.buildUse(ui, scope, child)),
518
+ .template_block => return self.failVoid(child, markup.template_top_level_message),
519
+ .import_block => return self.failVoid(child, markup.import_top_level_message),
520
+ .slot_block => try self.buildSlot(ui, scope, child, out),
521
+ .for_block => {
522
+ var else_node: ?markup.MarkupNode = null;
523
+ if (index + 1 < children.len and children[index + 1].kind == .else_block) {
524
+ else_node = children[index + 1];
525
+ index += 1;
526
+ }
527
+ const item_count = try self.buildFor(ui, scope, child, out);
528
+ if (item_count == 0) {
529
+ if (else_node) |else_block| {
530
+ try self.buildChildren(ui, scope, else_block, out);
531
+ }
532
+ }
533
+ },
534
+ .if_block => {
535
+ const test_attr = child.attrEntry("test") orelse {
536
+ return self.failVoid(child, "if requires a test attribute");
537
+ };
538
+ const condition = try self.evalAttrExpression(scope, child, test_attr);
539
+ var else_node: ?markup.MarkupNode = null;
540
+ if (index + 1 < children.len and children[index + 1].kind == .else_block) {
541
+ else_node = children[index + 1];
542
+ index += 1;
543
+ }
544
+ if (condition.truthy()) {
545
+ try self.buildChildren(ui, scope, child, out);
546
+ } else if (else_node) |else_block| {
547
+ try self.buildChildren(ui, scope, else_block, out);
548
+ }
549
+ },
550
+ .else_block => return self.failVoid(child, markup.else_placement_message),
551
+ .text => return self.failVoid(child, "text content is only allowed inside text-bearing elements"),
552
+ }
553
+ }
554
+ }
555
+
556
+ /// `<slot/>` in a template body: build the use-site children (the
557
+ /// slot capture) IN THE CONSUMER'S SCOPE, inline at the slot's
558
+ /// position — the point of slots is that content sees the model
559
+ /// paths and loop variables where the `<use>` was written. The
560
+ /// consumer's scope state is restored around the content build
561
+ /// (and the body's own entries saved, since the entries array is
562
+ /// shared), so structural ids and bindings behave exactly as if
563
+ /// the content were built at the use site and inserted here.
564
+ fn buildSlot(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!void {
565
+ if (node.attrs.len > 0) return self.failVoid(node, markup.slot_attrs_message);
566
+ if (node.children.len > 0) return self.failVoid(node.children[0], markup.slot_children_message);
567
+ const capture = scope.slotCapture() orelse {
568
+ if (scope.template_ctx == null) {
569
+ return self.failVoid(node, markup.slot_outside_template_message);
570
+ }
571
+ // A use with no children: the slot renders empty.
572
+ return;
573
+ };
574
+ if (capture.nodes.len == 0) return;
575
+ var saved_entries: [max_scope_depth]ScopeEntry = undefined;
576
+ const saved_len = scope.len;
577
+ const saved_floor = scope.floor;
578
+ const saved_ctx = scope.template_ctx;
579
+ for (scope.entries[capture.len..saved_len], 0..) |entry, offset| {
580
+ saved_entries[offset] = entry;
581
+ }
582
+ const saved_chain = scope.source_chain;
583
+ scope.len = capture.len;
584
+ scope.floor = capture.floor;
585
+ scope.template_ctx = capture.template_ctx;
586
+ scope.source_chain = capture.chain;
587
+ defer {
588
+ for (saved_entries[0 .. saved_len - capture.len], 0..) |entry, offset| {
589
+ scope.entries[capture.len + offset] = entry;
590
+ }
591
+ scope.len = saved_len;
592
+ scope.floor = saved_floor;
593
+ scope.template_ctx = saved_ctx;
594
+ scope.source_chain = saved_chain;
595
+ }
596
+ try self.buildChildList(ui, scope, capture.nodes, out);
597
+ }
598
+
599
+ /// Expands a `for` block: per item, the whole body (one or more
600
+ /// elements, `use` expansions, and nested `for`/`if`/`else`
601
+ /// structure) is appended to `out`. Returns the item count so the
602
+ /// caller can render a trailing `<else>` for the empty case.
603
+ fn buildFor(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, out: *std.ArrayListUnmanaged(Ui.Node)) BuildError!usize {
604
+ @setEvalBranchQuota(scan_quota);
605
+ const each = node.attr("each") orelse return self.failVoid(node, "for requires an each attribute");
606
+ const as_name = node.attr("as") orelse return self.failVoid(node, "for requires an as attribute");
607
+ const key_field = node.attr("key");
608
+ if (scope.len >= max_scope_depth) return self.failVoid(node, "for nesting is too deep");
609
+ if (node.children.len == 0) return self.failVoid(node, markup.for_children_message);
610
+ for (node.children) |child| {
611
+ switch (child.kind) {
612
+ .element, .use_block, .for_block, .if_block, .else_block, .slot_block => {},
613
+ else => return self.failVoid(child, markup.for_children_message),
614
+ }
615
+ }
616
+
617
+ inline for (item_types, 0..) |Item, type_index| {
618
+ if (try self.iterateItems(ui, Item, type_index, scope, each)) |items| {
619
+ for (items) |*item| {
620
+ scope.entries[scope.len] = .{
621
+ .name = as_name,
622
+ .payload = .{ .item = .{ .type_index = type_index, .ptr = @ptrCast(item) } },
623
+ };
624
+ scope.len += 1;
625
+ defer scope.len -= 1;
626
+
627
+ const first_emitted = out.items.len;
628
+ try self.buildChildren(ui, scope, node, out);
629
+ if (key_field) |field| {
630
+ // The item key stamps every node this item
631
+ // emitted (unless the node claims its own
632
+ // identity); later slots get a slot-suffixed
633
+ // key so same-kind siblings stay distinct.
634
+ const base = try self.itemKey(Item, item, node, field);
635
+ for (out.items[first_emitted..], 0..) |*built, slot| {
636
+ if (built.key == null and built.global_key == null) {
637
+ built.key = try canvas.forSlotKey(ui.arena, base, slot);
638
+ }
639
+ }
640
+ }
641
+ }
642
+ return items.len;
643
+ }
644
+ }
645
+ return self.failVoid(node, "each does not name an iterable (a model slice, array, or fn - or a slice-valued template arg)");
646
+ }
647
+
648
+ /// Resolve `each` to a slice of Item: a slice-valued template arg
649
+ /// in scope, or on the model a field, a public array declaration,
650
+ /// or a public function (with or without arena).
651
+ fn iterateItems(self: *Self, ui: *Ui, comptime Item: type, comptime type_index: usize, scope: *Scope, each: []const u8) BuildError!?[]const Item {
652
+ @setEvalBranchQuota(scan_quota);
653
+ _ = self;
654
+ if (scope.lookup(each)) |entry| {
655
+ switch (entry.payload) {
656
+ .slice => |slice_entry| {
657
+ if (slice_entry.type_index != type_index) return null;
658
+ const items: [*]const Item = @ptrCast(@alignCast(slice_entry.ptr));
659
+ return items[0..slice_entry.len];
660
+ },
661
+ // The name is shadowed by a non-iterable scope entry.
662
+ else => return null,
663
+ }
664
+ }
665
+ const model = scope.model;
666
+ inline for (@typeInfo(ModelT).@"struct".fields) |field| {
667
+ if (comptime sliceElement(field.type) != null and sliceElement(field.type).? == Item) {
668
+ if (std.mem.eql(u8, field.name, each)) {
669
+ return asSlice(Item, &@field(model, field.name));
670
+ }
671
+ }
672
+ }
673
+ inline for (@typeInfo(ModelT).@"struct".decls) |decl| {
674
+ const DeclType = @TypeOf(@field(ModelT, decl.name));
675
+ if (comptime sliceElement(DeclType) != null and sliceElement(DeclType).? == Item) {
676
+ if (std.mem.eql(u8, decl.name, each)) {
677
+ return asSlice(Item, &@field(ModelT, decl.name));
678
+ }
679
+ }
680
+ if (comptime isItemFn(DeclType, Item, false)) {
681
+ if (std.mem.eql(u8, decl.name, each)) {
682
+ return @field(ModelT, decl.name)(model);
683
+ }
684
+ }
685
+ if (comptime isItemFn(DeclType, Item, true)) {
686
+ if (std.mem.eql(u8, decl.name, each)) {
687
+ return @field(ModelT, decl.name)(model, ui.arena);
688
+ }
689
+ }
690
+ }
691
+ return null;
692
+ }
693
+
694
+ // ------------------------------------------------------- markdown
695
+
696
+ const Md = canvas.markdown.Markdown(MsgT);
697
+
698
+ /// `<markdown source="{body}" on-link="open_url"
699
+ /// on-details="toggle_details" details-expanded="{flags}" />`:
700
+ /// a leaf that renders its source binding through
701
+ /// `native_sdk.markdown.Markdown(Msg).view`. Only `source` is
702
+ /// required; without `on-details`/`details-expanded` the details
703
+ /// blocks render collapsed and inert (Md.view's null defaults).
704
+ fn buildMarkdown(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
705
+ if (node.children.len != 0) {
706
+ return self.failNode(node.children[0], markup.markdown_children_message);
707
+ }
708
+ var options: Md.Options = .{};
709
+ var source_text: ?[]const u8 = null;
710
+ for (node.attrs) |attribute| {
711
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
712
+ if (std.mem.eql(u8, attribute.name, "source")) {
713
+ const typed = markup.attrTyped(attribute);
714
+ if (typed != .binding) return self.failNode(node, markup.markdown_source_message);
715
+ const value = try self.evalBinding(scope, node, typed.binding, true);
716
+ source_text = switch (value) {
717
+ .string => |text| text,
718
+ else => return self.failNode(node, markup.markdown_source_message),
719
+ };
720
+ continue;
721
+ }
722
+ if (std.mem.eql(u8, attribute.name, "on-link")) {
723
+ const typed = markup.attrTyped(attribute);
724
+ if (typed != .message or typed.message.payload.len != 0) {
725
+ return self.failNode(node, markup.markdown_on_link_message);
726
+ }
727
+ options.on_link = linkConstructor(typed.message.tag) orelse {
728
+ return self.failNode(node, markup.markdown_on_link_message);
729
+ };
730
+ continue;
731
+ }
732
+ if (std.mem.eql(u8, attribute.name, "on-details")) {
733
+ const typed = markup.attrTyped(attribute);
734
+ if (typed != .message or typed.message.payload.len != 0) {
735
+ return self.failNode(node, markup.markdown_on_details_message);
736
+ }
737
+ options.on_details = detailsConstructor(typed.message.tag) orelse {
738
+ return self.failNode(node, markup.markdown_on_details_message);
739
+ };
740
+ continue;
741
+ }
742
+ if (std.mem.eql(u8, attribute.name, "details-expanded")) {
743
+ const typed = markup.attrTyped(attribute);
744
+ if (typed != .binding) return self.failNode(node, markup.markdown_details_expanded_message);
745
+ options.details_expanded = try self.boolItems(ui, scope, node, typed.binding);
746
+ continue;
747
+ }
748
+ if (std.mem.eql(u8, attribute.name, "issue-link-base")) {
749
+ const typed = markup.attrTyped(attribute);
750
+ if (typed == .equals or typed == .invalid) {
751
+ return self.failNode(node, markup.markdown_issue_link_base_message);
752
+ }
753
+ const value = try self.evalAttrExpression(scope, node, attribute);
754
+ const text = switch (value) {
755
+ .string => |text| text,
756
+ else => return self.failNode(node, markup.markdown_issue_link_base_message),
757
+ };
758
+ if (text.len > 0) options.issue_link_base = text;
759
+ continue;
760
+ }
761
+ return self.failNode(node, markup.markdown_attr_message);
762
+ }
763
+ const source_value = source_text orelse return self.failNode(node, markup.markdown_source_message);
764
+ return Md.view(ui, source_value, options);
765
+ }
766
+
767
+ // ------------------------------------------------ stepper/timeline
768
+
769
+ /// `<stepper active="{stage_index}"><step>Work</step>...</stepper>`:
770
+ /// the composite stage stepper. Steps are text leaves; their
771
+ /// completed/active/pending states derive from position against
772
+ /// the active index, mirroring `Ui.stepper`.
773
+ fn buildStepper(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
774
+ var options: Ui.StepperOptions = .{};
775
+ var has_active = false;
776
+ for (node.attrs) |attribute| {
777
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
778
+ if (std.mem.eql(u8, attribute.name, "active")) {
779
+ const value = try self.evalAttrExpression(scope, node, attribute);
780
+ options.active = switch (value) {
781
+ .integer => |int| if (int < 0) 0 else @intCast(int),
782
+ else => return self.failNode(node, markup.stepper_active_message),
783
+ };
784
+ has_active = true;
785
+ continue;
786
+ }
787
+ if (std.mem.eql(u8, attribute.name, "key")) {
788
+ options.key = try self.attrKey(scope, node, attribute);
789
+ continue;
790
+ }
791
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
792
+ options.global_key = try self.attrKey(scope, node, attribute);
793
+ continue;
794
+ }
795
+ if (std.mem.eql(u8, attribute.name, "label")) {
796
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
797
+ continue;
798
+ }
799
+ return self.failNode(node, markup.stepper_attr_message);
800
+ }
801
+ if (!has_active) return self.failNode(node, markup.stepper_active_message);
802
+
803
+ const steps = try ui.arena.alloc(Ui.StepperStep, node.children.len);
804
+ for (node.children, 0..) |child, index| {
805
+ if (child.kind != .element or !std.mem.eql(u8, child.name, "step")) {
806
+ return self.failNode(child, markup.stepper_children_message);
807
+ }
808
+ for (child.attrs) |attribute| {
809
+ if (!std.mem.eql(u8, attribute.name, "kind")) {
810
+ return self.failNode(child, markup.step_attr_message);
811
+ }
812
+ }
813
+ steps[index] = .{ .label = try self.interpolatedText(ui, scope, child) };
814
+ }
815
+ return ui.stepper(options, steps);
816
+ }
817
+
818
+ /// `<input-group>` — the composer-grade grouped input: exactly
819
+ /// one textarea child (built through the ordinary element path,
820
+ /// so its text/placeholder/on-input/autofocus behave like any
821
+ /// textarea) plus an optional input-group-actions row, lowered
822
+ /// through `Ui.inputGroup` so the chrome-dissolve and
823
+ /// focus-within treatment match Zig-built groups.
824
+ fn buildInputGroup(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
825
+ var options: Ui.InputGroupOptions = .{};
826
+ for (node.attrs) |attribute| {
827
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
828
+ if (std.mem.eql(u8, attribute.name, "label")) {
829
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
830
+ continue;
831
+ }
832
+ if (std.mem.eql(u8, attribute.name, "width")) {
833
+ options.width = try self.floatAttr(scope, node, attribute);
834
+ continue;
835
+ }
836
+ if (std.mem.eql(u8, attribute.name, "height")) {
837
+ options.height = try self.floatAttr(scope, node, attribute);
838
+ continue;
839
+ }
840
+ if (std.mem.eql(u8, attribute.name, "min-width")) {
841
+ options.min_width = try self.floatAttr(scope, node, attribute);
842
+ continue;
843
+ }
844
+ if (std.mem.eql(u8, attribute.name, "grow")) {
845
+ options.grow = try self.floatAttr(scope, node, attribute);
846
+ continue;
847
+ }
848
+ if (std.mem.eql(u8, attribute.name, "key")) {
849
+ options.key = try self.attrKey(scope, node, attribute);
850
+ continue;
851
+ }
852
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
853
+ options.global_key = try self.attrKey(scope, node, attribute);
854
+ continue;
855
+ }
856
+ return self.failNode(node, markup.input_group_attr_message);
857
+ }
858
+ // The child shape is static (mirrors the validator): the
859
+ // textarea first, then at most one actions row. Conditional
860
+ // content belongs INSIDE the actions row.
861
+ var entry: ?Ui.Node = null;
862
+ var actions: ?Ui.Node = null;
863
+ for (node.children) |child| {
864
+ if (child.kind != .element) return self.failNode(child, markup.input_group_children_message);
865
+ if (std.mem.eql(u8, child.name, "textarea")) {
866
+ if (entry != null or actions != null) return self.failNode(child, markup.input_group_children_message);
867
+ entry = try self.buildElement(ui, scope, child);
868
+ continue;
869
+ }
870
+ if (std.mem.eql(u8, child.name, "input-group-actions")) {
871
+ if (entry == null) return self.failNode(child, markup.input_group_textarea_message);
872
+ if (actions != null) return self.failNode(child, markup.input_group_children_message);
873
+ actions = try self.buildInputGroupActions(ui, scope, child);
874
+ continue;
875
+ }
876
+ return self.failNode(child, markup.input_group_children_message);
877
+ }
878
+ const entry_node = entry orelse return self.failNode(node, markup.input_group_textarea_message);
879
+ return ui.inputGroup(options, entry_node, actions);
880
+ }
881
+
882
+ /// `<input-group-actions>` — the group's accessory row; children
883
+ /// build through the ordinary pass (structure tags work) and
884
+ /// lower through `Ui.inputGroupActions`.
885
+ fn buildInputGroupActions(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
886
+ var options: Ui.InputGroupActionsOptions = .{};
887
+ for (node.attrs) |attribute| {
888
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
889
+ if (std.mem.eql(u8, attribute.name, "gap")) {
890
+ options.gap = try self.floatAttr(scope, node, attribute);
891
+ continue;
892
+ }
893
+ if (std.mem.eql(u8, attribute.name, "key")) {
894
+ options.key = try self.attrKey(scope, node, attribute);
895
+ continue;
896
+ }
897
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
898
+ options.global_key = try self.attrKey(scope, node, attribute);
899
+ continue;
900
+ }
901
+ return self.failNode(node, markup.input_group_actions_attr_message);
902
+ }
903
+ for (node.children) |child| {
904
+ if (child.kind == .text) return self.failNode(child, markup.input_group_actions_children_message);
905
+ }
906
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
907
+ try self.buildChildren(ui, scope, node, &children);
908
+ return ui.inputGroupActions(options, @as([]const Ui.Node, children.items));
909
+ }
910
+
911
+ /// `<timeline gap="4">` — a list container whose children are
912
+ /// timeline-item elements (structure tags work); mirrors
913
+ /// `Ui.timeline`.
914
+ fn buildTimeline(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
915
+ var options: Ui.TimelineOptions = .{};
916
+ for (node.attrs) |attribute| {
917
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
918
+ if (std.mem.eql(u8, attribute.name, "gap")) {
919
+ options.gap = try self.floatAttr(scope, node, attribute);
920
+ continue;
921
+ }
922
+ if (std.mem.eql(u8, attribute.name, "grow")) {
923
+ options.grow = try self.floatAttr(scope, node, attribute);
924
+ continue;
925
+ }
926
+ if (std.mem.eql(u8, attribute.name, "key")) {
927
+ options.key = try self.attrKey(scope, node, attribute);
928
+ continue;
929
+ }
930
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
931
+ options.global_key = try self.attrKey(scope, node, attribute);
932
+ continue;
933
+ }
934
+ if (std.mem.eql(u8, attribute.name, "label")) {
935
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
936
+ continue;
937
+ }
938
+ return self.failNode(node, markup.timeline_attr_message);
939
+ }
940
+ var children: std.ArrayListUnmanaged(Ui.Node) = .empty;
941
+ try self.buildChildren(ui, scope, node, &children);
942
+ return ui.timeline(options, @as([]const Ui.Node, children.items));
943
+ }
944
+
945
+ /// `<timeline-item title="{entry.title}" description="..."
946
+ /// meta="..." variant="primary" on-press="open_step:{entry.slot}"/>`:
947
+ /// one composite ledger item; mirrors `Ui.timelineItem`.
948
+ fn buildTimelineItem(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
949
+ if (node.children.len != 0) {
950
+ return self.failNode(node.children[0], markup.timeline_item_children_message);
951
+ }
952
+ var options: Ui.TimelineItemOptions = .{ .title = "" };
953
+ var has_title = false;
954
+ for (node.attrs) |attribute| {
955
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
956
+ if (std.mem.eql(u8, attribute.name, "title")) {
957
+ options.title = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
958
+ has_title = true;
959
+ continue;
960
+ }
961
+ if (std.mem.eql(u8, attribute.name, "description")) {
962
+ options.description = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
963
+ continue;
964
+ }
965
+ if (std.mem.eql(u8, attribute.name, "meta")) {
966
+ options.meta = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
967
+ continue;
968
+ }
969
+ if (std.mem.eql(u8, attribute.name, "indicator")) {
970
+ options.indicator = try self.stringAttr(scope, node, attribute, markup.timeline_item_text_attr_message);
971
+ continue;
972
+ }
973
+ if (std.mem.eql(u8, attribute.name, "icon")) {
974
+ // Vector icon indicator: the shared icon value grammar,
975
+ // like every icon attribute.
976
+ switch (markup.iconValueOf(attribute.value, markup.button_icon_message)) {
977
+ .builtin, .app => |name| options.icon = name,
978
+ .binding => options.icon = try self.stringAttr(scope, node, attribute, markup.button_icon_message),
979
+ .invalid => |message| return self.failVoid(node, message),
980
+ }
981
+ continue;
982
+ }
983
+ if (std.mem.eql(u8, attribute.name, "variant")) {
984
+ const text = try self.stringAttr(scope, node, attribute, "expected an option name");
985
+ options.variant = std.meta.stringToEnum(canvas.WidgetVariant, text) orelse {
986
+ return self.failNode(node, "unknown option value");
987
+ };
988
+ continue;
989
+ }
990
+ if (std.mem.eql(u8, attribute.name, "connector")) {
991
+ options.connector = (try self.evalAttrExpression(scope, node, attribute)).truthy();
992
+ continue;
993
+ }
994
+ if (std.mem.eql(u8, attribute.name, "selected")) {
995
+ options.selected = (try self.evalAttrExpression(scope, node, attribute)).truthy();
996
+ continue;
997
+ }
998
+ if (std.mem.eql(u8, attribute.name, "on-press")) {
999
+ // Reuse the full message-attr machinery (payload
1000
+ // bindings included) through a scratch options value.
1001
+ var scratch: Ui.ElementOptions = .{};
1002
+ try self.applyMessageAttr(scope, node, &scratch, attribute);
1003
+ options.on_press = scratch.on_press;
1004
+ continue;
1005
+ }
1006
+ if (std.mem.startsWith(u8, attribute.name, "on-")) {
1007
+ return self.failNode(node, markup.timeline_item_press_only_message);
1008
+ }
1009
+ if (std.mem.eql(u8, attribute.name, "key")) {
1010
+ options.key = try self.attrKey(scope, node, attribute);
1011
+ continue;
1012
+ }
1013
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
1014
+ options.global_key = try self.attrKey(scope, node, attribute);
1015
+ continue;
1016
+ }
1017
+ return self.failNode(node, markup.timeline_item_attr_message);
1018
+ }
1019
+ if (!has_title) return self.failNode(node, markup.timeline_item_title_message);
1020
+ return ui.timelineItem(options);
1021
+ }
1022
+
1023
+ // ---------------------------------------------------------- chart
1024
+
1025
+ /// `<chart y-min="0" grid-lines="4"><series kind="area"
1026
+ /// values="{cpuHistory}" color="accent"/></chart>`: the
1027
+ /// data-visualization composite, lowered through `Ui.chart` so
1028
+ /// markup charts get the same downsampling, semantics summary, and
1029
+ /// invalidation as Zig-built ones. Series values bind model
1030
+ /// iterables of f32 through the SAME resolution set as `for each`
1031
+ /// (slice-valued template args included); the series set itself is
1032
+ /// static — the data varies, the plot shape does not.
1033
+ fn buildChart(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1034
+ var options: Ui.ChartOptions = .{};
1035
+ for (node.attrs) |attribute| {
1036
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
1037
+ if (std.mem.startsWith(u8, attribute.name, "on-")) {
1038
+ return self.failNode(node, markup.chart_display_only_message);
1039
+ }
1040
+ if (std.mem.eql(u8, attribute.name, "y-min")) {
1041
+ options.y_min = try self.floatAttr(scope, node, attribute);
1042
+ continue;
1043
+ }
1044
+ if (std.mem.eql(u8, attribute.name, "y-max")) {
1045
+ options.y_max = try self.floatAttr(scope, node, attribute);
1046
+ continue;
1047
+ }
1048
+ if (std.mem.eql(u8, attribute.name, "grid-lines")) {
1049
+ const value = try self.evalAttrExpression(scope, node, attribute);
1050
+ options.grid_lines = switch (value) {
1051
+ .integer => |int| if (int < 0 or int > std.math.maxInt(u8))
1052
+ return self.failNode(node, "expected a non-negative whole number")
1053
+ else
1054
+ @intCast(int),
1055
+ else => return self.failNode(node, "expected a whole number"),
1056
+ };
1057
+ continue;
1058
+ }
1059
+ if (std.mem.eql(u8, attribute.name, "baseline")) {
1060
+ options.baseline = (try self.evalAttrExpression(scope, node, attribute)).truthy();
1061
+ continue;
1062
+ }
1063
+ if (std.mem.eql(u8, attribute.name, "x-labels")) {
1064
+ const typed = markup.attrTyped(attribute);
1065
+ if (typed != .binding) return self.failNode(node, markup.chart_x_labels_message);
1066
+ options.x_labels = try self.stringItems(ui, scope, node, typed.binding, markup.chart_x_labels_message);
1067
+ continue;
1068
+ }
1069
+ if (std.mem.eql(u8, attribute.name, "y-labels")) {
1070
+ options.y_labels = (try self.evalAttrExpression(scope, node, attribute)).truthy();
1071
+ continue;
1072
+ }
1073
+ if (std.mem.eql(u8, attribute.name, "hover-details")) {
1074
+ options.hover_details = (try self.evalAttrExpression(scope, node, attribute)).truthy();
1075
+ continue;
1076
+ }
1077
+ if (std.mem.eql(u8, attribute.name, "stroke-width")) {
1078
+ options.stroke_width = try self.floatAttr(scope, node, attribute);
1079
+ continue;
1080
+ }
1081
+ if (std.mem.eql(u8, attribute.name, "width")) {
1082
+ options.width = try self.floatAttr(scope, node, attribute);
1083
+ continue;
1084
+ }
1085
+ if (std.mem.eql(u8, attribute.name, "height")) {
1086
+ options.height = try self.floatAttr(scope, node, attribute);
1087
+ continue;
1088
+ }
1089
+ if (std.mem.eql(u8, attribute.name, "grow")) {
1090
+ options.grow = try self.floatAttr(scope, node, attribute);
1091
+ continue;
1092
+ }
1093
+ if (std.mem.eql(u8, attribute.name, "padding")) {
1094
+ options.padding = try self.floatAttr(scope, node, attribute);
1095
+ continue;
1096
+ }
1097
+ if (std.mem.eql(u8, attribute.name, "key")) {
1098
+ options.key = try self.attrKey(scope, node, attribute);
1099
+ continue;
1100
+ }
1101
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
1102
+ options.global_key = try self.attrKey(scope, node, attribute);
1103
+ continue;
1104
+ }
1105
+ if (std.mem.eql(u8, attribute.name, "label")) {
1106
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
1107
+ continue;
1108
+ }
1109
+ return self.failNode(node, markup.chart_attr_message);
1110
+ }
1111
+ if (node.children.len == 0) return self.failNode(node, markup.chart_series_required_message);
1112
+ const series = try ui.arena.alloc(canvas.ChartSeries, node.children.len);
1113
+ for (node.children, 0..) |child, index| {
1114
+ if (child.kind != .element or !std.mem.eql(u8, child.name, "series")) {
1115
+ return self.failNode(child, markup.chart_children_message);
1116
+ }
1117
+ series[index] = try self.buildSeries(ui, scope, child);
1118
+ }
1119
+ return ui.chart(options, series);
1120
+ }
1121
+
1122
+ fn buildSeries(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.ChartSeries {
1123
+ if (node.children.len != 0) {
1124
+ return self.failVoid(node.children[0], markup.series_children_message);
1125
+ }
1126
+ var series = canvas.ChartSeries{};
1127
+ var has_values = false;
1128
+ for (node.attrs) |attribute| {
1129
+ if (std.mem.eql(u8, attribute.name, "kind")) {
1130
+ // Closed literal vocabulary: area is the markup
1131
+ // spelling of a filled line; band stays with the
1132
+ // builder (it needs a paired lower edge).
1133
+ const typed = markup.attrTyped(attribute);
1134
+ if (typed != .literal) return self.failVoid(node, markup.series_kind_message);
1135
+ if (std.mem.eql(u8, typed.literal, "line")) {
1136
+ series.kind = .line;
1137
+ } else if (std.mem.eql(u8, typed.literal, "area")) {
1138
+ series.kind = .line;
1139
+ series.fill = true;
1140
+ } else if (std.mem.eql(u8, typed.literal, "bar")) {
1141
+ series.kind = .bar;
1142
+ } else {
1143
+ return self.failVoid(node, markup.series_kind_message);
1144
+ }
1145
+ continue;
1146
+ }
1147
+ if (std.mem.eql(u8, attribute.name, "values")) {
1148
+ const typed = markup.attrTyped(attribute);
1149
+ if (typed != .binding) return self.failVoid(node, markup.series_values_message);
1150
+ series.values = try self.f32Items(ui, scope, node, typed.binding);
1151
+ has_values = true;
1152
+ continue;
1153
+ }
1154
+ if (std.mem.eql(u8, attribute.name, "color")) {
1155
+ const typed = markup.attrTyped(attribute);
1156
+ if (typed != .literal) return self.failVoid(node, markup.series_color_message);
1157
+ series.color = std.meta.stringToEnum(canvas.ChartSeriesColor, typed.literal) orelse {
1158
+ return self.failVoid(node, markup.series_color_message);
1159
+ };
1160
+ continue;
1161
+ }
1162
+ if (std.mem.eql(u8, attribute.name, "label")) {
1163
+ series.label = try self.stringAttr(scope, node, attribute, markup.series_label_message);
1164
+ continue;
1165
+ }
1166
+ return self.failVoid(node, markup.series_attr_message);
1167
+ }
1168
+ if (!has_values) return self.failVoid(node, markup.series_values_message);
1169
+ return series;
1170
+ }
1171
+
1172
+ /// Resolve a series `values` binding to an f32 slice through the
1173
+ /// same sources `for each` accepts (scope slice args shadow model
1174
+ /// fields, pub decls, and fns).
1175
+ fn f32Items(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const f32 {
1176
+ @setEvalBranchQuota(scan_quota);
1177
+ inline for (item_types, 0..) |Item, type_index| {
1178
+ if (comptime (Item == f32)) {
1179
+ if (try self.iterateItems(ui, f32, type_index, scope, path)) |items| {
1180
+ return items;
1181
+ }
1182
+ }
1183
+ }
1184
+ return self.failText(node, markup.series_values_message);
1185
+ }
1186
+
1187
+ fn stringAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr, message: []const u8) BuildError![]const u8 {
1188
+ const value = try self.evalAttrExpression(scope, node, attribute);
1189
+ return switch (value) {
1190
+ .string => |text| text,
1191
+ else => self.failValue(node, message),
1192
+ };
1193
+ }
1194
+
1195
+ fn floatAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!f32 {
1196
+ const value = try self.evalAttrExpression(scope, node, attribute);
1197
+ return switch (value) {
1198
+ .float => |float| float,
1199
+ .integer => |int| @floatFromInt(int),
1200
+ else => self.failValue(node, "expected a number"),
1201
+ };
1202
+ }
1203
+
1204
+ /// Msg constructor for markdown link presses: the tag must name a
1205
+ /// `[]const u8` variant (mirrors `Ui.linkMsg`).
1206
+ fn linkConstructor(tag: []const u8) ?Ui.LinkMsgFn {
1207
+ @setEvalBranchQuota(scan_quota);
1208
+ inline for (@typeInfo(MsgT).@"union".fields) |field| {
1209
+ if (field.type == []const u8) {
1210
+ if (std.mem.eql(u8, field.name, tag)) {
1211
+ return Ui.linkMsg(@field(std.meta.Tag(MsgT), field.name));
1212
+ }
1213
+ }
1214
+ }
1215
+ return null;
1216
+ }
1217
+
1218
+ /// Msg constructor for markdown details toggles: the tag must name
1219
+ /// a `usize` variant (mirrors `Markdown(Msg).detailsMsg`).
1220
+ fn detailsConstructor(tag: []const u8) ?*const fn (index: usize) MsgT {
1221
+ @setEvalBranchQuota(scan_quota);
1222
+ inline for (@typeInfo(MsgT).@"union".fields) |field| {
1223
+ if (field.type == usize) {
1224
+ if (std.mem.eql(u8, field.name, tag)) {
1225
+ return Md.detailsMsg(@field(std.meta.Tag(MsgT), field.name));
1226
+ }
1227
+ }
1228
+ }
1229
+ return null;
1230
+ }
1231
+
1232
+ /// Resolve a string-list binding (chart `x-labels`) to a string
1233
+ /// slice through the same sources `for each` accepts (scope slice
1234
+ /// args shadow model fields, pub decls, and fns).
1235
+ fn stringItems(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8, message: []const u8) BuildError![]const []const u8 {
1236
+ @setEvalBranchQuota(scan_quota);
1237
+ inline for (item_types, 0..) |Item, type_index| {
1238
+ if (comptime (Item == []const u8)) {
1239
+ if (try self.iterateItems(ui, []const u8, type_index, scope, path)) |items| {
1240
+ return items;
1241
+ }
1242
+ }
1243
+ }
1244
+ return self.failText(node, message);
1245
+ }
1246
+
1247
+ /// Resolve a `details-expanded` binding to a bool slice through the
1248
+ /// same sources `for each` accepts (scope slice args shadow model
1249
+ /// fields, pub decls, and fns).
1250
+ fn boolItems(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, path: []const u8) BuildError![]const bool {
1251
+ @setEvalBranchQuota(scan_quota);
1252
+ inline for (item_types, 0..) |Item, type_index| {
1253
+ if (comptime (Item == bool)) {
1254
+ if (try self.iterateItems(ui, bool, type_index, scope, path)) |items| {
1255
+ return items;
1256
+ }
1257
+ }
1258
+ }
1259
+ return self.failText(node, markup.markdown_details_expanded_message);
1260
+ }
1261
+
1262
+ // ------------------------------------------------------ templates
1263
+
1264
+ /// A hard cap on template expansion nesting. Legit documents are
1265
+ /// bounded structurally (see `Scope.template_ctx`); the cap turns
1266
+ /// a hostile unvalidated document into an error, never a hang.
1267
+ const max_use_depth = 128;
1268
+
1269
+ /// Build a `<use>` site: evaluate the template args against the
1270
+ /// use-site scope, push them (plus the slot capture when the use
1271
+ /// has children) as scope entries, and build the template's single
1272
+ /// element child in place — structural ids hash through the parent
1273
+ /// chain at the expansion site, exactly as if the body were
1274
+ /// written inline.
1275
+ fn buildUse(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
1276
+ @setEvalBranchQuota(scan_quota);
1277
+ const template_name = node.attr("template") orelse {
1278
+ return self.failNode(node, markup.use_template_attr_message);
1279
+ };
1280
+ const template_index = self.document.templateIndex(template_name) orelse {
1281
+ return self.failNode(node, markup.use_undefined_template_message);
1282
+ };
1283
+ const template_node = self.document.templates[template_index];
1284
+ // The validator's define-before-use rule, enforced again at
1285
+ // build time: it is what makes expansion terminate, so an
1286
+ // unvalidated document must not slip past it.
1287
+ if (scope.template_ctx) |ctx_index| {
1288
+ if (template_index >= ctx_index) {
1289
+ return self.failNode(node, markup.use_earlier_template_message);
1290
+ }
1291
+ }
1292
+ if (scope.use_depth >= max_use_depth) {
1293
+ return self.failNode(node, "template expansion nests too deeply");
1294
+ }
1295
+ if (template_node.children.len != 1 or template_node.children[0].kind != .element) {
1296
+ return self.failNode(template_node, markup.template_one_child_message);
1297
+ }
1298
+ if (markup.templateSecondSlot(template_node.children[0])) |second| {
1299
+ return self.failNode(second, markup.template_one_slot_message);
1300
+ }
1301
+ if (node.children.len != 0 and markup.templateSlot(template_node) == null) {
1302
+ return self.failNode(node.children[0], markup.use_children_without_slot_message);
1303
+ }
1304
+
1305
+ for (node.attrs) |attribute| {
1306
+ if (std.mem.eql(u8, attribute.name, "template")) continue;
1307
+ if (!markup.templateDeclaresArg(template_node, attribute.name)) {
1308
+ return self.failNode(node, markup.use_extra_arg_message);
1309
+ }
1310
+ }
1311
+
1312
+ // Evaluate every arg against the pristine use-site scope before
1313
+ // any entry is pushed, so args cannot see each other.
1314
+ const saved_len = scope.len;
1315
+ const saved_floor = scope.floor;
1316
+ const saved_ctx = scope.template_ctx;
1317
+ var arg_count: usize = 0;
1318
+ var args = markup.templateArgs(template_node);
1319
+ while (args.next()) |token| {
1320
+ const arg = markup.parseTemplateArg(token);
1321
+ if (saved_len + arg_count >= max_scope_depth) {
1322
+ return self.failNode(node, "template args nest too deep");
1323
+ }
1324
+ const payload: ScopeEntry.Payload = if (node.attrEntry(arg.name)) |arg_attr|
1325
+ try self.argPayload(ui, scope, node, arg_attr)
1326
+ else if (arg.default) |default| blk: {
1327
+ // Defaults are literals only — a default cannot see
1328
+ // any scope (validator and compiled-engine parity).
1329
+ if (std.mem.indexOfScalar(u8, default, '{') != null) {
1330
+ return self.failNode(template_node, markup.template_default_literal_message);
1331
+ }
1332
+ // Quotes are not string delimiters in a default; they
1333
+ // would render verbatim (validator parity).
1334
+ if (default.len > 0 and (default[0] == '\'' or default[0] == '"')) {
1335
+ return self.failNode(template_node, markup.template_default_quoted_message);
1336
+ }
1337
+ break :blk .{ .value = literalValue(default) };
1338
+ } else return self.failNode(node, markup.use_missing_arg_message);
1339
+ scope.entries[saved_len + arg_count] = .{ .name = arg.name, .payload = payload };
1340
+ arg_count += 1;
1341
+ }
1342
+ // The slot capture: the use-site children plus the scope state
1343
+ // to build them under, consumed by the body's <slot/>.
1344
+ if (saved_len + arg_count >= max_scope_depth) {
1345
+ return self.failNode(node, "template args nest too deep");
1346
+ }
1347
+ scope.entries[saved_len + arg_count] = .{
1348
+ .name = "",
1349
+ .payload = .{ .slot = .{
1350
+ .nodes = node.children,
1351
+ .len = saved_len,
1352
+ .floor = saved_floor,
1353
+ .template_ctx = saved_ctx,
1354
+ .chain = scope.source_chain,
1355
+ } },
1356
+ };
1357
+ arg_count += 1;
1358
+
1359
+ scope.len = saved_len + arg_count;
1360
+ scope.floor = saved_len;
1361
+ scope.use_depth += 1;
1362
+ scope.template_ctx = template_index;
1363
+ // Provenance: the template body's nodes report this use site
1364
+ // (appended to any outer chain) alongside their definition
1365
+ // site, so "jump to its markup" can offer both.
1366
+ const saved_chain = scope.source_chain;
1367
+ if (ui.provenance_sink != null) {
1368
+ const chain = try ui.arena.alloc(ui_provenance.UseSite, saved_chain.len + 1);
1369
+ @memcpy(chain[0..saved_chain.len], saved_chain);
1370
+ chain[saved_chain.len] = .{
1371
+ .src_path = node.src_path,
1372
+ .span = node.span,
1373
+ .line = node.line,
1374
+ .column = node.column,
1375
+ };
1376
+ scope.source_chain = chain;
1377
+ }
1378
+ defer {
1379
+ scope.len = saved_len;
1380
+ scope.floor = saved_floor;
1381
+ scope.use_depth -= 1;
1382
+ scope.template_ctx = saved_ctx;
1383
+ scope.source_chain = saved_chain;
1384
+ }
1385
+ return self.buildElement(ui, scope, template_node.children[0]);
1386
+ }
1387
+
1388
+ /// A template arg's scope payload: a `{binding}` naming an iterable
1389
+ /// (in scope or on the model, the same resolution set as
1390
+ /// `for each`) binds as a slice; anything else evaluates to a
1391
+ /// `Value` at the use site.
1392
+ fn argPayload(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!ScopeEntry.Payload {
1393
+ @setEvalBranchQuota(scan_quota);
1394
+ const typed = markup.attrTyped(attribute);
1395
+ if (typed == .invalid) {
1396
+ return self.failPayload(node, markup.invalid_expression_message);
1397
+ }
1398
+ if (typed == .binding) {
1399
+ const path = typed.binding;
1400
+ if (scope.lookup(pathHead(path))) |entry| {
1401
+ if (entry.payload == .slice and pathTail(path) == null) {
1402
+ // Re-pass a slice arg to a nested use.
1403
+ return entry.payload;
1404
+ }
1405
+ } else {
1406
+ inline for (item_types, 0..) |Item, type_index| {
1407
+ // Strings stay scalars: a binding producing
1408
+ // []const u8 (a field, zero-arg fn, or arena fn)
1409
+ // binds as a value arg, never as an iterable of
1410
+ // bytes.
1411
+ if (comptime (Item != u8)) {
1412
+ if (try self.iterateItems(ui, Item, type_index, scope, path)) |items| {
1413
+ return .{ .slice = .{
1414
+ .type_index = type_index,
1415
+ .ptr = @ptrCast(items.ptr),
1416
+ .len = items.len,
1417
+ } };
1418
+ }
1419
+ }
1420
+ }
1421
+ }
1422
+ }
1423
+ return .{ .value = try self.evalAttrExpression(scope, node, attribute) };
1424
+ }
1425
+
1426
+ fn failPayload(self: *Self, node: markup.MarkupNode, message: []const u8) BuildError {
1427
+ self.setDiagnostic(node, message);
1428
+ return error.MarkupBuild;
1429
+ }
1430
+
1431
+ fn itemKey(self: *Self, comptime Item: type, item: *const Item, node: markup.MarkupNode, field: []const u8) BuildError!canvas.UiKey {
1432
+ @setEvalBranchQuota(scan_quota);
1433
+ // Keys stay identity-stable data: fields and zero-arg methods
1434
+ // only, never arena-computed values.
1435
+ const value = resolveOn(Item, item, field, null) orelse {
1436
+ return self.failKey(node, "key does not name a field on the item");
1437
+ };
1438
+ return switch (value) {
1439
+ .integer => |int| canvas.uiKey(@as(u64, @intCast(int))),
1440
+ .string => |text| canvas.uiKey(text),
1441
+ else => self.failKey(node, "key fields must be integers or strings"),
1442
+ };
1443
+ }
1444
+
1445
+ // ---------------------------------------------------- attributes
1446
+
1447
+ fn applyAttrs(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions) BuildError!void {
1448
+ for (node.attrs) |attribute| {
1449
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
1450
+ if (std.mem.startsWith(u8, attribute.name, "on-")) {
1451
+ try self.applyMessageAttr(scope, node, options, attribute);
1452
+ continue;
1453
+ }
1454
+ if (std.mem.eql(u8, attribute.name, "key")) {
1455
+ options.key = try self.attrKey(scope, node, attribute);
1456
+ continue;
1457
+ }
1458
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
1459
+ options.global_key = try self.attrKey(scope, node, attribute);
1460
+ continue;
1461
+ }
1462
+ if (std.mem.eql(u8, attribute.name, "role")) {
1463
+ const value = try self.evalAttrExpression(scope, node, attribute);
1464
+ const text = switch (value) {
1465
+ .string => |text| text,
1466
+ else => return self.failVoid(node, "role expects a role name"),
1467
+ };
1468
+ options.semantics.role = std.meta.stringToEnum(canvas.WidgetRole, text) orelse {
1469
+ return self.failVoid(node, "unknown role");
1470
+ };
1471
+ continue;
1472
+ }
1473
+ if (std.mem.eql(u8, attribute.name, "label")) {
1474
+ const value = try self.evalAttrExpression(scope, node, attribute);
1475
+ options.semantics.label = switch (value) {
1476
+ .string => |text| text,
1477
+ else => return self.failVoid(node, "label expects text"),
1478
+ };
1479
+ continue;
1480
+ }
1481
+ if (std.mem.eql(u8, attribute.name, "image")) {
1482
+ try self.applyImageAttr(scope, node, options, attribute);
1483
+ continue;
1484
+ }
1485
+ if (std.mem.eql(u8, attribute.name, "name")) {
1486
+ // Consumed by the icon branch in buildElement.
1487
+ if (!std.mem.eql(u8, node.name, "icon")) {
1488
+ return self.failVoid(node, markup.icon_name_element_message);
1489
+ }
1490
+ continue;
1491
+ }
1492
+ if (std.mem.eql(u8, attribute.name, "icon")) {
1493
+ try self.applyButtonIconAttr(scope, node, options, attribute);
1494
+ continue;
1495
+ }
1496
+ if (std.mem.eql(u8, attribute.name, "anchor")) {
1497
+ try self.applyAnchorAttr(node, options, attribute.value);
1498
+ continue;
1499
+ }
1500
+ if (std.mem.eql(u8, attribute.name, "anchor-alignment")) {
1501
+ try self.applyAnchorAlignmentAttr(node, options, attribute.value);
1502
+ continue;
1503
+ }
1504
+ if (std.mem.eql(u8, attribute.name, "anchor-offset")) {
1505
+ try self.applyAnchorOffsetAttr(node, options, attribute.value);
1506
+ continue;
1507
+ }
1508
+ if (try self.applyStyleTokenAttr(node, options, attribute)) continue;
1509
+ if (!try self.applyOptionAttr(scope, node, options, attribute)) {
1510
+ return self.failVoid(node, "unknown attribute for this element");
1511
+ }
1512
+ }
1513
+ }
1514
+
1515
+ /// Style token references (`background="surface"`, `radius="md"`):
1516
+ /// literal token names only, validated against the token FieldEnums
1517
+ /// and resolved by the builder's `finalizeWithTokens`.
1518
+ fn applyStyleTokenAttr(self: *Self, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!bool {
1519
+ inline for (color_style_attr_fields) |entry| {
1520
+ if (std.mem.eql(u8, attribute.name, entry.markup)) {
1521
+ const literal = try self.styleTokenLiteral(node, attribute);
1522
+ @field(options.style_tokens, entry.zig) = std.meta.stringToEnum(canvas.ColorTokenName, literal) orelse {
1523
+ return self.failVoid(node, markup.unknown_color_token_message);
1524
+ };
1525
+ return true;
1526
+ }
1527
+ }
1528
+ if (std.mem.eql(u8, attribute.name, "radius")) {
1529
+ const literal = try self.styleTokenLiteral(node, attribute);
1530
+ options.style_tokens.radius = std.meta.stringToEnum(canvas.RadiusTokenName, literal) orelse {
1531
+ return self.failVoid(node, markup.unknown_radius_token_message);
1532
+ };
1533
+ return true;
1534
+ }
1535
+ return false;
1536
+ }
1537
+
1538
+ fn styleTokenLiteral(self: *Self, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError![]const u8 {
1539
+ return switch (markup.attrTyped(attribute)) {
1540
+ .literal => |text| text,
1541
+ else => self.failPayload(node, markup.style_token_literal_message),
1542
+ };
1543
+ }
1544
+
1545
+ /// `image="{binding}"` on avatar: one binding producing a `u64`
1546
+ /// `canvas.ImageId` the app registered at runtime
1547
+ /// (`fx.registerImageBytes`) — the id is model data, never a
1548
+ /// markup literal, and 0 keeps the initials fallback. Scoped to
1549
+ /// avatar; the other image-bearing widgets (image, icon,
1550
+ /// icon-button) stay Zig views.
1551
+ fn applyImageAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
1552
+ if (!std.mem.eql(u8, node.name, "avatar")) {
1553
+ return self.failVoid(node, markup.avatar_image_element_message);
1554
+ }
1555
+ const typed = markup.attrTyped(attribute);
1556
+ if (typed != .binding) return self.failVoid(node, markup.avatar_image_message);
1557
+ const value = try self.evalBinding(scope, node, typed.binding, true);
1558
+ options.image = switch (value) {
1559
+ .integer => |int| @intCast(int),
1560
+ else => return self.failVoid(node, markup.avatar_image_message),
1561
+ };
1562
+ }
1563
+
1564
+ /// `icon="save"` on button, toggle-button, list-item, or
1565
+ /// menu-item: the same icon value grammar as `<icon name>` — a
1566
+ /// built-in literal can never rot silently, app:<name> and one
1567
+ /// {binding} defer to the registered set and the model — drawn
1568
+ /// inside the element so icon + label are one hit target with one
1569
+ /// tint. Mirrors the validator and the compiled engine.
1570
+ fn applyButtonIconAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
1571
+ if (!markup.iconAttrElement(node.name)) {
1572
+ return self.failVoid(node, markup.button_icon_element_message);
1573
+ }
1574
+ switch (markup.iconValueOf(attribute.value, markup.button_icon_message)) {
1575
+ .builtin, .app => |name| options.icon = name,
1576
+ .binding => options.icon = try self.stringAttr(scope, node, attribute, markup.button_icon_message),
1577
+ .invalid => |message| return self.failVoid(node, message),
1578
+ }
1579
+ }
1580
+
1581
+ /// `anchor="below|above"` on dropdown-menu: anchored floating
1582
+ /// placement — the surface floats against its parent's frame in
1583
+ /// the late window-level pass instead of the parent's flow.
1584
+ /// Literal placements only, mirroring the validator and the
1585
+ /// compiled engine's comptime resolution.
1586
+ fn applyAnchorAttr(self: *Self, node: markup.MarkupNode, options: *Ui.ElementOptions, raw: []const u8) BuildError!void {
1587
+ if (!markup.anchorElement(node.name)) {
1588
+ return self.failVoid(node, markup.anchor_element_message);
1589
+ }
1590
+ options.anchor = std.meta.stringToEnum(canvas.WidgetAnchorPlacement, raw) orelse {
1591
+ return self.failVoid(node, markup.anchor_value_message);
1592
+ };
1593
+ }
1594
+
1595
+ fn applyAnchorAlignmentAttr(self: *Self, node: markup.MarkupNode, options: *Ui.ElementOptions, raw: []const u8) BuildError!void {
1596
+ if (!markup.anchorElement(node.name)) {
1597
+ return self.failVoid(node, markup.anchor_element_message);
1598
+ }
1599
+ if (node.attr("anchor") == null) return self.failVoid(node, markup.anchor_dependent_attr_message);
1600
+ options.anchor_alignment = std.meta.stringToEnum(canvas.WidgetAnchorAlignment, raw) orelse {
1601
+ return self.failVoid(node, markup.anchor_alignment_value_message);
1602
+ };
1603
+ }
1604
+
1605
+ fn applyAnchorOffsetAttr(self: *Self, node: markup.MarkupNode, options: *Ui.ElementOptions, raw: []const u8) BuildError!void {
1606
+ if (!markup.anchorElement(node.name)) {
1607
+ return self.failVoid(node, markup.anchor_element_message);
1608
+ }
1609
+ if (node.attr("anchor") == null) return self.failVoid(node, markup.anchor_dependent_attr_message);
1610
+ options.anchor_offset = std.fmt.parseFloat(f32, raw) catch {
1611
+ return self.failVoid(node, markup.anchor_offset_value_message);
1612
+ };
1613
+ }
1614
+
1615
+ fn applyOptionAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!bool {
1616
+ inline for (attr_names) |name| {
1617
+ if (std.mem.eql(u8, attribute.name, name.markup)) {
1618
+ try self.setOptionField(scope, node, options, name.zig, attribute);
1619
+ return true;
1620
+ }
1621
+ }
1622
+ return false;
1623
+ }
1624
+
1625
+ fn setOptionField(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, comptime field: []const u8, attribute: markup.MarkupAttr) BuildError!void {
1626
+ const FieldType = @TypeOf(@field(options, field));
1627
+ const value = try self.evalAttrExpression(scope, node, attribute);
1628
+ switch (@typeInfo(FieldType)) {
1629
+ .float => @field(options, field) = switch (value) {
1630
+ .float => |float| float,
1631
+ .integer => |int| @floatFromInt(int),
1632
+ else => return self.failVoid(node, "expected a number"),
1633
+ },
1634
+ .bool => @field(options, field) = value.truthy(),
1635
+ // Optional bools (`expanded`): the attribute's PRESENCE
1636
+ // makes the state non-null; the value sets it.
1637
+ .optional => @field(options, field) = value.truthy(),
1638
+ .int => @field(options, field) = switch (value) {
1639
+ .integer => |int| if (int < 0)
1640
+ return self.failVoid(node, "expected a non-negative whole number")
1641
+ else
1642
+ @intCast(int),
1643
+ else => return self.failVoid(node, "expected a whole number"),
1644
+ },
1645
+ .@"enum" => {
1646
+ const text = switch (value) {
1647
+ .string => |text| text,
1648
+ else => return self.failVoid(node, "expected an option name"),
1649
+ };
1650
+ @field(options, field) = std.meta.stringToEnum(FieldType, text) orelse {
1651
+ return self.failVoid(node, "unknown option value");
1652
+ };
1653
+ },
1654
+ .pointer => @field(options, field) = switch (value) {
1655
+ .string => |text| text,
1656
+ else => return self.failVoid(node, "expected text"),
1657
+ },
1658
+ else => return self.failVoid(node, "attribute is not settable from markup"),
1659
+ }
1660
+ }
1661
+
1662
+ fn attrKey(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!canvas.UiKey {
1663
+ const value = try self.evalAttrExpression(scope, node, attribute);
1664
+ return switch (value) {
1665
+ .integer => |int| canvas.uiKey(@as(u64, @intCast(int))),
1666
+ .string => |text| canvas.uiKey(text),
1667
+ else => self.failKey(node, "keys must be integers or strings"),
1668
+ };
1669
+ }
1670
+
1671
+ fn applyMessageAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, options: *Ui.ElementOptions, attribute: markup.MarkupAttr) BuildError!void {
1672
+ const typed = markup.attrTyped(attribute);
1673
+ if (typed != .message) {
1674
+ return self.failVoid(node, "invalid message expression: on-* takes a Msg tag (\"add\") or tag with one binding payload (\"toggle:{item.id}\")");
1675
+ }
1676
+ const expression = typed.message;
1677
+ const event = attribute.name[3..];
1678
+ if (std.mem.eql(u8, event, "input")) {
1679
+ options.on_input = inputConstructor(expression.tag) orelse {
1680
+ return self.failVoid(node, "on-input tag must carry a TextInputEvent payload");
1681
+ };
1682
+ return;
1683
+ }
1684
+ if (std.mem.eql(u8, event, "scroll")) {
1685
+ if (!std.mem.eql(u8, node.name, "scroll")) {
1686
+ return self.failVoid(node, markup.on_scroll_element_message);
1687
+ }
1688
+ options.on_scroll = scrollConstructor(expression.tag) orelse {
1689
+ return self.failVoid(node, markup.on_scroll_payload_message);
1690
+ };
1691
+ return;
1692
+ }
1693
+ if (std.mem.eql(u8, event, "resize")) {
1694
+ if (!std.mem.eql(u8, node.name, "split")) {
1695
+ return self.failVoid(node, markup.on_resize_element_message);
1696
+ }
1697
+ options.on_resize = resizeConstructor(expression.tag) orelse {
1698
+ return self.failVoid(node, markup.on_resize_payload_message);
1699
+ };
1700
+ return;
1701
+ }
1702
+ const msg = try self.constructMessage(scope, node, expression);
1703
+ if (std.mem.eql(u8, event, "press")) {
1704
+ options.on_press = msg;
1705
+ } else if (std.mem.eql(u8, event, "toggle")) {
1706
+ options.on_toggle = msg;
1707
+ } else if (std.mem.eql(u8, event, "change")) {
1708
+ options.on_change = msg;
1709
+ } else if (std.mem.eql(u8, event, "submit")) {
1710
+ options.on_submit = msg;
1711
+ } else if (std.mem.eql(u8, event, "dismiss")) {
1712
+ // Only dismissible surfaces are ever dismissed by the
1713
+ // runtime; anywhere else the Msg could never fire.
1714
+ if (!markup.dismissEventElement(node.name)) {
1715
+ return self.failVoid(node, markup.on_dismiss_element_message);
1716
+ }
1717
+ options.on_dismiss = msg;
1718
+ } else if (std.mem.eql(u8, event, "hold")) {
1719
+ // Press family: like on-press, a bound hold makes any
1720
+ // element pressable.
1721
+ options.on_hold = msg;
1722
+ } else if (std.mem.eql(u8, event, "reach-end")) {
1723
+ // The approach-end signal (infinite-scroll fetch) is
1724
+ // emitted for scroll containers only.
1725
+ if (!std.mem.eql(u8, node.name, "scroll")) {
1726
+ return self.failVoid(node, markup.on_reach_end_element_message);
1727
+ }
1728
+ options.on_reach_end = msg;
1729
+ } else {
1730
+ return self.failVoid(node, "unknown event attribute");
1731
+ }
1732
+ }
1733
+
1734
+ fn constructMessage(self: *Self, scope: *Scope, node: markup.MarkupNode, expression: markup.MessageExpression) BuildError!MsgT {
1735
+ @setEvalBranchQuota(scan_quota);
1736
+ inline for (@typeInfo(MsgT).@"union".fields) |field| {
1737
+ if (std.mem.eql(u8, field.name, expression.tag)) {
1738
+ if (field.type == void) {
1739
+ if (expression.payload.len > 0) {
1740
+ return self.failMsg(node, "message does not take a payload");
1741
+ }
1742
+ return @unionInit(MsgT, field.name, {});
1743
+ }
1744
+ if (expression.payload.len == 0) {
1745
+ return self.failMsg(node, "message requires a payload");
1746
+ }
1747
+ const value = try self.evalBinding(scope, node, expression.payload, true);
1748
+ return @unionInit(MsgT, field.name, try self.coerce(field.type, node, value));
1749
+ }
1750
+ }
1751
+ return self.failMsg(node, "unknown message tag");
1752
+ }
1753
+
1754
+ fn coerce(self: *Self, comptime T: type, node: markup.MarkupNode, value: Value) BuildError!T {
1755
+ return switch (@typeInfo(T)) {
1756
+ .int => switch (value) {
1757
+ .integer => |int| @intCast(int),
1758
+ else => self.failCoerce(T, node),
1759
+ },
1760
+ .float => switch (value) {
1761
+ .float => |float| @floatCast(float),
1762
+ .integer => |int| @floatFromInt(int),
1763
+ else => self.failCoerce(T, node),
1764
+ },
1765
+ .@"enum" => switch (value) {
1766
+ .string => |text| std.meta.stringToEnum(T, text) orelse self.failCoerce(T, node),
1767
+ else => self.failCoerce(T, node),
1768
+ },
1769
+ .pointer => switch (value) {
1770
+ .string => |text| text,
1771
+ else => self.failCoerce(T, node),
1772
+ },
1773
+ .bool => value.truthy(),
1774
+ else => self.failCoerce(T, node),
1775
+ };
1776
+ }
1777
+
1778
+ fn failCoerce(self: *Self, comptime T: type, node: markup.MarkupNode) BuildError {
1779
+ _ = T;
1780
+ return self.failVoid(node, "payload type does not match the message");
1781
+ }
1782
+
1783
+ fn inputConstructor(tag: []const u8) ?Ui.InputMsgFn {
1784
+ @setEvalBranchQuota(scan_quota);
1785
+ inline for (@typeInfo(MsgT).@"union".fields) |field| {
1786
+ if (field.type == canvas.TextInputEvent) {
1787
+ if (std.mem.eql(u8, field.name, tag)) {
1788
+ return Ui.inputMsg(@field(std.meta.Tag(MsgT), field.name));
1789
+ }
1790
+ }
1791
+ }
1792
+ return null;
1793
+ }
1794
+
1795
+ fn scrollConstructor(tag: []const u8) ?Ui.ScrollMsgFn {
1796
+ @setEvalBranchQuota(scan_quota);
1797
+ inline for (@typeInfo(MsgT).@"union".fields) |field| {
1798
+ if (field.type == canvas.ScrollState) {
1799
+ if (std.mem.eql(u8, field.name, tag)) {
1800
+ return Ui.scrollMsg(@field(std.meta.Tag(MsgT), field.name));
1801
+ }
1802
+ }
1803
+ }
1804
+ return null;
1805
+ }
1806
+
1807
+ fn resizeConstructor(tag: []const u8) ?Ui.ValueMsgFn {
1808
+ @setEvalBranchQuota(scan_quota);
1809
+ inline for (@typeInfo(MsgT).@"union".fields) |field| {
1810
+ if (field.type == f32) {
1811
+ if (std.mem.eql(u8, field.name, tag)) {
1812
+ return Ui.valueMsg(@field(std.meta.Tag(MsgT), field.name));
1813
+ }
1814
+ }
1815
+ }
1816
+ return null;
1817
+ }
1818
+
1819
+ // --------------------------------------------------- expressions
1820
+
1821
+ /// Evaluate an attribute's TYPED value: the canonicalized form
1822
+ /// classified (and its expression tree parsed) once at document
1823
+ /// level, not per frame per use — the typed-document pass's whole
1824
+ /// point for the interpreter.
1825
+ fn evalAttrExpression(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!Value {
1826
+ return switch (markup.attrTyped(attribute)) {
1827
+ .literal => |text| literalValue(text),
1828
+ .binding => |path| try self.evalBinding(scope, node, path, true),
1829
+ // Arena-computed bindings are excluded from equality on
1830
+ // purpose: comparing freshly formatted strings is a smell —
1831
+ // compare the source fields, or bind a bool-returning fn.
1832
+ .equals => |sides| .{ .boolean = Value.eql(
1833
+ try self.evalBinding(scope, node, sides.left, false),
1834
+ try self.evalBinding(scope, node, sides.right, false),
1835
+ ) },
1836
+ .expression => |form| try self.evalExpressionForm(scope, node, form),
1837
+ .message, .invalid => self.failValue(node, markup.invalid_expression_message),
1838
+ };
1839
+ }
1840
+
1841
+ /// A typed expression: use the pre-parsed tree when the pass
1842
+ /// stamped one; a missing tree means the text does not parse (or
1843
+ /// the document was never canonicalized), and the re-parse
1844
+ /// surfaces the exact teaching diagnostic.
1845
+ fn evalExpressionForm(self: *Self, scope: *Scope, node: markup.MarkupNode, form: markup.TypedExprRef) BuildError!Value {
1846
+ if (form.tree) |tree| return self.evalParsedTree(scope, node, tree);
1847
+ return self.evalExpressionTree(scope, node, form.inner);
1848
+ }
1849
+
1850
+ /// Evaluate a full `{expression}`: parse it (bounded, allocation-
1851
+ /// free), resolve every binding node through the ordinary scope
1852
+ /// chain, and hand the values to the shared evaluator — the same
1853
+ /// code the compiled engine runs, so results match bit for bit.
1854
+ /// Parse, type, and value failures (division by zero, overflow)
1855
+ /// become build diagnostics carrying the evaluator's teaching
1856
+ /// message.
1857
+ fn evalExpressionTree(self: *Self, scope: *Scope, node: markup.MarkupNode, inner: []const u8) BuildError!Value {
1858
+ var tree: markup.expr.ExprTree = .{};
1859
+ var diagnostic: markup.expr.Diagnostic = .{};
1860
+ if (!markup.expr.parse(inner, &tree, &diagnostic)) {
1861
+ return self.failValue(node, diagnostic.message);
1862
+ }
1863
+ return self.evalParsedTree(scope, node, &tree);
1864
+ }
1865
+
1866
+ fn evalParsedTree(self: *Self, scope: *Scope, node: markup.MarkupNode, tree: *const markup.expr.ExprTree) BuildError!Value {
1867
+ var values: [markup.expr.max_expression_nodes]Value = undefined;
1868
+ for (tree.nodes[0..tree.len], 0..) |expr_node, index| {
1869
+ if (expr_node.kind != .binding) continue;
1870
+ // Comparison operands reject arena-computed scalars, the
1871
+ // same teaching rule as `{a == b}`.
1872
+ values[index] = try self.evalBinding(scope, node, expr_node.text, !expr_node.comparison_operand);
1873
+ }
1874
+ return switch (try markup.expr.eval(tree, &values, scope.arena)) {
1875
+ .value => |value| value,
1876
+ .fail => |message| self.failValue(node, message),
1877
+ };
1878
+ }
1879
+
1880
+ /// Resolve a binding path to a `Value`. `allow_arena` gates the
1881
+ /// arena-taking scalar fn form (allowed everywhere a scalar binding
1882
+ /// is — text interpolation, attribute values, message payloads —
1883
+ /// except inside `{a == b}` equality).
1884
+ fn evalBinding(self: *Self, scope: *Scope, node: markup.MarkupNode, path: []const u8, allow_arena: bool) BuildError!Value {
1885
+ @setEvalBranchQuota(scan_quota);
1886
+ const head = pathHead(path);
1887
+ const arena: ?std.mem.Allocator = if (allow_arena) scope.arena else null;
1888
+ if (scope.lookup(head)) |entry| {
1889
+ switch (entry.payload) {
1890
+ .item => |item_entry| {
1891
+ inline for (item_types, 0..) |Item, type_index| {
1892
+ if (item_entry.type_index == type_index) {
1893
+ const item: *const Item = @ptrCast(@alignCast(item_entry.ptr));
1894
+ if (pathTail(path)) |tail| {
1895
+ if (resolveOn(Item, item, tail, arena)) |value| return value;
1896
+ if (!allow_arena and resolveOn(Item, item, tail, scope.arena) != null) {
1897
+ return self.failValue(node, markup.arena_scalar_equality_message);
1898
+ }
1899
+ return self.failValue(node, "binding does not name a field on the loop item");
1900
+ }
1901
+ return valueOf(Item, item.*) orelse self.failValue(node, "loop items of this type cannot be used as values");
1902
+ }
1903
+ }
1904
+ unreachable;
1905
+ },
1906
+ .value => |value| {
1907
+ if (pathTail(path) != null) {
1908
+ return self.failValue(node, "template arg values have no fields");
1909
+ }
1910
+ return value;
1911
+ },
1912
+ .slice => return self.failValue(node, "slice-valued template args are only usable with for each"),
1913
+ // Slot captures carry an empty name, which no binding
1914
+ // head can equal.
1915
+ .slot => unreachable,
1916
+ }
1917
+ }
1918
+ if (resolveOn(ModelT, scope.model, path, arena)) |value| return value;
1919
+ if (!allow_arena and resolveOn(ModelT, scope.model, path, scope.arena) != null) {
1920
+ return self.failValue(node, markup.arena_scalar_equality_message);
1921
+ }
1922
+ if (fieldIsTextBuffer(ModelT, head)) {
1923
+ return self.failValue(node, markup.binding_text_buffer_message);
1924
+ }
1925
+ return self.failValue(node, "binding does not name a model field");
1926
+ }
1927
+
1928
+ // ------------------------------------------------ span paragraphs
1929
+
1930
+ /// A `<text>` with inline `<span>` children: the span paragraph.
1931
+ /// Each content child lowers to one `canvas.TextSpan` — plain runs
1932
+ /// (single-space separators the parser spliced included) keep the
1933
+ /// paragraph style, span children carry their own weight, mono,
1934
+ /// italic, and foreground — and the list lowers through
1935
+ /// `Ui.paragraph`, so byte rebasing, span-aware wrapping, and
1936
+ /// semantics match a builder span paragraph exactly: the widget
1937
+ /// announces as ONE text run (spans are visual, never semantic
1938
+ /// children). Mirrors the validator and the compiled engine.
1939
+ fn buildSpanParagraph(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, options: Ui.ElementOptions) BuildError!Ui.Node {
1940
+ // A span paragraph always word-wraps (builder parity), so the
1941
+ // single-line policies are dead data here. Mirrors the
1942
+ // validator and the compiled engine's compile error.
1943
+ for (node.attrs) |attribute| {
1944
+ if (std.mem.eql(u8, attribute.name, "wrap") or std.mem.eql(u8, attribute.name, "overflow")) {
1945
+ return self.failNode(node, markup.span_paragraph_wrap_message);
1946
+ }
1947
+ }
1948
+ const spans = try ui.arena.alloc(canvas.TextSpan, node.children.len);
1949
+ var len: usize = 0;
1950
+ for (node.children) |child| {
1951
+ if (child.kind == .text) {
1952
+ spans[len] = .{ .text = try self.runText(ui, scope, node, child) };
1953
+ len += 1;
1954
+ continue;
1955
+ }
1956
+ if (!markup.nodeIsSpan(child)) return self.failNode(child, markup.text_inline_children_message);
1957
+ spans[len] = try self.buildSpan(ui, scope, child);
1958
+ len += 1;
1959
+ }
1960
+ return ui.paragraph(options, spans[0..len]);
1961
+ }
1962
+
1963
+ /// One `<span>`: the shared shape check (closed attribute set,
1964
+ /// exactly one run, no nesting), then the style channels resolved
1965
+ /// against the scope — weight, scale, and the flags take bindings
1966
+ /// like any option attribute, foreground stays a literal token
1967
+ /// name. A bound scale is held to the same positive-finite bound
1968
+ /// the validator pins for literals: the engine draws anything
1969
+ /// else at the base size, and a silently dead binding is worse
1970
+ /// than a diagnostic.
1971
+ fn buildSpan(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!canvas.TextSpan {
1972
+ if (markup.spanShapeError(node)) |info| {
1973
+ self.diagnostic = .{ .line = info.line, .column = info.column, .message = info.message, .path = info.path };
1974
+ return error.MarkupBuild;
1975
+ }
1976
+ var span = canvas.TextSpan{};
1977
+ for (node.attrs) |attribute| {
1978
+ if (std.mem.eql(u8, attribute.name, "weight")) {
1979
+ const text = try self.stringAttr(scope, node, attribute, markup.span_weight_value_message);
1980
+ span.weight = std.meta.stringToEnum(canvas.TextSpanWeight, text) orelse {
1981
+ return self.failValue(node, markup.span_weight_value_message);
1982
+ };
1983
+ } else if (std.mem.eql(u8, attribute.name, "scale")) {
1984
+ const multiplier = switch (try self.evalAttrExpression(scope, node, attribute)) {
1985
+ .float => |float| float,
1986
+ .integer => |int| @as(f32, @floatFromInt(int)),
1987
+ else => return self.failValue(node, markup.span_scale_value_message),
1988
+ };
1989
+ if (!std.math.isFinite(multiplier) or multiplier <= 0) {
1990
+ return self.failValue(node, markup.span_scale_value_message);
1991
+ }
1992
+ span.scale = multiplier;
1993
+ } else if (std.mem.eql(u8, attribute.name, "mono")) {
1994
+ span.monospace = (try self.evalAttrExpression(scope, node, attribute)).truthy();
1995
+ } else if (std.mem.eql(u8, attribute.name, "italic")) {
1996
+ span.italic = (try self.evalAttrExpression(scope, node, attribute)).truthy();
1997
+ } else if (std.mem.eql(u8, attribute.name, "underline")) {
1998
+ span.underline = (try self.evalAttrExpression(scope, node, attribute)).truthy();
1999
+ } else if (std.mem.eql(u8, attribute.name, "foreground")) {
2000
+ span.color = std.meta.stringToEnum(canvas.TextSpanColor, attribute.value) orelse {
2001
+ return self.failValue(node, markup.unknown_color_token_message);
2002
+ };
2003
+ }
2004
+ // spanShapeError already rejected every other name.
2005
+ }
2006
+ span.text = try self.runText(ui, scope, node, node.children[0]);
2007
+ return span;
2008
+ }
2009
+
2010
+ fn interpolatedText(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError![]const u8 {
2011
+ if (node.children.len > 1) return self.failText(node, "text elements take a single run of text");
2012
+ for (node.children) |child| {
2013
+ if (child.kind != .text) {
2014
+ // A span here sits inside a single-style label, not a
2015
+ // paragraph — teach the <text> home instead of the
2016
+ // generic message.
2017
+ if (markup.nodeIsSpan(child)) return self.failText(child, markup.span_text_only_message);
2018
+ return self.failText(node, "text elements may only contain text");
2019
+ }
2020
+ return self.runText(ui, scope, node, child);
2021
+ }
2022
+ return "";
2023
+ }
2024
+
2025
+ /// Interpolate ONE text run (`{...}` bindings and expressions)
2026
+ /// into the build arena: the shared body behind plain text leaves
2027
+ /// and every span-paragraph run. `node` positions diagnostics.
2028
+ fn runText(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode, run: markup.MarkupNode) BuildError![]const u8 {
2029
+ const source = run.text;
2030
+ const segments = run.typed_text;
2031
+ if (std.mem.indexOfScalar(u8, source, '{') == null) return source;
2032
+
2033
+ var out: std.ArrayListUnmanaged(u8) = .empty;
2034
+ if (segments) |typed_segments| {
2035
+ // The canonicalized fast path: the run was split (and its
2036
+ // expressions parsed) once at document level.
2037
+ for (typed_segments) |segment| {
2038
+ switch (segment) {
2039
+ .literal => |text| try out.appendSlice(ui.arena, text),
2040
+ .binding => |path| try appendValue(&out, ui.arena, try self.evalBinding(scope, node, path, true)),
2041
+ .expression => |form| try appendValue(&out, ui.arena, try self.evalExpressionForm(scope, node, form)),
2042
+ .unterminated => return self.failText(node, "unterminated interpolation"),
2043
+ }
2044
+ }
2045
+ return out.items;
2046
+ }
2047
+ var rest = source;
2048
+ while (std.mem.indexOfScalar(u8, rest, '{')) |open| {
2049
+ try out.appendSlice(ui.arena, rest[0..open]);
2050
+ const close = std.mem.indexOfScalarPos(u8, rest, open, '}') orelse {
2051
+ return self.failText(node, "unterminated interpolation");
2052
+ };
2053
+ const inner = std.mem.trim(u8, rest[open + 1 .. close], " ");
2054
+ const value = if (markup.isBindingPath(inner))
2055
+ try self.evalBinding(scope, node, inner, true)
2056
+ else
2057
+ try self.evalExpressionTree(scope, node, inner);
2058
+ try appendValue(&out, ui.arena, value);
2059
+ rest = rest[close + 1 ..];
2060
+ }
2061
+ try out.appendSlice(ui.arena, rest);
2062
+ return out.items;
2063
+ }
2064
+
2065
+ // -------------------------------------------------- diagnostics
2066
+
2067
+ fn failNode(self: *Self, node: markup.MarkupNode, message: []const u8) BuildError!Ui.Node {
2068
+ self.setDiagnostic(node, message);
2069
+ return error.MarkupBuild;
2070
+ }
2071
+
2072
+ fn failVoid(self: *Self, node: markup.MarkupNode, message: []const u8) BuildError {
2073
+ self.setDiagnostic(node, message);
2074
+ return error.MarkupBuild;
2075
+ }
2076
+
2077
+ fn failValue(self: *Self, node: markup.MarkupNode, message: []const u8) BuildError {
2078
+ self.setDiagnostic(node, message);
2079
+ return error.MarkupBuild;
2080
+ }
2081
+
2082
+ fn failText(self: *Self, node: markup.MarkupNode, message: []const u8) BuildError {
2083
+ self.setDiagnostic(node, message);
2084
+ return error.MarkupBuild;
2085
+ }
2086
+
2087
+ fn failMsg(self: *Self, node: markup.MarkupNode, message: []const u8) BuildError {
2088
+ self.setDiagnostic(node, message);
2089
+ return error.MarkupBuild;
2090
+ }
2091
+
2092
+ fn failKey(self: *Self, node: markup.MarkupNode, message: []const u8) BuildError {
2093
+ self.setDiagnostic(node, message);
2094
+ return error.MarkupBuild;
2095
+ }
2096
+
2097
+ fn setDiagnostic(self: *Self, node: markup.MarkupNode, message: []const u8) void {
2098
+ self.diagnostic = .{ .line = node.line, .column = node.column, .message = message, .path = node.src_path };
2099
+ }
2100
+ };
2101
+ }
2102
+
2103
+ // ----------------------------------------------------------- reflection
2104
+
2105
+ /// Markup attribute name → `Ui.ElementOptions` field, derived from the
2106
+ /// registry (ui_schema.zig). Shared with the comptime-compiled path so
2107
+ /// both engines accept exactly the same attributes; the registry is the
2108
+ /// single statement of the mapping.
2109
+ pub const AttrName = markup.schema.NamePair;
2110
+
2111
+ pub const attr_names: []const AttrName = &markup.schema.option_field_pairs;
2112
+
2113
+ /// Markup color style attribute → `StyleTokenRefs` field, derived from
2114
+ /// the registry's style-color group. Shared with the comptime-compiled
2115
+ /// path; held consistent with the token structs by a conformance test.
2116
+ pub const color_style_attr_fields: []const AttrName = &markup.schema.color_style_field_pairs;
2117
+
2118
+ /// Model/Msg reflection predicates shared with the compiled engine and
2119
+ /// the model-contract describe step (see ui_markup_reflect.zig — one
2120
+ /// definition of what markup can bind, three consumers).
2121
+ const reflect = @import("ui_markup_reflect.zig");
2122
+ pub const typeScanQuota = reflect.typeScanQuota;
2123
+ pub const sliceElement = reflect.sliceElement;
2124
+ pub const isItemFn = reflect.isItemFn;
2125
+ pub const isArenaScalarFn = reflect.isArenaScalarFn;
2126
+
2127
+ fn collectItemTypes(comptime Model: type) []const type {
2128
+ comptime {
2129
+ @setEvalBranchQuota(typeScanQuota(Model));
2130
+ var types: []const type = &.{};
2131
+ for (@typeInfo(Model).@"struct".fields) |field| {
2132
+ if (sliceElement(field.type)) |Element| {
2133
+ types = appendUniqueType(types, Element);
2134
+ }
2135
+ }
2136
+ for (@typeInfo(Model).@"struct".decls) |decl| {
2137
+ const DeclType = @TypeOf(@field(Model, decl.name));
2138
+ if (sliceElement(DeclType)) |Element| {
2139
+ types = appendUniqueType(types, Element);
2140
+ }
2141
+ switch (@typeInfo(DeclType)) {
2142
+ .@"fn" => |info| {
2143
+ if (info.return_type) |Return| {
2144
+ if (sliceElement(Return)) |Element| {
2145
+ types = appendUniqueType(types, Element);
2146
+ }
2147
+ }
2148
+ },
2149
+ else => {},
2150
+ }
2151
+ }
2152
+ return types;
2153
+ }
2154
+ }
2155
+
2156
+ fn appendUniqueType(comptime types: []const type, comptime T: type) []const type {
2157
+ for (types) |existing| {
2158
+ if (existing == T) return types;
2159
+ }
2160
+ return types ++ &[_]type{T};
2161
+ }
2162
+
2163
+ pub fn asSlice(comptime Item: type, value: anytype) []const Item {
2164
+ const T = @TypeOf(value.*);
2165
+ return switch (@typeInfo(T)) {
2166
+ .array => value[0..],
2167
+ .pointer => value.*,
2168
+ else => @compileError("not a slice"),
2169
+ };
2170
+ }
2171
+
2172
+ /// Resolve a dotted path on a value: struct fields, zero-arg methods,
2173
+ /// arena-taking methods (`fn (*const T, std.mem.Allocator) V`, skipped
2174
+ /// when `arena` is null), and bounded model conventions (a
2175
+ /// `field_count`-style pair is the author's job; the resolver only follows
2176
+ /// what exists).
2177
+ fn resolveOn(comptime T: type, value: *const T, path: []const u8, arena: ?std.mem.Allocator) ?Value {
2178
+ @setEvalBranchQuota(comptime typeScanQuota(T));
2179
+ const head = pathHead(path);
2180
+ const tail = pathTail(path);
2181
+ switch (@typeInfo(T)) {
2182
+ .@"struct" => {
2183
+ inline for (@typeInfo(T).@"struct".fields) |field| {
2184
+ if (std.mem.eql(u8, field.name, head)) {
2185
+ if (tail) |rest| {
2186
+ return resolveNested(field.type, &@field(value, field.name), rest, arena);
2187
+ }
2188
+ return valueOf(field.type, @field(value, field.name));
2189
+ }
2190
+ }
2191
+ inline for (@typeInfo(T).@"struct".decls) |decl| {
2192
+ const DeclType = @TypeOf(@field(T, decl.name));
2193
+ switch (@typeInfo(DeclType)) {
2194
+ .@"fn" => |info| {
2195
+ if (info.params.len == 1 and info.return_type != null and info.params[0].type == *const T) {
2196
+ if (std.mem.eql(u8, decl.name, head) and tail == null) {
2197
+ return valueOf(info.return_type.?, @field(T, decl.name)(value));
2198
+ }
2199
+ }
2200
+ if (comptime isArenaScalarFn(T, DeclType)) {
2201
+ if (std.mem.eql(u8, decl.name, head) and tail == null) {
2202
+ const allocator = arena orelse return null;
2203
+ return valueOf(info.return_type.?, @field(T, decl.name)(value, allocator));
2204
+ }
2205
+ }
2206
+ },
2207
+ else => {},
2208
+ }
2209
+ }
2210
+ return null;
2211
+ },
2212
+ else => return null,
2213
+ }
2214
+ }
2215
+
2216
+ /// True when `head` names a struct field whose type is a
2217
+ /// `canvas.TextBuffer(N)` editor. Such a field is the EDIT MODEL, not
2218
+ /// bindable text, so both engines and the contract checker turn the
2219
+ /// generic missing-binding failure into a teaching message pointing at
2220
+ /// the buffer's text() accessor. Comptime-callable (the compiled engine
2221
+ /// runs it inside its comptime binding resolution).
2222
+ pub fn fieldIsTextBuffer(comptime T: type, head: []const u8) bool {
2223
+ @setEvalBranchQuota(comptime typeScanQuota(T));
2224
+ inline for (@typeInfo(T).@"struct".fields) |field| {
2225
+ const is_buffer = comptime (std.mem.indexOf(u8, @typeName(field.type), "TextBuffer(") != null);
2226
+ if (is_buffer and std.mem.eql(u8, field.name, head)) return true;
2227
+ }
2228
+ return false;
2229
+ }
2230
+
2231
+ fn resolveNested(comptime T: type, ptr: anytype, path: []const u8, arena: ?std.mem.Allocator) ?Value {
2232
+ return switch (@typeInfo(T)) {
2233
+ .@"struct" => resolveOn(T, ptr, path, arena),
2234
+ else => null,
2235
+ };
2236
+ }
2237
+
2238
+ pub fn valueOf(comptime T: type, value: T) ?Value {
2239
+ return switch (@typeInfo(T)) {
2240
+ .bool => .{ .boolean = value },
2241
+ .int => .{ .integer = @intCast(value) },
2242
+ .float => .{ .float = @floatCast(value) },
2243
+ .@"enum" => .{ .string = @tagName(value) },
2244
+ .comptime_int => .{ .integer = value },
2245
+ .pointer => |info| if (info.size == .slice and info.child == u8) .{ .string = value } else null,
2246
+ .array => |info| if (info.child == u8) null else null,
2247
+ .optional => if (value) |inner| valueOf(@TypeOf(inner), inner) else .{ .boolean = false },
2248
+ else => null,
2249
+ };
2250
+ }
2251
+
2252
+ pub const literalValue = reflect.literalValue;
2253
+
2254
+ /// Display-text formatting for interpolation and `++` concatenation:
2255
+ /// defined once in the expression core so both engines (and the evaluator
2256
+ /// itself) format identically, floats included.
2257
+ pub const appendValue = markup.expr.appendValue;
2258
+
2259
+ pub fn pathHead(path: []const u8) []const u8 {
2260
+ const dot = std.mem.indexOfScalar(u8, path, '.') orelse return path;
2261
+ return path[0..dot];
2262
+ }
2263
+
2264
+ pub fn pathTail(path: []const u8) ?[]const u8 {
2265
+ const dot = std.mem.indexOfScalar(u8, path, '.') orelse return null;
2266
+ return path[dot + 1 ..];
2267
+ }
2268
+
2269
+ // -------------------------------------------------------------- elements
2270
+
2271
+ /// One registry element resolved onto the engine's `WidgetKind`, computed
2272
+ /// ONCE at comptime — resolving per lookup would re-run the enum scan per
2273
+ /// element per call site and blow the comptime quota (the registry's
2274
+ /// comptime-cost mitigation is single-pass derivation).
2275
+ pub const ElementKindEntry = struct { name: []const u8, kind: canvas.WidgetKind, takes_text: bool, takes_children: bool };
2276
+
2277
+ fn widgetKindByName(comptime name: []const u8) canvas.WidgetKind {
2278
+ comptime {
2279
+ for (@typeInfo(canvas.WidgetKind).@"enum".fields) |field| {
2280
+ if (std.mem.eql(u8, field.name, name)) return @enumFromInt(field.value);
2281
+ }
2282
+ @compileError("registry element names an unknown widget kind: " ++ name);
2283
+ }
2284
+ }
2285
+
2286
+ /// The registry's plain elements with their kinds resolved: the one
2287
+ /// name→kind table both engines dispatch through. A registry entry naming
2288
+ /// a kind that does not exist is a compile error, so the mapping cannot
2289
+ /// rot.
2290
+ pub const element_kind_table = blk: {
2291
+ @setEvalBranchQuota(400_000);
2292
+ var count: usize = 0;
2293
+ for (markup.schema.elements) |entry| {
2294
+ if (entry.widget_kind.len > 0) count += 1;
2295
+ }
2296
+ var entries: [count]ElementKindEntry = undefined;
2297
+ var index: usize = 0;
2298
+ for (markup.schema.elements) |entry| {
2299
+ if (entry.widget_kind.len == 0) continue;
2300
+ entries[index] = .{
2301
+ .name = entry.name,
2302
+ .kind = widgetKindByName(entry.widget_kind),
2303
+ .takes_text = entry.takes_text,
2304
+ .takes_children = entry.takes_children,
2305
+ };
2306
+ index += 1;
2307
+ }
2308
+ break :blk entries;
2309
+ };
2310
+
2311
+ pub fn elementKind(name: []const u8) ?canvas.WidgetKind {
2312
+ @setEvalBranchQuota(20_000);
2313
+ for (&element_kind_table) |entry| {
2314
+ if (std.mem.eql(u8, name, entry.name)) return entry.kind;
2315
+ }
2316
+ return null;
2317
+ }
2318
+
2319
+ /// Tab triggers ARE segmented controls: markup composes the strip from
2320
+ /// `<button>` children (segmented-control is a documented markup
2321
+ /// exclusion), and both engines lower them to the widget kind the
2322
+ /// engine's tab strips are built on, so the active trigger lifts to the
2323
+ /// surface with a hairline exactly like the Zig builder's tabs.
2324
+ /// Handlers ride the widget id, so bindings are untouched; toggle-button
2325
+ /// children keep their kind (their on-toggle contract is different).
2326
+ pub fn lowerTabsTriggers(children: anytype) void {
2327
+ for (children) |*child| {
2328
+ if (child.widget.kind == .button) child.widget.kind = .segmented_control;
2329
+ }
2330
+ }
2331
+
2332
+ pub fn elementTakesText(kind: canvas.WidgetKind) bool {
2333
+ // The registry's takes-text predicate projected onto widget kinds
2334
+ // (both engines and the validator's text-leaf list read the same
2335
+ // registry fact).
2336
+ @setEvalBranchQuota(20_000);
2337
+ for (&element_kind_table) |entry| {
2338
+ if (entry.takes_text and entry.kind == kind) return true;
2339
+ }
2340
+ return false;
2341
+ }
2342
+
2343
+ pub fn elementTakesChildren(kind: canvas.WidgetKind) bool {
2344
+ // The registry's takes-children predicate projected onto widget
2345
+ // kinds: text-taking elements that ALSO accept element children in
2346
+ // place of the text run (the list-row composite).
2347
+ @setEvalBranchQuota(20_000);
2348
+ for (&element_kind_table) |entry| {
2349
+ if (entry.takes_children and entry.kind == kind) return true;
2350
+ }
2351
+ return false;
2352
+ }