@native-sdk/cli 0.0.0 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (373) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +50 -1
  3. package/app.zon +30 -0
  4. package/assets/icon.icns +0 -0
  5. package/assets/icon.ico +0 -0
  6. package/assets/icon.png +0 -0
  7. package/assets/icon.svg +15 -0
  8. package/assets/native-sdk.entitlements +10 -0
  9. package/assets/native-sdk.manifest +12 -0
  10. package/bin/native.js +156 -0
  11. package/build/app.zig +716 -0
  12. package/build.zig +2593 -0
  13. package/build.zig.zon +24 -0
  14. package/native-sdk.d.ts +615 -0
  15. package/package.json +45 -8
  16. package/skill-data/automation/SKILL.md +248 -0
  17. package/skill-data/core/SKILL.md +249 -0
  18. package/skill-data/core/references/app-model-runtime.md +141 -0
  19. package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
  20. package/skill-data/core/references/frontend-assets.md +124 -0
  21. package/skill-data/core/references/project-anatomy.md +108 -0
  22. package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
  23. package/skill-data/native-ui/SKILL.md +1080 -0
  24. package/skills/native-sdk/SKILL.md +33 -0
  25. package/src/app_runner/root.zig +740 -0
  26. package/src/assets/root.zig +20 -0
  27. package/src/automation/protocol.zig +285 -0
  28. package/src/automation/root.zig +13 -0
  29. package/src/automation/server.zig +506 -0
  30. package/src/automation/snapshot.zig +1202 -0
  31. package/src/automation/watcher.zig +165 -0
  32. package/src/bridge/root.zig +507 -0
  33. package/src/debug/root.zig +246 -0
  34. package/src/embed/app_exports.zig +11 -0
  35. package/src/embed/c_api.zig +793 -0
  36. package/src/embed/c_exports.zig +10 -0
  37. package/src/embed/chrome.zig +168 -0
  38. package/src/embed/conversions.zig +359 -0
  39. package/src/embed/host.zig +960 -0
  40. package/src/embed/root.zig +81 -0
  41. package/src/embed/tests.zig +2552 -0
  42. package/src/embed/types.zig +382 -0
  43. package/src/embed/ui_host.zig +289 -0
  44. package/src/extensions/root.zig +197 -0
  45. package/src/frontend/root.zig +48 -0
  46. package/src/js/root.zig +65 -0
  47. package/src/platform/android/NativeSdkActivity.java +1370 -0
  48. package/src/platform/android/android_host.c +696 -0
  49. package/src/platform/android/files.zig +9 -0
  50. package/src/platform/android/native_sdk_app.h +237 -0
  51. package/src/platform/ios/files.zig +8 -0
  52. package/src/platform/ios/native_sdk_app.h +289 -0
  53. package/src/platform/ios/uikit_host.m +2739 -0
  54. package/src/platform/linux/cef_host.cpp +599 -0
  55. package/src/platform/linux/gtk_host.c +5362 -0
  56. package/src/platform/linux/gtk_host.h +265 -0
  57. package/src/platform/linux/root.zig +1519 -0
  58. package/src/platform/macos/appkit_host.h +604 -0
  59. package/src/platform/macos/appkit_host.m +10392 -0
  60. package/src/platform/macos/automation_host.h +14 -0
  61. package/src/platform/macos/automation_host.m +12 -0
  62. package/src/platform/macos/cef_host.mm +3028 -0
  63. package/src/platform/macos/root.zig +2260 -0
  64. package/src/platform/null_platform.zig +2232 -0
  65. package/src/platform/null_platform_tests.zig +670 -0
  66. package/src/platform/policy_values.zig +14 -0
  67. package/src/platform/root.zig +181 -0
  68. package/src/platform/types.zig +2681 -0
  69. package/src/platform/windows/cef_host.cpp +3 -0
  70. package/src/platform/windows/root.zig +1521 -0
  71. package/src/platform/windows/webview2_host.cpp +6230 -0
  72. package/src/primitives/app_dirs/root.zig +440 -0
  73. package/src/primitives/app_manifest/root.zig +109 -0
  74. package/src/primitives/app_manifest/tests.zig +758 -0
  75. package/src/primitives/app_manifest/types.zig +587 -0
  76. package/src/primitives/app_manifest/validation.zig +828 -0
  77. package/src/primitives/assets/root.zig +520 -0
  78. package/src/primitives/canvas/a11y_audit.zig +390 -0
  79. package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
  80. package/src/primitives/canvas/app_icon.zig +1590 -0
  81. package/src/primitives/canvas/chart.zig +392 -0
  82. package/src/primitives/canvas/chart_tests.zig +751 -0
  83. package/src/primitives/canvas/commands.zig +446 -0
  84. package/src/primitives/canvas/drawing.zig +309 -0
  85. package/src/primitives/canvas/equality.zig +316 -0
  86. package/src/primitives/canvas/events.zig +696 -0
  87. package/src/primitives/canvas/font_coverage.zig +118 -0
  88. package/src/primitives/canvas/font_ttf.zig +510 -0
  89. package/src/primitives/canvas/font_ttf_tests.zig +280 -0
  90. package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
  91. package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
  92. package/src/primitives/canvas/frame.zig +679 -0
  93. package/src/primitives/canvas/frame_metrics.zig +462 -0
  94. package/src/primitives/canvas/gpu.zig +639 -0
  95. package/src/primitives/canvas/hash.zig +112 -0
  96. package/src/primitives/canvas/icons/alert.svg +1 -0
  97. package/src/primitives/canvas/icons/archive.svg +1 -0
  98. package/src/primitives/canvas/icons/arrow-down.svg +1 -0
  99. package/src/primitives/canvas/icons/arrow-right.svg +1 -0
  100. package/src/primitives/canvas/icons/arrow-up.svg +1 -0
  101. package/src/primitives/canvas/icons/check-circle.svg +1 -0
  102. package/src/primitives/canvas/icons/check.svg +1 -0
  103. package/src/primitives/canvas/icons/chevron-down.svg +1 -0
  104. package/src/primitives/canvas/icons/chevron-left.svg +1 -0
  105. package/src/primitives/canvas/icons/chevron-right.svg +1 -0
  106. package/src/primitives/canvas/icons/chevron-up.svg +1 -0
  107. package/src/primitives/canvas/icons/circle-dot.svg +1 -0
  108. package/src/primitives/canvas/icons/clock.svg +1 -0
  109. package/src/primitives/canvas/icons/copy.svg +1 -0
  110. package/src/primitives/canvas/icons/download.svg +1 -0
  111. package/src/primitives/canvas/icons/edit.svg +1 -0
  112. package/src/primitives/canvas/icons/ellipsis.svg +1 -0
  113. package/src/primitives/canvas/icons/external-link.svg +1 -0
  114. package/src/primitives/canvas/icons/eye.svg +1 -0
  115. package/src/primitives/canvas/icons/file-text.svg +1 -0
  116. package/src/primitives/canvas/icons/folder-open.svg +1 -0
  117. package/src/primitives/canvas/icons/folder.svg +1 -0
  118. package/src/primitives/canvas/icons/git-branch.svg +1 -0
  119. package/src/primitives/canvas/icons/git-merge.svg +1 -0
  120. package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
  121. package/src/primitives/canvas/icons/info.svg +1 -0
  122. package/src/primitives/canvas/icons/menu.svg +1 -0
  123. package/src/primitives/canvas/icons/missing.svg +1 -0
  124. package/src/primitives/canvas/icons/moon.svg +1 -0
  125. package/src/primitives/canvas/icons/music.svg +1 -0
  126. package/src/primitives/canvas/icons/panel-left.svg +1 -0
  127. package/src/primitives/canvas/icons/panel-right.svg +1 -0
  128. package/src/primitives/canvas/icons/pause.svg +1 -0
  129. package/src/primitives/canvas/icons/play.svg +1 -0
  130. package/src/primitives/canvas/icons/plus.svg +1 -0
  131. package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
  132. package/src/primitives/canvas/icons/repeat.svg +1 -0
  133. package/src/primitives/canvas/icons/save.svg +1 -0
  134. package/src/primitives/canvas/icons/search.svg +1 -0
  135. package/src/primitives/canvas/icons/send.svg +1 -0
  136. package/src/primitives/canvas/icons/settings.svg +1 -0
  137. package/src/primitives/canvas/icons/shuffle.svg +1 -0
  138. package/src/primitives/canvas/icons/skip-back.svg +1 -0
  139. package/src/primitives/canvas/icons/skip-forward.svg +1 -0
  140. package/src/primitives/canvas/icons/sun.svg +1 -0
  141. package/src/primitives/canvas/icons/terminal.svg +1 -0
  142. package/src/primitives/canvas/icons/trash.svg +1 -0
  143. package/src/primitives/canvas/icons/volume.svg +1 -0
  144. package/src/primitives/canvas/icons/wrench.svg +1 -0
  145. package/src/primitives/canvas/icons/x-circle.svg +1 -0
  146. package/src/primitives/canvas/icons/x.svg +1 -0
  147. package/src/primitives/canvas/icons.zig +249 -0
  148. package/src/primitives/canvas/layout_audit.zig +744 -0
  149. package/src/primitives/canvas/layout_audit_tests.zig +426 -0
  150. package/src/primitives/canvas/markdown.zig +1031 -0
  151. package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
  152. package/src/primitives/canvas/markdown_tests.zig +538 -0
  153. package/src/primitives/canvas/plan_key_index.zig +120 -0
  154. package/src/primitives/canvas/png.zig +355 -0
  155. package/src/primitives/canvas/reference.zig +1457 -0
  156. package/src/primitives/canvas/reference_blur.zig +104 -0
  157. package/src/primitives/canvas/reference_memo.zig +322 -0
  158. package/src/primitives/canvas/reference_paths.zig +184 -0
  159. package/src/primitives/canvas/reference_tests.zig +1650 -0
  160. package/src/primitives/canvas/render.zig +747 -0
  161. package/src/primitives/canvas/render_effects.zig +255 -0
  162. package/src/primitives/canvas/render_fingerprints.zig +256 -0
  163. package/src/primitives/canvas/render_generic_resources.zig +376 -0
  164. package/src/primitives/canvas/render_images.zig +247 -0
  165. package/src/primitives/canvas/render_layers.zig +260 -0
  166. package/src/primitives/canvas/render_paths.zig +347 -0
  167. package/src/primitives/canvas/render_resources.zig +51 -0
  168. package/src/primitives/canvas/render_tests.zig +3186 -0
  169. package/src/primitives/canvas/root.zig +744 -0
  170. package/src/primitives/canvas/serialization.zig +1528 -0
  171. package/src/primitives/canvas/svg_icon.zig +636 -0
  172. package/src/primitives/canvas/svg_icon_tests.zig +312 -0
  173. package/src/primitives/canvas/test_support.zig +422 -0
  174. package/src/primitives/canvas/testdata/markdown_document.md +83 -0
  175. package/src/primitives/canvas/tests.zig +30 -0
  176. package/src/primitives/canvas/text.zig +82 -0
  177. package/src/primitives/canvas/text_atlas.zig +408 -0
  178. package/src/primitives/canvas/text_batch_tests.zig +431 -0
  179. package/src/primitives/canvas/text_interaction.zig +615 -0
  180. package/src/primitives/canvas/text_layout.zig +1069 -0
  181. package/src/primitives/canvas/text_layout_cache.zig +281 -0
  182. package/src/primitives/canvas/text_layout_hash.zig +113 -0
  183. package/src/primitives/canvas/text_layout_types.zig +128 -0
  184. package/src/primitives/canvas/text_measure_cache.zig +279 -0
  185. package/src/primitives/canvas/text_metrics.zig +309 -0
  186. package/src/primitives/canvas/text_metrics_tests.zig +262 -0
  187. package/src/primitives/canvas/text_span_tests.zig +608 -0
  188. package/src/primitives/canvas/text_spans.zig +1010 -0
  189. package/src/primitives/canvas/text_tests.zig +1949 -0
  190. package/src/primitives/canvas/themes/geist.zig +470 -0
  191. package/src/primitives/canvas/tokens.zig +1522 -0
  192. package/src/primitives/canvas/ui.zig +2686 -0
  193. package/src/primitives/canvas/ui_markup.zig +3791 -0
  194. package/src/primitives/canvas/ui_markup_binary.zig +522 -0
  195. package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
  196. package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
  197. package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
  198. package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
  199. package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
  200. package/src/primitives/canvas/ui_markup_edit.zig +416 -0
  201. package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
  202. package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
  203. package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
  204. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
  205. package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
  206. package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
  207. package/src/primitives/canvas/ui_markup_view.zig +2352 -0
  208. package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
  209. package/src/primitives/canvas/ui_provenance.zig +96 -0
  210. package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
  211. package/src/primitives/canvas/ui_schema.zig +825 -0
  212. package/src/primitives/canvas/ui_schema_tests.zig +128 -0
  213. package/src/primitives/canvas/ui_tests.zig +1432 -0
  214. package/src/primitives/canvas/vector.zig +919 -0
  215. package/src/primitives/canvas/vector_tests.zig +547 -0
  216. package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
  217. package/src/primitives/canvas/virtual_extents.zig +540 -0
  218. package/src/primitives/canvas/widget_access.zig +208 -0
  219. package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
  220. package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
  221. package/src/primitives/canvas/widget_invalidation.zig +748 -0
  222. package/src/primitives/canvas/widget_layout.zig +2055 -0
  223. package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
  224. package/src/primitives/canvas/widget_metrics.zig +228 -0
  225. package/src/primitives/canvas/widget_render.zig +2534 -0
  226. package/src/primitives/canvas/widget_render_controls.zig +1563 -0
  227. package/src/primitives/canvas/widget_render_scroll.zig +136 -0
  228. package/src/primitives/canvas/widget_render_style.zig +626 -0
  229. package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
  230. package/src/primitives/canvas/widget_routing.zig +475 -0
  231. package/src/primitives/canvas/widget_runtime.zig +307 -0
  232. package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
  233. package/src/primitives/canvas/widget_semantics.zig +480 -0
  234. package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
  235. package/src/primitives/canvas/widget_tests.zig +8 -0
  236. package/src/primitives/canvas/widget_text_input.zig +351 -0
  237. package/src/primitives/canvas/widget_text_select.zig +147 -0
  238. package/src/primitives/canvas/widget_tree.zig +232 -0
  239. package/src/primitives/canvas/widgets.zig +1319 -0
  240. package/src/primitives/diagnostics/root.zig +445 -0
  241. package/src/primitives/geometry/root.zig +889 -0
  242. package/src/primitives/json/root.zig +272 -0
  243. package/src/primitives/platform_info/root.zig +450 -0
  244. package/src/primitives/trace/root.zig +569 -0
  245. package/src/root.zig +165 -0
  246. package/src/runtime/api.zig +431 -0
  247. package/src/runtime/async_bridge.zig +49 -0
  248. package/src/runtime/automation_commands.zig +686 -0
  249. package/src/runtime/automation_liveness_tests.zig +140 -0
  250. package/src/runtime/automation_snapshot.zig +246 -0
  251. package/src/runtime/automation_widget_dispatch.zig +617 -0
  252. package/src/runtime/bridge_dispatch_tests.zig +202 -0
  253. package/src/runtime/bridge_payload.zig +194 -0
  254. package/src/runtime/bridge_permission_tests.zig +551 -0
  255. package/src/runtime/bridge_responses.zig +414 -0
  256. package/src/runtime/builtin_bridge.zig +554 -0
  257. package/src/runtime/canvas_budget_edge_tests.zig +415 -0
  258. package/src/runtime/canvas_font_tests.zig +410 -0
  259. package/src/runtime/canvas_fonts.zig +229 -0
  260. package/src/runtime/canvas_frame.zig +1734 -0
  261. package/src/runtime/canvas_frame_animation_tests.zig +609 -0
  262. package/src/runtime/canvas_frame_cache_tests.zig +333 -0
  263. package/src/runtime/canvas_frame_helpers.zig +414 -0
  264. package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
  265. package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
  266. package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
  267. package/src/runtime/canvas_frame_retained_tests.zig +753 -0
  268. package/src/runtime/canvas_frame_tests.zig +9 -0
  269. package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
  270. package/src/runtime/canvas_image_tests.zig +362 -0
  271. package/src/runtime/canvas_images.zig +224 -0
  272. package/src/runtime/canvas_limits.zig +317 -0
  273. package/src/runtime/canvas_screenshot_tests.zig +289 -0
  274. package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
  275. package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
  276. package/src/runtime/canvas_widget_chart_tests.zig +249 -0
  277. package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
  278. package/src/runtime/canvas_widget_context_menu.zig +318 -0
  279. package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
  280. package/src/runtime/canvas_widget_control_tests.zig +2164 -0
  281. package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
  282. package/src/runtime/canvas_widget_display.zig +677 -0
  283. package/src/runtime/canvas_widget_event_tests.zig +1779 -0
  284. package/src/runtime/canvas_widget_events.zig +1098 -0
  285. package/src/runtime/canvas_widget_floating_tests.zig +695 -0
  286. package/src/runtime/canvas_widget_layout_tests.zig +26 -0
  287. package/src/runtime/canvas_widget_runtime.zig +1335 -0
  288. package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
  289. package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
  290. package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
  291. package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
  292. package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
  293. package/src/runtime/canvas_widget_state.zig +798 -0
  294. package/src/runtime/canvas_widget_text_tests.zig +2104 -0
  295. package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
  296. package/src/runtime/clock.zig +231 -0
  297. package/src/runtime/core.zig +927 -0
  298. package/src/runtime/effects.zig +4197 -0
  299. package/src/runtime/effects_audio_tests.zig +690 -0
  300. package/src/runtime/effects_clipboard_tests.zig +317 -0
  301. package/src/runtime/effects_fetch_tests.zig +1031 -0
  302. package/src/runtime/effects_file_tests.zig +407 -0
  303. package/src/runtime/effects_tests.zig +1251 -0
  304. package/src/runtime/flow.zig +1175 -0
  305. package/src/runtime/frame_profile.zig +221 -0
  306. package/src/runtime/gpu_surface_events.zig +639 -0
  307. package/src/runtime/launch_timing.zig +59 -0
  308. package/src/runtime/markdown_app_tests.zig +224 -0
  309. package/src/runtime/native_command_event_tests.zig +431 -0
  310. package/src/runtime/platform_bridge_tests.zig +8 -0
  311. package/src/runtime/platform_config_tests.zig +392 -0
  312. package/src/runtime/root.zig +150 -0
  313. package/src/runtime/runtime_event_tests.zig +1079 -0
  314. package/src/runtime/session_journal.zig +1522 -0
  315. package/src/runtime/session_record.zig +332 -0
  316. package/src/runtime/session_replay.zig +274 -0
  317. package/src/runtime/session_state.zig +30 -0
  318. package/src/runtime/session_tests.zig +399 -0
  319. package/src/runtime/shell_layout.zig +472 -0
  320. package/src/runtime/shell_layout_tests.zig +1246 -0
  321. package/src/runtime/shell_view_tests.zig +604 -0
  322. package/src/runtime/state.zig +110 -0
  323. package/src/runtime/system_services.zig +392 -0
  324. package/src/runtime/test_support.zig +79 -0
  325. package/src/runtime/tests.zig +30 -0
  326. package/src/runtime/ui_app.zig +3290 -0
  327. package/src/runtime/ui_app_huge_model_tests.zig +351 -0
  328. package/src/runtime/ui_app_provenance.zig +370 -0
  329. package/src/runtime/ui_app_tests.zig +3254 -0
  330. package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
  331. package/src/runtime/ui_app_window_tests.zig +442 -0
  332. package/src/runtime/validation.zig +206 -0
  333. package/src/runtime/view.zig +908 -0
  334. package/src/runtime/view_canvas.zig +850 -0
  335. package/src/runtime/view_widget_control.zig +433 -0
  336. package/src/runtime/view_widget_scroll.zig +360 -0
  337. package/src/runtime/view_widget_text.zig +275 -0
  338. package/src/runtime/view_widget_tree.zig +843 -0
  339. package/src/runtime/webview_view_bridge_tests.zig +664 -0
  340. package/src/runtime/widget_bridge.zig +231 -0
  341. package/src/runtime/window_command_bridge_tests.zig +403 -0
  342. package/src/runtime/window_storage.zig +255 -0
  343. package/src/runtime/window_view_runtime.zig +727 -0
  344. package/src/runtime/window_views.zig +334 -0
  345. package/src/security/root.zig +118 -0
  346. package/src/tooling/android.zig +1172 -0
  347. package/src/tooling/assets.zig +128 -0
  348. package/src/tooling/buildgraph.zig +379 -0
  349. package/src/tooling/cef.zig +671 -0
  350. package/src/tooling/codesign.zig +172 -0
  351. package/src/tooling/components/identity_tests.zig +185 -0
  352. package/src/tooling/components/stepper.zig +113 -0
  353. package/src/tooling/components/timeline.native +27 -0
  354. package/src/tooling/components/timeline_item.zig +131 -0
  355. package/src/tooling/default_icon.icns +0 -0
  356. package/src/tooling/default_icon.png +0 -0
  357. package/src/tooling/dev.zig +172 -0
  358. package/src/tooling/doctor.zig +349 -0
  359. package/src/tooling/eject_components.zig +170 -0
  360. package/src/tooling/embedlib.zig +290 -0
  361. package/src/tooling/ios.zig +528 -0
  362. package/src/tooling/manifest.zig +1939 -0
  363. package/src/tooling/package.zig +2388 -0
  364. package/src/tooling/process_tree.zig +114 -0
  365. package/src/tooling/raw_manifest.zig +193 -0
  366. package/src/tooling/root.zig +22 -0
  367. package/src/tooling/templates.zig +3191 -0
  368. package/src/tooling/toolchain.zig +371 -0
  369. package/src/tooling/verbs.zig +204 -0
  370. package/src/tooling/web_engine.zig +135 -0
  371. package/src/tooling/xcodeproj.zig +462 -0
  372. package/src/window_state/root.zig +507 -0
  373. package/index.js +0 -2
@@ -0,0 +1,1335 @@
1
+ const std = @import("std");
2
+ const geometry = @import("geometry");
3
+ const canvas = @import("canvas");
4
+ const canvas_limits = @import("canvas_limits.zig");
5
+ const platform = @import("../platform/root.zig");
6
+
7
+ /// Scratch for `RuntimeView.copyWidgetLayoutTree`'s reconcile pass. Too
8
+ /// large for the stack at the 1024-node budget (the semantics array alone
9
+ /// is ~270 KiB); the Runtime owns one instance (`canvas_widget_copy_scratch`)
10
+ /// and the single-threaded event loop makes sharing it safe.
11
+ pub const CanvasWidgetCopyScratch = struct {
12
+ source_semantics: [canvas_limits.max_canvas_widget_semantics_per_view]canvas.WidgetSemanticsNode = undefined,
13
+ control_entries: [canvas_limits.max_canvas_widget_nodes_per_view]CanvasWidgetControlReconcileEntry = undefined,
14
+ scroll_entries: [canvas_limits.max_canvas_widget_nodes_per_view]CanvasWidgetScrollReconcileEntry = undefined,
15
+ text_entries: [canvas_limits.max_canvas_widget_nodes_per_view]CanvasWidgetTextReconcileEntry = undefined,
16
+ text_bytes: [canvas_limits.max_canvas_widget_text_bytes_per_view]u8 = undefined,
17
+ };
18
+
19
+ const GpuSurfaceInputEvent = platform.GpuSurfaceInputEvent;
20
+
21
+ // -------------------------------------------------- reconcile id index
22
+ //
23
+ // The widget reconcile answers "what did the previous rebuild retain for
24
+ // this id?" once per node, against entry lists that scale with the view
25
+ // (control entries, source-control entries, source semantics, text
26
+ // entries). Linear scans made every rebuild O(n^2) at the 1024-node
27
+ // budget — the same shape the frame planners had before
28
+ // `plan_key_index` (see that file), and the same fix applies: an
29
+ // open-addressing probe table of entry indices whose chain preserves
30
+ // insertion order, so lookups return exactly what the scans returned
31
+ // (the lowest-index entry matching the caller's predicate). Outputs are
32
+ // byte-identical by construction; only the search cost changes.
33
+ const plan_key_index = canvas.plan_key_index;
34
+
35
+ /// Slot count sized for the per-view widget budgets (1024 nodes /
36
+ /// semantics entries) at the probe table's half-full bound.
37
+ const widget_id_index_slots = 2048;
38
+
39
+ /// Probe-table index over any entry slice keyed by an `id` field.
40
+ /// `build` decides per input whether the table pays for its reset
41
+ /// (`plan_key_index.min_entries_for_index`) and fits the half-full
42
+ /// bound; otherwise lookups keep the original linear scans, so small
43
+ /// views (the chart-tick floor) and oversized library inputs are
44
+ /// untouched.
45
+ pub fn CanvasWidgetIdIndex(comptime Entry: type) type {
46
+ return struct {
47
+ const Self = @This();
48
+ const Slots = plan_key_index.HashSlots(widget_id_index_slots);
49
+
50
+ table: Slots = .{},
51
+ entries: []const Entry = &.{},
52
+ indexed: bool = false,
53
+
54
+ pub fn build(self: *Self, entries: []const Entry) void {
55
+ self.entries = entries;
56
+ self.indexed = entries.len >= plan_key_index.min_entries_for_index and
57
+ plan_key_index.fitsHashSlots(widget_id_index_slots, entries.len);
58
+ if (!self.indexed) return;
59
+ self.table.reset();
60
+ for (entries, 0..) |entry, index| {
61
+ if (entry.id == 0) continue;
62
+ var probe = Slots.probe(plan_key_index.mixHash(entry.id));
63
+ while (self.table.next(&probe)) |_| {}
64
+ self.table.insert(probe, @intCast(index));
65
+ }
66
+ }
67
+
68
+ /// Lowest-index entry with this id — exactly the linear scan's
69
+ /// result (probe chains preserve insertion order; id-0 entries
70
+ /// can never match because id-0 queries return null up front,
71
+ /// matching every existing scan's contract).
72
+ pub fn first(self: *const Self, id: canvas.ObjectId) ?*const Entry {
73
+ if (id == 0) return null;
74
+ if (!self.indexed) {
75
+ for (self.entries) |*entry| {
76
+ if (entry.id == id) return entry;
77
+ }
78
+ return null;
79
+ }
80
+ var probe = Slots.probe(plan_key_index.mixHash(id));
81
+ while (self.table.next(&probe)) |candidate| {
82
+ if (self.entries[candidate].id == id) return &self.entries[candidate];
83
+ }
84
+ return null;
85
+ }
86
+
87
+ /// Lowest-index entry matching id AND kind — the two-field scan
88
+ /// predicate some reconcile passes use. Walks the whole probe
89
+ /// chain, so even (invalid) duplicate-id inputs resolve exactly
90
+ /// as the scan would.
91
+ pub fn firstWithKind(self: *const Self, id: canvas.ObjectId, kind: canvas.WidgetKind) ?*const Entry {
92
+ if (id == 0) return null;
93
+ if (!self.indexed) {
94
+ for (self.entries) |*entry| {
95
+ if (entry.id == id and entry.kind == kind) return entry;
96
+ }
97
+ return null;
98
+ }
99
+ var probe = Slots.probe(plan_key_index.mixHash(id));
100
+ while (self.table.next(&probe)) |candidate| {
101
+ const entry = &self.entries[candidate];
102
+ if (entry.id == id and entry.kind == kind) return entry;
103
+ }
104
+ return null;
105
+ }
106
+ };
107
+ }
108
+
109
+ pub const CanvasWidgetControlEntryIndex = CanvasWidgetIdIndex(CanvasWidgetControlReconcileEntry);
110
+ pub const CanvasWidgetSourceControlEntryIndex = CanvasWidgetIdIndex(CanvasWidgetSourceControlEntry);
111
+ pub const CanvasWidgetTextEntryIndex = CanvasWidgetIdIndex(CanvasWidgetTextReconcileEntry);
112
+ pub const CanvasWidgetSemanticsIndex = CanvasWidgetIdIndex(canvas.WidgetSemanticsNode);
113
+
114
+ /// Shared per-pass index scratch (~8 KiB of slots per table). The two
115
+ /// reconcile passes per rebuild (the staged reconcile, then the retained
116
+ /// copy) run back-to-back on the single-threaded event loop and each
117
+ /// rebuilds every table it uses, so one threadlocal set serves both —
118
+ /// the same pattern as the planners' probe-table scratch.
119
+ pub threadlocal var canvas_widget_reconcile_index_scratch: CanvasWidgetReconcileIndexScratch = .{};
120
+
121
+ pub const CanvasWidgetReconcileIndexScratch = struct {
122
+ controls: CanvasWidgetControlEntryIndex = .{},
123
+ source_controls: CanvasWidgetSourceControlEntryIndex = .{},
124
+ texts: CanvasWidgetTextEntryIndex = .{},
125
+ semantics: CanvasWidgetSemanticsIndex = .{},
126
+ };
127
+
128
+ pub const WidgetTextStorageRange = struct {
129
+ start: usize = 0,
130
+ end: usize = 0,
131
+ };
132
+
133
+ pub const CanvasWidgetScrollReconcileEntry = struct {
134
+ id: canvas.ObjectId = 0,
135
+ state: canvas.ScrollState = .{},
136
+ };
137
+
138
+ pub const CanvasWidgetControlReconcileEntry = struct {
139
+ id: canvas.ObjectId = 0,
140
+ kind: canvas.WidgetKind = .checkbox,
141
+ state: canvas.WidgetState = .{},
142
+ value: f32 = 0,
143
+ };
144
+
145
+ pub const CanvasWidgetTextReconcileEntry = struct {
146
+ id: canvas.ObjectId = 0,
147
+ kind: canvas.WidgetKind = .text_field,
148
+ text: []const u8 = &.{},
149
+ source_text_len: usize = 0,
150
+ source_text_hash: u64 = 0,
151
+ text_selection: ?canvas.TextSelection = null,
152
+ text_composition: ?canvas.TextRange = null,
153
+ value: f32 = 0,
154
+ };
155
+
156
+ pub const CanvasWidgetSourceScrollEntry = struct {
157
+ id: canvas.ObjectId = 0,
158
+ value: f32 = 0,
159
+ };
160
+
161
+ pub fn canvasWidgetSourceScrollById(entries: []const CanvasWidgetSourceScrollEntry, id: canvas.ObjectId) ?f32 {
162
+ for (entries) |entry| {
163
+ if (entry.id == id) return entry.value;
164
+ }
165
+ return null;
166
+ }
167
+
168
+ /// Scroll offsets and split fractions share the entry shape (id +
169
+ /// value) and the source-wins reconcile rule, so one collector serves
170
+ /// both.
171
+ pub fn canvasWidgetSourceValueKind(kind: canvas.WidgetKind) bool {
172
+ return kind == .scroll_view or kind == .split;
173
+ }
174
+
175
+ pub fn collectCanvasWidgetScrollOffsetEntries(
176
+ nodes: []const canvas.WidgetLayoutNode,
177
+ output: []CanvasWidgetSourceScrollEntry,
178
+ ) []const CanvasWidgetSourceScrollEntry {
179
+ var len: usize = 0;
180
+ for (nodes) |node| {
181
+ if (!canvasWidgetSourceValueKind(node.widget.kind) or node.widget.id == 0) continue;
182
+ if (len >= output.len) break;
183
+ output[len] = .{ .id = node.widget.id, .value = node.widget.value };
184
+ len += 1;
185
+ }
186
+ return output[0..len];
187
+ }
188
+
189
+ /// Scroll offsets follow the text-editing reconcile rule: the runtime-owned
190
+ /// offset (user scrolling) survives rebuilds as long as the SOURCE offset is
191
+ /// unchanged; a source-side change (programmatic scroll) wins. Runs as a
192
+ /// staged pass over the laid-out nodes because restoring the offset must
193
+ /// also TRANSLATE the region's descendants: the flex pass laid them out at
194
+ /// the source offset, and a value-only restore would render one whole
195
+ /// rebuild at the wrong scroll position (content snapped to the source
196
+ /// offset while the offset of record — and the scrollbar — stayed put).
197
+ pub fn restoreCanvasWidgetLayoutScrollOffsets(
198
+ nodes: []canvas.WidgetLayoutNode,
199
+ previous_runtime_offsets: []const CanvasWidgetSourceScrollEntry,
200
+ previous_source_offsets: []const CanvasWidgetSourceScrollEntry,
201
+ ) void {
202
+ for (nodes, 0..) |node, index| {
203
+ if (node.widget.kind != .scroll_view or node.widget.id == 0) continue;
204
+ const previous_runtime = canvasWidgetSourceScrollById(previous_runtime_offsets, node.widget.id) orelse continue;
205
+ const previous_source = canvasWidgetSourceScrollById(previous_source_offsets, node.widget.id) orelse continue;
206
+ if (node.widget.value != previous_source) continue;
207
+ if (node.widget.value == previous_runtime) continue;
208
+ const laid_out = node.widget.value;
209
+ nodes[index].widget.value = previous_runtime;
210
+ translateCanvasWidgetLayoutScrollDescendants(nodes, index, -(previous_runtime - laid_out));
211
+ }
212
+ }
213
+
214
+ pub const CanvasWidgetSourceTextEntry = struct {
215
+ id: canvas.ObjectId = 0,
216
+ kind: canvas.WidgetKind = .text_field,
217
+ text_len: usize = 0,
218
+ text_hash: u64 = 0,
219
+ };
220
+
221
+ /// The SOURCE-side selected state (and value, for sliders) of one
222
+ /// control on the previous rebuild, kept per view so the control
223
+ /// reconcile can tell a model-driven (controlled) widget from an
224
+ /// uncontrolled one.
225
+ pub const CanvasWidgetSourceControlEntry = struct {
226
+ id: canvas.ObjectId = 0,
227
+ kind: canvas.WidgetKind = .toggle_button,
228
+ selected: bool = false,
229
+ value: f32 = 0,
230
+ };
231
+
232
+ /// Which control kinds track their SOURCE state across rebuilds.
233
+ /// `toggle_button`: chips are the one boolean control whose retained
234
+ /// toggle fights model-driven exclusive groups. `slider`: the one
235
+ /// continuous control whose retained value fights a model-driven value
236
+ /// (progress-style sliders) — it follows the scroll reconcile rule
237
+ /// (source-side change wins, otherwise the retained drag survives).
238
+ /// The exclusive selectables (list/menu/data/segment rows, radios —
239
+ /// tree rows included when they are one of these kinds) follow the
240
+ /// slider rule for their SELECTED bool: a source-side flip wins (an
241
+ /// explicit `selected = false` after the model moved its selection
242
+ /// clears the retained wash), while a static source keeps the retained
243
+ /// (pointer-driven) selection. Toggles/checkboxes keep the
244
+ /// retained-wins contract locked by the control reconcile tests.
245
+ pub fn canvasWidgetSourceControlKind(kind: canvas.WidgetKind) bool {
246
+ // `accordion`: disclosure state tracks its source so a model-driven
247
+ // open/close (a flip, not a replay) wins reconcile — and arms the
248
+ // disclosure tween.
249
+ return kind == .toggle_button or kind == .slider or kind == .accordion or canvasWidgetSelectionClearsSiblings(kind);
250
+ }
251
+
252
+ pub fn collectCanvasWidgetSourceControlEntries(
253
+ nodes: []const canvas.WidgetLayoutNode,
254
+ output: []CanvasWidgetSourceControlEntry,
255
+ ) []const CanvasWidgetSourceControlEntry {
256
+ var len: usize = 0;
257
+ for (nodes) |node| {
258
+ if (node.widget.id == 0 or !canvasWidgetSourceControlKind(node.widget.kind)) continue;
259
+ if (len >= output.len) break;
260
+ output[len] = .{
261
+ .id = node.widget.id,
262
+ .kind = node.widget.kind,
263
+ .selected = canvasWidgetBooleanSelected(node.widget),
264
+ .value = node.widget.value,
265
+ };
266
+ len += 1;
267
+ }
268
+ return output[0..len];
269
+ }
270
+
271
+ pub fn canvasWidgetInteractionTargetExists(layout: canvas.WidgetLayoutTree, id: canvas.ObjectId) bool {
272
+ const index = canvasWidgetLayoutNodeIndexById(layout, id) orelse return false;
273
+ if (canvasWidgetLayoutNodeHidden(layout, index)) return false;
274
+ if (!canvasWidgetLayoutNodeFrameVisible(layout, index)) return false;
275
+ return canvasWidgetRuntimeHitTarget(layout.nodes[index].widget);
276
+ }
277
+
278
+ pub fn canvasWidgetSelectableTargetExists(layout: canvas.WidgetLayoutTree, id: canvas.ObjectId) bool {
279
+ const index = canvasWidgetLayoutNodeIndexById(layout, id) orelse return false;
280
+ if (canvasWidgetLayoutNodeHidden(layout, index)) return false;
281
+ const widget = layout.nodes[index].widget;
282
+ if (widget.id == 0 or widget.state.disabled) return false;
283
+ if (!canvasWidgetSelectionClearsSiblings(widget.kind)) return false;
284
+ return canvasWidgetSelectableTargetFrameAllowed(layout, index);
285
+ }
286
+
287
+ pub fn canvasWidgetSelectableTargetFrameAllowed(layout: canvas.WidgetLayoutTree, node_index: usize) bool {
288
+ if (node_index >= layout.nodes.len) return false;
289
+ const frame = layout.nodes[node_index].frame.normalized();
290
+ if (!frame.isEmpty()) return canvasWidgetLayoutNodeFrameVisible(layout, node_index);
291
+
292
+ var current = layout.nodes[node_index].parent_index;
293
+ while (current) |index| {
294
+ if (index >= layout.nodes.len) return true;
295
+ if (canvasWidgetClipsContent(layout.nodes[index].widget)) return false;
296
+ current = layout.nodes[index].parent_index;
297
+ }
298
+ return true;
299
+ }
300
+
301
+ pub fn canvasWidgetLayoutNodeIndexById(layout: canvas.WidgetLayoutTree, id: canvas.ObjectId) ?usize {
302
+ if (id == 0) return null;
303
+ for (layout.nodes, 0..) |node, index| {
304
+ if (node.widget.id == id) return index;
305
+ }
306
+ return null;
307
+ }
308
+
309
+ pub fn canvasWidgetLayoutNodeHidden(layout: canvas.WidgetLayoutTree, node_index: usize) bool {
310
+ var current: ?usize = node_index;
311
+ while (current) |index| {
312
+ if (index >= layout.nodes.len) return false;
313
+ const node = layout.nodes[index];
314
+ if (node.widget.semantics.hidden) return true;
315
+ current = node.parent_index;
316
+ }
317
+ return false;
318
+ }
319
+
320
+ pub fn canvasWidgetLayoutNodeFrameVisible(layout: canvas.WidgetLayoutTree, node_index: usize) bool {
321
+ if (node_index >= layout.nodes.len) return false;
322
+ const frame = layout.nodes[node_index].frame.normalized();
323
+ if (frame.isEmpty()) return false;
324
+ var current: usize = node_index;
325
+ while (true) {
326
+ // Anchored floating widgets escape ancestor clip regions (they
327
+ // render in the hoisted window-level pass).
328
+ if (canvas.widgetIsAnchored(layout.nodes[current].widget)) return true;
329
+ const index = layout.nodes[current].parent_index orelse return true;
330
+ if (index >= layout.nodes.len) return true;
331
+ const ancestor = layout.nodes[index];
332
+ if (canvasWidgetClipsContent(ancestor.widget) and geometry.RectF.intersection(frame, ancestor.frame.normalized()).isEmpty()) return false;
333
+ current = index;
334
+ }
335
+ }
336
+
337
+ pub fn canvasWidgetLayoutNodeClippedBounds(layout: canvas.WidgetLayoutTree, node_index: usize, bounds: geometry.RectF) ?geometry.RectF {
338
+ if (node_index >= layout.nodes.len) return null;
339
+ if (canvasWidgetLayoutNodeHidden(layout, node_index)) return null;
340
+
341
+ var clipped = bounds.normalized();
342
+ if (clipped.isEmpty()) return null;
343
+
344
+ var current: usize = node_index;
345
+ while (true) {
346
+ // Anchored floating widgets escape ancestor clip regions, so
347
+ // dirty bounds under them clip to the window only.
348
+ if (canvas.widgetIsAnchored(layout.nodes[current].widget)) break;
349
+ const index = layout.nodes[current].parent_index orelse break;
350
+ if (index >= layout.nodes.len) return null;
351
+ const ancestor = layout.nodes[index];
352
+ if (canvasWidgetClipsContent(ancestor.widget)) {
353
+ clipped = geometry.RectF.intersection(clipped, ancestor.frame.normalized());
354
+ if (clipped.isEmpty()) return null;
355
+ }
356
+ current = index;
357
+ }
358
+ return clipped;
359
+ }
360
+
361
+ pub fn canvasWidgetClipsContent(widget: canvas.Widget) bool {
362
+ return widget.kind == .scroll_view or widget.layout.clip_content;
363
+ }
364
+
365
+ pub fn canvasWidgetRuntimeHitTarget(widget: canvas.Widget) bool {
366
+ // Widget-level hit-target-ness lives in one place (canvas
367
+ // widget_access.zig: kind predicate plus bound press/toggle handlers)
368
+ // so the runtime, the engines' hit test, and the markup validation of
369
+ // pointer handlers can never drift.
370
+ return canvas.widgetIsHitTarget(widget);
371
+ }
372
+
373
+ /// A dismissed floating/dismissible surface: the structural id (for the
374
+ /// `canvas_widget_dismiss` app event that lets a TEA model own the close)
375
+ /// plus the dirty bounds to invalidate. The engine-side hide is the
376
+ /// optimistic echo; the source tree is truth on the next rebuild.
377
+ pub const CanvasWidgetSurfaceDismissal = struct {
378
+ id: canvas.ObjectId,
379
+ dirty: geometry.RectF,
380
+ };
381
+
382
+ pub fn canvasWidgetDismissibleSurfaceKind(kind: canvas.WidgetKind) bool {
383
+ // Single source of truth in canvas widget_access.zig, shared with the
384
+ // semantics layer's `dismiss` action and the builder's on_dismiss
385
+ // teaching warning.
386
+ return canvas.widgetKindDismissibleSurface(kind);
387
+ }
388
+
389
+ pub fn canvasWidgetEditableTextKind(kind: canvas.WidgetKind) bool {
390
+ return kind == .input or kind == .text_field or kind == .search_field or kind == .combobox or kind == .textarea;
391
+ }
392
+
393
+ /// Clipboard paste text clamped to what the view's shared widget text
394
+ /// storage can absorb (the bytes the edit replaces are freed first).
395
+ /// Clamping lands on a UTF-8 boundary; `truncated` is the loud flag the
396
+ /// runtime forwards to apps on the keyboard event.
397
+ pub const CanvasWidgetPasteClamp = struct {
398
+ text: []const u8 = "",
399
+ truncated: bool = false,
400
+ };
401
+
402
+ pub fn clampCanvasWidgetPasteText(widget: canvas.Widget, view_text_len: usize, text: []const u8) CanvasWidgetPasteClamp {
403
+ const capacity = @import("canvas_limits.zig").max_canvas_widget_text_bytes_per_view;
404
+ const replaced_len = blk: {
405
+ if (widget.text_composition) |composition| break :blk composition.byteLen(widget.text.len);
406
+ if (canvas.widgetTextSelectionRange(widget)) |range| break :blk range.byteLen(widget.text.len);
407
+ break :blk 0;
408
+ };
409
+ const used = view_text_len -| replaced_len;
410
+ const available = capacity -| used;
411
+ if (text.len <= available) return .{ .text = text };
412
+ const clamped = canvas.snapTextOffset(text, available);
413
+ return .{ .text = text[0..clamped], .truncated = true };
414
+ }
415
+
416
+ pub fn canvasWidgetSingleLineTextKind(kind: canvas.WidgetKind) bool {
417
+ return kind == .input or kind == .text_field or kind == .search_field or kind == .combobox;
418
+ }
419
+
420
+ pub fn canvasWidgetScrollableKind(kind: canvas.WidgetKind) bool {
421
+ return kind == .scroll_view or kind == .textarea;
422
+ }
423
+
424
+ pub fn canvasWidgetRuntimeControlKind(kind: canvas.WidgetKind) bool {
425
+ return switch (kind) {
426
+ .accordion,
427
+ .checkbox,
428
+ .radio,
429
+ .switch_control,
430
+ .toggle,
431
+ .toggle_button,
432
+ .slider,
433
+ .resizable,
434
+ .list_item,
435
+ .menu_item,
436
+ .data_cell,
437
+ .segmented_control,
438
+ => true,
439
+ else => false,
440
+ };
441
+ }
442
+
443
+ pub fn canvasWidgetResizableMinWidth(widget: canvas.Widget) f32 {
444
+ return @max(@as(f32, 48), widget.frame.height);
445
+ }
446
+
447
+ pub fn collectCanvasWidgetControlReconcileEntries(
448
+ nodes: []const canvas.WidgetLayoutNode,
449
+ output: []CanvasWidgetControlReconcileEntry,
450
+ ) []const CanvasWidgetControlReconcileEntry {
451
+ var len: usize = 0;
452
+ for (nodes) |node| {
453
+ if (node.widget.id == 0 or !canvasWidgetRuntimeControlKind(node.widget.kind)) continue;
454
+ if (len >= output.len) break;
455
+ output[len] = .{
456
+ .id = node.widget.id,
457
+ .kind = node.widget.kind,
458
+ .state = node.widget.state,
459
+ .value = if (node.widget.kind == .resizable) node.frame.width else node.widget.value,
460
+ };
461
+ len += 1;
462
+ }
463
+ return output[0..len];
464
+ }
465
+
466
+ pub fn collectCanvasWidgetScrollReconcileEntries(
467
+ nodes: []const canvas.WidgetLayoutNode,
468
+ states: []const canvas.ScrollState,
469
+ output: []CanvasWidgetScrollReconcileEntry,
470
+ ) []const CanvasWidgetScrollReconcileEntry {
471
+ var len: usize = 0;
472
+ const count = @min(nodes.len, states.len);
473
+ for (nodes[0..count], 0..) |node, index| {
474
+ if (node.widget.kind != .scroll_view or node.widget.id == 0) continue;
475
+ if (len >= output.len) break;
476
+ output[len] = .{ .id = node.widget.id, .state = states[index] };
477
+ len += 1;
478
+ }
479
+ return output[0..len];
480
+ }
481
+
482
+ pub fn canvasWidgetScrollStateForLayoutNode(
483
+ node: canvas.WidgetLayoutNode,
484
+ previous: []const CanvasWidgetScrollReconcileEntry,
485
+ ) canvas.ScrollState {
486
+ var state = canvas.ScrollState{ .offset = node.widget.value };
487
+ if (node.widget.kind != .scroll_view or node.widget.id == 0) return state;
488
+ for (previous) |entry| {
489
+ if (entry.id == node.widget.id) {
490
+ state.velocity = entry.state.velocity;
491
+ return state;
492
+ }
493
+ }
494
+ return state;
495
+ }
496
+
497
+ pub fn collectCanvasWidgetTextReconcileEntries(
498
+ nodes: []const canvas.WidgetLayoutNode,
499
+ source_entries: []const CanvasWidgetSourceTextEntry,
500
+ output: []CanvasWidgetTextReconcileEntry,
501
+ text_storage: []u8,
502
+ text_len: *usize,
503
+ ) anyerror![]const CanvasWidgetTextReconcileEntry {
504
+ var len: usize = 0;
505
+ for (nodes) |node| {
506
+ if (node.widget.id == 0 or !canvasWidgetTextReconcileKind(node.widget)) continue;
507
+ if (len >= output.len) break;
508
+ const text_range = try appendWidgetTextStorageRange(text_storage, text_len, node.widget.text);
509
+ const source_text = canvasWidgetSourceTextByIdKind(source_entries, node.widget.id, node.widget.kind) orelse canvasWidgetSourceTextFingerprint(node.widget.text);
510
+ output[len] = .{
511
+ .id = node.widget.id,
512
+ .kind = node.widget.kind,
513
+ .text = text_storage[text_range.start..text_range.end],
514
+ .source_text_len = source_text.len,
515
+ .source_text_hash = source_text.hash,
516
+ .text_selection = node.widget.text_selection,
517
+ .text_composition = node.widget.text_composition,
518
+ .value = node.widget.value,
519
+ };
520
+ len += 1;
521
+ }
522
+ return output[0..len];
523
+ }
524
+
525
+ /// Which widgets the text reconcile pass tracks across rebuilds: every
526
+ /// editable text input, plus static `.text` widgets that currently carry
527
+ /// a selection (bounded to the view's single static selection, so the
528
+ /// reconcile text storage never inflates for text-heavy views).
529
+ fn canvasWidgetTextReconcileKind(widget: canvas.Widget) bool {
530
+ if (canvasWidgetEditableTextKind(widget.kind)) return true;
531
+ if (widget.kind != .text) return false;
532
+ const selection = widget.text_selection orelse return false;
533
+ return !selection.isCollapsed(widget.text.len);
534
+ }
535
+
536
+ pub const CanvasWidgetSourceTextFingerprint = struct {
537
+ len: usize = 0,
538
+ hash: u64 = 0,
539
+ };
540
+
541
+ pub fn canvasWidgetSourceTextFingerprint(text: []const u8) CanvasWidgetSourceTextFingerprint {
542
+ return .{
543
+ .len = text.len,
544
+ .hash = std.hash.Wyhash.hash(0, text),
545
+ };
546
+ }
547
+
548
+ pub fn canvasWidgetSourceTextByIdKind(
549
+ entries: []const CanvasWidgetSourceTextEntry,
550
+ id: canvas.ObjectId,
551
+ kind: canvas.WidgetKind,
552
+ ) ?CanvasWidgetSourceTextFingerprint {
553
+ for (entries) |entry| {
554
+ if (entry.id != id or entry.kind != kind) continue;
555
+ return .{
556
+ .len = entry.text_len,
557
+ .hash = entry.text_hash,
558
+ };
559
+ }
560
+ return null;
561
+ }
562
+
563
+ pub fn canvasWidgetLayoutNodeWithControlReconcileState(
564
+ node: canvas.WidgetLayoutNode,
565
+ layout: canvas.WidgetLayoutTree,
566
+ node_index: usize,
567
+ previous: *const CanvasWidgetControlEntryIndex,
568
+ previous_source_controls: *const CanvasWidgetSourceControlEntryIndex,
569
+ ) canvas.WidgetLayoutNode {
570
+ var copy = node;
571
+ if (copy.widget.id == 0 or !canvasWidgetRuntimeControlKind(copy.widget.kind)) return copy;
572
+ if (copy.widget.state.disabled or canvasWidgetLayoutNodeHidden(layout, node_index)) return copy;
573
+
574
+ if (previous.firstWithKind(copy.widget.id, copy.widget.kind)) |entry| {
575
+ switch (copy.widget.kind) {
576
+ .toggle_button => {
577
+ // Chips: a toggle-button whose SOURCE asserts
578
+ // selected — on this rebuild, or on the previous one
579
+ // (tracked in the view's source control entries) — is
580
+ // model-driven, and the source wins over the retained
581
+ // toggle, so an exclusive chip group shows exactly the
582
+ // model's selection instead of every chip ever pressed.
583
+ // Only a toggle-button whose source has stayed
584
+ // unselected keeps the retained (uncontrolled) state.
585
+ const source_selected = canvasWidgetBooleanSelected(copy.widget);
586
+ const previously_selected = if (previous_source_controls.firstWithKind(copy.widget.id, copy.widget.kind)) |source_entry|
587
+ source_entry.selected
588
+ else
589
+ false;
590
+ const selected = if (source_selected or previously_selected)
591
+ source_selected
592
+ else
593
+ entry.state.selected or entry.value >= 0.5;
594
+ copy.widget.state.selected = selected;
595
+ copy.widget.value = if (selected) 1 else 0;
596
+ },
597
+ .checkbox, .switch_control, .toggle => {
598
+ const selected = entry.state.selected or entry.value >= 0.5;
599
+ copy.widget.state.selected = selected;
600
+ copy.widget.value = if (selected) 1 else 0;
601
+ },
602
+ .accordion => {
603
+ // Disclosure state follows the exclusive-selectable
604
+ // reconcile rule: a source-side FLIP wins — a model
605
+ // that opens or closes a section programmatically (an
606
+ // "expand all" affordance, deep-link restore) must see
607
+ // it land — while a source replaying the same value
608
+ // keeps the retained (pointer-driven) state, so
609
+ // uncontrolled accordions keep working with zero app
610
+ // wiring. The flip is also what arms the default-on
611
+ // disclosure tween in `setCanvasWidgetLayout`.
612
+ const source_selected = canvasWidgetBooleanSelected(copy.widget);
613
+ const previous_source = previous_source_controls.firstWithKind(copy.widget.id, copy.widget.kind);
614
+ const source_moved = if (previous_source) |source_entry| source_selected != source_entry.selected else false;
615
+ const selected = if (source_moved)
616
+ source_selected
617
+ else
618
+ entry.state.selected or entry.value >= 0.5;
619
+ copy.widget.state.selected = selected;
620
+ copy.widget.value = if (selected) 1 else 0;
621
+ },
622
+ .slider => {
623
+ // Sliders follow the scroll reconcile rule: the
624
+ // runtime-owned value (a user drag) survives rebuilds
625
+ // only while the SOURCE value is unchanged; a
626
+ // source-side move (a model-driven value — playback
627
+ // progress, a synced setting) wins. A slider whose
628
+ // source never moves keeps the retained (uncontrolled)
629
+ // contract, and a LIVE drag always keeps the thumb —
630
+ // a mid-gesture source tick must not yank it.
631
+ const previous_source = previous_source_controls.firstWithKind(copy.widget.id, copy.widget.kind);
632
+ const source_moved = if (previous_source) |source_entry|
633
+ copy.widget.value != source_entry.value
634
+ else
635
+ false;
636
+ if (!source_moved or entry.state.pressed) copy.widget.value = entry.value;
637
+ copy.widget.value = std.math.clamp(copy.widget.value, 0, 1);
638
+ },
639
+ .resizable => {
640
+ const width = @max(canvasWidgetResizableMinWidth(copy.widget), entry.value);
641
+ copy.frame.width = width;
642
+ copy.widget.frame.width = width;
643
+ },
644
+ .radio, .list_item, .menu_item, .data_cell, .segmented_control => {
645
+ // Exclusive selectables follow the slider/scroll
646
+ // reconcile rule on their selected bool: a source-side
647
+ // FLIP wins — a model that moved its selection clears
648
+ // the old row's retained wash with an explicit
649
+ // `selected = false` and lights the new one — while a
650
+ // source that replays the same value keeps the retained
651
+ // (pointer-driven) selection, so uncontrolled lists and
652
+ // trees keep working with zero app wiring.
653
+ const source_selected = canvasWidgetBooleanSelected(copy.widget);
654
+ const previous_source = previous_source_controls.firstWithKind(copy.widget.id, copy.widget.kind);
655
+ const source_moved = if (previous_source) |source_entry| source_selected != source_entry.selected else false;
656
+ const selected = if (source_moved)
657
+ source_selected
658
+ else
659
+ entry.state.selected or entry.value >= 0.5;
660
+ copy.widget.state.selected = selected;
661
+ copy.widget.value = if (selected) 1 else 0;
662
+ },
663
+ else => {},
664
+ }
665
+ }
666
+ return copy;
667
+ }
668
+
669
+ pub fn canvasWidgetLayoutNodeWithTextReconcileState(
670
+ node: canvas.WidgetLayoutNode,
671
+ layout: canvas.WidgetLayoutTree,
672
+ node_index: usize,
673
+ previous: *const CanvasWidgetTextEntryIndex,
674
+ ) canvas.WidgetLayoutNode {
675
+ var copy = node;
676
+ if (copy.widget.id == 0) return copy;
677
+ if (copy.widget.state.disabled or canvasWidgetLayoutNodeHidden(layout, node_index)) return copy;
678
+
679
+ if (copy.widget.kind == .text) {
680
+ // Static text selections survive rebuilds only while the source
681
+ // text is byte-identical; changed text drops the selection.
682
+ if (previous.firstWithKind(copy.widget.id, copy.widget.kind)) |entry| {
683
+ if (copy.widget.text_selection == null and std.mem.eql(u8, entry.text, copy.widget.text)) {
684
+ copy.widget.text_selection = entry.text_selection;
685
+ }
686
+ }
687
+ return copy;
688
+ }
689
+ if (!canvasWidgetEditableTextKind(copy.widget.kind)) return copy;
690
+
691
+ // NOTE: this pass's scan predicate has a third clause (the
692
+ // source-changed-and-text-diverged entry is SKIPPED, not matched), so
693
+ // the id/kind lookup alone is not the match — but retained trees hold
694
+ // at most one entry per (id, kind) (unique widget ids), so "first
695
+ // id/kind match, then apply the clause" resolves identically to the
696
+ // scan's "first entry passing all clauses".
697
+ if (previous.firstWithKind(copy.widget.id, copy.widget.kind)) |entry| {
698
+ const next_source_text = canvasWidgetSourceTextFingerprint(copy.widget.text);
699
+ const source_unchanged = entry.source_text_len == next_source_text.len and entry.source_text_hash == next_source_text.hash;
700
+ const source_matches_runtime_text = std.mem.eql(u8, entry.text, copy.widget.text);
701
+ if (!source_unchanged and !source_matches_runtime_text) return copy;
702
+ if (source_unchanged) copy.widget.text = entry.text;
703
+ if (copy.widget.kind == .textarea) copy.widget.value = entry.value;
704
+ if (copy.widget.text_selection == null and copy.widget.text_composition == null) {
705
+ copy.widget.text_selection = entry.text_selection;
706
+ copy.widget.text_composition = entry.text_composition;
707
+ }
708
+ }
709
+ return copy;
710
+ }
711
+
712
+ fn objectIdInList(ids: []const canvas.ObjectId, id: canvas.ObjectId) bool {
713
+ for (ids) |candidate| {
714
+ if (candidate == id) return true;
715
+ }
716
+ return false;
717
+ }
718
+
719
+ pub fn canvasWidgetLayoutTreeWithRuntimeReconcileState(
720
+ previous: canvas.WidgetLayoutTree,
721
+ next: canvas.WidgetLayoutTree,
722
+ source_semantics: []const canvas.WidgetSemanticsNode,
723
+ previous_source_text_entries: []const CanvasWidgetSourceTextEntry,
724
+ previous_source_scroll_entries: []const CanvasWidgetSourceScrollEntry,
725
+ previous_source_control_entries: []const CanvasWidgetSourceControlEntry,
726
+ node_buffer: []canvas.WidgetLayoutNode,
727
+ control_entries: []CanvasWidgetControlReconcileEntry,
728
+ scroll_offset_entries: []CanvasWidgetSourceScrollEntry,
729
+ text_entries: []CanvasWidgetTextReconcileEntry,
730
+ text_storage: []u8,
731
+ tokens: canvas.DesignTokens,
732
+ armed_split_tween_ids: []const canvas.ObjectId,
733
+ pressed_split_id: canvas.ObjectId,
734
+ ) anyerror!canvas.WidgetLayoutTree {
735
+ if (next.nodes.len > node_buffer.len) return error.WidgetNodeLimitReached;
736
+
737
+ const previous_control_states = collectCanvasWidgetControlReconcileEntries(
738
+ previous.nodes,
739
+ control_entries,
740
+ );
741
+ const previous_runtime_offsets = collectCanvasWidgetScrollOffsetEntries(
742
+ previous.nodes,
743
+ scroll_offset_entries,
744
+ );
745
+ var text_len: usize = 0;
746
+ const previous_text_states = try collectCanvasWidgetTextReconcileEntries(
747
+ previous.nodes,
748
+ previous_source_text_entries,
749
+ text_entries,
750
+ text_storage,
751
+ &text_len,
752
+ );
753
+
754
+ // Split fractions reconcile FIRST, as a staged copy of the laid-out
755
+ // tree, so the per-node passes below see final geometry. Outer
756
+ // splits restore before nested ones (ascending node order), so a
757
+ // nested split re-laid (or slid) by its ancestor still restores its
758
+ // own fraction afterwards. Two restore shapes:
759
+ // - SETTLED runtime-owned fraction (a past drag, no tween in
760
+ // flight): re-run the split's child layout in place — content
761
+ // honestly wraps at the restored width;
762
+ // - TWEEN in flight (armed already, or arming on this rebuild
763
+ // because the source moved with a nonzero duration): keep the
764
+ // source's child layout — it is laid at the tween's TARGET
765
+ // fraction — and slide the pane boundary back geometrically.
766
+ // Content never re-wraps mid-flight; the pane clip crops the
767
+ // overflowing side and the tween reveals it one presented
768
+ // frame at a time (the disclosure doctrine, horizontal).
769
+ const staged_nodes = node_buffer[0..next.nodes.len];
770
+ @memcpy(staged_nodes, next.nodes);
771
+ for (staged_nodes, 0..) |node, index| {
772
+ if (node.widget.kind != .split or node.widget.id == 0) continue;
773
+ const tween_armed = objectIdInList(armed_split_tween_ids, node.widget.id);
774
+ const previous_runtime = canvasWidgetSourceScrollById(previous_runtime_offsets, node.widget.id) orelse {
775
+ // A FRESH split (no retained fraction): a declared enter
776
+ // origin slides the first layout's boundary to the origin
777
+ // pose — children keep the declared value's (target) wrap —
778
+ // so the tween armed right after this reconcile eases it in
779
+ // instead of the mount popping to its value.
780
+ if (node.widget.resize_duration_ms != 0 and node.widget.resize_origin >= 0 and node.widget.children.len != 0) {
781
+ canvas.slideSplitChildren(node.frame, node.widget.resize_origin, index, staged_nodes);
782
+ }
783
+ continue;
784
+ };
785
+ const previous_source = canvasWidgetSourceScrollById(previous_source_scroll_entries, node.widget.id) orelse continue;
786
+ // Source-wins: the runtime-owned fraction survives rebuilds only
787
+ // while the SOURCE fraction is unchanged; a source-side change
788
+ // (the model echoing or driving the fraction) wins — UNLESS the
789
+ // split declares a layout tween (`resize_duration_ms` nonzero)
790
+ // or one is already in flight: then the moved source value is a
791
+ // TARGET, the rendered fraction stays where it is, and the
792
+ // runtime's tween lowering (armed right after this reconcile
793
+ // lands, in setCanvasWidgetLayout) eases it there one presented
794
+ // frame at a time. Reduced motion still snaps: the tween
795
+ // lowering's snap path applies the target through this same
796
+ // mutation family in the same rebuild.
797
+ const source_moved = node.widget.value != previous_source;
798
+ if (source_moved and node.widget.resize_duration_ms == 0 and !tween_armed) continue;
799
+ if (node.widget.value == previous_runtime) continue;
800
+ staged_nodes[index].widget.value = previous_runtime;
801
+ // Retained trees clear children; a split without them keeps the
802
+ // value restore only (frames follow on the next full layout).
803
+ if (node.widget.children.len == 0) continue;
804
+ // A pressed divider is a live drag: its echo rebuilds must keep
805
+ // re-wrapping at the dragged width (the pinned drag behavior),
806
+ // so the slide shape only applies to tween-owned motion.
807
+ const dragging = pressed_split_id != 0 and node.widget.id == pressed_split_id;
808
+ if (!dragging and (tween_armed or (source_moved and node.widget.resize_duration_ms != 0))) {
809
+ canvas.slideSplitChildren(node.frame, previous_runtime, index, staged_nodes);
810
+ } else {
811
+ try canvas.relayoutSplitChildren(staged_nodes[index].widget, node.frame, index, node.depth, node_buffer, tokens);
812
+ }
813
+ }
814
+
815
+ // Scroll restore is staged too (after splits, so translated frames
816
+ // are final geometry): the retained offset comes back WITH its
817
+ // descendants translated to match. Engine-side clamping happens at
818
+ // the caller AFTER native scroll drivers are stamped — a rebuild
819
+ // mid-rubber-band must not clamp an offset the OS scroller owns.
820
+ restoreCanvasWidgetLayoutScrollOffsets(staged_nodes, previous_runtime_offsets, previous_source_scroll_entries);
821
+
822
+ const index_scratch = &canvas_widget_reconcile_index_scratch;
823
+ index_scratch.controls.build(previous_control_states);
824
+ index_scratch.source_controls.build(previous_source_control_entries);
825
+ index_scratch.texts.build(previous_text_states);
826
+ index_scratch.semantics.build(source_semantics);
827
+
828
+ const staged = canvas.WidgetLayoutTree{ .nodes = staged_nodes };
829
+ for (staged_nodes, 0..) |node, index| {
830
+ const text_copy = canvasWidgetLayoutNodeWithTextReconcileState(node, staged, index, &index_scratch.texts);
831
+ const control_copy = canvasWidgetLayoutNodeWithControlReconcileState(text_copy, staged, index, &index_scratch.controls, &index_scratch.source_controls);
832
+ node_buffer[index] = canvasWidgetLayoutNodeWithSourceSemantics(control_copy, &index_scratch.semantics);
833
+ }
834
+ const reconciled = node_buffer[0..next.nodes.len];
835
+ clampCanvasWidgetLayoutTextOffsets(reconciled, tokens);
836
+ return .{ .nodes = reconciled };
837
+ }
838
+
839
+ pub fn canvasWidgetLayoutNodeWithSourceSemantics(
840
+ node: canvas.WidgetLayoutNode,
841
+ source_semantics: *const CanvasWidgetSemanticsIndex,
842
+ ) canvas.WidgetLayoutNode {
843
+ var copy = node;
844
+ if (source_semantics.first(node.widget.id)) |semantic_node| {
845
+ if (semantic_node.list.present) {
846
+ copy.widget.semantics.list_item_index = semantic_node.list.item_index;
847
+ copy.widget.semantics.list_item_count = semantic_node.list.item_count;
848
+ }
849
+ }
850
+ return copy;
851
+ }
852
+
853
+ pub fn applyCanvasWidgetSourceScrollSemantics(
854
+ nodes: []canvas.WidgetSemanticsNode,
855
+ source_semantics: *const CanvasWidgetSemanticsIndex,
856
+ ) void {
857
+ for (nodes) |*node| {
858
+ const source = source_semantics.first(node.id) orelse continue;
859
+ if (!source.scroll.present) continue;
860
+ node.value = source.value;
861
+ node.scroll = source.scroll;
862
+ node.actions = source.actions;
863
+ node.focusable = source.focusable;
864
+ }
865
+ }
866
+
867
+ pub fn clampCanvasWidgetLayoutScrollOffsets(nodes: []canvas.WidgetLayoutNode, states: ?[]canvas.ScrollState) void {
868
+ for (nodes, 0..) |node, index| {
869
+ if (node.widget.kind != .scroll_view) continue;
870
+ // Legacy virtualized containers are model-driven: the source
871
+ // offset is the only channel, so the engine never clamps it.
872
+ // Runtime-scrolled virtual lists (declared item count) clamp
873
+ // like plain scroll views, against the VIRTUAL content extent.
874
+ if (node.widget.layout.virtualized and !canvas.widgetVirtualRuntimeScrolled(node.widget)) continue;
875
+ // Native scroll drivers own clamping: the OS scroller constrains
876
+ // its own contentOffset (including mid-rubber-band rebuilds, which
877
+ // an engine clamp here would fight) and reports the settled offset
878
+ // back through the driver event.
879
+ if (node.widget.native_scroll) continue;
880
+
881
+ const viewport = node.frame.inset(node.widget.layout.padding).normalized();
882
+ if (viewport.isEmpty()) continue;
883
+
884
+ const content_extent = canvasWidgetLayoutScrollContentExtent(nodes, index, viewport);
885
+ const max_offset = @max(0, content_extent - viewport.height);
886
+ const current_offset = node.widget.value;
887
+ const next_offset = std.math.clamp(@max(0, current_offset), 0, max_offset);
888
+ if (next_offset == current_offset) continue;
889
+
890
+ const offset_delta = next_offset - current_offset;
891
+ nodes[index].widget.value = next_offset;
892
+ translateCanvasWidgetLayoutScrollDescendants(nodes, index, -offset_delta);
893
+ if (states) |scroll_states| {
894
+ if (index < scroll_states.len) {
895
+ scroll_states[index].offset = next_offset;
896
+ scroll_states[index].velocity = 0;
897
+ scroll_states[index].viewport_extent = viewport.height;
898
+ scroll_states[index].content_extent = content_extent;
899
+ }
900
+ }
901
+ }
902
+ }
903
+
904
+ pub fn clampCanvasWidgetLayoutTextOffsets(nodes: []canvas.WidgetLayoutNode, tokens: canvas.DesignTokens) void {
905
+ for (nodes) |*node| {
906
+ if (node.widget.kind != .textarea) continue;
907
+ node.widget.value = canvas.clampedTextInputScrollOffsetForWidget(node.widget, tokens, node.widget.value);
908
+ }
909
+ }
910
+
911
+ pub fn canvasWidgetLayoutScrollContentExtent(nodes: []const canvas.WidgetLayoutNode, scroll_index: usize, viewport: geometry.RectF) f32 {
912
+ if (scroll_index >= nodes.len) return 0;
913
+ const scroll_node = nodes[scroll_index];
914
+ if (scroll_node.widget.layout.virtualized) {
915
+ return @max(viewport.height, canvas.virtualWidgetScrollContentExtent(scroll_node.widget, viewport.height));
916
+ }
917
+ const scroll_depth = scroll_node.depth;
918
+ const offset = scroll_node.widget.value;
919
+ var bottom = viewport.maxY();
920
+ var index = scroll_index + 1;
921
+ while (index < nodes.len and nodes[index].depth > scroll_depth) : (index += 1) {
922
+ bottom = @max(bottom, nodes[index].frame.maxY() + offset);
923
+ }
924
+ return @max(0, bottom - viewport.y);
925
+ }
926
+
927
+ pub fn translateCanvasWidgetLayoutScrollDescendants(nodes: []canvas.WidgetLayoutNode, scroll_index: usize, dy: f32) void {
928
+ if (scroll_index >= nodes.len) return;
929
+ const scroll_depth = nodes[scroll_index].depth;
930
+ var index = scroll_index + 1;
931
+ while (index < nodes.len and nodes[index].depth > scroll_depth) : (index += 1) {
932
+ nodes[index].frame = nodes[index].frame.translate(geometry.OffsetF.init(0, dy));
933
+ nodes[index].widget.frame = nodes[index].frame;
934
+ }
935
+ }
936
+
937
+ pub fn appendWidgetTextStorageRange(buffer: []u8, len: *usize, value: []const u8) anyerror!WidgetTextStorageRange {
938
+ const end = len.* + value.len;
939
+ if (end > buffer.len) return error.WidgetTextTooLarge;
940
+ const start = len.*;
941
+ @memcpy(buffer[start..end], value);
942
+ len.* = end;
943
+ return .{ .start = start, .end = end };
944
+ }
945
+
946
+ pub fn canvasWidgetTextEditUnchanged(previous: canvas.TextEditState, next: canvas.TextEditState) bool {
947
+ return std.mem.eql(u8, previous.text, next.text) and
948
+ canvasTextSelectionsEqual(previous.selection, next.selection) and
949
+ optionalCanvasTextRangesEqual(previous.composition, next.composition);
950
+ }
951
+
952
+ pub fn canvasTextSelectionsEqual(a: canvas.TextSelection, b: canvas.TextSelection) bool {
953
+ return a.anchor == b.anchor and a.focus == b.focus;
954
+ }
955
+
956
+ pub fn textSelectionCollapsedAt(selection: ?canvas.TextSelection, offset: usize) bool {
957
+ const value = selection orelse return true;
958
+ return value.anchor == offset and value.focus == offset;
959
+ }
960
+
961
+ pub fn optionalCanvasTextRangesEqual(a: ?canvas.TextRange, b: ?canvas.TextRange) bool {
962
+ if (a) |left| {
963
+ if (b) |right| return left.start == right.start and left.end == right.end;
964
+ return false;
965
+ }
966
+ return b == null;
967
+ }
968
+
969
+ pub fn canvasWidgetCommandable(kind: canvas.WidgetKind) bool {
970
+ return switch (kind) {
971
+ .accordion, .button, .toggle_button, .icon_button, .select, .combobox, .menu_item, .list_item, .data_cell, .segmented_control, .checkbox, .radio, .switch_control, .toggle => true,
972
+ else => false,
973
+ };
974
+ }
975
+
976
+ pub fn canvasWidgetCommandFiresOnPointerDown(kind: canvas.WidgetKind) bool {
977
+ return switch (kind) {
978
+ .select, .combobox => true,
979
+ else => false,
980
+ };
981
+ }
982
+
983
+ pub fn canvasWidgetBooleanSelected(widget: canvas.Widget) bool {
984
+ return widget.state.selected or widget.value >= 0.5;
985
+ }
986
+
987
+ pub fn canvasWidgetSwitchControlKind(kind: canvas.WidgetKind) bool {
988
+ return kind == .switch_control;
989
+ }
990
+
991
+ pub fn canvasWidgetSelectableSelected(widget: canvas.Widget) bool {
992
+ return widget.state.selected or widget.value >= 0.5;
993
+ }
994
+
995
+ pub fn canvasWidgetSelectionClearsSiblings(kind: canvas.WidgetKind) bool {
996
+ return switch (kind) {
997
+ .list_item, .menu_item, .data_cell, .segmented_control, .radio => true,
998
+ else => false,
999
+ };
1000
+ }
1001
+
1002
+ pub fn canvasWidgetKineticScrollFrameMs(frame_interval_ns: u64) f32 {
1003
+ const normalized = if (frame_interval_ns > 0) frame_interval_ns else platform.default_gpu_frame_interval_ns;
1004
+ return @as(f32, @floatFromInt(normalized)) / 1_000_000.0;
1005
+ }
1006
+
1007
+ pub const CanvasWidgetScrollKeyboardTarget = enum {
1008
+ start,
1009
+ end,
1010
+ };
1011
+
1012
+ pub const CanvasWidgetStepDirection = enum {
1013
+ increment,
1014
+ decrement,
1015
+ };
1016
+
1017
+ pub const CanvasWidgetGroupFocusEdge = enum {
1018
+ first,
1019
+ last,
1020
+ };
1021
+
1022
+ pub fn canvasWidgetGroupFocusEdgeFromInput(input_event: GpuSurfaceInputEvent) ?CanvasWidgetGroupFocusEdge {
1023
+ if (input_event.kind != .key_down) return null;
1024
+ if (input_event.modifiers.control or input_event.modifiers.option or input_event.modifiers.command or input_event.modifiers.primary or input_event.modifiers.shift) return null;
1025
+ if (std.ascii.eqlIgnoreCase(input_event.key, "home")) return .first;
1026
+ if (std.ascii.eqlIgnoreCase(input_event.key, "end")) return .last;
1027
+ return null;
1028
+ }
1029
+
1030
+ pub fn canvasWidgetSpatialFocusDirection(input_event: GpuSurfaceInputEvent) ?canvas.WidgetFocusDirection {
1031
+ if (input_event.kind != .key_down) return null;
1032
+ if (input_event.modifiers.control or input_event.modifiers.option or input_event.modifiers.command or input_event.modifiers.primary) return null;
1033
+ if (std.ascii.eqlIgnoreCase(input_event.key, "arrowleft")) return .left;
1034
+ if (std.ascii.eqlIgnoreCase(input_event.key, "arrowright")) return .right;
1035
+ if (std.ascii.eqlIgnoreCase(input_event.key, "arrowup")) return .up;
1036
+ if (std.ascii.eqlIgnoreCase(input_event.key, "arrowdown")) return .down;
1037
+ return null;
1038
+ }
1039
+
1040
+ pub fn canvasWidgetSpatialFocusAllowed(layout: canvas.WidgetLayoutTree, focused: canvas.WidgetFocusTarget, target: canvas.WidgetFocusTarget, direction: canvas.WidgetFocusDirection) bool {
1041
+ if (focused.kind != target.kind) return false;
1042
+ const same_parent = canvasWidgetFocusTargetsShareParent(layout, focused, target);
1043
+ return switch (focused.kind) {
1044
+ .data_cell => true,
1045
+ .list_item, .menu_item => same_parent and (direction == .up or direction == .down),
1046
+ .segmented_control => same_parent and (direction == .left or direction == .right),
1047
+ .radio => same_parent,
1048
+ .button, .icon_button => same_parent and canvasWidgetParentAllowsHorizontalButtonFocus(canvasWidgetFocusParentKind(layout, focused)) and (direction == .left or direction == .right),
1049
+ .toggle_button => same_parent and canvasWidgetParentAllowsHorizontalToggleFocus(canvasWidgetFocusParentKind(layout, focused)) and (direction == .left or direction == .right),
1050
+ else => false,
1051
+ };
1052
+ }
1053
+
1054
+ pub fn canvasWidgetFocusTargetsShareParent(layout: canvas.WidgetLayoutTree, a: canvas.WidgetFocusTarget, b: canvas.WidgetFocusTarget) bool {
1055
+ if (a.index >= layout.nodes.len or b.index >= layout.nodes.len) return false;
1056
+ return layout.nodes[a.index].parent_index == layout.nodes[b.index].parent_index;
1057
+ }
1058
+
1059
+ pub fn canvasWidgetFocusParentKind(layout: canvas.WidgetLayoutTree, target: canvas.WidgetFocusTarget) ?canvas.WidgetKind {
1060
+ if (target.index >= layout.nodes.len) return null;
1061
+ const parent_index = layout.nodes[target.index].parent_index orelse return null;
1062
+ if (parent_index >= layout.nodes.len) return null;
1063
+ return layout.nodes[parent_index].widget.kind;
1064
+ }
1065
+
1066
+ pub fn canvasWidgetParentAllowsHorizontalButtonFocus(kind: ?canvas.WidgetKind) bool {
1067
+ return switch (kind orelse return false) {
1068
+ .button_group, .pagination, .breadcrumb => true,
1069
+ else => false,
1070
+ };
1071
+ }
1072
+
1073
+ pub fn canvasWidgetParentAllowsHorizontalToggleFocus(kind: ?canvas.WidgetKind) bool {
1074
+ return switch (kind orelse return false) {
1075
+ .button_group, .toggle_group => true,
1076
+ else => false,
1077
+ };
1078
+ }
1079
+
1080
+ pub fn canvasWidgetGroupHomeEndFocusKind(layout: canvas.WidgetLayoutTree, target: canvas.WidgetFocusTarget) bool {
1081
+ const kind = target.kind;
1082
+ return switch (kind) {
1083
+ .list_item, .menu_item, .data_cell, .segmented_control, .radio => true,
1084
+ .button, .icon_button => canvasWidgetParentAllowsHorizontalButtonFocus(canvasWidgetFocusParentKind(layout, target)),
1085
+ .toggle_button => canvasWidgetParentAllowsHorizontalToggleFocus(canvasWidgetFocusParentKind(layout, target)),
1086
+ else => false,
1087
+ };
1088
+ }
1089
+
1090
+ pub const CanvasWidgetGroupDirection = enum {
1091
+ previous,
1092
+ next,
1093
+ };
1094
+
1095
+ pub fn canvasWidgetGroupDirectionalFocusTarget(layout: canvas.WidgetLayoutTree, focused: canvas.WidgetFocusTarget, direction: canvas.WidgetFocusDirection) ?canvas.WidgetFocusTarget {
1096
+ if (focused.index >= layout.nodes.len) return null;
1097
+ const parent_index = layout.nodes[focused.index].parent_index orelse return null;
1098
+ if (parent_index >= layout.nodes.len) return null;
1099
+ const parent_kind = layout.nodes[parent_index].widget.kind;
1100
+ const group_direction = canvasWidgetGroupDirectionForFocus(parent_kind, focused.kind, direction) orelse return null;
1101
+ return canvasWidgetAdjacentGroupFocusTarget(layout, parent_index, focused, group_direction) orelse focused;
1102
+ }
1103
+
1104
+ pub fn canvasWidgetGroupDirectionForFocus(parent_kind: canvas.WidgetKind, child_kind: canvas.WidgetKind, direction: canvas.WidgetFocusDirection) ?CanvasWidgetGroupDirection {
1105
+ return switch (parent_kind) {
1106
+ .button_group, .pagination, .breadcrumb => if (child_kind == .button or child_kind == .icon_button)
1107
+ canvasWidgetHorizontalGroupDirection(direction)
1108
+ else
1109
+ null,
1110
+ .toggle_group => if (child_kind == .toggle_button)
1111
+ canvasWidgetHorizontalGroupDirection(direction)
1112
+ else
1113
+ null,
1114
+ .tabs => if (child_kind == .segmented_control)
1115
+ canvasWidgetHorizontalGroupDirection(direction)
1116
+ else
1117
+ null,
1118
+ .radio_group => if (child_kind == .radio)
1119
+ canvasWidgetAnyAxisGroupDirection(direction)
1120
+ else
1121
+ null,
1122
+ .list => if (child_kind == .list_item)
1123
+ canvasWidgetVerticalGroupDirection(direction)
1124
+ else
1125
+ null,
1126
+ .menu_surface, .dropdown_menu => if (child_kind == .menu_item)
1127
+ canvasWidgetVerticalGroupDirection(direction)
1128
+ else
1129
+ null,
1130
+ else => null,
1131
+ };
1132
+ }
1133
+
1134
+ pub fn canvasWidgetHorizontalGroupDirection(direction: canvas.WidgetFocusDirection) ?CanvasWidgetGroupDirection {
1135
+ return switch (direction) {
1136
+ .left => .previous,
1137
+ .right => .next,
1138
+ .up, .down, .forward, .backward => null,
1139
+ };
1140
+ }
1141
+
1142
+ pub fn canvasWidgetVerticalGroupDirection(direction: canvas.WidgetFocusDirection) ?CanvasWidgetGroupDirection {
1143
+ return switch (direction) {
1144
+ .up => .previous,
1145
+ .down => .next,
1146
+ .left, .right, .forward, .backward => null,
1147
+ };
1148
+ }
1149
+
1150
+ pub fn canvasWidgetAnyAxisGroupDirection(direction: canvas.WidgetFocusDirection) ?CanvasWidgetGroupDirection {
1151
+ return switch (direction) {
1152
+ .left, .up => .previous,
1153
+ .right, .down => .next,
1154
+ .forward, .backward => null,
1155
+ };
1156
+ }
1157
+
1158
+ pub fn canvasWidgetAdjacentGroupFocusTarget(
1159
+ layout: canvas.WidgetLayoutTree,
1160
+ parent_index: usize,
1161
+ focused: canvas.WidgetFocusTarget,
1162
+ direction: CanvasWidgetGroupDirection,
1163
+ ) ?canvas.WidgetFocusTarget {
1164
+ var previous: ?canvas.WidgetFocusTarget = null;
1165
+ var saw_focused = false;
1166
+ for (layout.nodes) |node| {
1167
+ if (node.parent_index != parent_index or node.widget.kind != focused.kind) continue;
1168
+ const target = layout.focusTargetById(node.widget.id) orelse continue;
1169
+ if (saw_focused) return target;
1170
+ if (target.id == focused.id) {
1171
+ if (direction == .previous) return previous;
1172
+ saw_focused = true;
1173
+ } else {
1174
+ previous = target;
1175
+ }
1176
+ }
1177
+ return null;
1178
+ }
1179
+
1180
+ // ---------------------------------------------------------- tree focus
1181
+ //
1182
+ // The ARIA tree's roving focus: rows are widgets carrying
1183
+ // `role = .treeitem` at ANY depth under the nearest `.tree` ancestor,
1184
+ // walked in node (DFS) order. Collapsed subtrees are model-owned — the
1185
+ // view does not render them — so "visible rows" is simply every row in
1186
+ // the layout that can take focus.
1187
+
1188
+ /// Index of the nearest `.tree` ancestor of `node_index`, or null when
1189
+ /// the node sits outside any tree.
1190
+ pub fn canvasWidgetTreeScopeIndex(layout: canvas.WidgetLayoutTree, node_index: usize) ?usize {
1191
+ if (node_index >= layout.nodes.len) return null;
1192
+ var current = layout.nodes[node_index].parent_index;
1193
+ while (current) |index| {
1194
+ if (index >= layout.nodes.len) return null;
1195
+ // A `.tree` container, or any container DECLARING the tree role
1196
+ // (a windowed virtual list whose rows are treeitems declares
1197
+ // `role = .tree` on the scroll container — there is no room for
1198
+ // a nested flow container between the virtualized scroll region
1199
+ // and its absolutely-placed rows).
1200
+ if (layout.nodes[index].widget.kind == .tree or layout.nodes[index].widget.semantics.role == .tree) return index;
1201
+ current = layout.nodes[index].parent_index;
1202
+ }
1203
+ return null;
1204
+ }
1205
+
1206
+ fn canvasWidgetTreeRowFocusTarget(layout: canvas.WidgetLayoutTree, node_index: usize) ?canvas.WidgetFocusTarget {
1207
+ if (layout.nodes[node_index].widget.semantics.role != .treeitem) return null;
1208
+ return layout.focusTargetById(layout.nodes[node_index].widget.id);
1209
+ }
1210
+
1211
+ /// The tree keymap's focus moves. Up/Down walk the scope's rows in node
1212
+ /// order; Home/End jump to its edges; Left moves to the PARENT row when
1213
+ /// the focused row is a leaf or collapsed (an expanded row collapses
1214
+ /// instead — no focus move, the routed toggle intent handles it); Right
1215
+ /// moves to the FIRST CHILD row when the focused row is expanded (a
1216
+ /// collapsed row expands instead). Null = no tree move (the caller
1217
+ /// falls through to group/spatial focus).
1218
+ pub fn canvasWidgetTreeDirectionalFocusTarget(
1219
+ layout: canvas.WidgetLayoutTree,
1220
+ focused: canvas.WidgetFocusTarget,
1221
+ direction: canvas.WidgetFocusDirection,
1222
+ ) ?canvas.WidgetFocusTarget {
1223
+ if (focused.index >= layout.nodes.len) return null;
1224
+ if (layout.nodes[focused.index].widget.semantics.role != .treeitem) return null;
1225
+ const tree_index = canvasWidgetTreeScopeIndex(layout, focused.index) orelse return null;
1226
+ return switch (direction) {
1227
+ .up => canvasWidgetTreeAdjacentRow(layout, tree_index, focused.index, .previous) orelse focused,
1228
+ .down => canvasWidgetTreeAdjacentRow(layout, tree_index, focused.index, .next) orelse focused,
1229
+ .left => blk: {
1230
+ const expanded = layout.nodes[focused.index].widget.state.expanded orelse false;
1231
+ if (expanded) break :blk null; // collapse intent, not a move
1232
+ break :blk canvasWidgetTreeParentRow(layout, tree_index, focused.index) orelse focused;
1233
+ },
1234
+ .right => blk: {
1235
+ const expanded = layout.nodes[focused.index].widget.state.expanded orelse false;
1236
+ if (!expanded) break :blk null; // expand intent (or leaf no-op)
1237
+ break :blk canvasWidgetTreeFirstChildRow(layout, focused.index) orelse focused;
1238
+ },
1239
+ .forward, .backward => null,
1240
+ };
1241
+ }
1242
+
1243
+ /// Home/End inside a tree: the scope's first/last focusable row.
1244
+ pub fn canvasWidgetTreeFocusEdgeTarget(
1245
+ layout: canvas.WidgetLayoutTree,
1246
+ focused: canvas.WidgetFocusTarget,
1247
+ edge: CanvasWidgetGroupFocusEdge,
1248
+ ) ?canvas.WidgetFocusTarget {
1249
+ if (focused.index >= layout.nodes.len) return null;
1250
+ if (layout.nodes[focused.index].widget.semantics.role != .treeitem) return null;
1251
+ const tree_index = canvasWidgetTreeScopeIndex(layout, focused.index) orelse return null;
1252
+ switch (edge) {
1253
+ .first => {
1254
+ var index = tree_index + 1;
1255
+ while (index < layout.nodes.len and layout.nodes[index].depth > layout.nodes[tree_index].depth) : (index += 1) {
1256
+ if (canvasWidgetTreeRowFocusTarget(layout, index)) |target| return target;
1257
+ }
1258
+ },
1259
+ .last => {
1260
+ var last: ?canvas.WidgetFocusTarget = null;
1261
+ var index = tree_index + 1;
1262
+ while (index < layout.nodes.len and layout.nodes[index].depth > layout.nodes[tree_index].depth) : (index += 1) {
1263
+ if (canvasWidgetTreeRowFocusTarget(layout, index)) |target| last = target;
1264
+ }
1265
+ return last;
1266
+ },
1267
+ }
1268
+ return null;
1269
+ }
1270
+
1271
+ fn canvasWidgetTreeAdjacentRow(
1272
+ layout: canvas.WidgetLayoutTree,
1273
+ tree_index: usize,
1274
+ focused_index: usize,
1275
+ direction: CanvasWidgetGroupDirection,
1276
+ ) ?canvas.WidgetFocusTarget {
1277
+ const tree_depth = layout.nodes[tree_index].depth;
1278
+ var previous: ?canvas.WidgetFocusTarget = null;
1279
+ var saw_focused = false;
1280
+ var index = tree_index + 1;
1281
+ while (index < layout.nodes.len and layout.nodes[index].depth > tree_depth) : (index += 1) {
1282
+ if (index == focused_index) {
1283
+ if (direction == .previous) return previous;
1284
+ saw_focused = true;
1285
+ continue;
1286
+ }
1287
+ const target = canvasWidgetTreeRowFocusTarget(layout, index) orelse continue;
1288
+ if (saw_focused) return target;
1289
+ previous = target;
1290
+ }
1291
+ return null;
1292
+ }
1293
+
1294
+ fn canvasWidgetTreeParentRow(layout: canvas.WidgetLayoutTree, tree_index: usize, focused_index: usize) ?canvas.WidgetFocusTarget {
1295
+ var current = layout.nodes[focused_index].parent_index;
1296
+ while (current) |index| {
1297
+ if (index >= layout.nodes.len or index == tree_index) return null;
1298
+ if (canvasWidgetTreeRowFocusTarget(layout, index)) |target| return target;
1299
+ current = layout.nodes[index].parent_index;
1300
+ }
1301
+ return null;
1302
+ }
1303
+
1304
+ fn canvasWidgetTreeFirstChildRow(layout: canvas.WidgetLayoutTree, focused_index: usize) ?canvas.WidgetFocusTarget {
1305
+ const row_depth = layout.nodes[focused_index].depth;
1306
+ var index = focused_index + 1;
1307
+ while (index < layout.nodes.len and layout.nodes[index].depth > row_depth) : (index += 1) {
1308
+ if (canvasWidgetTreeRowFocusTarget(layout, index)) |target| return target;
1309
+ }
1310
+ return null;
1311
+ }
1312
+
1313
+ pub fn canvasWidgetGroupFocusEdgeTarget(layout: canvas.WidgetLayoutTree, focused: canvas.WidgetFocusTarget, edge: CanvasWidgetGroupFocusEdge) ?canvas.WidgetFocusTarget {
1314
+ if (!canvasWidgetGroupHomeEndFocusKind(layout, focused)) return null;
1315
+ if (focused.index >= layout.nodes.len) return null;
1316
+ const parent_index = layout.nodes[focused.index].parent_index;
1317
+ switch (edge) {
1318
+ .first => {
1319
+ for (layout.nodes) |node| {
1320
+ if (node.parent_index != parent_index or node.widget.kind != focused.kind) continue;
1321
+ if (layout.focusTargetById(node.widget.id)) |target| return target;
1322
+ }
1323
+ },
1324
+ .last => {
1325
+ var index = layout.nodes.len;
1326
+ while (index > 0) {
1327
+ index -= 1;
1328
+ const node = layout.nodes[index];
1329
+ if (node.parent_index != parent_index or node.widget.kind != focused.kind) continue;
1330
+ if (layout.focusTargetById(node.widget.id)) |target| return target;
1331
+ }
1332
+ },
1333
+ }
1334
+ return null;
1335
+ }