@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,2055 @@
1
+ const std = @import("std");
2
+ const builtin = @import("builtin");
3
+ const geometry = @import("geometry");
4
+ const canvas = @import("root.zig");
5
+ const text_model = @import("text.zig");
6
+ const text_spans_model = @import("text_spans.zig");
7
+ const token_model = @import("tokens.zig");
8
+ const widget_model = @import("widgets.zig");
9
+ const event_model = @import("events.zig");
10
+ const widget_tree = @import("widget_tree.zig");
11
+ const widget_access = @import("widget_access.zig");
12
+ const widget_metrics = @import("widget_metrics.zig");
13
+ const widget_render = @import("widget_render.zig");
14
+
15
+ const Error = canvas.Error;
16
+ const Widget = widget_model.Widget;
17
+ const WidgetMainAlignment = widget_model.WidgetMainAlignment;
18
+ const WidgetCrossAlignment = widget_model.WidgetCrossAlignment;
19
+ const WidgetLayoutStyle = widget_model.WidgetLayoutStyle;
20
+ const WidgetLayoutNode = event_model.WidgetLayoutNode;
21
+ const DesignTokens = token_model.DesignTokens;
22
+ const virtualListRange = token_model.virtualListRange;
23
+ const measureTextWidthForFont = text_model.measureTextWidthForFont;
24
+
25
+ /// Text width for intrinsic sizing: the injected provider on `tokens` when
26
+ /// present, the deterministic estimator otherwise.
27
+ fn measuredTextWidth(tokens: DesignTokens, text: []const u8, size: f32) f32 {
28
+ return measureTextWidthForFont(tokens.text_measure, tokens.typography.font_id, text, size);
29
+ }
30
+ const gridColumnCount = widget_tree.gridColumnCount;
31
+ const gridRowCount = widget_tree.gridRowCount;
32
+ const saturatingU32 = widget_tree.saturatingU32;
33
+ const booleanControlSelected = widget_access.booleanControlSelected;
34
+ const widgetButtonTextSize = widget_metrics.widgetButtonTextSize;
35
+ const widgetBodyTextSize = widget_metrics.widgetBodyTextSize;
36
+ const widgetLabelTextSize = widget_metrics.widgetLabelTextSize;
37
+ const widgetTypographySize = widget_metrics.widgetTypographySize;
38
+ const widgetLineHeight = widget_metrics.widgetLineHeight;
39
+ const widgetDefaultRowHeight = widget_metrics.widgetDefaultRowHeight;
40
+ const widgetButtonInset = widget_metrics.widgetButtonInset;
41
+ const widgetControlInset = widget_metrics.widgetControlInset;
42
+ const widgetSizedDensityValue = widget_metrics.widgetSizedDensityValue;
43
+ const densityValue = widget_metrics.densityValue;
44
+ const widgetControlHeight = widget_metrics.widgetControlHeight;
45
+ const widgetStatusBarPadding = widget_render.widgetStatusBarPadding;
46
+ const controlStrokeWidth = widget_render.controlStrokeWidth;
47
+ const componentControlVisualTokens = widget_render.componentControlVisualTokens;
48
+ const widgetTextSpanLayoutOptions = widget_metrics.widgetTextSpanLayoutOptions;
49
+
50
+ pub const max_widget_depth: usize = 32;
51
+
52
+ pub fn layoutWidgetDepth(
53
+ widget: Widget,
54
+ frame: geometry.RectF,
55
+ parent_index: ?usize,
56
+ depth: usize,
57
+ output: []WidgetLayoutNode,
58
+ len: *usize,
59
+ tokens: DesignTokens,
60
+ ) Error!usize {
61
+ if (depth >= max_widget_depth) return error.WidgetDepthExceeded;
62
+ if (len.* >= output.len) return error.WidgetLayoutListFull;
63
+
64
+ const index = len.*;
65
+ output[index] = .{
66
+ .widget = widgetWithFrame(widget, frame),
67
+ .frame = frame,
68
+ .depth = depth,
69
+ .parent_index = parent_index,
70
+ };
71
+ len.* += 1;
72
+
73
+ const content = frame.inset(widget.layout.padding);
74
+ switch (widget.kind) {
75
+ .row, .breadcrumb, .pagination, .radio_group, .toggle_group => try layoutAxisChildren(widget.children, content, .horizontal, index, depth, output, len, widget.layout, tokens),
76
+ // Button groups flow like rows but at their register's effective
77
+ // gap: the detached register supplies its own inter-chip gap when
78
+ // the author left the group's gap at 0.
79
+ .button_group => try layoutAxisChildren(widget.children, content, .horizontal, index, depth, output, len, buttonGroupLayoutStyle(widget, tokens), tokens),
80
+ // Tab strips flow the same way: the underline register supplies
81
+ // its own inter-trigger gap when the author left the strip's gap
82
+ // at 0 (the house pill container keeps its flush triggers).
83
+ .tabs => try layoutAxisChildren(widget.children, content, .horizontal, index, depth, output, len, tabsLayoutStyle(widget, tokens), tokens),
84
+ .column => try layoutAxisChildren(widget.children, content, .vertical, index, depth, output, len, widget.layout, tokens),
85
+ // The grouped input flows vertically inside its own field chrome:
86
+ // the text entry (grow-stretched by `Ui.inputGroup`) above the
87
+ // accessory actions row.
88
+ .input_group => try layoutAxisChildren(widget.children, content, .vertical, index, depth, output, len, widget.layout, tokens),
89
+ .grid => if (widget.layout.virtualized)
90
+ try layoutVirtualGridChildren(widget.children, content, index, depth, output, len, widget.value, widget.layout, tokens)
91
+ else
92
+ try layoutGridChildren(widget.children, content, index, depth, output, len, widget.layout.gap, widget.layout.columns, tokens),
93
+ .data_grid, .table => if (widget.layout.virtualized)
94
+ try layoutVirtualVerticalChildren(widget.children, content, index, depth, output, len, widget.value, widget.layout, tokens)
95
+ else
96
+ try layoutAxisChildren(widget.children, content, .vertical, index, depth, output, len, widget.layout, tokens),
97
+ .data_row => try layoutAxisChildren(widget.children, content, .horizontal, index, depth, output, len, widget.layout, tokens),
98
+ // A list row with custom content: children flow horizontally
99
+ // (gap-consuming, like a row) inside the flat wash chrome, so
100
+ // apps compose indicator/title/meta rows without reaching for
101
+ // bordered card surfaces. Text/icon-only items stay leaves.
102
+ .list_item => try layoutAxisChildren(widget.children, content, .horizontal, index, depth, output, len, widget.layout, tokens),
103
+ .scroll_view => if (widget.layout.virtualized)
104
+ try layoutVirtualVerticalChildren(widget.children, content, index, depth, output, len, widget.value, widget.layout, tokens)
105
+ else
106
+ try layoutScrollChildren(widget.children, content, index, depth, output, len, widget.value, tokens),
107
+ .list => if (widget.layout.virtualized)
108
+ try layoutVirtualVerticalChildren(widget.children, content, index, depth, output, len, widget.value, widget.layout, tokens)
109
+ else
110
+ try layoutAxisChildren(widget.children, content, .vertical, index, depth, output, len, widget.layout, tokens),
111
+ .tree => try layoutAxisChildren(widget.children, content, .vertical, index, depth, output, len, widget.layout, tokens),
112
+ .split => try layoutSplitChildren(widget, content, index, depth, output, len, tokens),
113
+ .menu_surface, .dropdown_menu => try layoutAxisChildren(widget.children, content, .vertical, index, depth, output, len, widget.layout, tokens),
114
+ .accordion => {
115
+ // Disclosure contract: children lay out at FULL size whether
116
+ // or not the item is open. A closed (or still-revealing)
117
+ // item keeps its header-only extent — the content overflows
118
+ // the frame, unpainted and inert until revealed — so a
119
+ // reveal never re-wraps text mid-flight: child geometry is
120
+ // identical in both poses and only the item's own extent
121
+ // (plus whatever stacks below it) moves.
122
+ const child_content = accordionContentFrame(widget, content, tokens, depth);
123
+ for (widget.children) |child| {
124
+ if (child.layout.anchor != null) continue;
125
+ _ = try layoutWidgetDepth(child, stackChildFrame(child_content, child), index, depth + 1, output, len, tokens);
126
+ }
127
+ },
128
+ .alert => {
129
+ // Alert children (the description under a chrome-drawn
130
+ // title) hang past the icon column and start under the
131
+ // title line — the standard callout grid.
132
+ const child_content = alertContentFrame(widget, content, tokens);
133
+ for (widget.children) |child| {
134
+ if (child.layout.anchor != null) continue;
135
+ _ = try layoutWidgetDepth(child, stackChildFrame(child_content, child), index, depth + 1, output, len, tokens);
136
+ }
137
+ },
138
+ .stack, .bubble, .card, .dialog, .drawer, .sheet, .resizable, .panel, .popover => {
139
+ for (widget.children) |child| {
140
+ if (child.layout.anchor != null) continue;
141
+ _ = try layoutWidgetDepth(child, stackChildFrame(content, child), index, depth + 1, output, len, tokens);
142
+ }
143
+ },
144
+ // Span paragraphs and span-carrying table cells share the link
145
+ // hotspot child convention (no spans or no children is a no-op).
146
+ .text, .data_cell => try layoutTextSpanLinkChildren(widget, content, index, depth, output, len, tokens),
147
+ .icon, .image, .avatar, .badge, .button, .toggle_button, .icon_button, .select, .input, .text_field, .search_field, .combobox, .textarea, .tooltip, .menu_item, .status_bar, .segmented_control, .checkbox, .radio, .switch_control, .toggle, .slider, .progress, .separator, .skeleton, .spinner, .chart, .split_divider => {},
148
+ }
149
+
150
+ // Anchored floating children are excluded from every flow above (they
151
+ // consume no parent space) and positioned here instead, against this
152
+ // widget's resolved frame and the window (the layout root's frame).
153
+ // Leaf trigger kinds (select, button, ...) never lay out flow
154
+ // children, but their anchored children float all the same.
155
+ try layoutAnchoredChildren(widget.children, frame, index, depth, output, len, tokens);
156
+
157
+ return index;
158
+ }
159
+
160
+ fn layoutAnchoredChildren(
161
+ children: []const Widget,
162
+ anchor_rect: geometry.RectF,
163
+ parent_index: usize,
164
+ depth: usize,
165
+ output: []WidgetLayoutNode,
166
+ len: *usize,
167
+ tokens: DesignTokens,
168
+ ) Error!void {
169
+ for (children) |child| {
170
+ const anchor = child.layout.anchor orelse continue;
171
+ const child_frame = anchoredWidgetFrame(child, anchor, anchor_rect, output[0].frame, tokens);
172
+ _ = try layoutWidgetDepth(child, child_frame, parent_index, depth + 1, output, len, tokens);
173
+ }
174
+ }
175
+
176
+ /// Resolved frame of an anchored floating widget: sized from its explicit
177
+ /// frame or intrinsic content (`stretch` widens to at least the anchor's
178
+ /// width), placed on the preferred side of the anchor rect — flipping to
179
+ /// the other side when it does not fit and the other side has more room —
180
+ /// with height clamped to the chosen side's space and both axes clamped
181
+ /// into the window. Pure geometry, unit-testable on its own.
182
+ pub fn anchoredWidgetFrame(
183
+ child: Widget,
184
+ anchor: widget_model.WidgetAnchor,
185
+ anchor_rect: geometry.RectF,
186
+ window_rect: geometry.RectF,
187
+ tokens: DesignTokens,
188
+ ) geometry.RectF {
189
+ const window = window_rect.normalized();
190
+ const anchor_frame = anchor_rect.normalized();
191
+ const intrinsic = intrinsicWidgetSize(child, tokens);
192
+
193
+ var width = if (child.frame.width > 0) child.frame.width else intrinsic.width;
194
+ if (anchor.alignment == .stretch) width = @max(width, anchor_frame.width);
195
+ width = clampIntrinsicAxis(width, child.layout.min_size.width, child.layout.max_size.width);
196
+ width = @min(width, window.width);
197
+
198
+ var height = if (child.frame.height > 0) child.frame.height else intrinsic.height;
199
+ height = clampIntrinsicAxis(height, child.layout.min_size.height, child.layout.max_size.height);
200
+
201
+ const offset = nonNegative(anchor.offset);
202
+ const space_below = window.maxY() - anchor_frame.maxY() - offset;
203
+ const space_above = anchor_frame.y - window.y - offset;
204
+ const preferred_space = switch (anchor.placement) {
205
+ .below => space_below,
206
+ .above => space_above,
207
+ };
208
+ const other_space = switch (anchor.placement) {
209
+ .below => space_above,
210
+ .above => space_below,
211
+ };
212
+ const flipped = height > preferred_space and other_space > preferred_space;
213
+ const below = (anchor.placement == .below) != flipped;
214
+ const side_space = @max(0, if (below) space_below else space_above);
215
+ height = @min(height, side_space);
216
+
217
+ const y = if (below) anchor_frame.maxY() + offset else anchor_frame.y - offset - height;
218
+ var x = switch (anchor.alignment) {
219
+ .start, .stretch => anchor_frame.x,
220
+ .end => anchor_frame.maxX() - width,
221
+ };
222
+ x = std.math.clamp(x, window.x, @max(window.x, window.maxX() - width));
223
+ const clamped_y = std.math.clamp(y, window.y, @max(window.y, window.maxY() - height));
224
+ return geometry.RectF.init(x, clamped_y, width, height);
225
+ }
226
+
227
+ const LayoutAxis = enum {
228
+ horizontal,
229
+ vertical,
230
+ };
231
+
232
+ /// A button group's effective inter-member gap: the author's stated gap
233
+ /// always wins; a gap left at 0 means "the register decides" — attached
234
+ /// segments (0) under the house `.segmented` register, the pack's
235
+ /// `button_group_gap` metric under `.detached`. The default metric is 0,
236
+ /// so no-pack layout is unchanged by construction.
237
+ pub fn buttonGroupGap(widget: Widget, tokens: DesignTokens) f32 {
238
+ const gap = nonNegative(widget.layout.gap);
239
+ if (gap > 0 or tokens.controls.button_group_style != .detached) return gap;
240
+ return nonNegative(tokens.metrics.button_group_gap);
241
+ }
242
+
243
+ /// The group's layout style with the register's effective gap applied —
244
+ /// what `layoutAxisChildren` receives for `.button_group` parents.
245
+ fn buttonGroupLayoutStyle(widget: Widget, tokens: DesignTokens) WidgetLayoutStyle {
246
+ var style = widget.layout;
247
+ style.gap = buttonGroupGap(widget, tokens);
248
+ return style;
249
+ }
250
+
251
+ /// A tab strip's effective inter-trigger gap: the author's stated gap
252
+ /// always wins; a gap left at 0 means "the register decides" — flush
253
+ /// triggers (0) inside the house `.pill` container, the pack's
254
+ /// `tabs_gap` metric under `.underline`. The default metric is 0, so
255
+ /// no-pack layout is unchanged by construction.
256
+ pub fn tabsGap(widget: Widget, tokens: DesignTokens) f32 {
257
+ const gap = nonNegative(widget.layout.gap);
258
+ if (gap > 0 or tokens.controls.tabs_indicator != .underline) return gap;
259
+ return nonNegative(tokens.metrics.tabs_gap);
260
+ }
261
+
262
+ /// The strip's layout style with the register's effective gap applied —
263
+ /// what `layoutAxisChildren` receives for `.tabs` parents.
264
+ fn tabsLayoutStyle(widget: Widget, tokens: DesignTokens) WidgetLayoutStyle {
265
+ var style = widget.layout;
266
+ style.gap = tabsGap(widget, tokens);
267
+ return style;
268
+ }
269
+
270
+ fn layoutAxisChildren(
271
+ children: []const Widget,
272
+ content: geometry.RectF,
273
+ axis: LayoutAxis,
274
+ parent_index: usize,
275
+ depth: usize,
276
+ output: []WidgetLayoutNode,
277
+ len: *usize,
278
+ style: WidgetLayoutStyle,
279
+ tokens: DesignTokens,
280
+ ) Error!void {
281
+ // Anchored floating children take no flow slot: they are skipped in
282
+ // every pass here (measurement, gap counting, placement) and laid out
283
+ // by `layoutAnchoredChildren` against the parent's frame instead.
284
+ var flow_count: usize = 0;
285
+ for (children) |child| {
286
+ if (child.layout.anchor == null) flow_count += 1;
287
+ }
288
+ if (flow_count == 0) return;
289
+
290
+ const available_extent = switch (axis) {
291
+ .horizontal => content.width,
292
+ .vertical => content.height,
293
+ };
294
+ const cross_extent = switch (axis) {
295
+ .horizontal => content.height,
296
+ .vertical => content.width,
297
+ };
298
+ const clamped_gap = nonNegative(style.gap);
299
+ const total_gap = clamped_gap * @as(f32, @floatFromInt(flow_count - 1));
300
+ var fixed_extent: f32 = 0;
301
+ var grow_total: f32 = 0;
302
+ for (children) |child| {
303
+ if (child.layout.anchor != null) continue;
304
+ const grow = nonNegative(child.layout.grow);
305
+ if (grow > 0) {
306
+ grow_total += grow;
307
+ } else {
308
+ // The bubble thread fraction binds on a row's main axis
309
+ // (the child's width): a hug-sized bubble measures at most
310
+ // 80% of the row, so a spacer beside it keeps real share
311
+ // and a long message wraps instead of overflowing.
312
+ fixed_extent += mainExtentWithBubbleCap(child, axis, available_extent, preferredMainExtentInCross(child, axis, cross_extent, style.cross_alignment, tokens));
313
+ }
314
+ }
315
+
316
+ const remaining = @max(0, available_extent - fixed_extent - total_gap);
317
+ const assigned_extent = assignedAxisChildrenExtent(children, axis, fixed_extent, grow_total, remaining);
318
+ const used_extent = assigned_extent + total_gap;
319
+ if (axisLayoutOverflow(available_extent, used_extent)) |overflow| {
320
+ logAxisChildrenOverflow(output, parent_index, axis, available_extent, used_extent, overflow);
321
+ }
322
+ const free_extent = @max(0, available_extent - used_extent);
323
+ var child_gap = clamped_gap;
324
+ if (style.main_alignment == .space_between and flow_count > 1) {
325
+ child_gap += free_extent / @as(f32, @floatFromInt(flow_count - 1));
326
+ }
327
+ var cursor: f32 = switch (axis) {
328
+ .horizontal => content.x,
329
+ .vertical => content.y,
330
+ } + mainAxisAlignmentOffset(style.main_alignment, free_extent);
331
+
332
+ for (children) |child| {
333
+ if (child.layout.anchor != null) continue;
334
+ const grow = nonNegative(child.layout.grow);
335
+ const main_extent = if (grow > 0 and grow_total > 0)
336
+ clampMainExtent(child, axis, remaining * grow / grow_total)
337
+ else
338
+ mainExtentWithBubbleCap(child, axis, available_extent, preferredMainExtentInCross(child, axis, cross_extent, style.cross_alignment, tokens));
339
+ const cross = preferredCrossExtent(child, axis, cross_extent, style.cross_alignment, tokens);
340
+ const cross_origin = alignedCrossAxisOrigin(content, axis, cross_extent, cross, child, style.cross_alignment);
341
+ const child_frame = switch (axis) {
342
+ .horizontal => geometry.RectF.init(cursor, cross_origin, main_extent, cross),
343
+ .vertical => geometry.RectF.init(cross_origin, cursor, cross, main_extent),
344
+ };
345
+ _ = try layoutWidgetDepth(child, child_frame, parent_index, depth + 1, output, len, tokens);
346
+ cursor += main_extent + child_gap;
347
+ }
348
+ }
349
+
350
+ /// Tolerance separating real layout overflow from float noise.
351
+ pub const axis_layout_overflow_epsilon: f32 = 0.5;
352
+
353
+ /// Positive overflow of the children's assigned extent past the
354
+ /// container's content extent, or null when everything fits. Grow
355
+ /// children participate with their min-size floors, fixed children with
356
+ /// their preferred (intrinsic or explicit) extents — exactly the floors
357
+ /// `layoutAxisChildren` cannot shrink below.
358
+ pub fn axisLayoutOverflow(available_extent: f32, used_extent: f32) ?f32 {
359
+ const overflow = used_extent - available_extent;
360
+ if (overflow <= axis_layout_overflow_epsilon) return null;
361
+ return overflow;
362
+ }
363
+
364
+ const layout_log = std.log.scoped(.zero_canvas_layout);
365
+
366
+ /// Test seam for the overflow diagnostic below: incremented once per
367
+ /// emitted (post-suppression) diagnostic in test builds, so regression
368
+ /// tests can assert a shape stays quiet — or still warns — without
369
+ /// scraping log output. Release and debug app builds never touch it.
370
+ pub var test_axis_overflow_diagnostics: usize = 0;
371
+
372
+ /// Whether the container at `parent_index` sits inside a vertically
373
+ /// scrolling scope: a `.scroll_view` ancestor, or a virtualized
374
+ /// container ancestor (layout-culled model-driven lists/grids). Inside
375
+ /// such a scope, content taller than the viewport is the normal
376
+ /// operating mode — the scroll exists precisely to reveal it — so the
377
+ /// vertical overflow diagnostic must stay quiet there. The walk is the
378
+ /// same rule the layout audit applies (`scopeScrollsVertically`), which
379
+ /// is why the audit was already clean on these shapes. Anchored
380
+ /// floating subtrees hoist out of every ancestor scope (window-clipped,
381
+ /// not parent-clipped), so the walk stops at an anchor boundary just
382
+ /// like the audit's clip-scope walk.
383
+ fn widgetInsideVerticalScrollScope(output: []const WidgetLayoutNode, parent_index: usize) bool {
384
+ var current: ?usize = parent_index;
385
+ while (current) |index| {
386
+ const widget = output[index].widget;
387
+ if (widget.kind == .scroll_view or widget.layout.virtualized) return true;
388
+ if (widget.layout.anchor != null) return false;
389
+ current = output[index].parent_index;
390
+ }
391
+ return false;
392
+ }
393
+
394
+ /// Debug-build diagnostic for silent flex overflow: when the children's
395
+ /// minimum extents exceed the container, the extra pixels spill past the
396
+ /// content box with no visual cue at authoring time. Logged at .debug so
397
+ /// debug app runs surface it while release builds and test runs stay
398
+ /// quiet. Two carve-outs keep it honest:
399
+ ///
400
+ /// - Vertical overflow inside a vertically scrolling scope is expected,
401
+ /// never damage (see `widgetInsideVerticalScrollScope`): a tracked or
402
+ /// virtualized scroll's content wrapper is sized to the viewport and
403
+ /// its children legitimately extend past it on every rebuild, which
404
+ /// used to repeat this line hundreds of times for a perfectly correct
405
+ /// layout. Horizontal overflow still warns there — nothing scrolls
406
+ /// sideways to reveal it.
407
+ /// - The line names the concrete widget (root-first kind path, label
408
+ /// snippet, id — the same identity the layout audit prints), because
409
+ /// a bare kind like "column" is unactionable in any real tree.
410
+ fn logAxisChildrenOverflow(output: []const WidgetLayoutNode, parent_index: usize, axis: LayoutAxis, available_extent: f32, used_extent: f32, overflow: f32) void {
411
+ if (axis == .vertical and widgetInsideVerticalScrollScope(output, parent_index)) return;
412
+ if (builtin.is_test) test_axis_overflow_diagnostics += 1;
413
+ if (builtin.mode != .Debug) return;
414
+ var path_buffer: [256]u8 = undefined;
415
+ layout_log.debug(
416
+ "{s} children overflow the {s} axis by {d:.1}px (need {d:.1}px, have {d:.1}px): intrinsic/min sizes exceed the container - shrink the content, or give siblings grow factors or definite width/height that fit",
417
+ .{ axisOverflowWidgetPath(&path_buffer, output, parent_index), @tagName(axis), overflow, used_extent, available_extent },
418
+ );
419
+ }
420
+
421
+ /// The offending container's identity for the overflow diagnostic: the
422
+ /// root-first kind chain plus the container's label snippet and id
423
+ /// (`column > card > column "Inbox" (id 42)`), assembled into the
424
+ /// caller's buffer. Ancestors already have their layout nodes by the
425
+ /// time a container lays out its children, so the parent chain is
426
+ /// complete; a full audit-style path with sibling ordinals would need
427
+ /// the finished tree, which does not exist mid-layout. Truncation on a
428
+ /// deep tree keeps the prefix — the leaf id still lands via the audit.
429
+ fn axisOverflowWidgetPath(buffer: []u8, output: []const WidgetLayoutNode, parent_index: usize) []const u8 {
430
+ var chain: [max_widget_depth]usize = undefined;
431
+ var chain_len: usize = 0;
432
+ var current: ?usize = parent_index;
433
+ while (current) |index| {
434
+ if (chain_len >= chain.len) break;
435
+ chain[chain_len] = index;
436
+ chain_len += 1;
437
+ current = output[index].parent_index;
438
+ }
439
+
440
+ var writer = std.Io.Writer.fixed(buffer);
441
+ var position = chain_len;
442
+ while (position > 0) {
443
+ position -= 1;
444
+ if (position != chain_len - 1) writer.print(" > ", .{}) catch break;
445
+ writer.print("{s}", .{@tagName(output[chain[position]].widget.kind)}) catch break;
446
+ }
447
+ const widget = output[parent_index].widget;
448
+ const label = if (widget.text.len > 0) widget.text else widget.semantics.label;
449
+ if (label.len > 0) {
450
+ // Longest clean UTF-8 prefix of at most 24 bytes: never split a
451
+ // multi-byte sequence mid-codepoint in the log line.
452
+ var snippet_len: usize = @min(label.len, 24);
453
+ while (snippet_len > 0 and snippet_len < label.len and text_model.isUtf8ContinuationByte(label[snippet_len])) snippet_len -= 1;
454
+ writer.print(" \"{s}\"", .{label[0..snippet_len]}) catch {};
455
+ }
456
+ if (widget.id != 0) writer.print(" (id {d})", .{widget.id}) catch {};
457
+ return writer.buffered();
458
+ }
459
+
460
+ /// Floor `value` with the widget's `min_size` for the axis and cap it at
461
+ /// `max_size` when set (0 = unbounded). Explicit author sizes write both
462
+ /// bounds, making the extent definite.
463
+ fn clampMainExtent(widget: Widget, axis: LayoutAxis, value: f32) f32 {
464
+ return @max(minMainExtent(widget, axis), boundedByMax(value, maxMainExtent(widget, axis)));
465
+ }
466
+
467
+ fn maxMainExtent(widget: Widget, axis: LayoutAxis) f32 {
468
+ return switch (axis) {
469
+ .horizontal => widget.layout.max_size.width,
470
+ .vertical => widget.layout.max_size.height,
471
+ };
472
+ }
473
+
474
+ fn boundedByMax(value: f32, max: f32) f32 {
475
+ return if (max > 0) @min(value, max) else value;
476
+ }
477
+
478
+ /// Main extent of a non-growing flex child, given the cross-axis space it
479
+ /// will be offered. Identical to `preferredMainExtent` unless the child's
480
+ /// subtree contains span paragraphs and the axis is vertical: those
481
+ /// reserve their wrapped height at the width they will receive, so
482
+ /// stacked markdown blocks do not overlap. Trees without spans keep the
483
+ /// classic single-pass behavior byte-for-byte.
484
+ fn preferredMainExtentInCross(
485
+ child: Widget,
486
+ axis: LayoutAxis,
487
+ cross_extent: f32,
488
+ alignment: WidgetCrossAlignment,
489
+ tokens: DesignTokens,
490
+ ) f32 {
491
+ if (axis == .vertical and child.frame.height <= 0 and widgetSubtreeHasTextSpans(child, 0)) {
492
+ const width = preferredCrossExtent(child, axis, cross_extent, alignment, tokens);
493
+ return clampMainExtent(child, axis, wrappedVerticalExtentForWidth(child, width, tokens, 0));
494
+ }
495
+ return preferredMainExtent(child, axis, tokens);
496
+ }
497
+
498
+ /// Kinds whose `spans` field drives a span-paragraph text layout: plain
499
+ /// paragraphs and table cells (markdown tables put inline-styled runs in
500
+ /// `data_cell` widgets).
501
+ fn widgetIsSpanParagraph(widget: Widget) bool {
502
+ return (widget.kind == .text or widget.kind == .data_cell) and widget.spans.len > 0;
503
+ }
504
+
505
+ fn widgetSubtreeHasTextSpans(widget: Widget, depth: usize) bool {
506
+ if (depth >= max_widget_depth) return false;
507
+ if (widgetIsSpanParagraph(widget)) return true;
508
+ for (widget.children) |child| {
509
+ if (widgetSubtreeHasTextSpans(child, depth + 1)) return true;
510
+ }
511
+ return false;
512
+ }
513
+
514
+ /// Wrapped vertical extent of a widget when it is laid out at `width`.
515
+ /// This is the width-aware twin of `intrinsicWidgetSize` that span
516
+ /// paragraphs need; it recurses through the container kinds markdown
517
+ /// content composes from and falls back to the classic intrinsic extent
518
+ /// everywhere else.
519
+ fn wrappedVerticalExtentForWidth(widget: Widget, width: f32, tokens: DesignTokens, depth: usize) f32 {
520
+ if (depth >= max_widget_depth) return preferredMainExtent(widget, .vertical, tokens);
521
+ if (widget.frame.height > 0) return clampMainExtent(widget, .vertical, widget.frame.height);
522
+ const padding = widget.layout.padding;
523
+ const inner_width = @max(0, width - padding.left - padding.right);
524
+ const content_height: f32 = switch (widget.kind) {
525
+ .text, .data_cell => if (widget.spans.len > 0)
526
+ spanParagraphHeight(widget, inner_width, tokens)
527
+ else
528
+ return preferredMainExtent(widget, .vertical, tokens),
529
+ .column, .list, .data_grid, .table, .menu_surface, .dropdown_menu => blk: {
530
+ if (widget.layout.virtualized) return preferredMainExtent(widget, .vertical, tokens);
531
+ var sum: f32 = 0;
532
+ var flow_count: usize = 0;
533
+ for (widget.children) |child| {
534
+ if (child.layout.anchor != null) continue;
535
+ // A column-direct bubble hugs up to the thread fraction
536
+ // (the cross-extent seam), so its wrapped height must
537
+ // measure at that same capped width.
538
+ const child_width = if (child.frame.width > 0)
539
+ child.frame.width
540
+ else if (bubbleThreadCapEligible(child))
541
+ bubbleThreadWidthCap(child, inner_width, @min(inner_width, intrinsicWidgetSizeDepth(child, tokens, depth + 1).width))
542
+ else
543
+ inner_width;
544
+ sum += wrappedVerticalExtentForWidth(child, child_width, tokens, depth + 1);
545
+ flow_count += 1;
546
+ }
547
+ if (flow_count > 1) {
548
+ sum += nonNegative(widget.layout.gap) * @as(f32, @floatFromInt(flow_count - 1));
549
+ }
550
+ break :blk sum;
551
+ },
552
+ .stack, .panel, .card, .bubble, .resizable, .popover => blk: {
553
+ var max_height: f32 = 0;
554
+ for (widget.children) |child| {
555
+ if (child.layout.anchor != null) continue;
556
+ const child_width = if (child.frame.width > 0) child.frame.width else inner_width;
557
+ max_height = @max(max_height, wrappedVerticalExtentForWidth(child, child_width, tokens, depth + 1));
558
+ }
559
+ break :blk max_height;
560
+ },
561
+ // Alert children hang past the icon column and start under the
562
+ // title line (`alertContentFrame`), so wrapped descriptions
563
+ // measure at the indented width with the title's line reserved.
564
+ .alert => blk: {
565
+ const text_size = widgetBodyTextSize(widget, tokens);
566
+ const inset = widgetControlInset(widget, tokens, tokens.spacing.lg);
567
+ const icon_size = widgetSizedDensityValue(widget, tokens, 16);
568
+ const text_gap = widgetControlInset(widget, tokens, tokens.spacing.md);
569
+ const indent = if (widget.text.len > 0) icon_size + text_gap else 0;
570
+ var max_height: f32 = 0;
571
+ for (widget.children) |child| {
572
+ if (child.layout.anchor != null) continue;
573
+ const child_width = if (child.frame.width > 0) child.frame.width else @max(0, inner_width - indent);
574
+ max_height = @max(max_height, wrappedVerticalExtentForWidth(child, child_width, tokens, depth + 1));
575
+ }
576
+ var content = max_height;
577
+ if (widget.text.len > 0) {
578
+ const title_gap = widgetControlInset(widget, tokens, tokens.spacing.xs);
579
+ content = widgetLineHeight(text_size) + (if (max_height > 0) title_gap + max_height else 0);
580
+ }
581
+ // The same floor `intrinsicAlertWidgetSize` keeps, so wrapped
582
+ // and intrinsic measurements agree for short alerts.
583
+ const chrome_floor = @max(widgetSizedDensityValue(widget, tokens, 52), widgetLineHeight(text_size) + inset * 2);
584
+ break :blk @max(content, chrome_floor - padding.top - padding.bottom);
585
+ },
586
+ // Accordion content sits below the header band; wrapped content
587
+ // measures at the item's width so an expanded item reserves its
588
+ // real wrapped height.
589
+ .accordion => blk: {
590
+ const header_height = accordionHeaderHeight(widget, tokens);
591
+ if (!accordionChildrenVisible(widget)) break :blk header_height;
592
+ var max_height: f32 = 0;
593
+ for (widget.children) |child| {
594
+ if (child.layout.anchor != null) continue;
595
+ const child_width = if (child.frame.width > 0) child.frame.width else inner_width;
596
+ max_height = @max(max_height, wrappedVerticalExtentForWidth(child, child_width, tokens, depth + 1));
597
+ }
598
+ const gap = if (max_height > 0) nonNegative(widget.layout.gap) else 0;
599
+ break :blk header_height + gap + max_height;
600
+ },
601
+ .row, .data_row, .breadcrumb, .button_group, .pagination, .radio_group, .tabs, .toggle_group => blk: {
602
+ var max_height: f32 = 0;
603
+ for (widget.children, 0..) |child, index| {
604
+ if (child.layout.anchor != null) continue;
605
+ max_height = @max(max_height, wrappedVerticalExtentForWidth(
606
+ child,
607
+ rowChildWidth(widget, inner_width, index, tokens),
608
+ tokens,
609
+ depth + 1,
610
+ ));
611
+ }
612
+ break :blk max_height;
613
+ },
614
+ else => return preferredMainExtent(widget, .vertical, tokens),
615
+ };
616
+ return clampMainExtent(widget, .vertical, content_height + padding.top + padding.bottom);
617
+ }
618
+
619
+ /// The width the `index`-th child of a horizontal container receives —
620
+ /// the same fixed-vs-grow split `layoutAxisChildren` performs, replayed
621
+ /// so wrapped heights inside rows (blockquotes, list items) are computed
622
+ /// against real widths.
623
+ fn rowChildWidth(row: Widget, available_width: f32, index: usize, tokens: DesignTokens) f32 {
624
+ const children = row.children;
625
+ if (children.len == 0) return available_width;
626
+ var flow_count: usize = 0;
627
+ var fixed_extent: f32 = 0;
628
+ var grow_total: f32 = 0;
629
+ for (children) |child| {
630
+ if (child.layout.anchor != null) continue;
631
+ flow_count += 1;
632
+ const grow = nonNegative(child.layout.grow);
633
+ if (grow > 0) {
634
+ grow_total += grow;
635
+ } else {
636
+ fixed_extent += bubbleThreadWidthCap(child, available_width, preferredMainExtent(child, .horizontal, tokens));
637
+ }
638
+ }
639
+ if (flow_count == 0) return available_width;
640
+ // Button groups and tab strips replay their register's effective gap
641
+ // (see `buttonGroupGap`/`tabsGap`), matching what `layoutAxisChildren`
642
+ // used.
643
+ const row_gap = switch (row.kind) {
644
+ .button_group => buttonGroupGap(row, tokens),
645
+ .tabs => tabsGap(row, tokens),
646
+ else => nonNegative(row.layout.gap),
647
+ };
648
+ const total_gap = row_gap * @as(f32, @floatFromInt(flow_count - 1));
649
+ const remaining = @max(0, available_width - fixed_extent - total_gap);
650
+ const child = children[index];
651
+ const grow = nonNegative(child.layout.grow);
652
+ if (grow > 0 and grow_total > 0) return clampMainExtent(child, .horizontal, remaining * grow / grow_total);
653
+ // The same bubble thread cap `layoutAxisChildren` applies, replayed
654
+ // here so wrapped heights measure at the width the bubble will
655
+ // actually receive.
656
+ return bubbleThreadWidthCap(child, available_width, preferredMainExtent(child, .horizontal, tokens));
657
+ }
658
+
659
+ fn spanParagraphHeight(widget: Widget, width: f32, tokens: DesignTokens) f32 {
660
+ return text_spans_model.textSpansWrappedHeight(
661
+ widget.spans,
662
+ widgetTextSpanLayoutOptions(widget, tokens, width),
663
+ );
664
+ }
665
+
666
+ /// Position a span paragraph's link hit-area children. By convention the
667
+ /// children of a `.text` widget with spans are its link hotspots, one per
668
+ /// link span in order (`Ui.paragraph` builds them). Each child gets the
669
+ /// union frame of its span's laid-out runs; surplus children collapse to
670
+ /// an empty frame (never hit-testable).
671
+ fn layoutTextSpanLinkChildren(
672
+ widget: Widget,
673
+ content: geometry.RectF,
674
+ parent_index: usize,
675
+ depth: usize,
676
+ output: []WidgetLayoutNode,
677
+ len: *usize,
678
+ tokens: DesignTokens,
679
+ ) Error!void {
680
+ if (widget.children.len == 0) return;
681
+ if (widget.spans.len == 0) return;
682
+
683
+ var runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
684
+ const layout = text_spans_model.layoutTextSpans(
685
+ widget.spans,
686
+ widgetTextSpanLayoutOptions(widget, tokens, content.width),
687
+ &runs,
688
+ );
689
+
690
+ var child_index: usize = 0;
691
+ for (widget.spans, 0..) |span, span_index| {
692
+ if (span.link.len == 0) continue;
693
+ if (child_index >= widget.children.len) break;
694
+ const child = widget.children[child_index];
695
+ child_index += 1;
696
+ if (child.layout.anchor != null) continue;
697
+ const frame = if (text_spans_model.textSpanBounds(layout, span_index)) |bounds|
698
+ geometry.RectF.init(content.x + bounds.x, content.y + bounds.y, bounds.width, bounds.height)
699
+ else
700
+ geometry.RectF.init(content.x, content.y, 0, 0);
701
+ _ = try layoutWidgetDepth(child, frame, parent_index, depth + 1, output, len, tokens);
702
+ }
703
+ while (child_index < widget.children.len) : (child_index += 1) {
704
+ if (widget.children[child_index].layout.anchor != null) continue;
705
+ _ = try layoutWidgetDepth(widget.children[child_index], geometry.RectF.init(content.x, content.y, 0, 0), parent_index, depth + 1, output, len, tokens);
706
+ }
707
+ }
708
+
709
+ fn assignedAxisChildrenExtent(children: []const Widget, axis: LayoutAxis, fixed_extent: f32, grow_total: f32, remaining: f32) f32 {
710
+ if (grow_total <= 0) return fixed_extent;
711
+ var assigned = fixed_extent;
712
+ for (children) |child| {
713
+ if (child.layout.anchor != null) continue;
714
+ const grow = nonNegative(child.layout.grow);
715
+ if (grow <= 0) continue;
716
+ assigned += clampMainExtent(child, axis, remaining * grow / grow_total);
717
+ }
718
+ return assigned;
719
+ }
720
+
721
+ fn mainAxisAlignmentOffset(alignment: WidgetMainAlignment, free_extent: f32) f32 {
722
+ return switch (alignment) {
723
+ .start, .space_between => 0,
724
+ .center => free_extent * 0.5,
725
+ .end => free_extent,
726
+ };
727
+ }
728
+
729
+ fn alignedCrossAxisOrigin(
730
+ content: geometry.RectF,
731
+ axis: LayoutAxis,
732
+ available_extent: f32,
733
+ child_extent: f32,
734
+ child: Widget,
735
+ alignment: WidgetCrossAlignment,
736
+ ) f32 {
737
+ const start = switch (axis) {
738
+ .horizontal => content.y,
739
+ .vertical => content.x,
740
+ };
741
+ const offset = switch (axis) {
742
+ .horizontal => child.frame.y,
743
+ .vertical => child.frame.x,
744
+ };
745
+ return start + offset + switch (alignment) {
746
+ .stretch, .start => 0,
747
+ // Centering keeps its promise when the child is BIGGER than the
748
+ // band: the free extent goes negative and the overflow splits
749
+ // evenly across both edges — a taller-than-the-row child sits
750
+ // optically centered instead of pinning to the top and pushing
751
+ // its whole overflow past the bottom edge.
752
+ .center => (available_extent - child_extent) * 0.5,
753
+ .end => @max(0, available_extent - child_extent),
754
+ };
755
+ }
756
+
757
+ fn layoutGridChildren(
758
+ children: []const Widget,
759
+ content: geometry.RectF,
760
+ parent_index: usize,
761
+ depth: usize,
762
+ output: []WidgetLayoutNode,
763
+ len: *usize,
764
+ gap: f32,
765
+ requested_columns: usize,
766
+ tokens: DesignTokens,
767
+ ) Error!void {
768
+ if (children.len == 0) return;
769
+
770
+ const columns = gridColumnCount(children.len, requested_columns);
771
+ const rows = gridRowCount(children.len, columns);
772
+ const clamped_gap = nonNegative(gap);
773
+ const total_column_gap = clamped_gap * @as(f32, @floatFromInt(columns - 1));
774
+ const total_row_gap = clamped_gap * @as(f32, @floatFromInt(rows - 1));
775
+ const cell_width = if (columns > 0) @max(0, content.width - total_column_gap) / @as(f32, @floatFromInt(columns)) else 0;
776
+ const fallback_cell_height = if (rows > 0) @max(0, content.height - total_row_gap) / @as(f32, @floatFromInt(rows)) else 0;
777
+
778
+ for (children, 0..) |child, child_index| {
779
+ // Anchored floating children keep their grid slot empty.
780
+ if (child.layout.anchor != null) continue;
781
+ const column = child_index % columns;
782
+ const row = child_index / columns;
783
+ const x = content.x + @as(f32, @floatFromInt(column)) * (cell_width + clamped_gap);
784
+ const y = content.y + @as(f32, @floatFromInt(row)) * (fallback_cell_height + clamped_gap);
785
+ const width = clampIntrinsicAxis(if (child.frame.width > 0) child.frame.width else cell_width, child.layout.min_size.width, child.layout.max_size.width);
786
+ const height = clampIntrinsicAxis(if (child.frame.height > 0) child.frame.height else fallback_cell_height, child.layout.min_size.height, child.layout.max_size.height);
787
+ const child_frame = geometry.RectF.init(
788
+ x + child.frame.x,
789
+ y + child.frame.y,
790
+ width,
791
+ height,
792
+ );
793
+ _ = try layoutWidgetDepth(child, child_frame, parent_index, depth + 1, output, len, tokens);
794
+ }
795
+ }
796
+
797
+ fn layoutVirtualGridChildren(
798
+ children: []const Widget,
799
+ content: geometry.RectF,
800
+ parent_index: usize,
801
+ depth: usize,
802
+ output: []WidgetLayoutNode,
803
+ len: *usize,
804
+ scroll_y: f32,
805
+ style: WidgetLayoutStyle,
806
+ tokens: DesignTokens,
807
+ ) Error!void {
808
+ if (children.len == 0) return;
809
+
810
+ const columns = gridColumnCount(children.len, style.columns);
811
+ const rows = gridRowCount(children.len, columns);
812
+ if (columns == 0 or rows == 0) return;
813
+
814
+ const clamped_gap = nonNegative(style.gap);
815
+ const total_column_gap = clamped_gap * @as(f32, @floatFromInt(columns - 1));
816
+ const cell_width = @max(0, content.width - total_column_gap) / @as(f32, @floatFromInt(columns));
817
+ const item_extent = if (style.virtual_item_extent > 0)
818
+ style.virtual_item_extent
819
+ else
820
+ preferredGridRowExtent(children, columns, tokens);
821
+ const range = virtualListRange(.{
822
+ .item_count = rows,
823
+ .item_extent = item_extent,
824
+ .item_gap = clamped_gap,
825
+ .viewport_extent = content.height,
826
+ .scroll_offset = scroll_y,
827
+ .overscan = style.virtual_overscan,
828
+ });
829
+ output[parent_index].widget.layout.virtual_item_extent = range.item_extent;
830
+ output[parent_index].widget.semantics.list_item_count = saturatingU32(rows);
831
+ if (range.isEmpty()) return;
832
+
833
+ const stride = range.item_extent + range.item_gap;
834
+ var row = range.start_index;
835
+ while (row < range.end_index) : (row += 1) {
836
+ var column: usize = 0;
837
+ while (column < columns) : (column += 1) {
838
+ const child_index = row * columns + column;
839
+ if (child_index >= children.len) break;
840
+ if (children[child_index].layout.anchor != null) continue;
841
+
842
+ var child = children[child_index];
843
+ child.semantics.list_item_index = saturatingU32(child_index);
844
+ child.semantics.list_item_count = saturatingU32(children.len);
845
+ const x = content.x + @as(f32, @floatFromInt(column)) * (cell_width + clamped_gap);
846
+ const y = content.y + @as(f32, @floatFromInt(row)) * stride - range.layout_offset + child.frame.y;
847
+ const width = clampIntrinsicAxis(if (child.frame.width > 0) child.frame.width else cell_width, child.layout.min_size.width, child.layout.max_size.width);
848
+ const height = clampIntrinsicAxis(if (child.frame.height > 0) child.frame.height else range.item_extent, child.layout.min_size.height, child.layout.max_size.height);
849
+ const child_frame = geometry.RectF.init(
850
+ x + child.frame.x,
851
+ y,
852
+ width,
853
+ height,
854
+ );
855
+ _ = try layoutWidgetDepth(child, child_frame, parent_index, depth + 1, output, len, tokens);
856
+ }
857
+ }
858
+ }
859
+
860
+ fn preferredGridRowExtent(children: []const Widget, columns: usize, tokens: DesignTokens) f32 {
861
+ if (children.len == 0 or columns == 0) return 0;
862
+ var max_height: f32 = 0;
863
+ var index: usize = 0;
864
+ while (index < children.len and index < columns) : (index += 1) {
865
+ max_height = @max(max_height, preferredMainExtent(children[index], .vertical, tokens));
866
+ }
867
+ return max_height;
868
+ }
869
+
870
+ fn layoutScrollChildren(
871
+ children: []const Widget,
872
+ content: geometry.RectF,
873
+ parent_index: usize,
874
+ depth: usize,
875
+ output: []WidgetLayoutNode,
876
+ len: *usize,
877
+ scroll_y: f32,
878
+ tokens: DesignTokens,
879
+ ) Error!void {
880
+ const scrolled_content = content.translate(geometry.OffsetF.init(0, -scroll_y));
881
+ for (children) |child| {
882
+ if (child.layout.anchor != null) continue;
883
+ _ = try layoutWidgetDepth(child, stackChildFrame(scrolled_content, child), parent_index, depth + 1, output, len, tokens);
884
+ }
885
+ }
886
+
887
+ fn layoutVirtualVerticalChildren(
888
+ children: []const Widget,
889
+ content: geometry.RectF,
890
+ parent_index: usize,
891
+ depth: usize,
892
+ output: []WidgetLayoutNode,
893
+ len: *usize,
894
+ scroll_y: f32,
895
+ style: WidgetLayoutStyle,
896
+ tokens: DesignTokens,
897
+ ) Error!void {
898
+ if (children.len == 0) return;
899
+
900
+ // A VARIABLE-extent windowed virtual list (`virtual_total_extent >
901
+ // 0`): the window's offset table already priced everything outside
902
+ // the built slice — `virtual_anchor_extent` is the anchor row's
903
+ // absolute leading edge and `virtual_total_extent` the scroll
904
+ // content extent. Inside the window the rows stack at their
905
+ // INTRINSIC heights (the width-aware wrapped extent) around the
906
+ // anchor, so the content under the user's eyes never carries
907
+ // estimate error; the measure step reads these frames back to
908
+ // correct the table for the unmounted ranges.
909
+ if (style.virtual_total_extent > 0 and style.virtual_item_count > 0) {
910
+ return layoutVariableVirtualChildren(children, content, parent_index, depth, output, len, scroll_y, style, tokens);
911
+ }
912
+
913
+ // A windowed virtual list (`virtual_item_count > 0`) holds only the
914
+ // built slice in `children`: `first_index` names the virtual index of
915
+ // children[0], the declared count drives the range math (content
916
+ // extent, scrollbar, list_item_count semantics), and each built child
917
+ // lands at its ABSOLUTE virtual offset. The legacy contract (no
918
+ // declared count: children are the full item set) is byte-identical.
919
+ const first_index = style.virtual_first_index;
920
+ const item_count = if (style.virtual_item_count > 0)
921
+ @max(style.virtual_item_count, first_index + children.len)
922
+ else
923
+ children.len;
924
+ const item_extent = if (style.virtual_item_extent > 0)
925
+ style.virtual_item_extent
926
+ else
927
+ preferredMainExtent(children[0], .vertical, tokens);
928
+ const range = virtualListRange(.{
929
+ .item_count = item_count,
930
+ .item_extent = item_extent,
931
+ .item_gap = style.gap,
932
+ .viewport_extent = content.height,
933
+ .scroll_offset = scroll_y,
934
+ .overscan = style.virtual_overscan,
935
+ });
936
+ output[parent_index].widget.layout.virtual_item_extent = range.item_extent;
937
+ output[parent_index].widget.semantics.list_item_count = saturatingU32(item_count);
938
+ if (range.isEmpty()) return;
939
+
940
+ const stride = range.item_extent + range.item_gap;
941
+ var index = @max(range.start_index, first_index);
942
+ const end_index = @min(range.end_index, first_index + children.len);
943
+ while (index < end_index) : (index += 1) {
944
+ if (children[index - first_index].layout.anchor != null) continue;
945
+ var child = children[index - first_index];
946
+ child.semantics.list_item_index = saturatingU32(index);
947
+ child.semantics.list_item_count = saturatingU32(item_count);
948
+ const y = content.y + @as(f32, @floatFromInt(index)) * stride - range.layout_offset + child.frame.y;
949
+ const width = clampIntrinsicAxis(if (child.frame.width > 0) child.frame.width else content.width, child.layout.min_size.width, child.layout.max_size.width);
950
+ const height = clampIntrinsicAxis(if (child.frame.height > 0) child.frame.height else range.item_extent, child.layout.min_size.height, child.layout.max_size.height);
951
+ const child_frame = geometry.RectF.init(
952
+ content.x + child.frame.x,
953
+ y,
954
+ width,
955
+ height,
956
+ );
957
+ _ = try layoutWidgetDepth(child, child_frame, parent_index, depth + 1, output, len, tokens);
958
+ }
959
+ }
960
+
961
+ /// The variable-extent windowed virtual list's layout: the ANCHOR row
962
+ /// (the first visible one at the offset the window was computed for)
963
+ /// lands exactly at the offset table's leading edge for it, and the
964
+ /// other built rows stack around it at their intrinsic (width-aware
965
+ /// wrapped) heights — downward below the anchor, upward above it. The
966
+ /// asymmetry is the point: rows entering the window from above are
967
+ /// priced by ESTIMATES until the measure step corrects the table, and
968
+ /// upward stacking puts that pricing error off-screen above the anchor
969
+ /// instead of displacing the content under the user's eyes. The scroll
970
+ /// offset clamps against the table's total extent — the same value the
971
+ /// scrollbar and the native driver's content size report — with the
972
+ /// uniform path's rubber-band band on the layout offset.
973
+ fn layoutVariableVirtualChildren(
974
+ children: []const Widget,
975
+ content: geometry.RectF,
976
+ parent_index: usize,
977
+ depth: usize,
978
+ output: []WidgetLayoutNode,
979
+ len: *usize,
980
+ scroll_y: f32,
981
+ style: WidgetLayoutStyle,
982
+ tokens: DesignTokens,
983
+ ) Error!void {
984
+ const first_index = style.virtual_first_index;
985
+ const item_count = @max(style.virtual_item_count, first_index + children.len);
986
+ const gap = nonNegative(style.gap);
987
+ const total_extent = @max(0, style.virtual_total_extent);
988
+ const max_offset = @max(0, total_extent - content.height);
989
+ const raw_offset = if (std.math.isFinite(scroll_y)) scroll_y else 0;
990
+ const layout_offset = std.math.clamp(raw_offset, -content.height, max_offset + content.height);
991
+
992
+ output[parent_index].widget.semantics.list_item_count = saturatingU32(item_count);
993
+
994
+ const anchor_child = std.math.clamp(style.virtual_anchor_index -| first_index, 0, children.len - 1);
995
+ const anchor_y = content.y + @max(0, style.virtual_anchor_extent) - layout_offset;
996
+
997
+ // Pre-pass: back the start edge out of the anchor position by the
998
+ // above-anchor rows' intrinsic extents (the upward stack), then
999
+ // emit every row in window order so layout-node order — which
1000
+ // semantics and hit routing walk — matches the source order.
1001
+ var y = anchor_y;
1002
+ for (children[0..anchor_child]) |child| {
1003
+ y -= variableVirtualChildExtent(child, content.width, tokens, depth) + gap;
1004
+ }
1005
+ for (children, 0..) |child, offset| {
1006
+ const height = variableVirtualChildExtent(child, content.width, tokens, depth);
1007
+ if (child.layout.anchor == null) {
1008
+ try layoutVariableVirtualChild(child, content, y, height, first_index + offset, item_count, parent_index, depth, output, len, tokens);
1009
+ }
1010
+ y += height + gap;
1011
+ }
1012
+ }
1013
+
1014
+ fn variableVirtualChildExtent(child: Widget, width: f32, tokens: DesignTokens, depth: usize) f32 {
1015
+ return clampIntrinsicAxis(
1016
+ if (child.frame.height > 0) child.frame.height else variableVirtualRowExtent(child, width, tokens, depth + 1),
1017
+ child.layout.min_size.height,
1018
+ child.layout.max_size.height,
1019
+ );
1020
+ }
1021
+
1022
+ fn layoutVariableVirtualChild(
1023
+ child_source: Widget,
1024
+ content: geometry.RectF,
1025
+ y: f32,
1026
+ height: f32,
1027
+ item_index: usize,
1028
+ item_count: usize,
1029
+ parent_index: usize,
1030
+ depth: usize,
1031
+ output: []WidgetLayoutNode,
1032
+ len: *usize,
1033
+ tokens: DesignTokens,
1034
+ ) Error!void {
1035
+ var child = child_source;
1036
+ child.semantics.list_item_index = saturatingU32(item_index);
1037
+ child.semantics.list_item_count = saturatingU32(item_count);
1038
+ const width = clampIntrinsicAxis(if (child.frame.width > 0) child.frame.width else content.width, child.layout.min_size.width, child.layout.max_size.width);
1039
+ const child_frame = geometry.RectF.init(
1040
+ content.x + child.frame.x,
1041
+ y + child.frame.y,
1042
+ width,
1043
+ height,
1044
+ );
1045
+ _ = try layoutWidgetDepth(child, child_frame, parent_index, depth + 1, output, len, tokens);
1046
+ }
1047
+
1048
+ /// Intrinsic height of one variable-extent virtual row at the window
1049
+ /// width. `.list_item` rows measure like the `.row` family — the max of
1050
+ /// their children's WIDTH-AWARE wrapped heights at their real widths —
1051
+ /// because their children flow horizontally and routinely hold wrapped
1052
+ /// paragraph columns. Scoped to the variable virtual path so every
1053
+ /// non-virtual list_item keeps its classic intrinsic sizing
1054
+ /// byte-identically.
1055
+ fn variableVirtualRowExtent(child: Widget, width: f32, tokens: DesignTokens, depth: usize) f32 {
1056
+ if (child.kind != .list_item) return wrappedVerticalExtentForWidth(child, width, tokens, depth);
1057
+ if (depth >= max_widget_depth) return preferredMainExtent(child, .vertical, tokens);
1058
+ if (child.frame.height > 0) return clampMainExtent(child, .vertical, child.frame.height);
1059
+ const padding = child.layout.padding;
1060
+ const inner_width = @max(0, width - padding.left - padding.right);
1061
+ var max_height: f32 = 0;
1062
+ for (child.children, 0..) |grand, index| {
1063
+ if (grand.layout.anchor != null) continue;
1064
+ max_height = @max(max_height, wrappedVerticalExtentForWidth(
1065
+ grand,
1066
+ rowChildWidth(child, inner_width, index, tokens),
1067
+ tokens,
1068
+ depth + 1,
1069
+ ));
1070
+ }
1071
+ return clampMainExtent(child, .vertical, max_height + padding.top + padding.bottom);
1072
+ }
1073
+
1074
+ /// Width of a split's divider band (the hit target; the painted line is
1075
+ /// thinner). `layout.gap` overrides it, so markup `gap` on a split means
1076
+ /// "divider band thickness" — the one flow gap a splitter has.
1077
+ pub fn splitDividerExtent(widget: Widget) f32 {
1078
+ const gap = nonNegative(widget.layout.gap);
1079
+ return if (gap > 0) gap else 9;
1080
+ }
1081
+
1082
+ /// The fraction band a split's divider may occupy, derived from the
1083
+ /// panes' `min_size.width` floors against the width left for panes.
1084
+ /// Degenerate spaces (mins exceed the available width) collapse to the
1085
+ /// proportional midpoint so layout never inverts.
1086
+ pub const SplitFractionBounds = struct {
1087
+ low: f32,
1088
+ high: f32,
1089
+ };
1090
+
1091
+ pub fn splitFractionBounds(available: f32, first_min: f32, second_min: f32) SplitFractionBounds {
1092
+ if (available <= 0) return .{ .low = 0, .high = 1 };
1093
+ const low = std.math.clamp(nonNegative(first_min) / available, 0, 1);
1094
+ const high = std.math.clamp(1 - nonNegative(second_min) / available, 0, 1);
1095
+ if (low > high) {
1096
+ const mid = low / @max(low + (1 - high), 0.0001);
1097
+ return .{ .low = mid, .high = mid };
1098
+ }
1099
+ return .{ .low = low, .high = high };
1100
+ }
1101
+
1102
+ /// The effective first-pane fraction a split lays out at: the authored /
1103
+ /// reconciled `value` (0 = unset lays out at 0.5) clamped into the
1104
+ /// panes' min-width band. Shared with the runtime's divider drag so the
1105
+ /// two can never disagree about clamping.
1106
+ pub fn splitEffectiveFraction(value: f32, available: f32, first_min: f32, second_min: f32) f32 {
1107
+ const base = if (!std.math.isFinite(value) or value <= 0) 0.5 else @min(value, 1);
1108
+ const bounds = splitFractionBounds(available, first_min, second_min);
1109
+ return std.math.clamp(base, bounds.low, bounds.high);
1110
+ }
1111
+
1112
+ /// Split layout: [pane 1][divider][pane 2] along the horizontal axis.
1113
+ /// The divider is the builder-synthesized `.split_divider` child; panes
1114
+ /// are the remaining flow children (exactly two by the validator's
1115
+ /// rule — extras degrade to zero-width frames rather than failing). The
1116
+ /// first pane takes `splitEffectiveFraction` of the width left after
1117
+ /// the divider band; both panes stretch the full height.
1118
+ fn layoutSplitChildren(
1119
+ widget: Widget,
1120
+ content: geometry.RectF,
1121
+ parent_index: usize,
1122
+ depth: usize,
1123
+ output: []WidgetLayoutNode,
1124
+ len: *usize,
1125
+ tokens: DesignTokens,
1126
+ ) Error!void {
1127
+ var panes: [2]?Widget = .{ null, null };
1128
+ var divider: ?Widget = null;
1129
+ var extra_start: ?usize = null;
1130
+ for (widget.children, 0..) |child, child_index| {
1131
+ if (child.layout.anchor != null) continue;
1132
+ if (child.kind == .split_divider) {
1133
+ if (divider == null) divider = child;
1134
+ continue;
1135
+ }
1136
+ if (panes[0] == null) {
1137
+ panes[0] = child;
1138
+ } else if (panes[1] == null) {
1139
+ panes[1] = child;
1140
+ } else if (extra_start == null) {
1141
+ extra_start = child_index;
1142
+ }
1143
+ }
1144
+
1145
+ const divider_extent = if (divider != null) splitDividerExtent(widget) else 0;
1146
+ const available = @max(0, content.width - divider_extent);
1147
+ const first_min = if (panes[0]) |pane| nonNegative(pane.layout.min_size.width) else 0;
1148
+ const second_min = if (panes[1]) |pane| nonNegative(pane.layout.min_size.width) else 0;
1149
+ const fraction = splitEffectiveFraction(widget.value, available, first_min, second_min);
1150
+ const first_width = if (panes[1] == null) available else available * fraction;
1151
+
1152
+ var cursor = content.x;
1153
+ if (panes[0]) |pane| {
1154
+ _ = try layoutWidgetDepth(pane, geometry.RectF.init(cursor, content.y, first_width, content.height), parent_index, depth + 1, output, len, tokens);
1155
+ cursor += first_width;
1156
+ }
1157
+ if (divider) |handle| {
1158
+ // The handle mirrors the EFFECTIVE fraction so keyboard steps and
1159
+ // separator semantics read the position layout actually used.
1160
+ var handle_copy = handle;
1161
+ handle_copy.value = fraction;
1162
+ _ = try layoutWidgetDepth(handle_copy, geometry.RectF.init(cursor, content.y, divider_extent, content.height), parent_index, depth + 1, output, len, tokens);
1163
+ cursor += divider_extent;
1164
+ }
1165
+ if (panes[1]) |pane| {
1166
+ const second_width = @max(0, content.maxX() - cursor);
1167
+ _ = try layoutWidgetDepth(pane, geometry.RectF.init(cursor, content.y, second_width, content.height), parent_index, depth + 1, output, len, tokens);
1168
+ }
1169
+ // Panes past the first two never happen through the builder/markup
1170
+ // (the validator enforces exactly two); raw trees degrade to empty
1171
+ // frames so the node count still matches the source tree.
1172
+ if (extra_start) |start| {
1173
+ for (widget.children[start..]) |child| {
1174
+ if (child.layout.anchor != null or child.kind == .split_divider) continue;
1175
+ _ = try layoutWidgetDepth(child, geometry.RectF.init(content.maxX(), content.y, 0, 0), parent_index, depth + 1, output, len, tokens);
1176
+ }
1177
+ }
1178
+ }
1179
+
1180
+ /// Re-run a split node's child layout IN PLACE over an already-laid
1181
+ /// node buffer: the runtime reconcile restores a runtime-owned fraction
1182
+ /// after the source laid out at its own value, and the same children
1183
+ /// produce the same node sequence, so only frames (and the handle's
1184
+ /// mirrored value) change. `widget` must still carry its source
1185
+ /// children (the reconcile runs while the app's build arena is alive);
1186
+ /// callers pass the node's laid frame and depth.
1187
+ pub fn relayoutSplitChildren(
1188
+ widget: Widget,
1189
+ frame: geometry.RectF,
1190
+ node_index: usize,
1191
+ depth: usize,
1192
+ output: []WidgetLayoutNode,
1193
+ tokens: DesignTokens,
1194
+ ) Error!void {
1195
+ var len: usize = node_index + 1;
1196
+ const content = frame.inset(widget.layout.padding);
1197
+ try layoutSplitChildren(widget, content, node_index, depth, output, &len, tokens);
1198
+ try layoutAnchoredChildren(widget.children, frame, node_index, depth, output, &len, tokens);
1199
+ }
1200
+
1201
+ /// Slide a laid split's pane boundary to `fraction` GEOMETRICALLY, over
1202
+ /// an already-laid node buffer: pane frames move, the second pane's
1203
+ /// subtree translates with its leading edge, and NO child re-lays —
1204
+ /// every descendant keeps the wrap the source layout gave it. This is
1205
+ /// the reveal-under-clip half of the split layout tween (the disclosure
1206
+ /// doctrine, horizontal): the source lays panes out ONCE at the tween's
1207
+ /// TARGET fraction, this slide restores the mid-flight boundary, and
1208
+ /// the pane's built-in content clip crops the overflowing side while
1209
+ /// the tween walks the boundary to the already-laid pose. Contrast
1210
+ /// `relayoutSplitChildren`, which re-runs child layout at the restored
1211
+ /// fraction — the honest shape for a settled runtime-owned fraction,
1212
+ /// and exactly the per-step re-wrap a tween must never pay.
1213
+ pub fn slideSplitChildren(
1214
+ frame: geometry.RectF,
1215
+ fraction: f32,
1216
+ node_index: usize,
1217
+ nodes: []WidgetLayoutNode,
1218
+ ) void {
1219
+ const split_depth = nodes[node_index].depth;
1220
+ var pane_indices: [2]?usize = .{ null, null };
1221
+ var divider_index: ?usize = null;
1222
+ var child = node_index + 1;
1223
+ while (child < nodes.len and nodes[child].depth > split_depth) : (child += 1) {
1224
+ if (nodes[child].parent_index != node_index) continue;
1225
+ if (nodes[child].widget.layout.anchor != null) continue;
1226
+ if (nodes[child].widget.kind == .split_divider) {
1227
+ if (divider_index == null) divider_index = child;
1228
+ } else if (pane_indices[0] == null) {
1229
+ pane_indices[0] = child;
1230
+ } else if (pane_indices[1] == null) {
1231
+ pane_indices[1] = child;
1232
+ }
1233
+ }
1234
+ const first_index = pane_indices[0] orelse return;
1235
+ const second_index = pane_indices[1] orelse return;
1236
+ const handle_index = divider_index orelse return;
1237
+
1238
+ const content = frame.inset(nodes[node_index].widget.layout.padding).normalized();
1239
+ const divider_extent = nodes[handle_index].frame.width;
1240
+ const available = @max(0, content.width - divider_extent);
1241
+ const first_min = @max(0, nodes[first_index].widget.layout.min_size.width);
1242
+ const second_min = @max(0, nodes[second_index].widget.layout.min_size.width);
1243
+ // The same clamp family the runtime's drag echo applies: a
1244
+ // sub-epsilon fraction stays a sliver instead of falling into the
1245
+ // `<= 0` unset sentinel, and pane min widths bound the boundary.
1246
+ const effective = splitEffectiveFraction(@max(fraction, 0.0001), available, first_min, second_min);
1247
+
1248
+ const first_width = available * effective;
1249
+ const divider_x = content.x + first_width;
1250
+ const dx = divider_x - nodes[handle_index].frame.x;
1251
+
1252
+ nodes[node_index].widget.value = effective;
1253
+ nodes[handle_index].widget.value = effective;
1254
+ nodes[first_index].frame.width = first_width;
1255
+ nodes[first_index].widget.frame = nodes[first_index].frame;
1256
+ nodes[handle_index].frame.x = divider_x;
1257
+ nodes[handle_index].widget.frame = nodes[handle_index].frame;
1258
+ const second_x = divider_x + divider_extent;
1259
+ nodes[second_index].frame.x = second_x;
1260
+ nodes[second_index].frame.width = @max(0, content.maxX() - second_x);
1261
+ nodes[second_index].widget.frame = nodes[second_index].frame;
1262
+ if (dx == 0) return;
1263
+ // The second pane's content rides its leading edge: translate the
1264
+ // whole subtree (frames only — wraps and sizes stand).
1265
+ const second_depth = nodes[second_index].depth;
1266
+ var index = second_index + 1;
1267
+ while (index < nodes.len and nodes[index].depth > second_depth) : (index += 1) {
1268
+ nodes[index].frame.x += dx;
1269
+ nodes[index].widget.frame = nodes[index].frame;
1270
+ }
1271
+ }
1272
+
1273
+ /// Widget kinds whose layout gives every child the full content box
1274
+ /// (the `stackChildFrame` arm in `layoutWidgetDepth` — keep the two in
1275
+ /// lockstep): children layer on top of each other, so `layout.gap` can
1276
+ /// never space them. This is the source of truth for the builder's Debug
1277
+ /// gap diagnostic and (via a name list kept in sync by a test in
1278
+ /// ui_markup_view_tests.zig) the markup validator's stack-container list.
1279
+ /// `scroll_view` and `accordion` also stack children but consume `gap`
1280
+ /// (virtualized item spacing; header-to-content spacing), so they are
1281
+ /// excluded on purpose.
1282
+ pub fn widgetKindStacksChildren(kind: widget_model.WidgetKind) bool {
1283
+ return switch (kind) {
1284
+ .stack, .alert, .bubble, .card, .dialog, .drawer, .sheet, .resizable, .panel, .popover => true,
1285
+ else => false,
1286
+ };
1287
+ }
1288
+
1289
+ fn stackChildFrame(content: geometry.RectF, child: Widget) geometry.RectF {
1290
+ const width = if (child.frame.width > 0) child.frame.width else content.width;
1291
+ const height = if (child.frame.height > 0) child.frame.height else content.height;
1292
+ return geometry.RectF.init(
1293
+ content.x + child.frame.x,
1294
+ content.y + child.frame.y,
1295
+ clampIntrinsicAxis(width, child.layout.min_size.width, child.layout.max_size.width),
1296
+ clampIntrinsicAxis(height, child.layout.min_size.height, child.layout.max_size.height),
1297
+ );
1298
+ }
1299
+
1300
+ /// Whether the accordion's EXTENT includes its content: only while
1301
+ /// open. This gates measurement (a closed item stacks at header height)
1302
+ /// but NOT child layout — children lay out at full size regardless, so
1303
+ /// the runtime's disclosure tween can reveal or conceal them without a
1304
+ /// single mid-flight re-wrap.
1305
+ fn accordionChildrenVisible(widget: Widget) bool {
1306
+ return widget.kind != .accordion or booleanControlSelected(widget);
1307
+ }
1308
+
1309
+ fn accordionContentFrame(widget: Widget, content: geometry.RectF, tokens: DesignTokens, depth: usize) geometry.RectF {
1310
+ if (widget.kind != .accordion) return content;
1311
+ const header_height = accordionHeaderHeight(widget, tokens);
1312
+ const gap = nonNegative(widget.layout.gap);
1313
+ const y = content.y + header_height + gap;
1314
+ // An open item's frame reserves the content box below the header; a
1315
+ // closed item's header-only frame reserves nothing, so the content
1316
+ // box sizes itself from the children's own wrapped extent — the
1317
+ // same measurement the open pose's extent is built from, which is
1318
+ // what keeps child geometry identical across both poses.
1319
+ const remaining = content.maxY() - y;
1320
+ const height = if (remaining > 0) remaining else accordionOpenContentExtent(widget, content.width, tokens, depth);
1321
+ return geometry.RectF.init(content.x, y, content.width, height);
1322
+ }
1323
+
1324
+ /// The content height an OPEN pose grants: the tallest in-flow child at
1325
+ /// its wrapped width — the same per-child measurement the accordion's
1326
+ /// vertical extent uses, replayed so a closed pose can hand children
1327
+ /// full-size frames.
1328
+ fn accordionOpenContentExtent(widget: Widget, width: f32, tokens: DesignTokens, depth: usize) f32 {
1329
+ var max_height: f32 = 0;
1330
+ for (widget.children) |child| {
1331
+ if (child.layout.anchor != null) continue;
1332
+ const child_width = if (child.frame.width > 0) child.frame.width else width;
1333
+ max_height = @max(max_height, wrappedVerticalExtentForWidth(child, child_width, tokens, depth + 1));
1334
+ }
1335
+ return max_height;
1336
+ }
1337
+
1338
+ pub fn accordionHeaderHeight(widget: Widget, tokens: DesignTokens) f32 {
1339
+ // The house trigger band: the label line with py-4 — a spacing.lg
1340
+ // inset above and below it (density/size scaled).
1341
+ const text_size = widgetBodyTextSize(widget, tokens);
1342
+ const inset = widgetControlInset(widget, tokens, tokens.spacing.lg);
1343
+ return @max(widgetControlHeight(widget, tokens), widgetLineHeight(text_size) + inset * 2);
1344
+ }
1345
+
1346
+ /// Alert children start under the chrome-drawn title line and hang past
1347
+ /// the icon column (the standard callout grid: icon in column one, title and
1348
+ /// description stacked in column two). Text-less alerts keep the full
1349
+ /// content box.
1350
+ fn alertContentFrame(widget: Widget, content: geometry.RectF, tokens: DesignTokens) geometry.RectF {
1351
+ if (widget.kind != .alert or widget.text.len == 0) return content;
1352
+ const text_size = widgetBodyTextSize(widget, tokens);
1353
+ const icon_size = widgetSizedDensityValue(widget, tokens, 16);
1354
+ const text_gap = widgetControlInset(widget, tokens, tokens.spacing.md);
1355
+ const title_gap = widgetControlInset(widget, tokens, tokens.spacing.xs);
1356
+ const indent = @min(content.width, icon_size + text_gap);
1357
+ const y = @min(content.maxY(), content.y + widgetLineHeight(text_size) + title_gap);
1358
+ return geometry.RectF.init(
1359
+ content.x + indent,
1360
+ y,
1361
+ @max(0, content.width - indent),
1362
+ @max(0, content.maxY() - y),
1363
+ );
1364
+ }
1365
+
1366
+ pub fn intrinsicWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1367
+ return intrinsicWidgetSizeDepth(widget, tokens, 0);
1368
+ }
1369
+
1370
+ fn intrinsicWidgetSizeDepth(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1371
+ return switch (widget.kind) {
1372
+ .text => intrinsicTextWidgetSize(widget, tokens, widgetBodyTextSize(widget, tokens)),
1373
+ .icon => geometry.SizeF.init(intrinsicIconExtent(widget, tokens), intrinsicIconExtent(widget, tokens)),
1374
+ .avatar => intrinsicAvatarWidgetSize(widget, tokens),
1375
+ .badge => intrinsicBadgeWidgetSize(widget, tokens),
1376
+ .button, .toggle_button, .toggle => intrinsicButtonWidgetSize(widget, tokens),
1377
+ .icon_button => intrinsicSquareControlSize(widget, tokens),
1378
+ .select => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 200), widgetControlHeight(widget, tokens)),
1379
+ .input, .text_field => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 160), widgetControlHeight(widget, tokens)),
1380
+ .search_field, .combobox => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 200), widgetControlHeight(widget, tokens)),
1381
+ .textarea => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 200), widgetSizedDensityValue(widget, tokens, 80)),
1382
+ .tooltip => intrinsicPaddedTextWidgetSize(widget, tokens, widgetLabelTextSize(widget, tokens), widgetControlInset(widget, tokens, tokens.spacing.sm)),
1383
+ // Menu rows measure like list rows PLUS the trailing checkmark
1384
+ // slot every option reserves (committed or not, so commit moves
1385
+ // never reflow labels), on the menu's comfortable row band.
1386
+ .menu_item => blk: {
1387
+ const base = intrinsicRowTextWidgetSize(widget, tokens);
1388
+ const check_reserve = widget_metrics.widgetRowIconExtent(widget, tokens) + widget_metrics.widgetRowIconGap(widget, tokens);
1389
+ // The fractional check reserve can pull the total off the
1390
+ // snap grid again; ceil the final width so edge snapping
1391
+ // never shaves the label region (`pixelSnapCeil`).
1392
+ break :blk geometry.SizeF.init(pixelSnapCeil(tokens, base.width + check_reserve), widgetSizedDensityValue(widget, tokens, 32));
1393
+ },
1394
+ .list_item => blk: {
1395
+ const base = intrinsicRowTextWidgetSize(widget, tokens);
1396
+ if (widget.children.len == 0) break :blk base;
1397
+ const flow = intrinsicAxisChildrenSize(widget, tokens, .horizontal, depth);
1398
+ // A child flow only fractionally wider than the label-exact
1399
+ // base sits off the snap grid; ceil the winner so edge
1400
+ // snapping cannot land the row below its own label
1401
+ // (`pixelSnapCeil`).
1402
+ break :blk geometry.SizeF.init(pixelSnapCeil(tokens, @max(base.width, flow.width)), @max(base.height, flow.height));
1403
+ },
1404
+ // A span-carrying cell (markdown tables) measures like a padded
1405
+ // span paragraph; classic cells keep the single-line row metric.
1406
+ .data_cell => if (widget.spans.len > 0)
1407
+ paddedIntrinsicSize(widget, intrinsicTextWidgetSize(widget, tokens, widgetBodyTextSize(widget, tokens)))
1408
+ else
1409
+ intrinsicRowTextWidgetSize(widget, tokens),
1410
+ // Table rows sit taller than list rows: the comfortable row
1411
+ // band with tight cell padding, the reference table rhythm.
1412
+ .data_row => geometry.SizeF.init(0, widgetSizedDensityValue(widget, tokens, 36)),
1413
+ .status_bar => intrinsicStatusBarWidgetSize(widget, tokens),
1414
+ .segmented_control => intrinsicSegmentedControlSize(widget, tokens),
1415
+ .checkbox => intrinsicCheckboxWidgetSize(widget, tokens),
1416
+ .radio => intrinsicRadioWidgetSize(widget, tokens),
1417
+ .switch_control => intrinsicToggleWidgetSize(widget, tokens),
1418
+ .slider => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 160), @max(widgetSizedDensityValue(widget, tokens, 28), widgetSizedDensityValue(widget, tokens, 20))),
1419
+ // A 4px rail: the display-only bar is half the slider track so a
1420
+ // read-out never outweighs the control that edits the value.
1421
+ .progress => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 160), widgetSizedDensityValue(widget, tokens, 4)),
1422
+ .separator => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 160), controlStrokeWidth(widget, componentControlVisualTokens(widget, tokens), tokens.stroke.hairline)),
1423
+ .skeleton => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 120), widgetSizedDensityValue(widget, tokens, 20)),
1424
+ .spinner => intrinsicSpinnerWidgetSize(widget, tokens),
1425
+ // A plot has no natural content size; the default is a small
1426
+ // sparkline-friendly box, and definite `width`/`height` (or flex
1427
+ // grow) size real charts.
1428
+ .chart => geometry.SizeF.init(widgetSizedDensityValue(widget, tokens, 160), widgetSizedDensityValue(widget, tokens, 48)),
1429
+ .alert => intrinsicAlertWidgetSize(widget, tokens, depth),
1430
+ .card => intrinsicCardWidgetSize(widget, tokens, depth),
1431
+ .accordion => intrinsicAccordionWidgetSize(widget, tokens, depth),
1432
+ .dialog, .drawer, .sheet => intrinsicModalSurfaceWidgetSize(widget, tokens, depth),
1433
+ // Containers measure their children (matching the stacking axis the
1434
+ // layout pass uses), bounded by the widget depth cap. Scroll
1435
+ // viewports and virtualized containers stay zero: their content is
1436
+ // allowed to overflow the space they're given.
1437
+ .row, .breadcrumb, .button_group, .pagination, .radio_group, .tabs, .toggle_group => intrinsicAxisChildrenSize(widget, tokens, .horizontal, depth),
1438
+ .column, .menu_surface, .dropdown_menu, .input_group => intrinsicAxisChildrenSize(widget, tokens, .vertical, depth),
1439
+ .list, .data_grid, .table => if (widget.layout.virtualized)
1440
+ geometry.SizeF.zero()
1441
+ else
1442
+ intrinsicAxisChildrenSize(widget, tokens, .vertical, depth),
1443
+ .grid => if (widget.layout.virtualized)
1444
+ geometry.SizeF.zero()
1445
+ else
1446
+ intrinsicGridChildrenSize(widget, tokens, depth),
1447
+ .stack, .bubble, .resizable, .panel, .popover => intrinsicOverlayChildrenSize(widget, tokens, depth),
1448
+ .tree => intrinsicAxisChildrenSize(widget, tokens, .vertical, depth),
1449
+ // The divider band is thin along the row and cross-sized by the
1450
+ // panes it divides (like a bare separator in a row).
1451
+ .split_divider => geometry.SizeF.init(splitDividerExtent(widget), 0),
1452
+ // A split fills the space it is given (panes partition it);
1453
+ // like scroll viewports it reports no intrinsic size of its own.
1454
+ .scroll_view, .image, .split => geometry.SizeF.zero(),
1455
+ };
1456
+ }
1457
+
1458
+ fn intrinsicChildSize(child: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1459
+ const intrinsic = intrinsicWidgetSizeDepth(child, tokens, depth);
1460
+ return geometry.SizeF.init(
1461
+ clampIntrinsicAxis(@max(intrinsic.width, nonNegative(child.frame.width)), child.layout.min_size.width, child.layout.max_size.width),
1462
+ clampIntrinsicAxis(@max(intrinsic.height, nonNegative(child.frame.height)), child.layout.min_size.height, child.layout.max_size.height),
1463
+ );
1464
+ }
1465
+
1466
+ fn clampIntrinsicAxis(value: f32, min: f32, max: f32) f32 {
1467
+ return @max(min, boundedByMax(value, max));
1468
+ }
1469
+
1470
+ /// Child contribution to a flex container's intrinsic size. A bare
1471
+ /// separator inside a horizontal container is a divider: it contributes
1472
+ /// its stroke thickness on both axes (thin along the row, cross-sized by
1473
+ /// the siblings it divides) instead of its default horizontal-rule
1474
+ /// length. Vertical containers keep the classic contribution.
1475
+ fn intrinsicChildSizeInAxis(child: Widget, tokens: DesignTokens, depth: usize, axis: LayoutAxis) geometry.SizeF {
1476
+ const size = intrinsicChildSize(child, tokens, depth);
1477
+ if (child.kind == .separator and axis == .horizontal and child.frame.width <= 0) {
1478
+ const thin = @min(size.width, size.height);
1479
+ return geometry.SizeF.init(
1480
+ @max(nonNegative(child.layout.min_size.width), thin),
1481
+ @max(nonNegative(child.layout.min_size.height), thin),
1482
+ );
1483
+ }
1484
+ return size;
1485
+ }
1486
+
1487
+ fn intrinsicAxisChildrenSize(widget: Widget, tokens: DesignTokens, axis: LayoutAxis, depth: usize) geometry.SizeF {
1488
+ if (depth >= max_widget_depth or widget.children.len == 0) return intrinsicOwnMinSize(widget);
1489
+ var flow_count: usize = 0;
1490
+ var main_sum: f32 = 0;
1491
+ var cross_max: f32 = 0;
1492
+ for (widget.children) |child| {
1493
+ // Anchored floating children never grow their parent.
1494
+ if (child.layout.anchor != null) continue;
1495
+ flow_count += 1;
1496
+ const size = intrinsicChildSizeInAxis(child, tokens, depth + 1, axis);
1497
+ switch (axis) {
1498
+ .horizontal => {
1499
+ main_sum += size.width;
1500
+ cross_max = @max(cross_max, size.height);
1501
+ },
1502
+ .vertical => {
1503
+ main_sum += size.height;
1504
+ cross_max = @max(cross_max, size.width);
1505
+ },
1506
+ }
1507
+ }
1508
+ if (flow_count == 0) return intrinsicOwnMinSize(widget);
1509
+ // Button groups and tab strips measure at their register's effective
1510
+ // gap (see `buttonGroupGap`/`tabsGap`), so intrinsic sizing agrees
1511
+ // with placement.
1512
+ const child_gap = switch (widget.kind) {
1513
+ .button_group => buttonGroupGap(widget, tokens),
1514
+ .tabs => tabsGap(widget, tokens),
1515
+ else => nonNegative(widget.layout.gap),
1516
+ };
1517
+ const gap = child_gap * @as(f32, @floatFromInt(flow_count - 1));
1518
+ return paddedIntrinsicSize(widget, switch (axis) {
1519
+ .horizontal => geometry.SizeF.init(main_sum + gap, cross_max),
1520
+ .vertical => geometry.SizeF.init(cross_max, main_sum + gap),
1521
+ });
1522
+ }
1523
+
1524
+ fn intrinsicOverlayChildrenSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1525
+ if (depth >= max_widget_depth or widget.children.len == 0) return intrinsicOwnMinSize(widget);
1526
+ var width_max: f32 = 0;
1527
+ var height_max: f32 = 0;
1528
+ for (widget.children) |child| {
1529
+ if (child.layout.anchor != null) continue;
1530
+ const size = intrinsicChildSize(child, tokens, depth + 1);
1531
+ width_max = @max(width_max, size.width);
1532
+ height_max = @max(height_max, size.height);
1533
+ }
1534
+ return paddedIntrinsicSize(widget, geometry.SizeF.init(width_max, height_max));
1535
+ }
1536
+
1537
+ fn intrinsicGridChildrenSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1538
+ if (depth >= max_widget_depth or widget.children.len == 0) return intrinsicOwnMinSize(widget);
1539
+ var cell_width: f32 = 0;
1540
+ var cell_height: f32 = 0;
1541
+ for (widget.children) |child| {
1542
+ if (child.layout.anchor != null) continue;
1543
+ const size = intrinsicChildSize(child, tokens, depth + 1);
1544
+ cell_width = @max(cell_width, size.width);
1545
+ cell_height = @max(cell_height, size.height);
1546
+ }
1547
+ const columns = gridColumnCount(widget.children.len, widget.layout.columns);
1548
+ const rows = (widget.children.len + columns - 1) / columns;
1549
+ const gap = nonNegative(widget.layout.gap);
1550
+ return paddedIntrinsicSize(widget, geometry.SizeF.init(
1551
+ cell_width * @as(f32, @floatFromInt(columns)) + gap * @as(f32, @floatFromInt(columns - 1)),
1552
+ cell_height * @as(f32, @floatFromInt(rows)) + gap * @as(f32, @floatFromInt(rows - 1)),
1553
+ ));
1554
+ }
1555
+
1556
+ fn intrinsicOwnMinSize(widget: Widget) geometry.SizeF {
1557
+ return geometry.SizeF.init(nonNegative(widget.layout.min_size.width), nonNegative(widget.layout.min_size.height));
1558
+ }
1559
+
1560
+ fn paddedIntrinsicSize(widget: Widget, content: geometry.SizeF) geometry.SizeF {
1561
+ const padding = widget.layout.padding;
1562
+ return geometry.SizeF.init(
1563
+ @max(content.width + padding.left + padding.right, widget.layout.min_size.width),
1564
+ @max(content.height + padding.top + padding.bottom, widget.layout.min_size.height),
1565
+ );
1566
+ }
1567
+
1568
+ fn intrinsicTextWidgetSize(widget: Widget, tokens: DesignTokens, text_size: f32) geometry.SizeF {
1569
+ if (widgetIsSpanParagraph(widget)) {
1570
+ const options = widgetTextSpanLayoutOptions(widget, tokens, 0);
1571
+ return geometry.SizeF.init(
1572
+ text_spans_model.textSpansIntrinsicWidth(widget.spans, options),
1573
+ widgetLineHeight(text_size * text_spans_model.textSpansMaxScale(widget.spans)),
1574
+ );
1575
+ }
1576
+ return geometry.SizeF.init(
1577
+ measuredTextWidth(tokens, widget.text, text_size),
1578
+ widgetLineHeight(text_size),
1579
+ );
1580
+ }
1581
+
1582
+ fn intrinsicPaddedTextWidgetSize(widget: Widget, tokens: DesignTokens, text_size: f32, inset: f32) geometry.SizeF {
1583
+ const text = intrinsicTextWidgetSize(widget, tokens, text_size);
1584
+ // Ceil to the snap grid (`pixelSnapCeil`): the tooltip capsule hugs
1585
+ // its measured label exactly, so render-time edge snapping must not
1586
+ // shave it into eliding.
1587
+ return geometry.SizeF.init(pixelSnapCeil(tokens, text.width + inset * 2), @max(widgetControlHeight(widget, tokens), text.height + widgetSizedDensityValue(widget, tokens, 8)));
1588
+ }
1589
+
1590
+ fn intrinsicStatusBarWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1591
+ const text_size = widgetBodyTextSize(widget, tokens);
1592
+ const text = intrinsicTextWidgetSize(widget, tokens, text_size);
1593
+ const padding = widgetStatusBarPadding(widget);
1594
+ // Ceil to the snap grid (`pixelSnapCeil`): a hug-sized status bar
1595
+ // is label-exact and its text elides at the frame edge, the same
1596
+ // cliff every measured-label chip rides.
1597
+ return geometry.SizeF.init(pixelSnapCeil(tokens, text.width + padding.horizontal()), @max(widgetSizedDensityValue(widget, tokens, 32), text.height + padding.vertical()));
1598
+ }
1599
+
1600
+ fn intrinsicAlertWidgetSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1601
+ const text_size = widgetBodyTextSize(widget, tokens);
1602
+ const inset = widgetControlInset(widget, tokens, tokens.spacing.lg);
1603
+ // The chrome's fixed 16px icon (`emitAlertWidgetChrome`).
1604
+ const icon_size = widgetSizedDensityValue(widget, tokens, 16);
1605
+ const text_gap = widgetControlInset(widget, tokens, tokens.spacing.md);
1606
+ const text = intrinsicTextWidgetSize(widget, tokens, text_size);
1607
+ var size = geometry.SizeF.init(
1608
+ @max(widgetSizedDensityValue(widget, tokens, 240), text.width + inset * 2 + icon_size + text_gap),
1609
+ @max(widgetSizedDensityValue(widget, tokens, 52), widgetLineHeight(text_size) + inset * 2),
1610
+ );
1611
+ // A description column under the title (`alertContentFrame`) grows
1612
+ // the alert instead of overflowing it.
1613
+ const children = intrinsicStackedChildrenSize(widget, tokens, depth);
1614
+ if (children.height > 0 and widget.text.len > 0) {
1615
+ const title_gap = widgetControlInset(widget, tokens, tokens.spacing.xs);
1616
+ size.height = @max(size.height, widgetLineHeight(text_size) + title_gap + children.height + inset * 2);
1617
+ size.width = @max(size.width, children.width + icon_size + text_gap + inset * 2);
1618
+ } else if (children.height > 0) {
1619
+ size.height = @max(size.height, children.height + inset * 2);
1620
+ size.width = @max(size.width, children.width + inset * 2);
1621
+ }
1622
+ return size;
1623
+ }
1624
+
1625
+ /// The overlay max of a stacking surface's flow children, WITHOUT the
1626
+ /// widget's own padding or min-size floors (callers fold those in).
1627
+ fn intrinsicStackedChildrenSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1628
+ if (depth >= max_widget_depth) return geometry.SizeF.zero();
1629
+ var width_max: f32 = 0;
1630
+ var height_max: f32 = 0;
1631
+ for (widget.children) |child| {
1632
+ if (child.layout.anchor != null) continue;
1633
+ const size = intrinsicChildSize(child, tokens, depth + 1);
1634
+ width_max = @max(width_max, size.width);
1635
+ height_max = @max(height_max, size.height);
1636
+ }
1637
+ return geometry.SizeF.init(width_max, height_max);
1638
+ }
1639
+
1640
+ fn intrinsicAccordionWidgetSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1641
+ // Header band always; expanded content (plus the header-to-content
1642
+ // gap the accordion consumes) only while disclosed — so accordion
1643
+ // items size themselves and a toggle reflows the column around them.
1644
+ const header_height = accordionHeaderHeight(widget, tokens);
1645
+ var content = geometry.SizeF.zero();
1646
+ if (accordionChildrenVisible(widget)) {
1647
+ content = intrinsicStackedChildrenSize(widget, tokens, depth);
1648
+ }
1649
+ const gap = if (content.height > 0) nonNegative(widget.layout.gap) else 0;
1650
+ return paddedIntrinsicSize(widget, geometry.SizeF.init(content.width, header_height + gap + content.height));
1651
+ }
1652
+
1653
+ fn intrinsicCardWidgetSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1654
+ const title_size = widgetTypographySize(widget, tokens.typography.body_size + 1);
1655
+ const inset = widgetControlInset(widget, tokens, tokens.spacing.lg);
1656
+ const text = intrinsicTextWidgetSize(widget, tokens, title_size);
1657
+ var size = geometry.SizeF.init(
1658
+ @max(widgetSizedDensityValue(widget, tokens, 240), text.width + inset * 2),
1659
+ @max(widgetSizedDensityValue(widget, tokens, 120), if (widget.text.len > 0) widgetLineHeight(title_size) + inset * 2 else 0),
1660
+ );
1661
+ // Content-bearing cards grow around their children plus the card's
1662
+ // own padding (the default 24px house inset) instead of clipping
1663
+ // them against the 120pt floor.
1664
+ const children = intrinsicStackedChildrenSize(widget, tokens, depth);
1665
+ if (children.height > 0) {
1666
+ const padding = widget.layout.padding;
1667
+ size.height = @max(size.height, children.height + padding.top + padding.bottom);
1668
+ size.width = @max(size.width, children.width + padding.left + padding.right);
1669
+ }
1670
+ return size;
1671
+ }
1672
+
1673
+ fn intrinsicModalSurfaceWidgetSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
1674
+ const title_size = widgetTypographySize(widget, tokens.typography.title_size);
1675
+ const inset = widgetControlInset(widget, tokens, tokens.spacing.xl);
1676
+ const text = intrinsicTextWidgetSize(widget, tokens, title_size);
1677
+ const default_size = switch (widget.kind) {
1678
+ .drawer => geometry.SizeF.init(360, 280),
1679
+ .sheet => geometry.SizeF.init(320, 420),
1680
+ else => geometry.SizeF.init(420, 220),
1681
+ };
1682
+ var size = geometry.SizeF.init(
1683
+ @max(widgetSizedDensityValue(widget, tokens, default_size.width), text.width + inset * 2),
1684
+ @max(widgetSizedDensityValue(widget, tokens, default_size.height), if (widget.text.len > 0) widgetLineHeight(title_size) + inset * 2 else 0),
1685
+ );
1686
+ // Content-bearing modal surfaces hug their children plus their own
1687
+ // padding, like cards. The fixed default height is a placeholder for
1688
+ // childless surfaces, NOT a floor: content shorter than the default
1689
+ // would otherwise leave the surplus stacked below the last child, so
1690
+ // a dialog's inset under its button row breaks symmetry with the
1691
+ // insets its padding declares on the other three sides.
1692
+ const children = intrinsicStackedChildrenSize(widget, tokens, depth);
1693
+ if (children.height > 0) {
1694
+ const padding = widget.layout.padding;
1695
+ size.height = children.height + padding.top + padding.bottom;
1696
+ if (widget.text.len > 0) size.height = @max(size.height, widgetLineHeight(title_size) + inset * 2);
1697
+ size.width = @max(size.width, children.width + padding.left + padding.right);
1698
+ }
1699
+ return size;
1700
+ }
1701
+
1702
+ fn intrinsicButtonWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1703
+ const height = widgetControlHeight(widget, tokens);
1704
+ if (widget.size == .icon) return geometry.SizeF.init(height, height);
1705
+ // An inline icon (`widget.icon`) widens the button: icon + gap before
1706
+ // the label, or a square control when the label is empty. The extent
1707
+ // and gap are the shared render metrics, so measured width matches
1708
+ // painted pixels.
1709
+ if (widget.icon.len > 0 and widget.text.len == 0) return geometry.SizeF.init(height, height);
1710
+ const icon_width = if (widget.icon.len > 0)
1711
+ widget_metrics.widgetButtonIconExtent(widget, tokens) + widget_metrics.widgetButtonIconGap(widget, tokens)
1712
+ else
1713
+ 0;
1714
+ // Measured with the button-label face (not the body face) so the
1715
+ // medium advances the render draws are the widths layout reserves.
1716
+ const text_width = measureTextWidthForFont(tokens.text_measure, tokens.typography.buttonFontId(), widget.text, widgetButtonTextSize(widget, tokens));
1717
+ // Ceil to the snap grid (`pixelSnapCeil`): a button or toggle chip
1718
+ // sized exactly to its measured label ("PID" in a sort-chip row)
1719
+ // must not lose a fraction of a pixel to render-time edge snapping
1720
+ // and elide its own label.
1721
+ const width = pixelSnapCeil(tokens, @max(widgetSizedDensityValue(widget, tokens, 44), icon_width + text_width + widgetButtonInset(widget, tokens) * 2));
1722
+ return geometry.SizeF.init(width, height);
1723
+ }
1724
+
1725
+ fn intrinsicAvatarWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1726
+ const size = widgetSizedDensityValue(widget, tokens, 40);
1727
+ return geometry.SizeF.init(size, size);
1728
+ }
1729
+
1730
+ fn intrinsicBadgeWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1731
+ const min_width = widgetSizedDensityValue(widget, tokens, 24);
1732
+ const height = widgetSizedDensityValue(widget, tokens, 20);
1733
+ // An ICON-ONLY badge carries no text insets: the renderer centers
1734
+ // the glyph, so it takes the same minimum chip width a single-digit
1735
+ // badge gets — a timeline's completed check and its numbered
1736
+ // neighbors stay the same capsule instead of the icon one widening
1737
+ // by the text insets.
1738
+ if (widget.icon.len > 0 and widget.text.len == 0) {
1739
+ const icon_extent = widget_metrics.widgetBadgeIconExtent(widget, tokens);
1740
+ return geometry.SizeF.init(@max(min_width, icon_extent + tokens.spacing.xs * 2), height);
1741
+ }
1742
+ const text_width = measuredTextWidth(tokens, widget.text, widget_metrics.widgetBadgeTextSize(widget, tokens));
1743
+ const inset = widgetControlInset(widget, tokens, tokens.spacing.sm);
1744
+ // An inline icon widens the badge by the same shared metrics
1745
+ // the renderer paints with (gap only when a label follows).
1746
+ const icon_width = if (widget.icon.len > 0)
1747
+ widget_metrics.widgetBadgeIconExtent(widget, tokens) + widget_metrics.widgetBadgeIconGap(widget, tokens)
1748
+ else
1749
+ 0;
1750
+ // The compact chip: a 20px band (the reference badge height) with
1751
+ // the tight 8px side insets. Under geometry pixel snapping the
1752
+ // fractional exact-fit width rides a cliff: render snaps the chip's
1753
+ // frame edges to the pixel grid, which can shave up to one snap
1754
+ // step off the box the label was measured to fill exactly — and the
1755
+ // elision pass then swaps real glyphs for an ellipsis. Rounding the
1756
+ // intrinsic width UP to the snap grid keeps the snapped chip at
1757
+ // least label-wide; themes without geometry snapping keep the exact
1758
+ // measurement, bit-identical to before.
1759
+ return geometry.SizeF.init(pixelSnapCeil(tokens, @max(min_width, icon_width + text_width + inset * 2)), height);
1760
+ }
1761
+
1762
+ /// True exactly when the renderer's geometry snapping is live — the
1763
+ /// same guard its rect snapping applies (geometry on, usable scale) —
1764
+ /// so intrinsic sizing and painting agree about whether edges move.
1765
+ fn pixelSnapGeometryActive(tokens: DesignTokens) bool {
1766
+ return tokens.pixel_snap.geometry and std.math.isFinite(tokens.pixel_snap.scale) and tokens.pixel_snap.scale > 0;
1767
+ }
1768
+
1769
+ /// Round a length UP to the pixel-snap grid when geometry snapping is
1770
+ /// on (no-op otherwise): intrinsic boxes sized from measured text must
1771
+ /// survive the renderer's edge snapping without losing content width.
1772
+ /// A width on the snap grid is preserved exactly by edge snapping at
1773
+ /// ANY position (both edges move by the same rounding), so the snapped
1774
+ /// box is never narrower than the label it was measured for.
1775
+ fn pixelSnapCeil(tokens: DesignTokens, value: f32) f32 {
1776
+ if (!pixelSnapGeometryActive(tokens)) return value;
1777
+ const scale = tokens.pixel_snap.scale;
1778
+ return @ceil(value * scale) / scale;
1779
+ }
1780
+
1781
+ fn intrinsicSquareControlSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1782
+ const height = widgetControlHeight(widget, tokens);
1783
+ return geometry.SizeF.init(height, height);
1784
+ }
1785
+
1786
+ /// The spinner is an inline activity glyph, not a control box: it sizes
1787
+ /// to the icon register — 16 (sm) / 20 (default) / 24 (lg), density
1788
+ /// scaled — so it sits flush in a row of compact controls instead of
1789
+ /// claiming a 36px control square.
1790
+ fn intrinsicSpinnerWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1791
+ const base: f32 = switch (widget.size) {
1792
+ .sm => 16,
1793
+ // heading/display are text-leaf typography rungs; the spinner
1794
+ // glyph sits at the default register.
1795
+ .default, .icon, .heading, .display => 20,
1796
+ .lg => 24,
1797
+ };
1798
+ const extent = densityValue(tokens, base);
1799
+ return geometry.SizeF.init(extent, extent);
1800
+ }
1801
+
1802
+ fn intrinsicSegmentedControlSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1803
+ const text_width = measuredTextWidth(tokens, widget.text, widgetLabelTextSize(widget, tokens));
1804
+ // Ceil to the snap grid (`pixelSnapCeil`): a segment / tabs trigger
1805
+ // sized exactly to its measured label must survive render-time edge
1806
+ // snapping without eliding.
1807
+ const width = pixelSnapCeil(tokens, @max(widgetSizedDensityValue(widget, tokens, 44), text_width + widgetControlInset(widget, tokens, tokens.spacing.md) * 2));
1808
+ return geometry.SizeF.init(width, widgetControlHeight(widget, tokens));
1809
+ }
1810
+
1811
+ fn intrinsicRowTextWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1812
+ const text_size = widgetBodyTextSize(widget, tokens);
1813
+ const inset = widgetControlInset(widget, tokens, tokens.spacing.md);
1814
+ const text_width = measuredTextWidth(tokens, widget.text, text_size);
1815
+ // A leading icon widens the row by the same shared metrics the
1816
+ // renderer paints with, so measured widths and pixels agree.
1817
+ const icon_width = if (widget.icon.len > 0)
1818
+ widget_metrics.widgetRowIconExtent(widget, tokens) + widget_metrics.widgetRowIconGap(widget, tokens)
1819
+ else
1820
+ 0;
1821
+ // Ceil to the snap grid (`pixelSnapCeil`): list rows and table
1822
+ // cells hug their measured label exactly, so render-time edge
1823
+ // snapping must not shave the label below its own width and elide.
1824
+ return geometry.SizeF.init(pixelSnapCeil(tokens, icon_width + text_width + inset * 2), widgetDefaultRowHeight(widget, tokens));
1825
+ }
1826
+
1827
+ fn intrinsicCheckboxWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1828
+ const box_size = widgetSizedDensityValue(widget, tokens, 18);
1829
+ const label_size = widgetLabelTextSize(widget, tokens);
1830
+ const label_width = measuredTextWidth(tokens, widget.text, label_size);
1831
+ const gap = if (widget.text.len > 0) widgetControlInset(widget, tokens, tokens.spacing.sm) else 0;
1832
+ // Ceil to the snap grid (`pixelSnapCeil`): the label tail after the
1833
+ // box is measured exactly, and render-time edge snapping must not
1834
+ // shave it into eliding.
1835
+ return geometry.SizeF.init(pixelSnapCeil(tokens, box_size + gap + label_width), @max(box_size, widgetLineHeight(label_size)));
1836
+ }
1837
+
1838
+ fn intrinsicRadioWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1839
+ const circle_size = widgetSizedDensityValue(widget, tokens, 18);
1840
+ const label_size = widgetLabelTextSize(widget, tokens);
1841
+ const label_width = measuredTextWidth(tokens, widget.text, label_size);
1842
+ const gap = if (widget.text.len > 0) widgetControlInset(widget, tokens, tokens.spacing.sm) else 0;
1843
+ // Same label-exact tail as the checkbox: ceil so snapping cannot
1844
+ // elide the label (`pixelSnapCeil`).
1845
+ return geometry.SizeF.init(pixelSnapCeil(tokens, circle_size + gap + label_width), @max(circle_size, widgetLineHeight(label_size)));
1846
+ }
1847
+
1848
+ fn intrinsicToggleWidgetSize(widget: Widget, tokens: DesignTokens) geometry.SizeF {
1849
+ // Matches the rendered 44x24 switch track.
1850
+ const track_width = widgetSizedDensityValue(widget, tokens, 44);
1851
+ const track_height = widgetSizedDensityValue(widget, tokens, 24);
1852
+ const label_size = widgetLabelTextSize(widget, tokens);
1853
+ const label_width = measuredTextWidth(tokens, widget.text, label_size);
1854
+ const gap = if (widget.text.len > 0) widgetControlInset(widget, tokens, tokens.spacing.sm) else 0;
1855
+ const height = @max(track_height, widgetLineHeight(label_size));
1856
+ // The renderer widens the track to 1.75x a tall row's height and
1857
+ // snaps the track RECT itself to the pixel grid, where nearest-
1858
+ // rounding can grow it past a fractional reserve (a 38.5px sm track
1859
+ // paints 39px wide at scale 1) and push the label into eliding.
1860
+ // With snapping live, reserve the ceiled render extent and ceil the
1861
+ // total like every measured-label chip; non-snapping themes keep
1862
+ // the classic reserve bit-identically.
1863
+ const track_reserve = if (pixelSnapGeometryActive(tokens))
1864
+ pixelSnapCeil(tokens, @max(track_width, height * 1.75))
1865
+ else
1866
+ track_width;
1867
+ return geometry.SizeF.init(pixelSnapCeil(tokens, track_reserve + gap + label_width), height);
1868
+ }
1869
+
1870
+ fn intrinsicIconExtent(widget: Widget, tokens: DesignTokens) f32 {
1871
+ return widgetSizedDensityValue(widget, tokens, 18);
1872
+ }
1873
+
1874
+ /// The chat bubble's thread fraction: a bubble with no explicit width
1875
+ /// caps at 80% of the content width its container offers — the measured
1876
+ /// reference treatment — so one long message wraps into a readable
1877
+ /// column instead of spanning the whole thread. Ghost bubbles are
1878
+ /// exempt (the reference lets the chrome-less variant run full width:
1879
+ /// its content carries the message).
1880
+ pub const bubble_max_width_fraction: f32 = 0.8;
1881
+
1882
+ /// Cap a bubble child's inline extent at the thread fraction. `value`
1883
+ /// is the width the container's math chose, `available` the container's
1884
+ /// content width. Only hug-sized bubbles participate: an explicit
1885
+ /// `width` (definite through the frame channel), an author `max_size`,
1886
+ /// or a ghost variant leaves the classic result untouched, and a
1887
+ /// container that offers no definite space (hug measurement) cannot
1888
+ /// name a fraction of it. An author min-width floor still wins over the
1889
+ /// cap, the same precedence `clampIntrinsicAxis` keeps everywhere.
1890
+ /// The row main-axis seam of the bubble thread cap: the main extent of
1891
+ /// a horizontal container's child is its width, so hug-sized bubbles
1892
+ /// cap at the thread fraction there. Vertical containers pass through —
1893
+ /// their main axis is height; the cross-extent seam in
1894
+ /// `preferredCrossExtent` owns the width there.
1895
+ fn mainExtentWithBubbleCap(child: Widget, axis: LayoutAxis, available: f32, value: f32) f32 {
1896
+ if (axis != .horizontal) return value;
1897
+ return bubbleThreadWidthCap(child, available, value);
1898
+ }
1899
+
1900
+ fn bubbleThreadCapEligible(child: Widget) bool {
1901
+ if (child.kind != .bubble or child.variant == .ghost) return false;
1902
+ return child.frame.width <= 0 and child.layout.max_size.width <= 0;
1903
+ }
1904
+
1905
+ fn bubbleThreadWidthCap(child: Widget, available: f32, value: f32) f32 {
1906
+ if (!bubbleThreadCapEligible(child) or available <= 0) return value;
1907
+ return @min(value, @max(available * bubble_max_width_fraction, nonNegative(child.layout.min_size.width)));
1908
+ }
1909
+
1910
+ fn preferredMainExtent(widget: Widget, axis: LayoutAxis, tokens: DesignTokens) f32 {
1911
+ const value = switch (axis) {
1912
+ .horizontal => widget.frame.width,
1913
+ .vertical => widget.frame.height,
1914
+ };
1915
+ return clampMainExtent(widget, axis, if (value > 0) value else intrinsicMainExtent(widget, axis, tokens));
1916
+ }
1917
+
1918
+ fn preferredCrossExtent(widget: Widget, axis: LayoutAxis, available: f32, alignment: WidgetCrossAlignment, tokens: DesignTokens) f32 {
1919
+ const value = switch (axis) {
1920
+ .horizontal => widget.frame.height,
1921
+ .vertical => widget.frame.width,
1922
+ };
1923
+ const min_value = switch (axis) {
1924
+ .horizontal => widget.layout.min_size.height,
1925
+ .vertical => widget.layout.min_size.width,
1926
+ };
1927
+ const max_value = switch (axis) {
1928
+ .horizontal => widget.layout.max_size.height,
1929
+ .vertical => widget.layout.max_size.width,
1930
+ };
1931
+ if (value > 0) return @max(min_value, boundedByMax(value, max_value));
1932
+ // The cross axis of a vertical container is the child's WIDTH, so
1933
+ // the bubble thread contract applies here: a bubble directly in a
1934
+ // column HUGS its message up to the thread fraction — even under
1935
+ // the default stretch alignment, because a stretched message
1936
+ // surface is exactly the shape the reference bubble never takes
1937
+ // (fit-content, capped at 80% of the thread).
1938
+ if (axis == .vertical and bubbleThreadCapEligible(widget) and available > 0) {
1939
+ const fitted = @min(available, intrinsicCrossExtent(widget, axis, tokens));
1940
+ return @max(min_value, boundedByMax(bubbleThreadWidthCap(widget, available, fitted), max_value));
1941
+ }
1942
+ if (alignment == .stretch) return @max(min_value, boundedByMax(available, max_value));
1943
+ const intrinsic = intrinsicCrossExtent(widget, axis, tokens);
1944
+ // A cross-CENTERED child of a horizontal container keeps its
1945
+ // intrinsic HEIGHT even past the band, so the centering rule above
1946
+ // (`alignedCrossAxisOrigin`) can split the overflow across both
1947
+ // edges — clamping here handed an oversized stack a band-sized
1948
+ // frame whose inner flow then spilled past the bottom edge only
1949
+ // (top-heavy rows: a two-line text stack inside a fixed-height list
1950
+ // row). Explicit heights already pass through unclamped (the
1951
+ // `value` branch above); this extends the same honesty to intrinsic
1952
+ // ones. The WIDTH cross axis (vertical containers) keeps the clamp:
1953
+ // the offered width is what drives text wrap, and an unclamped
1954
+ // width would un-wrap centered paragraphs.
1955
+ if (axis == .horizontal and alignment == .center) {
1956
+ return @max(min_value, boundedByMax(intrinsic, max_value));
1957
+ }
1958
+ return @max(min_value, boundedByMax(@min(available, intrinsic), max_value));
1959
+ }
1960
+
1961
+ fn minMainExtent(widget: Widget, axis: LayoutAxis) f32 {
1962
+ return switch (axis) {
1963
+ .horizontal => nonNegative(widget.layout.min_size.width),
1964
+ .vertical => nonNegative(widget.layout.min_size.height),
1965
+ };
1966
+ }
1967
+
1968
+ fn intrinsicMainExtent(widget: Widget, axis: LayoutAxis, tokens: DesignTokens) f32 {
1969
+ const size = orientedIntrinsicWidgetSize(widget, tokens, axis);
1970
+ return switch (axis) {
1971
+ .horizontal => size.width,
1972
+ .vertical => size.height,
1973
+ };
1974
+ }
1975
+
1976
+ fn intrinsicCrossExtent(widget: Widget, axis: LayoutAxis, tokens: DesignTokens) f32 {
1977
+ const size = orientedIntrinsicWidgetSize(widget, tokens, axis);
1978
+ return switch (axis) {
1979
+ .horizontal => size.height,
1980
+ .vertical => size.width,
1981
+ };
1982
+ }
1983
+
1984
+ /// Axis-aware intrinsic size. A separator's intrinsic size is authored as
1985
+ /// a horizontal rule (default length x stroke width); inside a horizontal
1986
+ /// container the rule runs vertically, so the components swap — the
1987
+ /// separator stays hairline-thin along the row's main axis (a pane
1988
+ /// divider) instead of eating its default length from the row. Explicit
1989
+ /// `width`/`frame` values still win through the min/frame channels.
1990
+ fn orientedIntrinsicWidgetSize(widget: Widget, tokens: DesignTokens, axis: LayoutAxis) geometry.SizeF {
1991
+ const size = intrinsicWidgetSize(widget, tokens);
1992
+ if (widget.kind == .separator and axis == .horizontal) {
1993
+ return geometry.SizeF.init(size.height, size.width);
1994
+ }
1995
+ return size;
1996
+ }
1997
+
1998
+ pub fn virtualWidgetScrollContentExtent(widget: Widget, viewport_extent: f32) f32 {
1999
+ return virtualWidgetScrollContentExtentWithTokens(widget, viewport_extent, .{});
2000
+ }
2001
+
2002
+ pub fn virtualWidgetScrollContentExtentWithTokens(widget: Widget, viewport_extent: f32, tokens: DesignTokens) f32 {
2003
+ // A variable-extent windowed virtual list DECLARES its content
2004
+ // extent: the window's offset table already summed estimates plus
2005
+ // measured corrections plus gaps, and stamping it here is what
2006
+ // keeps the engine scrollbar, the scroll semantics, and the native
2007
+ // driver's content size telling the same (converging) truth.
2008
+ if (widget.layout.virtual_total_extent > 0 and widget.layout.virtual_item_count > 0) {
2009
+ return widget.layout.virtual_total_extent;
2010
+ }
2011
+ const item_count = virtualWidgetScrollItemCount(widget);
2012
+ if (item_count == 0) return 0;
2013
+ const item_extent = if (widget.layout.virtual_item_extent > 0)
2014
+ widget.layout.virtual_item_extent
2015
+ else if (widget.kind == .grid and widget.children.len > 0)
2016
+ preferredGridRowExtent(widget.children, gridColumnCount(widget.children.len, widget.layout.columns), tokens)
2017
+ else if (widget.children.len > 0)
2018
+ preferredMainExtent(widget.children[0], .vertical, tokens)
2019
+ else
2020
+ return 0;
2021
+ return virtualListRange(.{
2022
+ .item_count = item_count,
2023
+ .item_extent = item_extent,
2024
+ .item_gap = widget.layout.gap,
2025
+ .viewport_extent = viewport_extent,
2026
+ .scroll_offset = widget.value,
2027
+ }).content_extent;
2028
+ }
2029
+
2030
+ fn virtualWidgetScrollItemCount(widget: Widget) usize {
2031
+ // A windowed virtual list's extent comes from its DECLARED count:
2032
+ // children hold only the built window, so counting them would
2033
+ // collapse the scrollbar (and the native driver's content size) to
2034
+ // the window.
2035
+ if (widget.layout.virtual_item_count > 0) {
2036
+ return @max(widget.layout.virtual_item_count, widget.layout.virtual_first_index + widget.children.len);
2037
+ }
2038
+ if (widget.kind == .grid and widget.children.len > 0) {
2039
+ const columns = gridColumnCount(widget.children.len, widget.layout.columns);
2040
+ return gridRowCount(widget.children.len, columns);
2041
+ }
2042
+ if (widget.children.len > 0) return widget.children.len;
2043
+ if (widget.semantics.list_item_count) |count| return @intCast(count);
2044
+ return 0;
2045
+ }
2046
+
2047
+ fn widgetWithFrame(widget: Widget, frame: geometry.RectF) Widget {
2048
+ var copy = widget;
2049
+ copy.frame = frame;
2050
+ return copy;
2051
+ }
2052
+
2053
+ fn nonNegative(value: f32) f32 {
2054
+ return @max(0, value);
2055
+ }