@native-sdk/cli 0.10.1 → 0.11.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 (555) hide show
  1. package/README.md +8 -6
  2. package/bin/native.js +2 -1
  3. package/build/app.zig +393 -97
  4. package/build/corewire.zig +53 -0
  5. package/build/test_shards.zig +49 -0
  6. package/build/ts_run.mjs +3 -3
  7. package/build.zig +2030 -83
  8. package/native-sdk.d.ts +5 -1
  9. package/package.json +20 -14
  10. package/packages/core/compile-surface/core.ts +164 -72
  11. package/packages/core/package-lock.json +75 -357
  12. package/packages/core/package.json +25 -4
  13. package/packages/core/scripts/compiler_command.mjs +17 -12
  14. package/packages/core/scripts/compiler_typecheck.mjs +1 -0
  15. package/packages/core/scripts/gen_declarations.mjs +3 -2
  16. package/packages/core/scripts/gen_service_surface.mjs +22 -10
  17. package/packages/core/scripts/install_scriptc.mjs +182 -0
  18. package/packages/core/scripts/invoke_scriptc.mjs +18 -0
  19. package/packages/core/scripts/merge_static_archives.mjs +6 -1
  20. package/packages/core/scripts/npm_static_spike.mjs +2 -2
  21. package/packages/core/scripts/run_external_core_compiler.mjs +22 -10
  22. package/packages/core/scripts/run_external_service_compiler.mjs +15 -15
  23. package/packages/core/scripts/run_library_compiler.mjs +51 -0
  24. package/packages/core/scripts/run_native_tests.mjs +22 -0
  25. package/packages/core/scripts/scriptc-toolchain.json +101 -0
  26. package/packages/core/scripts/scriptc_toolchain.mjs +121 -0
  27. package/packages/core/scripts/stage_external_core.mjs +68 -1
  28. package/packages/core/scripts/surface_manifest_diff.mjs +19 -12
  29. package/packages/core/sdk/bytes.d.ts +2 -0
  30. package/packages/core/sdk/bytes.ts +86 -0
  31. package/packages/core/sdk/core.d.ts +332 -5
  32. package/packages/core/sdk/core.ts +371 -85
  33. package/packages/core/sdk/events.d.ts +252 -0
  34. package/packages/core/sdk/events.ts +240 -2
  35. package/packages/core/sdk/internal/image_identity.d.ts +7 -0
  36. package/packages/core/sdk/internal/image_identity.ts +71 -0
  37. package/packages/core/sdk/internal/markdown_policy.d.ts +5 -0
  38. package/packages/core/sdk/internal/markdown_policy.ts +878 -0
  39. package/packages/core/sdk/text.d.ts +27 -0
  40. package/packages/core/sdk/text.ts +206 -15
  41. package/packages/core/sdk/theme.d.ts +251 -0
  42. package/packages/core/sdk/theme.ts +267 -0
  43. package/packages/core/src/authorization.ts +137 -0
  44. package/packages/core/src/automation_commands.ts +201 -0
  45. package/packages/core/src/automation_interactions.ts +92 -0
  46. package/packages/core/src/bridge_descriptors.ts +104 -0
  47. package/packages/core/src/builtin_coordination.ts +172 -0
  48. package/packages/core/src/canvas_frame_coordination.ts +81 -0
  49. package/packages/core/src/canvas_presentation_lifecycle.ts +84 -0
  50. package/packages/core/src/canvas_scene_observation.ts +145 -0
  51. package/packages/core/src/chart_content.ts +161 -0
  52. package/packages/core/src/chart_plans.ts +432 -0
  53. package/packages/core/src/checker.ts +186 -19
  54. package/packages/core/src/cli.ts +2 -11
  55. package/packages/core/src/code_content.ts +580 -0
  56. package/packages/core/src/command_coordination.ts +140 -0
  57. package/packages/core/src/component_composition.ts +94 -0
  58. package/packages/core/src/component_construction.ts +149 -0
  59. package/packages/core/src/context_menus.ts +76 -0
  60. package/packages/core/src/contract.ts +25 -7
  61. package/packages/core/src/control_appearance.ts +232 -0
  62. package/packages/core/src/control_commands.ts +115 -0
  63. package/packages/core/src/control_content.ts +105 -0
  64. package/packages/core/src/control_geometry.ts +107 -0
  65. package/packages/core/src/control_payloads.ts +119 -0
  66. package/packages/core/src/control_primitives.ts +134 -0
  67. package/packages/core/src/devhost.ts +82 -7
  68. package/packages/core/src/devhost_journal.mjs +30 -7
  69. package/packages/core/src/diagnostics.ts +7 -0
  70. package/packages/core/src/effect_plans.ts +71 -0
  71. package/packages/core/src/extent_program.ts +53 -0
  72. package/packages/core/src/flow_measurement.ts +121 -0
  73. package/packages/core/src/font_registry.ts +37 -0
  74. package/packages/core/src/frontend.ts +12 -10
  75. package/packages/core/src/glyph_atlas.ts +86 -0
  76. package/packages/core/src/gpu_packet_preparation.ts +130 -0
  77. package/packages/core/src/gpu_planning.ts +123 -0
  78. package/packages/core/src/gpu_presentation.ts +55 -0
  79. package/packages/core/src/host_flow.ts +89 -0
  80. package/packages/core/src/host_input_regions.ts +165 -0
  81. package/packages/core/src/image_registry.ts +66 -0
  82. package/packages/core/src/indicator_plans.ts +132 -0
  83. package/packages/core/src/infer.ts +150 -8
  84. package/packages/core/src/input_composition.ts +116 -0
  85. package/packages/core/src/intrinsic_measure.ts +100 -0
  86. package/packages/core/src/layout_coordination.ts +207 -0
  87. package/packages/core/src/leaf_plans.ts +134 -0
  88. package/packages/core/src/markup_canonical.ts +140 -0
  89. package/packages/core/src/markup_construction.ts +189 -0
  90. package/packages/core/src/markup_development.ts +160 -0
  91. package/packages/core/src/markup_display.ts +43 -0
  92. package/packages/core/src/markup_document.ts +226 -0
  93. package/packages/core/src/markup_elements.ts +225 -0
  94. package/packages/core/src/markup_expressions.ts +79 -0
  95. package/packages/core/src/markup_imports.ts +146 -0
  96. package/packages/core/src/markup_parser.ts +228 -0
  97. package/packages/core/src/markup_runtime.ts +93 -0
  98. package/packages/core/src/markup_scalars.ts +143 -0
  99. package/packages/core/src/markup_shapes.ts +243 -0
  100. package/packages/core/src/markup_specialized.ts +242 -0
  101. package/packages/core/src/markup_text.ts +86 -0
  102. package/packages/core/src/markup_values.ts +336 -0
  103. package/packages/core/src/measurement_coordination.ts +185 -0
  104. package/packages/core/src/modules.ts +1 -1
  105. package/packages/core/src/motion_sampling.ts +100 -0
  106. package/packages/core/src/paragraph_layout.ts +192 -0
  107. package/packages/core/src/portable_sources.json +98 -0
  108. package/packages/core/src/portable_sources.ts +17 -0
  109. package/packages/core/src/registered_font.ts +92 -0
  110. package/packages/core/src/render_animation_lifecycle.ts +128 -0
  111. package/packages/core/src/render_coordination.ts +170 -0
  112. package/packages/core/src/render_resources.ts +234 -0
  113. package/packages/core/src/runtime_policy.ts +3954 -0
  114. package/packages/core/src/scalar_text.ts +82 -0
  115. package/packages/core/src/scroll_mutation.ts +86 -0
  116. package/packages/core/src/scroll_tree.ts +86 -0
  117. package/packages/core/src/service_contract.ts +55 -3
  118. package/packages/core/src/session_codecs.ts +309 -0
  119. package/packages/core/src/shell_admission.ts +126 -0
  120. package/packages/core/src/shell_coordination.ts +93 -0
  121. package/packages/core/src/sqlite_cli.ts +14 -46
  122. package/packages/core/src/sqlite_codegen.ts +84 -0
  123. package/packages/core/src/staged_rebuild.ts +60 -0
  124. package/packages/core/src/status_validation.ts +232 -0
  125. package/packages/core/src/stream_policy.ts +63 -0
  126. package/packages/core/src/surface_recipes.ts +136 -0
  127. package/packages/core/src/system_admission.ts +60 -0
  128. package/packages/core/src/terminal_paint.ts +436 -0
  129. package/packages/core/src/text_document_width.ts +81 -0
  130. package/packages/core/src/text_measurement_cache.ts +69 -0
  131. package/packages/core/src/text_query_layout.ts +179 -0
  132. package/packages/core/src/text_run_layout.ts +410 -0
  133. package/packages/core/src/text_span_queries.ts +355 -0
  134. package/packages/core/src/typed_ast.ts +18 -1
  135. package/packages/core/src/types.ts +7 -5
  136. package/packages/core/src/vector_effects.ts +98 -0
  137. package/packages/core/src/vector_raster.ts +243 -0
  138. package/packages/core/src/view_bridge.ts +161 -0
  139. package/packages/core/src/view_check.ts +145 -0
  140. package/packages/core/src/view_components.ts +2415 -0
  141. package/packages/core/src/view_coordination.ts +198 -0
  142. package/packages/core/src/view_evaluator.ts +823 -0
  143. package/packages/core/src/view_frontend.ts +1102 -0
  144. package/packages/core/src/view_program.ts +221 -0
  145. package/packages/core/src/view_reload.ts +166 -0
  146. package/packages/core/src/view_reload_watch.ts +98 -0
  147. package/packages/core/src/view_sources.ts +34 -0
  148. package/packages/core/src/virtual_windows.ts +88 -0
  149. package/packages/core/src/web_panes.ts +67 -0
  150. package/packages/core/src/widget_audits.ts +202 -0
  151. package/packages/core/src/widget_changes.ts +258 -0
  152. package/packages/core/src/widget_continuity.ts +37 -0
  153. package/packages/core/src/widget_metrics.ts +139 -0
  154. package/packages/core/src/widget_motion.ts +171 -0
  155. package/packages/core/src/widget_paint.ts +272 -0
  156. package/packages/core/src/widget_paint_walk.ts +203 -0
  157. package/packages/core/src/widget_presentation.ts +141 -0
  158. package/packages/core/src/widget_reconcile.ts +63 -0
  159. package/packages/core/src/widget_retention.ts +62 -0
  160. package/packages/core/src/widget_routing.ts +262 -0
  161. package/packages/core/src/window_lifecycle.ts +92 -0
  162. package/packages/core/testing/index.ts +430 -0
  163. package/schemas/app.schema.json +13 -0
  164. package/skill-data/core/SKILL.md +2 -2
  165. package/skill-data/native-ui/SKILL.md +77 -18
  166. package/skill-data/ts-core/SKILL.md +117 -11
  167. package/skill-data/ts-services/SKILL.md +1 -1
  168. package/skill-data/ts-services/references/service-surface.md +288 -144
  169. package/src/app_runner/root.zig +39 -12
  170. package/src/app_runner/ts_compiled_view.zig +1702 -0
  171. package/src/app_runner/ts_core_main.zig +185 -14
  172. package/src/app_runner/ts_core_mobile.zig +25 -13
  173. package/src/automation/server.zig +57 -6
  174. package/src/automation/snapshot.zig +9 -637
  175. package/src/automation/snapshot_reference.zig +656 -0
  176. package/src/automation/snapshot_serialization.zig +566 -0
  177. package/src/embed/host.zig +2 -0
  178. package/src/embed/tests.zig +2 -0
  179. package/src/embed/ui_host.zig +2 -0
  180. package/src/platform/keyboard_input.zig +32 -0
  181. package/src/platform/linux/gtk_host.c +36 -24
  182. package/src/platform/linux/gtk_pixels.h +118 -0
  183. package/src/platform/linux/gtk_pixels_test.c +241 -0
  184. package/src/platform/linux/root.zig +4 -0
  185. package/src/platform/macos/appkit_host.h +7 -2
  186. package/src/platform/macos/appkit_host.m +219 -39
  187. package/src/platform/macos/cef_host.mm +15 -1
  188. package/src/platform/macos/global_shortcuts.zig +138 -0
  189. package/src/platform/macos/key_codes.zig +102 -0
  190. package/src/platform/macos/keyboard_input.zig +81 -0
  191. package/src/platform/macos/pointer_motion_test.m +96 -0
  192. package/src/platform/macos/root.zig +25 -3
  193. package/src/platform/macos/scroll_driver_test.m +105 -0
  194. package/src/platform/macos/spectrum_bins.h +34 -0
  195. package/src/platform/macos/spectrum_bins_test.c +56 -0
  196. package/src/platform/macos/text_keyboard_test.m +80 -0
  197. package/src/platform/macos/webview_inspection_test.m +106 -0
  198. package/src/platform/macos/widget_focus_test.m +66 -0
  199. package/src/platform/null_platform.zig +130 -2
  200. package/src/platform/null_platform_tests.zig +13 -0
  201. package/src/platform/root.zig +15 -0
  202. package/src/platform/types.zig +115 -0
  203. package/src/platform/windows/global_shortcuts.zig +157 -0
  204. package/src/platform/windows/key_codes.zig +63 -0
  205. package/src/platform/windows/keyboard_input.zig +136 -0
  206. package/src/platform/windows/root.zig +17 -0
  207. package/src/platform/windows/webview2_host.cpp +7 -2
  208. package/src/primitives/app_manifest/comptime_scene.zig +1 -1
  209. package/src/primitives/app_manifest/root.zig +2 -0
  210. package/src/primitives/app_manifest/tests.zig +4 -1
  211. package/src/primitives/app_manifest/validation.zig +2 -0
  212. package/src/primitives/canvas/a11y_audit.zig +3 -1
  213. package/src/primitives/canvas/canvas_frame_coordination_policy.zig +134 -0
  214. package/src/primitives/canvas/chart_content_policy.zig +75 -0
  215. package/src/primitives/canvas/chart_plan_policy.zig +196 -0
  216. package/src/primitives/canvas/code_content_policy.zig +162 -0
  217. package/src/primitives/canvas/commands.zig +29 -1
  218. package/src/primitives/canvas/component_composition.zig +125 -0
  219. package/src/primitives/canvas/component_composition_policy.zig +44 -0
  220. package/src/primitives/canvas/component_construction_policy.zig +187 -0
  221. package/src/primitives/canvas/container_layout_policy.zig +87 -0
  222. package/src/primitives/canvas/control_appearance_policy.zig +142 -0
  223. package/src/primitives/canvas/control_command_policy.zig +75 -0
  224. package/src/primitives/canvas/control_content_policy.zig +90 -0
  225. package/src/primitives/canvas/control_geometry_policy.zig +165 -0
  226. package/src/primitives/canvas/control_payload_policy.zig +48 -0
  227. package/src/primitives/canvas/control_primitive_policy.zig +129 -0
  228. package/src/primitives/canvas/decision_reuse.zig +133 -0
  229. package/src/primitives/canvas/effect_plan_policy.zig +96 -0
  230. package/src/primitives/canvas/events.zig +1027 -1
  231. package/src/primitives/canvas/flow_measurement_policy.zig +237 -0
  232. package/src/primitives/canvas/font_ttf.zig +27 -3
  233. package/src/primitives/canvas/fonts/Geist-Bold.ttf +0 -0
  234. package/src/primitives/canvas/fonts/Geist-Medium.ttf +0 -0
  235. package/src/primitives/canvas/fonts/OFL.txt +93 -0
  236. package/src/primitives/canvas/fonts/README.md +15 -0
  237. package/src/primitives/canvas/frame.zig +92 -23
  238. package/src/primitives/canvas/glyph_atlas_policy.zig +40 -0
  239. package/src/primitives/canvas/gpu.zig +10 -0
  240. package/src/primitives/canvas/gpu_packet_preparation_policy.zig +325 -0
  241. package/src/primitives/canvas/gpu_planning_policy.zig +364 -0
  242. package/src/primitives/canvas/gpu_presentation_policy.zig +74 -0
  243. package/src/primitives/canvas/grid_layout_policy.zig +51 -0
  244. package/src/primitives/canvas/indicator_plan_policy.zig +128 -0
  245. package/src/primitives/canvas/intrinsic_layout_policy.zig +87 -0
  246. package/src/primitives/canvas/intrinsic_measure_policy.zig +131 -0
  247. package/src/primitives/canvas/layout_audit.zig +5 -2
  248. package/src/primitives/canvas/layout_coordination_policy.zig +262 -0
  249. package/src/primitives/canvas/leaf_plan_policy.zig +117 -0
  250. package/src/primitives/canvas/markdown.zig +13 -3
  251. package/src/primitives/canvas/markdown_content_policy.zig +227 -0
  252. package/src/primitives/canvas/markdown_image_binding.zig +81 -0
  253. package/src/primitives/canvas/markdown_tests.zig +3 -3
  254. package/src/primitives/canvas/measurement_coordination_policy.zig +288 -0
  255. package/src/primitives/canvas/numeric_capabilities.zig +14 -0
  256. package/src/primitives/canvas/reference.zig +15 -11
  257. package/src/primitives/canvas/reference_tests.zig +58 -0
  258. package/src/primitives/canvas/registered_font_policy.zig +152 -0
  259. package/src/primitives/canvas/render.zig +48 -0
  260. package/src/primitives/canvas/render_cache_policy.zig +184 -0
  261. package/src/primitives/canvas/render_coordination_policy.zig +163 -0
  262. package/src/primitives/canvas/render_damage_policy.zig +160 -0
  263. package/src/primitives/canvas/render_effects.zig +14 -0
  264. package/src/primitives/canvas/render_generic_resources.zig +14 -0
  265. package/src/primitives/canvas/render_images.zig +14 -0
  266. package/src/primitives/canvas/render_layers.zig +14 -0
  267. package/src/primitives/canvas/render_override_policy.zig +119 -0
  268. package/src/primitives/canvas/render_paths.zig +14 -0
  269. package/src/primitives/canvas/render_plan_policy.zig +199 -0
  270. package/src/primitives/canvas/render_resource_policy.zig +476 -0
  271. package/src/primitives/canvas/root.zig +107 -4
  272. package/src/primitives/canvas/semantic_tree_policy.zig +158 -0
  273. package/src/primitives/canvas/serialization.zig +8 -1
  274. package/src/primitives/canvas/surface_layout_policy.zig +36 -0
  275. package/src/primitives/canvas/surface_recipe_policy.zig +231 -0
  276. package/src/primitives/canvas/terminal_grid.zig +10 -696
  277. package/src/primitives/canvas/terminal_grid_reference.zig +709 -0
  278. package/src/primitives/canvas/terminal_paint_policy.zig +191 -0
  279. package/src/primitives/canvas/text.zig +1 -0
  280. package/src/primitives/canvas/text_atlas.zig +113 -0
  281. package/src/primitives/canvas/text_cache_policy.zig +94 -0
  282. package/src/primitives/canvas/text_document_policy.zig +120 -0
  283. package/src/primitives/canvas/text_layout.zig +51 -2
  284. package/src/primitives/canvas/text_layout_cache.zig +29 -0
  285. package/src/primitives/canvas/text_layout_hash.zig +1 -0
  286. package/src/primitives/canvas/text_layout_types.zig +3 -0
  287. package/src/primitives/canvas/text_measure_cache.zig +46 -0
  288. package/src/primitives/canvas/text_measurement_policy.zig +124 -0
  289. package/src/primitives/canvas/text_metrics.zig +53 -37
  290. package/src/primitives/canvas/text_metrics_tests.zig +52 -7
  291. package/src/primitives/canvas/text_query_policy.zig +295 -0
  292. package/src/primitives/canvas/text_run_policy.zig +252 -0
  293. package/src/primitives/canvas/text_scalar_policy.zig +234 -0
  294. package/src/primitives/canvas/text_span_query_policy.zig +232 -0
  295. package/src/primitives/canvas/text_spans.zig +280 -8
  296. package/src/primitives/canvas/text_tests.zig +10 -0
  297. package/src/primitives/canvas/tokens.zig +47 -0
  298. package/src/primitives/canvas/ui.zig +251 -83
  299. package/src/primitives/canvas/ui_markup.zig +207 -46
  300. package/src/primitives/canvas/ui_markup_canonical.zig +237 -0
  301. package/src/primitives/canvas/ui_markup_compiled.zig +1724 -80
  302. package/src/primitives/canvas/ui_markup_compiled_tests.zig +225 -0
  303. package/src/primitives/canvas/ui_markup_compiled_values.zig +76 -0
  304. package/src/primitives/canvas/ui_markup_construction.zig +146 -0
  305. package/src/primitives/canvas/ui_markup_contract.zig +146 -14
  306. package/src/primitives/canvas/ui_markup_contract_tests.zig +77 -0
  307. package/src/primitives/canvas/ui_markup_display.zig +82 -0
  308. package/src/primitives/canvas/ui_markup_document.zig +196 -0
  309. package/src/primitives/canvas/ui_markup_elements.zig +163 -0
  310. package/src/primitives/canvas/ui_markup_expr.zig +192 -2
  311. package/src/primitives/canvas/ui_markup_imports.zig +262 -0
  312. package/src/primitives/canvas/ui_markup_parser.zig +117 -0
  313. package/src/primitives/canvas/ui_markup_reflect.zig +56 -0
  314. package/src/primitives/canvas/ui_markup_runtime.zig +76 -0
  315. package/src/primitives/canvas/ui_markup_scalars.zig +115 -0
  316. package/src/primitives/canvas/ui_markup_shapes.zig +224 -0
  317. package/src/primitives/canvas/ui_markup_specialized.zig +106 -0
  318. package/src/primitives/canvas/ui_markup_tests.zig +116 -0
  319. package/src/primitives/canvas/ui_markup_text.zig +128 -0
  320. package/src/primitives/canvas/ui_markup_values.zig +368 -0
  321. package/src/primitives/canvas/ui_markup_view.zig +3026 -57
  322. package/src/primitives/canvas/ui_markup_view_tests.zig +142 -10
  323. package/src/primitives/canvas/ui_schema.zig +13 -1
  324. package/src/primitives/canvas/ui_schema_tests.zig +11 -7
  325. package/src/primitives/canvas/ui_tests.zig +1 -0
  326. package/src/primitives/canvas/vector.zig +36 -2
  327. package/src/primitives/canvas/vector_effect_policy.zig +236 -0
  328. package/src/primitives/canvas/vector_raster_policy.zig +190 -0
  329. package/src/primitives/canvas/virtual_extent_policy.zig +350 -0
  330. package/src/primitives/canvas/virtual_extents.zig +113 -3
  331. package/src/primitives/canvas/virtual_flow_policy.zig +108 -0
  332. package/src/primitives/canvas/widget_audit_policy.zig +293 -0
  333. package/src/primitives/canvas/widget_change_policy.zig +298 -0
  334. package/src/primitives/canvas/widget_invalidation.zig +113 -2
  335. package/src/primitives/canvas/widget_layout.zig +673 -128
  336. package/src/primitives/canvas/widget_layout_tests.zig +4 -0
  337. package/src/primitives/canvas/widget_metric_policy.zig +129 -0
  338. package/src/primitives/canvas/widget_metrics.zig +45 -6
  339. package/src/primitives/canvas/widget_paint_policy.zig +166 -0
  340. package/src/primitives/canvas/widget_paint_walk_policy.zig +252 -0
  341. package/src/primitives/canvas/widget_presentation_policy.zig +168 -0
  342. package/src/primitives/canvas/widget_render.zig +744 -74
  343. package/src/primitives/canvas/widget_render_controls.zig +1217 -133
  344. package/src/primitives/canvas/widget_render_scroll.zig +11 -3
  345. package/src/primitives/canvas/widget_render_style.zig +94 -0
  346. package/src/primitives/canvas/widget_render_surfaces.zig +61 -50
  347. package/src/primitives/canvas/widget_routing.zig +75 -0
  348. package/src/primitives/canvas/widget_routing_policy.zig +135 -0
  349. package/src/primitives/canvas/widget_runtime.zig +21 -1
  350. package/src/primitives/canvas/widget_semantics.zig +25 -0
  351. package/src/primitives/canvas/widget_text_input.zig +86 -23
  352. package/src/primitives/canvas/widget_text_select.zig +1 -0
  353. package/src/primitives/canvas/widget_tree.zig +8 -1
  354. package/src/primitives/canvas/widgets.zig +42 -12
  355. package/src/primitives/canvas/wrapped_layout_policy.zig +104 -0
  356. package/src/root.zig +26 -0
  357. package/src/runtime/accessibility_policy.zig +122 -0
  358. package/src/runtime/api.zig +29 -0
  359. package/src/runtime/app_dispatch_policy.zig +68 -0
  360. package/src/runtime/authorization_policy.zig +150 -0
  361. package/src/runtime/automation_command_policy.zig +241 -0
  362. package/src/runtime/automation_interaction_policy.zig +107 -0
  363. package/src/runtime/automation_snapshot.zig +52 -22
  364. package/src/runtime/automation_snapshot_reference.zig +290 -0
  365. package/src/runtime/automation_widget_dispatch.zig +267 -355
  366. package/src/runtime/automation_widget_dispatch_reference.zig +900 -0
  367. package/src/runtime/blob_policy.zig +58 -0
  368. package/src/runtime/blob_reference.zig +64 -0
  369. package/src/runtime/bridge_descriptor_policy.zig +206 -0
  370. package/src/runtime/bridge_payload.zig +2 -0
  371. package/src/runtime/builtin_bridge.zig +180 -44
  372. package/src/runtime/builtin_coordination_policy.zig +121 -0
  373. package/src/runtime/callback_policy_test_reference.zig +97 -0
  374. package/src/runtime/canvas_drag_policy.zig +94 -0
  375. package/src/runtime/canvas_fonts.zig +38 -10
  376. package/src/runtime/canvas_frame.zig +590 -134
  377. package/src/runtime/canvas_frame_helpers.zig +1 -0
  378. package/src/runtime/canvas_hover_policy.zig +131 -0
  379. package/src/runtime/canvas_images.zig +59 -30
  380. package/src/runtime/canvas_keyboard_focus_policy.zig +154 -0
  381. package/src/runtime/canvas_limits.zig +1 -1
  382. package/src/runtime/canvas_presentation_lifecycle_policy.zig +86 -0
  383. package/src/runtime/canvas_scene_observation_policy.zig +181 -0
  384. package/src/runtime/canvas_widget_context_menu.zig +183 -4
  385. package/src/runtime/canvas_widget_context_menu_test_cases.zig +1147 -0
  386. package/src/runtime/canvas_widget_context_menu_tests.zig +23 -1098
  387. package/src/runtime/canvas_widget_display.zig +110 -9
  388. package/src/runtime/canvas_widget_events.zig +718 -328
  389. package/src/runtime/canvas_widget_runtime.zig +720 -150
  390. package/src/runtime/canvas_widget_scroll_drivers.zig +39 -1
  391. package/src/runtime/canvas_widget_scroll_tests.zig +31 -0
  392. package/src/runtime/canvas_widget_state.zig +93 -56
  393. package/src/runtime/channel_sources.zig +80 -0
  394. package/src/runtime/command_policy.zig +161 -0
  395. package/src/runtime/component_policy.zig +83 -0
  396. package/src/runtime/context_menu_policy.zig +112 -0
  397. package/src/runtime/core.zig +170 -6
  398. package/src/runtime/desktop_files.zig +189 -0
  399. package/src/runtime/effects.zig +638 -33
  400. package/src/runtime/effects_host_tests.zig +25 -0
  401. package/src/runtime/effects_tests.zig +28 -0
  402. package/src/runtime/file_policy_test_reference.zig +113 -0
  403. package/src/runtime/flow.zig +419 -341
  404. package/src/runtime/flow_reference.zig +1427 -0
  405. package/src/runtime/font_registry_policy.zig +72 -0
  406. package/src/runtime/frame_profile.zig +24 -33
  407. package/src/runtime/global_shortcuts.zig +75 -0
  408. package/src/runtime/gpu_surface_events.zig +97 -35
  409. package/src/runtime/host_event_pump.zig +303 -0
  410. package/src/runtime/host_event_pump_reference.zig +268 -0
  411. package/src/runtime/host_flow_policy.zig +155 -0
  412. package/src/runtime/host_input_regions_policy.zig +156 -0
  413. package/src/runtime/image_registry_policy.zig +94 -0
  414. package/src/runtime/input_composition_policy.zig +138 -0
  415. package/src/runtime/journal_policy.zig +66 -0
  416. package/src/runtime/journal_reference.zig +53 -0
  417. package/src/runtime/keyboard_input.zig +86 -0
  418. package/src/runtime/lifecycle_policy_test_reference.zig +97 -0
  419. package/src/runtime/markup_development_policy.zig +91 -0
  420. package/src/runtime/media_surface.zig +40 -1
  421. package/src/runtime/motion_sampling_policy.zig +159 -0
  422. package/src/runtime/recording_policy.zig +42 -0
  423. package/src/runtime/render_animation_policy.zig +206 -0
  424. package/src/runtime/replay_policy.zig +66 -0
  425. package/src/runtime/replay_policy_reference.zig +326 -0
  426. package/src/runtime/replay_progress_policy.zig +64 -0
  427. package/src/runtime/replay_progress_reference.zig +34 -0
  428. package/src/runtime/request_policy_test_reference.zig +188 -0
  429. package/src/runtime/root.zig +16 -0
  430. package/src/runtime/scroll_mutation_policy.zig +130 -0
  431. package/src/runtime/scroll_mutation_runtime.zig +158 -0
  432. package/src/runtime/scroll_tree_policy.zig +99 -0
  433. package/src/runtime/session_blobs.zig +157 -58
  434. package/src/runtime/session_blobs_reference.zig +692 -0
  435. package/src/runtime/session_codec.zig +361 -0
  436. package/src/runtime/session_codec_reference.zig +1027 -0
  437. package/src/runtime/session_journal.zig +246 -983
  438. package/src/runtime/session_record.zig +231 -45
  439. package/src/runtime/session_replay.zig +157 -415
  440. package/src/runtime/session_tests.zig +552 -0
  441. package/src/runtime/shell_admission_policy.zig +225 -0
  442. package/src/runtime/shell_coordination_policy.zig +138 -0
  443. package/src/runtime/shell_layout.zig +126 -1
  444. package/src/runtime/snapshot_policy.zig +60 -0
  445. package/src/runtime/snapshot_reference.zig +64 -0
  446. package/src/runtime/staged_rebuild_policy.zig +83 -0
  447. package/src/runtime/state.zig +16 -0
  448. package/src/runtime/status_validation.zig +190 -0
  449. package/src/runtime/system_admission_policy.zig +140 -0
  450. package/src/runtime/system_services.zig +65 -36
  451. package/src/runtime/tests.zig +2 -0
  452. package/src/runtime/ts_canvas_hooks.zig +261 -0
  453. package/src/runtime/ts_canvas_hooks_tests.zig +279 -0
  454. package/src/runtime/ts_canvas_icons.zig +234 -0
  455. package/src/runtime/ts_core_host.zig +2451 -411
  456. package/src/runtime/ts_core_host_tests.zig +1982 -0
  457. package/src/runtime/ts_test_host.zig +699 -0
  458. package/src/runtime/ts_ui_app.zig +1111 -45
  459. package/src/runtime/ui_app.zig +1381 -650
  460. package/src/runtime/ui_app_provenance.zig +147 -5
  461. package/src/runtime/ui_app_provenance_reference.zig +370 -0
  462. package/src/runtime/ui_app_tests.zig +667 -188
  463. package/src/runtime/ui_app_window_tests.zig +222 -0
  464. package/src/runtime/view.zig +25 -0
  465. package/src/runtime/view_canvas.zig +30 -6
  466. package/src/runtime/view_coordination_policy.zig +147 -0
  467. package/src/runtime/view_reload.zig +226 -0
  468. package/src/runtime/view_widget_control.zig +51 -14
  469. package/src/runtime/view_widget_scroll.zig +46 -26
  470. package/src/runtime/view_widget_text.zig +153 -68
  471. package/src/runtime/view_widget_tree.zig +503 -69
  472. package/src/runtime/virtual_window_policy.zig +128 -0
  473. package/src/runtime/web_pane_policy.zig +111 -0
  474. package/src/runtime/web_pane_test_cases.zig +204 -0
  475. package/src/runtime/webview_view_bridge_tests.zig +2 -0
  476. package/src/runtime/widget_continuity_policy.zig +73 -0
  477. package/src/runtime/widget_motion_policy.zig +238 -0
  478. package/src/runtime/widget_reconcile_policy.zig +81 -0
  479. package/src/runtime/widget_retention_policy.zig +75 -0
  480. package/src/runtime/window_command_bridge_tests.zig +63 -0
  481. package/src/runtime/window_lifecycle_policy.zig +127 -0
  482. package/src/runtime/window_storage.zig +74 -8
  483. package/src/runtime/window_view_runtime.zig +148 -33
  484. package/src/runtime/window_views.zig +179 -11
  485. package/src/security/root.zig +1 -0
  486. package/src/tooling/components/identity_tests.zig +62 -36
  487. package/src/tooling/components/stepper.native +24 -0
  488. package/src/tooling/components/stepper.zig +0 -19
  489. package/src/tooling/components/timeline-item.native +22 -0
  490. package/src/tooling/components/timeline.native +5 -3
  491. package/src/tooling/components/timeline_item.zig +0 -32
  492. package/src/tooling/eject_components.zig +76 -8
  493. package/src/tooling/ios.zig +66 -12
  494. package/src/tooling/manifest.zig +35 -0
  495. package/src/tooling/raw_manifest.zig +6 -0
  496. package/src/tooling/templates.zig +23 -3
  497. package/src/tooling/ts_core.zig +97 -4
  498. package/src/tooling/verbs.zig +87 -1
  499. package/tools/corewire/build_profile.mjs +54 -0
  500. package/tools/corewire/contract_intake.ts +303 -0
  501. package/tools/corewire/contract_intake.zig +82 -0
  502. package/tools/corewire/core.test.ts +188 -0
  503. package/tools/corewire/core_abi.zig +55 -0
  504. package/tools/corewire/core_cases.ts +233 -0
  505. package/tools/corewire/core_contract.ts +407 -0
  506. package/tools/corewire/core_emission.ts +136 -0
  507. package/tools/corewire/core_emission.zig +69 -0
  508. package/tools/corewire/core_goldens.json +3110 -0
  509. package/tools/corewire/core_policy.ts +14 -0
  510. package/tools/corewire/core_policy.zig +48 -0
  511. package/tools/corewire/core_projection.ts +361 -0
  512. package/tools/corewire/core_vocabulary.ts +6 -0
  513. package/tools/corewire/emit.zig +19 -1590
  514. package/tools/corewire/emit_facade.ts +714 -0
  515. package/tools/corewire/emit_facade.zig +10 -3752
  516. package/tools/corewire/emit_mirror.ts +300 -0
  517. package/tools/corewire/emit_profile.ts +50 -0
  518. package/tools/corewire/emit_service.ts +331 -0
  519. package/tools/corewire/emit_service.zig +23 -842
  520. package/tools/corewire/entry.test.ts +95 -0
  521. package/tools/corewire/entry_coordination.ts +127 -0
  522. package/tools/corewire/entry_coordination.zig +190 -0
  523. package/tools/corewire/facade_templates.ts +85 -0
  524. package/tools/corewire/filesystem.test.ts +166 -0
  525. package/tools/corewire/filesystem_coordination.ts +213 -0
  526. package/tools/corewire/filesystem_coordination.zig +245 -0
  527. package/tools/corewire/intake.test.ts +39 -0
  528. package/tools/corewire/intake_cases.ts +55 -0
  529. package/tools/corewire/intake_goldens.json +12237 -0
  530. package/tools/corewire/invocation.test.ts +28 -0
  531. package/tools/corewire/invocation.ts +175 -0
  532. package/tools/corewire/invocation.zig +136 -0
  533. package/tools/corewire/invocation_cases.ts +60 -0
  534. package/tools/corewire/invocation_fixture.ts +33 -0
  535. package/tools/corewire/invocation_goldens.json +1143 -0
  536. package/tools/corewire/invocation_tests.zig +111 -0
  537. package/tools/corewire/invocation_usage.ts +2 -0
  538. package/tools/corewire/lossless_projection.ts +96 -0
  539. package/tools/corewire/main.zig +7 -680
  540. package/tools/corewire/main_reference.zig +415 -0
  541. package/tools/corewire/mirror_templates.ts +59 -0
  542. package/tools/corewire/profile.test.ts +248 -0
  543. package/tools/corewire/profile.zig +56 -0
  544. package/tools/corewire/profile_emission.ts +204 -0
  545. package/tools/corewire/profile_library.json +29 -0
  546. package/tools/corewire/profile_tests.zig +136 -0
  547. package/tools/corewire/service.test.ts +75 -0
  548. package/tools/corewire/service_cases.ts +61 -0
  549. package/tools/corewire/service_contract.zig +40 -146
  550. package/tools/corewire/service_goldens.json +650 -0
  551. package/tools/corewire/service_projection.zig +52 -0
  552. package/tools/corewire/service_templates.ts +14 -0
  553. package/tools/corewire/shim_rt.zig +9 -0
  554. package/tools/corewire/sidecar.zig +269 -1438
  555. package/tools/corewire/emit_profile.zig +0 -521
@@ -0,0 +1,1702 @@
1
+ //! Native consumer of the optional compiled TypeScript view. This module
2
+ //! never evaluates bindings or reads the model. Tree data is copied before
3
+ //! scriptc's result arena resets; strings and nodes then live for this native
4
+ //! tree generation. Canonical event envelopes become ordinary journaled Msgs.
5
+ const std = @import("std");
6
+ const sdk = @import("native_sdk");
7
+ const core = @import("core.zig");
8
+ pub const enabled = @hasDecl(core, "nativeView");
9
+ const Ui = sdk.canvas.Ui(core.Msg);
10
+
11
+ /// Raw text uses byte arrays only; JSON strings would silently admit a
12
+ /// second encoding and could replace malformed model bytes.
13
+ const ByteText = struct {
14
+ bytes: []const u8,
15
+
16
+ pub fn jsonParse(allocator: std.mem.Allocator, source: anytype, options: std.json.ParseOptions) !ByteText {
17
+ const value = try std.json.innerParse(std.json.Value, allocator, source, options);
18
+ const array = switch (value) {
19
+ .array => |items| items.items,
20
+ else => return error.UnexpectedToken,
21
+ };
22
+ const bytes = try allocator.alloc(u8, array.len);
23
+ for (array, bytes) |item, *byte| {
24
+ const number = switch (item) {
25
+ .integer => |number| number,
26
+ else => return error.UnexpectedToken,
27
+ };
28
+ if (number < 0 or number > 255) return error.Overflow;
29
+ byte.* = @intCast(number);
30
+ }
31
+ return .{ .bytes = bytes };
32
+ }
33
+ };
34
+
35
+ fn byteText(raw: ?ByteText, fallback: []const u8) []const u8 {
36
+ return if (raw) |text| text.bytes else fallback;
37
+ }
38
+
39
+ const SpanRecord = struct {
40
+ text: []const u8,
41
+ textBytes: ?ByteText = null,
42
+ weight: sdk.canvas.TextSpanWeight = .regular,
43
+ color: ?sdk.canvas.TextSpanColor = null,
44
+ scale: ?f32 = null,
45
+ monospace: bool = false,
46
+ italic: bool = false,
47
+ underline: bool = false,
48
+ };
49
+
50
+ const ChartSeriesRecord = struct {
51
+ kind: enum { line, bar },
52
+ values: []const u32,
53
+ color: sdk.canvas.ChartSeriesColor,
54
+ fill: bool,
55
+ label: ByteText,
56
+ };
57
+
58
+ const Record = struct {
59
+ chartSeries: ?[]const ChartSeriesRecord = null,
60
+ chartXLabels: ?[]const ByteText = null,
61
+ chartYMin: ?u32 = null,
62
+ chartYMax: ?u32 = null,
63
+ chartGridLines: ?u8 = null,
64
+ chartBaseline: ?bool = null,
65
+ chartYLabels: ?bool = null,
66
+ chartHoverDetails: ?bool = null,
67
+ chartStrokeWidth: ?u32 = null,
68
+ videoSrc: ?[]const u8 = null,
69
+ videoControls: bool = false,
70
+ videoAutoplay: bool = true,
71
+ videoLoop: bool = false,
72
+ videoMuted: bool = false,
73
+ videoControl: sdk.canvas.VideoControlVerb = .none,
74
+ zeroIntrinsic: ?bool = null,
75
+ clipContent: ?bool = null,
76
+ overflow: sdk.canvas.TextOverflow = .ellipsis,
77
+ end: usize,
78
+ kind: enum { image, combobox, bubble, table, data_grid, popover, menu_surface, data_row, data_cell, progress, skeleton, spinner, column, row, stack, grid, card, alert, dialog, drawer, sheet, separator, panel, badge, input, text_field, search_field, textarea, text, icon, button, checkbox, switch_control, toggle, slider, status_bar, spacer, scroll, avatar, radio, radio_group, button_group, breadcrumb, pagination, toggle_button, toggle_group, accordion, tabs, segmented_control, tree, list, list_item, select, dropdown_menu, menu_item, tooltip, split, resizable, media_surface, terminal, input_group, input_group_actions, code, chart, markdown },
79
+ text: []const u8,
80
+ textBytes: ?ByteText = null,
81
+ placeholder: []const u8 = "",
82
+ placeholderBytes: ?ByteText = null,
83
+ command: []const u8 = "",
84
+ wrap: ?bool = null,
85
+ submitOnEnter: bool = false,
86
+ key: ?[]const u8 = null,
87
+ keyBytes: ?ByteText = null,
88
+ keyInt: ?i64 = null,
89
+ keySlot: usize = 0,
90
+ globalKey: ?[]const u8 = null,
91
+ globalKeyBytes: ?ByteText = null,
92
+ globalKeyInt: ?i64 = null,
93
+ columns: usize = 0,
94
+ virtualized: bool = false,
95
+ virtualItemExtent: f32 = 0,
96
+ virtualWindow: ?usize = null,
97
+ gap: f32 = 0,
98
+ padding: ?f32 = null,
99
+ paddingTop: ?f32 = null,
100
+ paddingBottom: ?f32 = null,
101
+ paddingLeft: ?f32 = null,
102
+ paddingRight: ?f32 = null,
103
+ grow: f32 = 0,
104
+ width: f32 = 0,
105
+ height: f32 = 0,
106
+ minWidth: f32 = 0,
107
+ maxWidth: f32 = 0,
108
+ resizeDuration: u32 = 0,
109
+ resizeEasing: sdk.canvas.Easing = .standard,
110
+ resizeOrigin: f32 = -1,
111
+ value: f32 = 0,
112
+ valueX: ?f32 = null,
113
+ axis: ?sdk.canvas.ScrollAxes = null,
114
+ overscroll: ?sdk.canvas.WidgetOverscroll = null,
115
+ image: u64 = 0,
116
+ imageLower: ?u32 = null,
117
+ imageUpper: ?u32 = null,
118
+ imageFit: ?sdk.canvas.ImageFit = null,
119
+ pty: ?u64 = null,
120
+ ptyBytes: ?ByteText = null,
121
+ scrollback: ?u32 = null,
122
+ terminal: ?u8 = null,
123
+ icon: []const u8 = "",
124
+ iconBytes: ?ByteText = null,
125
+ iconPlacement: sdk.canvas.WidgetIconPlacement = .leading,
126
+ label: []const u8 = "",
127
+ labelBytes: ?ByteText = null,
128
+ role: @FieldType(sdk.canvas.WidgetSemantics, "role") = .none,
129
+ background: @FieldType(sdk.canvas.StyleTokenRefs, "background") = null,
130
+ foreground: @FieldType(sdk.canvas.StyleTokenRefs, "foreground") = null,
131
+ borderColor: @FieldType(sdk.canvas.StyleTokenRefs, "border_color") = null,
132
+ focusRing: @FieldType(sdk.canvas.StyleTokenRefs, "focus_ring") = null,
133
+ radius: @FieldType(sdk.canvas.StyleTokenRefs, "radius") = null,
134
+ accent: @FieldType(sdk.canvas.StyleTokenRefs, "accent") = null,
135
+ accentForeground: @FieldType(sdk.canvas.StyleTokenRefs, "accent_foreground") = null,
136
+ backdropBlur: @FieldType(Ui.ElementOptions, "backdrop_blur_token") = null,
137
+ quietHover: bool = false,
138
+ windowDrag: bool = false,
139
+ main: @FieldType(Ui.ElementOptions, "main") = .start,
140
+ cross: @FieldType(Ui.ElementOptions, "cross") = .stretch,
141
+ size: @FieldType(Ui.ElementOptions, "size") = .default,
142
+ variant: @FieldType(Ui.ElementOptions, "variant") = .default,
143
+ checked: bool = false,
144
+ disabled: bool = false,
145
+ selected: bool = false,
146
+ focusable: bool = false,
147
+ autofocus: bool = false,
148
+ expanded: ?bool = null,
149
+ treeLevel: u16 = 0,
150
+ listItemIndex: ?u32 = null,
151
+ listItemCount: ?u32 = null,
152
+ spans: ?[]const SpanRecord = null,
153
+ textAlignment: sdk.canvas.TextAlign = .start,
154
+ spanWeight: ?sdk.canvas.TextSpanWeight = null,
155
+ spanColor: ?sdk.canvas.TextSpanColor = null,
156
+ spanScale: ?f32 = null,
157
+ markdownRecipe: ?ByteText = null,
158
+ markdownLink: ?u8 = null,
159
+ markdownDetails: ?u8 = null,
160
+ codeLanguage: ?sdk.canvas.code.Language = null,
161
+ codeLineDigits: ?u8 = null,
162
+ codeEditable: ?bool = null,
163
+ codeNumbered: ?bool = null,
164
+ codeAddedLines: ?[]const u8 = null,
165
+ codeRemovedLines: ?[]const u8 = null,
166
+ contextMenu: []const ContextMenuRecord = &.{},
167
+ press: ?[]const u8 = null,
168
+ doublePress: ?[]const u8 = null,
169
+ hold: ?[]const u8 = null,
170
+ hoverEnter: ?[]const u8 = null,
171
+ hoverLeave: ?[]const u8 = null,
172
+ reachEnd: ?[]const u8 = null,
173
+ reachStart: ?[]const u8 = null,
174
+ toggle: ?[]const u8 = null,
175
+ change: ?[]const u8 = null,
176
+ drag: ?[]const u8 = null,
177
+ scroll: ?u8 = null,
178
+ input: ?u8 = null,
179
+ valueChange: ?u8 = null,
180
+ resize: ?u8 = null,
181
+ submit: ?[]const u8 = null,
182
+ dismiss: ?[]const u8 = null,
183
+ anchor: ?sdk.canvas.WidgetAnchorPlacement = null,
184
+ anchorAlignment: sdk.canvas.WidgetAnchorAlignment = .start,
185
+ anchorOffset: f32 = 4,
186
+ tooltipDelay: ?i32 = null,
187
+ };
188
+ const ContextMenuRecord = struct {
189
+ label: []const u8,
190
+ labelBytes: ?ByteText = null,
191
+ press: ?[]const u8 = null,
192
+ enabled: bool,
193
+ separator: bool,
194
+ };
195
+ /// A view-reload build (Debug) decodes the development records: each node's
196
+ /// authored source and the markup files the view names, for `provenance`
197
+ /// and write-back. Every other build decodes exactly the release records.
198
+ const view_sources = @import("builtin").mode == .Debug and @hasDecl(core, "nativeDevViewPrepare");
199
+ const SourceSite = struct { file: []const u8, start: usize, end: usize, line: usize, column: usize };
200
+ const SourceEntry = struct { file: []const u8, start: usize, end: usize, line: usize, column: usize, chain: []const SourceSite };
201
+ const SourceFile = struct { path: []const u8, hash: []const u8 };
202
+ const Tree = if (view_sources) struct {
203
+ format: u32,
204
+ nodes: []const Record,
205
+ sources: []const i64 = &.{},
206
+ sourceTable: []const SourceEntry = &.{},
207
+ sourceFiles: []const SourceFile = &.{},
208
+ } else struct { format: u32, nodes: []const Record };
209
+ /// Per-record sources of the tree being decoded (view-reload builds only).
210
+ var decoding_sources: []const ?*const sdk.canvas.ui_provenance.NodeSource = &.{};
211
+
212
+ const VirtualRequest = struct {
213
+ id: []const u8,
214
+ itemCount: usize,
215
+ itemExtent: f32,
216
+ gap: f32,
217
+ overscan: usize,
218
+ viewportFallback: f32,
219
+ indexBase: u64,
220
+ trailing: bool,
221
+ estimateHelper: i64,
222
+ };
223
+ const VirtualRequests = struct { format: u32, requests: []const VirtualRequest };
224
+ const ResolvedVirtual = struct { options: Ui.VirtualListOptions, range: sdk.canvas.VirtualListRange };
225
+
226
+ fn virtualView(ui: *Ui, label: []const u8, video: []const u8) !Ui.Node {
227
+ const bytes = core.nativeVirtualRequests(label, video, ui.arena);
228
+ const requests = try std.json.parseFromSliceLeaky(VirtualRequests, ui.arena, bytes, .{ .allocate = .alloc_always });
229
+ if (requests.format != 1 or requests.requests.len > sdk.canvas.max_virtual_windows) return error.InvalidView;
230
+ const resolved = try ui.arena.alloc(ResolvedVirtual, requests.requests.len);
231
+ const context = try ui.arena.alloc(u8, 8 + resolved.len * 96);
232
+ std.mem.writeInt(u32, context[0..4], 1, .little);
233
+ std.mem.writeInt(u32, context[4..8], @intCast(resolved.len), .little);
234
+ for (requests.requests, resolved, 0..) |request, *target, index| {
235
+ if (request.id.len == 0 or request.id.len > 256 or request.itemCount > 9007199254740991 or request.overscan > 9007199254740991 or request.indexBase > 9007199254740991 - request.itemCount) return error.InvalidView;
236
+ for (requests.requests[0..index]) |prior| if (std.mem.eql(u8, prior.id, request.id)) return error.InvalidView;
237
+ for ([_]f32{ request.itemExtent, request.gap, request.viewportFallback }) |extent| if (!std.math.isFinite(extent) or extent < 0) return error.InvalidView;
238
+ if (request.estimateHelper < -1 or request.estimateHelper > std.math.maxInt(u32)) return error.InvalidView;
239
+ if (request.estimateHelper == -1 and request.itemExtent == 0) return error.InvalidView;
240
+ target.options = .{
241
+ .id = request.id,
242
+ .item_count = request.itemCount,
243
+ .item_extent = request.itemExtent,
244
+ .gap = request.gap,
245
+ .overscan = request.overscan,
246
+ .viewport_fallback = request.viewportFallback,
247
+ .index_base = request.indexBase,
248
+ .anchor = if (request.trailing) .trailing else .leading,
249
+ .extent_estimate = if (request.estimateHelper >= 0) core.virtualExtentEstimate else null,
250
+ .extent_context = if (request.estimateHelper >= 0) @ptrFromInt(@as(usize, @intCast(request.estimateHelper)) + 1) else null,
251
+ };
252
+ target.range = ui.virtualWindow(target.options);
253
+ const at = 8 + index * 96;
254
+ inline for (@typeInfo(sdk.canvas.VirtualListRange).@"struct".fields, 0..) |field, offset| {
255
+ const value = @field(target.range, field.name);
256
+ const number: f64 = if (@typeInfo(field.type) == .int) @floatFromInt(value) else value;
257
+ std.mem.writeInt(u64, context[at + offset * 8 ..][0..8], @bitCast(number), .little);
258
+ }
259
+ }
260
+ return decodeVirtual(ui, core.nativeVirtualView(label, context, video, ui.arena), resolved);
261
+ }
262
+
263
+ /// Concrete callbacks for generated compiled-model apps.
264
+ pub fn buildRuntime(ui: *Ui, model: *const core.RuntimeModel) Ui.Node {
265
+ _ = model;
266
+ return buildCommitted(ui);
267
+ }
268
+
269
+ pub fn buildRuntimeWindow(ui: *Ui, model: *const core.RuntimeModel, label: []const u8) Ui.Node {
270
+ _ = model;
271
+ return buildCommittedWindow(ui, label);
272
+ }
273
+
274
+ /// Callbacks for any host model representation. The compiled view never
275
+ /// reads the model it is handed, so harnesses that keep another host model
276
+ /// install the same committed tree source as generated apps.
277
+ pub fn ViewFor(comptime HostModel: type) type {
278
+ return struct {
279
+ pub fn build(ui: *Ui, model: *const HostModel) Ui.Node {
280
+ _ = model;
281
+ return buildCommitted(ui);
282
+ }
283
+
284
+ pub fn buildWindow(ui: *Ui, model: *const HostModel, label: []const u8) Ui.Node {
285
+ _ = model;
286
+ return buildCommittedWindow(ui, label);
287
+ }
288
+ };
289
+ }
290
+
291
+ fn buildCommitted(ui: *Ui) Ui.Node {
292
+ var context: [20]u8 = undefined;
293
+ if (comptime @hasDecl(core, "nativeVirtualRequests")) return virtualView(ui, "", videoContext(ui, &context)) catch @panic("invalid compiled virtual view data");
294
+ const bytes = if (comptime @hasDecl(core, "nativeMediaView")) core.nativeMediaView(videoContext(ui, &context), ui.arena) else core.nativeView(ui.arena);
295
+ return decode(ui, bytes) catch @panic("invalid compiled TypeScript view data");
296
+ }
297
+
298
+ fn buildCommittedWindow(ui: *Ui, label: []const u8) Ui.Node {
299
+ var context: [20]u8 = undefined;
300
+ if (comptime @hasDecl(core, "nativeVirtualRequests")) return virtualView(ui, label, videoContext(ui, &context)) catch @panic("invalid compiled virtual window view data");
301
+ const bytes = if (comptime @hasDecl(core, "nativeMediaWindowView")) core.nativeMediaWindowView(label, videoContext(ui, &context), ui.arena) else core.nativeWindowView(label, ui.arena);
302
+ return decode(ui, bytes) catch @panic("invalid compiled TypeScript window view data");
303
+ }
304
+
305
+ fn videoContext(ui: *Ui, buffer: *[20]u8) []const u8 {
306
+ const state = ui.video_state;
307
+ buffer.* = @splat(0);
308
+ buffer[0] = 1;
309
+ buffer[1] = @as(u8, @intFromBool(state.active)) | (@as(u8, @intFromBool(state.playing)) << 1) | (@as(u8, @intFromBool(state.buffering)) << 2) | (@as(u8, @intFromBool(state.completed)) << 3);
310
+ std.mem.writeInt(u64, buffer[4..12], @bitCast(@as(f64, @floatFromInt(state.position_ms))), .little);
311
+ std.mem.writeInt(u64, buffer[12..20], @bitCast(@as(f64, @floatFromInt(state.duration_ms))), .little);
312
+ return buffer;
313
+ }
314
+
315
+ fn decode(ui: *Ui, bytes: []const u8) !Ui.Node {
316
+ return decodeVirtual(ui, bytes, &.{});
317
+ }
318
+
319
+ fn decodeVirtual(ui: *Ui, bytes: []const u8, virtuals: []const ResolvedVirtual) !Ui.Node {
320
+ if (comptime @hasDecl(core, "nativeWindowPolicy")) {
321
+ sdk.canvas.decision_reuse.registerPurePolicy(core.nativeWindowPolicy);
322
+ ui.construction_policy = core.nativeWindowPolicy;
323
+ ui.composition_policy = core.nativeWindowPolicy;
324
+ ui.code_content_policy = core.nativeWindowPolicy;
325
+ ui.chart_content_policy = core.nativeWindowPolicy;
326
+ ui.markdown_content_policy = core.nativeMarkdownPolicy;
327
+ }
328
+ // Numeric JSON byte arrays need up to four transport bytes per source
329
+ // byte. Preserve the native retained-text budget at the decoder boundary.
330
+ if (bytes.len > 4 * 1024 * 1024) return error.ViewTooLarge;
331
+ const tree = std.json.parseFromSliceLeaky(Tree, ui.arena, bytes, .{ .allocate = .alloc_always }) catch |err| switch (err) {
332
+ error.OutOfMemory => return err,
333
+ else => return error.InvalidView,
334
+ };
335
+ if (tree.format != 2 or tree.nodes.len == 0 or tree.nodes.len > 1024) return error.InvalidView;
336
+ if (tree.nodes[0].end != tree.nodes.len) return error.InvalidView;
337
+ var seen: [sdk.canvas.max_virtual_windows]bool = @splat(false);
338
+ for (tree.nodes) |record| if (record.virtualWindow) |window| {
339
+ if (window >= virtuals.len or seen[window]) return error.InvalidView;
340
+ seen[window] = true;
341
+ };
342
+ for (seen[0..virtuals.len]) |present| if (!present) return error.InvalidView;
343
+ if (comptime view_sources) {
344
+ if (ui.provenance_sink != null) decoding_sources = try stampSources(ui, tree);
345
+ defer decoding_sources = &.{};
346
+ return node(ui, tree.nodes, 0, tree.nodes.len, 0, virtuals, null);
347
+ }
348
+ return node(ui, tree.nodes, 0, tree.nodes.len, 0, virtuals, null);
349
+ }
350
+
351
+ /// Resolve the development records' attribution into one source per record
352
+ /// (in the build arena) and publish the files the view names. A view
353
+ /// without attribution gets no sources, as builder-authored views do.
354
+ fn stampSources(ui: *Ui, tree: Tree) ![]const ?*const sdk.canvas.ui_provenance.NodeSource {
355
+ const provenance = sdk.canvas.ui_provenance;
356
+ const reload = sdk.view_reload;
357
+ if (tree.sources.len != tree.nodes.len) return &.{};
358
+ const entries = try ui.arena.alloc(provenance.NodeSource, tree.sourceTable.len);
359
+ for (tree.sourceTable, entries) |entry, *out| {
360
+ const chain = try ui.arena.alloc(provenance.UseSite, entry.chain.len);
361
+ for (entry.chain, chain) |site, *use| use.* = .{ .src_path = site.file, .span = .{ .start = site.start, .end = site.end }, .line = site.line, .column = site.column };
362
+ out.* = .{ .src_path = entry.file, .span = .{ .start = entry.start, .end = entry.end }, .line = entry.line, .column = entry.column, .chain = chain };
363
+ }
364
+ const stamped = try ui.arena.alloc(?*const provenance.NodeSource, tree.nodes.len);
365
+ for (tree.sources, stamped) |index, *out| {
366
+ if (index < 0 or index >= entries.len) return error.InvalidView;
367
+ out.* = &entries[@intCast(index)];
368
+ }
369
+ reload.view_files_len = 0;
370
+ for (tree.sourceFiles) |file| {
371
+ if (reload.view_files_len == reload.max_view_files) break;
372
+ const hash = std.fmt.parseUnsigned(u64, file.hash, 16) catch return error.InvalidView;
373
+ // Paths are stamped relative to the app's src/; the root view file is stamped empty.
374
+ const disk = try std.fmt.allocPrint(ui.arena, "src/{s}", .{if (file.path.len == 0) "app.native" else file.path});
375
+ reload.view_files[reload.view_files_len] = .{ .stamped = file.path, .disk = disk, .hash = hash };
376
+ reload.view_files_len += 1;
377
+ }
378
+ return stamped;
379
+ }
380
+
381
+ /// One record and its subtree. View-reload builds stamp each node with its
382
+ /// authored source; every other build decodes the records alone.
383
+ const node = if (view_sources) nodeWithSource else nodeRecord;
384
+
385
+ fn nodeWithSource(ui: *Ui, records: []const Record, index: usize, parent_end: usize, depth: usize, virtuals: []const ResolvedVirtual, parent_kind: ?@FieldType(Record, "kind")) anyerror!Ui.Node {
386
+ var built = try nodeRecord(ui, records, index, parent_end, depth, virtuals, parent_kind);
387
+ if (index < decoding_sources.len) built.source = decoding_sources[index];
388
+ return built;
389
+ }
390
+
391
+ fn nodeRecord(ui: *Ui, records: []const Record, index: usize, parent_end: usize, depth: usize, virtuals: []const ResolvedVirtual, parent_kind: ?@FieldType(Record, "kind")) !Ui.Node {
392
+ if (depth > 64) return error.ViewTooDeep;
393
+ var value = records[index];
394
+ if (value.keyBytes != null and value.key != null or value.globalKeyBytes != null and value.globalKey != null) return error.InvalidView;
395
+ value.text = byteText(value.textBytes, value.text);
396
+ if (value.keyBytes) |bytes| value.key = bytes.bytes;
397
+ if (value.globalKeyBytes) |bytes| value.globalKey = bytes.bytes;
398
+ value.label = byteText(value.labelBytes, value.label);
399
+ value.placeholder = byteText(value.placeholderBytes, value.placeholder);
400
+ value.icon = byteText(value.iconBytes, value.icon);
401
+ if (value.end <= index or value.end > parent_end) return error.InvalidView;
402
+ if (!std.math.isFinite(value.gap) or value.gap < 0 or !std.math.isFinite(value.grow) or value.grow < 0) return error.InvalidView;
403
+ if (value.padding) |padding| if (!std.math.isFinite(padding) or padding < 0) return error.InvalidView;
404
+ for ([_]?f32{ value.paddingTop, value.paddingBottom, value.paddingLeft, value.paddingRight }) |side| if (side) |padding| if (!std.math.isFinite(padding) or padding < 0) return error.InvalidView;
405
+ if ((value.kind == .code or value.kind == .input_group or value.kind == .input_group_actions) and
406
+ (value.paddingTop != null or value.paddingBottom != null or value.paddingLeft != null or value.paddingRight != null)) return error.InvalidView;
407
+ for ([_]f32{ value.width, value.height, value.minWidth, value.maxWidth }) |extent| if (!std.math.isFinite(extent) or extent < 0) return error.InvalidView;
408
+ if (value.kind == .resizable and value.gap != 0) return error.InvalidView;
409
+ if (value.resize != null and value.kind != .split) return error.InvalidView;
410
+ if (!std.math.isFinite(value.resizeOrigin) or (value.resizeOrigin != -1 and (value.resizeOrigin < 0 or value.resizeOrigin > 1))) return error.InvalidView;
411
+ if ((value.resizeDuration != 0 or value.resizeEasing != .standard or value.resizeOrigin != -1) and value.kind != .split) return error.InvalidView;
412
+ if ((value.resizeEasing != .standard or value.resizeOrigin != -1) and value.resizeDuration == 0) return error.InvalidView;
413
+ if (!std.math.isFinite(value.virtualItemExtent) or value.virtualItemExtent < 0) return error.InvalidView;
414
+ if ((value.columns != 0 or value.virtualized or value.virtualItemExtent != 0) and value.kind != .grid) return error.InvalidView;
415
+ if (value.columns > 9007199254740991) return error.InvalidView;
416
+ if (!std.math.isFinite(value.value)) return error.InvalidView;
417
+ if (value.virtualWindow) |window| if (value.kind != .scroll or window >= virtuals.len) return error.InvalidView;
418
+ if ((value.reachEnd != null or value.reachStart != null) and value.kind != .scroll) return error.InvalidView;
419
+ if ((value.valueX != null or value.axis != null or value.overscroll != null) and value.kind != .scroll) return error.InvalidView;
420
+ if (value.valueX) |offset| if (!std.math.isFinite(offset)) return error.InvalidView;
421
+ if (value.key != null and value.keyInt != null or value.globalKey != null and value.globalKeyInt != null) return error.InvalidView;
422
+ if (value.keySlot > 1024 or value.keySlot != 0 and value.key == null and value.keyInt == null) return error.InvalidView;
423
+ if (value.image > 9007199254740991) return error.InvalidView;
424
+ if ((value.imageLower == null) != (value.imageUpper == null) or value.imageLower != null and value.image != 0) return error.InvalidView;
425
+ if (value.imageLower != null and value.kind != .image and value.kind != .avatar) return error.InvalidView;
426
+ if (value.imageFit != null and value.kind != .image) return error.InvalidView;
427
+ if (value.kind == .image and (value.text.len != 0 or value.textBytes != null)) return error.InvalidView;
428
+ for ([_]?i64{ value.keyInt, value.globalKeyInt }) |int| if (int) |key| if (key < -9007199254740991 or key > 9007199254740991) return error.InvalidView;
429
+ const chart_metadata = value.chartSeries != null or value.chartXLabels != null or value.chartYMin != null or value.chartYMax != null or value.chartGridLines != null or value.chartBaseline != null or value.chartYLabels != null or value.chartHoverDetails != null or value.chartStrokeWidth != null;
430
+ if (chart_metadata and value.kind != .chart) return error.InvalidView;
431
+ if (value.kind == .chart and (value.chartSeries == null or value.chartXLabels == null or value.chartSeries.?.len == 0 or value.chartSeries.?.len > 64 or value.text.len != 0 or value.textBytes != null or value.role != .none or value.focusable or value.hold != null or value.drag != null or value.hoverEnter != null or value.hoverLeave != null or value.contextMenu.len != 0)) return error.InvalidView;
432
+ const modal = value.kind == .dialog or value.kind == .drawer or value.kind == .sheet;
433
+ const container = value.kind == .bubble or value.kind == .table or value.kind == .data_grid or value.kind == .popover or value.kind == .menu_surface or value.kind == .data_row or value.kind == .input_group or value.kind == .input_group_actions or modal or value.kind == .card or value.kind == .alert or value.kind == .grid or value.kind == .column or value.kind == .row or value.kind == .stack or value.kind == .scroll or value.kind == .panel or value.kind == .radio_group or value.kind == .button_group or value.kind == .breadcrumb or value.kind == .pagination or value.kind == .toggle_group or value.kind == .accordion or value.kind == .tabs or value.kind == .tree or value.kind == .list or value.kind == .list_item or value.kind == .dropdown_menu or value.kind == .split or value.kind == .resizable;
434
+ const tree_row = (value.kind == .column or value.kind == .row or value.kind == .panel or value.kind == .list_item) and value.role == .treeitem;
435
+ if (value.role == .treeitem and !tree_row) return error.InvalidView;
436
+ if (value.role == .tree and value.kind != .column and value.kind != .row and value.kind != .panel and value.kind != .scroll and value.kind != .tree) return error.InvalidView;
437
+ if (value.kind == .data_row and parent_kind != .table and parent_kind != .data_grid or value.kind == .data_cell and parent_kind != .data_row) return error.InvalidView;
438
+ if (!container and value.end != index + 1) return error.InvalidView;
439
+ if (value.kind == .list_item and value.end != index + 1 and value.text.len != 0) return error.InvalidView;
440
+ if (value.dismiss != null and !modal and value.kind != .dropdown_menu and value.kind != .popover and value.kind != .menu_surface) return error.InvalidView;
441
+ if (!std.math.isFinite(value.anchorOffset)) return error.InvalidView;
442
+ if ((value.anchor != null or value.anchorAlignment != .start or value.anchorOffset != 4) and value.kind != .dropdown_menu and value.kind != .popover and value.kind != .menu_surface and value.kind != .tooltip) return error.InvalidView;
443
+ if (value.tooltipDelay) |delay| if (value.kind != .tooltip or value.anchor == null or delay < 0) return error.InvalidView;
444
+ if (value.anchor == null and (value.anchorAlignment != .start or value.anchorOffset != 4)) return error.InvalidView;
445
+ if ((value.expanded != null or value.treeLevel != 0) and value.role != .treeitem) return error.InvalidView;
446
+ if (value.change != null and !tree_row and value.kind != .radio and value.kind != .slider and value.kind != .list_item) return error.InvalidView;
447
+ if (value.valueChange != null and (value.kind != .slider or value.change != null)) return error.InvalidView;
448
+ if (value.scroll != null and value.kind != .scroll) return error.InvalidView;
449
+ const text_entry = value.kind == .combobox or value.kind == .text_field or value.kind == .input or value.kind == .search_field or value.kind == .textarea or (value.kind == .code and value.codeEditable == true);
450
+ if ((value.pty != null or value.ptyBytes != null or value.scrollback != null or value.terminal != null) and value.kind != .terminal) return error.InvalidView;
451
+ if (value.pty != null and value.ptyBytes != null) return error.InvalidView;
452
+ if (value.pty) |key| if (key > 9007199254740991) return error.InvalidView;
453
+ if (value.kind == .terminal and (value.text.len != 0 or value.textBytes != null)) return error.InvalidView;
454
+ if (value.autofocus and !text_entry and value.kind != .terminal) return error.InvalidView;
455
+ if (value.submitOnEnter and value.kind != .textarea) return error.InvalidView;
456
+ if (value.input != null and !text_entry) return error.InvalidView;
457
+ if (value.submit != null and !text_entry and !tree_row) return error.InvalidView;
458
+ if (value.placeholder.len != 0 and !text_entry and value.kind != .select) return error.InvalidView;
459
+ if (value.wrap != null and value.kind != .text and value.kind != .code) return error.InvalidView;
460
+ const legacy_paragraph = value.spanWeight != null or value.spanColor != null or value.spanScale != null;
461
+ const paragraph = value.spans != null or legacy_paragraph;
462
+ if (value.spans) |spans| {
463
+ if (legacy_paragraph or value.text.len != 0 or value.wrap != null or spans.len == 0 or spans.len > 2048) return error.InvalidView;
464
+ for (spans) |span| if (span.scale) |scale| if (!std.math.isFinite(scale) or scale <= 0) return error.InvalidView;
465
+ }
466
+ if (paragraph and value.kind != .text) return error.InvalidView;
467
+ if (value.spanScale) |scale| if (!std.math.isFinite(scale) or scale <= 0) return error.InvalidView;
468
+ if (value.kind == .markdown) {
469
+ if (value.markdownRecipe == null or value.end != index + 1) return error.InvalidView;
470
+ const defaults: Record = .{ .end = value.end, .kind = .markdown, .text = "" };
471
+ inline for (@typeInfo(Record).@"struct".fields) |field| {
472
+ if (comptime !std.mem.eql(u8, field.name, "end") and !std.mem.eql(u8, field.name, "kind") and !std.mem.eql(u8, field.name, "markdownRecipe") and !std.mem.eql(u8, field.name, "markdownLink") and !std.mem.eql(u8, field.name, "markdownDetails")) {
473
+ const provided = @field(value, field.name);
474
+ const expected = @field(defaults, field.name);
475
+ if (comptime @typeInfo(field.type) == .pointer and @typeInfo(field.type).pointer.size == .slice) {
476
+ if (provided.len != expected.len) return error.InvalidView;
477
+ } else if (!std.meta.eql(provided, expected)) return error.InvalidView;
478
+ }
479
+ }
480
+ return stampCompound(try sdk.canvas.MarkdownContentPolicy.execute(core.Msg, ui, value.markdownRecipe.?.bytes, .{
481
+ .on_link = if (value.markdownLink) |tag| try markdownLinkEvent(tag) else null,
482
+ .on_details = if (value.markdownDetails) |tag| try markdownDetailsEvent(tag) else null,
483
+ }));
484
+ }
485
+ if (value.markdownRecipe != null or value.markdownLink != null or value.markdownDetails != null) return error.InvalidView;
486
+ if (value.kind == .code) {
487
+ if (value.codeLanguage == null or value.codeEditable == null or value.codeNumbered == null or value.wrap == null or value.codeLineDigits != null or
488
+ paragraph or value.placeholder.len != 0 or value.submitOnEnter or value.submit != null or value.contextMenu.len != 0 or value.autofocus or
489
+ value.disabled or value.selected or value.checked or value.padding != null or value.gap != 0 or value.maxWidth != 0 or
490
+ value.role != .none or value.focusable or value.windowDrag or value.image != 0 or value.icon.len != 0 or value.command.len != 0 or
491
+ value.press != null or value.doublePress != null or value.hold != null or value.drag != null or value.hoverEnter != null or value.hoverLeave != null or
492
+ value.background != null or value.foreground != null or value.borderColor != null or value.focusRing != null or value.radius != null)
493
+ return error.InvalidView;
494
+ } else if (value.codeEditable != null or value.codeNumbered != null) return error.InvalidView;
495
+ if (value.kind != .code and (value.codeLanguage != null or value.codeLineDigits != null)) {
496
+ if (value.codeLanguage == null or value.codeLineDigits == null or value.kind != .textarea or
497
+ value.codeLineDigits.? > 5 or value.placeholder.len != 0 or value.submitOnEnter or paragraph)
498
+ return error.InvalidView;
499
+ }
500
+ var code_diff: ?sdk.canvas.CodeDiffLines = null;
501
+ if (value.codeAddedLines != null or value.codeRemovedLines != null) {
502
+ if (value.codeLanguage == null or value.codeAddedLines == null or value.codeRemovedLines == null) return error.InvalidView;
503
+ const added = try codeLineMask(value.codeAddedLines.?);
504
+ const removed = try codeLineMask(value.codeRemovedLines.?);
505
+ if (added & removed != 0) return error.InvalidView;
506
+ if (added != 0 or removed != 0) code_diff = .{ .added = added, .removed = removed };
507
+ }
508
+ if (value.listItemIndex) |item| if (value.listItemCount == null or item >= value.listItemCount.?) return error.InvalidView;
509
+ var children: std.ArrayList(Ui.Node) = .empty;
510
+ var child_index = index + 1;
511
+ while (child_index < value.end) {
512
+ try children.append(ui.arena, try node(ui, records, child_index, value.end, depth + 1, virtuals, value.kind));
513
+ child_index = records[child_index].end;
514
+ }
515
+ if (value.kind == .split and children.items.len != 2) return error.InvalidView;
516
+ if (value.kind == .code) {
517
+ const added = try ui.arena.alloc(usize, if (value.codeAddedLines) |lines| lines.len else 0);
518
+ const removed = try ui.arena.alloc(usize, if (value.codeRemovedLines) |lines| lines.len else 0);
519
+ if (value.codeAddedLines) |lines| for (lines, added) |line, *slot| {
520
+ slot.* = line;
521
+ };
522
+ if (value.codeRemovedLines) |lines| for (lines, removed) |line, *slot| {
523
+ slot.* = line;
524
+ };
525
+ return stampCompound(ui.code(.{
526
+ .key = if (value.key) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .str = key }, value.keySlot) else if (value.keyInt) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .int = @bitCast(key) }, value.keySlot) else null,
527
+ .global_key = if (value.globalKey) |key| .{ .str = key } else if (value.globalKeyInt) |key| .{ .int = @bitCast(key) } else null,
528
+ .width = value.width,
529
+ .height = value.height,
530
+ .min_width = value.minWidth,
531
+ .grow = value.grow,
532
+ .semantics = .{ .label = value.label },
533
+ .language = value.codeLanguage.?,
534
+ .wrap = value.wrap.?,
535
+ .editable = value.codeEditable.?,
536
+ .line_numbers = value.codeNumbered.?,
537
+ .added_lines = added,
538
+ .removed_lines = removed,
539
+ .on_input = if (value.input) |tag| try inputEvent(tag) else null,
540
+ }, value.text));
541
+ }
542
+ if (value.kind == .input_group_actions) {
543
+ if (parent_kind != .input_group) return error.InvalidView;
544
+ return stampCompound(ui.inputGroupActions(.{
545
+ .key = if (value.key) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .str = key }, value.keySlot) else if (value.keyInt) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .int = @bitCast(key) }, value.keySlot) else null,
546
+ .global_key = if (value.globalKey) |key| .{ .str = key } else if (value.globalKeyInt) |key| .{ .int = @bitCast(key) } else null,
547
+ .gap = value.gap,
548
+ }, children.items));
549
+ }
550
+ if (value.kind == .input_group) {
551
+ if (children.items.len < 1 or children.items.len > 2 or children.items[0].widget.kind != .textarea) return error.InvalidView;
552
+ if (children.items.len == 2 and records[records[index + 1].end].kind != .input_group_actions) return error.InvalidView;
553
+ return stampCompound(ui.inputGroup(.{
554
+ .key = if (value.key) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .str = key }, value.keySlot) else if (value.keyInt) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .int = @bitCast(key) }, value.keySlot) else null,
555
+ .global_key = if (value.globalKey) |key| .{ .str = key } else if (value.globalKeyInt) |key| .{ .int = @bitCast(key) } else null,
556
+ .width = value.width,
557
+ .height = value.height,
558
+ .min_width = value.minWidth,
559
+ .grow = value.grow,
560
+ .semantics = .{ .role = value.role, .label = value.label },
561
+ }, children.items[0], if (children.items.len == 2) children.items[1] else null));
562
+ }
563
+ const kind: sdk.canvas.WidgetKind = switch (value.kind) {
564
+ .input_group_actions, .code, .markdown => return error.InvalidView,
565
+ .spacer => .stack,
566
+ .scroll => .scroll_view,
567
+ inline else => |tag| @field(sdk.canvas.WidgetKind, @tagName(tag)),
568
+ };
569
+ if (value.contextMenu.len > 32) return error.InvalidView;
570
+ const non_hit_target = switch (value.kind) {
571
+ .row, .column, .stack, .list, .grid, .split, .tree, .breadcrumb, .button_group, .pagination, .radio_group, .tabs, .toggle_group, .badge, .avatar, .tooltip, .separator, .spacer, .table, .data_grid, .data_row, .skeleton, .spinner, .icon => true,
572
+ else => false,
573
+ };
574
+ if (value.quietHover and non_hit_target) return error.InvalidView;
575
+ if (value.contextMenu.len > 0 and non_hit_target and value.press == null and value.doublePress == null and value.toggle == null and value.hold == null and value.drag == null) return error.InvalidView;
576
+ const context_menu = try ui.arena.alloc(Ui.ContextMenuItem, value.contextMenu.len);
577
+ for (value.contextMenu, context_menu) |raw_item, *slot| {
578
+ var item = raw_item;
579
+ item.label = byteText(item.labelBytes, item.label);
580
+ if (item.separator and (item.label.len != 0 or item.press != null) or
581
+ !item.separator and (item.label.len == 0 or item.press == null)) return error.InvalidView;
582
+ slot.* = .{ .label = item.label, .msg = if (item.press) |bytes| try event(ui, bytes) else null, .enabled = item.enabled, .separator = item.separator };
583
+ }
584
+ var result = ui.el(kind, .{
585
+ .style = .{ .quiet_hover = value.quietHover, .stroke_width = if (value.chartStrokeWidth) |bits| @as(f32, @bitCast(bits)) else null },
586
+ .context_menu = context_menu,
587
+ .key = if (value.key) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .str = key }, value.keySlot) else if (value.keyInt) |key| try sdk.canvas.forSlotKey(ui.arena, .{ .int = @bitCast(key) }, value.keySlot) else null,
588
+ .global_key = if (value.globalKey) |key| .{ .str = key } else if (value.globalKeyInt) |key| .{ .int = @bitCast(key) } else null,
589
+ .text = value.text,
590
+ .placeholder = value.placeholder,
591
+ .command = value.command,
592
+ .wrap = value.wrap,
593
+ .overflow = value.overflow,
594
+ .text_alignment = value.textAlignment,
595
+ .submit_on_enter = value.submitOnEnter,
596
+ .autofocus = value.autofocus,
597
+ .columns = value.columns,
598
+ .virtualized = value.virtualized,
599
+ .virtual_item_extent = value.virtualItemExtent,
600
+ .gap = value.gap,
601
+ .padding = value.padding,
602
+ .grow = value.grow,
603
+ .width = value.width,
604
+ .height = value.height,
605
+ .min_width = value.minWidth,
606
+ .max_width = value.maxWidth,
607
+ .resize_duration = value.resizeDuration,
608
+ .resize_easing = value.resizeEasing,
609
+ .resize_origin = value.resizeOrigin,
610
+ .value = value.value,
611
+ .value_x = value.valueX orelse 0,
612
+ .axis = value.axis orelse .vertical,
613
+ .overscroll = value.overscroll orelse .default,
614
+ .image = if (value.imageLower) |lower| @as(u64, lower) | (@as(u64, value.imageUpper.?) << 32) else value.image,
615
+ .pty = value.pty orelse 0,
616
+ .pty_name = if (value.ptyBytes) |key| key.bytes else "",
617
+ .scrollback = value.scrollback orelse 0,
618
+ .on_terminal = if (value.terminal) |tag| try terminalEvent(tag) else null,
619
+ .icon = value.icon,
620
+ .icon_placement = value.iconPlacement,
621
+ .window_drag = value.windowDrag,
622
+ .backdrop_blur_token = value.backdropBlur,
623
+ .semantics = .{ .role = value.role, .label = value.label, .focusable = value.focusable, .list_item_index = value.listItemIndex, .list_item_count = value.listItemCount },
624
+ .style_tokens = .{ .background = value.background, .foreground = value.foreground, .border_color = value.borderColor, .focus_ring = value.focusRing, .accent = value.accent, .accent_foreground = value.accentForeground, .radius = value.radius },
625
+ .main = value.main,
626
+ .cross = value.cross,
627
+ .size = value.size,
628
+ .variant = value.variant,
629
+ .checked = value.checked,
630
+ .disabled = value.disabled,
631
+ .selected = value.selected,
632
+ .expanded = value.expanded,
633
+ .tree_level = value.treeLevel,
634
+ .on_press = if (value.press) |bytes| try event(ui, bytes) else null,
635
+ .on_double_press = if (value.doublePress) |bytes| try event(ui, bytes) else null,
636
+ .on_hold = if (value.hold) |bytes| try event(ui, bytes) else null,
637
+ .on_hover_enter = if (value.hoverEnter) |bytes| try event(ui, bytes) else null,
638
+ .on_hover_leave = if (value.hoverLeave) |bytes| try event(ui, bytes) else null,
639
+ .on_reach_end = if (value.reachEnd) |bytes| try event(ui, bytes) else null,
640
+ .on_reach_start = if (value.reachStart) |bytes| try event(ui, bytes) else null,
641
+ .on_change = if (value.change) |bytes| try event(ui, bytes) else null,
642
+ .on_toggle = if (value.toggle) |bytes| try event(ui, bytes) else null,
643
+ .on_drag = if (value.drag) |bytes| try dragEvent(ui, bytes) else null,
644
+ .on_scroll = if (value.scroll) |tag| try scrollEvent(tag) else null,
645
+ .on_input = if (value.input) |tag| try inputEvent(tag) else null,
646
+ .on_value = if (value.valueChange) |tag| try valueEvent(tag) else null,
647
+ .on_resize = if (value.resize) |tag| try valueEvent(tag) else null,
648
+ .on_submit = if (value.submit) |bytes| try event(ui, bytes) else null,
649
+ .on_dismiss = if (value.dismiss) |bytes| try event(ui, bytes) else null,
650
+ .anchor = value.anchor,
651
+ .anchor_alignment = value.anchorAlignment,
652
+ .anchor_offset = value.anchorOffset,
653
+ .tooltip_delay = value.tooltipDelay orelse -1,
654
+ }, children.items);
655
+ if (value.kind == .avatar) result.widget.image_fit = .cover;
656
+ if (value.imageFit) |fit| result.widget.image_fit = fit;
657
+ if (value.paddingTop) |padding| result.widget.layout.padding.top = padding;
658
+ if (value.paddingBottom) |padding| result.widget.layout.padding.bottom = padding;
659
+ if (value.paddingLeft) |padding| result.widget.layout.padding.left = padding;
660
+ if (value.paddingRight) |padding| result.widget.layout.padding.right = padding;
661
+ if (value.virtualWindow) |window| {
662
+ const resolved = virtuals[window];
663
+ if (children.items.len != resolved.range.itemCount()) return error.InvalidView;
664
+ for (children.items) |child| if (child.key == null and child.global_key == null) return error.InvalidView;
665
+ var options = resolved.options;
666
+ options.width = value.width;
667
+ options.height = value.height;
668
+ options.min_width = value.minWidth;
669
+ options.grow = value.grow;
670
+ options.padding = value.padding orelse 0;
671
+ options.style_tokens = result.style_tokens;
672
+ options.semantics = result.widget.semantics;
673
+ options.overscroll = value.overscroll orelse .default;
674
+ options.on_scroll = result.on_scroll;
675
+ options.on_reach_end = result.on_reach_end;
676
+ options.on_reach_start = result.on_reach_start;
677
+ result = ui.virtualList(options, resolved.range, .{children.items});
678
+ }
679
+ if (value.videoSrc) |src| {
680
+ if (src.len > 0) ui.video_declaration = .{ .src = src, .controls = value.videoControls, .autoplay = value.videoAutoplay, .loop = value.videoLoop, .muted = value.videoMuted };
681
+ }
682
+ result.widget.video_control = value.videoControl;
683
+ if (value.zeroIntrinsic) |flag| result.widget.layout.zero_intrinsic = flag;
684
+ if (value.clipContent) |flag| result.widget.layout.clip_content = flag;
685
+ if (comptime @hasDecl(core, "nativeWindowPolicy")) result.widget.appearance_policy = core.nativeWindowPolicy;
686
+ if (comptime @hasDecl(core, "nativeTextPolicy")) {
687
+ // Every primitive can carry composed semantics. Specialized callbacks
688
+ // below also accept shared keyboard, semantic-control and action tags.
689
+ result.widget.interaction_policy = core.nativeTextPolicy;
690
+ }
691
+ if (comptime @hasDecl(core, "nativeRadioPolicy")) {
692
+ if (value.kind == .radio or value.kind == .radio_group) result.widget.interaction_policy = core.nativeRadioPolicy;
693
+ }
694
+ if (comptime @hasDecl(core, "nativeTabsPolicy")) {
695
+ if (value.kind == .tabs or value.kind == .segmented_control) result.widget.interaction_policy = core.nativeTabsPolicy;
696
+ }
697
+ if (comptime @hasDecl(core, "nativeTreePolicy")) {
698
+ if (value.kind == .tree or value.role == .tree or value.role == .treeitem) result.widget.interaction_policy = core.nativeTreePolicy;
699
+ }
700
+ if (comptime @hasDecl(core, "nativeListPolicy")) {
701
+ if ((value.kind == .list or value.kind == .list_item) and value.role != .tree and value.role != .treeitem) result.widget.interaction_policy = core.nativeListPolicy;
702
+ }
703
+ if (comptime @hasDecl(core, "nativeMenuPolicy")) {
704
+ if (value.kind == .dropdown_menu or value.kind == .menu_item) result.widget.interaction_policy = core.nativeMenuPolicy;
705
+ }
706
+ if (comptime @hasDecl(core, "nativeTogglePolicy")) {
707
+ if (value.kind == .toggle_group or value.kind == .toggle_button or value.kind == .checkbox or value.kind == .switch_control or value.kind == .toggle) result.widget.interaction_policy = core.nativeTogglePolicy;
708
+ }
709
+ if (comptime @hasDecl(core, "nativeAccordionPolicy")) {
710
+ if (value.kind == .accordion) result.widget.interaction_policy = core.nativeAccordionPolicy;
711
+ }
712
+ if (comptime @hasDecl(core, "nativeSliderPolicy")) {
713
+ if (value.kind == .slider) result.widget.interaction_policy = core.nativeSliderPolicy;
714
+ }
715
+ if (comptime @hasDecl(core, "nativeSplitPolicy")) {
716
+ if (value.kind == .split) result.widget.interaction_policy = core.nativeSplitPolicy;
717
+ }
718
+ if (comptime @hasDecl(core, "nativeResizablePolicy")) {
719
+ if (value.kind == .resizable) result.widget.interaction_policy = core.nativeResizablePolicy;
720
+ }
721
+ if (comptime @hasDecl(core, "nativeScrollPolicy")) {
722
+ if (value.kind == .scroll) {
723
+ result.widget.runtime_flags.compiled_scroll_policy = true;
724
+ result.widget.interaction_policy = if (value.role == .tree) scrollTreePolicy else core.nativeScrollPolicy;
725
+ }
726
+ }
727
+ if (value.kind == .chart) {
728
+ const raw_series = value.chartSeries.?;
729
+ const series = try ui.arena.alloc(sdk.canvas.ChartSeries, raw_series.len);
730
+ for (raw_series, series) |raw, *entry| {
731
+ if (raw.values.len > sdk.canvas.max_chart_points_per_series or raw.fill and raw.kind != .line) return error.InvalidView;
732
+ const values = try ui.arena.alloc(f32, raw.values.len);
733
+ for (raw.values, values) |bits, *sample| sample.* = @bitCast(bits);
734
+ entry.* = .{ .kind = if (raw.kind == .bar) .bar else .line, .values = values, .color = raw.color, .fill = raw.fill, .label = raw.label.bytes };
735
+ }
736
+ const labels = try ui.arena.alloc([]const u8, value.chartXLabels.?.len);
737
+ for (value.chartXLabels.?, labels) |raw, *label| label.* = raw.bytes;
738
+ result.widget.chart = .{
739
+ .series = series,
740
+ .x_labels = labels,
741
+ .y_min = if (value.chartYMin) |bits| @as(f32, @bitCast(bits)) else null,
742
+ .y_max = if (value.chartYMax) |bits| @as(f32, @bitCast(bits)) else null,
743
+ .grid_lines = value.chartGridLines orelse 0,
744
+ .baseline = value.chartBaseline orelse false,
745
+ .y_labels = value.chartYLabels orelse false,
746
+ .hover_details = value.chartHoverDetails orelse false,
747
+ };
748
+ }
749
+ if (value.spans) |runs| {
750
+ // Rebase every run into one native-owned paragraph buffer. No pointer
751
+ // into the compiled result arena survives this tree generation.
752
+ var text_len: usize = 0;
753
+ for (runs) |run| text_len += (byteText(run.textBytes, run.text)).len;
754
+ const text_bytes = try ui.arena.alloc(u8, text_len);
755
+ const spans = try ui.arena.alloc(sdk.canvas.TextSpan, runs.len);
756
+ var offset: usize = 0;
757
+ for (runs, spans) |raw_run, *span| {
758
+ var run = raw_run;
759
+ run.text = byteText(run.textBytes, run.text);
760
+ @memcpy(text_bytes[offset .. offset + run.text.len], run.text);
761
+ span.* = .{ .text = text_bytes[offset .. offset + run.text.len], .weight = run.weight, .color = run.color, .scale = run.scale orelse 0, .monospace = run.monospace, .italic = run.italic, .underline = run.underline };
762
+ offset += run.text.len;
763
+ }
764
+ result.widget.text = text_bytes;
765
+ result.widget.spans = spans;
766
+ } else if (legacy_paragraph) {
767
+ const spans = try ui.arena.alloc(sdk.canvas.TextSpan, 1);
768
+ spans[0] = .{ .text = result.widget.text, .weight = value.spanWeight orelse .regular, .color = value.spanColor, .scale = value.spanScale orelse 0 };
769
+ result.widget.spans = spans;
770
+ }
771
+ if (value.codeLanguage) |language| {
772
+ const spans = try ui.arena.alloc(sdk.canvas.TextSpan, 1);
773
+ spans[0] = .{ .text = result.widget.text, .monospace = true, .color = .syntax_plain };
774
+ result.widget.spans = spans;
775
+ result.widget.runtime_flags.code_editor = true;
776
+ result.widget.code_language = language;
777
+ result.widget.code_line_number_digits = value.codeLineDigits.?;
778
+ if (code_diff) |lines| result.widget.setCodeDiffLines(lines);
779
+ result.widget.text_no_wrap = true;
780
+ result.widget.layout.clip_content = true;
781
+ }
782
+ return result;
783
+ }
784
+
785
+ test "compiled per-side padding keeps uniform defaults and rejects invalid extents" {
786
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
787
+ defer arena.deinit();
788
+ var ui = Ui.init(arena.allocator());
789
+ const node_value = try decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"column\",\"text\":\"\",\"padding\":8,\"paddingTop\":2,\"paddingLeft\":16}]}" );
790
+ try std.testing.expectEqualDeep(@TypeOf(node_value.widget.layout.padding){ .top = 2, .bottom = 8, .left = 16, .right = 8 }, node_value.widget.layout.padding);
791
+ for ([_][]const u8{ "paddingTop", "paddingBottom", "paddingLeft", "paddingRight" }) |field| {
792
+ const invalid = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"column\",\"text\":\"\",\"{s}\":-1}}]}}", .{field});
793
+ try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
794
+ const group = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":2,\"kind\":\"input_group\",\"text\":\"\",\"{s}\":0}},{{\"end\":2,\"kind\":\"textarea\",\"text\":\"\"}}]}}", .{field});
795
+ try std.testing.expectError(error.InvalidView, decode(&ui, group));
796
+ const actions = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":3,\"kind\":\"input_group\",\"text\":\"\"}},{{\"end\":2,\"kind\":\"textarea\",\"text\":\"\"}},{{\"end\":3,\"kind\":\"input_group_actions\",\"text\":\"\",\"{s}\":0}}]}}", .{field});
797
+ try std.testing.expectError(error.InvalidView, decode(&ui, actions));
798
+ }
799
+ }
800
+
801
+ test "compiled image records preserve exact identities fit and independent native ownership" {
802
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
803
+ defer arena.deinit();
804
+ var ui = Ui.init(arena.allocator());
805
+ for ([_]u64{ 0, 1, 4294967295, 4294967296, 9007199254740993, 9223372036854775808, std.math.maxInt(u64) }) |id| {
806
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"image\",\"text\":\"\",\"imageLower\":{d},\"imageUpper\":{d},\"imageFit\":\"cover\",\"width\":140,\"height\":140,\"label\":\"Art bay\"}}]}}", .{ @as(u32, @truncate(id)), id >> 32 });
807
+ const retained = try decode(&ui, bytes);
808
+ @memset(bytes, 'x');
809
+ _ = try decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"image\",\"text\":\"\",\"image\":7}]}" );
810
+ var reference = Ui.init(arena.allocator());
811
+ reference.construction_policy = ui.construction_policy;
812
+ reference.composition_policy = ui.composition_policy;
813
+ var expected = reference.image(.{ .image = id, .width = 140, .height = 140, .semantics = .{ .label = "Art bay" } });
814
+ if (comptime @hasDecl(core, "nativeWindowPolicy")) expected.widget.appearance_policy = core.nativeWindowPolicy;
815
+ if (comptime @hasDecl(core, "nativeTextPolicy")) expected.widget.interaction_policy = core.nativeTextPolicy;
816
+ expected.widget.image_fit = .cover;
817
+ try std.testing.expectEqualDeep(expected.widget, retained.widget);
818
+ }
819
+ for ([_][]const u8{
820
+ "\"imageLower\":1", "\"imageUpper\":1", "\"imageLower\":0,\"imageUpper\":0,\"image\":1",
821
+ "\"imageLower\":4294967296,\"imageUpper\":0", "\"imageLower\":0,\"imageUpper\":-1",
822
+ "\"imageLower\":1.5,\"imageUpper\":0", "\"image\":9007199254740992", "\"imageFit\":\"invalid\"",
823
+ }) |fields| {
824
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"image\",\"text\":\"\",{s}}}]}}", .{fields});
825
+ try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
826
+ }
827
+ for ([_][]const u8{
828
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"image\",\"text\":\"caption\"}]}",
829
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"image\",\"text\":\"\",\"textBytes\":[]}]}",
830
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"panel\",\"text\":\"\",\"imageLower\":0,\"imageUpper\":0}]}",
831
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"avatar\",\"text\":\"DK\",\"imageFit\":\"cover\"}]}",
832
+ }) |bytes| try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
833
+ }
834
+
835
+ test "compiled app Markdown rejects unrelated fields malformed bytes and incompatible routes" {
836
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
837
+ defer arena.deinit();
838
+ var recipe = [_]u8{0} ** 80;
839
+ recipe[0] = 1;
840
+ recipe[4] = 1;
841
+ recipe[16] = 3;
842
+ @memset(recipe[48..52], 255);
843
+ @memset(recipe[76..80], 255);
844
+ const encoded = try std.json.Stringify.valueAlloc(arena.allocator(), recipe, .{});
845
+ for ([_][]const u8{
846
+ "\"textBytes\":[]", "\"labelBytes\":[]", "\"placeholderBytes\":[]", "\"placeholder\":\"x\"", "\"command\":\"x\"",
847
+ "\"wrap\":false", "\"keyInt\":1", "\"globalKeyInt\":1", "\"columns\":1", "\"virtualized\":true",
848
+ "\"padding\":0", "\"minWidth\":1", "\"maxWidth\":1", "\"image\":1", "\"icon\":\"x\"",
849
+ "\"role\":\"group\"", "\"foreground\":\"text_muted\"", "\"windowDrag\":true", "\"main\":\"center\"", "\"cross\":\"center\"",
850
+ "\"checked\":true", "\"disabled\":true", "\"selected\":true", "\"focusable\":true", "\"spans\":[]",
851
+ "\"spanScale\":1", "\"textAlignment\":\"center\"", "\"codeLineDigits\":1", "\"videoAutoplay\":false", "\"clipContent\":true",
852
+ "\"zeroIntrinsic\":true", "\"overflow\":\"clip\"", "\"markdownLink\":255", "\"markdownDetails\":255",
853
+ }) |fields| {
854
+ var ui = Ui.init(arena.allocator());
855
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"markdown\",\"text\":\"\",\"markdownRecipe\":{s},{s}}}]}}", .{ encoded, fields });
856
+ try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
857
+ }
858
+ for ([_][]const u8{ "null", "[1.0]", "[1.5]", "[-1]", "[256]", "[true]", "[\"1\"]" }) |invalid| {
859
+ var ui = Ui.init(arena.allocator());
860
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"markdown\",\"text\":\"\",\"markdownRecipe\":{s}}}]}}", .{invalid});
861
+ try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
862
+ }
863
+ var ui = Ui.init(arena.allocator());
864
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"markdown\",\"text\":\"\",\"markdownRecipe\":{s}}}]}}", .{encoded});
865
+ _ = try decode(&ui, bytes);
866
+ }
867
+
868
+ test "compiled app code rejects incomplete modes and unrelated renderer metadata" {
869
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
870
+ defer arena.deinit();
871
+ for ([_][]const u8{
872
+ "\"codeNumbered\":false,\"wrap\":true",
873
+ "\"codeEditable\":false,\"wrap\":true",
874
+ "\"codeEditable\":false,\"codeNumbered\":false",
875
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"codeLineDigits\":1",
876
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"input\":0",
877
+ "\"codeEditable\":true,\"codeNumbered\":false,\"wrap\":true,\"submitOnEnter\":true",
878
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"role\":\"group\"",
879
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"focusable\":true",
880
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"padding\":0",
881
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"image\":1",
882
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"codeAddedLines\":[1]",
883
+ "\"codeEditable\":false,\"codeNumbered\":false,\"wrap\":true,\"codeAddedLines\":[1],\"codeRemovedLines\":[1]",
884
+ }) |fields| {
885
+ var ui = Ui.init(arena.allocator());
886
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"code\",\"text\":\"\",\"codeLanguage\":\"plain\",{s}}}]}}", .{fields});
887
+ try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
888
+ }
889
+ }
890
+
891
+ // Compound records create their root after decoding the child primitives.
892
+ // Attach both owners exactly as the ordinary primitive decoder does.
893
+ fn stampCompound(node_value: Ui.Node) Ui.Node {
894
+ var result = node_value;
895
+ if (comptime @hasDecl(core, "nativeWindowPolicy")) result.widget.appearance_policy = core.nativeWindowPolicy;
896
+ if (comptime @hasDecl(core, "nativeTextPolicy")) result.widget.interaction_policy = core.nativeTextPolicy;
897
+ return result;
898
+ }
899
+
900
+ /// Pack already parsed, unique line ordinals into the renderer's exact masks.
901
+ /// The compiled component owns text parsing; this checks untrusted tree data.
902
+ fn codeLineMask(lines: []const u8) !u128 {
903
+ if (lines.len > sdk.canvas.code.max_diff_lines) return error.InvalidView;
904
+ var mask: u128 = 0;
905
+ for (lines) |line| {
906
+ if (line == 0 or line > sdk.canvas.code.max_diff_lines) return error.InvalidView;
907
+ const bit = @as(u128, 1) << @as(u7, @intCast(line - 1));
908
+ if (mask & bit != 0) return error.InvalidView;
909
+ mask |= bit;
910
+ }
911
+ return mask;
912
+ }
913
+
914
+ /// A scroll container can also own logical tree navigation. The explicit
915
+ /// scroll tag preserves both policies in one callback without tree scans.
916
+ fn scrollTreePolicy(request: []const u8, output: []u8) usize {
917
+ if (comptime @hasDecl(core, "nativeScrollPolicy") and @hasDecl(core, "nativeTreePolicy")) {
918
+ if (request.len > 0 and request[0] >= 128) return core.nativeScrollPolicy(request, output);
919
+ return core.nativeTreePolicy(request, output);
920
+ } else unreachable;
921
+ }
922
+
923
+ fn event(ui: *Ui, bytes: []const u8) !core.Msg {
924
+ if (bytes.len < 2 or bytes[0] != 1 or bytes[1] >= @typeInfo(core.Msg).@"union".fields.len) return error.InvalidView;
925
+ return core.nativeViewEvent(bytes, ui.arena) orelse error.InvalidView;
926
+ }
927
+
928
+ fn dragEvent(ui: *Ui, bytes: []const u8) !core.Msg {
929
+ const msg = try event(ui, bytes);
930
+ switch (msg) {
931
+ inline else => |payload| {
932
+ if (comptime !sdk.canvas.ui_markup_reflect.declaredWidgetDragDropRecord(@TypeOf(payload))) return error.InvalidView;
933
+ },
934
+ }
935
+ return msg;
936
+ }
937
+
938
+ fn scrollEvent(tag: u8) !Ui.ScrollMsgFn {
939
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, index| {
940
+ if (comptime sdk.canvas.ui_markup_reflect.declaredScrollStateRecord(field.type)) {
941
+ if (tag == index) return Ui.translatedScrollMsg(@field(std.meta.Tag(core.Msg), field.name), field.type);
942
+ }
943
+ }
944
+ return error.InvalidView;
945
+ }
946
+
947
+ fn terminalEvent(tag: u8) !Ui.TerminalMsgFn {
948
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, index| {
949
+ if (comptime sdk.canvas.ui_markup_reflect.declaredTerminalStateRecord(field.type)) {
950
+ if (tag == index) return Ui.translatedTerminalMsg(@field(std.meta.Tag(core.Msg), field.name), field.type);
951
+ }
952
+ }
953
+ return error.InvalidView;
954
+ }
955
+
956
+ fn inputEvent(tag: u8) !Ui.InputMsgFn {
957
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, index| {
958
+ if (comptime sdk.canvas.ui_markup_reflect.declaredTextInputUnion(field.type)) {
959
+ if (tag == index) return Ui.translatedInputMsg(@field(std.meta.Tag(core.Msg), field.name), field.type);
960
+ }
961
+ }
962
+ return error.InvalidView;
963
+ }
964
+
965
+ fn markdownLinkEvent(tag: u8) !Ui.LinkMsgFn {
966
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, index| {
967
+ if (comptime field.type == []const u8) if (tag == index) return Ui.linkMsg(@field(std.meta.Tag(core.Msg), field.name));
968
+ }
969
+ return error.InvalidView;
970
+ }
971
+ fn markdownDetailsEvent(tag: u8) !*const fn (usize) core.Msg {
972
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, index| {
973
+ if (comptime @typeInfo(field.type) == .int or @typeInfo(field.type) == .float) {
974
+ if (tag == index) return struct {
975
+ fn make(ordinal: usize) core.Msg {
976
+ const value: field.type = if (comptime @typeInfo(field.type) == .int) @intCast(ordinal) else @floatFromInt(ordinal);
977
+ return @unionInit(core.Msg, field.name, value);
978
+ }
979
+ }.make;
980
+ }
981
+ }
982
+ return error.InvalidView;
983
+ }
984
+
985
+ fn valueEvent(tag: u8) !Ui.ValueMsgFn {
986
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, index| {
987
+ if (comptime @typeInfo(field.type) == .float) {
988
+ if (tag == index) return Ui.translatedValueMsg(@field(std.meta.Tag(core.Msg), field.name), field.type);
989
+ }
990
+ }
991
+ return error.InvalidView;
992
+ }
993
+
994
+ test "compiled scrolls reject misplaced properties and incompatible channels" {
995
+ if (comptime enabled) {
996
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
997
+ defer arena.deinit();
998
+ var ui = Ui.init(arena.allocator());
999
+ for ([_][]const u8{
1000
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"column\",\"text\":\"\",\"axis\":\"both\"}]}",
1001
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"column\",\"text\":\"\",\"valueX\":2}]}",
1002
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"column\",\"text\":\"\",\"overscroll\":\"none\"}]}",
1003
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"scroll\",\"text\":\"\",\"scroll\":255}]}",
1004
+ }) |source| try std.testing.expectError(error.InvalidView, decode(&ui, source));
1005
+ if (comptime @hasDecl(core, "nativeScrollPolicy") and @hasDecl(core, "nativeTreePolicy")) {
1006
+ const source = "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"scroll\",\"text\":\"\",\"role\":\"tree\"}]}";
1007
+ const result = try decode(&ui, source);
1008
+ try std.testing.expect(result.widget.runtime_flags.compiled_scroll_policy);
1009
+ const offset = sdk.canvas.widgetCompiledScrollResult(result.widget, .{ .operation = 1, .current = 20.5, .viewport = 100, .content = 400, .delta = 30.25 }).?;
1010
+ try std.testing.expectEqual(@as(f32, 50.75), offset.dx);
1011
+ const tree_request = [_]u8{ 0, 0, 0, 1, 0, 255, 255, 2, 0, 0, 0 };
1012
+ var expected: [2]u8 = undefined;
1013
+ var actual: [2]u8 = undefined;
1014
+ try std.testing.expectEqual(core.nativeTreePolicy(&tree_request, &expected), result.widget.interaction_policy.?(&tree_request, &actual));
1015
+ try std.testing.expectEqualSlices(u8, &expected, &actual);
1016
+ }
1017
+ } else return error.SkipZigTest;
1018
+ }
1019
+
1020
+ test "compiled sliders reject incompatible value channels" {
1021
+ if (comptime enabled) {
1022
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1023
+ defer arena.deinit();
1024
+ var ui = Ui.init(arena.allocator());
1025
+ for ([_][]const u8{
1026
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"button\",\"text\":\"\",\"valueChange\":0}]}",
1027
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"slider\",\"text\":\"\",\"valueChange\":255}]}",
1028
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"slider\",\"text\":\"\",\"valueChange\":0,\"change\":[1,0]}]}",
1029
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"slider\",\"text\":\"\"},{\"end\":2,\"kind\":\"text\",\"text\":\"child\"}]}",
1030
+ }) |source| try std.testing.expectError(error.InvalidView, decode(&ui, source));
1031
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, tag| {
1032
+ if (comptime @typeInfo(field.type) != .float) try std.testing.expectError(error.InvalidView, valueEvent(tag));
1033
+ }
1034
+ } else return error.SkipZigTest;
1035
+ }
1036
+
1037
+ test "compiled splits reject malformed panes and resize channels" {
1038
+ if (comptime enabled) {
1039
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1040
+ defer arena.deinit();
1041
+ var ui = Ui.init(arena.allocator());
1042
+ for ([_][]const u8{
1043
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"button\",\"text\":\"\",\"resize\":0}]}",
1044
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"split\",\"text\":\"\"}]}",
1045
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"split\",\"text\":\"\"},{\"end\":2,\"kind\":\"column\",\"text\":\"\"}]}",
1046
+ "{\"format\":2,\"nodes\":[{\"end\":3,\"kind\":\"split\",\"text\":\"\",\"resize\":255},{\"end\":2,\"kind\":\"column\",\"text\":\"\"},{\"end\":3,\"kind\":\"column\",\"text\":\"\"}]}",
1047
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"column\",\"text\":\"\",\"minWidth\":-1}]}",
1048
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"column\",\"text\":\"\",\"resizeDuration\":180}]}",
1049
+ }) |source| try std.testing.expectError(error.InvalidView, decode(&ui, source));
1050
+ } else return error.SkipZigTest;
1051
+ }
1052
+
1053
+ test "compiled view strings belong to the native tree arena" {
1054
+ if (comptime enabled) {
1055
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1056
+ defer arena.deinit();
1057
+ var ui = Ui.init(arena.allocator());
1058
+ const source = "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"button\",\"text\":\"café\",\"command\":\"run.café\",\"key\":\"label\"}]}";
1059
+ const bytes = try std.testing.allocator.dupe(u8, source);
1060
+ defer std.testing.allocator.free(bytes);
1061
+ const result = try decode(&ui, bytes);
1062
+ @memset(bytes, 0);
1063
+ try std.testing.expectEqualStrings("café", result.widget.text);
1064
+ try std.testing.expectEqualStrings("label", result.key.?.str);
1065
+ try std.testing.expectEqualStrings("run.café", result.widget.command);
1066
+ } else return error.SkipZigTest;
1067
+ }
1068
+
1069
+ test "compiled context menus reject malformed rows and preserve owned dispatch" {
1070
+ try testContextMenuRecords();
1071
+ }
1072
+
1073
+ pub fn testContextMenuRecords() !void {
1074
+ if (comptime enabled) {
1075
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1076
+ defer arena.deinit();
1077
+ var ui = Ui.init(arena.allocator());
1078
+ for ([_][]const u8{
1079
+ "{\"label\":\"\",\"enabled\":true,\"separator\":false,\"press\":[1,0]}",
1080
+ "{\"label\":\"Missing handler\",\"enabled\":true,\"separator\":false}",
1081
+ "{\"label\":\"Bad handler\",\"enabled\":true,\"separator\":false,\"press\":[1,255]}",
1082
+ "{\"label\":\"Separator text\",\"enabled\":true,\"separator\":true}",
1083
+ "{\"label\":\"\",\"enabled\":true,\"separator\":true,\"press\":[1,0]}",
1084
+ }) |row| {
1085
+ const source = try std.fmt.allocPrint(ui.arena, "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"list_item\",\"text\":\"\",\"contextMenu\":[{s}]}}]}}", .{row});
1086
+ try std.testing.expectError(error.InvalidView, decode(&ui, source));
1087
+ }
1088
+ const oversized = try ui.arena.alloc(ContextMenuRecord, 33);
1089
+ @memset(oversized, .{ .label = "", .enabled = true, .separator = true });
1090
+ const records = [_]Record{.{ .end = 1, .kind = .list_item, .text = "", .contextMenu = oversized }};
1091
+ try std.testing.expectError(error.InvalidView, node(&ui, &records, 0, 1, 0, &.{}, null));
1092
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, tag| {
1093
+ if (comptime field.type == void) {
1094
+ const source = try std.fmt.allocPrint(std.testing.allocator, "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"list_item\",\"text\":\"Task\",\"contextMenu\":[{{\"label\":\"Café\",\"press\":[1,{d}],\"enabled\":false,\"separator\":false}},{{\"label\":\"\",\"enabled\":true,\"separator\":true}}]}}]}}", .{tag});
1095
+ defer std.testing.allocator.free(source);
1096
+ const result = try decode(&ui, source);
1097
+ @memset(source, 0);
1098
+ const items = result.context_menu;
1099
+ try std.testing.expectEqual(@as(usize, 2), items.len);
1100
+ try std.testing.expectEqualStrings("Café", items[0].label);
1101
+ try std.testing.expectEqual(@as(core.Msg, @unionInit(core.Msg, field.name, {})), items[0].msg.?);
1102
+ try std.testing.expect(!items[0].enabled);
1103
+ try std.testing.expect(items[1].separator and items[1].msg == null);
1104
+ break;
1105
+ }
1106
+ }
1107
+ } else return error.SkipZigTest;
1108
+ }
1109
+
1110
+ test "compiled paragraph spans share owned text and validate primitive fields" {
1111
+ if (comptime enabled) {
1112
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1113
+ defer arena.deinit();
1114
+ var ui = Ui.init(arena.allocator());
1115
+ const source = "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"café\",\"spanWeight\":\"bold\",\"spanColor\":\"text_muted\",\"spanScale\":0.9}]}";
1116
+ const bytes = try std.testing.allocator.dupe(u8, source);
1117
+ defer std.testing.allocator.free(bytes);
1118
+ const result = try decode(&ui, bytes);
1119
+ @memset(bytes, 0);
1120
+ try std.testing.expectEqualStrings("café", result.widget.spans[0].text);
1121
+ try std.testing.expect(result.widget.spans[0].text.ptr == result.widget.text.ptr);
1122
+ try std.testing.expectEqual(.bold, result.widget.spans[0].weight);
1123
+ try std.testing.expectEqual(.text_muted, result.widget.spans[0].color.?);
1124
+ try std.testing.expectError(error.InvalidView, decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"badge\",\"text\":\"\",\"spanWeight\":\"bold\"}]}"));
1125
+ try std.testing.expectError(error.InvalidView, decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"spanScale\":-1}]}"));
1126
+ try std.testing.expectError(error.InvalidView, decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"row\",\"text\":\"\",\"listItemIndex\":1,\"listItemCount\":1}]}"));
1127
+ } else return error.SkipZigTest;
1128
+ }
1129
+
1130
+ test "compiled view negative integer slot keys match native iteration identities" {
1131
+ if (comptime enabled) {
1132
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1133
+ defer arena.deinit();
1134
+ var ui = Ui.init(arena.allocator());
1135
+ const result = try decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"keyInt\":-7,\"keySlot\":1}]}");
1136
+ const expected = try sdk.canvas.forSlotKey(ui.arena, .{ .int = @bitCast(@as(i64, -7)) }, 1);
1137
+ try std.testing.expectEqualStrings(expected.str, result.key.?.str);
1138
+ } else return error.SkipZigTest;
1139
+ }
1140
+
1141
+ test "compiled view message bytes belong to the native tree arena" {
1142
+ if (comptime enabled) {
1143
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, tag| {
1144
+ if (comptime field.type == []const u8) {
1145
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1146
+ defer arena.deinit();
1147
+ var ui = Ui.init(arena.allocator());
1148
+ var envelope = [_]u8{ 1, tag, 5, 0, 0, 0, 'c', 'a', 'f', 0xc3, 0xa9 };
1149
+ const msg = try event(&ui, &envelope);
1150
+ @memset(&envelope, 0);
1151
+ try std.testing.expectEqualStrings("café", @field(msg, field.name));
1152
+ return;
1153
+ }
1154
+ }
1155
+ }
1156
+ return error.SkipZigTest;
1157
+ }
1158
+
1159
+ test "compiled view refuses bad versions, spans, kinds and geometry" {
1160
+ if (comptime enabled) {
1161
+ const cases = [_][]const u8{
1162
+ "{\"format\":1,\"nodes\":[]}",
1163
+ "{\"format\":2,\"nodes\":[]}",
1164
+ "{\"format\":2,\"nodes\":[{\"end\":0,\"kind\":\"text\",\"text\":\"\"}]}",
1165
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"column\",\"text\":\"\"},{\"end\":3,\"kind\":\"text\",\"text\":\"\"}]}",
1166
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"text\",\"text\":\"\"},{\"end\":2,\"kind\":\"text\",\"text\":\"\"}]}",
1167
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"unknown\",\"text\":\"\"}]}",
1168
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"gap\":-1}]}",
1169
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"grow\":1e300}]}",
1170
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"press\":[0,0]}]}",
1171
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"input\":0}]}",
1172
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"input\",\"text\":\"\",\"input\":255}]}",
1173
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"input\",\"text\":\"\",\"submit\":[0,0]}]}",
1174
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"panel\",\"text\":\"\",\"wrap\":true}]}",
1175
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"list_item\",\"text\":\"Mixed\"},{\"end\":2,\"kind\":\"text\",\"text\":\"child\"}]}",
1176
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"role\":\"treeitem\"}]}",
1177
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"list\",\"text\":\"\",\"role\":\"tree\"}]}",
1178
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"list_item\",\"text\":\"\",\"toggle\":[0,0]}]}",
1179
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"toggle_group\",\"text\":\"\",\"toggle\":[0,0]}]}",
1180
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"toggle_button\",\"text\":\"\",\"change\":[1,0]}]}",
1181
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"toggle_button\",\"text\":\"Mixed\"},{\"end\":2,\"kind\":\"text\",\"text\":\"child\"}]}",
1182
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"checkbox\",\"text\":\"Setting\",\"press\":[0,0]}]}",
1183
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"checkbox\",\"text\":\"Setting\",\"change\":[1,0]}]}",
1184
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"switch_control\",\"text\":\"Setting\",\"press\":[0,0]}]}",
1185
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"switch_control\",\"text\":\"Setting\",\"change\":[1,0]}]}",
1186
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"toggle\",\"text\":\"Setting\",\"press\":[0,0]}]}",
1187
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"toggle\",\"text\":\"Setting\",\"change\":[1,0]}]}",
1188
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"accordion\",\"text\":\"Section\",\"press\":[0,0]}]}",
1189
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"accordion\",\"text\":\"Section\",\"change\":[1,0]}]}",
1190
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"accordion\",\"text\":\"Section\",\"placeholder\":\"Help\"}]}",
1191
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"button\",\"text\":\"\",\"dismiss\":[1,0]}]}",
1192
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"panel\",\"text\":\"\",\"anchor\":\"below\"}]}",
1193
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"dropdown_menu\",\"text\":\"\",\"anchorAlignment\":\"stretch\"}]}",
1194
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"dropdown_menu\",\"text\":\"\",\"anchorOffset\":1e300}]}",
1195
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"menu_item\",\"text\":\"Mixed\"},{\"end\":2,\"kind\":\"text\",\"text\":\"child\"}]}",
1196
+ };
1197
+ for (cases) |source| {
1198
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1199
+ defer arena.deinit();
1200
+ var ui = Ui.init(arena.allocator());
1201
+ if (decode(&ui, source)) |_| return error.TestExpectedError else |_| {}
1202
+ }
1203
+ } else return error.SkipZigTest;
1204
+ }
1205
+
1206
+ test "compiled resizable panels preserve initial minimum sizing and reject flow gap" {
1207
+ if (comptime enabled) {
1208
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1209
+ defer arena.deinit();
1210
+ var ui = Ui.init(arena.allocator());
1211
+ const source = "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"resizable\",\"text\":\"\",\"width\":260,\"height\":112,\"minWidth\":100},{\"end\":2,\"kind\":\"text\",\"text\":\"Research\"}]}";
1212
+ const result = try decode(&ui, source);
1213
+ try std.testing.expectEqual(sdk.canvas.WidgetKind.resizable, result.widget.kind);
1214
+ try std.testing.expectEqual(@as(f32, 260), result.widget.layout.min_size.width);
1215
+ try std.testing.expectEqual(@as(f32, 0), result.widget.layout.max_size.width);
1216
+ try std.testing.expect(result.widget.interaction_policy != null);
1217
+ for ([_][]const u8{
1218
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"resizable\",\"text\":\"\",\"gap\":8}]}",
1219
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"resizable\",\"text\":\"\",\"resize\":0}]}",
1220
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1221
+ } else return error.SkipZigTest;
1222
+ }
1223
+
1224
+ test "compiled textareas preserve multiline Enter policy and text keyboard callback" {
1225
+ if (comptime enabled) {
1226
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1227
+ defer arena.deinit();
1228
+ var ui = Ui.init(arena.allocator());
1229
+ const result = try decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"Café\\nnotes\",\"placeholder\":\"Write\",\"width\":420,\"height\":180,\"submitOnEnter\":true}]}");
1230
+ try std.testing.expectEqual(sdk.canvas.WidgetKind.textarea, result.widget.kind);
1231
+ try std.testing.expectEqualStrings("Café\nnotes", result.widget.text);
1232
+ try std.testing.expect(result.widget.submit_on_enter);
1233
+ try std.testing.expect(result.widget.interaction_policy != null);
1234
+ for ([_][]const u8{
1235
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"input\",\"text\":\"\",\"submitOnEnter\":true}]}",
1236
+ "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"textarea\",\"text\":\"\"},{\"end\":2,\"kind\":\"text\",\"text\":\"child\"}]}",
1237
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1238
+ }
1239
+ }
1240
+
1241
+ test "compiled code records preserve owned monospace source and reject misplaced metadata" {
1242
+ if (comptime enabled) {
1243
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1244
+ defer arena.deinit();
1245
+ var ui = Ui.init(arena.allocator());
1246
+ const result = try decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"const café = 1;\\n\",\"codeLanguage\":\"typescript\",\"codeLineDigits\":1}]}");
1247
+ try std.testing.expect(result.widget.runtime_flags.code_editor);
1248
+ try std.testing.expect(result.widget.text_no_wrap and result.widget.layout.clip_content);
1249
+ try std.testing.expectEqual(sdk.canvas.code.Language.typescript, result.widget.code_language);
1250
+ try std.testing.expectEqual(@as(u8, 1), result.widget.code_line_number_digits);
1251
+ try std.testing.expect(result.widget.spans[0].monospace);
1252
+ try std.testing.expect(result.widget.spans[0].text.ptr == result.widget.text.ptr);
1253
+ try std.testing.expectEqualStrings("const café = 1;\n", result.widget.text);
1254
+ for ([_][]const u8{
1255
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":0}]}",
1256
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\"}]}",
1257
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLineDigits\":1}]}",
1258
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":6}]}",
1259
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":1,\"submitOnEnter\":true}]}",
1260
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1261
+ }
1262
+ }
1263
+
1264
+ test "compiled code diff ordinals preserve every mask word and reject invalid tree metadata" {
1265
+ if (comptime enabled) {
1266
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1267
+ defer arena.deinit();
1268
+ var ui = Ui.init(arena.allocator());
1269
+ const bytes = try std.testing.allocator.dupe(u8, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"café\\n日本\\n\",\"codeLanguage\":\"typescript\",\"codeLineDigits\":1,\"codeAddedLines\":[1,32,33,64,65,96,97,128],\"codeRemovedLines\":[2,31,34,63,66,95,98,127]}]}");
1270
+ defer std.testing.allocator.free(bytes);
1271
+ const result = try decode(&ui, bytes);
1272
+ @memset(bytes, 'x');
1273
+ var added: u128 = 0;
1274
+ var removed: u128 = 0;
1275
+ for ([_]u7{ 0, 31, 32, 63, 64, 95, 96, 127 }) |bit| added |= @as(u128, 1) << bit;
1276
+ for ([_]u7{ 1, 30, 33, 62, 65, 94, 97, 126 }) |bit| removed |= @as(u128, 1) << bit;
1277
+ try std.testing.expectEqualDeep(sdk.canvas.CodeDiffLines{ .added = added, .removed = removed }, result.widget.codeDiffLines().?);
1278
+ try std.testing.expectEqual(@as(u8, 1), result.widget.codeLineNumberDigits());
1279
+ try std.testing.expectEqualStrings("café\n日本\n", result.widget.text);
1280
+ const next = try decode(&ui, "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"other\",\"codeLanguage\":\"python\",\"codeLineDigits\":0,\"codeAddedLines\":[],\"codeRemovedLines\":[]}]}");
1281
+ try std.testing.expect(next.widget.codeDiffLines() == null);
1282
+ try std.testing.expectEqualDeep(sdk.canvas.CodeDiffLines{ .added = added, .removed = removed }, result.widget.codeDiffLines().?);
1283
+ for ([_][]const u8{
1284
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeAddedLines\":[1],\"codeRemovedLines\":[]}]}",
1285
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":0,\"codeAddedLines\":[1]}]}",
1286
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":0,\"codeAddedLines\":[0],\"codeRemovedLines\":[]}]}",
1287
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":0,\"codeAddedLines\":[129],\"codeRemovedLines\":[]}]}",
1288
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":0,\"codeAddedLines\":[1,1],\"codeRemovedLines\":[]}]}",
1289
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"textarea\",\"text\":\"\",\"codeLanguage\":\"plain\",\"codeLineDigits\":0,\"codeAddedLines\":[128],\"codeRemovedLines\":[128]}]}",
1290
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1291
+ try std.testing.expectError(error.InvalidView, codeLineMask(&([_]u8{1} ** 129)));
1292
+ }
1293
+ }
1294
+
1295
+ /// Reference markup in these checks binds nothing, so it compiles against an
1296
+ /// empty model rather than the app's source model.
1297
+ const BindingFreeModel = struct {};
1298
+
1299
+ // The native construction oracle supplies authored data. Compiled views also
1300
+ // attach the appearance owner to every node; compare that callback explicitly
1301
+ // instead of erasing it from the decoded tree.
1302
+ fn withCompiledAppearance(reference: Ui.Node) Ui.Node {
1303
+ var result = reference;
1304
+ if (comptime @hasDecl(core, "nativeWindowPolicy")) result.widget.appearance_policy = core.nativeWindowPolicy;
1305
+ for (@constCast(result.nodes)) |*child| child.* = withCompiledAppearance(child.*);
1306
+ return result;
1307
+ }
1308
+
1309
+ test "compiled tooltip records match native anchored and static primitives" {
1310
+ if (comptime enabled) {
1311
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1312
+ defer arena.deinit();
1313
+ var ui = Ui.init(arena.allocator());
1314
+ const source = "{\"format\":2,\"nodes\":[{\"end\":4,\"kind\":\"stack\",\"text\":\"\"},{\"end\":2,\"kind\":\"button\",\"text\":\"Run\"},{\"end\":3,\"kind\":\"tooltip\",\"text\":\"Run café\",\"anchor\":\"above\",\"anchorAlignment\":\"end\",\"anchorOffset\":8,\"tooltipDelay\":250},{\"end\":4,\"kind\":\"tooltip\",\"text\":\"Static hint\"}]}";
1315
+ const bytes = try std.testing.allocator.dupe(u8, source);
1316
+ defer std.testing.allocator.free(bytes);
1317
+ var actual = try decode(&ui, bytes);
1318
+ @memset(bytes, 0);
1319
+ var reference_ui = Ui.init(arena.allocator());
1320
+ const Reference = sdk.canvas.CompiledMarkupView(BindingFreeModel, core.Msg, "<stack><button>Run</button><tooltip anchor=\"above\" anchor-alignment=\"end\" anchor-offset=\"8\" tooltip-delay=\"250\">Run café</tooltip><tooltip>Static hint</tooltip></stack>");
1321
+ const model: BindingFreeModel = .{};
1322
+ const expected = withCompiledAppearance(Reference.build(&reference_ui, &model));
1323
+ // The runtime-only callback differs by frontend; every authored
1324
+ // primitive field, child, owned string and anchor must match.
1325
+ actual.widget.interaction_policy = null;
1326
+ for (@constCast(actual.nodes)) |*child| child.widget.interaction_policy = null;
1327
+ const actual_tree = try ui.finalize(actual);
1328
+ const expected_tree = try reference_ui.finalize(expected);
1329
+ try std.testing.expectEqualDeep(expected_tree.root, actual_tree.root);
1330
+ try std.testing.expectEqualDeep(expected_tree.handlers, actual_tree.handlers);
1331
+ for ([_][]const u8{
1332
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"button\",\"text\":\"\",\"tooltipDelay\":0}]}",
1333
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"tooltip\",\"text\":\"\",\"tooltipDelay\":0}]}",
1334
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"tooltip\",\"text\":\"\",\"anchor\":\"above\",\"tooltipDelay\":-1}]}",
1335
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1336
+ } else return error.SkipZigTest;
1337
+ }
1338
+
1339
+ test "compiled modal records preserve caption constraints children and dismiss ownership" {
1340
+ try testModalRecords();
1341
+ }
1342
+
1343
+ pub fn testModalRecords() !void {
1344
+ if (comptime enabled) {
1345
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1346
+ defer arena.deinit();
1347
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, tag| {
1348
+ if (comptime field.type == void) {
1349
+ inline for (.{ sdk.canvas.WidgetKind.dialog, sdk.canvas.WidgetKind.drawer, sdk.canvas.WidgetKind.sheet }) |kind| {
1350
+ var ui = Ui.init(arena.allocator());
1351
+ const source = try std.fmt.allocPrint(
1352
+ ui.arena,
1353
+ "{{\"format\":2,\"nodes\":[{{\"end\":2,\"kind\":\"{s}\",\"text\":\"Café\",\"minWidth\":120,\"maxWidth\":420,\"height\":220,\"dismiss\":[1,{d}]}},{{\"end\":2,\"kind\":\"text\",\"text\":\"Owned content\"}}]}}",
1354
+ .{ @tagName(kind), tag },
1355
+ );
1356
+ const bytes = try ui.arena.dupe(u8, source);
1357
+ const actual = try decode(&ui, bytes);
1358
+ @memset(bytes, 0);
1359
+ var expected_ui = Ui.init(arena.allocator());
1360
+ var child = expected_ui.text(.{}, "Owned content");
1361
+ if (comptime @hasDecl(core, "nativeTextPolicy")) child.widget.interaction_policy = core.nativeTextPolicy;
1362
+ var expected = expected_ui.el(kind, .{ .text = "Café", .min_width = 120, .max_width = 420, .height = 220, .on_dismiss = @unionInit(core.Msg, field.name, {}) }, .{child});
1363
+ if (comptime @hasDecl(core, "nativeTextPolicy")) expected.widget.interaction_policy = core.nativeTextPolicy;
1364
+ expected = withCompiledAppearance(expected);
1365
+ try std.testing.expectEqualDeep(expected, actual);
1366
+ }
1367
+ break;
1368
+ }
1369
+ }
1370
+ var ui = Ui.init(arena.allocator());
1371
+ for ([_][]const u8{
1372
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"dialog\",\"text\":\"\",\"maxWidth\":-1}]}",
1373
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"sheet\",\"text\":\"\",\"dismiss\":[1,255]}]}",
1374
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"button\",\"text\":\"\",\"dismiss\":[1,0]}]}",
1375
+ }) |source| try std.testing.expectError(error.InvalidView, decode(&ui, source));
1376
+ } else return error.SkipZigTest;
1377
+ }
1378
+
1379
+ pub fn testGridRecords() !void {
1380
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1381
+ defer arena.deinit();
1382
+ var ui = Ui.init(arena.allocator());
1383
+ const bytes = try std.testing.allocator.dupe(u8, "{\"format\":2,\"nodes\":[{\"end\":2,\"kind\":\"grid\",\"text\":\"\",\"columns\":3,\"virtualized\":true,\"virtualItemExtent\":40,\"value\":24},{\"end\":2,\"kind\":\"text\",\"text\":\"Café\"}]}");
1384
+ defer std.testing.allocator.free(bytes);
1385
+ var actual = try decode(&ui, bytes);
1386
+ @memset(bytes, 'x');
1387
+ var reference_ui = Ui.init(arena.allocator());
1388
+ const expected = withCompiledAppearance(reference_ui.el(.grid, .{ .columns = 3, .virtualized = true, .virtual_item_extent = 40, .value = 24 }, .{reference_ui.text(.{}, "Café")}));
1389
+ actual.widget.interaction_policy = null;
1390
+ for (@constCast(actual.nodes)) |*child| child.widget.interaction_policy = null;
1391
+ const actual_tree = try ui.finalize(actual);
1392
+ const expected_tree = try reference_ui.finalize(expected);
1393
+ try std.testing.expectEqualDeep(expected_tree.root, actual_tree.root);
1394
+ try std.testing.expectEqualDeep(expected_tree.handlers, actual_tree.handlers);
1395
+ try std.testing.expectEqualStrings("Café", actual_tree.root.children[0].text);
1396
+ for ([_][]const u8{
1397
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"row\",\"text\":\"\",\"columns\":2}]}",
1398
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"virtualized\":true}]}",
1399
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"grid\",\"text\":\"\",\"virtualItemExtent\":-1}]}",
1400
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"grid\",\"text\":\"\",\"columns\":9007199254740992}]}",
1401
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1402
+ }
1403
+
1404
+ pub fn testContentSurfaceRecords() !void {
1405
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1406
+ defer arena.deinit();
1407
+ inline for (.{ sdk.canvas.WidgetKind.card, sdk.canvas.WidgetKind.alert }) |kind| {
1408
+ for ([_]?f32{ null, 0, 12 }) |padding| {
1409
+ for ([_]sdk.canvas.WidgetVariant{ .default, .destructive }) |variant| {
1410
+ var ui = Ui.init(arena.allocator());
1411
+ const spacing = if (padding) |value| try std.fmt.allocPrint(ui.arena, ",\"padding\":{d}", .{value}) else "";
1412
+ const source = try std.fmt.allocPrint(ui.arena, "{{\"format\":2,\"nodes\":[{{\"end\":2,\"kind\":\"{s}\",\"text\":\"Café\",\"width\":360,\"minWidth\":120,\"maxWidth\":420,\"variant\":\"{s}\"{s}}},{{\"end\":2,\"kind\":\"text\",\"text\":\"Owned content\"}}]}}", .{ @tagName(kind), @tagName(variant), spacing });
1413
+ const bytes = try ui.arena.dupe(u8, source);
1414
+ const actual = try decode(&ui, bytes);
1415
+ @memset(bytes, 0);
1416
+ var reference = Ui.init(arena.allocator());
1417
+ var child = reference.text(.{}, "Owned content");
1418
+ if (comptime @hasDecl(core, "nativeTextPolicy")) child.widget.interaction_policy = core.nativeTextPolicy;
1419
+ var expected = reference.el(kind, .{ .text = "Café", .width = 360, .min_width = 120, .max_width = 420, .padding = padding, .variant = variant }, .{child});
1420
+ if (comptime @hasDecl(core, "nativeTextPolicy")) expected.widget.interaction_policy = core.nativeTextPolicy;
1421
+ expected = withCompiledAppearance(expected);
1422
+ try std.testing.expectEqualDeep(expected, actual);
1423
+ const actual_tree = try ui.finalize(actual);
1424
+ const reference_tree = try reference.finalize(expected);
1425
+ try std.testing.expectEqualDeep(reference_tree.root, actual_tree.root);
1426
+ try std.testing.expectEqualDeep(reference_tree.handlers, actual_tree.handlers);
1427
+ }
1428
+ }
1429
+ }
1430
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, tag| {
1431
+ if (field.type == void) {
1432
+ try testAuthoredPrimitiveRecords(@unionInit(core.Msg, field.name, {}), @intCast(tag));
1433
+ return;
1434
+ }
1435
+ }
1436
+ }
1437
+
1438
+ fn testAuthoredPrimitiveRecords(press: core.Msg, tag: u8) !void {
1439
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1440
+ defer arena.deinit();
1441
+ inline for (.{ sdk.canvas.WidgetKind.bubble, .table, .data_row, .data_cell, .progress, .skeleton, .spinner, .text, .combobox }) |kind| {
1442
+ var ui = Ui.init(arena.allocator());
1443
+ const quiet = kind == .text or kind == .combobox or kind == .data_cell or kind == .bubble or kind == .progress;
1444
+ const record = try std.fmt.allocPrint(ui.arena, "{{\"end\":END,\"kind\":\"{s}\",\"text\":\"\",\"textBytes\":[65,0,255],\"iconBytes\":[112,0,255],\"iconPlacement\":\"trailing\",\"textAlignment\":\"end\",\"selected\":true,\"radius\":\"xl\",\"accent\":\"success\",\"accentForeground\":\"success_text\",\"quietHover\":{s},\"press\":[1,{d}]}}", .{ @tagName(kind), if (quiet) "true" else "false", tag });
1445
+ const wrapper = switch (kind) {
1446
+ .data_row => "{\"end\":2,\"kind\":\"table\",\"text\":\"\"},",
1447
+ .data_cell => "{\"end\":3,\"kind\":\"table\",\"text\":\"\"},{\"end\":3,\"kind\":\"data_row\",\"text\":\"\"},",
1448
+ else => "",
1449
+ };
1450
+ const count: usize = if (kind == .data_cell) 3 else if (kind == .data_row) 2 else 1;
1451
+ const ordinal = try std.fmt.allocPrint(ui.arena, "{d}", .{count});
1452
+ const resolved = try std.mem.replaceOwned(u8, ui.arena, record, "END", ordinal);
1453
+ const bytes = try std.fmt.allocPrint(ui.arena, "{{\"format\":2,\"nodes\":[{s}{s}]}}", .{ wrapper, resolved });
1454
+ const actual = try decode(&ui, bytes);
1455
+ @memset(bytes, 0);
1456
+ var reference = Ui.init(arena.allocator());
1457
+ var expected = stampCompound(reference.el(kind, .{
1458
+ .text = "A\x00\xff",
1459
+ .icon = "p\x00\xff",
1460
+ .icon_placement = .trailing,
1461
+ .text_alignment = .end,
1462
+ .selected = true,
1463
+ .style_tokens = .{ .radius = .xl, .accent = .success, .accent_foreground = .success_text },
1464
+ .style = .{ .quiet_hover = quiet },
1465
+ .on_press = press,
1466
+ }, .{}));
1467
+ if (kind == .data_cell) expected = stampCompound(reference.el(.data_row, .{}, .{expected}));
1468
+ if (kind == .data_row or kind == .data_cell) expected = stampCompound(reference.el(.table, .{}, .{expected}));
1469
+ try std.testing.expectEqualDeep(expected, actual);
1470
+ const actual_tree = try ui.finalize(actual);
1471
+ const reference_tree = try reference.finalize(expected);
1472
+ try std.testing.expectEqualDeep(reference_tree.root, actual_tree.root);
1473
+ try std.testing.expectEqualDeep(reference_tree.handlers, actual_tree.handlers);
1474
+ }
1475
+ var ui = Ui.init(arena.allocator());
1476
+ for ([_][]const u8{
1477
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"data_row\",\"text\":\"\"}]}",
1478
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"data_cell\",\"text\":\"\"}]}",
1479
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"spinner\",\"text\":\"\",\"quietHover\":true}]}",
1480
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1481
+ }
1482
+
1483
+ pub fn testInlineParagraphRecords() !void {
1484
+ if (comptime !enabled) return error.SkipZigTest;
1485
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1486
+ defer arena.deinit();
1487
+ var ui = Ui.init(arena.allocator());
1488
+ const source =
1489
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","textAlignment":"end","size":"display","key":"readout","label":"Result","spans":[{"text":"Value"},{"text":" "},{"text":"café","weight":"medium","monospace":true,"italic":true,"underline":true,"scale":1.5,"color":"accent"},{"text":"."}]}]}
1490
+ ;
1491
+ const bytes = try std.testing.allocator.dupe(u8, source);
1492
+ defer std.testing.allocator.free(bytes);
1493
+ var actual = try decode(&ui, bytes);
1494
+ @memset(bytes, 0);
1495
+ var reference_ui = Ui.init(arena.allocator());
1496
+ const Reference = sdk.canvas.CompiledMarkupView(BindingFreeModel, core.Msg,
1497
+ \\<text text-alignment="end" size="display" key="readout" label="Result">Value <span weight="medium" mono="true" italic="true" underline="true" scale="1.5" foreground="accent">café</span>.</text>
1498
+ );
1499
+ const model: BindingFreeModel = .{};
1500
+ const expected = withCompiledAppearance(Reference.build(&reference_ui, &model));
1501
+ actual.widget.interaction_policy = null;
1502
+ const actual_tree = try ui.finalize(actual);
1503
+ const expected_tree = try reference_ui.finalize(expected);
1504
+ try std.testing.expectEqualDeep(expected_tree.root, actual_tree.root);
1505
+ try std.testing.expectEqualDeep(expected_tree.handlers, actual_tree.handlers);
1506
+ try std.testing.expectEqualStrings("Value café.", actual.widget.text);
1507
+ var offset: usize = 0;
1508
+ for (actual.widget.spans) |span| {
1509
+ try std.testing.expect(span.text.ptr == actual.widget.text.ptr + offset);
1510
+ offset += span.text.len;
1511
+ }
1512
+ for ([_][]const u8{
1513
+ \\{"format":2,"nodes":[{"end":1,"kind":"button","text":"","spans":[{"text":"x"}]}]}
1514
+ ,
1515
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"hidden","spans":[{"text":"x"}]}]}
1516
+ ,
1517
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","spans":[{"text":"x","scale":0}]}]}
1518
+ ,
1519
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","spans":[{"text":"x","scale":-1}]}]}
1520
+ ,
1521
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","spans":[{"text":"x","weight":"heavy"}]}]}
1522
+ ,
1523
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","spans":[{"text":"x","color":"invalid"}]}]}
1524
+ ,
1525
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","spans":[{"text":"x","extra":true}]}]}
1526
+ ,
1527
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","spanWeight":"bold","spans":[{"text":"x"}]}]}
1528
+ ,
1529
+ \\{"format":2,"nodes":[{"end":1,"kind":"text","text":"","wrap":true,"spans":[{"text":"x"}]}]}
1530
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1531
+ }
1532
+
1533
+ test "compiled byte text preserves owned labels placeholders spans and menus and rejects nonbytes" {
1534
+ try testByteTextRecords();
1535
+ }
1536
+
1537
+ pub fn testByteTextRecords() !void {
1538
+ if (comptime enabled) {
1539
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1540
+ defer arena.deinit();
1541
+ var ui = Ui.init(arena.allocator());
1542
+ const source =
1543
+ \\{"format":2,"nodes":[{"end":4,"kind":"column","text":""},
1544
+ \\{"end":2,"kind":"button","text":"replacement","textBytes":[255,0,120],"icon":"replacement","iconBytes":[112,0,255],"label":"replacement","labelBytes":[195,0],"contextMenu":[{"label":"replacement","labelBytes":[254,0],"press":[1,MENU_TAG],"enabled":true,"separator":false}]},
1545
+ \\{"end":3,"kind":"textarea","text":"replacement","textBytes":[255],"placeholder":"replacement","placeholderBytes":[128,0]},
1546
+ \\{"end":4,"kind":"text","text":"","spans":[{"text":"replacement","textBytes":[255,0]}]}]}
1547
+ ;
1548
+ const tag = comptime blk: {
1549
+ for (@typeInfo(core.Msg).@"union".fields, 0..) |field, index| if (field.type == void) break :blk index;
1550
+ break :blk 255;
1551
+ };
1552
+ var tag_buffer: [3]u8 = undefined;
1553
+ const bytes = try std.mem.replaceOwned(u8, std.testing.allocator, source, "MENU_TAG", try std.fmt.bufPrint(&tag_buffer, "{d}", .{tag}));
1554
+ defer std.testing.allocator.free(bytes);
1555
+ const result = try ui.finalize(try decode(&ui, bytes));
1556
+ @memset(bytes, 0);
1557
+ try std.testing.expectEqualStrings("\xff\x00x", result.root.children[0].text);
1558
+ try std.testing.expectEqualStrings("p\x00\xff", result.root.children[0].icon);
1559
+ try std.testing.expectEqualStrings("\xc3\x00", result.root.children[0].semantics.label);
1560
+ try std.testing.expectEqualStrings("\xfe\x00", result.root.children[0].context_menu[0].label);
1561
+ try std.testing.expectEqualStrings("\xff", result.root.children[1].text);
1562
+ try std.testing.expectEqualStrings("\x80\x00", result.root.children[1].placeholder);
1563
+ try std.testing.expectEqualStrings("\xff\x00", result.root.children[2].text);
1564
+ try std.testing.expectEqualStrings("\xff\x00", result.root.children[2].spans[0].text);
1565
+ for ([_][]const u8{ "[256]", "[-1]", "[1.5]", "[true]", "[null]", "\"bad\"" }) |invalid| {
1566
+ for ([_][]const u8{ "textBytes", "iconBytes" }) |field| {
1567
+ const malformed = try std.fmt.allocPrint(ui.arena, "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"{s}\":{s}}}]}}", .{ field, invalid });
1568
+ try std.testing.expectError(error.InvalidView, decode(&ui, malformed));
1569
+ }
1570
+ }
1571
+ } else return error.SkipZigTest;
1572
+ }
1573
+
1574
+ test "compiled byte keys own raw empty local and global identities and reject conflicting encodings" {
1575
+ try testByteKeyRecords();
1576
+ }
1577
+
1578
+ pub fn testByteKeyRecords() !void {
1579
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1580
+ defer arena.deinit();
1581
+ var ui = Ui.init(arena.allocator());
1582
+ const bytes = try ui.arena.dupe(u8,
1583
+ \\{"format":2,"nodes":[{"end":3,"kind":"column","text":"","keyBytes":[255,0,192,175]},
1584
+ \\{"end":2,"kind":"text","keyBytes":[],"text":"empty"},
1585
+ \\{"end":3,"kind":"text","globalKeyBytes":[128,0],"text":"global"}]}
1586
+ );
1587
+ const result = try decode(&ui, bytes);
1588
+ @memset(bytes, 0xA5);
1589
+ try std.testing.expectEqualStrings("\xff\x00\xc0\xaf", result.key.?.str);
1590
+ try std.testing.expectEqualStrings("", result.nodes[0].key.?.str);
1591
+ try std.testing.expectEqualStrings("\x80\x00", result.nodes[1].global_key.?.str);
1592
+ for ([_][]const u8{
1593
+ "\"keyBytes\":[],\"key\":\"ignored\"", "\"keyBytes\":[],\"keyInt\":1",
1594
+ "\"globalKeyBytes\":[],\"globalKey\":\"ignored\"", "\"globalKeyBytes\":[],\"globalKeyInt\":1",
1595
+ "\"keyBytes\":[256]", "\"globalKeyBytes\":[-1]",
1596
+ "\"keyBytes\":[1.5]", "\"globalKeyBytes\":[true]",
1597
+ }) |fields| {
1598
+ const malformed = try std.fmt.allocPrint(ui.arena, "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"text\",\"text\":\"\",{s}}}]}}", .{fields});
1599
+ try std.testing.expectError(error.InvalidView, decode(&ui, malformed));
1600
+ }
1601
+ }
1602
+
1603
+ pub fn testTerminalRecords() !void {
1604
+ if (comptime enabled) {
1605
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1606
+ defer arena.deinit();
1607
+ var ui = Ui.init(arena.allocator());
1608
+ for ([_][]const u8{
1609
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"text\",\"text\":\"\",\"ptyBytes\":[115]}]}",
1610
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"terminal\",\"text\":\"authored\"}]}",
1611
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"terminal\",\"text\":\"\",\"terminal\":255}]}",
1612
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"terminal\",\"text\":\"\",\"pty\":9007199254740992}]}",
1613
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"terminal\",\"text\":\"\",\"pty\":1,\"ptyBytes\":[115]}]}",
1614
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"terminal\",\"text\":\"\",\"ptyBytes\":[256]}]}",
1615
+ "{\"format\":2,\"nodes\":[{\"end\":1,\"kind\":\"terminal\",\"text\":\"\",\"scrollback\":4294967296}]}",
1616
+ }) |source| try std.testing.expectError(error.InvalidView, decode(&ui, source));
1617
+ inline for (@typeInfo(core.Msg).@"union".fields, 0..) |field, tag| {
1618
+ if (comptime !sdk.canvas.ui_markup_reflect.declaredTerminalStateRecord(field.type)) try std.testing.expectError(error.InvalidView, terminalEvent(tag));
1619
+ }
1620
+ } else return error.SkipZigTest;
1621
+ }
1622
+
1623
+ test "compiled terminal records reject misplaced malformed and incompatible ownership channels" {
1624
+ try testTerminalRecords();
1625
+ }
1626
+
1627
+ test "compiled grouped input records preserve complete owned primitive trees" {
1628
+ if (comptime enabled) {
1629
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1630
+ defer arena.deinit();
1631
+ var ui = Ui.init(arena.allocator());
1632
+ const bytes = try ui.arena.dupe(u8,
1633
+ \\{"format":2,"nodes":[{"end":4,"kind":"input_group","text":"","label":"Compose","width":360,"height":160},{"end":2,"kind":"textarea","text":"","textBytes":[67,0,255],"label":"Draft","borderColor":"accent"},{"end":4,"kind":"input_group_actions","text":"","gap":6},{"end":4,"kind":"button","text":"Send"}]}
1634
+ );
1635
+ const actual_node = try decode(&ui, bytes);
1636
+ @memset(bytes, 0);
1637
+ core.rt.frameReset();
1638
+ var reference = Ui.init(arena.allocator());
1639
+ const entry = reference.el(.textarea, .{ .text = "C\x00\xff", .semantics = .{ .label = "Draft" }, .style_tokens = .{ .border_color = .accent } }, .{});
1640
+ const expected_node = reference.inputGroup(.{ .width = 360, .height = 160, .semantics = .{ .label = "Compose" } }, entry, reference.inputGroupActions(.{}, .{reference.button(.{}, "Send")}));
1641
+ const expected_tree = try reference.finalize(compoundOwners(expected_node));
1642
+ const actual_tree = try ui.finalize(actual_node);
1643
+ try std.testing.expectEqualDeep(expected_tree.root, actual_tree.root);
1644
+ try std.testing.expectEqualDeep(expected_tree.handlers, actual_tree.handlers);
1645
+ } else return error.SkipZigTest;
1646
+ }
1647
+ fn compoundOwners(node_value: Ui.Node) Ui.Node {
1648
+ const result = stampCompound(node_value);
1649
+ for (@constCast(result.nodes)) |*child| child.* = compoundOwners(child.*);
1650
+ return result;
1651
+ }
1652
+
1653
+ test "compiled grouped input refuses orphan actions invalid order and excess children" {
1654
+ if (comptime enabled) {
1655
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1656
+ defer arena.deinit();
1657
+ var ui = Ui.init(arena.allocator());
1658
+ for ([_][]const u8{
1659
+ \\{"format":2,"nodes":[{"end":1,"kind":"input_group_actions","text":""}]}
1660
+ ,
1661
+ \\{"format":2,"nodes":[{"end":1,"kind":"input_group","text":""}]}
1662
+ ,
1663
+ \\{"format":2,"nodes":[{"end":2,"kind":"input_group","text":""},{"end":2,"kind":"button","text":""}]}
1664
+ ,
1665
+ \\{"format":2,"nodes":[{"end":3,"kind":"input_group","text":""},{"end":2,"kind":"textarea","text":""},{"end":3,"kind":"button","text":""}]}
1666
+ ,
1667
+ \\{"format":2,"nodes":[{"end":4,"kind":"input_group","text":""},{"end":2,"kind":"textarea","text":""},{"end":3,"kind":"textarea","text":""},{"end":4,"kind":"input_group_actions","text":""}]}
1668
+ ,
1669
+ }) |invalid| try std.testing.expectError(error.InvalidView, decode(&ui, invalid));
1670
+ } else return error.SkipZigTest;
1671
+ }
1672
+
1673
+ test "compiled app chart rejects misplaced metadata unbounded samples and invalid series records" {
1674
+ var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
1675
+ defer arena.deinit();
1676
+ for ([_][]const u8{
1677
+ "\"chartSeries\":[]", "\"chartXLabels\":[]", "\"chartYMin\":0", "\"chartYMax\":0",
1678
+ "\"chartGridLines\":0", "\"chartBaseline\":false", "\"chartYLabels\":false", "\"chartHoverDetails\":false",
1679
+ "\"chartStrokeWidth\":0",
1680
+ }) |fields| {
1681
+ var ui = Ui.init(arena.allocator());
1682
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"column\",\"text\":\"\",{s}}}]}}", .{fields});
1683
+ try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
1684
+ }
1685
+ for ([_][]const u8{
1686
+ "\"chartSeries\":[],\"chartXLabels\":[]",
1687
+ "\"chartSeries\":[{\"kind\":\"band\",\"values\":[],\"color\":\"accent\",\"fill\":false,\"label\":[]}],\"chartXLabels\":[]",
1688
+ "\"chartSeries\":[{\"kind\":\"bar\",\"values\":[null],\"color\":\"accent\",\"fill\":false,\"label\":[]}],\"chartXLabels\":[]",
1689
+ "\"chartSeries\":[{\"kind\":\"bar\",\"values\":[4294967296],\"color\":\"accent\",\"fill\":false,\"label\":[]}],\"chartXLabels\":[]",
1690
+ "\"chartSeries\":[{\"kind\":\"bar\",\"values\":[],\"color\":\"accent\",\"fill\":true,\"label\":[]}],\"chartXLabels\":[]",
1691
+ "\"chartSeries\":[{\"kind\":\"line\",\"values\":[],\"color\":\"accent\",\"fill\":false,\"label\":[256]}],\"chartXLabels\":[]",
1692
+ }) |fields| {
1693
+ var ui = Ui.init(arena.allocator());
1694
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"chart\",\"text\":\"\",{s}}}]}}", .{fields});
1695
+ try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
1696
+ }
1697
+ const values = try arena.allocator().alloc(u8, 2 * 257 - 1);
1698
+ for (values, 0..) |*byte, i| byte.* = if (i % 2 == 0) '0' else ',';
1699
+ var ui = Ui.init(arena.allocator());
1700
+ const bytes = try std.fmt.allocPrint(arena.allocator(), "{{\"format\":2,\"nodes\":[{{\"end\":1,\"kind\":\"chart\",\"text\":\"\",\"chartSeries\":[{{\"kind\":\"line\",\"values\":[{s}],\"color\":\"accent\",\"fill\":false,\"label\":[]}}],\"chartXLabels\":[]}}]}}", .{values});
1701
+ try std.testing.expectError(error.InvalidView, decode(&ui, bytes));
1702
+ }