@native-sdk/cli 0.0.0 → 0.4.0

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 (367) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/bin/native.js +156 -0
  5. package/build/app.zig +716 -0
  6. package/build.zig +2593 -0
  7. package/build.zig.zon +24 -0
  8. package/native-sdk.d.ts +615 -0
  9. package/package.json +44 -8
  10. package/skill-data/automation/SKILL.md +248 -0
  11. package/skill-data/core/SKILL.md +249 -0
  12. package/skill-data/core/references/app-model-runtime.md +141 -0
  13. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  14. package/skill-data/core/references/frontend-assets.md +124 -0
  15. package/skill-data/core/references/project-anatomy.md +108 -0
  16. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  17. package/skill-data/native-ui/SKILL.md +1080 -0
  18. package/skills/native-sdk/SKILL.md +33 -0
  19. package/src/app_runner/root.zig +740 -0
  20. package/src/assets/root.zig +20 -0
  21. package/src/automation/protocol.zig +285 -0
  22. package/src/automation/root.zig +13 -0
  23. package/src/automation/server.zig +506 -0
  24. package/src/automation/snapshot.zig +1202 -0
  25. package/src/automation/watcher.zig +165 -0
  26. package/src/bridge/root.zig +507 -0
  27. package/src/debug/root.zig +246 -0
  28. package/src/embed/app_exports.zig +11 -0
  29. package/src/embed/c_api.zig +793 -0
  30. package/src/embed/c_exports.zig +10 -0
  31. package/src/embed/chrome.zig +168 -0
  32. package/src/embed/conversions.zig +359 -0
  33. package/src/embed/host.zig +960 -0
  34. package/src/embed/root.zig +81 -0
  35. package/src/embed/tests.zig +2552 -0
  36. package/src/embed/types.zig +382 -0
  37. package/src/embed/ui_host.zig +289 -0
  38. package/src/extensions/root.zig +197 -0
  39. package/src/frontend/root.zig +48 -0
  40. package/src/js/root.zig +65 -0
  41. package/src/platform/android/NativeSdkActivity.java +1370 -0
  42. package/src/platform/android/android_host.c +696 -0
  43. package/src/platform/android/files.zig +9 -0
  44. package/src/platform/android/native_sdk_app.h +237 -0
  45. package/src/platform/ios/files.zig +8 -0
  46. package/src/platform/ios/native_sdk_app.h +289 -0
  47. package/src/platform/ios/uikit_host.m +2739 -0
  48. package/src/platform/linux/cef_host.cpp +599 -0
  49. package/src/platform/linux/gtk_host.c +5362 -0
  50. package/src/platform/linux/gtk_host.h +265 -0
  51. package/src/platform/linux/root.zig +1519 -0
  52. package/src/platform/macos/appkit_host.h +604 -0
  53. package/src/platform/macos/appkit_host.m +10392 -0
  54. package/src/platform/macos/automation_host.h +14 -0
  55. package/src/platform/macos/automation_host.m +12 -0
  56. package/src/platform/macos/cef_host.mm +3028 -0
  57. package/src/platform/macos/root.zig +2260 -0
  58. package/src/platform/null_platform.zig +2232 -0
  59. package/src/platform/null_platform_tests.zig +670 -0
  60. package/src/platform/policy_values.zig +14 -0
  61. package/src/platform/root.zig +181 -0
  62. package/src/platform/types.zig +2681 -0
  63. package/src/platform/windows/cef_host.cpp +3 -0
  64. package/src/platform/windows/root.zig +1521 -0
  65. package/src/platform/windows/webview2_host.cpp +6230 -0
  66. package/src/primitives/app_dirs/root.zig +440 -0
  67. package/src/primitives/app_manifest/root.zig +109 -0
  68. package/src/primitives/app_manifest/tests.zig +758 -0
  69. package/src/primitives/app_manifest/types.zig +587 -0
  70. package/src/primitives/app_manifest/validation.zig +828 -0
  71. package/src/primitives/assets/root.zig +520 -0
  72. package/src/primitives/canvas/a11y_audit.zig +390 -0
  73. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  74. package/src/primitives/canvas/app_icon.zig +1590 -0
  75. package/src/primitives/canvas/chart.zig +392 -0
  76. package/src/primitives/canvas/chart_tests.zig +751 -0
  77. package/src/primitives/canvas/commands.zig +446 -0
  78. package/src/primitives/canvas/drawing.zig +309 -0
  79. package/src/primitives/canvas/equality.zig +316 -0
  80. package/src/primitives/canvas/events.zig +696 -0
  81. package/src/primitives/canvas/font_coverage.zig +118 -0
  82. package/src/primitives/canvas/font_ttf.zig +510 -0
  83. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  84. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  85. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  86. package/src/primitives/canvas/frame.zig +679 -0
  87. package/src/primitives/canvas/frame_metrics.zig +462 -0
  88. package/src/primitives/canvas/gpu.zig +639 -0
  89. package/src/primitives/canvas/hash.zig +112 -0
  90. package/src/primitives/canvas/icons/alert.svg +1 -0
  91. package/src/primitives/canvas/icons/archive.svg +1 -0
  92. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  93. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  94. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  95. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  96. package/src/primitives/canvas/icons/check.svg +1 -0
  97. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  98. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  99. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  100. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  101. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  102. package/src/primitives/canvas/icons/clock.svg +1 -0
  103. package/src/primitives/canvas/icons/copy.svg +1 -0
  104. package/src/primitives/canvas/icons/download.svg +1 -0
  105. package/src/primitives/canvas/icons/edit.svg +1 -0
  106. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  107. package/src/primitives/canvas/icons/external-link.svg +1 -0
  108. package/src/primitives/canvas/icons/eye.svg +1 -0
  109. package/src/primitives/canvas/icons/file-text.svg +1 -0
  110. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  111. package/src/primitives/canvas/icons/folder.svg +1 -0
  112. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  113. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  114. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  115. package/src/primitives/canvas/icons/info.svg +1 -0
  116. package/src/primitives/canvas/icons/menu.svg +1 -0
  117. package/src/primitives/canvas/icons/missing.svg +1 -0
  118. package/src/primitives/canvas/icons/moon.svg +1 -0
  119. package/src/primitives/canvas/icons/music.svg +1 -0
  120. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  121. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  122. package/src/primitives/canvas/icons/pause.svg +1 -0
  123. package/src/primitives/canvas/icons/play.svg +1 -0
  124. package/src/primitives/canvas/icons/plus.svg +1 -0
  125. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  126. package/src/primitives/canvas/icons/repeat.svg +1 -0
  127. package/src/primitives/canvas/icons/save.svg +1 -0
  128. package/src/primitives/canvas/icons/search.svg +1 -0
  129. package/src/primitives/canvas/icons/send.svg +1 -0
  130. package/src/primitives/canvas/icons/settings.svg +1 -0
  131. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  132. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  133. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  134. package/src/primitives/canvas/icons/sun.svg +1 -0
  135. package/src/primitives/canvas/icons/terminal.svg +1 -0
  136. package/src/primitives/canvas/icons/trash.svg +1 -0
  137. package/src/primitives/canvas/icons/volume.svg +1 -0
  138. package/src/primitives/canvas/icons/wrench.svg +1 -0
  139. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  140. package/src/primitives/canvas/icons/x.svg +1 -0
  141. package/src/primitives/canvas/icons.zig +249 -0
  142. package/src/primitives/canvas/layout_audit.zig +744 -0
  143. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  144. package/src/primitives/canvas/markdown.zig +1031 -0
  145. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  146. package/src/primitives/canvas/markdown_tests.zig +538 -0
  147. package/src/primitives/canvas/plan_key_index.zig +120 -0
  148. package/src/primitives/canvas/png.zig +355 -0
  149. package/src/primitives/canvas/reference.zig +1457 -0
  150. package/src/primitives/canvas/reference_blur.zig +104 -0
  151. package/src/primitives/canvas/reference_memo.zig +322 -0
  152. package/src/primitives/canvas/reference_paths.zig +184 -0
  153. package/src/primitives/canvas/reference_tests.zig +1650 -0
  154. package/src/primitives/canvas/render.zig +747 -0
  155. package/src/primitives/canvas/render_effects.zig +255 -0
  156. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  157. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  158. package/src/primitives/canvas/render_images.zig +247 -0
  159. package/src/primitives/canvas/render_layers.zig +260 -0
  160. package/src/primitives/canvas/render_paths.zig +347 -0
  161. package/src/primitives/canvas/render_resources.zig +51 -0
  162. package/src/primitives/canvas/render_tests.zig +3186 -0
  163. package/src/primitives/canvas/root.zig +744 -0
  164. package/src/primitives/canvas/serialization.zig +1528 -0
  165. package/src/primitives/canvas/svg_icon.zig +636 -0
  166. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  167. package/src/primitives/canvas/test_support.zig +422 -0
  168. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  169. package/src/primitives/canvas/tests.zig +30 -0
  170. package/src/primitives/canvas/text.zig +82 -0
  171. package/src/primitives/canvas/text_atlas.zig +408 -0
  172. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  173. package/src/primitives/canvas/text_interaction.zig +615 -0
  174. package/src/primitives/canvas/text_layout.zig +1069 -0
  175. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  176. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  177. package/src/primitives/canvas/text_layout_types.zig +128 -0
  178. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  179. package/src/primitives/canvas/text_metrics.zig +309 -0
  180. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  181. package/src/primitives/canvas/text_span_tests.zig +608 -0
  182. package/src/primitives/canvas/text_spans.zig +1010 -0
  183. package/src/primitives/canvas/text_tests.zig +1949 -0
  184. package/src/primitives/canvas/themes/geist.zig +470 -0
  185. package/src/primitives/canvas/tokens.zig +1522 -0
  186. package/src/primitives/canvas/ui.zig +2686 -0
  187. package/src/primitives/canvas/ui_markup.zig +3791 -0
  188. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  189. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  190. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  191. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  192. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  193. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  194. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  195. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  196. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  197. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  198. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  199. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  200. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  201. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  202. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  203. package/src/primitives/canvas/ui_provenance.zig +96 -0
  204. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  205. package/src/primitives/canvas/ui_schema.zig +825 -0
  206. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  207. package/src/primitives/canvas/ui_tests.zig +1432 -0
  208. package/src/primitives/canvas/vector.zig +919 -0
  209. package/src/primitives/canvas/vector_tests.zig +547 -0
  210. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  211. package/src/primitives/canvas/virtual_extents.zig +540 -0
  212. package/src/primitives/canvas/widget_access.zig +208 -0
  213. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  214. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  215. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  216. package/src/primitives/canvas/widget_layout.zig +2055 -0
  217. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  218. package/src/primitives/canvas/widget_metrics.zig +228 -0
  219. package/src/primitives/canvas/widget_render.zig +2534 -0
  220. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  221. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  222. package/src/primitives/canvas/widget_render_style.zig +626 -0
  223. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  224. package/src/primitives/canvas/widget_routing.zig +475 -0
  225. package/src/primitives/canvas/widget_runtime.zig +307 -0
  226. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  227. package/src/primitives/canvas/widget_semantics.zig +480 -0
  228. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  229. package/src/primitives/canvas/widget_tests.zig +8 -0
  230. package/src/primitives/canvas/widget_text_input.zig +351 -0
  231. package/src/primitives/canvas/widget_text_select.zig +147 -0
  232. package/src/primitives/canvas/widget_tree.zig +232 -0
  233. package/src/primitives/canvas/widgets.zig +1319 -0
  234. package/src/primitives/diagnostics/root.zig +445 -0
  235. package/src/primitives/geometry/root.zig +889 -0
  236. package/src/primitives/json/root.zig +272 -0
  237. package/src/primitives/platform_info/root.zig +450 -0
  238. package/src/primitives/trace/root.zig +569 -0
  239. package/src/root.zig +165 -0
  240. package/src/runtime/api.zig +431 -0
  241. package/src/runtime/async_bridge.zig +49 -0
  242. package/src/runtime/automation_commands.zig +686 -0
  243. package/src/runtime/automation_liveness_tests.zig +140 -0
  244. package/src/runtime/automation_snapshot.zig +246 -0
  245. package/src/runtime/automation_widget_dispatch.zig +617 -0
  246. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  247. package/src/runtime/bridge_payload.zig +194 -0
  248. package/src/runtime/bridge_permission_tests.zig +551 -0
  249. package/src/runtime/bridge_responses.zig +414 -0
  250. package/src/runtime/builtin_bridge.zig +554 -0
  251. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  252. package/src/runtime/canvas_font_tests.zig +410 -0
  253. package/src/runtime/canvas_fonts.zig +229 -0
  254. package/src/runtime/canvas_frame.zig +1734 -0
  255. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  256. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  257. package/src/runtime/canvas_frame_helpers.zig +414 -0
  258. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  259. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  260. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  261. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  262. package/src/runtime/canvas_frame_tests.zig +9 -0
  263. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  264. package/src/runtime/canvas_image_tests.zig +362 -0
  265. package/src/runtime/canvas_images.zig +224 -0
  266. package/src/runtime/canvas_limits.zig +317 -0
  267. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  268. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  269. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  270. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  271. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  272. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  273. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  274. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  275. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  276. package/src/runtime/canvas_widget_display.zig +677 -0
  277. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  278. package/src/runtime/canvas_widget_events.zig +1098 -0
  279. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  280. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  281. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  282. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  283. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  284. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  285. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  286. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  287. package/src/runtime/canvas_widget_state.zig +798 -0
  288. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  289. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  290. package/src/runtime/clock.zig +231 -0
  291. package/src/runtime/core.zig +927 -0
  292. package/src/runtime/effects.zig +4197 -0
  293. package/src/runtime/effects_audio_tests.zig +690 -0
  294. package/src/runtime/effects_clipboard_tests.zig +317 -0
  295. package/src/runtime/effects_fetch_tests.zig +1031 -0
  296. package/src/runtime/effects_file_tests.zig +407 -0
  297. package/src/runtime/effects_tests.zig +1251 -0
  298. package/src/runtime/flow.zig +1175 -0
  299. package/src/runtime/frame_profile.zig +221 -0
  300. package/src/runtime/gpu_surface_events.zig +639 -0
  301. package/src/runtime/launch_timing.zig +59 -0
  302. package/src/runtime/markdown_app_tests.zig +224 -0
  303. package/src/runtime/native_command_event_tests.zig +431 -0
  304. package/src/runtime/platform_bridge_tests.zig +8 -0
  305. package/src/runtime/platform_config_tests.zig +392 -0
  306. package/src/runtime/root.zig +150 -0
  307. package/src/runtime/runtime_event_tests.zig +1079 -0
  308. package/src/runtime/session_journal.zig +1522 -0
  309. package/src/runtime/session_record.zig +332 -0
  310. package/src/runtime/session_replay.zig +274 -0
  311. package/src/runtime/session_state.zig +30 -0
  312. package/src/runtime/session_tests.zig +399 -0
  313. package/src/runtime/shell_layout.zig +472 -0
  314. package/src/runtime/shell_layout_tests.zig +1246 -0
  315. package/src/runtime/shell_view_tests.zig +604 -0
  316. package/src/runtime/state.zig +110 -0
  317. package/src/runtime/system_services.zig +392 -0
  318. package/src/runtime/test_support.zig +79 -0
  319. package/src/runtime/tests.zig +30 -0
  320. package/src/runtime/ui_app.zig +3290 -0
  321. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  322. package/src/runtime/ui_app_provenance.zig +370 -0
  323. package/src/runtime/ui_app_tests.zig +3254 -0
  324. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  325. package/src/runtime/ui_app_window_tests.zig +442 -0
  326. package/src/runtime/validation.zig +206 -0
  327. package/src/runtime/view.zig +908 -0
  328. package/src/runtime/view_canvas.zig +850 -0
  329. package/src/runtime/view_widget_control.zig +433 -0
  330. package/src/runtime/view_widget_scroll.zig +360 -0
  331. package/src/runtime/view_widget_text.zig +275 -0
  332. package/src/runtime/view_widget_tree.zig +843 -0
  333. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  334. package/src/runtime/widget_bridge.zig +231 -0
  335. package/src/runtime/window_command_bridge_tests.zig +403 -0
  336. package/src/runtime/window_storage.zig +255 -0
  337. package/src/runtime/window_view_runtime.zig +727 -0
  338. package/src/runtime/window_views.zig +334 -0
  339. package/src/security/root.zig +118 -0
  340. package/src/tooling/android.zig +1172 -0
  341. package/src/tooling/assets.zig +128 -0
  342. package/src/tooling/buildgraph.zig +379 -0
  343. package/src/tooling/cef.zig +671 -0
  344. package/src/tooling/codesign.zig +172 -0
  345. package/src/tooling/components/identity_tests.zig +185 -0
  346. package/src/tooling/components/stepper.zig +113 -0
  347. package/src/tooling/components/timeline.native +27 -0
  348. package/src/tooling/components/timeline_item.zig +131 -0
  349. package/src/tooling/default_icon.icns +0 -0
  350. package/src/tooling/default_icon.png +0 -0
  351. package/src/tooling/dev.zig +172 -0
  352. package/src/tooling/doctor.zig +349 -0
  353. package/src/tooling/eject_components.zig +170 -0
  354. package/src/tooling/embedlib.zig +290 -0
  355. package/src/tooling/ios.zig +528 -0
  356. package/src/tooling/manifest.zig +1939 -0
  357. package/src/tooling/package.zig +2388 -0
  358. package/src/tooling/process_tree.zig +114 -0
  359. package/src/tooling/raw_manifest.zig +193 -0
  360. package/src/tooling/root.zig +22 -0
  361. package/src/tooling/templates.zig +3191 -0
  362. package/src/tooling/toolchain.zig +371 -0
  363. package/src/tooling/verbs.zig +204 -0
  364. package/src/tooling/web_engine.zig +135 -0
  365. package/src/tooling/xcodeproj.zig +462 -0
  366. package/src/window_state/root.zig +507 -0
  367. package/index.js +0 -2
@@ -0,0 +1,2552 @@
1
+ const std = @import("std");
2
+ const geometry = @import("geometry");
3
+ const canvas = @import("canvas");
4
+ const runtime = @import("../runtime/root.zig");
5
+ const platform = @import("../platform/root.zig");
6
+ const types = @import("types.zig");
7
+ const host = @import("host.zig");
8
+ const ui_host = @import("ui_host.zig");
9
+ const c_api = @import("c_api.zig");
10
+ const conversions = @import("conversions.zig");
11
+
12
+ const MobileWidgetRole = types.MobileWidgetRole;
13
+ const MobileWidgetFlag = types.MobileWidgetFlag;
14
+ const MobileWidgetAction = types.MobileWidgetAction;
15
+ const MobileWidgetActionKind = types.MobileWidgetActionKind;
16
+ const MobileWidgetSemantics = types.MobileWidgetSemantics;
17
+ const MobileWidgetTextGeometry = types.MobileWidgetTextGeometry;
18
+ const MobileWidgetActionRequest = types.MobileWidgetActionRequest;
19
+ const MobileTextInputState = types.MobileTextInputState;
20
+ const MobileViewportState = types.MobileViewportState;
21
+ const MobileGpuFrameState = types.MobileGpuFrameState;
22
+ const MobileCanvasPixels = types.MobileCanvasPixels;
23
+ const EmbeddedApp = host.EmbeddedApp;
24
+ const mobileApp = host.mobileApp;
25
+ const mobile_html = host.mobile_html;
26
+ const mobile_gpu_surface_label = types.mobile_gpu_surface_label;
27
+ const mobileWidgetFlags = conversions.mobileWidgetFlags;
28
+ const mobileWidgetActions = conversions.mobileWidgetActions;
29
+ const mobileWidgetActionKindFromInt = conversions.mobileWidgetActionKindFromInt;
30
+ const native_sdk_app_create = c_api.native_sdk_app_create;
31
+ const native_sdk_app_destroy = c_api.native_sdk_app_destroy;
32
+ const native_sdk_app_start = c_api.native_sdk_app_start;
33
+ const native_sdk_app_activate = c_api.native_sdk_app_activate;
34
+ const native_sdk_app_deactivate = c_api.native_sdk_app_deactivate;
35
+ const native_sdk_app_resize = c_api.native_sdk_app_resize;
36
+ const native_sdk_app_viewport = c_api.native_sdk_app_viewport;
37
+ const native_sdk_app_viewport_state = c_api.native_sdk_app_viewport_state;
38
+ const native_sdk_app_gpu_frame_state = c_api.native_sdk_app_gpu_frame_state;
39
+ const native_sdk_app_touch = c_api.native_sdk_app_touch;
40
+ const native_sdk_app_scroll = c_api.native_sdk_app_scroll;
41
+ const native_sdk_app_key = c_api.native_sdk_app_key;
42
+ const native_sdk_app_text = c_api.native_sdk_app_text;
43
+ const native_sdk_app_ime = c_api.native_sdk_app_ime;
44
+ const native_sdk_app_command = c_api.native_sdk_app_command;
45
+ const native_sdk_app_set_asset_root = c_api.native_sdk_app_set_asset_root;
46
+ const native_sdk_app_set_asset_entry = c_api.native_sdk_app_set_asset_entry;
47
+ const native_sdk_app_last_command_count = c_api.native_sdk_app_last_command_count;
48
+ const native_sdk_app_last_command_name = c_api.native_sdk_app_last_command_name;
49
+ const native_sdk_app_last_error_name = c_api.native_sdk_app_last_error_name;
50
+ const native_sdk_app_widget_semantics_count = c_api.native_sdk_app_widget_semantics_count;
51
+ const native_sdk_app_widget_semantics_at = c_api.native_sdk_app_widget_semantics_at;
52
+ const native_sdk_app_widget_semantics_by_id = c_api.native_sdk_app_widget_semantics_by_id;
53
+ const native_sdk_app_widget_text_geometry = c_api.native_sdk_app_widget_text_geometry;
54
+ const native_sdk_app_widget_action = c_api.native_sdk_app_widget_action;
55
+
56
+ fn mobileWidgetSemanticsByIdForTest(app: ?*anyopaque, id: u64) !MobileWidgetSemantics {
57
+ var node: MobileWidgetSemantics = .{};
58
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_semantics_by_id(app, id, &node));
59
+ try std.testing.expectEqual(id, node.id);
60
+ return node;
61
+ }
62
+
63
+ fn expectNoMobileWidgetSemanticsByIdForTest(app: ?*anyopaque, id: u64) !void {
64
+ var node: MobileWidgetSemantics = .{};
65
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_widget_semantics_by_id(app, id, &node));
66
+ }
67
+ test "embedded app starts and loads source" {
68
+ var null_platform = platform.NullPlatform.init(.{});
69
+ var state: u8 = 0;
70
+ const embedded = try std.testing.allocator.create(EmbeddedApp);
71
+ defer std.testing.allocator.destroy(embedded);
72
+ embedded.initInPlace(.{
73
+ .context = &state,
74
+ .name = "embedded",
75
+ .source = platform.WebViewSource.html("<p>Embedded</p>"),
76
+ }, null_platform.platform());
77
+
78
+ try embedded.start();
79
+ try @import("std").testing.expectEqualStrings("<p>Embedded</p>", null_platform.loaded_source.?.bytes);
80
+ }
81
+
82
+ test "mobile C ABI can load packaged asset source" {
83
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
84
+ defer native_sdk_app_destroy(app);
85
+
86
+ const asset_root = "/tmp/native-sdk-mobile-assets";
87
+ native_sdk_app_set_asset_root(app, asset_root, asset_root.len);
88
+ native_sdk_app_start(app);
89
+
90
+ const self = mobileApp(app).?;
91
+ const source = self.null_platform.loaded_source.?;
92
+ try std.testing.expectEqual(platform.WebViewSourceKind.assets, source.kind);
93
+ try std.testing.expectEqualStrings("zero://app", source.bytes);
94
+ try std.testing.expect(source.asset_options != null);
95
+ try std.testing.expectEqualStrings(asset_root, source.asset_options.?.root_path);
96
+ try std.testing.expectEqualStrings("index.html", source.asset_options.?.entry);
97
+ try std.testing.expect(source.asset_options.?.spa_fallback);
98
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
99
+ }
100
+
101
+ test "mobile C ABI can load custom packaged asset entry" {
102
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
103
+ defer native_sdk_app_destroy(app);
104
+
105
+ const asset_root = "/tmp/native-sdk-mobile-assets";
106
+ const asset_entry = "main.html";
107
+ native_sdk_app_set_asset_root(app, asset_root, asset_root.len);
108
+ native_sdk_app_set_asset_entry(app, asset_entry, asset_entry.len);
109
+ native_sdk_app_start(app);
110
+
111
+ const self = mobileApp(app).?;
112
+ const source = self.null_platform.loaded_source.?;
113
+ try std.testing.expectEqual(platform.WebViewSourceKind.assets, source.kind);
114
+ try std.testing.expect(source.asset_options != null);
115
+ try std.testing.expectEqualStrings(asset_root, source.asset_options.?.root_path);
116
+ try std.testing.expectEqualStrings(asset_entry, source.asset_options.?.entry);
117
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
118
+ }
119
+
120
+ test "mobile C ABI can reset asset root before startup" {
121
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
122
+ defer native_sdk_app_destroy(app);
123
+
124
+ const asset_root = "/tmp/native-sdk-mobile-assets";
125
+ native_sdk_app_set_asset_root(app, asset_root, asset_root.len);
126
+ native_sdk_app_set_asset_root(app, asset_root, 0);
127
+ native_sdk_app_start(app);
128
+
129
+ const self = mobileApp(app).?;
130
+ const source = self.null_platform.loaded_source.?;
131
+ try std.testing.expectEqual(platform.WebViewSourceKind.html, source.kind);
132
+ try std.testing.expectEqualStrings(mobile_html, source.bytes);
133
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
134
+ }
135
+
136
+ test "mobile C ABI forwards activation lifecycle through embedded runtime" {
137
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
138
+ defer native_sdk_app_destroy(app);
139
+
140
+ native_sdk_app_start(app);
141
+ native_sdk_app_activate(app);
142
+ native_sdk_app_deactivate(app);
143
+
144
+ const self = mobileApp(app).?;
145
+ try std.testing.expectEqual(@as(usize, 1), self.activation_count);
146
+ try std.testing.expectEqual(@as(usize, 1), self.deactivation_count);
147
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
148
+ }
149
+
150
+ test "mobile C ABI forwards surface resize and touch input" {
151
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
152
+ defer native_sdk_app_destroy(app);
153
+
154
+ var native_surface_token: u8 = 0;
155
+ native_sdk_app_resize(app, 390, 844, 3, &native_surface_token);
156
+
157
+ const self = mobileApp(app).?;
158
+ try std.testing.expectEqual(@as(usize, 1), self.mobile_surface_resize_count);
159
+ try std.testing.expectEqual(@as(f32, 390), self.mobile_surface_width);
160
+ try std.testing.expectEqual(@as(f32, 844), self.mobile_surface_height);
161
+ try std.testing.expectEqual(@as(f32, 3), self.mobile_surface_scale);
162
+
163
+ native_sdk_app_viewport(app, 390, 700, 3, &native_surface_token, 47, 0, 34, 0, 0, 0, 144, 0);
164
+ try std.testing.expectEqual(@as(usize, 2), self.mobile_surface_resize_count);
165
+ try std.testing.expectEqual(@as(f32, 390), self.embedded.runtime.surface.size.width);
166
+ try std.testing.expectEqual(@as(f32, 700), self.embedded.runtime.surface.size.height);
167
+ try std.testing.expectEqual(@as(f32, 47), self.embedded.runtime.surface.safe_area_insets.top);
168
+ try std.testing.expectEqual(@as(f32, 34), self.embedded.runtime.surface.safe_area_insets.bottom);
169
+ try std.testing.expectEqual(@as(f32, 144), self.embedded.runtime.surface.keyboard_insets.bottom);
170
+
171
+ var viewport: MobileViewportState = .{};
172
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_viewport_state(app, &viewport));
173
+ try std.testing.expectEqual(@as(f32, 390), viewport.width);
174
+ try std.testing.expectEqual(@as(f32, 700), viewport.height);
175
+ try std.testing.expectEqual(@as(f32, 3), viewport.scale);
176
+ try std.testing.expectEqual(@as(c_int, 1), viewport.has_surface);
177
+ try std.testing.expectEqual(@as(f32, 47), viewport.safe_top);
178
+ try std.testing.expectEqual(@as(f32, 34), viewport.safe_bottom);
179
+ try std.testing.expectEqual(@as(f32, 144), viewport.keyboard_bottom);
180
+ try std.testing.expectEqual(@as(f32, 0), viewport.content_x);
181
+ try std.testing.expectEqual(@as(f32, 47), viewport.content_y);
182
+ try std.testing.expectEqual(@as(f32, 390), viewport.content_width);
183
+ try std.testing.expectEqual(@as(f32, 509), viewport.content_height);
184
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
185
+
186
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_viewport_state(app, null));
187
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
188
+
189
+ native_sdk_app_touch(app, 42, 0, 11, 22, 0.5);
190
+ try std.testing.expectEqual(@as(usize, 1), self.touch_count);
191
+ try std.testing.expectEqual(@as(u64, 42), self.last_touch_id);
192
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.pointer_down, self.last_touch_kind);
193
+ try std.testing.expect(self.last_touch_timestamp_ns > 0);
194
+ try std.testing.expectEqual(@as(f32, 11), self.last_touch_x);
195
+ try std.testing.expectEqual(@as(f32, 22), self.last_touch_y);
196
+ try std.testing.expectEqual(@as(f32, 0.5), self.last_touch_pressure);
197
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
198
+
199
+ native_sdk_app_touch(app, 42, 2, 13, 25, 0.75);
200
+ try std.testing.expectEqual(@as(usize, 2), self.touch_count);
201
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.pointer_drag, self.last_touch_kind);
202
+ try std.testing.expectEqual(@as(f32, 13), self.last_touch_x);
203
+ try std.testing.expectEqual(@as(f32, 25), self.last_touch_y);
204
+ try std.testing.expectEqual(@as(f32, 0.75), self.last_touch_pressure);
205
+
206
+ native_sdk_app_touch(app, 42, 3, 13, 25, 0);
207
+ try std.testing.expectEqual(@as(usize, 3), self.touch_count);
208
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.pointer_cancel, self.last_touch_kind);
209
+
210
+ native_sdk_app_scroll(app, 42, 15, 26, -2, 18);
211
+ try std.testing.expectEqual(@as(usize, 4), self.touch_count);
212
+ try std.testing.expectEqual(@as(usize, 4), self.input_count);
213
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.scroll, self.last_touch_kind);
214
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.scroll, self.last_input_kind);
215
+ try std.testing.expectEqual(@as(u64, 42), self.last_touch_id);
216
+ try std.testing.expectEqual(@as(f32, 15), self.last_touch_x);
217
+ try std.testing.expectEqual(@as(f32, 26), self.last_touch_y);
218
+ try std.testing.expectEqual(@as(f32, -2), self.last_touch_delta_x);
219
+ try std.testing.expectEqual(@as(f32, 18), self.last_touch_delta_y);
220
+ try std.testing.expectEqual(@as(f32, 0), self.last_touch_pressure);
221
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
222
+
223
+ native_sdk_app_touch(app, 42, 99, 13, 25, 0);
224
+ try std.testing.expectEqual(@as(usize, 4), self.touch_count);
225
+ try std.testing.expectEqualStrings("InvalidTouchPhase", std.mem.span(native_sdk_app_last_error_name(app)));
226
+ }
227
+
228
+ test "mobile C ABI forwards key text and IME input" {
229
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
230
+ defer native_sdk_app_destroy(app);
231
+
232
+ const self = mobileApp(app).?;
233
+ native_sdk_app_key(app, 0, "enter", "enter".len, "", 0, 17);
234
+ try std.testing.expectEqual(@as(usize, 1), self.input_count);
235
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.key_down, self.last_input_kind);
236
+ try std.testing.expectEqualStrings("enter", self.last_input_key[0..self.last_input_key_len]);
237
+ try std.testing.expect(self.last_input_modifiers.primary);
238
+ try std.testing.expect(self.last_input_modifiers.shift);
239
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
240
+
241
+ native_sdk_app_text(app, "é", "é".len);
242
+ try std.testing.expectEqual(@as(usize, 2), self.input_count);
243
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.text_input, self.last_input_kind);
244
+ try std.testing.expectEqualStrings("é", self.last_input_text[0..self.last_input_text_len]);
245
+
246
+ native_sdk_app_ime(app, 0, "かな", "かな".len, "かな".len);
247
+ try std.testing.expectEqual(@as(usize, 3), self.input_count);
248
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.ime_set_composition, self.last_input_kind);
249
+ try std.testing.expectEqualStrings("かな", self.last_input_text[0..self.last_input_text_len]);
250
+ try std.testing.expectEqual(@as(?usize, "かな".len), self.last_input_composition_cursor);
251
+
252
+ native_sdk_app_ime(app, 1, "", 0, -1);
253
+ try std.testing.expectEqual(@as(usize, 4), self.input_count);
254
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.ime_commit_composition, self.last_input_kind);
255
+
256
+ native_sdk_app_ime(app, 2, "", 0, -1);
257
+ try std.testing.expectEqual(@as(usize, 5), self.input_count);
258
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.ime_cancel_composition, self.last_input_kind);
259
+
260
+ native_sdk_app_key(app, 99, "enter", "enter".len, "", 0, 0);
261
+ try std.testing.expectEqual(@as(usize, 5), self.input_count);
262
+ try std.testing.expectEqualStrings("InvalidKeyPhase", std.mem.span(native_sdk_app_last_error_name(app)));
263
+
264
+ native_sdk_app_ime(app, 99, "", 0, -1);
265
+ try std.testing.expectEqual(@as(usize, 5), self.input_count);
266
+ try std.testing.expectEqualStrings("InvalidImeKind", std.mem.span(native_sdk_app_last_error_name(app)));
267
+ }
268
+
269
+ test "mobile C ABI exposes GPU frame state" {
270
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
271
+ defer native_sdk_app_destroy(app);
272
+
273
+ const self = mobileApp(app).?;
274
+ self.null_platform.gpu_surfaces = true;
275
+ native_sdk_app_start(app);
276
+ const view = try self.embedded.runtime.createView(.{
277
+ .window_id = 1,
278
+ .label = mobile_gpu_surface_label,
279
+ .kind = .gpu_surface,
280
+ .frame = geometry.RectF.init(0, 0, 390, 844),
281
+ });
282
+
283
+ const children = [_]canvas.Widget{
284
+ .{
285
+ .id = 2,
286
+ .kind = .button,
287
+ .frame = geometry.RectF.init(16, 16, 120, 36),
288
+ .text = "Continue",
289
+ .semantics = .{ .label = "Continue" },
290
+ },
291
+ };
292
+ var nodes: [4]canvas.WidgetLayoutNode = undefined;
293
+ const layout = try canvas.layoutWidgetTree(.{
294
+ .id = 1,
295
+ .kind = .panel,
296
+ .children = &children,
297
+ .semantics = .{ .label = "Mobile GPU frame" },
298
+ }, geometry.RectF.init(0, 0, 390, 844), &nodes);
299
+ _ = try self.embedded.runtime.setCanvasWidgetLayout(1, mobile_gpu_surface_label, layout);
300
+ _ = try self.embedded.runtime.emitCanvasWidgetDisplayList(1, mobile_gpu_surface_label, .{});
301
+
302
+ try self.embedded.runtime.dispatchPlatformEvent(self.embedded.app, .{ .gpu_surface_input = .{
303
+ .window_id = 1,
304
+ .label = mobile_gpu_surface_label,
305
+ .kind = .pointer_down,
306
+ .timestamp_ns = 1_000_000,
307
+ .pointer_id = 9,
308
+ .x = 22,
309
+ .y = 28,
310
+ .pressure = 0.75,
311
+ } });
312
+ try self.embedded.runtime.dispatchPlatformEvent(self.embedded.app, .{ .gpu_surface_frame = .{
313
+ .window_id = 1,
314
+ .label = mobile_gpu_surface_label,
315
+ .size = geometry.SizeF.init(390, 844),
316
+ .scale_factor = 3,
317
+ .frame_index = 7,
318
+ .timestamp_ns = 21_000_000,
319
+ .frame_interval_ns = 8_333_333,
320
+ .nonblank = true,
321
+ .sample_color = 0xff3366ff,
322
+ .status = .ready,
323
+ .vsync = true,
324
+ } });
325
+
326
+ var state: MobileGpuFrameState = .{};
327
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_gpu_frame_state(app, &state));
328
+ try std.testing.expectEqual(view.id, state.surface_id);
329
+ try std.testing.expectEqual(@as(u64, 1), state.window_id);
330
+ try std.testing.expectEqual(@as(f32, 390), state.width);
331
+ try std.testing.expectEqual(@as(f32, 844), state.height);
332
+ try std.testing.expectEqual(@as(f32, 3), state.scale);
333
+ try std.testing.expectEqual(@as(u64, 7), state.frame_index);
334
+ try std.testing.expectEqual(@as(u64, 21_000_000), state.timestamp_ns);
335
+ try std.testing.expectEqual(@as(u64, 8_333_333), state.frame_interval_ns);
336
+ try std.testing.expectEqual(@as(u64, 1_000_000), state.input_timestamp_ns);
337
+ try std.testing.expectEqual(@as(u64, 20_000_000), state.input_latency_ns);
338
+ try std.testing.expectEqual(@as(u64, 8_333_333), state.input_latency_budget_ns);
339
+ try std.testing.expectEqual(@as(usize, 1), state.input_latency_budget_exceeded_count);
340
+ try std.testing.expectEqual(@as(c_int, 0), state.input_latency_budget_ok);
341
+ try std.testing.expectEqual(@as(c_int, 1), state.nonblank);
342
+ try std.testing.expectEqual(@as(u32, 0xff3366ff), state.sample_color);
343
+ try std.testing.expectEqual(@intFromEnum(platform.GpuSurfaceStatus.ready), state.status);
344
+ try std.testing.expectEqual(@as(c_int, 1), state.vsync);
345
+ try std.testing.expect(state.canvas_revision > 0);
346
+ try std.testing.expectEqual(@as(usize, 2), state.widget_node_count);
347
+ try std.testing.expectEqual(@as(usize, 2), state.widget_semantics_count);
348
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
349
+
350
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_gpu_frame_state(app, null));
351
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
352
+ }
353
+
354
+ test "mobile C ABI exposes GPU widget accessibility semantics" {
355
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
356
+ defer native_sdk_app_destroy(app);
357
+
358
+ const self = mobileApp(app).?;
359
+ self.null_platform.gpu_surfaces = true;
360
+ native_sdk_app_start(app);
361
+ _ = try self.embedded.runtime.createView(.{
362
+ .window_id = 1,
363
+ .label = mobile_gpu_surface_label,
364
+ .kind = .gpu_surface,
365
+ .frame = geometry.RectF.init(0, 0, 320, 180),
366
+ });
367
+
368
+ const scroll_children = [_]canvas.Widget{
369
+ .{
370
+ .id = 5,
371
+ .kind = .button,
372
+ .frame = geometry.RectF.init(0, 0, 0, 28),
373
+ .text = "Top",
374
+ },
375
+ .{
376
+ .id = 6,
377
+ .kind = .button,
378
+ .frame = geometry.RectF.init(0, 88, 0, 28),
379
+ .text = "Bottom",
380
+ },
381
+ };
382
+ const list_children = [_]canvas.Widget{
383
+ .{
384
+ .id = 8,
385
+ .kind = .list_item,
386
+ .text = "Inbox",
387
+ },
388
+ .{
389
+ .id = 9,
390
+ .kind = .list_item,
391
+ .text = "Archive",
392
+ },
393
+ };
394
+ const grid_cells = [_]canvas.Widget{
395
+ .{
396
+ .id = 12,
397
+ .kind = .data_cell,
398
+ .text = "Project",
399
+ .layout = .{ .grow = 1 },
400
+ },
401
+ .{
402
+ .id = 13,
403
+ .kind = .data_cell,
404
+ .text = "Status",
405
+ .layout = .{ .grow = 1 },
406
+ },
407
+ };
408
+ const grid_rows = [_]canvas.Widget{.{
409
+ .id = 11,
410
+ .kind = .data_row,
411
+ .children = &grid_cells,
412
+ }};
413
+ const children = [_]canvas.Widget{
414
+ .{
415
+ .id = 2,
416
+ .kind = .button,
417
+ .frame = geometry.RectF.init(12, 16, 96, 32),
418
+ .text = "Run",
419
+ .semantics = .{ .label = "Run report" },
420
+ },
421
+ .{
422
+ .id = 3,
423
+ .kind = .text_field,
424
+ .frame = geometry.RectF.init(12, 56, 160, 32),
425
+ .text = "Draft",
426
+ .placeholder = "Report title placeholder",
427
+ .text_selection = canvas.TextSelection{ .anchor = 1, .focus = 4 },
428
+ .state = .{ .focused = true },
429
+ .semantics = .{ .label = "Report title" },
430
+ },
431
+ .{
432
+ .id = 4,
433
+ .kind = .scroll_view,
434
+ .frame = geometry.RectF.init(12, 96, 120, 48),
435
+ .value = 20,
436
+ .semantics = .{ .label = "Mobile scroll" },
437
+ .children = &scroll_children,
438
+ },
439
+ .{
440
+ .id = 7,
441
+ .kind = .list,
442
+ .frame = geometry.RectF.init(160, 16, 120, 68),
443
+ .text = "Mailboxes",
444
+ .layout = .{ .gap = 4 },
445
+ .children = &list_children,
446
+ },
447
+ .{
448
+ .id = 10,
449
+ .kind = .data_grid,
450
+ .frame = geometry.RectF.init(160, 96, 140, 40),
451
+ .text = "Deployments",
452
+ .layout = .{ .gap = 2 },
453
+ .children = &grid_rows,
454
+ },
455
+ };
456
+ var nodes: [16]canvas.WidgetLayoutNode = undefined;
457
+ const layout = try canvas.layoutWidgetTree(.{
458
+ .id = 1,
459
+ .kind = .stack,
460
+ .children = &children,
461
+ .semantics = .{ .label = "Mobile canvas widgets" },
462
+ }, geometry.RectF.init(0, 0, 320, 180), &nodes);
463
+ _ = try self.embedded.runtime.setCanvasWidgetLayout(1, mobile_gpu_surface_label, layout);
464
+
465
+ try std.testing.expectEqual(@as(usize, 13), native_sdk_app_widget_semantics_count(app));
466
+
467
+ var root_node: MobileWidgetSemantics = .{};
468
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_semantics_at(app, 0, &root_node));
469
+ try std.testing.expectEqual(@as(u64, 1), root_node.id);
470
+ try std.testing.expectEqual(@as(u64, 0), root_node.parent_id);
471
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.group), root_node.role);
472
+ try std.testing.expectEqualStrings("Mobile canvas widgets", root_node.label.?[0..root_node.label_len]);
473
+
474
+ var button_node: MobileWidgetSemantics = .{};
475
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_semantics_at(app, 1, &button_node));
476
+ try std.testing.expectEqual(@as(u64, 2), button_node.id);
477
+ try std.testing.expectEqual(@as(u64, 1), button_node.parent_id);
478
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.button), button_node.role);
479
+ try std.testing.expectEqualStrings("Run report", button_node.label.?[0..button_node.label_len]);
480
+ try std.testing.expect((button_node.flags & @intFromEnum(MobileWidgetFlag.focusable)) != 0);
481
+ try std.testing.expect((button_node.actions & @intFromEnum(MobileWidgetAction.press)) != 0);
482
+ try std.testing.expectEqual(@as(f32, 12), button_node.x);
483
+ try std.testing.expectEqual(@as(f32, 16), button_node.y);
484
+ try std.testing.expectEqual(@as(f32, 96), button_node.width);
485
+ try std.testing.expectEqual(@as(f32, 32), button_node.height);
486
+
487
+ var text_node: MobileWidgetSemantics = .{};
488
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_semantics_at(app, 2, &text_node));
489
+ try std.testing.expectEqual(@as(u64, 3), text_node.id);
490
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.textbox), text_node.role);
491
+ try std.testing.expectEqualStrings("Report title", text_node.label.?[0..text_node.label_len]);
492
+ try std.testing.expectEqualStrings("Draft", text_node.text.?[0..text_node.text_len]);
493
+ try std.testing.expectEqualStrings("Report title placeholder", text_node.placeholder.?[0..text_node.placeholder_len]);
494
+ try std.testing.expectEqual(@as(isize, 1), text_node.text_selection_start);
495
+ try std.testing.expectEqual(@as(isize, 4), text_node.text_selection_end);
496
+ try std.testing.expect((text_node.flags & @intFromEnum(MobileWidgetFlag.focused)) != 0);
497
+ try std.testing.expect((text_node.actions & @intFromEnum(MobileWidgetAction.set_text)) != 0);
498
+ try std.testing.expect((text_node.actions & @intFromEnum(MobileWidgetAction.set_selection)) != 0);
499
+
500
+ const scroll_node = try mobileWidgetSemanticsByIdForTest(app, 4);
501
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.group), scroll_node.role);
502
+ try std.testing.expectEqual(@as(c_int, 1), scroll_node.has_scroll);
503
+ try std.testing.expectEqual(@as(f32, 20), scroll_node.scroll_offset);
504
+ try std.testing.expectEqual(@as(f32, 48), scroll_node.scroll_viewport_extent);
505
+ try std.testing.expectEqual(@as(f32, 116), scroll_node.scroll_content_extent);
506
+ try std.testing.expect((scroll_node.actions & @intFromEnum(MobileWidgetAction.increment)) != 0);
507
+ try std.testing.expect((scroll_node.actions & @intFromEnum(MobileWidgetAction.decrement)) != 0);
508
+
509
+ native_sdk_app_scroll(app, 11, 24, 112, 0, 14);
510
+ const scrolled_node = try mobileWidgetSemanticsByIdForTest(app, 4);
511
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.scroll, self.last_input_kind);
512
+ try std.testing.expectEqual(@as(u64, 11), self.last_touch_id);
513
+ try std.testing.expectEqual(@as(f32, 14), self.last_touch_delta_y);
514
+ try std.testing.expectEqual(@as(f32, 34), scrolled_node.scroll_offset);
515
+ try std.testing.expectEqual(@as(f32, 48), scrolled_node.scroll_viewport_extent);
516
+ try std.testing.expectEqual(@as(f32, 116), scrolled_node.scroll_content_extent);
517
+
518
+ const list_node = try mobileWidgetSemanticsByIdForTest(app, 7);
519
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.list), list_node.role);
520
+ try std.testing.expectEqualStrings("Mailboxes", list_node.label.?[0..list_node.label_len]);
521
+ const archive_node = try mobileWidgetSemanticsByIdForTest(app, 9);
522
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.listitem), archive_node.role);
523
+ try std.testing.expectEqual(@as(u64, 7), archive_node.parent_id);
524
+ try std.testing.expectEqual(@as(isize, 1), archive_node.list_item_index);
525
+ try std.testing.expectEqual(@as(isize, 2), archive_node.list_item_count);
526
+ try std.testing.expect((archive_node.actions & @intFromEnum(MobileWidgetAction.select)) != 0);
527
+
528
+ const grid_node = try mobileWidgetSemanticsByIdForTest(app, 10);
529
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.grid), grid_node.role);
530
+ try std.testing.expectEqual(@as(isize, 1), grid_node.grid_row_count);
531
+ try std.testing.expectEqual(@as(isize, 2), grid_node.grid_column_count);
532
+ const status_cell = try mobileWidgetSemanticsByIdForTest(app, 13);
533
+ try std.testing.expectEqual(@intFromEnum(MobileWidgetRole.gridcell), status_cell.role);
534
+ try std.testing.expectEqual(@as(u64, 11), status_cell.parent_id);
535
+ try std.testing.expectEqual(@as(isize, 0), status_cell.grid_row_index);
536
+ try std.testing.expectEqual(@as(isize, 1), status_cell.grid_column_index);
537
+ try std.testing.expectEqual(@as(isize, 1), status_cell.grid_row_count);
538
+ try std.testing.expectEqual(@as(isize, 2), status_cell.grid_column_count);
539
+ try std.testing.expect((status_cell.actions & @intFromEnum(MobileWidgetAction.select)) != 0);
540
+
541
+ var text_geometry: MobileWidgetTextGeometry = .{};
542
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_text_geometry(app, 3, &text_geometry));
543
+ try std.testing.expectEqual(@as(u64, 3), text_geometry.id);
544
+ try std.testing.expectEqual(@as(c_int, 0), text_geometry.has_caret_bounds);
545
+ try std.testing.expectEqual(@as(c_int, 1), text_geometry.has_selection_bounds);
546
+ try std.testing.expectEqual(@as(usize, 1), text_geometry.selection_rect_count);
547
+ try std.testing.expect(text_geometry.selection_width > 0);
548
+ try std.testing.expectEqual(@as(c_int, 0), text_geometry.has_composition_bounds);
549
+
550
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_widget_text_geometry(app, 2, &text_geometry));
551
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
552
+
553
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_widget_semantics_at(app, 99, &text_node));
554
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
555
+
556
+ try expectNoMobileWidgetSemanticsByIdForTest(app, 99);
557
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
558
+ try expectNoMobileWidgetSemanticsByIdForTest(app, 0);
559
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
560
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_widget_semantics_by_id(app, 2, null));
561
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
562
+ }
563
+
564
+ test "mobile C ABI maps widget state and dismiss action flags" {
565
+ const expanded_node = canvas.WidgetSemanticsNode{
566
+ .id = 1,
567
+ .role = .button,
568
+ .label = "Menu",
569
+ .bounds = geometry.RectF.init(0, 0, 120, 32),
570
+ .state = .{ .expanded = true, .required = true, .read_only = true, .invalid = true },
571
+ .actions = .{ .dismiss = true },
572
+ };
573
+ try std.testing.expect((mobileWidgetFlags(expanded_node) & @intFromEnum(MobileWidgetFlag.expanded)) != 0);
574
+ try std.testing.expect((mobileWidgetFlags(expanded_node) & @intFromEnum(MobileWidgetFlag.collapsed)) == 0);
575
+ try std.testing.expect((mobileWidgetFlags(expanded_node) & @intFromEnum(MobileWidgetFlag.required)) != 0);
576
+ try std.testing.expect((mobileWidgetFlags(expanded_node) & @intFromEnum(MobileWidgetFlag.read_only)) != 0);
577
+ try std.testing.expect((mobileWidgetFlags(expanded_node) & @intFromEnum(MobileWidgetFlag.invalid)) != 0);
578
+ try std.testing.expect((mobileWidgetActions(expanded_node.actions) & @intFromEnum(MobileWidgetAction.dismiss)) != 0);
579
+ try std.testing.expectEqual(runtime.CanvasWidgetAccessibilityActionKind.dismiss, try mobileWidgetActionKindFromInt(@intFromEnum(MobileWidgetActionKind.dismiss)));
580
+
581
+ const collapsed_node = canvas.WidgetSemanticsNode{
582
+ .id = 2,
583
+ .role = .button,
584
+ .label = "Menu",
585
+ .bounds = geometry.RectF.init(0, 0, 120, 32),
586
+ .state = .{ .expanded = false },
587
+ };
588
+ try std.testing.expect((mobileWidgetFlags(collapsed_node) & @intFromEnum(MobileWidgetFlag.collapsed)) != 0);
589
+ try std.testing.expect((mobileWidgetFlags(collapsed_node) & @intFromEnum(MobileWidgetFlag.expanded)) == 0);
590
+ }
591
+
592
+ test "mobile C ABI dispatches GPU widget accessibility actions" {
593
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
594
+ defer native_sdk_app_destroy(app);
595
+
596
+ const self = mobileApp(app).?;
597
+ self.null_platform.gpu_surfaces = true;
598
+ native_sdk_app_start(app);
599
+ _ = try self.embedded.runtime.createView(.{
600
+ .window_id = 1,
601
+ .label = mobile_gpu_surface_label,
602
+ .kind = .gpu_surface,
603
+ .frame = geometry.RectF.init(0, 0, 360, 220),
604
+ });
605
+
606
+ const children = [_]canvas.Widget{
607
+ .{
608
+ .id = 2,
609
+ .kind = .button,
610
+ .frame = geometry.RectF.init(12, 16, 96, 32),
611
+ .text = "Run",
612
+ .command = "widget.run",
613
+ .semantics = .{ .label = "Run report" },
614
+ },
615
+ .{
616
+ .id = 3,
617
+ .kind = .checkbox,
618
+ .frame = geometry.RectF.init(12, 56, 144, 28),
619
+ .text = "Enabled",
620
+ },
621
+ .{
622
+ .id = 4,
623
+ .kind = .slider,
624
+ .frame = geometry.RectF.init(12, 92, 160, 32),
625
+ .value = 0.5,
626
+ .semantics = .{ .label = "Confidence" },
627
+ },
628
+ .{
629
+ .id = 5,
630
+ .kind = .text_field,
631
+ .frame = geometry.RectF.init(12, 136, 180, 32),
632
+ .text = "Draft",
633
+ .semantics = .{ .label = "Report title" },
634
+ },
635
+ .{
636
+ .id = 6,
637
+ .kind = .list_item,
638
+ .frame = geometry.RectF.init(210, 16, 120, 32),
639
+ .text = "Inbox",
640
+ },
641
+ .{
642
+ .id = 7,
643
+ .kind = .button,
644
+ .frame = geometry.RectF.init(210, 56, 120, 32),
645
+ .text = "Drag",
646
+ .semantics = .{ .actions = .{ .drag = true } },
647
+ },
648
+ .{
649
+ .id = 8,
650
+ .kind = .button,
651
+ .frame = geometry.RectF.init(210, 96, 120, 32),
652
+ .text = "Drop",
653
+ .semantics = .{ .actions = .{ .drop_files = true } },
654
+ },
655
+ };
656
+ var nodes: [10]canvas.WidgetLayoutNode = undefined;
657
+ const layout = try canvas.layoutWidgetTree(.{
658
+ .id = 1,
659
+ .kind = .panel,
660
+ .children = &children,
661
+ .semantics = .{ .label = "Mobile action widgets" },
662
+ }, geometry.RectF.init(0, 0, 360, 220), &nodes);
663
+ _ = try self.embedded.runtime.setCanvasWidgetLayout(1, mobile_gpu_surface_label, layout);
664
+
665
+ var action = MobileWidgetActionRequest{ .id = 2, .action = @intFromEnum(MobileWidgetActionKind.press) };
666
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
667
+ try std.testing.expectEqual(@as(usize, 1), native_sdk_app_last_command_count(app));
668
+ try std.testing.expectEqualStrings("widget.run", std.mem.span(native_sdk_app_last_command_name(app)));
669
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), self.embedded.runtime.views[0].canvas_widget_focused_id);
670
+ // The press rides the key-driven activation (the automation press
671
+ // verb's route): the surface saw a real enter key_down, so widgets
672
+ // wired through message handlers actuate exactly like commanded
673
+ // ones — a direct-command shortcut would silently no-op for them.
674
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.key_down, self.last_input_kind);
675
+ try std.testing.expectEqualStrings("enter", self.last_input_key[0..self.last_input_key_len]);
676
+
677
+ action = .{ .id = 3, .action = @intFromEnum(MobileWidgetActionKind.toggle) };
678
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
679
+ const checkbox = try mobileWidgetSemanticsByIdForTest(app, 3);
680
+ try std.testing.expectEqual(@as(c_int, 1), checkbox.has_value);
681
+ try std.testing.expectEqual(@as(f32, 1), checkbox.value);
682
+ try std.testing.expect((checkbox.flags & @intFromEnum(MobileWidgetFlag.selected)) != 0);
683
+
684
+ action = .{ .id = 4, .action = @intFromEnum(MobileWidgetActionKind.increment) };
685
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
686
+ const slider = try mobileWidgetSemanticsByIdForTest(app, 4);
687
+ try std.testing.expectApproxEqAbs(@as(f32, 0.55), slider.value, 0.001);
688
+
689
+ const title = "Hello world";
690
+ action = .{
691
+ .id = 5,
692
+ .action = @intFromEnum(MobileWidgetActionKind.set_text),
693
+ .text = title,
694
+ .text_len = title.len,
695
+ };
696
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
697
+ var text_field = try mobileWidgetSemanticsByIdForTest(app, 5);
698
+ try std.testing.expectEqualStrings(title, text_field.text.?[0..text_field.text_len]);
699
+ try std.testing.expectEqual(@as(isize, @intCast(title.len)), text_field.text_selection_start);
700
+ try std.testing.expectEqual(@as(isize, @intCast(title.len)), text_field.text_selection_end);
701
+
702
+ const composition = "!";
703
+ action = .{
704
+ .id = 5,
705
+ .action = @intFromEnum(MobileWidgetActionKind.set_composition),
706
+ .text = composition,
707
+ .text_len = composition.len,
708
+ };
709
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
710
+ text_field = try mobileWidgetSemanticsByIdForTest(app, 5);
711
+ try std.testing.expectEqualStrings("Hello world!", text_field.text.?[0..text_field.text_len]);
712
+ try std.testing.expectEqual(@as(isize, @intCast(title.len)), text_field.text_composition_start);
713
+ try std.testing.expectEqual(@as(isize, @intCast(title.len + composition.len)), text_field.text_composition_end);
714
+
715
+ action = .{ .id = 5, .action = @intFromEnum(MobileWidgetActionKind.commit_composition) };
716
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
717
+ text_field = try mobileWidgetSemanticsByIdForTest(app, 5);
718
+ try std.testing.expectEqualStrings("Hello world!", text_field.text.?[0..text_field.text_len]);
719
+ try std.testing.expectEqual(@as(isize, -1), text_field.text_composition_start);
720
+ try std.testing.expectEqual(@as(isize, -1), text_field.text_composition_end);
721
+
722
+ action = .{
723
+ .id = 5,
724
+ .action = @intFromEnum(MobileWidgetActionKind.set_selection),
725
+ .selection_anchor = 0,
726
+ .selection_focus = 5,
727
+ .has_selection = 1,
728
+ };
729
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
730
+ text_field = try mobileWidgetSemanticsByIdForTest(app, 5);
731
+ try std.testing.expectEqual(@as(isize, 0), text_field.text_selection_start);
732
+ try std.testing.expectEqual(@as(isize, 5), text_field.text_selection_end);
733
+
734
+ action = .{ .id = 6, .action = @intFromEnum(MobileWidgetActionKind.select) };
735
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
736
+ const list_item = try mobileWidgetSemanticsByIdForTest(app, 6);
737
+ try std.testing.expectEqual(@as(c_int, 1), list_item.has_value);
738
+ try std.testing.expectEqual(@as(f32, 1), list_item.value);
739
+ try std.testing.expect((list_item.flags & @intFromEnum(MobileWidgetFlag.selected)) != 0);
740
+
741
+ const drag_delta = "6 2";
742
+ action = .{
743
+ .id = 7,
744
+ .action = @intFromEnum(MobileWidgetActionKind.drag),
745
+ .text = drag_delta,
746
+ .text_len = drag_delta.len,
747
+ };
748
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
749
+ try std.testing.expectEqual(platform.GpuSurfaceInputKind.pointer_drag, self.last_input_kind);
750
+ try std.testing.expectApproxEqAbs(@as(f32, 276), self.last_touch_x, 0.001);
751
+ try std.testing.expectApproxEqAbs(@as(f32, 74), self.last_touch_y, 0.001);
752
+
753
+ const drop_paths = "/tmp/mobile-report.csv";
754
+ action = .{
755
+ .id = 8,
756
+ .action = @intFromEnum(MobileWidgetActionKind.drop_files),
757
+ .text = drop_paths,
758
+ .text_len = drop_paths.len,
759
+ };
760
+ try std.testing.expectEqual(@as(c_int, 1), native_sdk_app_widget_action(app, &action));
761
+ try std.testing.expectEqualStrings("drop:files", self.null_platform.lastWindowEventName());
762
+ try std.testing.expect(std.mem.indexOf(u8, self.null_platform.lastWindowEventDetail(), "\"paths\":[\"/tmp/mobile-report.csv\"]") != null);
763
+
764
+ action = .{ .id = 99, .action = @intFromEnum(MobileWidgetActionKind.press) };
765
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_widget_action(app, &action));
766
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
767
+
768
+ action = .{ .id = 5, .action = @intFromEnum(MobileWidgetActionKind.set_selection) };
769
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_widget_action(app, &action));
770
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
771
+
772
+ action = .{ .id = 2, .action = 999 };
773
+ try std.testing.expectEqual(@as(c_int, 0), native_sdk_app_widget_action(app, &action));
774
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
775
+ }
776
+
777
+ // ------------------------------------------------------------- UiApp host
778
+ //
779
+ // M1 end-to-end: a user UiApp compiled into the embed host answers the same
780
+ // C ABI the mobile shims call — create, viewport, host-pumped frames that
781
+ // render pixels, touch that mutates the model, and IME text that lands in a
782
+ // textbox — with NullPlatform standing in for the (M2) real surface.
783
+
784
+ const MobileCounterDef = struct {
785
+ pub const Model = struct {
786
+ count: u32 = 0,
787
+ draft: canvas.TextBuffer(64) = .{},
788
+ };
789
+
790
+ pub const Msg = union(enum) {
791
+ increment,
792
+ draft_edit: canvas.TextInputEvent,
793
+ };
794
+
795
+ const App = runtime.UiApp(Model, Msg);
796
+
797
+ pub fn initModel() Model {
798
+ return .{};
799
+ }
800
+
801
+ pub fn mobileOptions() App.Options {
802
+ return .{
803
+ .name = "mobile-counter",
804
+ .scene = ui_host.mobile_shell_scene,
805
+ .canvas_label = mobile_gpu_surface_label,
806
+ .update = update,
807
+ .view = view,
808
+ };
809
+ }
810
+
811
+ fn update(model: *Model, msg: Msg) void {
812
+ switch (msg) {
813
+ .increment => model.count += 1,
814
+ .draft_edit => |edit| model.draft.apply(edit),
815
+ }
816
+ }
817
+
818
+ fn view(ui: *App.Ui, model: *const Model) App.Ui.Node {
819
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
820
+ ui.text(.{}, ui.fmt("Count {d}", .{model.count})),
821
+ ui.button(.{ .variant = .primary, .on_press = .increment }, "Increment"),
822
+ ui.textField(.{
823
+ .text = model.draft.text(),
824
+ .placeholder = "Note",
825
+ .on_input = App.Ui.inputMsg(.draft_edit),
826
+ }),
827
+ });
828
+ }
829
+ };
830
+
831
+ const MobileCounterHost = ui_host.UiAppHost(MobileCounterDef);
832
+ const MobileCounterApi = c_api.MobileCApi(MobileCounterHost);
833
+
834
+ fn expectNoUiHostError(app: ?*anyopaque) !void {
835
+ try std.testing.expectEqualStrings("", std.mem.span(MobileCounterApi.native_sdk_app_last_error_name(app)));
836
+ }
837
+
838
+ fn findMobileSemanticsByRole(app: ?*anyopaque, role: MobileWidgetRole) !MobileWidgetSemantics {
839
+ const count = MobileCounterApi.native_sdk_app_widget_semantics_count(app);
840
+ var index: usize = 0;
841
+ while (index < count) : (index += 1) {
842
+ var node: MobileWidgetSemantics = .{};
843
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_at(app, index, &node));
844
+ if (node.role == @intFromEnum(role)) return node;
845
+ }
846
+ return error.TestUnexpectedResult;
847
+ }
848
+
849
+ fn uiHostRetainedTextExists(self: *MobileCounterHost, text: []const u8) !bool {
850
+ const layout = try self.embedded.runtime.canvasWidgetLayout(1, mobile_gpu_surface_label);
851
+ for (layout.nodes) |node| {
852
+ if (node.widget.kind == .text and std.mem.eql(u8, node.widget.text, text)) return true;
853
+ }
854
+ return false;
855
+ }
856
+
857
+ fn tapMobileWidget(app: ?*anyopaque, node: MobileWidgetSemantics) !void {
858
+ const x = node.x + node.width / 2;
859
+ const y = node.y + node.height / 2;
860
+ MobileCounterApi.native_sdk_app_touch(app, 1, 0, x, y, 1);
861
+ try expectNoUiHostError(app);
862
+ MobileCounterApi.native_sdk_app_touch(app, 1, 1, x, y, 0);
863
+ try expectNoUiHostError(app);
864
+ }
865
+
866
+ test "mobile C ABI drives a user UiApp canvas scene end to end" {
867
+ const app = MobileCounterApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
868
+ defer MobileCounterApi.native_sdk_app_destroy(app);
869
+ const self: *MobileCounterHost = @ptrCast(@alignCast(app));
870
+
871
+ MobileCounterApi.native_sdk_app_start(app);
872
+ try expectNoUiHostError(app);
873
+
874
+ // Host-reported viewport: window and mobile-surface view take the size.
875
+ var surface_token: u8 = 0;
876
+ MobileCounterApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
877
+ try expectNoUiHostError(app);
878
+ var viewport: MobileViewportState = .{};
879
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_viewport_state(app, &viewport));
880
+ try std.testing.expectEqual(@as(f32, 390), viewport.width);
881
+ try std.testing.expectEqual(@as(f32, 844), viewport.height);
882
+
883
+ // First host-pumped frame installs the widget tree and presents pixels.
884
+ MobileCounterApi.native_sdk_app_frame(app);
885
+ try expectNoUiHostError(app);
886
+ try std.testing.expect(self.ui.installed);
887
+ try std.testing.expect(try uiHostRetainedTextExists(self, "Count 0"));
888
+ try std.testing.expectEqual(@as(usize, 1), self.null_platform.gpu_surface_present_count);
889
+
890
+ // The next frame reports the presented pixels: nonblank with a sample.
891
+ MobileCounterApi.native_sdk_app_frame(app);
892
+ try expectNoUiHostError(app);
893
+ var frame_state: MobileGpuFrameState = .{};
894
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_gpu_frame_state(app, &frame_state));
895
+ try std.testing.expectEqual(@as(f32, 390), frame_state.width);
896
+ try std.testing.expectEqual(@as(f32, 844), frame_state.height);
897
+ try std.testing.expectEqual(@as(c_int, 1), frame_state.nonblank);
898
+ try std.testing.expect(frame_state.sample_color != 0);
899
+ try std.testing.expect(frame_state.widget_node_count > 0);
900
+ try std.testing.expect(frame_state.widget_semantics_count > 0);
901
+
902
+ // Pixels are retrievable over the ABI and are not blank.
903
+ var pixel_info: MobileCanvasPixels = .{};
904
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_render_pixel_size(app, 1, &pixel_info));
905
+ try std.testing.expectEqual(@as(usize, 390), pixel_info.width);
906
+ try std.testing.expectEqual(@as(usize, 844), pixel_info.height);
907
+ try std.testing.expectEqual(@as(usize, 390 * 844 * 4), pixel_info.byte_len);
908
+ const pixels = try std.testing.allocator.alloc(u8, pixel_info.byte_len);
909
+ defer std.testing.allocator.free(pixels);
910
+ var rendered: MobileCanvasPixels = .{};
911
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_render_pixels(app, 1, pixels.ptr, pixels.len, &rendered));
912
+ try std.testing.expectEqual(pixel_info.byte_len, rendered.byte_len);
913
+ var nonblank_pixels = false;
914
+ for (pixels) |byte| {
915
+ if (byte != 0) {
916
+ nonblank_pixels = true;
917
+ break;
918
+ }
919
+ }
920
+ try std.testing.expect(nonblank_pixels);
921
+
922
+ // Before any input lands nothing is focused: the platform keyboard
923
+ // must stay hidden.
924
+ var text_input: MobileTextInputState = .{};
925
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_text_input_state(app, &text_input));
926
+ try std.testing.expectEqual(@as(c_int, 0), text_input.active);
927
+ try std.testing.expectEqual(@as(u64, 0), text_input.widget_id);
928
+
929
+ // A tap on the button (located through the semantics exports) flows
930
+ // through typed dispatch into update + rebuild.
931
+ const button = try findMobileSemanticsByRole(app, .button);
932
+ try std.testing.expect(button.width > 0 and button.height > 0);
933
+ try tapMobileWidget(app, button);
934
+ try std.testing.expectEqual(@as(u32, 1), self.ui.model.count);
935
+ try std.testing.expect(try uiHostRetainedTextExists(self, "Count 1"));
936
+ try std.testing.expectEqual(button.id, (try findMobileSemanticsByRole(app, .button)).id);
937
+
938
+ // The button takes focus but is not editable text: still no keyboard.
939
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_text_input_state(app, &text_input));
940
+ try std.testing.expectEqual(@as(c_int, 0), text_input.active);
941
+ try std.testing.expectEqual(button.id, text_input.widget_id);
942
+
943
+ // Tap the textbox to focus it, then type and compose through the same
944
+ // IME path desktop uses; the edits land in the model's text buffer.
945
+ const textbox = try findMobileSemanticsByRole(app, .textbox);
946
+ try tapMobileWidget(app, textbox);
947
+
948
+ // Textbox focus is IME intent: the state the shim keys the system
949
+ // keyboard's show/hide on, with the widget's frame for caret tracking.
950
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_text_input_state(app, &text_input));
951
+ try std.testing.expectEqual(@as(c_int, 1), text_input.active);
952
+ try std.testing.expectEqual(textbox.id, text_input.widget_id);
953
+ try std.testing.expectEqual(textbox.x, text_input.x);
954
+ try std.testing.expectEqual(textbox.y, text_input.y);
955
+ try std.testing.expectEqual(textbox.width, text_input.width);
956
+ try std.testing.expectEqual(textbox.height, text_input.height);
957
+ try std.testing.expectEqual(@as(c_int, 0), MobileCounterApi.native_sdk_app_text_input_state(app, null));
958
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(MobileCounterApi.native_sdk_app_last_error_name(app)));
959
+
960
+ MobileCounterApi.native_sdk_app_text(app, "hi", "hi".len);
961
+ try expectNoUiHostError(app);
962
+ try std.testing.expectEqualStrings("hi", self.ui.model.draft.text());
963
+ MobileCounterApi.native_sdk_app_ime(app, 0, "ho", "ho".len, "ho".len);
964
+ try expectNoUiHostError(app);
965
+ MobileCounterApi.native_sdk_app_ime(app, 1, "ho", "ho".len, -1);
966
+ try expectNoUiHostError(app);
967
+ try std.testing.expectEqualStrings("hiho", self.ui.model.draft.text());
968
+ var textbox_after: MobileWidgetSemantics = .{};
969
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_by_id(app, textbox.id, &textbox_after));
970
+ try std.testing.expectEqualStrings("hiho", textbox_after.text.?[0..textbox_after.text_len]);
971
+
972
+ // Tapping the (non-editable) button again moves focus away from the
973
+ // textbox: IME intent clears and the shim hides the keyboard.
974
+ try tapMobileWidget(app, button);
975
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_text_input_state(app, &text_input));
976
+ try std.testing.expectEqual(@as(c_int, 0), text_input.active);
977
+ try std.testing.expectEqual(button.id, text_input.widget_id);
978
+
979
+ // The model-driven UI keeps presenting through host-pumped frames.
980
+ MobileCounterApi.native_sdk_app_frame(app);
981
+ try expectNoUiHostError(app);
982
+ try std.testing.expect(self.null_platform.gpu_surface_present_count >= 2);
983
+ }
984
+
985
+ test "mobile UiApp host insets widget layout by safe-area and keyboard viewport chrome" {
986
+ const app = MobileCounterApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
987
+ defer MobileCounterApi.native_sdk_app_destroy(app);
988
+
989
+ MobileCounterApi.native_sdk_app_start(app);
990
+ try expectNoUiHostError(app);
991
+
992
+ // Portrait with a notch and home indicator: widget layout starts below
993
+ // the top inset and ends above the bottom one while the canvas itself
994
+ // keeps the full surface size (chrome/clear paint edge to edge).
995
+ var surface_token: u8 = 0;
996
+ MobileCounterApi.native_sdk_app_viewport(app, 390, 844, 3, &surface_token, 59, 0, 34, 0, 0, 0, 0, 0);
997
+ MobileCounterApi.native_sdk_app_frame(app);
998
+ try expectNoUiHostError(app);
999
+
1000
+ var root: MobileWidgetSemantics = .{};
1001
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_at(app, 0, &root));
1002
+ try std.testing.expectEqual(@as(f32, 0), root.x);
1003
+ try std.testing.expectEqual(@as(f32, 59), root.y);
1004
+ try std.testing.expectEqual(@as(f32, 390), root.width);
1005
+ try std.testing.expectEqual(@as(f32, 844 - 59 - 34), root.height);
1006
+ const button = try findMobileSemanticsByRole(app, .button);
1007
+ try std.testing.expect(button.y >= 59);
1008
+
1009
+ // The canvas stays surface-sized and device pixels honor the viewport
1010
+ // scale end to end: 390x844 points at 3x renders 1170x2532 pixels.
1011
+ var frame_state: MobileGpuFrameState = .{};
1012
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_gpu_frame_state(app, &frame_state));
1013
+ try std.testing.expectEqual(@as(f32, 390), frame_state.width);
1014
+ try std.testing.expectEqual(@as(f32, 844), frame_state.height);
1015
+ try std.testing.expectEqual(@as(f32, 3), frame_state.scale);
1016
+ var pixel_info: MobileCanvasPixels = .{};
1017
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_render_pixel_size(app, 3, &pixel_info));
1018
+ try std.testing.expectEqual(@as(usize, 1170), pixel_info.width);
1019
+ try std.testing.expectEqual(@as(usize, 2532), pixel_info.height);
1020
+
1021
+ // Rotation: landscape swaps the size and moves the notch to the sides;
1022
+ // the viewport resize relayouts against the new insets.
1023
+ MobileCounterApi.native_sdk_app_viewport(app, 844, 390, 3, &surface_token, 0, 59, 21, 59, 0, 0, 0, 0);
1024
+ MobileCounterApi.native_sdk_app_frame(app);
1025
+ try expectNoUiHostError(app);
1026
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_at(app, 0, &root));
1027
+ try std.testing.expectEqual(@as(f32, 59), root.x);
1028
+ try std.testing.expectEqual(@as(f32, 0), root.y);
1029
+ try std.testing.expectEqual(@as(f32, 844 - 59 - 59), root.width);
1030
+ try std.testing.expectEqual(@as(f32, 390 - 21), root.height);
1031
+
1032
+ // System keyboard: its inset combines edge-wise with the safe areas so
1033
+ // content is laid out above it.
1034
+ MobileCounterApi.native_sdk_app_viewport(app, 390, 844, 3, &surface_token, 59, 0, 34, 0, 0, 0, 336, 0);
1035
+ MobileCounterApi.native_sdk_app_frame(app);
1036
+ try expectNoUiHostError(app);
1037
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_at(app, 0, &root));
1038
+ try std.testing.expectEqual(@as(f32, 59), root.y);
1039
+ try std.testing.expectEqual(@as(f32, 844 - 59 - 336), root.height);
1040
+
1041
+ // Removing the insets restores the desktop-identical full-bounds
1042
+ // layout (desktop surfaces report zero insets, so this is the layout
1043
+ // golden tests cover).
1044
+ MobileCounterApi.native_sdk_app_viewport(app, 390, 844, 3, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1045
+ MobileCounterApi.native_sdk_app_frame(app);
1046
+ try expectNoUiHostError(app);
1047
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_at(app, 0, &root));
1048
+ try std.testing.expectEqual(@as(f32, 0), root.x);
1049
+ try std.testing.expectEqual(@as(f32, 0), root.y);
1050
+ try std.testing.expectEqual(@as(f32, 390), root.width);
1051
+ try std.testing.expectEqual(@as(f32, 844), root.height);
1052
+ }
1053
+
1054
+ // Chrome-subscribed app: `on_chrome` maps the window-chrome channel into
1055
+ // the model, and the view pads by the delivered insets — the identical
1056
+ // code path a desktop hidden-titlebar app pads its header with.
1057
+ const MobileChromeDef = struct {
1058
+ pub const Model = struct {
1059
+ chrome: platform.WindowChrome = .{},
1060
+ chrome_deliveries: u32 = 0,
1061
+ };
1062
+
1063
+ pub const Msg = union(enum) {
1064
+ chrome_changed: platform.WindowChrome,
1065
+ };
1066
+
1067
+ const App = runtime.UiApp(Model, Msg);
1068
+
1069
+ pub fn initModel() Model {
1070
+ return .{};
1071
+ }
1072
+
1073
+ pub fn mobileOptions() App.Options {
1074
+ return .{
1075
+ .name = "mobile-chrome",
1076
+ .scene = ui_host.mobile_shell_scene,
1077
+ .canvas_label = mobile_gpu_surface_label,
1078
+ .update = update,
1079
+ .view = view,
1080
+ .on_chrome = onChrome,
1081
+ };
1082
+ }
1083
+
1084
+ fn onChrome(chrome: platform.WindowChrome) ?Msg {
1085
+ return .{ .chrome_changed = chrome };
1086
+ }
1087
+
1088
+ fn update(model: *Model, msg: Msg) void {
1089
+ switch (msg) {
1090
+ .chrome_changed => |chrome| {
1091
+ model.chrome = chrome;
1092
+ model.chrome_deliveries += 1;
1093
+ },
1094
+ }
1095
+ }
1096
+
1097
+ fn view(ui: *App.Ui, model: *const Model) App.Ui.Node {
1098
+ // Fixed-height bands sized by the delivered chrome stand in for
1099
+ // the safe-area padding a real app derives the same way.
1100
+ return ui.column(.{}, .{
1101
+ ui.el(.column, .{ .height = model.chrome.insets.top }, .{}),
1102
+ ui.text(.{}, "Safe content"),
1103
+ ui.spacer(1),
1104
+ ui.el(.column, .{ .height = model.chrome.insets.bottom }, .{}),
1105
+ });
1106
+ }
1107
+ };
1108
+
1109
+ const MobileChromeHost = ui_host.UiAppHost(MobileChromeDef);
1110
+ const MobileChromeApi = c_api.MobileCApi(MobileChromeHost);
1111
+
1112
+ test "mobile UiApp host delivers safe areas through the window-chrome channel" {
1113
+ const app = MobileChromeApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1114
+ defer MobileChromeApi.native_sdk_app_destroy(app);
1115
+ const self: *MobileChromeHost = @ptrCast(@alignCast(app));
1116
+
1117
+ MobileChromeApi.native_sdk_app_start(app);
1118
+ try std.testing.expectEqualStrings("", std.mem.span(MobileChromeApi.native_sdk_app_last_error_name(app)));
1119
+
1120
+ // Portrait with a notch and home indicator: the chrome Msg arrives
1121
+ // before the first view build carrying exactly the safe-area insets,
1122
+ // and — because the app subscribed — widget layout keeps the FULL
1123
+ // surface bounds (the app's own padding is the inset now).
1124
+ var surface_token: u8 = 0;
1125
+ MobileChromeApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 59, 0, 34, 0, 0, 0, 0, 0);
1126
+ MobileChromeApi.native_sdk_app_frame(app);
1127
+ try std.testing.expectEqualStrings("", std.mem.span(MobileChromeApi.native_sdk_app_last_error_name(app)));
1128
+ try std.testing.expectEqual(@as(f32, 59), self.ui.model.chrome.insets.top);
1129
+ try std.testing.expectEqual(@as(f32, 34), self.ui.model.chrome.insets.bottom);
1130
+ try std.testing.expectEqual(@as(f32, 0), self.ui.model.chrome.insets.left);
1131
+ const deliveries_after_install = self.ui.model.chrome_deliveries;
1132
+ try std.testing.expect(deliveries_after_install >= 1);
1133
+
1134
+ var root: MobileWidgetSemantics = .{};
1135
+ try std.testing.expectEqual(@as(c_int, 1), MobileChromeApi.native_sdk_app_widget_semantics_at(app, 0, &root));
1136
+ try std.testing.expectEqual(@as(f32, 0), root.x);
1137
+ try std.testing.expectEqual(@as(f32, 0), root.y);
1138
+ try std.testing.expectEqual(@as(f32, 390), root.width);
1139
+ try std.testing.expectEqual(@as(f32, 844), root.height);
1140
+
1141
+ // The view's model-derived padding places content below the notch —
1142
+ // the app-owned equivalent of the unsubscribed automatic inset.
1143
+ const text = try mobileChromeSemanticsByRole(app, .text);
1144
+ try std.testing.expect(text.y >= 59);
1145
+
1146
+ // An identical viewport re-report does not re-deliver (change-gated,
1147
+ // same dedupe the macOS fullscreen transitions rely on).
1148
+ MobileChromeApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 59, 0, 34, 0, 0, 0, 0, 0);
1149
+ MobileChromeApi.native_sdk_app_frame(app);
1150
+ try std.testing.expectEqual(deliveries_after_install, self.ui.model.chrome_deliveries);
1151
+
1152
+ // The keyboard is not chrome: it must not change the report, and the
1153
+ // runtime keeps insetting layout by its residual overlap beyond the
1154
+ // app-owned safe area (336 keyboard - 34 home indicator = 302).
1155
+ MobileChromeApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 59, 0, 34, 0, 0, 0, 336, 0);
1156
+ MobileChromeApi.native_sdk_app_frame(app);
1157
+ try std.testing.expectEqual(@as(f32, 34), self.ui.model.chrome.insets.bottom);
1158
+ try std.testing.expectEqual(deliveries_after_install, self.ui.model.chrome_deliveries);
1159
+ try std.testing.expectEqual(@as(c_int, 1), MobileChromeApi.native_sdk_app_widget_semantics_at(app, 0, &root));
1160
+ try std.testing.expectEqual(@as(f32, 0), root.y);
1161
+ try std.testing.expectEqual(@as(f32, 844 - 302), root.height);
1162
+
1163
+ // Rotation moves the notch to the sides: one new delivery with the
1164
+ // landscape insets.
1165
+ MobileChromeApi.native_sdk_app_viewport(app, 844, 390, 1, &surface_token, 0, 59, 21, 59, 0, 0, 0, 0);
1166
+ MobileChromeApi.native_sdk_app_frame(app);
1167
+ try std.testing.expectEqual(deliveries_after_install + 1, self.ui.model.chrome_deliveries);
1168
+ try std.testing.expectEqual(@as(f32, 0), self.ui.model.chrome.insets.top);
1169
+ try std.testing.expectEqual(@as(f32, 59), self.ui.model.chrome.insets.left);
1170
+ try std.testing.expectEqual(@as(f32, 59), self.ui.model.chrome.insets.right);
1171
+ try std.testing.expectEqual(@as(f32, 21), self.ui.model.chrome.insets.bottom);
1172
+ }
1173
+
1174
+ test "android-shaped insets ride the same viewport and chrome contracts" {
1175
+ // The chrome channel and keyboard residual are host-agnostic: the
1176
+ // Android host reports status-bar/cutout/gesture-nav bands as the
1177
+ // safe area and the IME inset as the keyboard, in density-independent
1178
+ // points, exactly like the iOS host reports notch and home-indicator
1179
+ // bands — pinned here with Android-shaped geometry (412x915 @2.625,
1180
+ // 28pt status band over a cutout, 24pt gesture nav, 322pt IME).
1181
+ var surface_token: u8 = 0;
1182
+
1183
+ // Unsubscribed app: the automatic runtime inset keeps layout clear of
1184
+ // the bands and combines the keyboard edge-wise, byte-identically to
1185
+ // the iOS-shaped run above.
1186
+ const counter_app = MobileCounterApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1187
+ defer MobileCounterApi.native_sdk_app_destroy(counter_app);
1188
+ MobileCounterApi.native_sdk_app_start(counter_app);
1189
+ MobileCounterApi.native_sdk_app_viewport(counter_app, 412, 915, 2.625, &surface_token, 28, 0, 24, 0, 0, 0, 0, 0);
1190
+ MobileCounterApi.native_sdk_app_frame(counter_app);
1191
+ try expectNoUiHostError(counter_app);
1192
+ var root: MobileWidgetSemantics = .{};
1193
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_at(counter_app, 0, &root));
1194
+ try std.testing.expectEqual(@as(f32, 28), root.y);
1195
+ try std.testing.expectEqual(@as(f32, 915 - 28 - 24), root.height);
1196
+ MobileCounterApi.native_sdk_app_viewport(counter_app, 412, 915, 2.625, &surface_token, 28, 0, 24, 0, 0, 0, 322, 0);
1197
+ MobileCounterApi.native_sdk_app_frame(counter_app);
1198
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_widget_semantics_at(counter_app, 0, &root));
1199
+ try std.testing.expectEqual(@as(f32, 915 - 28 - 322), root.height);
1200
+
1201
+ // Chrome-subscribed app: the safe area arrives over the window-chrome
1202
+ // channel, the IME stays out of the report, and the runtime insets
1203
+ // layout by the keyboard's residual overlap beyond the app-owned
1204
+ // gesture-nav band (322 - 24 = 298).
1205
+ const chrome_app = MobileChromeApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1206
+ defer MobileChromeApi.native_sdk_app_destroy(chrome_app);
1207
+ const chrome_self: *MobileChromeHost = @ptrCast(@alignCast(chrome_app));
1208
+ MobileChromeApi.native_sdk_app_start(chrome_app);
1209
+ MobileChromeApi.native_sdk_app_viewport(chrome_app, 412, 915, 2.625, &surface_token, 28, 0, 24, 0, 0, 0, 322, 0);
1210
+ MobileChromeApi.native_sdk_app_frame(chrome_app);
1211
+ try std.testing.expectEqual(@as(f32, 28), chrome_self.ui.model.chrome.insets.top);
1212
+ try std.testing.expectEqual(@as(f32, 24), chrome_self.ui.model.chrome.insets.bottom);
1213
+ try std.testing.expectEqual(@as(c_int, 1), MobileChromeApi.native_sdk_app_widget_semantics_at(chrome_app, 0, &root));
1214
+ try std.testing.expectEqual(@as(f32, 0), root.y);
1215
+ try std.testing.expectEqual(@as(f32, 915 - 298), root.height);
1216
+
1217
+ // Rotation with a corner cutout: landscape moves the cutout band to
1218
+ // one side only (Android reports asymmetric cutout insets).
1219
+ MobileChromeApi.native_sdk_app_viewport(chrome_app, 915, 412, 2.625, &surface_token, 0, 0, 24, 28, 0, 0, 0, 0);
1220
+ MobileChromeApi.native_sdk_app_frame(chrome_app);
1221
+ try std.testing.expectEqual(@as(f32, 28), chrome_self.ui.model.chrome.insets.left);
1222
+ try std.testing.expectEqual(@as(f32, 0), chrome_self.ui.model.chrome.insets.right);
1223
+ try std.testing.expectEqual(@as(f32, 24), chrome_self.ui.model.chrome.insets.bottom);
1224
+ }
1225
+
1226
+ fn mobileChromeSemanticsByRole(app: ?*anyopaque, role: MobileWidgetRole) !MobileWidgetSemantics {
1227
+ const count = MobileChromeApi.native_sdk_app_widget_semantics_count(app);
1228
+ var index: usize = 0;
1229
+ while (index < count) : (index += 1) {
1230
+ var node: MobileWidgetSemantics = .{};
1231
+ try std.testing.expectEqual(@as(c_int, 1), MobileChromeApi.native_sdk_app_widget_semantics_at(app, index, &node));
1232
+ if (node.role == @intFromEnum(role)) return node;
1233
+ }
1234
+ return error.TestUnexpectedResult;
1235
+ }
1236
+
1237
+ /// Fake shim text measurement: every UTF-8 byte is `size` wide — far wider
1238
+ /// than any estimator advance factor (max 0.91), so measured layout
1239
+ /// visibly diverges from the estimator baseline. Counts calls through the
1240
+ /// context pointer to prove the provider (not the estimator) measured.
1241
+ fn fakeMobileMeasureText(context: ?*anyopaque, font_id: u64, size: f64, text: ?[*]const u8, text_len: usize) callconv(.c) f64 {
1242
+ _ = font_id;
1243
+ _ = text;
1244
+ const calls: *usize = @ptrCast(@alignCast(context.?));
1245
+ calls.* += 1;
1246
+ return size * @as(f64, @floatFromInt(text_len));
1247
+ }
1248
+
1249
+ // Row-based view: a row child's main-axis extent is its intrinsic width,
1250
+ // which goes through the tokens' text-measure seam — so a registered
1251
+ // provider directly changes the text widget's laid-out bounds.
1252
+ const MobileMeasureDef = struct {
1253
+ pub const Model = struct { pressed: u32 = 0 };
1254
+
1255
+ pub const Msg = union(enum) { press };
1256
+
1257
+ const App = runtime.UiApp(Model, Msg);
1258
+
1259
+ pub fn initModel() Model {
1260
+ return .{};
1261
+ }
1262
+
1263
+ pub fn mobileOptions() App.Options {
1264
+ return .{
1265
+ .name = "mobile-measure",
1266
+ .scene = ui_host.mobile_shell_scene,
1267
+ .canvas_label = mobile_gpu_surface_label,
1268
+ .update = update,
1269
+ .view = view,
1270
+ };
1271
+ }
1272
+
1273
+ fn update(model: *Model, msg: Msg) void {
1274
+ switch (msg) {
1275
+ .press => model.pressed += 1,
1276
+ }
1277
+ }
1278
+
1279
+ fn view(ui: *App.Ui, model: *const Model) App.Ui.Node {
1280
+ _ = model;
1281
+ return ui.row(.{ .gap = 8, .padding = 12 }, .{
1282
+ ui.text(.{}, "Measured run"),
1283
+ ui.button(.{ .on_press = .press }, "OK"),
1284
+ });
1285
+ }
1286
+ };
1287
+
1288
+ const MobileMeasureApi = c_api.MobileCApi(ui_host.UiAppHost(MobileMeasureDef));
1289
+
1290
+ fn measureTestTextWidth(app: ?*anyopaque) !f32 {
1291
+ const count = MobileMeasureApi.native_sdk_app_widget_semantics_count(app);
1292
+ var index: usize = 0;
1293
+ while (index < count) : (index += 1) {
1294
+ var node: MobileWidgetSemantics = .{};
1295
+ try std.testing.expectEqual(@as(c_int, 1), MobileMeasureApi.native_sdk_app_widget_semantics_at(app, index, &node));
1296
+ if (node.role == @intFromEnum(MobileWidgetRole.text)) return node.width;
1297
+ }
1298
+ return error.TestUnexpectedResult;
1299
+ }
1300
+
1301
+ test "mobile C ABI text measure provider changes embed text layout" {
1302
+ var surface_token: u8 = 0;
1303
+
1304
+ // Baseline: no provider, deterministic estimator metrics.
1305
+ const baseline_app = MobileMeasureApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1306
+ MobileMeasureApi.native_sdk_app_start(baseline_app);
1307
+ MobileMeasureApi.native_sdk_app_viewport(baseline_app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1308
+ MobileMeasureApi.native_sdk_app_frame(baseline_app);
1309
+ const baseline_width = try measureTestTextWidth(baseline_app);
1310
+ try std.testing.expect(baseline_width > 0);
1311
+ MobileMeasureApi.native_sdk_app_destroy(baseline_app);
1312
+
1313
+ // Registering a measure callback before start makes the installing
1314
+ // layout measure through it: the text widget's bounds change.
1315
+ const app = MobileMeasureApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1316
+ defer MobileMeasureApi.native_sdk_app_destroy(app);
1317
+ const self: *ui_host.UiAppHost(MobileMeasureDef) = @ptrCast(@alignCast(app));
1318
+ var measure_calls: usize = 0;
1319
+ try std.testing.expectEqual(@as(c_int, 1), MobileMeasureApi.native_sdk_app_set_text_measure(app, fakeMobileMeasureText, &measure_calls));
1320
+ try std.testing.expect(self.embedded.runtime.textMeasureProvider() != null);
1321
+ MobileMeasureApi.native_sdk_app_start(app);
1322
+ MobileMeasureApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1323
+ MobileMeasureApi.native_sdk_app_frame(app);
1324
+ try std.testing.expect(measure_calls > 0);
1325
+ const measured_width = try measureTestTextWidth(app);
1326
+ try std.testing.expect(measured_width > baseline_width);
1327
+ // "Measured run" is 12 bytes at one `size` per byte; the estimator's
1328
+ // widest advance factor is 0.91, so the fake at least ~10% wider.
1329
+ try std.testing.expect(measured_width >= baseline_width * 1.1);
1330
+
1331
+ // Clearing the callback falls back to the estimator on the next
1332
+ // rebuild (a viewport resize here): baseline layout returns. The
1333
+ // runtime-side provider stays installed — retained display-list
1334
+ // commands carry its pointer for the runtime's lifetime — but the
1335
+ // bridge reports no measurement, which is the estimator path.
1336
+ try std.testing.expectEqual(@as(c_int, 1), MobileMeasureApi.native_sdk_app_set_text_measure(app, null, null));
1337
+ try std.testing.expect(self.embedded.runtime.textMeasureProvider() != null);
1338
+ MobileMeasureApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1339
+ MobileMeasureApi.native_sdk_app_frame(app);
1340
+ const restored_width = try measureTestTextWidth(app);
1341
+ try std.testing.expectEqual(baseline_width, restored_width);
1342
+ }
1343
+
1344
+ test "mobile C ABI publishes automation snapshots into a host-set directory" {
1345
+ const app = MobileCounterApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1346
+ defer MobileCounterApi.native_sdk_app_destroy(app);
1347
+ const self: *MobileCounterHost = @ptrCast(@alignCast(app));
1348
+
1349
+ const dir = ".zig-cache/test-mobile-embed-automation";
1350
+ std.Io.Dir.cwd().deleteTree(std.testing.io, dir) catch {};
1351
+ defer std.Io.Dir.cwd().deleteTree(std.testing.io, dir) catch {};
1352
+
1353
+ MobileCounterApi.native_sdk_app_start(app);
1354
+ try expectNoUiHostError(app);
1355
+ try std.testing.expectEqual(@as(c_int, 0), MobileCounterApi.native_sdk_app_set_automation_dir(app, "", 0));
1356
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(MobileCounterApi.native_sdk_app_last_error_name(app)));
1357
+ try std.testing.expectEqual(@as(c_int, 1), MobileCounterApi.native_sdk_app_set_automation_dir(app, dir, dir.len));
1358
+ try expectNoUiHostError(app);
1359
+ try std.testing.expect(self.embedded.runtime.options.automation != null);
1360
+
1361
+ var surface_token: u8 = 0;
1362
+ MobileCounterApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1363
+ MobileCounterApi.native_sdk_app_frame(app);
1364
+ try expectNoUiHostError(app);
1365
+
1366
+ var buffer: [16 * 1024]u8 = undefined;
1367
+ var file = try std.Io.Dir.cwd().openFile(std.testing.io, dir ++ "/snapshot.txt", .{});
1368
+ defer file.close(std.testing.io);
1369
+ const snapshot = buffer[0..try file.readPositionalAll(std.testing.io, &buffer, 0)];
1370
+ try std.testing.expect(std.mem.indexOf(u8, snapshot, "mobile-surface") != null);
1371
+ try std.testing.expect(std.mem.indexOf(u8, snapshot, "Increment") != null);
1372
+ }
1373
+
1374
+ test "mobile C ABI dispatches native commands through embedded runtime" {
1375
+ const app = native_sdk_app_create() orelse return error.TestUnexpectedResult;
1376
+ defer native_sdk_app_destroy(app);
1377
+
1378
+ native_sdk_app_command(app, "mobile.refresh", "mobile.refresh".len);
1379
+ try std.testing.expectEqual(@as(usize, 1), native_sdk_app_last_command_count(app));
1380
+ try std.testing.expectEqualStrings("mobile.refresh", std.mem.span(native_sdk_app_last_command_name(app)));
1381
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
1382
+
1383
+ native_sdk_app_command(app, "", 0);
1384
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(native_sdk_app_last_error_name(app)));
1385
+
1386
+ native_sdk_app_command(app, "mobile.open", "mobile.open".len);
1387
+ try std.testing.expectEqual(@as(usize, 2), native_sdk_app_last_command_count(app));
1388
+ try std.testing.expectEqualStrings("mobile.open", std.mem.span(native_sdk_app_last_command_name(app)));
1389
+ try std.testing.expectEqualStrings("", std.mem.span(native_sdk_app_last_error_name(app)));
1390
+ }
1391
+
1392
+ // ------------------------------------------------------------ audio service
1393
+ //
1394
+ // The embed audio seam end to end: hosts decline audio until the shim
1395
+ // registers a real service (`native_sdk_app_set_audio_service`), a
1396
+ // registered callback table receives the fx.playAudio family through the
1397
+ // same PlatformServices seam AVAudioPlayer serves on macOS, and shim
1398
+ // reports (`native_sdk_app_audio_event`) land as the app's `on_event` Msg
1399
+ // with the runtime's playback mirrors updated. A position report with
1400
+ // playing=0 is the exact shape the iOS host emits from an audio-session
1401
+ // interruption, so its state flip is pinned here.
1402
+
1403
+ const MobileAudioDef = struct {
1404
+ pub const Model = struct {
1405
+ event_count: usize = 0,
1406
+ last_kind: ?runtime.EffectAudioEventKind = null,
1407
+ last_position_ms: u64 = 0,
1408
+ last_duration_ms: u64 = 0,
1409
+ last_playing: bool = false,
1410
+ last_buffering: bool = false,
1411
+ };
1412
+
1413
+ pub const Msg = union(enum) {
1414
+ play,
1415
+ play_stream,
1416
+ pause,
1417
+ seek,
1418
+ quiet,
1419
+ audio_event: runtime.EffectAudio,
1420
+ };
1421
+
1422
+ const App = runtime.UiApp(Model, Msg);
1423
+
1424
+ pub fn initModel() Model {
1425
+ return .{};
1426
+ }
1427
+
1428
+ pub fn mobileOptions() App.Options {
1429
+ return .{
1430
+ .name = "mobile-audio",
1431
+ .scene = ui_host.mobile_shell_scene,
1432
+ .canvas_label = mobile_gpu_surface_label,
1433
+ .update_fx = update,
1434
+ .view = view,
1435
+ };
1436
+ }
1437
+
1438
+ fn update(model: *Model, msg: Msg, fx: *App.Effects) void {
1439
+ switch (msg) {
1440
+ .play => fx.playAudio(.{
1441
+ .key = 7,
1442
+ .path = "/tmp/mobile-audio-track.mp3",
1443
+ .on_event = App.Effects.audioMsg(.audio_event),
1444
+ }),
1445
+ .play_stream => fx.playAudio(.{
1446
+ .key = 8,
1447
+ .url = "https://music.example.test/pack/track.mp3",
1448
+ .cache_path = "/tmp/mobile-audio-caches/audio/track.mp3",
1449
+ .expected_bytes = 2_048,
1450
+ .on_event = App.Effects.audioMsg(.audio_event),
1451
+ }),
1452
+ .pause => fx.pauseAudio(),
1453
+ .seek => fx.seekAudio(12_000),
1454
+ .quiet => fx.setAudioVolume(0.25),
1455
+ .audio_event => |event| {
1456
+ model.event_count += 1;
1457
+ model.last_kind = event.kind;
1458
+ model.last_position_ms = event.position_ms;
1459
+ model.last_duration_ms = event.duration_ms;
1460
+ model.last_playing = event.playing;
1461
+ model.last_buffering = event.buffering;
1462
+ },
1463
+ }
1464
+ }
1465
+
1466
+ fn view(ui: *App.Ui, model: *const Model) App.Ui.Node {
1467
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
1468
+ ui.text(.{}, ui.fmt("{d} events", .{model.event_count})),
1469
+ ui.button(.{ .on_press = .play }, "Play"),
1470
+ ui.button(.{ .on_press = .play_stream }, "Stream"),
1471
+ ui.button(.{ .on_press = .pause }, "Pause"),
1472
+ ui.button(.{ .on_press = .seek }, "Seek"),
1473
+ ui.button(.{ .on_press = .quiet }, "Quiet"),
1474
+ });
1475
+ }
1476
+ };
1477
+
1478
+ const MobileAudioHost = ui_host.UiAppHost(MobileAudioDef);
1479
+ const MobileAudioApi = c_api.MobileCApi(MobileAudioHost);
1480
+
1481
+ /// What the fake shim service records — the mobile mirror of the null
1482
+ /// platform's call counters, held by the test and reached through the
1483
+ /// registered context pointer.
1484
+ const MobileAudioRecorder = struct {
1485
+ load_count: usize = 0,
1486
+ load_url_count: usize = 0,
1487
+ play_count: usize = 0,
1488
+ pause_count: usize = 0,
1489
+ stop_count: usize = 0,
1490
+ seek_count: usize = 0,
1491
+ volume_count: usize = 0,
1492
+ last_path: [256]u8 = undefined,
1493
+ last_path_len: usize = 0,
1494
+ last_url: [256]u8 = undefined,
1495
+ last_url_len: usize = 0,
1496
+ last_cache_path: [256]u8 = undefined,
1497
+ last_cache_path_len: usize = 0,
1498
+ last_expected_bytes: u64 = 0,
1499
+ last_seek_ms: u64 = 0,
1500
+ last_volume: f64 = 1.0,
1501
+ load_result: c_int = 0,
1502
+ load_url_result: c_int = 0,
1503
+
1504
+ fn from(context: ?*anyopaque) *MobileAudioRecorder {
1505
+ return @ptrCast(@alignCast(context.?));
1506
+ }
1507
+ };
1508
+
1509
+ fn recorderAudioLoad(context: ?*anyopaque, path: ?[*]const u8, path_len: usize) callconv(.c) c_int {
1510
+ const recorder = MobileAudioRecorder.from(context);
1511
+ recorder.load_count += 1;
1512
+ recorder.last_path_len = conversions.copyInputText(&recorder.last_path, if (path) |value| value[0..path_len] else "");
1513
+ return recorder.load_result;
1514
+ }
1515
+
1516
+ fn recorderAudioLoadUrl(context: ?*anyopaque, url: ?[*]const u8, url_len: usize, cache_path: ?[*]const u8, cache_path_len: usize, expected_bytes: u64) callconv(.c) c_int {
1517
+ const recorder = MobileAudioRecorder.from(context);
1518
+ recorder.load_url_count += 1;
1519
+ recorder.last_url_len = conversions.copyInputText(&recorder.last_url, if (url) |value| value[0..url_len] else "");
1520
+ recorder.last_cache_path_len = conversions.copyInputText(&recorder.last_cache_path, if (cache_path) |value| value[0..cache_path_len] else "");
1521
+ recorder.last_expected_bytes = expected_bytes;
1522
+ return recorder.load_url_result;
1523
+ }
1524
+
1525
+ fn recorderAudioPlay(context: ?*anyopaque) callconv(.c) c_int {
1526
+ MobileAudioRecorder.from(context).play_count += 1;
1527
+ return 1;
1528
+ }
1529
+
1530
+ fn recorderAudioPause(context: ?*anyopaque) callconv(.c) c_int {
1531
+ MobileAudioRecorder.from(context).pause_count += 1;
1532
+ return 1;
1533
+ }
1534
+
1535
+ fn recorderAudioStop(context: ?*anyopaque) callconv(.c) c_int {
1536
+ MobileAudioRecorder.from(context).stop_count += 1;
1537
+ return 1;
1538
+ }
1539
+
1540
+ fn recorderAudioSeek(context: ?*anyopaque, position_ms: u64) callconv(.c) c_int {
1541
+ const recorder = MobileAudioRecorder.from(context);
1542
+ recorder.seek_count += 1;
1543
+ recorder.last_seek_ms = position_ms;
1544
+ return 1;
1545
+ }
1546
+
1547
+ fn recorderAudioSetVolume(context: ?*anyopaque, volume: f64) callconv(.c) c_int {
1548
+ const recorder = MobileAudioRecorder.from(context);
1549
+ recorder.volume_count += 1;
1550
+ recorder.last_volume = volume;
1551
+ return 1;
1552
+ }
1553
+
1554
+ fn recorderAudioService() types.MobileAudioService {
1555
+ return .{
1556
+ .load = recorderAudioLoad,
1557
+ .load_url = recorderAudioLoadUrl,
1558
+ .play = recorderAudioPlay,
1559
+ .pause = recorderAudioPause,
1560
+ .stop = recorderAudioStop,
1561
+ .seek = recorderAudioSeek,
1562
+ .set_volume = recorderAudioSetVolume,
1563
+ };
1564
+ }
1565
+
1566
+ fn pressAudioButton(app: ?*anyopaque, label: []const u8) !void {
1567
+ const count = MobileAudioApi.native_sdk_app_widget_semantics_count(app);
1568
+ var index: usize = 0;
1569
+ while (index < count) : (index += 1) {
1570
+ var node: MobileWidgetSemantics = .{};
1571
+ try std.testing.expectEqual(@as(c_int, 1), MobileAudioApi.native_sdk_app_widget_semantics_at(app, index, &node));
1572
+ if (node.role != @intFromEnum(MobileWidgetRole.button)) continue;
1573
+ const node_label = if (node.label) |ptr| ptr[0..node.label_len] else "";
1574
+ if (!std.mem.eql(u8, node_label, label)) continue;
1575
+ // A synthesized tap at the button's center — the same touch path
1576
+ // the shim forwards — flows through typed dispatch into update.
1577
+ const x = node.x + node.width / 2;
1578
+ const y = node.y + node.height / 2;
1579
+ MobileAudioApi.native_sdk_app_touch(app, 1, 0, x, y, 1);
1580
+ MobileAudioApi.native_sdk_app_touch(app, 1, 1, x, y, 0);
1581
+ try std.testing.expectEqualStrings("", std.mem.span(MobileAudioApi.native_sdk_app_last_error_name(app)));
1582
+ return;
1583
+ }
1584
+ return error.TestUnexpectedResult;
1585
+ }
1586
+
1587
+ fn startAudioHost(app: ?*anyopaque) !void {
1588
+ MobileAudioApi.native_sdk_app_start(app);
1589
+ var surface_token: u8 = 0;
1590
+ MobileAudioApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1591
+ MobileAudioApi.native_sdk_app_frame(app);
1592
+ try std.testing.expectEqualStrings("", std.mem.span(MobileAudioApi.native_sdk_app_last_error_name(app)));
1593
+ }
1594
+
1595
+ test "mobile hosts decline audio until the shim registers a service" {
1596
+ const app = MobileAudioApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1597
+ defer MobileAudioApi.native_sdk_app_destroy(app);
1598
+ const self: *MobileAudioHost = @ptrCast(@alignCast(app));
1599
+
1600
+ // No registered service: the capability answers are honest noes and
1601
+ // the runtime's service table carries no audio entries.
1602
+ try std.testing.expect(!self.embedded.runtime.options.platform.supports(.audio_playback));
1603
+ try std.testing.expect(!self.embedded.runtime.options.platform.supports(.audio_streaming));
1604
+ try std.testing.expect(self.embedded.runtime.options.platform.services.audio_load_fn == null);
1605
+
1606
+ try startAudioHost(app);
1607
+
1608
+ // fx.playAudio degrades to exactly one explicit failed Msg (delivered
1609
+ // on the next effects drain — a pumped frame), never silence.
1610
+ try pressAudioButton(app, "Play");
1611
+ MobileAudioApi.native_sdk_app_frame(app);
1612
+ try std.testing.expectEqual(@as(usize, 1), self.ui.model.event_count);
1613
+ try std.testing.expectEqual(runtime.EffectAudioEventKind.failed, self.ui.model.last_kind.?);
1614
+
1615
+ // Audio events from a shim that never registered still resolve
1616
+ // against an idle channel: swallowed without error, no Msg.
1617
+ MobileAudioApi.native_sdk_app_audio_event(app, 1, 500, 30_000, 1, 0);
1618
+ try std.testing.expectEqual(@as(usize, 1), self.ui.model.event_count);
1619
+ }
1620
+
1621
+ test "mobile audio service bridges effects out and events back" {
1622
+ const app = MobileAudioApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1623
+ defer MobileAudioApi.native_sdk_app_destroy(app);
1624
+ const self: *MobileAudioHost = @ptrCast(@alignCast(app));
1625
+
1626
+ var recorder = MobileAudioRecorder{};
1627
+ const service = recorderAudioService();
1628
+ try std.testing.expectEqual(@as(c_int, 1), MobileAudioApi.native_sdk_app_set_audio_service(app, &service, &recorder));
1629
+ try std.testing.expect(self.embedded.runtime.options.platform.supports(.audio_playback));
1630
+ try std.testing.expect(self.embedded.runtime.options.platform.supports(.audio_streaming));
1631
+
1632
+ try startAudioHost(app);
1633
+
1634
+ // playAudio resolves the local path through the registered load and
1635
+ // starts the transport through play.
1636
+ try pressAudioButton(app, "Play");
1637
+ try std.testing.expectEqual(@as(usize, 1), recorder.load_count);
1638
+ try std.testing.expectEqualStrings("/tmp/mobile-audio-track.mp3", recorder.last_path[0..recorder.last_path_len]);
1639
+ try std.testing.expectEqual(@as(usize, 1), recorder.play_count);
1640
+
1641
+ // The shim's loaded acknowledgment lands as the app's on_event Msg
1642
+ // with the real decoded duration.
1643
+ MobileAudioApi.native_sdk_app_audio_event(app, 0, 0, 30_000, 1, 0);
1644
+ try std.testing.expectEqual(@as(usize, 1), self.ui.model.event_count);
1645
+ try std.testing.expectEqual(runtime.EffectAudioEventKind.loaded, self.ui.model.last_kind.?);
1646
+ try std.testing.expectEqual(@as(u64, 30_000), self.ui.model.last_duration_ms);
1647
+
1648
+ // Position ticks update the model and the runtime's honest mirrors.
1649
+ MobileAudioApi.native_sdk_app_audio_event(app, 1, 1_500, 30_000, 1, 0);
1650
+ try std.testing.expectEqual(runtime.EffectAudioEventKind.position, self.ui.model.last_kind.?);
1651
+ try std.testing.expectEqual(@as(u64, 1_500), self.ui.model.last_position_ms);
1652
+ try std.testing.expect(self.ui.model.last_playing);
1653
+ try std.testing.expect(self.ui.effects.audioSnapshot().playing);
1654
+
1655
+ // A position report with playing=0 is the audio-session interruption
1656
+ // shape the iOS host emits (the OS paused the route): the app and the
1657
+ // playback mirrors flip to paused, honestly, without a pause command.
1658
+ MobileAudioApi.native_sdk_app_audio_event(app, 1, 2_000, 30_000, 0, 0);
1659
+ try std.testing.expect(!self.ui.model.last_playing);
1660
+ try std.testing.expect(!self.ui.effects.audioSnapshot().playing);
1661
+ try std.testing.expectEqual(@as(usize, 0), recorder.pause_count);
1662
+
1663
+ // Transport commands reach the service: pause, seek, volume.
1664
+ try pressAudioButton(app, "Pause");
1665
+ try std.testing.expectEqual(@as(usize, 1), recorder.pause_count);
1666
+ try pressAudioButton(app, "Seek");
1667
+ try std.testing.expectEqual(@as(usize, 1), recorder.seek_count);
1668
+ try std.testing.expectEqual(@as(u64, 12_000), recorder.last_seek_ms);
1669
+ try pressAudioButton(app, "Quiet");
1670
+ try std.testing.expectEqual(@as(usize, 1), recorder.volume_count);
1671
+ try std.testing.expectApproxEqAbs(@as(f64, 0.25), recorder.last_volume, 0.0001);
1672
+
1673
+ // Exactly one completion at natural end, carrying the terminal
1674
+ // position.
1675
+ MobileAudioApi.native_sdk_app_audio_event(app, 2, 30_000, 30_000, 0, 0);
1676
+ try std.testing.expectEqual(runtime.EffectAudioEventKind.completed, self.ui.model.last_kind.?);
1677
+ try std.testing.expectEqual(@as(u64, 30_000), self.ui.model.last_position_ms);
1678
+ try std.testing.expect(!self.ui.effects.audioSnapshot().playing);
1679
+ try std.testing.expectEqual(@as(u64, 30_000), self.ui.effects.audioSnapshot().position_ms);
1680
+
1681
+ // An out-of-range event kind is refused loudly.
1682
+ MobileAudioApi.native_sdk_app_audio_event(app, 9, 0, 0, 0, 0);
1683
+ try std.testing.expectEqualStrings("InvalidAudioOptions", std.mem.span(MobileAudioApi.native_sdk_app_last_error_name(app)));
1684
+ }
1685
+
1686
+ test "mobile audio streams resolve through the registered load_url" {
1687
+ const app = MobileAudioApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1688
+ defer MobileAudioApi.native_sdk_app_destroy(app);
1689
+ const self: *MobileAudioHost = @ptrCast(@alignCast(app));
1690
+
1691
+ var recorder = MobileAudioRecorder{};
1692
+ const service = recorderAudioService();
1693
+ try std.testing.expectEqual(@as(c_int, 1), MobileAudioApi.native_sdk_app_set_audio_service(app, &service, &recorder));
1694
+ try startAudioHost(app);
1695
+
1696
+ // load_url answering 0 is a started stream: buffering starts true
1697
+ // optimistically until the loaded acknowledgment.
1698
+ try pressAudioButton(app, "Stream");
1699
+ try std.testing.expectEqual(@as(usize, 1), recorder.load_url_count);
1700
+ try std.testing.expectEqualStrings("https://music.example.test/pack/track.mp3", recorder.last_url[0..recorder.last_url_len]);
1701
+ try std.testing.expectEqualStrings("/tmp/mobile-audio-caches/audio/track.mp3", recorder.last_cache_path[0..recorder.last_cache_path_len]);
1702
+ try std.testing.expectEqual(@as(u64, 2_048), recorder.last_expected_bytes);
1703
+ try std.testing.expectEqual(runtime.EffectAudioSource.stream, self.ui.effects.audioSnapshot().source);
1704
+ try std.testing.expect(self.ui.effects.audioSnapshot().buffering);
1705
+
1706
+ // load_url answering 1 is a verified cache hit: local playback, no
1707
+ // buffering.
1708
+ recorder.load_url_result = 1;
1709
+ try pressAudioButton(app, "Stream");
1710
+ try std.testing.expectEqual(@as(usize, 2), recorder.load_url_count);
1711
+ try std.testing.expectEqual(runtime.EffectAudioSource.cache, self.ui.effects.audioSnapshot().source);
1712
+ try std.testing.expect(!self.ui.effects.audioSnapshot().buffering);
1713
+ }
1714
+
1715
+ test "mobile audio service registration is all-or-nothing per tier" {
1716
+ const app = MobileAudioApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1717
+ defer MobileAudioApi.native_sdk_app_destroy(app);
1718
+ const self: *MobileAudioHost = @ptrCast(@alignCast(app));
1719
+
1720
+ var recorder = MobileAudioRecorder{};
1721
+
1722
+ // A partial playback table is refused whole; the host keeps declining.
1723
+ var partial = types.MobileAudioService{};
1724
+ partial.load = recorderAudioLoad;
1725
+ partial.play = recorderAudioPlay;
1726
+ try std.testing.expectEqual(@as(c_int, 0), MobileAudioApi.native_sdk_app_set_audio_service(app, &partial, &recorder));
1727
+ try std.testing.expectEqualStrings("InvalidCommand", std.mem.span(MobileAudioApi.native_sdk_app_last_error_name(app)));
1728
+ try std.testing.expect(!self.embedded.runtime.options.platform.supports(.audio_playback));
1729
+
1730
+ // The playback tier without load_url registers playback but declines
1731
+ // streaming.
1732
+ var local_only = recorderAudioService();
1733
+ local_only.load_url = null;
1734
+ try std.testing.expectEqual(@as(c_int, 1), MobileAudioApi.native_sdk_app_set_audio_service(app, &local_only, &recorder));
1735
+ try std.testing.expect(self.embedded.runtime.options.platform.supports(.audio_playback));
1736
+ try std.testing.expect(!self.embedded.runtime.options.platform.supports(.audio_streaming));
1737
+ try std.testing.expect(self.embedded.runtime.options.platform.services.audio_load_url_fn == null);
1738
+
1739
+ // A null (or all-null) table clears the registration: back to the
1740
+ // honest decline.
1741
+ try std.testing.expectEqual(@as(c_int, 1), MobileAudioApi.native_sdk_app_set_audio_service(app, null, null));
1742
+ try std.testing.expect(!self.embedded.runtime.options.platform.supports(.audio_playback));
1743
+ try std.testing.expect(self.embedded.runtime.options.platform.services.audio_load_fn == null);
1744
+ }
1745
+
1746
+ // ------------------------------------------------------------ image service
1747
+ //
1748
+ // The embed image-decode seam end to end: hosts decline
1749
+ // `fx.registerImageBytes` until the shim registers a codec
1750
+ // (`native_sdk_app_set_image_service`) — the pinned degrade behind the
1751
+ // avatar-initials fallback on phones — and a registered callback receives
1752
+ // the encoded bytes through the same `decode_image_fn` seam the desktop
1753
+ // platform codecs serve, with the shim-reported result codes mapping
1754
+ // exactly like the macOS host's (1 decoded / -1 too large / else failed)
1755
+ // and the reported dimensions re-validated before any pixels are trusted.
1756
+
1757
+ const MobileImageDef = struct {
1758
+ pub const Model = struct {
1759
+ cover: u64 = 0,
1760
+ width: usize = 0,
1761
+ height: usize = 0,
1762
+ register_error: ?anyerror = null,
1763
+ };
1764
+
1765
+ pub const Msg = union(enum) {
1766
+ register,
1767
+ };
1768
+
1769
+ const App = runtime.UiApp(Model, Msg);
1770
+
1771
+ pub fn initModel() Model {
1772
+ return .{};
1773
+ }
1774
+
1775
+ pub fn mobileOptions() App.Options {
1776
+ return .{
1777
+ .name = "mobile-image",
1778
+ .scene = ui_host.mobile_shell_scene,
1779
+ .canvas_label = mobile_gpu_surface_label,
1780
+ .update_fx = update,
1781
+ .view = view,
1782
+ };
1783
+ }
1784
+
1785
+ // The soundboard boot shape: decode-and-register committed encoded
1786
+ // bytes synchronously, store the id in the model only on success so a
1787
+ // failed decode leaves the avatar on its initials fallback.
1788
+ fn update(model: *Model, msg: Msg, fx: *App.Effects) void {
1789
+ switch (msg) {
1790
+ .register => {
1791
+ model.register_error = null;
1792
+ const registered = fx.registerImageBytes(7, "encoded-cover-bytes") catch |err| {
1793
+ model.register_error = err;
1794
+ return;
1795
+ };
1796
+ model.cover = 7;
1797
+ model.width = registered.width;
1798
+ model.height = registered.height;
1799
+ },
1800
+ }
1801
+ }
1802
+
1803
+ fn view(ui: *App.Ui, model: *const Model) App.Ui.Node {
1804
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
1805
+ ui.text(.{}, ui.fmt("cover {d}", .{model.cover})),
1806
+ ui.button(.{ .on_press = .register }, "Register"),
1807
+ });
1808
+ }
1809
+ };
1810
+
1811
+ const MobileImageHost = ui_host.UiAppHost(MobileImageDef);
1812
+ const MobileImageApi = c_api.MobileCApi(MobileImageHost);
1813
+
1814
+ /// What the fake shim codec records — the mobile mirror of the null
1815
+ /// platform's decode counter, held by the test and reached through the
1816
+ /// registered context pointer. `result`/`width`/`height` script the next
1817
+ /// answer so every shim result code is exercised.
1818
+ const MobileImageRecorder = struct {
1819
+ decode_count: usize = 0,
1820
+ last_bytes: [64]u8 = undefined,
1821
+ last_bytes_len: usize = 0,
1822
+ last_pixels_len: usize = 0,
1823
+ result: c_int = 1,
1824
+ width: usize = 2,
1825
+ height: usize = 2,
1826
+ fill: u8 = 0xAB,
1827
+
1828
+ fn from(context: ?*anyopaque) *MobileImageRecorder {
1829
+ return @ptrCast(@alignCast(context.?));
1830
+ }
1831
+ };
1832
+
1833
+ fn recorderImageDecode(
1834
+ context: ?*anyopaque,
1835
+ bytes: ?[*]const u8,
1836
+ bytes_len: usize,
1837
+ pixels: ?[*]u8,
1838
+ pixels_len: usize,
1839
+ out_width: ?*usize,
1840
+ out_height: ?*usize,
1841
+ ) callconv(.c) c_int {
1842
+ const recorder = MobileImageRecorder.from(context);
1843
+ recorder.decode_count += 1;
1844
+ recorder.last_bytes_len = conversions.copyInputText(&recorder.last_bytes, if (bytes) |value| value[0..bytes_len] else "");
1845
+ recorder.last_pixels_len = pixels_len;
1846
+ if (recorder.result != 1) return recorder.result;
1847
+ out_width.?.* = recorder.width;
1848
+ out_height.?.* = recorder.height;
1849
+ const byte_len = recorder.width *% recorder.height *% 4;
1850
+ if (pixels != null and byte_len <= pixels_len) {
1851
+ @memset(pixels.?[0..byte_len], recorder.fill);
1852
+ }
1853
+ return 1;
1854
+ }
1855
+
1856
+ fn startImageHost(app: ?*anyopaque) !void {
1857
+ MobileImageApi.native_sdk_app_start(app);
1858
+ var surface_token: u8 = 0;
1859
+ MobileImageApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
1860
+ MobileImageApi.native_sdk_app_frame(app);
1861
+ try std.testing.expectEqualStrings("", std.mem.span(MobileImageApi.native_sdk_app_last_error_name(app)));
1862
+ }
1863
+
1864
+ fn pressRegisterButton(app: ?*anyopaque) !void {
1865
+ const count = MobileImageApi.native_sdk_app_widget_semantics_count(app);
1866
+ var index: usize = 0;
1867
+ while (index < count) : (index += 1) {
1868
+ var node: MobileWidgetSemantics = .{};
1869
+ try std.testing.expectEqual(@as(c_int, 1), MobileImageApi.native_sdk_app_widget_semantics_at(app, index, &node));
1870
+ if (node.role != @intFromEnum(MobileWidgetRole.button)) continue;
1871
+ const node_label = if (node.label) |ptr| ptr[0..node.label_len] else "";
1872
+ if (!std.mem.eql(u8, node_label, "Register")) continue;
1873
+ const x = node.x + node.width / 2;
1874
+ const y = node.y + node.height / 2;
1875
+ MobileImageApi.native_sdk_app_touch(app, 1, 0, x, y, 1);
1876
+ MobileImageApi.native_sdk_app_touch(app, 1, 1, x, y, 0);
1877
+ try std.testing.expectEqualStrings("", std.mem.span(MobileImageApi.native_sdk_app_last_error_name(app)));
1878
+ return;
1879
+ }
1880
+ return error.TestUnexpectedResult;
1881
+ }
1882
+
1883
+ test "mobile hosts decline image decode until the shim registers a codec" {
1884
+ const app = MobileImageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1885
+ defer MobileImageApi.native_sdk_app_destroy(app);
1886
+ const self: *MobileImageHost = @ptrCast(@alignCast(app));
1887
+
1888
+ try startImageHost(app);
1889
+
1890
+ // No registered service: registerImageBytes reports the honest
1891
+ // decline synchronously, the id never reaches the model, and the
1892
+ // registry stays empty — the initials-fallback shape the soundboard
1893
+ // suite pins on the null platform, unchanged by the new seam.
1894
+ try pressRegisterButton(app);
1895
+ try std.testing.expectEqual(error.UnsupportedService, self.ui.model.register_error.?);
1896
+ try std.testing.expectEqual(@as(u64, 0), self.ui.model.cover);
1897
+ try std.testing.expectEqual(@as(usize, 0), self.embedded.runtime.registeredCanvasImageCount());
1898
+ }
1899
+
1900
+ test "mobile image service decodes registerImageBytes through the shim codec" {
1901
+ const app = MobileImageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1902
+ defer MobileImageApi.native_sdk_app_destroy(app);
1903
+ const self: *MobileImageHost = @ptrCast(@alignCast(app));
1904
+
1905
+ var recorder = MobileImageRecorder{};
1906
+ const service = types.MobileImageService{ .decode = recorderImageDecode };
1907
+ try std.testing.expectEqual(@as(c_int, 1), MobileImageApi.native_sdk_app_set_image_service(app, &service, &recorder));
1908
+
1909
+ try startImageHost(app);
1910
+
1911
+ // The encoded bytes reach the shim callback verbatim and the decoded
1912
+ // pixels land in the runtime's image registry with the shim-reported
1913
+ // dimensions; the model stores the id only after that success.
1914
+ try pressRegisterButton(app);
1915
+ try std.testing.expectEqual(@as(usize, 1), recorder.decode_count);
1916
+ try std.testing.expectEqualStrings("encoded-cover-bytes", recorder.last_bytes[0..recorder.last_bytes_len]);
1917
+ try std.testing.expect(self.ui.model.register_error == null);
1918
+ try std.testing.expectEqual(@as(u64, 7), self.ui.model.cover);
1919
+ try std.testing.expectEqual(@as(usize, 2), self.ui.model.width);
1920
+ try std.testing.expectEqual(@as(usize, 2), self.ui.model.height);
1921
+ const registered = self.embedded.runtime.registeredCanvasImage(7) orelse return error.TestUnexpectedResult;
1922
+ try std.testing.expectEqual(@as(usize, 2), registered.width);
1923
+ try std.testing.expectEqual(@as(usize, 2), registered.height);
1924
+ const resources = self.embedded.runtime.registeredCanvasImages();
1925
+ try std.testing.expectEqual(@as(usize, 1), resources.len);
1926
+ try std.testing.expectEqual(@as(u8, 0xAB), resources[0].pixels[0]);
1927
+
1928
+ // The decode happens once per registration: nothing about presenting
1929
+ // frames re-decodes (the registry holds the pixels).
1930
+ MobileImageApi.native_sdk_app_frame(app);
1931
+ MobileImageApi.native_sdk_app_frame(app);
1932
+ try std.testing.expectEqual(@as(usize, 1), recorder.decode_count);
1933
+ }
1934
+
1935
+ test "mobile image decode failures map to the macOS host's result codes" {
1936
+ const app = MobileImageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1937
+ defer MobileImageApi.native_sdk_app_destroy(app);
1938
+ const self: *MobileImageHost = @ptrCast(@alignCast(app));
1939
+
1940
+ var recorder = MobileImageRecorder{};
1941
+ const service = types.MobileImageService{ .decode = recorderImageDecode };
1942
+ try std.testing.expectEqual(@as(c_int, 1), MobileImageApi.native_sdk_app_set_image_service(app, &service, &recorder));
1943
+ try startImageHost(app);
1944
+
1945
+ // 0 (undecodable bytes) → ImageDecodeFailed, nothing registered.
1946
+ recorder.result = 0;
1947
+ try pressRegisterButton(app);
1948
+ try std.testing.expectEqual(error.ImageDecodeFailed, self.ui.model.register_error.?);
1949
+ try std.testing.expectEqual(@as(u64, 0), self.ui.model.cover);
1950
+
1951
+ // -1 (decoded pixels over the buffer) → ImageTooLarge.
1952
+ recorder.result = -1;
1953
+ try pressRegisterButton(app);
1954
+ try std.testing.expectEqual(error.ImageTooLarge, self.ui.model.register_error.?);
1955
+
1956
+ // A shim that answers success with dimensions the buffer cannot hold
1957
+ // is refused before any pixel slice is formed: decode failed, never
1958
+ // an out-of-bounds read.
1959
+ recorder.result = 1;
1960
+ recorder.width = 100_000;
1961
+ recorder.height = 100_000;
1962
+ try pressRegisterButton(app);
1963
+ try std.testing.expectEqual(error.ImageDecodeFailed, self.ui.model.register_error.?);
1964
+
1965
+ // Zero dimensions on a success answer are refused the same way.
1966
+ recorder.width = 0;
1967
+ recorder.height = 0;
1968
+ try pressRegisterButton(app);
1969
+ try std.testing.expectEqual(error.ImageDecodeFailed, self.ui.model.register_error.?);
1970
+ try std.testing.expectEqual(@as(usize, 0), self.embedded.runtime.registeredCanvasImageCount());
1971
+ }
1972
+
1973
+ test "clearing the mobile image service returns to the honest decline" {
1974
+ const app = MobileImageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
1975
+ defer MobileImageApi.native_sdk_app_destroy(app);
1976
+ const self: *MobileImageHost = @ptrCast(@alignCast(app));
1977
+
1978
+ var recorder = MobileImageRecorder{};
1979
+ const service = types.MobileImageService{ .decode = recorderImageDecode };
1980
+ try std.testing.expectEqual(@as(c_int, 1), MobileImageApi.native_sdk_app_set_image_service(app, &service, &recorder));
1981
+ try std.testing.expect(self.embedded.runtime.options.platform.services.decode_image_fn != null);
1982
+
1983
+ // A null (or all-null) table clears the registration: the service
1984
+ // entry empties and registerImageBytes declines again.
1985
+ try std.testing.expectEqual(@as(c_int, 1), MobileImageApi.native_sdk_app_set_image_service(app, null, null));
1986
+ try std.testing.expect(self.embedded.runtime.options.platform.services.decode_image_fn == null);
1987
+
1988
+ try startImageHost(app);
1989
+ try pressRegisterButton(app);
1990
+ try std.testing.expectEqual(error.UnsupportedService, self.ui.model.register_error.?);
1991
+ try std.testing.expectEqual(@as(usize, 0), recorder.decode_count);
1992
+ }
1993
+
1994
+ // ------------------------------------------------------ declared platform chrome
1995
+ //
1996
+ // The declared-chrome seam end to end: the shell metadata's tab set and
1997
+ // primary action read back over the ABI, the model's selected_tab_fn
1998
+ // derivation projects as a declared index the host polls, taps dispatch
1999
+ // the declared command ids through the ordinary command path into
2000
+ // update, the host chrome reports (form factor, tabs-projected) ride
2001
+ // the window-chrome channel into on_chrome Msgs, icons rasterize as
2002
+ // template pixels through the canvas vector core, and the whole loop
2003
+ // replays deterministically — the same sequence into two fresh hosts
2004
+ // lands the same model and the same projected selection.
2005
+
2006
+ const MobileTabsDef = struct {
2007
+ pub const Model = struct {
2008
+ tab: enum { feed, library } = .feed,
2009
+ /// One page deep on the feed tab (the soundboard's album-detail
2010
+ /// shape): the navigation projection derives depth from it.
2011
+ open_item: bool = false,
2012
+ action_count: u32 = 0,
2013
+ chrome_form_factor: platform.FormFactor = .unknown,
2014
+ chrome_tabs_projected: bool = false,
2015
+ chrome_bottom: f32 = 0,
2016
+ };
2017
+
2018
+ pub const Msg = union(enum) {
2019
+ show_feed,
2020
+ show_library,
2021
+ open_item,
2022
+ close_item,
2023
+ act,
2024
+ chrome_changed: platform.WindowChrome,
2025
+ };
2026
+
2027
+ const App = runtime.UiApp(Model, Msg);
2028
+
2029
+ const tabs = [_]@import("app_manifest").ShellTab{
2030
+ .{ .id = "tabs.feed", .label = "Feed", .icon = "menu" },
2031
+ .{ .id = "tabs.library", .label = "Library", .icon = "music" },
2032
+ };
2033
+
2034
+ const scene: @import("app_manifest").ShellConfig = .{
2035
+ .windows = ui_host.mobile_shell_scene.windows,
2036
+ .chrome = .{
2037
+ .tabs = &tabs,
2038
+ .primary_action = .{ .id = "action.act", .label = "Act", .icon = "plus" },
2039
+ },
2040
+ };
2041
+
2042
+ pub fn initModel() Model {
2043
+ return .{};
2044
+ }
2045
+
2046
+ pub fn mobileOptions() App.Options {
2047
+ return .{
2048
+ .name = "mobile-tabs",
2049
+ .scene = scene,
2050
+ .canvas_label = mobile_gpu_surface_label,
2051
+ .update = update,
2052
+ .view = view,
2053
+ .on_command = onCommand,
2054
+ .on_chrome = onChrome,
2055
+ .selected_tab_fn = selectedTab,
2056
+ .navigation_depth_fn = navigationDepth,
2057
+ .navigation_back_command = "nav.back",
2058
+ };
2059
+ }
2060
+
2061
+ fn update(model: *Model, msg: Msg) void {
2062
+ switch (msg) {
2063
+ .show_feed => model.tab = .feed,
2064
+ .show_library => model.tab = .library,
2065
+ .open_item => model.open_item = true,
2066
+ .close_item => model.open_item = false,
2067
+ .act => model.action_count += 1,
2068
+ .chrome_changed => |chrome_state| {
2069
+ model.chrome_form_factor = chrome_state.form_factor;
2070
+ model.chrome_tabs_projected = chrome_state.tabs_projected;
2071
+ model.chrome_bottom = chrome_state.insets.bottom;
2072
+ },
2073
+ }
2074
+ }
2075
+
2076
+ fn onCommand(name: []const u8) ?Msg {
2077
+ if (std.mem.eql(u8, name, "tabs.feed")) return .show_feed;
2078
+ if (std.mem.eql(u8, name, "tabs.library")) return .show_library;
2079
+ if (std.mem.eql(u8, name, "item.open")) return .open_item;
2080
+ if (std.mem.eql(u8, name, "nav.back")) return .close_item;
2081
+ if (std.mem.eql(u8, name, "action.act")) return .act;
2082
+ return null;
2083
+ }
2084
+
2085
+ fn onChrome(chrome_state: platform.WindowChrome) ?Msg {
2086
+ return .{ .chrome_changed = chrome_state };
2087
+ }
2088
+
2089
+ fn selectedTab(model: *const Model) []const u8 {
2090
+ return switch (model.tab) {
2091
+ .feed => "tabs.feed",
2092
+ .library => "tabs.library",
2093
+ };
2094
+ }
2095
+
2096
+ /// Depth follows the VISIBLE page stack (the soundboard rule): the
2097
+ /// open item counts only while the feed tab shows it, so a tab
2098
+ /// switch with an item open reads as lateral, never as a pop.
2099
+ fn navigationDepth(model: *const Model) usize {
2100
+ return if (model.tab == .feed and model.open_item) 1 else 0;
2101
+ }
2102
+
2103
+ fn view(ui: *App.Ui, model: *const Model) App.Ui.Node {
2104
+ return ui.column(.{ .gap = 8, .padding = 12 }, .{
2105
+ ui.text(.{}, switch (model.tab) {
2106
+ .feed => "Feed page",
2107
+ .library => "Library page",
2108
+ }),
2109
+ ui.text(.{}, ui.fmt("Acted {d}", .{model.action_count})),
2110
+ });
2111
+ }
2112
+ };
2113
+
2114
+ const MobileTabsHost = ui_host.UiAppHost(MobileTabsDef);
2115
+ const MobileTabsApi = c_api.MobileCApi(MobileTabsHost);
2116
+ const MobileChromeItem = @import("chrome.zig").MobileChromeItem;
2117
+
2118
+ fn chromeItemString(pointer: ?[*]const u8, len: usize) []const u8 {
2119
+ const bytes = pointer orelse return "";
2120
+ return bytes[0..len];
2121
+ }
2122
+
2123
+ test "mobile C ABI exposes declared platform chrome and mirrors model selection" {
2124
+ const app = MobileTabsApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2125
+ defer MobileTabsApi.native_sdk_app_destroy(app);
2126
+ const self: *MobileTabsHost = @ptrCast(@alignCast(app));
2127
+
2128
+ // The declaration reads back exactly as the scene states it.
2129
+ try std.testing.expectEqual(@as(usize, 2), MobileTabsApi.native_sdk_app_chrome_tab_count(app));
2130
+ var tab: MobileChromeItem = .{};
2131
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_chrome_tab_at(app, 0, &tab));
2132
+ try std.testing.expectEqualStrings("tabs.feed", chromeItemString(tab.id, tab.id_len));
2133
+ try std.testing.expectEqualStrings("Feed", chromeItemString(tab.label, tab.label_len));
2134
+ try std.testing.expectEqualStrings("menu", chromeItemString(tab.icon, tab.icon_len));
2135
+ try std.testing.expectEqual(@as(c_int, 0), MobileTabsApi.native_sdk_app_chrome_tab_at(app, 2, &tab));
2136
+ var action: MobileChromeItem = .{};
2137
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_chrome_primary_action(app, &action));
2138
+ try std.testing.expectEqualStrings("action.act", chromeItemString(action.id, action.id_len));
2139
+
2140
+ // Before the first rebuild no selection has been derived: the host
2141
+ // projects "no selected item" honestly.
2142
+ try std.testing.expectEqual(@as(isize, -1), MobileTabsApi.native_sdk_app_chrome_selected_tab(app));
2143
+
2144
+ MobileTabsApi.native_sdk_app_start(app);
2145
+ var surface_token: u8 = 0;
2146
+ MobileTabsApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
2147
+ MobileTabsApi.native_sdk_app_frame(app);
2148
+ try std.testing.expect(self.ui.installed);
2149
+
2150
+ // The installing rebuild derived the model's selection.
2151
+ try std.testing.expectEqual(@as(isize, 0), MobileTabsApi.native_sdk_app_chrome_selected_tab(app));
2152
+
2153
+ // A projected tap dispatches the declared command id through the
2154
+ // ordinary command path; update moves the model and the projection
2155
+ // follows on the same dispatch (synchronous).
2156
+ MobileTabsApi.native_sdk_app_command(app, "tabs.library", "tabs.library".len);
2157
+ try std.testing.expectEqual(.library, self.ui.model.tab);
2158
+ try std.testing.expectEqual(@as(isize, 1), MobileTabsApi.native_sdk_app_chrome_selected_tab(app));
2159
+
2160
+ // The primary action dispatches like any command; selection holds.
2161
+ MobileTabsApi.native_sdk_app_command(app, "action.act", "action.act".len);
2162
+ try std.testing.expectEqual(@as(u32, 1), self.ui.model.action_count);
2163
+ try std.testing.expectEqual(@as(isize, 1), MobileTabsApi.native_sdk_app_chrome_selected_tab(app));
2164
+
2165
+ // A programmatic Msg (no bar involved) moves the model; the next
2166
+ // projection poll reads the new selection — the bar is downstream.
2167
+ try self.embedded.runtime.dispatchCommand(self.embedded.app, .{ .name = "tabs.feed", .source = .menu, .window_id = 1 });
2168
+ try std.testing.expectEqual(.feed, self.ui.model.tab);
2169
+ try std.testing.expectEqual(@as(isize, 0), MobileTabsApi.native_sdk_app_chrome_selected_tab(app));
2170
+ }
2171
+
2172
+ test "mobile host chrome reports ride the window-chrome channel" {
2173
+ const app = MobileTabsApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2174
+ defer MobileTabsApi.native_sdk_app_destroy(app);
2175
+ const self: *MobileTabsHost = @ptrCast(@alignCast(app));
2176
+
2177
+ // The host files its standing reports before start (the iOS host's
2178
+ // order): the pre-install chrome delivery already carries them.
2179
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_set_form_factor(app, 1));
2180
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_set_chrome_tabs_projected(app, 1));
2181
+
2182
+ MobileTabsApi.native_sdk_app_start(app);
2183
+ var surface_token: u8 = 0;
2184
+ MobileTabsApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 47, 0, 34, 0, 0, 0, 0, 0);
2185
+ MobileTabsApi.native_sdk_app_frame(app);
2186
+ try std.testing.expect(self.ui.installed);
2187
+
2188
+ // Both reports landed in the model as one on_chrome Msg, beside the
2189
+ // safe-area insets the same channel always carried.
2190
+ try std.testing.expectEqual(platform.FormFactor.compact, self.ui.model.chrome_form_factor);
2191
+ try std.testing.expect(self.ui.model.chrome_tabs_projected);
2192
+ try std.testing.expectEqual(@as(f32, 34), self.ui.model.chrome_bottom);
2193
+
2194
+ // A viewport push never erases the standing reports.
2195
+ MobileTabsApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 47, 0, 40, 0, 0, 0, 0, 0);
2196
+ MobileTabsApi.native_sdk_app_frame(app);
2197
+ try std.testing.expectEqual(platform.FormFactor.compact, self.ui.model.chrome_form_factor);
2198
+ try std.testing.expect(self.ui.model.chrome_tabs_projected);
2199
+ try std.testing.expectEqual(@as(f32, 40), self.ui.model.chrome_bottom);
2200
+
2201
+ // The size-class flip arrives as a fresh chrome Msg with the next
2202
+ // viewport-driven re-query (an iPad rotation's trait change).
2203
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_set_form_factor(app, 2));
2204
+ MobileTabsApi.native_sdk_app_viewport(app, 1024, 768, 1, &surface_token, 24, 0, 20, 0, 0, 0, 0, 0);
2205
+ MobileTabsApi.native_sdk_app_frame(app);
2206
+ try std.testing.expectEqual(platform.FormFactor.regular, self.ui.model.chrome_form_factor);
2207
+ }
2208
+
2209
+ test "chrome icon pixels rasterize declared vocabulary glyphs as templates" {
2210
+ const app = MobileTabsApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2211
+ defer MobileTabsApi.native_sdk_app_destroy(app);
2212
+
2213
+ const size: usize = 48;
2214
+ var pixels: [size * size * 4]u8 = undefined;
2215
+
2216
+ // A built-in glyph inks alpha; color channels are premultiplied
2217
+ // white (never exceeding alpha, equal at full coverage).
2218
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_chrome_icon_pixels(app, "plus", "plus".len, size, &pixels, pixels.len));
2219
+ var ink: usize = 0;
2220
+ var index: usize = 0;
2221
+ while (index < pixels.len) : (index += 4) {
2222
+ const alpha = pixels[index + 3];
2223
+ if (alpha > 0) ink += 1;
2224
+ try std.testing.expectEqual(alpha, pixels[index]);
2225
+ try std.testing.expectEqual(alpha, pixels[index + 1]);
2226
+ try std.testing.expectEqual(alpha, pixels[index + 2]);
2227
+ }
2228
+ try std.testing.expect(ink > 40);
2229
+
2230
+ // Determinism: the same request renders identical bytes.
2231
+ var second: [size * size * 4]u8 = undefined;
2232
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_chrome_icon_pixels(app, "plus", "plus".len, size, &second, second.len));
2233
+ try std.testing.expectEqualSlices(u8, &pixels, &second);
2234
+
2235
+ // An unresolvable name renders the honest missing glyph — visible,
2236
+ // never an empty image.
2237
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_chrome_icon_pixels(app, "no-such-glyph", "no-such-glyph".len, size, &pixels, pixels.len));
2238
+ ink = 0;
2239
+ index = 3;
2240
+ while (index < pixels.len) : (index += 4) {
2241
+ if (pixels[index] > 0) ink += 1;
2242
+ }
2243
+ try std.testing.expect(ink > 40);
2244
+
2245
+ // Structurally invalid requests are refused loudly.
2246
+ try std.testing.expectEqual(@as(c_int, 0), MobileTabsApi.native_sdk_app_chrome_icon_pixels(app, "plus", "plus".len, 0, &pixels, pixels.len));
2247
+ try std.testing.expectEqualStrings("InvalidIconRequest", std.mem.span(MobileTabsApi.native_sdk_app_last_error_name(app)));
2248
+ try std.testing.expectEqual(@as(c_int, 0), MobileTabsApi.native_sdk_app_chrome_icon_pixels(app, "plus", "plus".len, size, &pixels, pixels.len - 1));
2249
+ try std.testing.expectEqual(@as(c_int, 0), MobileTabsApi.native_sdk_app_chrome_icon_pixels(app, "", 0, size, &pixels, pixels.len));
2250
+ }
2251
+
2252
+ test "mobile C ABI projects navigation depth and dispatches the back command" {
2253
+ const app = MobileTabsApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2254
+ defer MobileTabsApi.native_sdk_app_destroy(app);
2255
+ const self: *MobileTabsHost = @ptrCast(@alignCast(app));
2256
+
2257
+ // The static half reads back before start: the declared back
2258
+ // command is what a completed platform back gesture dispatches.
2259
+ var back: MobileChromeItem = .{};
2260
+ try std.testing.expectEqual(@as(c_int, 1), MobileTabsApi.native_sdk_app_chrome_navigation_back_command(app, &back));
2261
+ try std.testing.expectEqualStrings("nav.back", chromeItemString(back.id, back.id_len));
2262
+
2263
+ // Before the first rebuild no depth has been derived: hosts read -1
2264
+ // and present no transitions.
2265
+ try std.testing.expectEqual(@as(isize, -1), MobileTabsApi.native_sdk_app_chrome_navigation_depth(app));
2266
+
2267
+ MobileTabsApi.native_sdk_app_start(app);
2268
+ var surface_token: u8 = 0;
2269
+ MobileTabsApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
2270
+ MobileTabsApi.native_sdk_app_frame(app);
2271
+ try std.testing.expect(self.ui.installed);
2272
+
2273
+ // The installing rebuild derived the root depth.
2274
+ try std.testing.expectEqual(@as(isize, 0), MobileTabsApi.native_sdk_app_chrome_navigation_depth(app));
2275
+
2276
+ // A Msg that opens a page moves the projection to 1 — the host
2277
+ // reads the change on its next poll and presents the push.
2278
+ MobileTabsApi.native_sdk_app_command(app, "item.open", "item.open".len);
2279
+ try std.testing.expect(self.ui.model.open_item);
2280
+ try std.testing.expectEqual(@as(isize, 1), MobileTabsApi.native_sdk_app_chrome_navigation_depth(app));
2281
+
2282
+ // A tab switch with the page open is LATERAL: the depth drops with
2283
+ // the selected tab in the same poll (the host reconciles without a
2284
+ // transition), and switching back restores it the same way — the
2285
+ // model state never moved.
2286
+ MobileTabsApi.native_sdk_app_command(app, "tabs.library", "tabs.library".len);
2287
+ try std.testing.expectEqual(@as(isize, 0), MobileTabsApi.native_sdk_app_chrome_navigation_depth(app));
2288
+ try std.testing.expectEqual(@as(isize, 1), MobileTabsApi.native_sdk_app_chrome_selected_tab(app));
2289
+ try std.testing.expect(self.ui.model.open_item);
2290
+ MobileTabsApi.native_sdk_app_command(app, "tabs.feed", "tabs.feed".len);
2291
+ try std.testing.expectEqual(@as(isize, 1), MobileTabsApi.native_sdk_app_chrome_navigation_depth(app));
2292
+
2293
+ // The completed gesture's dispatch: the declared back command rides
2294
+ // the ordinary command path, update closes the page, and the
2295
+ // projection answers 0 on the same dispatch — exactly once.
2296
+ MobileTabsApi.native_sdk_app_command(app, back.id, back.id_len);
2297
+ try std.testing.expect(!self.ui.model.open_item);
2298
+ try std.testing.expectEqual(@as(isize, 0), MobileTabsApi.native_sdk_app_chrome_navigation_depth(app));
2299
+ }
2300
+
2301
+ test "apps without a navigation projection answer the honest zeros" {
2302
+ // The damage def declares no navigation_depth_fn: depth stays -1
2303
+ // forever (hosts present no transitions, never arm the gesture) and
2304
+ // no back command exists.
2305
+ const app = MobileDamageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2306
+ defer MobileDamageApi.native_sdk_app_destroy(app);
2307
+
2308
+ try std.testing.expectEqual(@as(isize, -1), MobileDamageApi.native_sdk_app_chrome_navigation_depth(app));
2309
+ var back: MobileChromeItem = .{};
2310
+ try std.testing.expectEqual(@as(c_int, 0), MobileDamageApi.native_sdk_app_chrome_navigation_back_command(app, &back));
2311
+
2312
+ MobileDamageApi.native_sdk_app_start(app);
2313
+ var surface_token: u8 = 0;
2314
+ MobileDamageApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
2315
+ MobileDamageApi.native_sdk_app_frame(app);
2316
+ try std.testing.expectEqual(@as(isize, -1), MobileDamageApi.native_sdk_app_chrome_navigation_depth(app));
2317
+ try std.testing.expectEqual(@as(c_int, 0), MobileDamageApi.native_sdk_app_chrome_navigation_back_command(app, &back));
2318
+ }
2319
+
2320
+ test "declared chrome projection replays deterministically" {
2321
+ // One driving sequence into two fresh hosts: identical model state
2322
+ // and identical projected selection AND navigation depth at every
2323
+ // step — both projections are pure functions of the Msg journal, so
2324
+ // record/replay holds for the native bar and the push/pop stack
2325
+ // exactly as it does for the canvas.
2326
+ var selected_a: [12]isize = undefined;
2327
+ var selected_b: [12]isize = undefined;
2328
+ var depth_a: [10]isize = undefined;
2329
+ var depth_b: [10]isize = undefined;
2330
+ var model_a: MobileTabsDef.Model = undefined;
2331
+ var model_b: MobileTabsDef.Model = undefined;
2332
+
2333
+ for (0..2) |round| {
2334
+ const app = MobileTabsApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2335
+ defer MobileTabsApi.native_sdk_app_destroy(app);
2336
+ const self: *MobileTabsHost = @ptrCast(@alignCast(app));
2337
+ const selected = if (round == 0) &selected_a else &selected_b;
2338
+ const depth = if (round == 0) &depth_a else &depth_b;
2339
+
2340
+ _ = MobileTabsApi.native_sdk_app_set_form_factor(app, 1);
2341
+ _ = MobileTabsApi.native_sdk_app_set_chrome_tabs_projected(app, 1);
2342
+ MobileTabsApi.native_sdk_app_start(app);
2343
+ var surface_token: u8 = 0;
2344
+ MobileTabsApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 47, 0, 34, 0, 0, 0, 0, 0);
2345
+ MobileTabsApi.native_sdk_app_frame(app);
2346
+ selected[0] = MobileTabsApi.native_sdk_app_chrome_selected_tab(app);
2347
+ depth[0] = MobileTabsApi.native_sdk_app_chrome_navigation_depth(app);
2348
+
2349
+ // Tab moves, a push, a lateral switch away and back over the
2350
+ // open page, the gesture's back dispatch, and a repeat push —
2351
+ // the sequence a stress session produces.
2352
+ const journal = [_][]const u8{ "tabs.library", "action.act", "tabs.feed", "item.open", "tabs.library", "tabs.feed", "nav.back", "item.open", "action.act" };
2353
+ for (journal, 1..) |command_name, step| {
2354
+ MobileTabsApi.native_sdk_app_command(app, command_name.ptr, command_name.len);
2355
+ selected[step] = MobileTabsApi.native_sdk_app_chrome_selected_tab(app);
2356
+ depth[step] = MobileTabsApi.native_sdk_app_chrome_navigation_depth(app);
2357
+ }
2358
+ selected[journal.len + 1] = MobileTabsApi.native_sdk_app_chrome_selected_tab(app);
2359
+ selected[journal.len + 2] = @intCast(self.ui.model.action_count);
2360
+ if (round == 0) model_a = self.ui.model else model_b = self.ui.model;
2361
+ }
2362
+
2363
+ try std.testing.expectEqualSlices(isize, &selected_a, &selected_b);
2364
+ try std.testing.expectEqualSlices(isize, &depth_a, &depth_b);
2365
+ try std.testing.expectEqualDeep(model_a, model_b);
2366
+ }
2367
+
2368
+ // ------------------------------------------------------- damage render ABI
2369
+ //
2370
+ // The incremental pixel seam mobile hosts upload from: the runtime's frame
2371
+ // dispatch presents changed frames through the dirty-scissored CPU raster
2372
+ // (packet presenters dropped, pixel-present retained baseline on — see
2373
+ // `host.installPresentCapture`), the host captures each present, and
2374
+ // `native_sdk_app_render_pixels_damage` copies only the damaged region
2375
+ // into the shim's retained buffer and names it, so a keystroke's upload is
2376
+ // the field, not the surface.
2377
+
2378
+ const MobileDamageDef = struct {
2379
+ pub const Model = struct {
2380
+ count: u32 = 0,
2381
+ };
2382
+
2383
+ pub const Msg = union(enum) {
2384
+ increment,
2385
+ };
2386
+
2387
+ const App = runtime.UiApp(Model, Msg);
2388
+
2389
+ pub fn initModel() Model {
2390
+ return .{};
2391
+ }
2392
+
2393
+ pub fn mobileOptions() App.Options {
2394
+ return .{
2395
+ .name = "mobile-damage",
2396
+ .scene = ui_host.mobile_shell_scene,
2397
+ .canvas_label = mobile_gpu_surface_label,
2398
+ .update = update,
2399
+ .view = view,
2400
+ };
2401
+ }
2402
+
2403
+ fn update(model: *Model, msg: Msg) void {
2404
+ switch (msg) {
2405
+ .increment => model.count += 1,
2406
+ }
2407
+ }
2408
+
2409
+ fn rowKey(index: *const usize) canvas.UiKey {
2410
+ return canvas.uiKey(@as(u64, index.*));
2411
+ }
2412
+
2413
+ fn rowView(ui: *App.Ui, index: *const usize) App.Ui.Node {
2414
+ return ui.text(.{}, ui.fmt("row {d}", .{index.*}));
2415
+ }
2416
+
2417
+ fn indices(arena: std.mem.Allocator) []const usize {
2418
+ const items = arena.alloc(usize, 20) catch return &.{};
2419
+ for (items, 0..) |*item, item_index| item.* = item_index;
2420
+ return items;
2421
+ }
2422
+
2423
+ fn view(ui: *App.Ui, model: *const Model) App.Ui.Node {
2424
+ // Deliberately UNDER the packet hosts' small-list gate: the
2425
+ // pixel-adopted baseline refines dirty bounds at any command
2426
+ // count (a full window would mean a full raster AND a full
2427
+ // upload on mobile). The CHANGING content is pinned to the top
2428
+ // of the surface so refined damage stays a small band.
2429
+ return ui.column(.{ .gap = 2, .padding = 8 }, .{
2430
+ ui.text(.{}, ui.fmt("Count {d}", .{model.count})),
2431
+ ui.button(.{ .variant = .primary, .on_press = .increment }, "Increment"),
2432
+ ui.column(.{ .gap = 2 }, ui.each(indices(ui.arena), rowKey, rowView)),
2433
+ });
2434
+ }
2435
+ };
2436
+
2437
+ const MobileDamageHost = ui_host.UiAppHost(MobileDamageDef);
2438
+ const MobileDamageApi = c_api.MobileCApi(MobileDamageHost);
2439
+
2440
+ fn findDamageSemanticsByRole(app: ?*anyopaque, role: MobileWidgetRole) !MobileWidgetSemantics {
2441
+ const count = MobileDamageApi.native_sdk_app_widget_semantics_count(app);
2442
+ var index: usize = 0;
2443
+ while (index < count) : (index += 1) {
2444
+ var node: MobileWidgetSemantics = .{};
2445
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_widget_semantics_at(app, index, &node));
2446
+ if (node.role == @intFromEnum(role)) return node;
2447
+ }
2448
+ return error.TestUnexpectedResult;
2449
+ }
2450
+
2451
+ test "mobile damage render copies only changed regions from the presented capture" {
2452
+ const app = MobileDamageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2453
+ defer MobileDamageApi.native_sdk_app_destroy(app);
2454
+ const self: *MobileDamageHost = @ptrCast(@alignCast(app));
2455
+
2456
+ MobileDamageApi.native_sdk_app_start(app);
2457
+ var surface_token: u8 = 0;
2458
+ MobileDamageApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
2459
+ MobileDamageApi.native_sdk_app_frame(app);
2460
+ try std.testing.expectEqualStrings("", std.mem.span(MobileDamageApi.native_sdk_app_last_error_name(app)));
2461
+ try std.testing.expectEqual(@as(usize, 1), self.null_platform.gpu_surface_present_count);
2462
+
2463
+ var info: MobileCanvasPixels = .{};
2464
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixel_size(app, 1, &info));
2465
+ const retained = try std.testing.allocator.alloc(u8, info.byte_len);
2466
+ defer std.testing.allocator.free(retained);
2467
+
2468
+ // First delivery: the whole surface with full damage.
2469
+ var damage: types.MobileCanvasPixelsDamage = .{};
2470
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixels_damage(app, 1, retained.ptr, retained.len, &damage));
2471
+ try std.testing.expectEqual(info.width, damage.width);
2472
+ try std.testing.expectEqual(info.height, damage.height);
2473
+ try std.testing.expectEqual(info.byte_len, damage.byte_len);
2474
+ try std.testing.expectEqual(@as(usize, 0), damage.damage_x);
2475
+ try std.testing.expectEqual(@as(usize, 0), damage.damage_y);
2476
+ try std.testing.expectEqual(info.width, damage.damage_width);
2477
+ try std.testing.expectEqual(info.height, damage.damage_height);
2478
+ var nonblank = false;
2479
+ for (retained) |byte| {
2480
+ if (byte != 0) {
2481
+ nonblank = true;
2482
+ break;
2483
+ }
2484
+ }
2485
+ try std.testing.expect(nonblank);
2486
+
2487
+ // Idle frame: nothing presents, and the next delivery reports EMPTY
2488
+ // damage — the host skips its upload entirely.
2489
+ MobileDamageApi.native_sdk_app_frame(app);
2490
+ try std.testing.expectEqual(@as(usize, 1), self.null_platform.gpu_surface_present_count);
2491
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixels_damage(app, 1, retained.ptr, retained.len, &damage));
2492
+ try std.testing.expectEqual(@as(usize, 0), damage.damage_width);
2493
+ try std.testing.expectEqual(@as(usize, 0), damage.damage_height);
2494
+
2495
+ // A model change presents INCREMENTALLY: the dirty region refines to
2496
+ // the changed commands (the counter text and the button's state, both
2497
+ // pinned near the top), never the whole window. Between the change
2498
+ // and its present, a delivery must report the OLD revision with
2499
+ // empty damage — the host's signal to call again next tick (the
2500
+ // gate-on-sightings race that once stranded a boot relayout off the
2501
+ // glass).
2502
+ const revision_before_tap = damage.revision;
2503
+ const button = try findDamageSemanticsByRole(app, .button);
2504
+ const tap_x = button.x + button.width / 2;
2505
+ const tap_y = button.y + button.height / 2;
2506
+ MobileDamageApi.native_sdk_app_touch(app, 1, 0, tap_x, tap_y, 1);
2507
+ MobileDamageApi.native_sdk_app_touch(app, 1, 1, tap_x, tap_y, 0);
2508
+ try std.testing.expectEqual(@as(u32, 1), self.ui.model.count);
2509
+ var frame_state: MobileGpuFrameState = .{};
2510
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_gpu_frame_state(app, &frame_state));
2511
+ try std.testing.expect(frame_state.canvas_revision > revision_before_tap);
2512
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixels_damage(app, 1, retained.ptr, retained.len, &damage));
2513
+ try std.testing.expectEqual(@as(usize, 0), damage.damage_width);
2514
+ try std.testing.expectEqual(revision_before_tap, damage.revision);
2515
+ try std.testing.expect(damage.revision < frame_state.canvas_revision);
2516
+ MobileDamageApi.native_sdk_app_frame(app);
2517
+ try std.testing.expectEqual(@as(usize, 2), self.null_platform.gpu_surface_present_count);
2518
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixels_damage(app, 1, retained.ptr, retained.len, &damage));
2519
+ try std.testing.expect(damage.damage_width > 0);
2520
+ try std.testing.expect(damage.damage_height > 0);
2521
+ try std.testing.expect(damage.damage_height < info.height / 2);
2522
+ try std.testing.expectEqual(frame_state.canvas_revision, damage.revision);
2523
+
2524
+ // Anti-tearing invariant: after the incremental copy the retained
2525
+ // buffer is byte-identical to a fresh full-surface render of the
2526
+ // same retained scene.
2527
+ const full = try std.testing.allocator.alloc(u8, info.byte_len);
2528
+ defer std.testing.allocator.free(full);
2529
+ var rendered: MobileCanvasPixels = .{};
2530
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixels(app, 1, full.ptr, full.len, &rendered));
2531
+ try std.testing.expectEqualSlices(u8, full, retained);
2532
+ }
2533
+
2534
+ test "mobile damage render falls back to a full render without a capture" {
2535
+ const app = MobileDamageApi.native_sdk_app_create() orelse return error.TestUnexpectedResult;
2536
+ defer MobileDamageApi.native_sdk_app_destroy(app);
2537
+
2538
+ MobileDamageApi.native_sdk_app_start(app);
2539
+ var surface_token: u8 = 0;
2540
+ MobileDamageApi.native_sdk_app_viewport(app, 390, 844, 1, &surface_token, 0, 0, 0, 0, 0, 0, 0, 0);
2541
+ // No frame pumped: nothing has presented, so the damage entry takes
2542
+ // the full-render fallback with full damage — exactly the plain
2543
+ // render_pixels contract plus an honest damage report.
2544
+ var info: MobileCanvasPixels = .{};
2545
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixel_size(app, 1, &info));
2546
+ const buffer = try std.testing.allocator.alloc(u8, info.byte_len);
2547
+ defer std.testing.allocator.free(buffer);
2548
+ var damage: types.MobileCanvasPixelsDamage = .{};
2549
+ try std.testing.expectEqual(@as(c_int, 1), MobileDamageApi.native_sdk_app_render_pixels_damage(app, 1, buffer.ptr, buffer.len, &damage));
2550
+ try std.testing.expectEqual(info.width, damage.damage_width);
2551
+ try std.testing.expectEqual(info.height, damage.damage_height);
2552
+ }