@native-sdk/cli 0.0.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +50 -1
- package/app.zon +30 -0
- package/assets/icon.icns +0 -0
- package/assets/icon.ico +0 -0
- package/assets/icon.png +0 -0
- package/assets/icon.svg +15 -0
- package/assets/native-sdk.entitlements +10 -0
- package/assets/native-sdk.manifest +12 -0
- package/bin/native.js +156 -0
- package/build/app.zig +716 -0
- package/build.zig +2593 -0
- package/build.zig.zon +24 -0
- package/native-sdk.d.ts +615 -0
- package/package.json +45 -8
- package/skill-data/automation/SKILL.md +248 -0
- package/skill-data/core/SKILL.md +249 -0
- package/skill-data/core/references/app-model-runtime.md +141 -0
- package/skill-data/core/references/bridge-security-native-capabilities.md +230 -0
- package/skill-data/core/references/frontend-assets.md +124 -0
- package/skill-data/core/references/project-anatomy.md +108 -0
- package/skill-data/core/references/web-engines-packaging-debugging.md +199 -0
- package/skill-data/native-ui/SKILL.md +1080 -0
- package/skills/native-sdk/SKILL.md +33 -0
- package/src/app_runner/root.zig +740 -0
- package/src/assets/root.zig +20 -0
- package/src/automation/protocol.zig +285 -0
- package/src/automation/root.zig +13 -0
- package/src/automation/server.zig +506 -0
- package/src/automation/snapshot.zig +1202 -0
- package/src/automation/watcher.zig +165 -0
- package/src/bridge/root.zig +507 -0
- package/src/debug/root.zig +246 -0
- package/src/embed/app_exports.zig +11 -0
- package/src/embed/c_api.zig +793 -0
- package/src/embed/c_exports.zig +10 -0
- package/src/embed/chrome.zig +168 -0
- package/src/embed/conversions.zig +359 -0
- package/src/embed/host.zig +960 -0
- package/src/embed/root.zig +81 -0
- package/src/embed/tests.zig +2552 -0
- package/src/embed/types.zig +382 -0
- package/src/embed/ui_host.zig +289 -0
- package/src/extensions/root.zig +197 -0
- package/src/frontend/root.zig +48 -0
- package/src/js/root.zig +65 -0
- package/src/platform/android/NativeSdkActivity.java +1370 -0
- package/src/platform/android/android_host.c +696 -0
- package/src/platform/android/files.zig +9 -0
- package/src/platform/android/native_sdk_app.h +237 -0
- package/src/platform/ios/files.zig +8 -0
- package/src/platform/ios/native_sdk_app.h +289 -0
- package/src/platform/ios/uikit_host.m +2739 -0
- package/src/platform/linux/cef_host.cpp +599 -0
- package/src/platform/linux/gtk_host.c +5362 -0
- package/src/platform/linux/gtk_host.h +265 -0
- package/src/platform/linux/root.zig +1519 -0
- package/src/platform/macos/appkit_host.h +604 -0
- package/src/platform/macos/appkit_host.m +10392 -0
- package/src/platform/macos/automation_host.h +14 -0
- package/src/platform/macos/automation_host.m +12 -0
- package/src/platform/macos/cef_host.mm +3028 -0
- package/src/platform/macos/root.zig +2260 -0
- package/src/platform/null_platform.zig +2232 -0
- package/src/platform/null_platform_tests.zig +670 -0
- package/src/platform/policy_values.zig +14 -0
- package/src/platform/root.zig +181 -0
- package/src/platform/types.zig +2681 -0
- package/src/platform/windows/cef_host.cpp +3 -0
- package/src/platform/windows/root.zig +1521 -0
- package/src/platform/windows/webview2_host.cpp +6230 -0
- package/src/primitives/app_dirs/root.zig +440 -0
- package/src/primitives/app_manifest/root.zig +109 -0
- package/src/primitives/app_manifest/tests.zig +758 -0
- package/src/primitives/app_manifest/types.zig +587 -0
- package/src/primitives/app_manifest/validation.zig +828 -0
- package/src/primitives/assets/root.zig +520 -0
- package/src/primitives/canvas/a11y_audit.zig +390 -0
- package/src/primitives/canvas/a11y_audit_tests.zig +225 -0
- package/src/primitives/canvas/app_icon.zig +1590 -0
- package/src/primitives/canvas/chart.zig +392 -0
- package/src/primitives/canvas/chart_tests.zig +751 -0
- package/src/primitives/canvas/commands.zig +446 -0
- package/src/primitives/canvas/drawing.zig +309 -0
- package/src/primitives/canvas/equality.zig +316 -0
- package/src/primitives/canvas/events.zig +696 -0
- package/src/primitives/canvas/font_coverage.zig +118 -0
- package/src/primitives/canvas/font_ttf.zig +510 -0
- package/src/primitives/canvas/font_ttf_tests.zig +280 -0
- package/src/primitives/canvas/fonts/Geist-Regular.ttf +0 -0
- package/src/primitives/canvas/fonts/GeistMono-Regular.ttf +0 -0
- package/src/primitives/canvas/frame.zig +679 -0
- package/src/primitives/canvas/frame_metrics.zig +462 -0
- package/src/primitives/canvas/gpu.zig +639 -0
- package/src/primitives/canvas/hash.zig +112 -0
- package/src/primitives/canvas/icons/alert.svg +1 -0
- package/src/primitives/canvas/icons/archive.svg +1 -0
- package/src/primitives/canvas/icons/arrow-down.svg +1 -0
- package/src/primitives/canvas/icons/arrow-right.svg +1 -0
- package/src/primitives/canvas/icons/arrow-up.svg +1 -0
- package/src/primitives/canvas/icons/check-circle.svg +1 -0
- package/src/primitives/canvas/icons/check.svg +1 -0
- package/src/primitives/canvas/icons/chevron-down.svg +1 -0
- package/src/primitives/canvas/icons/chevron-left.svg +1 -0
- package/src/primitives/canvas/icons/chevron-right.svg +1 -0
- package/src/primitives/canvas/icons/chevron-up.svg +1 -0
- package/src/primitives/canvas/icons/circle-dot.svg +1 -0
- package/src/primitives/canvas/icons/clock.svg +1 -0
- package/src/primitives/canvas/icons/copy.svg +1 -0
- package/src/primitives/canvas/icons/download.svg +1 -0
- package/src/primitives/canvas/icons/edit.svg +1 -0
- package/src/primitives/canvas/icons/ellipsis.svg +1 -0
- package/src/primitives/canvas/icons/external-link.svg +1 -0
- package/src/primitives/canvas/icons/eye.svg +1 -0
- package/src/primitives/canvas/icons/file-text.svg +1 -0
- package/src/primitives/canvas/icons/folder-open.svg +1 -0
- package/src/primitives/canvas/icons/folder.svg +1 -0
- package/src/primitives/canvas/icons/git-branch.svg +1 -0
- package/src/primitives/canvas/icons/git-merge.svg +1 -0
- package/src/primitives/canvas/icons/git-pull-request.svg +1 -0
- package/src/primitives/canvas/icons/info.svg +1 -0
- package/src/primitives/canvas/icons/menu.svg +1 -0
- package/src/primitives/canvas/icons/missing.svg +1 -0
- package/src/primitives/canvas/icons/moon.svg +1 -0
- package/src/primitives/canvas/icons/music.svg +1 -0
- package/src/primitives/canvas/icons/panel-left.svg +1 -0
- package/src/primitives/canvas/icons/panel-right.svg +1 -0
- package/src/primitives/canvas/icons/pause.svg +1 -0
- package/src/primitives/canvas/icons/play.svg +1 -0
- package/src/primitives/canvas/icons/plus.svg +1 -0
- package/src/primitives/canvas/icons/refresh-cw.svg +1 -0
- package/src/primitives/canvas/icons/repeat.svg +1 -0
- package/src/primitives/canvas/icons/save.svg +1 -0
- package/src/primitives/canvas/icons/search.svg +1 -0
- package/src/primitives/canvas/icons/send.svg +1 -0
- package/src/primitives/canvas/icons/settings.svg +1 -0
- package/src/primitives/canvas/icons/shuffle.svg +1 -0
- package/src/primitives/canvas/icons/skip-back.svg +1 -0
- package/src/primitives/canvas/icons/skip-forward.svg +1 -0
- package/src/primitives/canvas/icons/sun.svg +1 -0
- package/src/primitives/canvas/icons/terminal.svg +1 -0
- package/src/primitives/canvas/icons/trash.svg +1 -0
- package/src/primitives/canvas/icons/volume.svg +1 -0
- package/src/primitives/canvas/icons/wrench.svg +1 -0
- package/src/primitives/canvas/icons/x-circle.svg +1 -0
- package/src/primitives/canvas/icons/x.svg +1 -0
- package/src/primitives/canvas/icons.zig +249 -0
- package/src/primitives/canvas/layout_audit.zig +744 -0
- package/src/primitives/canvas/layout_audit_tests.zig +426 -0
- package/src/primitives/canvas/markdown.zig +1031 -0
- package/src/primitives/canvas/markdown_hostile_tests.zig +471 -0
- package/src/primitives/canvas/markdown_tests.zig +538 -0
- package/src/primitives/canvas/plan_key_index.zig +120 -0
- package/src/primitives/canvas/png.zig +355 -0
- package/src/primitives/canvas/reference.zig +1457 -0
- package/src/primitives/canvas/reference_blur.zig +104 -0
- package/src/primitives/canvas/reference_memo.zig +322 -0
- package/src/primitives/canvas/reference_paths.zig +184 -0
- package/src/primitives/canvas/reference_tests.zig +1650 -0
- package/src/primitives/canvas/render.zig +747 -0
- package/src/primitives/canvas/render_effects.zig +255 -0
- package/src/primitives/canvas/render_fingerprints.zig +256 -0
- package/src/primitives/canvas/render_generic_resources.zig +376 -0
- package/src/primitives/canvas/render_images.zig +247 -0
- package/src/primitives/canvas/render_layers.zig +260 -0
- package/src/primitives/canvas/render_paths.zig +347 -0
- package/src/primitives/canvas/render_resources.zig +51 -0
- package/src/primitives/canvas/render_tests.zig +3186 -0
- package/src/primitives/canvas/root.zig +744 -0
- package/src/primitives/canvas/serialization.zig +1528 -0
- package/src/primitives/canvas/svg_icon.zig +636 -0
- package/src/primitives/canvas/svg_icon_tests.zig +312 -0
- package/src/primitives/canvas/test_support.zig +422 -0
- package/src/primitives/canvas/testdata/markdown_document.md +83 -0
- package/src/primitives/canvas/tests.zig +30 -0
- package/src/primitives/canvas/text.zig +82 -0
- package/src/primitives/canvas/text_atlas.zig +408 -0
- package/src/primitives/canvas/text_batch_tests.zig +431 -0
- package/src/primitives/canvas/text_interaction.zig +615 -0
- package/src/primitives/canvas/text_layout.zig +1069 -0
- package/src/primitives/canvas/text_layout_cache.zig +281 -0
- package/src/primitives/canvas/text_layout_hash.zig +113 -0
- package/src/primitives/canvas/text_layout_types.zig +128 -0
- package/src/primitives/canvas/text_measure_cache.zig +279 -0
- package/src/primitives/canvas/text_metrics.zig +309 -0
- package/src/primitives/canvas/text_metrics_tests.zig +262 -0
- package/src/primitives/canvas/text_span_tests.zig +608 -0
- package/src/primitives/canvas/text_spans.zig +1010 -0
- package/src/primitives/canvas/text_tests.zig +1949 -0
- package/src/primitives/canvas/themes/geist.zig +470 -0
- package/src/primitives/canvas/tokens.zig +1522 -0
- package/src/primitives/canvas/ui.zig +2686 -0
- package/src/primitives/canvas/ui_markup.zig +3791 -0
- package/src/primitives/canvas/ui_markup_binary.zig +522 -0
- package/src/primitives/canvas/ui_markup_binary_tests.zig +591 -0
- package/src/primitives/canvas/ui_markup_compiled.zig +2565 -0
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +1669 -0
- package/src/primitives/canvas/ui_markup_contract.zig +1620 -0
- package/src/primitives/canvas/ui_markup_contract_tests.zig +953 -0
- package/src/primitives/canvas/ui_markup_edit.zig +416 -0
- package/src/primitives/canvas/ui_markup_edit_tests.zig +341 -0
- package/src/primitives/canvas/ui_markup_expr.zig +1152 -0
- package/src/primitives/canvas/ui_markup_expr_tests.zig +779 -0
- package/src/primitives/canvas/ui_markup_huge_model_tests.zig +505 -0
- package/src/primitives/canvas/ui_markup_reflect.zig +127 -0
- package/src/primitives/canvas/ui_markup_tests.zig +1545 -0
- package/src/primitives/canvas/ui_markup_view.zig +2352 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +3709 -0
- package/src/primitives/canvas/ui_provenance.zig +96 -0
- package/src/primitives/canvas/ui_provenance_tests.zig +186 -0
- package/src/primitives/canvas/ui_schema.zig +825 -0
- package/src/primitives/canvas/ui_schema_tests.zig +128 -0
- package/src/primitives/canvas/ui_tests.zig +1432 -0
- package/src/primitives/canvas/vector.zig +919 -0
- package/src/primitives/canvas/vector_tests.zig +547 -0
- package/src/primitives/canvas/virtual_extent_tests.zig +317 -0
- package/src/primitives/canvas/virtual_extents.zig +540 -0
- package/src/primitives/canvas/widget_access.zig +208 -0
- package/src/primitives/canvas/widget_anchored_tests.zig +264 -0
- package/src/primitives/canvas/widget_builtin_tests.zig +3160 -0
- package/src/primitives/canvas/widget_invalidation.zig +748 -0
- package/src/primitives/canvas/widget_layout.zig +2055 -0
- package/src/primitives/canvas/widget_layout_tests.zig +2691 -0
- package/src/primitives/canvas/widget_metrics.zig +228 -0
- package/src/primitives/canvas/widget_render.zig +2534 -0
- package/src/primitives/canvas/widget_render_controls.zig +1563 -0
- package/src/primitives/canvas/widget_render_scroll.zig +136 -0
- package/src/primitives/canvas/widget_render_style.zig +626 -0
- package/src/primitives/canvas/widget_render_surfaces.zig +794 -0
- package/src/primitives/canvas/widget_routing.zig +475 -0
- package/src/primitives/canvas/widget_runtime.zig +307 -0
- package/src/primitives/canvas/widget_runtime_tests.zig +3015 -0
- package/src/primitives/canvas/widget_semantics.zig +480 -0
- package/src/primitives/canvas/widget_semantics_tests.zig +2129 -0
- package/src/primitives/canvas/widget_tests.zig +8 -0
- package/src/primitives/canvas/widget_text_input.zig +351 -0
- package/src/primitives/canvas/widget_text_select.zig +147 -0
- package/src/primitives/canvas/widget_tree.zig +232 -0
- package/src/primitives/canvas/widgets.zig +1319 -0
- package/src/primitives/diagnostics/root.zig +445 -0
- package/src/primitives/geometry/root.zig +889 -0
- package/src/primitives/json/root.zig +272 -0
- package/src/primitives/platform_info/root.zig +450 -0
- package/src/primitives/trace/root.zig +569 -0
- package/src/root.zig +165 -0
- package/src/runtime/api.zig +431 -0
- package/src/runtime/async_bridge.zig +49 -0
- package/src/runtime/automation_commands.zig +686 -0
- package/src/runtime/automation_liveness_tests.zig +140 -0
- package/src/runtime/automation_snapshot.zig +246 -0
- package/src/runtime/automation_widget_dispatch.zig +617 -0
- package/src/runtime/bridge_dispatch_tests.zig +202 -0
- package/src/runtime/bridge_payload.zig +194 -0
- package/src/runtime/bridge_permission_tests.zig +551 -0
- package/src/runtime/bridge_responses.zig +414 -0
- package/src/runtime/builtin_bridge.zig +554 -0
- package/src/runtime/canvas_budget_edge_tests.zig +415 -0
- package/src/runtime/canvas_font_tests.zig +410 -0
- package/src/runtime/canvas_fonts.zig +229 -0
- package/src/runtime/canvas_frame.zig +1734 -0
- package/src/runtime/canvas_frame_animation_tests.zig +609 -0
- package/src/runtime/canvas_frame_cache_tests.zig +333 -0
- package/src/runtime/canvas_frame_helpers.zig +414 -0
- package/src/runtime/canvas_frame_invalidation_tests.zig +544 -0
- package/src/runtime/canvas_frame_packet_tests.zig +1353 -0
- package/src/runtime/canvas_frame_patch_tests.zig +1051 -0
- package/src/runtime/canvas_frame_retained_tests.zig +753 -0
- package/src/runtime/canvas_frame_tests.zig +9 -0
- package/src/runtime/canvas_frame_text_layout_budget_tests.zig +149 -0
- package/src/runtime/canvas_image_tests.zig +362 -0
- package/src/runtime/canvas_images.zig +224 -0
- package/src/runtime/canvas_limits.zig +317 -0
- package/src/runtime/canvas_screenshot_tests.zig +289 -0
- package/src/runtime/canvas_widget_accessibility_tests.zig +1494 -0
- package/src/runtime/canvas_widget_anchored_tests.zig +678 -0
- package/src/runtime/canvas_widget_chart_tests.zig +249 -0
- package/src/runtime/canvas_widget_clipboard_tests.zig +399 -0
- package/src/runtime/canvas_widget_context_menu.zig +318 -0
- package/src/runtime/canvas_widget_context_menu_tests.zig +462 -0
- package/src/runtime/canvas_widget_control_tests.zig +2164 -0
- package/src/runtime/canvas_widget_disclosure_tests.zig +440 -0
- package/src/runtime/canvas_widget_display.zig +677 -0
- package/src/runtime/canvas_widget_event_tests.zig +1779 -0
- package/src/runtime/canvas_widget_events.zig +1098 -0
- package/src/runtime/canvas_widget_floating_tests.zig +695 -0
- package/src/runtime/canvas_widget_layout_tests.zig +26 -0
- package/src/runtime/canvas_widget_runtime.zig +1335 -0
- package/src/runtime/canvas_widget_scroll_driver_tests.zig +582 -0
- package/src/runtime/canvas_widget_scroll_drivers.zig +202 -0
- package/src/runtime/canvas_widget_scroll_tests.zig +1549 -0
- package/src/runtime/canvas_widget_semantics_tests.zig +810 -0
- package/src/runtime/canvas_widget_split_tree_tests.zig +1031 -0
- package/src/runtime/canvas_widget_state.zig +798 -0
- package/src/runtime/canvas_widget_text_tests.zig +2104 -0
- package/src/runtime/canvas_widget_window_drag_tests.zig +284 -0
- package/src/runtime/clock.zig +231 -0
- package/src/runtime/core.zig +927 -0
- package/src/runtime/effects.zig +4197 -0
- package/src/runtime/effects_audio_tests.zig +690 -0
- package/src/runtime/effects_clipboard_tests.zig +317 -0
- package/src/runtime/effects_fetch_tests.zig +1031 -0
- package/src/runtime/effects_file_tests.zig +407 -0
- package/src/runtime/effects_tests.zig +1251 -0
- package/src/runtime/flow.zig +1175 -0
- package/src/runtime/frame_profile.zig +221 -0
- package/src/runtime/gpu_surface_events.zig +639 -0
- package/src/runtime/launch_timing.zig +59 -0
- package/src/runtime/markdown_app_tests.zig +224 -0
- package/src/runtime/native_command_event_tests.zig +431 -0
- package/src/runtime/platform_bridge_tests.zig +8 -0
- package/src/runtime/platform_config_tests.zig +392 -0
- package/src/runtime/root.zig +150 -0
- package/src/runtime/runtime_event_tests.zig +1079 -0
- package/src/runtime/session_journal.zig +1522 -0
- package/src/runtime/session_record.zig +332 -0
- package/src/runtime/session_replay.zig +274 -0
- package/src/runtime/session_state.zig +30 -0
- package/src/runtime/session_tests.zig +399 -0
- package/src/runtime/shell_layout.zig +472 -0
- package/src/runtime/shell_layout_tests.zig +1246 -0
- package/src/runtime/shell_view_tests.zig +604 -0
- package/src/runtime/state.zig +110 -0
- package/src/runtime/system_services.zig +392 -0
- package/src/runtime/test_support.zig +79 -0
- package/src/runtime/tests.zig +30 -0
- package/src/runtime/ui_app.zig +3290 -0
- package/src/runtime/ui_app_huge_model_tests.zig +351 -0
- package/src/runtime/ui_app_provenance.zig +370 -0
- package/src/runtime/ui_app_tests.zig +3254 -0
- package/src/runtime/ui_app_virtual_variable_tests.zig +485 -0
- package/src/runtime/ui_app_window_tests.zig +442 -0
- package/src/runtime/validation.zig +206 -0
- package/src/runtime/view.zig +908 -0
- package/src/runtime/view_canvas.zig +850 -0
- package/src/runtime/view_widget_control.zig +433 -0
- package/src/runtime/view_widget_scroll.zig +360 -0
- package/src/runtime/view_widget_text.zig +275 -0
- package/src/runtime/view_widget_tree.zig +843 -0
- package/src/runtime/webview_view_bridge_tests.zig +664 -0
- package/src/runtime/widget_bridge.zig +231 -0
- package/src/runtime/window_command_bridge_tests.zig +403 -0
- package/src/runtime/window_storage.zig +255 -0
- package/src/runtime/window_view_runtime.zig +727 -0
- package/src/runtime/window_views.zig +334 -0
- package/src/security/root.zig +118 -0
- package/src/tooling/android.zig +1172 -0
- package/src/tooling/assets.zig +128 -0
- package/src/tooling/buildgraph.zig +379 -0
- package/src/tooling/cef.zig +671 -0
- package/src/tooling/codesign.zig +172 -0
- package/src/tooling/components/identity_tests.zig +185 -0
- package/src/tooling/components/stepper.zig +113 -0
- package/src/tooling/components/timeline.native +27 -0
- package/src/tooling/components/timeline_item.zig +131 -0
- package/src/tooling/default_icon.icns +0 -0
- package/src/tooling/default_icon.png +0 -0
- package/src/tooling/dev.zig +172 -0
- package/src/tooling/doctor.zig +349 -0
- package/src/tooling/eject_components.zig +170 -0
- package/src/tooling/embedlib.zig +290 -0
- package/src/tooling/ios.zig +528 -0
- package/src/tooling/manifest.zig +1939 -0
- package/src/tooling/package.zig +2388 -0
- package/src/tooling/process_tree.zig +114 -0
- package/src/tooling/raw_manifest.zig +193 -0
- package/src/tooling/root.zig +22 -0
- package/src/tooling/templates.zig +3191 -0
- package/src/tooling/toolchain.zig +371 -0
- package/src/tooling/verbs.zig +204 -0
- package/src/tooling/web_engine.zig +135 -0
- package/src/tooling/xcodeproj.zig +462 -0
- package/src/window_state/root.zig +507 -0
- package/index.js +0 -2
|
@@ -0,0 +1,3709 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
const canvas = @import("root.zig");
|
|
3
|
+
const markup_view = @import("ui_markup_view.zig");
|
|
4
|
+
|
|
5
|
+
const testing = std.testing;
|
|
6
|
+
|
|
7
|
+
pub const Filter = enum { all, active, done };
|
|
8
|
+
|
|
9
|
+
pub const Task = struct {
|
|
10
|
+
id: u32,
|
|
11
|
+
title_storage: [24]u8 = [_]u8{0} ** 24,
|
|
12
|
+
title_len: usize = 0,
|
|
13
|
+
done: bool = false,
|
|
14
|
+
|
|
15
|
+
pub fn title(task: *const Task) []const u8 {
|
|
16
|
+
return task.title_storage[0..task.title_len];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
fn key(task: *const Task) canvas.UiKey {
|
|
20
|
+
return canvas.uiKey(task.id);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
pub const Msg = union(enum) {
|
|
25
|
+
add,
|
|
26
|
+
toggle: u32,
|
|
27
|
+
set_filter: Filter,
|
|
28
|
+
draft: canvas.TextInputEvent,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
pub const Model = struct {
|
|
32
|
+
tasks: [8]Task = undefined,
|
|
33
|
+
task_count: usize = 0,
|
|
34
|
+
filter: Filter = .all,
|
|
35
|
+
|
|
36
|
+
pub const filters = [_]Filter{ .all, .active, .done };
|
|
37
|
+
|
|
38
|
+
fn addTask(model: *Model, text: []const u8, done: bool) void {
|
|
39
|
+
var task = Task{ .id = @intCast(model.task_count + 1), .done = done };
|
|
40
|
+
const len = @min(text.len, task.title_storage.len);
|
|
41
|
+
@memcpy(task.title_storage[0..len], text[0..len]);
|
|
42
|
+
task.title_len = len;
|
|
43
|
+
model.tasks[model.task_count] = task;
|
|
44
|
+
model.task_count += 1;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
pub fn visible(model: *const Model, arena: std.mem.Allocator) []const Task {
|
|
48
|
+
const out = arena.alloc(Task, model.task_count) catch return &.{};
|
|
49
|
+
var len: usize = 0;
|
|
50
|
+
for (model.tasks[0..model.task_count]) |task| {
|
|
51
|
+
const keep = switch (model.filter) {
|
|
52
|
+
.all => true,
|
|
53
|
+
.active => !task.done,
|
|
54
|
+
.done => task.done,
|
|
55
|
+
};
|
|
56
|
+
if (keep) {
|
|
57
|
+
out[len] = task;
|
|
58
|
+
len += 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return out[0..len];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
pub fn open_count(model: *const Model) usize {
|
|
65
|
+
var open: usize = 0;
|
|
66
|
+
for (model.tasks[0..model.task_count]) |task| open += @intFromBool(!task.done);
|
|
67
|
+
return open;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const InboxUi = canvas.Ui(Msg);
|
|
72
|
+
const InboxMarkup = markup_view.MarkupView(Model, Msg);
|
|
73
|
+
|
|
74
|
+
pub const inbox_markup_source =
|
|
75
|
+
\\<column gap="12" padding="16">
|
|
76
|
+
\\ <row gap="8" cross="center">
|
|
77
|
+
\\ <text-field placeholder="New task…" on-input="draft" on-submit="add" grow="1" />
|
|
78
|
+
\\ <button variant="primary" on-press="add">Add</button>
|
|
79
|
+
\\ </row>
|
|
80
|
+
\\ <row gap="8">
|
|
81
|
+
\\ <for each="filters" as="f">
|
|
82
|
+
\\ <button selected="{f == filter}" size="sm" on-press="set_filter:{f}">{f}</button>
|
|
83
|
+
\\ </for>
|
|
84
|
+
\\ </row>
|
|
85
|
+
\\ <scroll grow="1">
|
|
86
|
+
\\ <column gap="2">
|
|
87
|
+
\\ <for each="visible" key="id" as="t">
|
|
88
|
+
\\ <row gap="8" padding="6" cross="center">
|
|
89
|
+
\\ <checkbox checked="{t.done}" on-toggle="toggle:{t.id}" label="Done" />
|
|
90
|
+
\\ <text grow="1">{t.title}</text>
|
|
91
|
+
\\ </row>
|
|
92
|
+
\\ </for>
|
|
93
|
+
\\ </column>
|
|
94
|
+
\\ </scroll>
|
|
95
|
+
\\ <status-bar>{open_count} open</status-bar>
|
|
96
|
+
\\</column>
|
|
97
|
+
;
|
|
98
|
+
|
|
99
|
+
/// The hand-written equivalent of the markup above; parity means the
|
|
100
|
+
/// interpreter builds exactly this tree.
|
|
101
|
+
pub fn handView(ui: *InboxUi, model: *const Model) InboxUi.Node {
|
|
102
|
+
return ui.column(.{ .gap = 12, .padding = 16 }, .{
|
|
103
|
+
ui.row(.{ .gap = 8, .cross = .center }, .{
|
|
104
|
+
ui.textField(.{ .placeholder = "New task…", .on_input = InboxUi.inputMsg(.draft), .on_submit = .add, .grow = 1 }),
|
|
105
|
+
ui.button(.{ .variant = .primary, .on_press = .add }, "Add"),
|
|
106
|
+
}),
|
|
107
|
+
ui.row(.{ .gap = 8 }, filterNodes(ui, model)),
|
|
108
|
+
ui.scroll(.{ .grow = 1 }, ui.column(.{ .gap = 2 }, ui.each(model.visible(ui.arena), Task.key, taskRow))),
|
|
109
|
+
ui.statusBar(.{}, ui.fmt("{d} open", .{model.open_count()})),
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// Filter buttons without explicit keys, matching the markup `for`
|
|
114
|
+
/// (sibling-index identity).
|
|
115
|
+
fn filterNodes(ui: *InboxUi, model: *const Model) []const InboxUi.Node {
|
|
116
|
+
const nodes = ui.arena.alloc(InboxUi.Node, Model.filters.len) catch {
|
|
117
|
+
ui.failed = true;
|
|
118
|
+
return &.{};
|
|
119
|
+
};
|
|
120
|
+
for (Model.filters, 0..) |filter, index| {
|
|
121
|
+
nodes[index] = ui.button(.{
|
|
122
|
+
.size = .sm,
|
|
123
|
+
.selected = filter == model.filter,
|
|
124
|
+
.on_press = Msg{ .set_filter = filter },
|
|
125
|
+
}, @tagName(filter));
|
|
126
|
+
}
|
|
127
|
+
return nodes;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn taskRow(ui: *InboxUi, task: *const Task) InboxUi.Node {
|
|
131
|
+
return ui.row(.{ .gap = 8, .padding = 6, .cross = .center }, .{
|
|
132
|
+
ui.checkbox(.{ .checked = task.done, .on_toggle = Msg{ .toggle = task.id } }),
|
|
133
|
+
ui.text(.{ .grow = 1 }, task.title()),
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
pub fn collectIds(widget: canvas.Widget, ids: *std.ArrayListUnmanaged(canvas.ObjectId), allocator: std.mem.Allocator) !void {
|
|
138
|
+
try ids.append(allocator, widget.id);
|
|
139
|
+
for (widget.children) |child| try collectIds(child, ids, allocator);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
pub fn findByKind(widget: canvas.Widget, kind: canvas.WidgetKind) ?canvas.Widget {
|
|
143
|
+
if (widget.kind == kind) return widget;
|
|
144
|
+
for (widget.children) |child| {
|
|
145
|
+
if (findByKind(child, kind)) |found| return found;
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
pub fn testModel() Model {
|
|
151
|
+
var model = Model{};
|
|
152
|
+
model.addTask("Ship IR", false);
|
|
153
|
+
model.addTask("Write decisions", true);
|
|
154
|
+
model.addTask("Hot reload", false);
|
|
155
|
+
return model;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
test "markup view builds the same tree as the hand-written view" {
|
|
159
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
160
|
+
defer arena_state.deinit();
|
|
161
|
+
const arena = arena_state.allocator();
|
|
162
|
+
|
|
163
|
+
const model = testModel();
|
|
164
|
+
|
|
165
|
+
var view = try InboxMarkup.init(arena, inbox_markup_source);
|
|
166
|
+
var markup_ui = InboxUi.init(arena);
|
|
167
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
168
|
+
|
|
169
|
+
var hand_ui = InboxUi.init(arena);
|
|
170
|
+
const hand_tree = try hand_ui.finalize(handView(&hand_ui, &model));
|
|
171
|
+
|
|
172
|
+
// Identical structural ids, node for node.
|
|
173
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
174
|
+
defer markup_ids.deinit(testing.allocator);
|
|
175
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
176
|
+
defer hand_ids.deinit(testing.allocator);
|
|
177
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
178
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
179
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
180
|
+
|
|
181
|
+
// Identical handler tables: same count, same dispatch results.
|
|
182
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
183
|
+
const add_button = findByKind(markup_tree.root, .button).?;
|
|
184
|
+
try testing.expectEqual(Msg.add, markup_tree.msgForPointer(add_button.id, .up).?);
|
|
185
|
+
|
|
186
|
+
const hand_checkbox = findByKind(hand_tree.root, .checkbox).?;
|
|
187
|
+
const markup_checkbox = findByKind(markup_tree.root, .checkbox).?;
|
|
188
|
+
try testing.expectEqual(hand_checkbox.id, markup_checkbox.id);
|
|
189
|
+
try testing.expectEqual(
|
|
190
|
+
hand_tree.msgForPointer(hand_checkbox.id, .up).?,
|
|
191
|
+
markup_tree.msgForPointer(markup_checkbox.id, .up).?,
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
// Text edits dispatch through the markup-declared on-input constructor.
|
|
195
|
+
const text_field = findByKind(markup_tree.root, .text_field).?;
|
|
196
|
+
const typed = canvas.WidgetKeyboardEvent{ .phase = .text_input, .text = "x" };
|
|
197
|
+
try testing.expectEqualStrings("x", markup_tree.msgForKeyboard(text_field.id, typed).?.draft.insert_text);
|
|
198
|
+
|
|
199
|
+
// Interpolation and state rendering match.
|
|
200
|
+
try testing.expectEqualStrings("2 open", findByKind(markup_tree.root, .status_bar).?.text);
|
|
201
|
+
try testing.expectEqualStrings(
|
|
202
|
+
findByKind(hand_tree.root, .status_bar).?.text,
|
|
203
|
+
findByKind(markup_tree.root, .status_bar).?.text,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
test "markup keyed rows keep ids across model changes and filters dispatch" {
|
|
208
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
209
|
+
defer arena_state.deinit();
|
|
210
|
+
const arena = arena_state.allocator();
|
|
211
|
+
|
|
212
|
+
var model = testModel();
|
|
213
|
+
var view = try InboxMarkup.init(arena, inbox_markup_source);
|
|
214
|
+
|
|
215
|
+
var first_ui = InboxUi.init(arena);
|
|
216
|
+
const first = try first_ui.finalize(try view.build(&first_ui, &model));
|
|
217
|
+
const first_checkbox = findByKind(first.root, .checkbox).?;
|
|
218
|
+
|
|
219
|
+
// Dispatch the done filter through a markup-built button.
|
|
220
|
+
var done_msg: ?Msg = null;
|
|
221
|
+
for (first.handlers) |handler| {
|
|
222
|
+
if (handler.action == .message and handler.action.message == .set_filter) {
|
|
223
|
+
if (handler.action.message.set_filter == .done) done_msg = handler.action.message;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
try testing.expectEqual(Filter.done, done_msg.?.set_filter);
|
|
227
|
+
model.filter = done_msg.?.set_filter;
|
|
228
|
+
|
|
229
|
+
var second_ui = InboxUi.init(arena);
|
|
230
|
+
const second = try second_ui.finalize(try view.build(&second_ui, &model));
|
|
231
|
+
|
|
232
|
+
// Only the done task remains, and it is a different task than the first
|
|
233
|
+
// visible row was — keyed identity distinguishes them.
|
|
234
|
+
const second_checkbox = findByKind(second.root, .checkbox).?;
|
|
235
|
+
try testing.expect(first_checkbox.id != second_checkbox.id);
|
|
236
|
+
try testing.expectEqual(@as(u32, 2), second.msgForPointer(second_checkbox.id, .up).?.toggle);
|
|
237
|
+
|
|
238
|
+
// Back to all: the original first row returns with its original id.
|
|
239
|
+
model.filter = .all;
|
|
240
|
+
var third_ui = InboxUi.init(arena);
|
|
241
|
+
const third = try third_ui.finalize(try view.build(&third_ui, &model));
|
|
242
|
+
try testing.expectEqual(first_checkbox.id, findByKind(third.root, .checkbox).?.id);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ------------------------------------------------ template/use fixture
|
|
246
|
+
|
|
247
|
+
pub const Fruit = struct {
|
|
248
|
+
id: u32,
|
|
249
|
+
name: []const u8,
|
|
250
|
+
|
|
251
|
+
pub fn key(fruit: *const Fruit) canvas.UiKey {
|
|
252
|
+
return canvas.uiKey(fruit.id);
|
|
253
|
+
}
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
pub const TemplateMsg = union(enum) { pick: u32 };
|
|
257
|
+
|
|
258
|
+
pub const TemplateModel = struct {
|
|
259
|
+
top: []const Fruit = &.{},
|
|
260
|
+
bottom: []const Fruit = &.{},
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
/// Templates with a value arg (`title`) and a slice arg (`items`), a `for`
|
|
264
|
+
/// nested inside the template body iterating the slice arg, a nested
|
|
265
|
+
/// `<use>` whose arg binds a loop item field, and style token attributes.
|
|
266
|
+
pub const template_markup_source =
|
|
267
|
+
\\<template name="fruit-pill" args="label">
|
|
268
|
+
\\ <badge background="surface" radius="md">{label}</badge>
|
|
269
|
+
\\</template>
|
|
270
|
+
\\<template name="fruit-list" args="title items">
|
|
271
|
+
\\ <column gap="4" label="{title}">
|
|
272
|
+
\\ <text foreground="text_muted">{title}</text>
|
|
273
|
+
\\ <for each="items" key="id" as="f">
|
|
274
|
+
\\ <row gap="2">
|
|
275
|
+
\\ <use template="fruit-pill" label="{f.name}" />
|
|
276
|
+
\\ <button on-press="pick:{f.id}">{f.name}</button>
|
|
277
|
+
\\ </row>
|
|
278
|
+
\\ </for>
|
|
279
|
+
\\ </column>
|
|
280
|
+
\\</template>
|
|
281
|
+
\\<row gap="8">
|
|
282
|
+
\\ <use template="fruit-list" title="Top" items="{top}" />
|
|
283
|
+
\\ <use template="fruit-list" title="Bottom" items="{bottom}" />
|
|
284
|
+
\\</row>
|
|
285
|
+
;
|
|
286
|
+
|
|
287
|
+
pub const TemplateUi = canvas.Ui(TemplateMsg);
|
|
288
|
+
|
|
289
|
+
/// The hand-written equivalent of the template markup: expansion happens
|
|
290
|
+
/// at the use site, so ids and handlers must match this exactly.
|
|
291
|
+
pub fn handTemplateView(ui: *TemplateUi, model: *const TemplateModel) TemplateUi.Node {
|
|
292
|
+
return ui.row(.{ .gap = 8 }, .{
|
|
293
|
+
fruitColumn(ui, "Top", model.top),
|
|
294
|
+
fruitColumn(ui, "Bottom", model.bottom),
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
fn fruitColumn(ui: *TemplateUi, title: []const u8, items: []const Fruit) TemplateUi.Node {
|
|
299
|
+
return ui.column(.{ .gap = 4, .semantics = .{ .label = title } }, .{
|
|
300
|
+
ui.text(.{ .style_tokens = .{ .foreground = .text_muted } }, title),
|
|
301
|
+
ui.each(items, Fruit.key, fruitRow),
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
fn fruitRow(ui: *TemplateUi, fruit: *const Fruit) TemplateUi.Node {
|
|
306
|
+
var badge = ui.el(.badge, .{ .style_tokens = .{ .background = .surface, .radius = .md } }, .{});
|
|
307
|
+
badge.widget.text = fruit.name;
|
|
308
|
+
return ui.row(.{ .gap = 2 }, .{
|
|
309
|
+
badge,
|
|
310
|
+
ui.button(.{ .on_press = TemplateMsg{ .pick = fruit.id } }, fruit.name),
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
pub fn templateTestModel() TemplateModel {
|
|
315
|
+
return .{
|
|
316
|
+
.top = &[_]Fruit{ .{ .id = 1, .name = "apple" }, .{ .id = 2, .name = "pear" } },
|
|
317
|
+
.bottom = &[_]Fruit{.{ .id = 7, .name = "plum" }},
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
pub fn findByText(widget: canvas.Widget, kind: canvas.WidgetKind, text: []const u8) ?canvas.Widget {
|
|
322
|
+
if (widget.kind == kind and std.mem.eql(u8, widget.text, text)) return widget;
|
|
323
|
+
for (widget.children) |child| {
|
|
324
|
+
if (findByText(child, kind, text)) |found| return found;
|
|
325
|
+
}
|
|
326
|
+
return null;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
pub fn findByRoleLabel(widget: canvas.Widget, role: canvas.WidgetRole, label: []const u8) ?canvas.Widget {
|
|
330
|
+
if (widget.semantics.role == role and std.mem.eql(u8, widget.semantics.label, label)) return widget;
|
|
331
|
+
for (widget.children) |child| {
|
|
332
|
+
if (findByRoleLabel(child, role, label)) |found| return found;
|
|
333
|
+
}
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
test "template expansion builds the hand-written tree with ids from the expansion site" {
|
|
338
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
339
|
+
defer arena_state.deinit();
|
|
340
|
+
const arena = arena_state.allocator();
|
|
341
|
+
|
|
342
|
+
const model = templateTestModel();
|
|
343
|
+
const TemplateMarkup = markup_view.MarkupView(TemplateModel, TemplateMsg);
|
|
344
|
+
|
|
345
|
+
var view = try TemplateMarkup.init(arena, template_markup_source);
|
|
346
|
+
var markup_ui = TemplateUi.init(arena);
|
|
347
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
348
|
+
|
|
349
|
+
var hand_ui = TemplateUi.init(arena);
|
|
350
|
+
const hand_tree = try hand_ui.finalize(handTemplateView(&hand_ui, &model));
|
|
351
|
+
|
|
352
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
353
|
+
defer markup_ids.deinit(testing.allocator);
|
|
354
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
355
|
+
defer hand_ids.deinit(testing.allocator);
|
|
356
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
357
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
358
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
359
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
360
|
+
|
|
361
|
+
// Value args flow into interpolation and semantics; message payloads
|
|
362
|
+
// built from loop items inside the template dispatch normally.
|
|
363
|
+
try testing.expect(findByText(markup_tree.root, .text, "Top") != null);
|
|
364
|
+
try testing.expect(findByText(markup_tree.root, .badge, "plum") != null);
|
|
365
|
+
const pear_button = findByText(markup_tree.root, .button, "pear").?;
|
|
366
|
+
try testing.expectEqual(@as(u32, 2), markup_tree.msgForPointer(pear_button.id, .up).?.pick);
|
|
367
|
+
|
|
368
|
+
// Two uses of the same template at different sites get different ids;
|
|
369
|
+
// the same site is stable across rebuilds.
|
|
370
|
+
const top_text = findByText(markup_tree.root, .text, "Top").?;
|
|
371
|
+
const bottom_text = findByText(markup_tree.root, .text, "Bottom").?;
|
|
372
|
+
try testing.expect(top_text.id != bottom_text.id);
|
|
373
|
+
|
|
374
|
+
var rebuild_ui = TemplateUi.init(arena);
|
|
375
|
+
const rebuilt = try rebuild_ui.finalize(try view.build(&rebuild_ui, &model));
|
|
376
|
+
try testing.expectEqual(top_text.id, findByText(rebuilt.root, .text, "Top").?.id);
|
|
377
|
+
try testing.expectEqual(pear_button.id, findByText(rebuilt.root, .button, "pear").?.id);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
test "style token references resolve against tokens at finalize time" {
|
|
381
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
382
|
+
defer arena_state.deinit();
|
|
383
|
+
const arena = arena_state.allocator();
|
|
384
|
+
|
|
385
|
+
const model = templateTestModel();
|
|
386
|
+
const TemplateMarkup = markup_view.MarkupView(TemplateModel, TemplateMsg);
|
|
387
|
+
var view = try TemplateMarkup.init(arena, template_markup_source);
|
|
388
|
+
|
|
389
|
+
// Plain finalize resolves against the default (light) tokens.
|
|
390
|
+
var light_ui = TemplateUi.init(arena);
|
|
391
|
+
const light = try light_ui.finalize(try view.build(&light_ui, &model));
|
|
392
|
+
const light_tokens = canvas.DesignTokens{};
|
|
393
|
+
const light_badge = findByText(light.root, .badge, "apple").?;
|
|
394
|
+
try testing.expectEqualDeep(light_tokens.colors.surface, light_badge.style.background.?);
|
|
395
|
+
try testing.expectEqual(light_tokens.radius.md, light_badge.style.radius.?);
|
|
396
|
+
const light_text = findByText(light.root, .text, "Top").?;
|
|
397
|
+
try testing.expectEqualDeep(light_tokens.colors.text_muted, light_text.style.foreground.?);
|
|
398
|
+
|
|
399
|
+
// finalizeWithTokens re-resolves the same references against live
|
|
400
|
+
// tokens: a theme change rebuilds into different concrete colors.
|
|
401
|
+
var dark_ui = TemplateUi.init(arena);
|
|
402
|
+
const dark_tokens = canvas.DesignTokens.theme(.{ .color_scheme = .dark });
|
|
403
|
+
const dark = try dark_ui.finalizeWithTokens(try view.build(&dark_ui, &model), dark_tokens);
|
|
404
|
+
const dark_badge = findByText(dark.root, .badge, "apple").?;
|
|
405
|
+
try testing.expectEqualDeep(dark_tokens.colors.surface, dark_badge.style.background.?);
|
|
406
|
+
try testing.expect(!std.meta.eql(light_badge.style.background.?, dark_badge.style.background.?));
|
|
407
|
+
|
|
408
|
+
// Ids are independent of token resolution.
|
|
409
|
+
try testing.expectEqual(light_badge.id, dark_badge.id);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
test "explicit style values win over token references" {
|
|
413
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
414
|
+
defer arena_state.deinit();
|
|
415
|
+
const arena = arena_state.allocator();
|
|
416
|
+
|
|
417
|
+
const explicit = canvas.Color.rgb8(1, 2, 3);
|
|
418
|
+
var ui = TemplateUi.init(arena);
|
|
419
|
+
const node = ui.el(.badge, .{
|
|
420
|
+
.style = .{ .background = explicit },
|
|
421
|
+
.style_tokens = .{ .background = .surface, .radius = .md },
|
|
422
|
+
}, .{});
|
|
423
|
+
const tree = try ui.finalize(node);
|
|
424
|
+
try testing.expectEqualDeep(explicit, tree.root.style.background.?);
|
|
425
|
+
try testing.expectEqual((canvas.DesignTokens{}).radius.md, tree.root.style.radius.?);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
test "the style token name lists match the canvas token structs and the interpreter table" {
|
|
429
|
+
// Every ColorTokens field is listed, and every listed name is a field.
|
|
430
|
+
const color_fields = @typeInfo(canvas.ColorTokens).@"struct".fields;
|
|
431
|
+
try testing.expectEqual(color_fields.len, canvas.ui_markup.known_color_token_names.len);
|
|
432
|
+
inline for (color_fields) |field| {
|
|
433
|
+
try testing.expect(nameListed(field.name, &canvas.ui_markup.known_color_token_names));
|
|
434
|
+
}
|
|
435
|
+
const radius_fields = @typeInfo(canvas.RadiusTokens).@"struct".fields;
|
|
436
|
+
try testing.expectEqual(radius_fields.len, canvas.ui_markup.known_radius_token_names.len);
|
|
437
|
+
inline for (radius_fields) |field| {
|
|
438
|
+
try testing.expect(nameListed(field.name, &canvas.ui_markup.known_radius_token_names));
|
|
439
|
+
}
|
|
440
|
+
// The validator's attribute list matches the engines' shared table.
|
|
441
|
+
try testing.expectEqual(markup_view.color_style_attr_fields.len, canvas.ui_markup.known_color_style_attrs.len);
|
|
442
|
+
for (markup_view.color_style_attr_fields) |entry| {
|
|
443
|
+
try testing.expect(nameListed(entry.markup, &canvas.ui_markup.known_color_style_attrs));
|
|
444
|
+
}
|
|
445
|
+
// Every color entry targets a StyleTokenRefs field, and every color
|
|
446
|
+
// field of StyleTokenRefs is reachable from markup.
|
|
447
|
+
inline for (@typeInfo(canvas.StyleTokenRefs).@"struct".fields) |field| {
|
|
448
|
+
if (comptime std.mem.eql(u8, field.name, "radius")) continue;
|
|
449
|
+
var found = false;
|
|
450
|
+
for (markup_view.color_style_attr_fields) |entry| {
|
|
451
|
+
if (std.mem.eql(u8, entry.zig, field.name)) found = true;
|
|
452
|
+
}
|
|
453
|
+
try testing.expect(found);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
fn nameListed(name: []const u8, list: []const []const u8) bool {
|
|
458
|
+
for (list) |candidate| {
|
|
459
|
+
if (std.mem.eql(u8, name, candidate)) return true;
|
|
460
|
+
}
|
|
461
|
+
return false;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
test "template and style token build failures carry position and message" {
|
|
465
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
466
|
+
defer arena_state.deinit();
|
|
467
|
+
const arena = arena_state.allocator();
|
|
468
|
+
const model = Model{};
|
|
469
|
+
|
|
470
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
471
|
+
.{
|
|
472
|
+
.source = "<column>\n <use template=\"nope\" />\n</column>",
|
|
473
|
+
.message = canvas.ui_markup.use_undefined_template_message,
|
|
474
|
+
},
|
|
475
|
+
.{
|
|
476
|
+
// Self-recursion parses; the build's expansion-depth guard
|
|
477
|
+
// reports it instead of recursing forever.
|
|
478
|
+
.source = "<template name=\"loop\"><column><use template=\"loop\" /></column></template>\n<use template=\"loop\" />",
|
|
479
|
+
.message = canvas.ui_markup.use_earlier_template_message,
|
|
480
|
+
},
|
|
481
|
+
.{
|
|
482
|
+
.source = "<template name=\"t\" args=\"v\"><text>{v.x}</text></template>\n<column><use template=\"t\" v=\"1\" /></column>",
|
|
483
|
+
.message = "template arg values have no fields",
|
|
484
|
+
},
|
|
485
|
+
.{
|
|
486
|
+
// A slice arg (filters is a pub const array) is only iterable.
|
|
487
|
+
.source = "<template name=\"t\" args=\"items\"><text>{items}</text></template>\n<column><use template=\"t\" items=\"{filters}\" /></column>",
|
|
488
|
+
.message = "slice-valued template args are only usable with for each",
|
|
489
|
+
},
|
|
490
|
+
.{
|
|
491
|
+
.source = "<template name=\"t\" args=\"extra\"><text>{extra}</text></template>\n<column><use template=\"t\" /></column>",
|
|
492
|
+
.message = canvas.ui_markup.use_missing_arg_message,
|
|
493
|
+
},
|
|
494
|
+
.{
|
|
495
|
+
.source = "<column background=\"{filter}\" />",
|
|
496
|
+
.message = canvas.ui_markup.style_token_literal_message,
|
|
497
|
+
},
|
|
498
|
+
.{
|
|
499
|
+
.source = "<column background=\"pink\" />",
|
|
500
|
+
.message = canvas.ui_markup.unknown_color_token_message,
|
|
501
|
+
},
|
|
502
|
+
.{
|
|
503
|
+
.source = "<column radius=\"huge\" />",
|
|
504
|
+
.message = canvas.ui_markup.unknown_radius_token_message,
|
|
505
|
+
},
|
|
506
|
+
};
|
|
507
|
+
for (cases) |case| {
|
|
508
|
+
var view = try InboxMarkup.init(arena, case.source);
|
|
509
|
+
var ui = InboxUi.init(arena);
|
|
510
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
511
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
512
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
test "markup build failures carry position and message" {
|
|
517
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
518
|
+
defer arena_state.deinit();
|
|
519
|
+
const arena = arena_state.allocator();
|
|
520
|
+
const model = Model{};
|
|
521
|
+
|
|
522
|
+
const cases = [_][]const u8{
|
|
523
|
+
"<column>\n <bogus-element />\n</column>",
|
|
524
|
+
"<column gap=\"{missing_binding}\" />",
|
|
525
|
+
"<column>\n <button on-press=\"unknown_msg\">X</button>\n</column>",
|
|
526
|
+
"<column>\n <button on-press=\"toggle\">X</button>\n</column>",
|
|
527
|
+
"<column>\n <for each=\"nope\" as=\"t\"><text>{t}</text></for>\n</column>",
|
|
528
|
+
"<column bogus-attr=\"1\" />",
|
|
529
|
+
"<column on-input=\"draft\">\n <text>dead handler</text>\n</column>",
|
|
530
|
+
};
|
|
531
|
+
for (cases) |source| {
|
|
532
|
+
var view = try InboxMarkup.init(arena, source);
|
|
533
|
+
var ui = InboxUi.init(arena);
|
|
534
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
535
|
+
try testing.expect(view.diagnostic.message.len > 0);
|
|
536
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
test "dead value handlers on non-hit-target elements fail the build with the teaching message" {
|
|
541
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
542
|
+
defer arena_state.deinit();
|
|
543
|
+
const arena = arena_state.allocator();
|
|
544
|
+
const model = Model{};
|
|
545
|
+
|
|
546
|
+
// Value/text handlers on layout containers are dead — the element has
|
|
547
|
+
// no control or text behavior to bind.
|
|
548
|
+
const sources = [_][]const u8{
|
|
549
|
+
"<column>\n <row on-change=\"add\">\n <text>press me</text>\n </row>\n</column>",
|
|
550
|
+
"<column>\n <toggle-group on-submit=\"add\">\n <toggle-button>A</toggle-button>\n </toggle-group>\n</column>",
|
|
551
|
+
};
|
|
552
|
+
for (sources) |source| {
|
|
553
|
+
var view = try InboxMarkup.init(arena, source);
|
|
554
|
+
var ui = InboxUi.init(arena);
|
|
555
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
556
|
+
try testing.expectEqualStrings(canvas.ui_markup.non_hit_target_handler_message, view.diagnostic.message);
|
|
557
|
+
try testing.expectEqual(@as(usize, 2), view.diagnostic.line);
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// The same handler on a hit-target leaf builds fine.
|
|
561
|
+
var view = try InboxMarkup.init(arena, "<column>\n <list-item on-press=\"add\">press me</list-item>\n</column>");
|
|
562
|
+
var ui = InboxUi.init(arena);
|
|
563
|
+
_ = try view.build(&ui, &model);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
test "list-item element children build the list-row composite" {
|
|
567
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
568
|
+
defer arena_state.deinit();
|
|
569
|
+
const arena = arena_state.allocator();
|
|
570
|
+
const model = Model{};
|
|
571
|
+
|
|
572
|
+
// Element children in place of the text run: the row keeps its flat
|
|
573
|
+
// list-item chrome (wash states, focus ring) and flows the children
|
|
574
|
+
// horizontally inside it — no bordered container needed for a
|
|
575
|
+
// composite row.
|
|
576
|
+
var view = try InboxMarkup.init(arena, "<column>\n <list-item on-press=\"add\" label=\"Groceries row\" padding=\"8\" gap=\"8\">\n <text grow=\"1\">Groceries</text>\n <badge variant=\"secondary\">3</badge>\n </list-item>\n</column>");
|
|
577
|
+
var ui = InboxUi.init(arena);
|
|
578
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
579
|
+
const item = tree.root.children[0];
|
|
580
|
+
try testing.expectEqual(canvas.WidgetKind.list_item, item.kind);
|
|
581
|
+
try testing.expectEqual(@as(usize, 2), item.children.len);
|
|
582
|
+
try testing.expectEqual(canvas.WidgetKind.text, item.children[0].kind);
|
|
583
|
+
try testing.expectEqual(canvas.WidgetKind.badge, item.children[1].kind);
|
|
584
|
+
// The composite carries no text run of its own; the label names it.
|
|
585
|
+
try testing.expectEqual(@as(usize, 0), item.text.len);
|
|
586
|
+
try testing.expect(item.semantics.actions.press);
|
|
587
|
+
try testing.expectEqual(Msg.add, tree.msgForPointer(item.id, .up).?);
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
test "mixed text and element content inside a list-item is a teaching error" {
|
|
591
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
592
|
+
defer arena_state.deinit();
|
|
593
|
+
const arena = arena_state.allocator();
|
|
594
|
+
const model = Model{};
|
|
595
|
+
|
|
596
|
+
var view = try InboxMarkup.init(arena, "<column>\n <list-item on-press=\"add\" label=\"Groceries row\">Groceries<badge>3</badge></list-item>\n</column>");
|
|
597
|
+
var ui = InboxUi.init(arena);
|
|
598
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
599
|
+
try testing.expectEqualStrings(canvas.ui_markup.text_or_children_content_message, view.diagnostic.message);
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
test "press handlers on layout elements build and stamp the press action" {
|
|
603
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
604
|
+
defer arena_state.deinit();
|
|
605
|
+
const arena = arena_state.allocator();
|
|
606
|
+
const model = Model{};
|
|
607
|
+
|
|
608
|
+
// A pressable row with plain text children: the bound handler makes
|
|
609
|
+
// the row a widget-level hit target (semantics.actions.press) and the
|
|
610
|
+
// press fall-through routes clicks on the text to it.
|
|
611
|
+
var view = try InboxMarkup.init(arena, "<column>\n <row on-press=\"add\" gap=\"8\">\n <text>press me</text>\n </row>\n</column>");
|
|
612
|
+
var ui = InboxUi.init(arena);
|
|
613
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
614
|
+
const row = tree.root.children[0];
|
|
615
|
+
try testing.expectEqual(canvas.WidgetKind.row, row.kind);
|
|
616
|
+
try testing.expect(row.semantics.actions.press);
|
|
617
|
+
try testing.expect(canvas.widgetIsHitTarget(row));
|
|
618
|
+
try testing.expect(canvas.widgetClaimsPress(row));
|
|
619
|
+
try testing.expectEqual(Msg.add, tree.msgForPointer(row.id, .up).?);
|
|
620
|
+
// The plain text child stays fall-through: no press claim of its own.
|
|
621
|
+
try testing.expect(!canvas.widgetClaimsPress(row.children[0]));
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
test "window-drag marks an element as a window-drag region without claiming presses" {
|
|
625
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
626
|
+
defer arena_state.deinit();
|
|
627
|
+
const arena = arena_state.allocator();
|
|
628
|
+
const model = Model{};
|
|
629
|
+
|
|
630
|
+
// The hidden-titlebar header shape: a drag-region row holding a real
|
|
631
|
+
// button. The row becomes a hit target (so presses on its background
|
|
632
|
+
// land on IT), but it never claims presses — the button inside stays
|
|
633
|
+
// a button and the window-drag walk stops at it.
|
|
634
|
+
var view = try InboxMarkup.init(arena, "<column>\n <row window-drag=\"true\" gap=\"8\">\n <text>Title</text>\n <button on-press=\"add\">Open</button>\n </row>\n</column>");
|
|
635
|
+
var ui = InboxUi.init(arena);
|
|
636
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
637
|
+
const row = tree.root.children[0];
|
|
638
|
+
try testing.expectEqual(canvas.WidgetKind.row, row.kind);
|
|
639
|
+
try testing.expect(row.window_drag);
|
|
640
|
+
try testing.expect(canvas.widgetIsWindowDragRegion(row));
|
|
641
|
+
try testing.expect(canvas.widgetIsHitTarget(row));
|
|
642
|
+
try testing.expect(!canvas.widgetClaimsPress(row));
|
|
643
|
+
try testing.expect(!row.children[0].window_drag);
|
|
644
|
+
try testing.expect(canvas.widgetClaimsPress(row.children[1]));
|
|
645
|
+
|
|
646
|
+
// window-drag="false" (and absent) leave the row inert.
|
|
647
|
+
var plain_view = try InboxMarkup.init(arena, "<column>\n <row window-drag=\"false\" gap=\"8\">\n <text>Title</text>\n </row>\n</column>");
|
|
648
|
+
var plain_ui = InboxUi.init(arena);
|
|
649
|
+
const plain_tree = try plain_ui.finalize(try plain_view.build(&plain_ui, &model));
|
|
650
|
+
try testing.expect(!plain_tree.root.children[0].window_drag);
|
|
651
|
+
try testing.expect(!canvas.widgetIsHitTarget(plain_tree.root.children[0]));
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
test "overscroll on scroll stamps the region's edge behavior" {
|
|
655
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
656
|
+
defer arena_state.deinit();
|
|
657
|
+
const arena = arena_state.allocator();
|
|
658
|
+
const model = Model{};
|
|
659
|
+
|
|
660
|
+
// A region opting into rubber-band; unset regions keep `.default`
|
|
661
|
+
// (follow the ScrollPhysics.overscroll token, off unless a theme
|
|
662
|
+
// flips it).
|
|
663
|
+
var view = try InboxMarkup.init(arena, "<column>\n <scroll overscroll=\"rubber_band\">\n <column><text>a</text></column>\n </scroll>\n <scroll>\n <column><text>b</text></column>\n </scroll>\n</column>");
|
|
664
|
+
var ui = InboxUi.init(arena);
|
|
665
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
666
|
+
const bouncy = tree.root.children[0];
|
|
667
|
+
try testing.expectEqual(canvas.WidgetKind.scroll_view, bouncy.kind);
|
|
668
|
+
try testing.expectEqual(canvas.WidgetOverscroll.rubber_band, bouncy.overscroll);
|
|
669
|
+
try testing.expectEqual(canvas.WidgetOverscroll.default, tree.root.children[1].overscroll);
|
|
670
|
+
|
|
671
|
+
// The per-region override resolves onto the physics token for both
|
|
672
|
+
// scroll paths.
|
|
673
|
+
const physics = canvas.ScrollPhysics{};
|
|
674
|
+
try testing.expectEqual(canvas.ScrollOverscroll.rubber_band, canvas.widgetScrollPhysics(bouncy, physics).overscroll);
|
|
675
|
+
try testing.expectEqual(canvas.ScrollOverscroll.none, canvas.widgetScrollPhysics(tree.root.children[1], physics).overscroll);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
test "overscroll value vocabulary mirrors the live WidgetOverscroll enum" {
|
|
679
|
+
// The validator's std-only mirror of the enum's member names; a new
|
|
680
|
+
// member cannot ship without its markup spelling.
|
|
681
|
+
const fields = @typeInfo(canvas.WidgetOverscroll).@"enum".fields;
|
|
682
|
+
try testing.expectEqual(fields.len, canvas.ui_markup.overscroll_value_names.len);
|
|
683
|
+
inline for (fields, 0..) |field, index| {
|
|
684
|
+
try testing.expectEqualStrings(field.name, canvas.ui_markup.overscroll_value_names[index]);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
test "overflow value vocabulary mirrors the live TextOverflow enum" {
|
|
689
|
+
// The validator's std-only mirror of the enum's member names; a new
|
|
690
|
+
// member cannot ship without its markup spelling.
|
|
691
|
+
const fields = @typeInfo(canvas.TextOverflow).@"enum".fields;
|
|
692
|
+
try testing.expectEqual(fields.len, canvas.ui_markup.overflow_value_names.len);
|
|
693
|
+
inline for (fields, 0..) |field, index| {
|
|
694
|
+
try testing.expectEqualStrings(field.name, canvas.ui_markup.overflow_value_names[index]);
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
test "resize-duration and resize-easing on split stamp the layout-tween declaration" {
|
|
699
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
700
|
+
defer arena_state.deinit();
|
|
701
|
+
const arena = arena_state.allocator();
|
|
702
|
+
const model = Model{};
|
|
703
|
+
|
|
704
|
+
// A split declaring the tween pair; a bare split keeps the zero
|
|
705
|
+
// (snap) defaults, so existing documents lower byte-identically.
|
|
706
|
+
var view = try InboxMarkup.init(arena, "<column>\n <split value=\"0.3\" resize-duration=\"180\" resize-easing=\"emphasized\">\n <panel><text>a</text></panel>\n <panel><text>b</text></panel>\n </split>\n <split value=\"0.5\">\n <panel><text>c</text></panel>\n <panel><text>d</text></panel>\n </split>\n</column>");
|
|
707
|
+
var ui = InboxUi.init(arena);
|
|
708
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
709
|
+
const tweened = tree.root.children[0];
|
|
710
|
+
try testing.expectEqual(canvas.WidgetKind.split, tweened.kind);
|
|
711
|
+
try testing.expectEqual(@as(u32, 180), tweened.resize_duration_ms);
|
|
712
|
+
try testing.expectEqual(canvas.Easing.emphasized, tweened.resize_easing);
|
|
713
|
+
try testing.expectEqual(@as(f32, 0.3), tweened.value);
|
|
714
|
+
const plain = tree.root.children[1];
|
|
715
|
+
try testing.expectEqual(@as(u32, 0), plain.resize_duration_ms);
|
|
716
|
+
try testing.expectEqual(canvas.Easing.standard, plain.resize_easing);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
test "resize-easing value vocabulary mirrors the live Easing enum" {
|
|
720
|
+
// The validator's std-only mirror of the enum's member names; a new
|
|
721
|
+
// member cannot ship without its markup spelling.
|
|
722
|
+
const fields = @typeInfo(canvas.Easing).@"enum".fields;
|
|
723
|
+
try testing.expectEqual(fields.len, canvas.ui_markup.resize_easing_value_names.len);
|
|
724
|
+
inline for (fields, 0..) |field, index| {
|
|
725
|
+
try testing.expectEqualStrings(field.name, canvas.ui_markup.resize_easing_value_names[index]);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
test "span weight vocabulary mirrors the live TextSpanWeight enum" {
|
|
730
|
+
// The validator's std-only mirror of the enum's member names; a new
|
|
731
|
+
// member cannot ship without its markup spelling.
|
|
732
|
+
const fields = @typeInfo(canvas.TextSpanWeight).@"enum".fields;
|
|
733
|
+
try testing.expectEqual(fields.len, canvas.ui_markup.span_weight_value_names.len);
|
|
734
|
+
inline for (fields, 0..) |field, index| {
|
|
735
|
+
try testing.expectEqualStrings(field.name, canvas.ui_markup.span_weight_value_names[index]);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
test "gap on stacking containers fails the build with the teaching message" {
|
|
740
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
741
|
+
defer arena_state.deinit();
|
|
742
|
+
const arena = arena_state.allocator();
|
|
743
|
+
const model = Model{};
|
|
744
|
+
|
|
745
|
+
// A plain overlay container and a modal surface kind: both layer
|
|
746
|
+
// their children, so the gap could never space them.
|
|
747
|
+
const sources = [_][]const u8{
|
|
748
|
+
"<column>\n <panel gap=\"8\">\n <text>a</text>\n <text>b</text>\n </panel>\n</column>",
|
|
749
|
+
"<column>\n <sheet text=\"Share\" gap=\"8\">\n <text>a</text>\n </sheet>\n</column>",
|
|
750
|
+
};
|
|
751
|
+
for (sources) |source| {
|
|
752
|
+
var view = try InboxMarkup.init(arena, source);
|
|
753
|
+
var ui = InboxUi.init(arena);
|
|
754
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
755
|
+
try testing.expectEqualStrings(canvas.ui_markup.stack_container_gap_message, view.diagnostic.message);
|
|
756
|
+
try testing.expectEqual(@as(usize, 2), view.diagnostic.line);
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// gap on flow containers stays fine, including inside a panel.
|
|
760
|
+
var view = try InboxMarkup.init(arena, "<panel>\n <column gap=\"8\">\n <text>a</text>\n <text>b</text>\n </column>\n</panel>");
|
|
761
|
+
var ui = InboxUi.init(arena);
|
|
762
|
+
_ = try view.build(&ui, &model);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
test "text size rungs build text widgets on the typography ladder" {
|
|
766
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
767
|
+
defer arena_state.deinit();
|
|
768
|
+
const arena = arena_state.allocator();
|
|
769
|
+
const model = Model{};
|
|
770
|
+
|
|
771
|
+
var view = try InboxMarkup.init(arena, "<column gap=\"4\">\n <text size=\"heading\">Inbox</text>\n <text size=\"display\">42</text>\n <text size=\"sm\">detail</text>\n</column>");
|
|
772
|
+
var ui = InboxUi.init(arena);
|
|
773
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
774
|
+
try testing.expectEqual(canvas.WidgetSize.heading, tree.root.children[0].size);
|
|
775
|
+
try testing.expectEqual(canvas.WidgetSize.display, tree.root.children[1].size);
|
|
776
|
+
try testing.expectEqual(canvas.WidgetSize.sm, tree.root.children[2].size);
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
test "size teaching errors: typography rungs off text, unknown values, numbers" {
|
|
780
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
781
|
+
defer arena_state.deinit();
|
|
782
|
+
const arena = arena_state.allocator();
|
|
783
|
+
const model = Model{};
|
|
784
|
+
|
|
785
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
786
|
+
// heading/display are typography rungs only text resolves; a
|
|
787
|
+
// control's size register is the control scale.
|
|
788
|
+
.{
|
|
789
|
+
.source = "<column>\n <button size=\"display\" on-press=\"add\">Go</button>\n</column>",
|
|
790
|
+
.message = canvas.ui_markup.text_size_element_message,
|
|
791
|
+
},
|
|
792
|
+
.{
|
|
793
|
+
.source = "<column>\n <badge size=\"heading\">3</badge>\n</column>",
|
|
794
|
+
.message = canvas.ui_markup.text_size_element_message,
|
|
795
|
+
},
|
|
796
|
+
// Unknown values and numeric literals teach the vocabulary and
|
|
797
|
+
// the deliberate no-numbers line.
|
|
798
|
+
.{
|
|
799
|
+
.source = "<column>\n <text size=\"title\">Inbox</text>\n</column>",
|
|
800
|
+
.message = canvas.ui_markup.size_value_message,
|
|
801
|
+
},
|
|
802
|
+
.{
|
|
803
|
+
.source = "<column>\n <text size=\"48\">42</text>\n</column>",
|
|
804
|
+
.message = canvas.ui_markup.size_value_message,
|
|
805
|
+
},
|
|
806
|
+
};
|
|
807
|
+
for (cases) |case| {
|
|
808
|
+
var view = try InboxMarkup.init(arena, case.source);
|
|
809
|
+
var ui = InboxUi.init(arena);
|
|
810
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
811
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
812
|
+
try testing.expectEqual(@as(usize, 2), view.diagnostic.line);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
test "the registry's size vocabulary matches the live WidgetSize enum" {
|
|
817
|
+
// size="..." values resolve through std.meta.stringToEnum on
|
|
818
|
+
// canvas.WidgetSize; the registry's std-only mirrors (control scale +
|
|
819
|
+
// text-only typography rungs) must list exactly the same names so the
|
|
820
|
+
// validator and the engines accept identically.
|
|
821
|
+
const fields = std.meta.fields(canvas.WidgetSize);
|
|
822
|
+
try testing.expectEqual(
|
|
823
|
+
fields.len,
|
|
824
|
+
canvas.ui_markup.schema.control_size_value_names.len + canvas.ui_markup.schema.text_size_value_names.len,
|
|
825
|
+
);
|
|
826
|
+
inline for (fields) |field| {
|
|
827
|
+
const in_control = nameListed(field.name, &canvas.ui_markup.schema.control_size_value_names);
|
|
828
|
+
const in_text = nameListed(field.name, &canvas.ui_markup.schema.text_size_value_names);
|
|
829
|
+
// Every enum member sits on exactly one axis.
|
|
830
|
+
try testing.expect(in_control != in_text);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
test "markup icons build icon widgets with validated names" {
|
|
835
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
836
|
+
defer arena_state.deinit();
|
|
837
|
+
const arena = arena_state.allocator();
|
|
838
|
+
const model = Model{};
|
|
839
|
+
|
|
840
|
+
var view = try InboxMarkup.init(arena, "<row gap=\"8\">\n <icon name=\"search\" width=\"16\" height=\"16\" />\n <text>Search</text>\n</row>");
|
|
841
|
+
var ui = InboxUi.init(arena);
|
|
842
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
843
|
+
const icon = tree.root.children[0];
|
|
844
|
+
try testing.expectEqual(canvas.WidgetKind.icon, icon.kind);
|
|
845
|
+
try testing.expectEqualStrings("search", icon.text);
|
|
846
|
+
|
|
847
|
+
// The other two grammar forms ride the EXPLICIT icon channel
|
|
848
|
+
// (`Widget.icon`), resolved at draw time: an app: reference carries
|
|
849
|
+
// its full spelling, and a bound name carries whatever the model
|
|
850
|
+
// produced (an unknown value draws the missing-icon fallback with a
|
|
851
|
+
// Debug warning - loud, never silent).
|
|
852
|
+
var open_view = try InboxMarkup.init(arena, "<row gap=\"8\">\n <icon name=\"app:wave\" />\n <icon name=\"{filter}\" />\n</row>");
|
|
853
|
+
var open_ui = InboxUi.init(arena);
|
|
854
|
+
const open_tree = try open_ui.finalize(try open_view.build(&open_ui, &model));
|
|
855
|
+
try testing.expectEqualStrings("app:wave", open_tree.root.children[0].icon);
|
|
856
|
+
try testing.expectEqualStrings("all", open_tree.root.children[1].icon);
|
|
857
|
+
|
|
858
|
+
// Unknown built-in names, unknown namespaces, malformed app: names,
|
|
859
|
+
// misplaced name attrs, and children fail the build with the
|
|
860
|
+
// validator's messages.
|
|
861
|
+
const failing = [_][]const u8{
|
|
862
|
+
"<row>\n <icon />\n</row>",
|
|
863
|
+
"<row>\n <icon name=\"sparkle-pony\" />\n</row>",
|
|
864
|
+
"<row>\n <icon name=\"lib:search\" />\n</row>",
|
|
865
|
+
"<row>\n <icon name=\"app:\" />\n</row>",
|
|
866
|
+
"<row>\n <icon name=\"app:Wave Pulse\" />\n</row>",
|
|
867
|
+
"<row>\n <badge name=\"search\">3</badge>\n</row>",
|
|
868
|
+
"<row>\n <icon name=\"search\"><text>x</text></icon>\n</row>",
|
|
869
|
+
};
|
|
870
|
+
for (failing) |source| {
|
|
871
|
+
var failing_view = try InboxMarkup.init(arena, source);
|
|
872
|
+
var failing_ui = InboxUi.init(arena);
|
|
873
|
+
try testing.expectError(error.MarkupBuild, failing_view.build(&failing_ui, &model));
|
|
874
|
+
try testing.expect(failing_view.diagnostic.message.len > 0);
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
test "markup buttons take an inline icon with validated names" {
|
|
879
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
880
|
+
defer arena_state.deinit();
|
|
881
|
+
const arena = arena_state.allocator();
|
|
882
|
+
const model = Model{};
|
|
883
|
+
|
|
884
|
+
var view = try InboxMarkup.init(arena, "<row gap=\"8\">\n <button icon=\"save\" on-press=\"add\">Save</button>\n <button icon=\"refresh-cw\" label=\"Refresh\" on-press=\"add\"></button>\n <toggle-button icon=\"arrow-up\" on-toggle=\"add\">Newest</toggle-button>\n <list-item icon=\"folder\" on-press=\"add\">Projects</list-item>\n <menu-item icon=\"trash\" on-press=\"add\">Delete</menu-item>\n</row>");
|
|
885
|
+
var ui = InboxUi.init(arena);
|
|
886
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
887
|
+
const labeled = tree.root.children[0];
|
|
888
|
+
try testing.expectEqual(canvas.WidgetKind.button, labeled.kind);
|
|
889
|
+
try testing.expectEqualStrings("save", labeled.icon);
|
|
890
|
+
try testing.expectEqualStrings("Save", labeled.text);
|
|
891
|
+
const icon_only = tree.root.children[1];
|
|
892
|
+
try testing.expectEqualStrings("refresh-cw", icon_only.icon);
|
|
893
|
+
try testing.expectEqualStrings("", icon_only.text);
|
|
894
|
+
// One hit target: the button dispatches its own press; there is no
|
|
895
|
+
// icon child to duplicate the handler onto.
|
|
896
|
+
try testing.expectEqual(@as(usize, 0), labeled.children.len);
|
|
897
|
+
try testing.expect(tree.msgFor(labeled.id, .press) != null);
|
|
898
|
+
// The rest of the labeled interactive set: toggle-buttons
|
|
899
|
+
// (chips, tab strips), list items, and menu items carry the icon in
|
|
900
|
+
// the same field with the same closed vocabulary.
|
|
901
|
+
const chip = tree.root.children[2];
|
|
902
|
+
try testing.expectEqual(canvas.WidgetKind.toggle_button, chip.kind);
|
|
903
|
+
try testing.expectEqualStrings("arrow-up", chip.icon);
|
|
904
|
+
try testing.expectEqualStrings("Newest", chip.text);
|
|
905
|
+
try testing.expect(tree.msgFor(chip.id, .toggle) != null);
|
|
906
|
+
const row_item = tree.root.children[3];
|
|
907
|
+
try testing.expectEqual(canvas.WidgetKind.list_item, row_item.kind);
|
|
908
|
+
try testing.expectEqualStrings("folder", row_item.icon);
|
|
909
|
+
const menu_row = tree.root.children[4];
|
|
910
|
+
try testing.expectEqual(canvas.WidgetKind.menu_item, menu_row.kind);
|
|
911
|
+
try testing.expectEqualStrings("trash", menu_row.icon);
|
|
912
|
+
|
|
913
|
+
// The open grammar forms on the inline attribute: app: references
|
|
914
|
+
// carry their full spelling, bound names carry the resolved value -
|
|
915
|
+
// both resolved at draw time through the missing-icon fallback.
|
|
916
|
+
var open_view = try InboxMarkup.init(arena, "<row gap=\"8\">\n <button icon=\"app:wave\" on-press=\"add\">Wave</button>\n <button icon=\"{filter}\" on-press=\"add\">Filter</button>\n</row>");
|
|
917
|
+
var open_ui = InboxUi.init(arena);
|
|
918
|
+
const open_tree = try open_ui.finalize(try open_view.build(&open_ui, &model));
|
|
919
|
+
try testing.expectEqualStrings("app:wave", open_tree.root.children[0].icon);
|
|
920
|
+
try testing.expectEqualStrings("all", open_tree.root.children[1].icon);
|
|
921
|
+
|
|
922
|
+
// Unknown built-in names, unknown namespaces, and out-of-scope
|
|
923
|
+
// elements fail the build with the validator's messages.
|
|
924
|
+
const failing = [_]struct { source: []const u8, message: []const u8 }{
|
|
925
|
+
.{ .source = "<row>\n <button icon=\"sparkle-pony\">Save</button>\n</row>", .message = canvas.ui_markup.button_icon_message },
|
|
926
|
+
.{ .source = "<row>\n <toggle-button icon=\"sparkle-pony\">Bold</toggle-button>\n</row>", .message = canvas.ui_markup.button_icon_message },
|
|
927
|
+
.{ .source = "<row>\n <button icon=\"lib:save\">Save</button>\n</row>", .message = canvas.ui_markup.icon_namespace_message },
|
|
928
|
+
.{ .source = "<row>\n <button icon=\"app:Sparkle Pony\">Save</button>\n</row>", .message = canvas.ui_markup.app_icon_shape_message },
|
|
929
|
+
.{ .source = "<row>\n <badge icon=\"sparkle-pony\">3</badge>\n</row>", .message = canvas.ui_markup.button_icon_message },
|
|
930
|
+
.{ .source = "<column>\n <checkbox icon=\"check\">Done</checkbox>\n</column>", .message = canvas.ui_markup.button_icon_element_message },
|
|
931
|
+
};
|
|
932
|
+
for (failing) |case| {
|
|
933
|
+
var failing_view = try InboxMarkup.init(arena, case.source);
|
|
934
|
+
var failing_ui = InboxUi.init(arena);
|
|
935
|
+
try testing.expectError(error.MarkupBuild, failing_view.build(&failing_ui, &model));
|
|
936
|
+
try testing.expectEqualStrings(case.message, failing_view.diagnostic.message);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
test "markup autofocus binds to focusable controls in both shapes" {
|
|
941
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
942
|
+
defer arena_state.deinit();
|
|
943
|
+
const arena = arena_state.allocator();
|
|
944
|
+
const model = Model{};
|
|
945
|
+
|
|
946
|
+
var view = try InboxMarkup.init(arena, "<column gap=\"8\">\n <text-field autofocus=\"true\" label=\"First\" on-input=\"draft\" />\n <text-field label=\"Second\" on-input=\"draft\" />\n</column>");
|
|
947
|
+
var ui = InboxUi.init(arena);
|
|
948
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
949
|
+
try testing.expect(tree.root.children[0].autofocus);
|
|
950
|
+
try testing.expect(!tree.root.children[1].autofocus);
|
|
951
|
+
|
|
952
|
+
// Non-focusable elements reject the request with the teaching error.
|
|
953
|
+
var failing_view = try InboxMarkup.init(arena, "<column>\n <row autofocus=\"true\">\n <text>x</text>\n </row>\n</column>");
|
|
954
|
+
var failing_ui = InboxUi.init(arena);
|
|
955
|
+
try testing.expectError(error.MarkupBuild, failing_view.build(&failing_ui, &model));
|
|
956
|
+
try testing.expectEqualStrings(canvas.ui_markup.autofocus_element_message, failing_view.diagnostic.message);
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
/// Shared source for the anchored-picker parity tests (interpreter here,
|
|
960
|
+
/// compiled engine in ui_markup_compiled_tests.zig): the sanctioned select
|
|
961
|
+
/// composition with a floating dropdown, dismissal Msg, and a
|
|
962
|
+
/// press-and-hold crumb.
|
|
963
|
+
pub const picker_markup_source =
|
|
964
|
+
\\<column gap="8">
|
|
965
|
+
\\ <stack height="28">
|
|
966
|
+
\\ <select text="Repo" on-press="add"/>
|
|
967
|
+
\\ <dropdown-menu anchor="below" anchor-alignment="stretch" anchor-offset="6" width="160" height="90" on-dismiss="add">
|
|
968
|
+
\\ <menu-item on-press="toggle:{open_count}">Alpha</menu-item>
|
|
969
|
+
\\ </dropdown-menu>
|
|
970
|
+
\\ </stack>
|
|
971
|
+
\\ <button on-press="add" on-hold="add">Crumb</button>
|
|
972
|
+
\\</column>
|
|
973
|
+
;
|
|
974
|
+
|
|
975
|
+
test "markup anchors dropdown-menus and binds dismiss and hold handlers" {
|
|
976
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
977
|
+
defer arena_state.deinit();
|
|
978
|
+
const arena = arena_state.allocator();
|
|
979
|
+
const model = Model{};
|
|
980
|
+
|
|
981
|
+
var view = try InboxMarkup.init(arena, picker_markup_source);
|
|
982
|
+
var ui = InboxUi.init(arena);
|
|
983
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
984
|
+
|
|
985
|
+
const picker_stack = tree.root.children[0];
|
|
986
|
+
const dropdown = picker_stack.children[1];
|
|
987
|
+
try testing.expectEqual(canvas.WidgetKind.dropdown_menu, dropdown.kind);
|
|
988
|
+
const anchor = dropdown.layout.anchor orelse return error.TestUnexpectedResult;
|
|
989
|
+
try testing.expectEqual(canvas.WidgetAnchorPlacement.below, anchor.placement);
|
|
990
|
+
try testing.expectEqual(canvas.WidgetAnchorAlignment.stretch, anchor.alignment);
|
|
991
|
+
try testing.expectEqual(@as(f32, 6), anchor.offset);
|
|
992
|
+
try testing.expect(tree.msgFor(dropdown.id, .dismiss) != null);
|
|
993
|
+
|
|
994
|
+
const crumb = tree.root.children[1];
|
|
995
|
+
try testing.expect(tree.msgFor(crumb.id, .hold) != null);
|
|
996
|
+
// A hold handler makes the element pressable, like on-press.
|
|
997
|
+
try testing.expect(crumb.semantics.actions.press);
|
|
998
|
+
|
|
999
|
+
// Misplaced and malformed anchor/dismiss attributes fail the build
|
|
1000
|
+
// with the validator's teaching messages.
|
|
1001
|
+
const failing = [_]struct { source: []const u8, message: []const u8 }{
|
|
1002
|
+
.{ .source = "<row>\n <button anchor=\"below\">Save</button>\n</row>", .message = canvas.ui_markup.anchor_element_message },
|
|
1003
|
+
.{ .source = "<row>\n <dropdown-menu anchor=\"sideways\"><menu-item on-press=\"add\">A</menu-item></dropdown-menu>\n</row>", .message = canvas.ui_markup.anchor_value_message },
|
|
1004
|
+
.{ .source = "<row>\n <dropdown-menu anchor-alignment=\"end\"><menu-item on-press=\"add\">A</menu-item></dropdown-menu>\n</row>", .message = canvas.ui_markup.anchor_dependent_attr_message },
|
|
1005
|
+
.{ .source = "<row>\n <dropdown-menu anchor=\"below\" anchor-offset=\"lots\"><menu-item on-press=\"add\">A</menu-item></dropdown-menu>\n</row>", .message = canvas.ui_markup.anchor_offset_value_message },
|
|
1006
|
+
.{ .source = "<row>\n <button on-dismiss=\"add\">Save</button>\n</row>", .message = canvas.ui_markup.on_dismiss_element_message },
|
|
1007
|
+
};
|
|
1008
|
+
for (failing) |case| {
|
|
1009
|
+
var failing_view = try InboxMarkup.init(arena, case.source);
|
|
1010
|
+
var failing_ui = InboxUi.init(arena);
|
|
1011
|
+
try testing.expectError(error.MarkupBuild, failing_view.build(&failing_ui, &model));
|
|
1012
|
+
try testing.expectEqualStrings(case.message, failing_view.diagnostic.message);
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// The validator teaches the same rules through `markup check`.
|
|
1016
|
+
for (failing) |case| {
|
|
1017
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
1018
|
+
const document = try parser.parse();
|
|
1019
|
+
const diagnostic = canvas.ui_markup.validate(document) orelse return error.TestUnexpectedResult;
|
|
1020
|
+
try testing.expectEqualStrings(case.message, diagnostic.message);
|
|
1021
|
+
}
|
|
1022
|
+
var good_parser = canvas.ui_markup.Parser.init(arena, picker_markup_source);
|
|
1023
|
+
const good_document = try good_parser.parse();
|
|
1024
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(good_document));
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
pub const context_menu_markup_source =
|
|
1028
|
+
\\<column gap="4">
|
|
1029
|
+
\\ <for each="visible" key="id" as="t">
|
|
1030
|
+
\\ <list-item on-press="toggle:{t.id}" padding="6" label="{t.title}">
|
|
1031
|
+
\\ <text grow="1">{t.title}</text>
|
|
1032
|
+
\\ <context-menu>
|
|
1033
|
+
\\ <if test="{t.done}">
|
|
1034
|
+
\\ <menu-item on-press="toggle:{t.id}">Reopen</menu-item>
|
|
1035
|
+
\\ </if>
|
|
1036
|
+
\\ <else>
|
|
1037
|
+
\\ <menu-item on-press="toggle:{t.id}">Complete</menu-item>
|
|
1038
|
+
\\ </else>
|
|
1039
|
+
\\ <separator />
|
|
1040
|
+
\\ <menu-item on-press="add" disabled="{t.done}">Add Another</menu-item>
|
|
1041
|
+
\\ </context-menu>
|
|
1042
|
+
\\ </list-item>
|
|
1043
|
+
\\ </for>
|
|
1044
|
+
\\</column>
|
|
1045
|
+
;
|
|
1046
|
+
|
|
1047
|
+
test "markup context-menus lower to declared platform-menu items on their host" {
|
|
1048
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1049
|
+
defer arena_state.deinit();
|
|
1050
|
+
const arena = arena_state.allocator();
|
|
1051
|
+
const model = testModel();
|
|
1052
|
+
|
|
1053
|
+
var view = try InboxMarkup.init(arena, context_menu_markup_source);
|
|
1054
|
+
var ui = InboxUi.init(arena);
|
|
1055
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
1056
|
+
|
|
1057
|
+
// Three tasks, three rows; the menu is metadata, never a flow
|
|
1058
|
+
// child — each row renders exactly its one text child.
|
|
1059
|
+
const list = tree.root;
|
|
1060
|
+
try testing.expectEqual(@as(usize, 3), list.children.len);
|
|
1061
|
+
for (list.children) |row| {
|
|
1062
|
+
try testing.expectEqual(canvas.WidgetKind.list_item, row.kind);
|
|
1063
|
+
try testing.expectEqual(@as(usize, 1), row.children.len);
|
|
1064
|
+
try testing.expectEqual(canvas.WidgetKind.text, row.children[0].kind);
|
|
1065
|
+
try testing.expectEqual(@as(usize, 3), row.context_menu.len);
|
|
1066
|
+
try testing.expect(row.context_menu[1].separator);
|
|
1067
|
+
try testing.expectEqualStrings("Add Another", row.context_menu[2].label);
|
|
1068
|
+
}
|
|
1069
|
+
// Row 1 ("Ship IR", open): Complete leads and every item is live.
|
|
1070
|
+
const open_row = list.children[0];
|
|
1071
|
+
try testing.expectEqualStrings("Complete", open_row.context_menu[0].label);
|
|
1072
|
+
try testing.expect(open_row.context_menu[0].enabled);
|
|
1073
|
+
try testing.expect(open_row.context_menu[2].enabled);
|
|
1074
|
+
// Row 2 ("Write decisions", done): the if swapped the first item and
|
|
1075
|
+
// the binding disabled the last.
|
|
1076
|
+
const done_row = list.children[1];
|
|
1077
|
+
try testing.expectEqualStrings("Reopen", done_row.context_menu[0].label);
|
|
1078
|
+
try testing.expect(!done_row.context_menu[2].enabled);
|
|
1079
|
+
|
|
1080
|
+
// Selections resolve through the handler table with typed payloads —
|
|
1081
|
+
// the SAME entry native picks and the anchored fallback both use.
|
|
1082
|
+
const toggle_msg = tree.msgForContextMenu(done_row.id, 0) orelse return error.TestUnexpectedResult;
|
|
1083
|
+
try testing.expectEqual(Msg{ .toggle = 2 }, toggle_msg);
|
|
1084
|
+
const add_msg = tree.msgForContextMenu(open_row.id, 2) orelse return error.TestUnexpectedResult;
|
|
1085
|
+
try testing.expectEqual(Msg.add, add_msg);
|
|
1086
|
+
// The separator slot is inert.
|
|
1087
|
+
try testing.expectEqual(@as(?Msg, null), tree.msgForContextMenu(open_row.id, 1));
|
|
1088
|
+
|
|
1089
|
+
// Teaching errors, engine and validator agreeing on the message.
|
|
1090
|
+
const failing = [_]struct { source: []const u8, message: []const u8 }{
|
|
1091
|
+
.{ .source = "<row>\n <context-menu><menu-item on-press=\"add\">A</menu-item></context-menu>\n</row>", .message = canvas.ui_markup.context_menu_host_message },
|
|
1092
|
+
.{ .source = "<column>\n <if test=\"{open_count}\">\n <context-menu><menu-item on-press=\"add\">A</menu-item></context-menu>\n </if>\n</column>", .message = canvas.ui_markup.context_menu_parent_message },
|
|
1093
|
+
.{ .source = "<column>\n <button>Save<context-menu><menu-item on-press=\"add\">A</menu-item></context-menu><context-menu><menu-item on-press=\"add\">B</menu-item></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_single_message },
|
|
1094
|
+
.{ .source = "<column>\n <button>Save<context-menu anchor=\"below\"><menu-item on-press=\"add\">A</menu-item></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_attrs_message },
|
|
1095
|
+
.{ .source = "<column>\n <button>Save<context-menu><button on-press=\"add\">A</button></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_children_message },
|
|
1096
|
+
.{ .source = "<column>\n <button>Save<context-menu></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_empty_message },
|
|
1097
|
+
.{ .source = "<column>\n <button>Save<context-menu><menu-item>A</menu-item></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_item_press_message },
|
|
1098
|
+
.{ .source = "<column>\n <button>Save<context-menu><menu-item on-press=\"add\" icon=\"copy\">A</menu-item></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_item_attr_message },
|
|
1099
|
+
.{ .source = "<column>\n <button>Save<context-menu><menu-item on-press=\"add\" /></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_item_label_message },
|
|
1100
|
+
.{ .source = "<column>\n <button>Save<context-menu><menu-item on-press=\"add\">A</menu-item><separator gap=\"2\" /></context-menu></button>\n</column>", .message = canvas.ui_markup.context_menu_separator_message },
|
|
1101
|
+
};
|
|
1102
|
+
for (failing) |case| {
|
|
1103
|
+
var failing_view = try InboxMarkup.init(arena, case.source);
|
|
1104
|
+
var failing_ui = InboxUi.init(arena);
|
|
1105
|
+
try testing.expectError(error.MarkupBuild, failing_view.build(&failing_ui, &model));
|
|
1106
|
+
try testing.expectEqualStrings(case.message, failing_view.diagnostic.message);
|
|
1107
|
+
}
|
|
1108
|
+
for (failing) |case| {
|
|
1109
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
1110
|
+
const document = try parser.parse();
|
|
1111
|
+
const diagnostic = canvas.ui_markup.validate(document) orelse return error.TestUnexpectedResult;
|
|
1112
|
+
try testing.expectEqualStrings(case.message, diagnostic.message);
|
|
1113
|
+
}
|
|
1114
|
+
var good_parser = canvas.ui_markup.Parser.init(arena, context_menu_markup_source);
|
|
1115
|
+
const good_document = try good_parser.parse();
|
|
1116
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(good_document));
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
test "the schema registry's icon section matches the comptime icon registry" {
|
|
1120
|
+
// ui_schema.zig is std-only (ui_markup.zig doubles as the LSP's module
|
|
1121
|
+
// root), so its icon section is a data mirror of the comptime-parsed
|
|
1122
|
+
// icon registry; this conformance test keeps the two in lockstep.
|
|
1123
|
+
try testing.expectEqual(canvas.icons.known_icon_names.len, canvas.ui_markup.known_icon_names.len);
|
|
1124
|
+
for (canvas.ui_markup.known_icon_names) |name| {
|
|
1125
|
+
try testing.expect(canvas.icons.find(name) != null);
|
|
1126
|
+
}
|
|
1127
|
+
for (canvas.icons.known_icon_names) |name| {
|
|
1128
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.known_icon_names));
|
|
1129
|
+
}
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
test "the registry's element vocabulary resolves through the interpreter" {
|
|
1133
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1134
|
+
try testing.expect(markup_view.elementKind(name) != null);
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
test "the registry's attr value classes match the ElementOptions field types" {
|
|
1139
|
+
// The registry (and the contract rules derived from it) states the
|
|
1140
|
+
// attribute value classes as data; the engines derive them from the
|
|
1141
|
+
// real field types in setOptionField. This conformance test holds the
|
|
1142
|
+
// two readings equal, so the registry, the check-time pass, and the
|
|
1143
|
+
// build-time pass cannot drift.
|
|
1144
|
+
const contract = canvas.ui_markup.contract;
|
|
1145
|
+
inline for (markup_view.attr_names) |name| {
|
|
1146
|
+
const FieldType = @FieldType(InboxUi.ElementOptions, name.zig);
|
|
1147
|
+
const expected: contract.AttrClass = switch (@typeInfo(FieldType)) {
|
|
1148
|
+
.float => .number,
|
|
1149
|
+
.int => .whole,
|
|
1150
|
+
.bool, .optional => .truthy,
|
|
1151
|
+
.@"enum" => .option,
|
|
1152
|
+
.pointer => .text,
|
|
1153
|
+
else => unreachable,
|
|
1154
|
+
};
|
|
1155
|
+
var found = false;
|
|
1156
|
+
for (contract.attr_kind_rules) |rule| {
|
|
1157
|
+
if (std.mem.eql(u8, rule.name, name.markup)) {
|
|
1158
|
+
found = true;
|
|
1159
|
+
try testing.expectEqual(expected, rule.class);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
try testing.expect(found);
|
|
1163
|
+
}
|
|
1164
|
+
// Every rule names a real option attribute (dead table entries would
|
|
1165
|
+
// silently check nothing).
|
|
1166
|
+
for (contract.attr_kind_rules) |rule| {
|
|
1167
|
+
var known = false;
|
|
1168
|
+
inline for (markup_view.attr_names) |name| {
|
|
1169
|
+
if (std.mem.eql(u8, rule.name, name.markup)) known = true;
|
|
1170
|
+
}
|
|
1171
|
+
try testing.expect(known);
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
|
|
1175
|
+
test "the registry's hit-target predicate matches the engine's" {
|
|
1176
|
+
// The engine predicate (canvas.widgetKindHitTarget, which the runtime's
|
|
1177
|
+
// pointer dispatch and both markup engines use) is the source of truth;
|
|
1178
|
+
// the registry's std-only predicate data must mirror it exactly so an
|
|
1179
|
+
// element can never accept a handler the runtime would never fire.
|
|
1180
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1181
|
+
const kind = markup_view.elementKind(name).?;
|
|
1182
|
+
try testing.expectEqual(
|
|
1183
|
+
!canvas.widgetKindHitTarget(kind),
|
|
1184
|
+
nameListed(name, &canvas.ui_markup.known_non_hit_target_element_names),
|
|
1185
|
+
);
|
|
1186
|
+
}
|
|
1187
|
+
// Every listed non-hit-target name is a known element.
|
|
1188
|
+
for (canvas.ui_markup.known_non_hit_target_element_names) |name| {
|
|
1189
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.known_element_names));
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
|
|
1193
|
+
test "the registry's stacking predicate matches the engine's" {
|
|
1194
|
+
// The engine predicate (canvas.widgetKindStacksChildren, which the
|
|
1195
|
+
// layout pass, the builder's Debug gap diagnostic, and both markup
|
|
1196
|
+
// engines use) is the source of truth; the registry's std-only
|
|
1197
|
+
// predicate data must mirror it exactly so an element can never
|
|
1198
|
+
// accept a gap the layout would never apply.
|
|
1199
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1200
|
+
const kind = markup_view.elementKind(name).?;
|
|
1201
|
+
try testing.expectEqual(
|
|
1202
|
+
canvas.widgetKindStacksChildren(kind),
|
|
1203
|
+
nameListed(name, &canvas.ui_markup.known_stack_container_element_names),
|
|
1204
|
+
);
|
|
1205
|
+
}
|
|
1206
|
+
// Every listed stack-container name is a known element.
|
|
1207
|
+
for (canvas.ui_markup.known_stack_container_element_names) |name| {
|
|
1208
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.known_element_names));
|
|
1209
|
+
}
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
test "the registry's role vocabulary matches the live WidgetRole enum" {
|
|
1213
|
+
// role="..." values resolve through std.meta.stringToEnum on
|
|
1214
|
+
// canvas.WidgetRole; the registry's std-only mirror must list exactly
|
|
1215
|
+
// the same names so the validator and the engines accept identically.
|
|
1216
|
+
const fields = std.meta.fields(canvas.WidgetRole);
|
|
1217
|
+
try testing.expectEqual(fields.len, canvas.ui_markup.schema.role_names.len);
|
|
1218
|
+
inline for (fields) |field| {
|
|
1219
|
+
try testing.expect(nameListed(field.name, &canvas.ui_markup.schema.role_names));
|
|
1220
|
+
}
|
|
1221
|
+
// Container roles are a subset of the vocabulary.
|
|
1222
|
+
for (canvas.ui_markup.schema.container_role_names) |name| {
|
|
1223
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.schema.role_names));
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
test "the registry's a11y name classes match the engine's control predicates" {
|
|
1228
|
+
// The lint's element classes are registry data; hold them to the
|
|
1229
|
+
// canvas layer's own predicates so an element the engine treats as
|
|
1230
|
+
// an operable control can never skip the accessible-name lint.
|
|
1231
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1232
|
+
const entry = canvas.ui_markup.schema.elementByName(name).?;
|
|
1233
|
+
const kind = markup_view.elementKind(name).?;
|
|
1234
|
+
// Editable is EXACTLY the text-input kinds plus select (which
|
|
1235
|
+
// announces its placeholder while empty).
|
|
1236
|
+
try testing.expectEqual(
|
|
1237
|
+
canvas.widgetTextInputKind(kind) or kind == .select,
|
|
1238
|
+
entry.a11y_name == .editable,
|
|
1239
|
+
);
|
|
1240
|
+
// Every control-class element is a press-claiming hit target
|
|
1241
|
+
// (the engine's definition of an operable leaf control).
|
|
1242
|
+
if (entry.a11y_name == .control) {
|
|
1243
|
+
try testing.expect(canvas.widgetKindHitTarget(kind));
|
|
1244
|
+
try testing.expect(canvas.widgetKindClaimsPress(kind));
|
|
1245
|
+
}
|
|
1246
|
+
// Image-class elements are announced under the image role.
|
|
1247
|
+
if (entry.a11y_name == .image) {
|
|
1248
|
+
try testing.expectEqual(canvas.WidgetKind.avatar, kind);
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
|
|
1253
|
+
test "the registry's dismissible predicate matches the engine's dismissal machinery" {
|
|
1254
|
+
// Every registry-dismissible element must lower to a widget kind the
|
|
1255
|
+
// runtime's dismissal machinery (Escape, click outside, automation
|
|
1256
|
+
// dismiss) actually closes — otherwise an on-dismiss the validator
|
|
1257
|
+
// accepted could never fire. The engine set is wider on purpose
|
|
1258
|
+
// (popover/menu-surface stay Zig views), so this is one-directional.
|
|
1259
|
+
for (canvas.ui_markup.known_dismiss_element_names) |name| {
|
|
1260
|
+
const kind = markup_view.elementKind(name).?;
|
|
1261
|
+
try testing.expect(canvas.widgetKindDismissibleSurface(kind));
|
|
1262
|
+
}
|
|
1263
|
+
// ...and every markup element whose kind is dismissible is either
|
|
1264
|
+
// listed or a documented exception (the tooltip LEAF shares the
|
|
1265
|
+
// dismissible tooltip kind, but markup tooltips are static text with
|
|
1266
|
+
// no model-owned open flag for an on-dismiss to clear).
|
|
1267
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1268
|
+
const kind = markup_view.elementKind(name).?;
|
|
1269
|
+
if (canvas.widgetKindDismissibleSurface(kind) and kind != .tooltip) {
|
|
1270
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.known_dismiss_element_names));
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
|
|
1275
|
+
test "the registry's takes-text predicate matches the interpreter's takes-text set" {
|
|
1276
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1277
|
+
const kind = markup_view.elementKind(name).?;
|
|
1278
|
+
try testing.expectEqual(
|
|
1279
|
+
markup_view.elementTakesText(kind),
|
|
1280
|
+
nameListed(name, &canvas.ui_markup.known_text_leaf_element_names),
|
|
1281
|
+
);
|
|
1282
|
+
}
|
|
1283
|
+
// Every listed text leaf is a known element.
|
|
1284
|
+
for (canvas.ui_markup.known_text_leaf_element_names) |name| {
|
|
1285
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.known_element_names));
|
|
1286
|
+
}
|
|
1287
|
+
}
|
|
1288
|
+
|
|
1289
|
+
test "the registry's takes-children predicate matches the interpreter's takes-children set" {
|
|
1290
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1291
|
+
const kind = markup_view.elementKind(name).?;
|
|
1292
|
+
try testing.expectEqual(
|
|
1293
|
+
markup_view.elementTakesChildren(kind),
|
|
1294
|
+
nameListed(name, &canvas.ui_markup.known_text_or_children_element_names),
|
|
1295
|
+
);
|
|
1296
|
+
}
|
|
1297
|
+
// Every text-or-children element also takes text (the flag refines
|
|
1298
|
+
// the text leaf, it never stands alone) and is a known element.
|
|
1299
|
+
for (canvas.ui_markup.known_text_or_children_element_names) |name| {
|
|
1300
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.known_element_names));
|
|
1301
|
+
try testing.expect(nameListed(name, &canvas.ui_markup.known_text_leaf_element_names));
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
/// Widget kinds deliberately NOT expressible in markup v1 — each needs
|
|
1306
|
+
/// something the closed grammar cannot carry, so these are written as Zig
|
|
1307
|
+
/// view functions instead of forcing a bad markup shape:
|
|
1308
|
+
/// - image, icon_button: reference image assets by runtime ImageId,
|
|
1309
|
+
/// which markup's literal/binding attribute values cannot express.
|
|
1310
|
+
/// (icon IS expressible: the built-in vector set is a closed literal
|
|
1311
|
+
/// vocabulary, comptime-validated.)
|
|
1312
|
+
/// - data_grid: a virtualized data grid needs per-column cell templates
|
|
1313
|
+
/// (arbitrary render callbacks).
|
|
1314
|
+
/// - popover, menu_surface: floating surfaces anchored to runtime geometry
|
|
1315
|
+
/// the static tree cannot express (dropdown-menu covers the declarative
|
|
1316
|
+
/// menu case).
|
|
1317
|
+
/// - segmented_control: engine kind for shell chrome segments; tabs and
|
|
1318
|
+
/// toggle-group cover the component catalog's use cases.
|
|
1319
|
+
/// - chart: expressible as the `<chart>` COMPOSITE (series children whose
|
|
1320
|
+
/// values bind model f32 iterables, lowered through `Ui.chart`), so no
|
|
1321
|
+
/// plain element maps to the kind here — like the other composites, the
|
|
1322
|
+
/// bespoke builder is the channel, not the element table.
|
|
1323
|
+
/// - split_divider: never authored — the builder synthesizes the drag
|
|
1324
|
+
/// handle between a split's two panes, so both markup engines get it
|
|
1325
|
+
/// through the same finalize that Zig views do.
|
|
1326
|
+
/// - input_group: expressible as the `<input-group>` COMPOSITE (one
|
|
1327
|
+
/// textarea child plus an optional `<input-group-actions>` row,
|
|
1328
|
+
/// lowered through `Ui.inputGroup`), so no plain element maps to the
|
|
1329
|
+
/// kind here — like chart, the bespoke builder is the channel.
|
|
1330
|
+
const markup_excluded_widget_kinds = [_]canvas.WidgetKind{
|
|
1331
|
+
.image, .icon_button, .data_grid, .popover, .menu_surface, .segmented_control, .chart, .split_divider, .input_group,
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1334
|
+
fn kindExpressible(kind: canvas.WidgetKind) bool {
|
|
1335
|
+
for (canvas.ui_markup.known_element_names) |name| {
|
|
1336
|
+
if (markup_view.elementKind(name) == kind) return true;
|
|
1337
|
+
}
|
|
1338
|
+
return false;
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
test "known_element_names covers every markup-expressible widget kind" {
|
|
1342
|
+
// Exactly the excluded kinds are inexpressible: a new widget kind must
|
|
1343
|
+
// either get a markup element or a documented exclusion above.
|
|
1344
|
+
for (std.enums.values(canvas.WidgetKind)) |kind| {
|
|
1345
|
+
const excluded = std.mem.indexOfScalar(canvas.WidgetKind, &markup_excluded_widget_kinds, kind) != null;
|
|
1346
|
+
try testing.expectEqual(!excluded, kindExpressible(kind));
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
test "every built-in component is expressible in markup" {
|
|
1351
|
+
for (canvas.builtin_component_kinds) |component| {
|
|
1352
|
+
const descriptor = canvas.builtinComponentDescriptor(component);
|
|
1353
|
+
try testing.expect(kindExpressible(descriptor.root_widget_kind));
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
// ---------------------------------------------- arena-scalar binding fixture
|
|
1358
|
+
|
|
1359
|
+
pub const Expense = struct {
|
|
1360
|
+
id: u32,
|
|
1361
|
+
cents: u32,
|
|
1362
|
+
|
|
1363
|
+
/// Arena-taking item method: formats into the build arena, so the
|
|
1364
|
+
/// string lives exactly as long as the built tree.
|
|
1365
|
+
pub fn amount(expense: *const Expense, arena: std.mem.Allocator) []const u8 {
|
|
1366
|
+
return std.fmt.allocPrint(arena, "${d}.{d:0>2}", .{ expense.cents / 100, expense.cents % 100 }) catch "";
|
|
1367
|
+
}
|
|
1368
|
+
};
|
|
1369
|
+
|
|
1370
|
+
pub const ExpensesMsg = union(enum) {
|
|
1371
|
+
pick: []const u8,
|
|
1372
|
+
refresh,
|
|
1373
|
+
};
|
|
1374
|
+
|
|
1375
|
+
pub const ExpensesModel = struct {
|
|
1376
|
+
expenses: []const Expense = &.{},
|
|
1377
|
+
filter: []const u8 = "all",
|
|
1378
|
+
|
|
1379
|
+
/// Arena-taking scalar binding: `{summary}` binds this directly — no
|
|
1380
|
+
/// one-element `<for>` needed.
|
|
1381
|
+
pub fn summary(model: *const ExpensesModel, arena: std.mem.Allocator) []const u8 {
|
|
1382
|
+
var total: u32 = 0;
|
|
1383
|
+
for (model.expenses) |expense| total += expense.cents;
|
|
1384
|
+
return std.fmt.allocPrint(arena, "{d} expenses · ${d}.{d:0>2}", .{ model.expenses.len, total / 100, total % 100 }) catch "";
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1388
|
+
/// Arena scalars everywhere a scalar binding works: text interpolation
|
|
1389
|
+
/// (mixed with other bindings), attribute values (label), message
|
|
1390
|
+
/// payloads, if-test truthiness, and item-level arena methods.
|
|
1391
|
+
pub const expenses_markup_source =
|
|
1392
|
+
\\<column gap="8">
|
|
1393
|
+
\\ <for each="expenses" key="id" as="e">
|
|
1394
|
+
\\ <row gap="4">
|
|
1395
|
+
\\ <text grow="1">{e.amount}</text>
|
|
1396
|
+
\\ <button size="sm" on-press="pick:{e.amount}">Pick</button>
|
|
1397
|
+
\\ </row>
|
|
1398
|
+
\\ </for>
|
|
1399
|
+
\\ <if test="{summary}">
|
|
1400
|
+
\\ <badge>summarized</badge>
|
|
1401
|
+
\\ </if>
|
|
1402
|
+
\\ <text label="{summary}">{filter}: {summary}</text>
|
|
1403
|
+
\\ <status-bar>{summary}</status-bar>
|
|
1404
|
+
\\</column>
|
|
1405
|
+
;
|
|
1406
|
+
|
|
1407
|
+
pub const ExpensesUi = canvas.Ui(ExpensesMsg);
|
|
1408
|
+
|
|
1409
|
+
fn expenseRow(ui: *ExpensesUi, expense: *const Expense) ExpensesUi.Node {
|
|
1410
|
+
return ui.row(.{ .gap = 4 }, .{
|
|
1411
|
+
ui.text(.{ .grow = 1 }, expense.amount(ui.arena)),
|
|
1412
|
+
ui.button(.{ .size = .sm, .on_press = ExpensesMsg{ .pick = expense.amount(ui.arena) } }, "Pick"),
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
|
|
1416
|
+
fn expenseKey(expense: *const Expense) canvas.UiKey {
|
|
1417
|
+
return canvas.uiKey(expense.id);
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
fn expensesBadge(ui: *ExpensesUi) ExpensesUi.Node {
|
|
1421
|
+
var node = ui.el(.badge, .{}, .{});
|
|
1422
|
+
node.widget.text = "summarized";
|
|
1423
|
+
return node;
|
|
1424
|
+
}
|
|
1425
|
+
|
|
1426
|
+
/// The hand-written equivalent of the arena-scalar markup: parity means
|
|
1427
|
+
/// both engines build exactly this.
|
|
1428
|
+
pub fn handExpensesView(ui: *ExpensesUi, model: *const ExpensesModel) ExpensesUi.Node {
|
|
1429
|
+
return ui.column(.{ .gap = 8 }, .{
|
|
1430
|
+
ui.each(model.expenses, expenseKey, expenseRow),
|
|
1431
|
+
expensesBadge(ui),
|
|
1432
|
+
ui.text(
|
|
1433
|
+
.{ .semantics = .{ .label = model.summary(ui.arena) } },
|
|
1434
|
+
ui.fmt("{s}: {s}", .{ model.filter, model.summary(ui.arena) }),
|
|
1435
|
+
),
|
|
1436
|
+
ui.statusBar(.{}, model.summary(ui.arena)),
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
pub fn expensesTestModel() ExpensesModel {
|
|
1441
|
+
return .{
|
|
1442
|
+
.expenses = &[_]Expense{
|
|
1443
|
+
.{ .id = 1, .cents = 1234 },
|
|
1444
|
+
.{ .id = 2, .cents = 60 },
|
|
1445
|
+
},
|
|
1446
|
+
};
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
test "arena-taking scalar bindings work in interpolation, attributes, payloads, and if tests" {
|
|
1450
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1451
|
+
defer arena_state.deinit();
|
|
1452
|
+
const arena = arena_state.allocator();
|
|
1453
|
+
|
|
1454
|
+
const model = expensesTestModel();
|
|
1455
|
+
const ExpensesMarkup = markup_view.MarkupView(ExpensesModel, ExpensesMsg);
|
|
1456
|
+
|
|
1457
|
+
var view = try ExpensesMarkup.init(arena, expenses_markup_source);
|
|
1458
|
+
var markup_ui = ExpensesUi.init(arena);
|
|
1459
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
1460
|
+
|
|
1461
|
+
var hand_ui = ExpensesUi.init(arena);
|
|
1462
|
+
const hand_tree = try hand_ui.finalize(handExpensesView(&hand_ui, &model));
|
|
1463
|
+
|
|
1464
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
1465
|
+
defer markup_ids.deinit(testing.allocator);
|
|
1466
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
1467
|
+
defer hand_ids.deinit(testing.allocator);
|
|
1468
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
1469
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
1470
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
1471
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
1472
|
+
|
|
1473
|
+
// The scalar binds directly — text content and interpolation.
|
|
1474
|
+
try testing.expectEqualStrings("2 expenses · $12.94", findByKind(markup_tree.root, .status_bar).?.text);
|
|
1475
|
+
const labeled = findByText(markup_tree.root, .text, "all: 2 expenses · $12.94").?;
|
|
1476
|
+
// Attribute values (accessible label).
|
|
1477
|
+
try testing.expectEqualStrings("2 expenses · $12.94", labeled.semantics.label);
|
|
1478
|
+
// Item-level arena methods.
|
|
1479
|
+
try testing.expect(findByText(markup_tree.root, .text, "$12.34") != null);
|
|
1480
|
+
try testing.expect(findByText(markup_tree.root, .text, "$0.60") != null);
|
|
1481
|
+
// If-test truthiness on an arena scalar (non-empty string).
|
|
1482
|
+
try testing.expect(findByText(markup_tree.root, .badge, "summarized") != null);
|
|
1483
|
+
|
|
1484
|
+
// Message payloads carry the arena string; it lives while the tree
|
|
1485
|
+
// does (the build arena outlives dispatch between rebuilds).
|
|
1486
|
+
const pick_button = findByKind(markup_tree.root, .button).?;
|
|
1487
|
+
try testing.expectEqualStrings("$12.34", markup_tree.msgForPointer(pick_button.id, .up).?.pick);
|
|
1488
|
+
}
|
|
1489
|
+
|
|
1490
|
+
test "arena scalars are rejected inside equality with a teaching error" {
|
|
1491
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1492
|
+
defer arena_state.deinit();
|
|
1493
|
+
const arena = arena_state.allocator();
|
|
1494
|
+
const model = expensesTestModel();
|
|
1495
|
+
const ExpensesMarkup = markup_view.MarkupView(ExpensesModel, ExpensesMsg);
|
|
1496
|
+
|
|
1497
|
+
const cases = [_][]const u8{
|
|
1498
|
+
"<column>\n <badge selected=\"{summary == filter}\">x</badge>\n</column>",
|
|
1499
|
+
"<column>\n <badge selected=\"{filter == summary}\">x</badge>\n</column>",
|
|
1500
|
+
"<column>\n <if test=\"{summary == filter}\"><text>x</text></if>\n</column>",
|
|
1501
|
+
};
|
|
1502
|
+
for (cases) |source| {
|
|
1503
|
+
var view = try ExpensesMarkup.init(arena, source);
|
|
1504
|
+
var ui = ExpensesUi.init(arena);
|
|
1505
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
1506
|
+
try testing.expectEqualStrings(canvas.ui_markup.arena_scalar_equality_message, view.diagnostic.message);
|
|
1507
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
|
|
1511
|
+
test "string-producing bindings pass to templates as value args" {
|
|
1512
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1513
|
+
defer arena_state.deinit();
|
|
1514
|
+
const arena = arena_state.allocator();
|
|
1515
|
+
const model = expensesTestModel();
|
|
1516
|
+
const ExpensesMarkup = markup_view.MarkupView(ExpensesModel, ExpensesMsg);
|
|
1517
|
+
|
|
1518
|
+
// Both a string field (filter) and an arena scalar (summary) bind as
|
|
1519
|
+
// scalar value args — never as iterables of bytes.
|
|
1520
|
+
const source =
|
|
1521
|
+
"<template name=\"line\" args=\"title\"><text>{title}</text></template>\n" ++
|
|
1522
|
+
"<column>\n" ++
|
|
1523
|
+
" <use template=\"line\" title=\"{filter}\" />\n" ++
|
|
1524
|
+
" <use template=\"line\" title=\"{summary}\" />\n" ++
|
|
1525
|
+
"</column>";
|
|
1526
|
+
var view = try ExpensesMarkup.init(arena, source);
|
|
1527
|
+
var ui = ExpensesUi.init(arena);
|
|
1528
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
1529
|
+
try testing.expect(findByText(tree.root, .text, "all") != null);
|
|
1530
|
+
try testing.expect(findByText(tree.root, .text, "2 expenses · $12.94") != null);
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
// --------------------------------------------------- markdown element fixture
|
|
1534
|
+
|
|
1535
|
+
pub const DocMsg = union(enum) {
|
|
1536
|
+
open_url: []const u8,
|
|
1537
|
+
toggle_details: usize,
|
|
1538
|
+
refresh,
|
|
1539
|
+
};
|
|
1540
|
+
|
|
1541
|
+
pub const doc_body_source =
|
|
1542
|
+
\\## Release
|
|
1543
|
+
\\
|
|
1544
|
+
\\Read [the guide](https://example.com/guide) before shipping.
|
|
1545
|
+
\\
|
|
1546
|
+
\\Tracked in #12, see https://status.example.com.
|
|
1547
|
+
\\
|
|
1548
|
+
\\<details>
|
|
1549
|
+
\\<summary>Rollout</summary>
|
|
1550
|
+
\\
|
|
1551
|
+
\\Enable for 5% of traffic.
|
|
1552
|
+
\\
|
|
1553
|
+
\\</details>
|
|
1554
|
+
;
|
|
1555
|
+
|
|
1556
|
+
pub const DocModel = struct {
|
|
1557
|
+
body: []const u8 = doc_body_source,
|
|
1558
|
+
details_expanded: [2]bool = .{ false, false },
|
|
1559
|
+
opened_count: usize = 0,
|
|
1560
|
+
issue_base: []const u8 = "ghissue://",
|
|
1561
|
+
|
|
1562
|
+
/// Arena scalar as a markdown source: composed at view time.
|
|
1563
|
+
pub fn banner(model: *const DocModel, arena: std.mem.Allocator) []const u8 {
|
|
1564
|
+
return std.fmt.allocPrint(arena, "**{d}** links opened", .{model.opened_count}) catch "";
|
|
1565
|
+
}
|
|
1566
|
+
};
|
|
1567
|
+
|
|
1568
|
+
pub const doc_markup_source =
|
|
1569
|
+
\\<column gap="8">
|
|
1570
|
+
\\ <markdown source="{body}" on-link="open_url" on-details="toggle_details" details-expanded="{details_expanded}" issue-link-base="{issue_base}" />
|
|
1571
|
+
\\ <markdown source="{banner}" />
|
|
1572
|
+
\\</column>
|
|
1573
|
+
;
|
|
1574
|
+
|
|
1575
|
+
pub const DocUi = canvas.Ui(DocMsg);
|
|
1576
|
+
const DocMd = canvas.markdown.Markdown(DocMsg);
|
|
1577
|
+
|
|
1578
|
+
/// The hand-written equivalent of the markdown markup: both engines must
|
|
1579
|
+
/// build exactly what direct `Md.view` calls produce.
|
|
1580
|
+
pub fn handDocView(ui: *DocUi, model: *const DocModel) DocUi.Node {
|
|
1581
|
+
return ui.column(.{ .gap = 8 }, .{
|
|
1582
|
+
DocMd.view(ui, model.body, .{
|
|
1583
|
+
.on_link = DocUi.linkMsg(.open_url),
|
|
1584
|
+
.on_details = DocMd.detailsMsg(.toggle_details),
|
|
1585
|
+
.details_expanded = &model.details_expanded,
|
|
1586
|
+
.issue_link_base = model.issue_base,
|
|
1587
|
+
}),
|
|
1588
|
+
DocMd.view(ui, model.banner(ui.arena), .{}),
|
|
1589
|
+
});
|
|
1590
|
+
}
|
|
1591
|
+
|
|
1592
|
+
/// The link payload of the span whose text is exactly `span_text`, found
|
|
1593
|
+
/// anywhere in the subtree; null when no such linked span exists.
|
|
1594
|
+
pub fn findSpanLink(widget: canvas.Widget, span_text: []const u8) ?[]const u8 {
|
|
1595
|
+
for (widget.spans) |span| {
|
|
1596
|
+
if (span.link.len > 0 and std.mem.eql(u8, span.text, span_text)) return span.link;
|
|
1597
|
+
}
|
|
1598
|
+
for (widget.children) |child| {
|
|
1599
|
+
if (findSpanLink(child, span_text)) |link| return link;
|
|
1600
|
+
}
|
|
1601
|
+
return null;
|
|
1602
|
+
}
|
|
1603
|
+
|
|
1604
|
+
pub fn findByRole(widget: canvas.Widget, role: canvas.WidgetRole) ?canvas.Widget {
|
|
1605
|
+
if (widget.semantics.role == role) return widget;
|
|
1606
|
+
for (widget.children) |child| {
|
|
1607
|
+
if (findByRole(child, role)) |found| return found;
|
|
1608
|
+
}
|
|
1609
|
+
return null;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
test "the markdown element builds the hand-written Md.view tree and dispatches links and details" {
|
|
1613
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1614
|
+
defer arena_state.deinit();
|
|
1615
|
+
const arena = arena_state.allocator();
|
|
1616
|
+
|
|
1617
|
+
var model = DocModel{};
|
|
1618
|
+
const DocMarkup = markup_view.MarkupView(DocModel, DocMsg);
|
|
1619
|
+
|
|
1620
|
+
var view = try DocMarkup.init(arena, doc_markup_source);
|
|
1621
|
+
var markup_ui = DocUi.init(arena);
|
|
1622
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
1623
|
+
|
|
1624
|
+
var hand_ui = DocUi.init(arena);
|
|
1625
|
+
const hand_tree = try hand_ui.finalize(handDocView(&hand_ui, &model));
|
|
1626
|
+
|
|
1627
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
1628
|
+
defer markup_ids.deinit(testing.allocator);
|
|
1629
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
1630
|
+
defer hand_ids.deinit(testing.allocator);
|
|
1631
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
1632
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
1633
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
1634
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
1635
|
+
|
|
1636
|
+
// Link spans dispatch the typed on-link message carrying the URL.
|
|
1637
|
+
const link = findByRole(markup_tree.root, .link).?;
|
|
1638
|
+
try testing.expectEqualStrings("https://example.com/guide", markup_tree.msgForPointer(link.id, .up).?.open_url);
|
|
1639
|
+
|
|
1640
|
+
// Issue refs linkify through the issue-link-base binding, and bare
|
|
1641
|
+
// URLs autolink (trailing punctuation trimmed).
|
|
1642
|
+
try testing.expectEqualStrings("ghissue://12", findSpanLink(markup_tree.root, "#12").?);
|
|
1643
|
+
try testing.expectEqualStrings("https://status.example.com", findSpanLink(markup_tree.root, "https://status.example.com").?);
|
|
1644
|
+
|
|
1645
|
+
// Details summary dispatches on-details with the block index; the body
|
|
1646
|
+
// is hidden while the caller-owned flag is false.
|
|
1647
|
+
try testing.expect(findByText(markup_tree.root, .text, "Enable for 5% of traffic.") == null);
|
|
1648
|
+
const summary_item = findByKind(markup_tree.root, .list_item).?;
|
|
1649
|
+
try testing.expectEqual(@as(usize, 0), markup_tree.msgForPointer(summary_item.id, .up).?.toggle_details);
|
|
1650
|
+
|
|
1651
|
+
model.details_expanded[0] = true;
|
|
1652
|
+
var expanded_ui = DocUi.init(arena);
|
|
1653
|
+
const expanded_tree = try expanded_ui.finalize(try view.build(&expanded_ui, &model));
|
|
1654
|
+
try testing.expect(findByText(expanded_tree.root, .text, "Enable for 5% of traffic.") != null);
|
|
1655
|
+
}
|
|
1656
|
+
|
|
1657
|
+
test "markdown misuse fails the build with teaching messages" {
|
|
1658
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1659
|
+
defer arena_state.deinit();
|
|
1660
|
+
const arena = arena_state.allocator();
|
|
1661
|
+
const model = DocModel{};
|
|
1662
|
+
const DocMarkup = markup_view.MarkupView(DocModel, DocMsg);
|
|
1663
|
+
|
|
1664
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
1665
|
+
.{
|
|
1666
|
+
// Missing source entirely.
|
|
1667
|
+
.source = "<column>\n <markdown on-link=\"open_url\" />\n</column>",
|
|
1668
|
+
.message = canvas.ui_markup.markdown_source_message,
|
|
1669
|
+
},
|
|
1670
|
+
.{
|
|
1671
|
+
// A literal is not a source binding.
|
|
1672
|
+
.source = "<column>\n <markdown source=\"# hi\" />\n</column>",
|
|
1673
|
+
.message = canvas.ui_markup.markdown_source_message,
|
|
1674
|
+
},
|
|
1675
|
+
.{
|
|
1676
|
+
// Source binding must produce text (opened_count is a usize).
|
|
1677
|
+
.source = "<column>\n <markdown source=\"{opened_count}\" />\n</column>",
|
|
1678
|
+
.message = canvas.ui_markup.markdown_source_message,
|
|
1679
|
+
},
|
|
1680
|
+
.{
|
|
1681
|
+
// on-link tag must carry a []const u8 payload (refresh is void).
|
|
1682
|
+
.source = "<column>\n <markdown source=\"{body}\" on-link=\"refresh\" />\n</column>",
|
|
1683
|
+
.message = canvas.ui_markup.markdown_on_link_message,
|
|
1684
|
+
},
|
|
1685
|
+
.{
|
|
1686
|
+
// on-link takes a bare tag, never a payload binding.
|
|
1687
|
+
.source = "<column>\n <markdown source=\"{body}\" on-link=\"open_url:{body}\" />\n</column>",
|
|
1688
|
+
.message = canvas.ui_markup.markdown_on_link_message,
|
|
1689
|
+
},
|
|
1690
|
+
.{
|
|
1691
|
+
// on-details tag must carry a usize payload.
|
|
1692
|
+
.source = "<column>\n <markdown source=\"{body}\" on-details=\"refresh\" />\n</column>",
|
|
1693
|
+
.message = canvas.ui_markup.markdown_on_details_message,
|
|
1694
|
+
},
|
|
1695
|
+
.{
|
|
1696
|
+
// details-expanded must name a bool iterable (body is text).
|
|
1697
|
+
.source = "<column>\n <markdown source=\"{body}\" details-expanded=\"{body}\" />\n</column>",
|
|
1698
|
+
.message = canvas.ui_markup.markdown_details_expanded_message,
|
|
1699
|
+
},
|
|
1700
|
+
.{
|
|
1701
|
+
// Closed attribute set.
|
|
1702
|
+
.source = "<column>\n <markdown source=\"{body}\" gap=\"8\" />\n</column>",
|
|
1703
|
+
.message = canvas.ui_markup.markdown_attr_message,
|
|
1704
|
+
},
|
|
1705
|
+
.{
|
|
1706
|
+
// No children: the source binding provides the content.
|
|
1707
|
+
.source = "<column>\n <markdown source=\"{body}\">text</markdown>\n</column>",
|
|
1708
|
+
.message = canvas.ui_markup.markdown_children_message,
|
|
1709
|
+
},
|
|
1710
|
+
};
|
|
1711
|
+
for (cases) |case| {
|
|
1712
|
+
var view = try DocMarkup.init(arena, case.source);
|
|
1713
|
+
var ui = DocUi.init(arena);
|
|
1714
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
1715
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
1716
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
test "markdown misuse is caught by the model-agnostic validator with positions" {
|
|
1721
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1722
|
+
defer arena_state.deinit();
|
|
1723
|
+
const arena = arena_state.allocator();
|
|
1724
|
+
|
|
1725
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
1726
|
+
.{
|
|
1727
|
+
.source = "<column>\n <markdown on-link=\"open_url\" />\n</column>",
|
|
1728
|
+
.message = canvas.ui_markup.markdown_source_message,
|
|
1729
|
+
},
|
|
1730
|
+
.{
|
|
1731
|
+
.source = "<column>\n <markdown source=\"# literal\" />\n</column>",
|
|
1732
|
+
.message = canvas.ui_markup.markdown_source_message,
|
|
1733
|
+
},
|
|
1734
|
+
.{
|
|
1735
|
+
.source = "<column>\n <markdown source=\"{body}\" on-link=\"open_url:{body}\" />\n</column>",
|
|
1736
|
+
.message = canvas.ui_markup.markdown_on_link_message,
|
|
1737
|
+
},
|
|
1738
|
+
.{
|
|
1739
|
+
.source = "<column>\n <markdown source=\"{body}\" on-details=\"toggle:{body}\" />\n</column>",
|
|
1740
|
+
.message = canvas.ui_markup.markdown_on_details_message,
|
|
1741
|
+
},
|
|
1742
|
+
.{
|
|
1743
|
+
.source = "<column>\n <markdown source=\"{body}\" details-expanded=\"literal\" />\n</column>",
|
|
1744
|
+
.message = canvas.ui_markup.markdown_details_expanded_message,
|
|
1745
|
+
},
|
|
1746
|
+
.{
|
|
1747
|
+
.source = "<column>\n <markdown source=\"{body}\" padding=\"8\" />\n</column>",
|
|
1748
|
+
.message = canvas.ui_markup.markdown_attr_message,
|
|
1749
|
+
},
|
|
1750
|
+
.{
|
|
1751
|
+
.source = "<column>\n <markdown source=\"{body}\"><text>x</text></markdown>\n</column>",
|
|
1752
|
+
.message = canvas.ui_markup.markdown_children_message,
|
|
1753
|
+
},
|
|
1754
|
+
};
|
|
1755
|
+
for (cases) |case| {
|
|
1756
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
1757
|
+
const info = canvas.ui_markup.validate(try parser.parse()) orelse return error.TestUnexpectedResult;
|
|
1758
|
+
try testing.expectEqualStrings(case.message, info.message);
|
|
1759
|
+
try testing.expect(info.line > 0);
|
|
1760
|
+
try testing.expect(info.column > 0);
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
// A correct markdown element validates cleanly.
|
|
1764
|
+
var parser = canvas.ui_markup.Parser.init(arena, "<column><markdown source=\"{body}\" on-link=\"open_url\" on-details=\"toggle_details\" details-expanded=\"{flags}\" /></column>");
|
|
1765
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
// -------------------------------------------------- component catalog fixture
|
|
1769
|
+
|
|
1770
|
+
pub const CatalogRow = struct {
|
|
1771
|
+
id: u32,
|
|
1772
|
+
name: []const u8,
|
|
1773
|
+
qty: u32,
|
|
1774
|
+
|
|
1775
|
+
pub fn key(row: *const CatalogRow) canvas.UiKey {
|
|
1776
|
+
return canvas.uiKey(row.id);
|
|
1777
|
+
}
|
|
1778
|
+
};
|
|
1779
|
+
|
|
1780
|
+
pub const CatalogMsg = union(enum) {
|
|
1781
|
+
open_picker,
|
|
1782
|
+
set_tab: u32,
|
|
1783
|
+
toggle_bold,
|
|
1784
|
+
toggle_details,
|
|
1785
|
+
set_page: u32,
|
|
1786
|
+
pick_row: u32,
|
|
1787
|
+
query_edit: canvas.TextInputEvent,
|
|
1788
|
+
submit_query,
|
|
1789
|
+
};
|
|
1790
|
+
|
|
1791
|
+
pub const CatalogModel = struct {
|
|
1792
|
+
tab: u32 = 0,
|
|
1793
|
+
overview_tab: u32 = 0,
|
|
1794
|
+
data_tab: u32 = 1,
|
|
1795
|
+
bold: bool = false,
|
|
1796
|
+
details_open: bool = true,
|
|
1797
|
+
dialog_open: bool = false,
|
|
1798
|
+
loading: bool = true,
|
|
1799
|
+
page: u32 = 1,
|
|
1800
|
+
stage: usize = 1,
|
|
1801
|
+
choice: []const u8 = "Bananas",
|
|
1802
|
+
query: []const u8 = "",
|
|
1803
|
+
rows: []const CatalogRow = &.{},
|
|
1804
|
+
|
|
1805
|
+
pub fn prevPage(model: *const CatalogModel) u32 {
|
|
1806
|
+
return model.page -| 1;
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
pub fn nextPage(model: *const CatalogModel) u32 {
|
|
1810
|
+
return model.page + 1;
|
|
1811
|
+
}
|
|
1812
|
+
};
|
|
1813
|
+
|
|
1814
|
+
/// One instance of every element added for built-in component coverage:
|
|
1815
|
+
/// row containers (breadcrumb, tabs, toggle-group, button-group,
|
|
1816
|
+
/// radio-group, pagination), vertical containers (table + table-row +
|
|
1817
|
+
/// table-cell, dropdown-menu), surfaces (accordion, alert, bubble,
|
|
1818
|
+
/// resizable, dialog, drawer, sheet), text leaves (avatar, select, switch,
|
|
1819
|
+
/// toggle-button, tooltip), text entry (input, combobox), and plain leaves
|
|
1820
|
+
/// (skeleton, spinner).
|
|
1821
|
+
pub const catalog_markup_source =
|
|
1822
|
+
\\<column gap="8">
|
|
1823
|
+
\\ <breadcrumb gap="4">
|
|
1824
|
+
\\ <text>Home</text>
|
|
1825
|
+
\\ <text>Products</text>
|
|
1826
|
+
\\ </breadcrumb>
|
|
1827
|
+
\\ <tabs gap="4">
|
|
1828
|
+
\\ <button selected="{tab == overview_tab}" on-press="set_tab:{overview_tab}">Overview</button>
|
|
1829
|
+
\\ <button selected="{tab == data_tab}" on-press="set_tab:{data_tab}">Data</button>
|
|
1830
|
+
\\ </tabs>
|
|
1831
|
+
\\ <row gap="8" cross="center">
|
|
1832
|
+
\\ <avatar>CT</avatar>
|
|
1833
|
+
\\ <select placeholder="Pick a fruit" on-press="open_picker">{choice}</select>
|
|
1834
|
+
\\ <switch checked="{bold}" on-toggle="toggle_bold">Bold</switch>
|
|
1835
|
+
\\ <toggle-group gap="4">
|
|
1836
|
+
\\ <toggle-button selected="{bold}" on-toggle="toggle_bold">B</toggle-button>
|
|
1837
|
+
\\ </toggle-group>
|
|
1838
|
+
\\ <button-group gap="4">
|
|
1839
|
+
\\ <button size="sm" on-press="open_picker">Open</button>
|
|
1840
|
+
\\ </button-group>
|
|
1841
|
+
\\ </row>
|
|
1842
|
+
\\ <row gap="8">
|
|
1843
|
+
\\ <input text="{query}" placeholder="Name" autofocus="true" on-input="query_edit" on-submit="submit_query" grow="1" />
|
|
1844
|
+
\\ <combobox text="{query}" placeholder="Search fruit" on-input="query_edit" />
|
|
1845
|
+
\\ </row>
|
|
1846
|
+
\\ <radio-group gap="4">
|
|
1847
|
+
\\ <radio checked="{bold}" on-toggle="toggle_bold" label="Bold" />
|
|
1848
|
+
\\ </radio-group>
|
|
1849
|
+
\\ <accordion text="Details" selected="{details_open}" on-toggle="toggle_details" padding="8">
|
|
1850
|
+
\\ <text>More info</text>
|
|
1851
|
+
\\ </accordion>
|
|
1852
|
+
\\ <alert text="Heads up" />
|
|
1853
|
+
\\ <bubble padding="8">
|
|
1854
|
+
\\ <text>Hi!</text>
|
|
1855
|
+
\\ </bubble>
|
|
1856
|
+
\\ <table gap="2">
|
|
1857
|
+
\\ <table-row gap="4">
|
|
1858
|
+
\\ <table-cell>Name</table-cell>
|
|
1859
|
+
\\ <table-cell>Qty</table-cell>
|
|
1860
|
+
\\ </table-row>
|
|
1861
|
+
\\ <for each="rows" key="id" as="r">
|
|
1862
|
+
\\ <table-row gap="4">
|
|
1863
|
+
\\ <table-cell on-press="pick_row:{r.id}">{r.name}</table-cell>
|
|
1864
|
+
\\ <table-cell>{r.qty}</table-cell>
|
|
1865
|
+
\\ </table-row>
|
|
1866
|
+
\\ </for>
|
|
1867
|
+
\\ </table>
|
|
1868
|
+
\\ <stepper active="{stage}" key="pipeline">
|
|
1869
|
+
\\ <step>Work</step>
|
|
1870
|
+
\\ <step>Review · {page}</step>
|
|
1871
|
+
\\ <step>Ready</step>
|
|
1872
|
+
\\ </stepper>
|
|
1873
|
+
\\ <timeline gap="4" label="run ledger">
|
|
1874
|
+
\\ <for each="rows" key="id" as="entry">
|
|
1875
|
+
\\ <timeline-item title="{entry.name}" description="Step summary" meta="claude · sonnet" variant="primary" on-press="pick_row:{entry.id}" />
|
|
1876
|
+
\\ </for>
|
|
1877
|
+
\\ <timeline-item title="Ready for review" icon="check" variant="secondary" connector="false" selected="true" />
|
|
1878
|
+
\\ </timeline>
|
|
1879
|
+
\\ <pagination gap="4">
|
|
1880
|
+
\\ <button size="sm" on-press="set_page:{prevPage}">Prev</button>
|
|
1881
|
+
\\ <badge>{page}</badge>
|
|
1882
|
+
\\ <button size="sm" on-press="set_page:{nextPage}">Next</button>
|
|
1883
|
+
\\ </pagination>
|
|
1884
|
+
\\ <dropdown-menu gap="2">
|
|
1885
|
+
\\ <menu-item on-press="open_picker">Rename</menu-item>
|
|
1886
|
+
\\ </dropdown-menu>
|
|
1887
|
+
\\ <resizable width="240">
|
|
1888
|
+
\\ <column padding="8">
|
|
1889
|
+
\\ <text>Sidebar</text>
|
|
1890
|
+
\\ </column>
|
|
1891
|
+
\\ </resizable>
|
|
1892
|
+
\\ <if test="{loading}">
|
|
1893
|
+
\\ <row gap="4" cross="center">
|
|
1894
|
+
\\ <spinner />
|
|
1895
|
+
\\ <skeleton width="120" height="16" />
|
|
1896
|
+
\\ </row>
|
|
1897
|
+
\\ </if>
|
|
1898
|
+
\\ <tooltip>Copied!</tooltip>
|
|
1899
|
+
\\ <if test="{dialog_open}">
|
|
1900
|
+
\\ <dialog text="Confirm">
|
|
1901
|
+
\\ <column gap="8" padding="12">
|
|
1902
|
+
\\ <text>Are you sure?</text>
|
|
1903
|
+
\\ <button variant="primary" on-press="submit_query">Yes</button>
|
|
1904
|
+
\\ </column>
|
|
1905
|
+
\\ </dialog>
|
|
1906
|
+
\\ </if>
|
|
1907
|
+
\\ <drawer text="Filters">
|
|
1908
|
+
\\ <column padding="8">
|
|
1909
|
+
\\ <text>Drawer body</text>
|
|
1910
|
+
\\ </column>
|
|
1911
|
+
\\ </drawer>
|
|
1912
|
+
\\ <sheet text="Share">
|
|
1913
|
+
\\ <column padding="8">
|
|
1914
|
+
\\ <text>Sheet body</text>
|
|
1915
|
+
\\ </column>
|
|
1916
|
+
\\ </sheet>
|
|
1917
|
+
\\</column>
|
|
1918
|
+
;
|
|
1919
|
+
|
|
1920
|
+
pub const CatalogUi = canvas.Ui(CatalogMsg);
|
|
1921
|
+
|
|
1922
|
+
fn textLeaf(ui: *CatalogUi, kind: canvas.WidgetKind, options: CatalogUi.ElementOptions, content: []const u8) CatalogUi.Node {
|
|
1923
|
+
var node = ui.el(kind, options, .{});
|
|
1924
|
+
node.widget.text = content;
|
|
1925
|
+
return node;
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
fn catalogTableRow(ui: *CatalogUi, row: *const CatalogRow) CatalogUi.Node {
|
|
1929
|
+
return ui.el(.data_row, .{ .gap = 4 }, .{
|
|
1930
|
+
textLeaf(ui, .data_cell, .{ .on_press = CatalogMsg{ .pick_row = row.id } }, row.name),
|
|
1931
|
+
textLeaf(ui, .data_cell, .{}, ui.fmt("{d}", .{row.qty})),
|
|
1932
|
+
});
|
|
1933
|
+
}
|
|
1934
|
+
|
|
1935
|
+
fn catalogTimelineEntry(ui: *CatalogUi, row: *const CatalogRow) CatalogUi.Node {
|
|
1936
|
+
return ui.timelineItem(.{
|
|
1937
|
+
.title = row.name,
|
|
1938
|
+
.description = "Step summary",
|
|
1939
|
+
.meta = "claude · sonnet",
|
|
1940
|
+
.variant = .primary,
|
|
1941
|
+
.on_press = CatalogMsg{ .pick_row = row.id },
|
|
1942
|
+
});
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
/// The hand-written equivalent of the catalog markup for a model with
|
|
1946
|
+
/// `loading` true and `dialog_open` false (the fixture model): parity
|
|
1947
|
+
/// means the interpreter and the compiled view both build exactly this.
|
|
1948
|
+
pub fn handCatalogView(ui: *CatalogUi, model: *const CatalogModel) CatalogUi.Node {
|
|
1949
|
+
return ui.column(.{ .gap = 8 }, .{
|
|
1950
|
+
ui.el(.breadcrumb, .{ .gap = 4 }, .{
|
|
1951
|
+
ui.text(.{}, "Home"),
|
|
1952
|
+
ui.text(.{}, "Products"),
|
|
1953
|
+
}),
|
|
1954
|
+
// Tab triggers ARE segmented controls: the markup engines lower
|
|
1955
|
+
// `<button>` children of `<tabs>` to the segmented kind, so the
|
|
1956
|
+
// hand-written equivalent builds segments directly.
|
|
1957
|
+
ui.el(.tabs, .{ .gap = 4 }, .{
|
|
1958
|
+
textLeaf(ui, .segmented_control, .{ .selected = model.tab == model.overview_tab, .on_press = CatalogMsg{ .set_tab = model.overview_tab } }, "Overview"),
|
|
1959
|
+
textLeaf(ui, .segmented_control, .{ .selected = model.tab == model.data_tab, .on_press = CatalogMsg{ .set_tab = model.data_tab } }, "Data"),
|
|
1960
|
+
}),
|
|
1961
|
+
ui.row(.{ .gap = 8, .cross = .center }, .{
|
|
1962
|
+
textLeaf(ui, .avatar, .{}, "CT"),
|
|
1963
|
+
textLeaf(ui, .select, .{ .placeholder = "Pick a fruit", .on_press = .open_picker }, model.choice),
|
|
1964
|
+
textLeaf(ui, .switch_control, .{ .checked = model.bold, .on_toggle = .toggle_bold }, "Bold"),
|
|
1965
|
+
ui.el(.toggle_group, .{ .gap = 4 }, .{
|
|
1966
|
+
textLeaf(ui, .toggle_button, .{ .selected = model.bold, .on_toggle = .toggle_bold }, "B"),
|
|
1967
|
+
}),
|
|
1968
|
+
ui.el(.button_group, .{ .gap = 4 }, .{
|
|
1969
|
+
ui.button(.{ .size = .sm, .on_press = .open_picker }, "Open"),
|
|
1970
|
+
}),
|
|
1971
|
+
}),
|
|
1972
|
+
ui.row(.{ .gap = 8 }, .{
|
|
1973
|
+
ui.el(.input, .{ .text = model.query, .placeholder = "Name", .autofocus = true, .on_input = CatalogUi.inputMsg(.query_edit), .on_submit = .submit_query, .grow = 1 }, .{}),
|
|
1974
|
+
ui.el(.combobox, .{ .text = model.query, .placeholder = "Search fruit", .on_input = CatalogUi.inputMsg(.query_edit) }, .{}),
|
|
1975
|
+
}),
|
|
1976
|
+
ui.el(.radio_group, .{ .gap = 4 }, .{
|
|
1977
|
+
ui.el(.radio, .{ .checked = model.bold, .on_toggle = .toggle_bold }, .{}),
|
|
1978
|
+
}),
|
|
1979
|
+
ui.el(.accordion, .{ .text = "Details", .selected = model.details_open, .on_toggle = .toggle_details, .padding = 8 }, .{
|
|
1980
|
+
ui.text(.{}, "More info"),
|
|
1981
|
+
}),
|
|
1982
|
+
ui.el(.alert, .{ .text = "Heads up" }, .{}),
|
|
1983
|
+
ui.el(.bubble, .{ .padding = 8 }, .{
|
|
1984
|
+
ui.text(.{}, "Hi!"),
|
|
1985
|
+
}),
|
|
1986
|
+
ui.el(.table, .{ .gap = 2 }, .{
|
|
1987
|
+
ui.el(.data_row, .{ .gap = 4 }, .{
|
|
1988
|
+
textLeaf(ui, .data_cell, .{}, "Name"),
|
|
1989
|
+
textLeaf(ui, .data_cell, .{}, "Qty"),
|
|
1990
|
+
}),
|
|
1991
|
+
ui.each(model.rows, CatalogRow.key, catalogTableRow),
|
|
1992
|
+
}),
|
|
1993
|
+
ui.stepper(.{ .active = model.stage, .key = canvas.uiKey("pipeline") }, &.{
|
|
1994
|
+
.{ .label = "Work" },
|
|
1995
|
+
.{ .label = ui.fmt("Review · {d}", .{model.page}) },
|
|
1996
|
+
.{ .label = "Ready" },
|
|
1997
|
+
}),
|
|
1998
|
+
ui.timeline(.{ .gap = 4, .semantics = .{ .label = "run ledger" } }, .{
|
|
1999
|
+
ui.each(model.rows, CatalogRow.key, catalogTimelineEntry),
|
|
2000
|
+
ui.timelineItem(.{
|
|
2001
|
+
.title = "Ready for review",
|
|
2002
|
+
.icon = "check",
|
|
2003
|
+
.variant = .secondary,
|
|
2004
|
+
.connector = false,
|
|
2005
|
+
.selected = true,
|
|
2006
|
+
}),
|
|
2007
|
+
}),
|
|
2008
|
+
ui.el(.pagination, .{ .gap = 4 }, .{
|
|
2009
|
+
ui.button(.{ .size = .sm, .on_press = CatalogMsg{ .set_page = model.prevPage() } }, "Prev"),
|
|
2010
|
+
textLeaf(ui, .badge, .{}, ui.fmt("{d}", .{model.page})),
|
|
2011
|
+
ui.button(.{ .size = .sm, .on_press = CatalogMsg{ .set_page = model.nextPage() } }, "Next"),
|
|
2012
|
+
}),
|
|
2013
|
+
ui.el(.dropdown_menu, .{ .gap = 2 }, .{
|
|
2014
|
+
textLeaf(ui, .menu_item, .{ .on_press = .open_picker }, "Rename"),
|
|
2015
|
+
}),
|
|
2016
|
+
ui.el(.resizable, .{ .width = 240 }, .{
|
|
2017
|
+
ui.column(.{ .padding = 8 }, ui.text(.{}, "Sidebar")),
|
|
2018
|
+
}),
|
|
2019
|
+
// The fixture model has loading=true and dialog_open=false; the
|
|
2020
|
+
// interpreter/compiled if blocks flatten to exactly these siblings.
|
|
2021
|
+
ui.row(.{ .gap = 4, .cross = .center }, .{
|
|
2022
|
+
ui.el(.spinner, .{}, .{}),
|
|
2023
|
+
ui.el(.skeleton, .{ .width = 120, .height = 16 }, .{}),
|
|
2024
|
+
}),
|
|
2025
|
+
textLeaf(ui, .tooltip, .{}, "Copied!"),
|
|
2026
|
+
ui.el(.drawer, .{ .text = "Filters" }, .{
|
|
2027
|
+
ui.column(.{ .padding = 8 }, ui.text(.{}, "Drawer body")),
|
|
2028
|
+
}),
|
|
2029
|
+
ui.el(.sheet, .{ .text = "Share" }, .{
|
|
2030
|
+
ui.column(.{ .padding = 8 }, ui.text(.{}, "Sheet body")),
|
|
2031
|
+
}),
|
|
2032
|
+
});
|
|
2033
|
+
}
|
|
2034
|
+
|
|
2035
|
+
pub fn catalogTestModel() CatalogModel {
|
|
2036
|
+
return .{
|
|
2037
|
+
.rows = &[_]CatalogRow{
|
|
2038
|
+
.{ .id = 1, .name = "Apples", .qty = 4 },
|
|
2039
|
+
.{ .id = 2, .name = "Pears", .qty = 7 },
|
|
2040
|
+
},
|
|
2041
|
+
};
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
test "the catalog fixture passes structural validation" {
|
|
2045
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2046
|
+
defer arena_state.deinit();
|
|
2047
|
+
var parser = canvas.ui_markup.Parser.init(arena_state.allocator(), catalog_markup_source);
|
|
2048
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
|
|
2049
|
+
}
|
|
2050
|
+
|
|
2051
|
+
test "catalog elements build the hand-written tree and dispatch typed messages" {
|
|
2052
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2053
|
+
defer arena_state.deinit();
|
|
2054
|
+
const arena = arena_state.allocator();
|
|
2055
|
+
|
|
2056
|
+
const model = catalogTestModel();
|
|
2057
|
+
const CatalogMarkup = markup_view.MarkupView(CatalogModel, CatalogMsg);
|
|
2058
|
+
|
|
2059
|
+
var view = try CatalogMarkup.init(arena, catalog_markup_source);
|
|
2060
|
+
var markup_ui = CatalogUi.init(arena);
|
|
2061
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
2062
|
+
|
|
2063
|
+
var hand_ui = CatalogUi.init(arena);
|
|
2064
|
+
const hand_tree = try hand_ui.finalize(handCatalogView(&hand_ui, &model));
|
|
2065
|
+
|
|
2066
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2067
|
+
defer markup_ids.deinit(testing.allocator);
|
|
2068
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2069
|
+
defer hand_ids.deinit(testing.allocator);
|
|
2070
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
2071
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
2072
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
2073
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
2074
|
+
|
|
2075
|
+
// Text-bearing leaves carry their content.
|
|
2076
|
+
try testing.expect(findByText(markup_tree.root, .avatar, "CT") != null);
|
|
2077
|
+
try testing.expect(findByText(markup_tree.root, .select, "Bananas") != null);
|
|
2078
|
+
try testing.expect(findByText(markup_tree.root, .tooltip, "Copied!") != null);
|
|
2079
|
+
try testing.expect(findByText(markup_tree.root, .data_cell, "Pears") != null);
|
|
2080
|
+
try testing.expect(findByText(markup_tree.root, .badge, "1") != null);
|
|
2081
|
+
|
|
2082
|
+
// Surface titles flow through the text attribute.
|
|
2083
|
+
try testing.expectEqualStrings("Heads up", findByKind(markup_tree.root, .alert).?.text);
|
|
2084
|
+
try testing.expectEqualStrings("Details", findByKind(markup_tree.root, .accordion).?.text);
|
|
2085
|
+
|
|
2086
|
+
// Typed dispatch through the engine's semantic intents: select presses,
|
|
2087
|
+
// switch/toggle-button/accordion toggle, table cells select-press.
|
|
2088
|
+
const select = findByKind(markup_tree.root, .select).?;
|
|
2089
|
+
try testing.expectEqual(CatalogMsg.open_picker, markup_tree.msgForPointer(select.id, .up).?);
|
|
2090
|
+
const switch_control = findByKind(markup_tree.root, .switch_control).?;
|
|
2091
|
+
try testing.expectEqual(CatalogMsg.toggle_bold, markup_tree.msgForPointer(switch_control.id, .up).?);
|
|
2092
|
+
const toggle_button = findByKind(markup_tree.root, .toggle_button).?;
|
|
2093
|
+
try testing.expectEqual(CatalogMsg.toggle_bold, markup_tree.msgForPointer(toggle_button.id, .up).?);
|
|
2094
|
+
const accordion = findByKind(markup_tree.root, .accordion).?;
|
|
2095
|
+
try testing.expectEqual(CatalogMsg.toggle_details, markup_tree.msgForPointer(accordion.id, .up).?);
|
|
2096
|
+
const pears_cell = findByText(markup_tree.root, .data_cell, "Pears").?;
|
|
2097
|
+
try testing.expectEqual(@as(u32, 2), markup_tree.msgForPointer(pears_cell.id, .up).?.pick_row);
|
|
2098
|
+
|
|
2099
|
+
// Composite stepper: the active step (index 1) is selected and carries
|
|
2100
|
+
// its interpolated label + state in semantics.
|
|
2101
|
+
const active_step = findByRoleLabel(markup_tree.root, .listitem, "Review · 1 (active)").?;
|
|
2102
|
+
try testing.expect(active_step.state.selected);
|
|
2103
|
+
try testing.expect(findByRoleLabel(markup_tree.root, .listitem, "Work (completed)") != null);
|
|
2104
|
+
// Composite timeline: an item press dispatches from the item's root
|
|
2105
|
+
// (the bound handler makes it a hit target; presses on the content
|
|
2106
|
+
// fall through to it).
|
|
2107
|
+
const ledger_item = findByRoleLabel(markup_tree.root, .listitem, "Pears").?;
|
|
2108
|
+
try testing.expect(canvas.widgetClaimsPress(ledger_item));
|
|
2109
|
+
try testing.expectEqual(@as(u32, 2), markup_tree.msgForPointer(ledger_item.id, .up).?.pick_row);
|
|
2110
|
+
const prev_button = findByText(markup_tree.root, .button, "Prev").?;
|
|
2111
|
+
try testing.expectEqual(@as(u32, 0), markup_tree.msgForPointer(prev_button.id, .up).?.set_page);
|
|
2112
|
+
|
|
2113
|
+
// Text entry: edits and enter-to-submit dispatch on input.
|
|
2114
|
+
const input = findByKind(markup_tree.root, .input).?;
|
|
2115
|
+
const typed = canvas.WidgetKeyboardEvent{ .phase = .text_input, .text = "q" };
|
|
2116
|
+
try testing.expectEqualStrings("q", markup_tree.msgForKeyboard(input.id, typed).?.query_edit.insert_text);
|
|
2117
|
+
const submit = canvas.WidgetKeyboardEvent{ .phase = .key_down, .key = "enter" };
|
|
2118
|
+
try testing.expectEqual(CatalogMsg.submit_query, markup_tree.msgForKeyboard(input.id, submit).?);
|
|
2119
|
+
|
|
2120
|
+
// The whole catalog lays out through the canvas engine.
|
|
2121
|
+
var nodes: [256]canvas.WidgetLayoutNode = undefined;
|
|
2122
|
+
const layout = try canvas.layoutWidgetTree(markup_tree.root, @import("geometry").RectF.init(0, 0, 900, 1400), &nodes);
|
|
2123
|
+
try testing.expect(layout.nodes.len > 0);
|
|
2124
|
+
}
|
|
2125
|
+
|
|
2126
|
+
test "new element misuse is validated with positions and teaching messages" {
|
|
2127
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2128
|
+
defer arena_state.deinit();
|
|
2129
|
+
const arena = arena_state.allocator();
|
|
2130
|
+
|
|
2131
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
2132
|
+
.{
|
|
2133
|
+
.source = "<column>\n <table-row><table-cell>x</table-cell></table-row>\n</column>",
|
|
2134
|
+
.message = canvas.ui_markup.table_row_parent_message,
|
|
2135
|
+
},
|
|
2136
|
+
.{
|
|
2137
|
+
.source = "<table>\n <table-cell>x</table-cell>\n</table>",
|
|
2138
|
+
.message = canvas.ui_markup.table_cell_parent_message,
|
|
2139
|
+
},
|
|
2140
|
+
.{
|
|
2141
|
+
.source = "<row>\n <select><button on-press=\"x\">pick</button></select>\n</row>",
|
|
2142
|
+
.message = canvas.ui_markup.text_leaf_children_message,
|
|
2143
|
+
},
|
|
2144
|
+
.{
|
|
2145
|
+
.source = "<row>\n <avatar><text>CT</text></avatar>\n</row>",
|
|
2146
|
+
.message = canvas.ui_markup.text_leaf_children_message,
|
|
2147
|
+
},
|
|
2148
|
+
.{
|
|
2149
|
+
// A list-item holds EITHER one text run OR element children
|
|
2150
|
+
// (the list-row composite), never both at once.
|
|
2151
|
+
.source = "<column>\n <list-item on-press=\"pick\" label=\"Row\">Pears<badge>3</badge></list-item>\n</column>",
|
|
2152
|
+
.message = canvas.ui_markup.text_or_children_content_message,
|
|
2153
|
+
},
|
|
2154
|
+
};
|
|
2155
|
+
for (cases) |case| {
|
|
2156
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
2157
|
+
const info = canvas.ui_markup.validate(try parser.parse()) orelse return error.TestUnexpectedResult;
|
|
2158
|
+
try testing.expectEqualStrings(case.message, info.message);
|
|
2159
|
+
try testing.expect(info.line > 0);
|
|
2160
|
+
try testing.expect(info.column > 0);
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
// Structure tags between a table and its rows are fine.
|
|
2164
|
+
var parser = canvas.ui_markup.Parser.init(arena, "<table><for each=\"rows\" as=\"r\"><table-row><table-cell>{r.name}</table-cell></table-row></for></table>");
|
|
2165
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
|
|
2166
|
+
|
|
2167
|
+
// The list-row composite validates: element children (including
|
|
2168
|
+
// structure tags) in place of the list-item's text run.
|
|
2169
|
+
var composite_parser = canvas.ui_markup.Parser.init(arena, "<column>\n <list-item on-press=\"pick\" label=\"Pears row\" padding=\"8\" gap=\"8\">\n <text grow=\"1\">Pears</text>\n <badge variant=\"secondary\">3</badge>\n </list-item>\n</column>");
|
|
2170
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try composite_parser.parse()));
|
|
2171
|
+
}
|
|
2172
|
+
|
|
2173
|
+
// --------------------------------------------------------- text wrapping
|
|
2174
|
+
|
|
2175
|
+
pub const WrapMsg = union(enum) { refresh };
|
|
2176
|
+
|
|
2177
|
+
pub const WrapModel = struct {
|
|
2178
|
+
message: []const u8 = "A long error message that should wrap onto several lines instead of clipping on one",
|
|
2179
|
+
};
|
|
2180
|
+
|
|
2181
|
+
pub const wrap_markup_source =
|
|
2182
|
+
\\<column gap="8" width="360">
|
|
2183
|
+
\\ <text wrap="true">{message}</text>
|
|
2184
|
+
\\ <text>{message}</text>
|
|
2185
|
+
\\ <text wrap="false">{message}</text>
|
|
2186
|
+
\\ <text overflow="clip">{message}</text>
|
|
2187
|
+
\\</column>
|
|
2188
|
+
;
|
|
2189
|
+
|
|
2190
|
+
pub const WrapUi = canvas.Ui(WrapMsg);
|
|
2191
|
+
|
|
2192
|
+
pub fn handWrapView(ui: *WrapUi, model: *const WrapModel) WrapUi.Node {
|
|
2193
|
+
return ui.column(.{ .gap = 8, .width = 360 }, .{
|
|
2194
|
+
ui.text(.{ .wrap = true }, model.message),
|
|
2195
|
+
ui.text(.{}, model.message),
|
|
2196
|
+
ui.text(.{ .wrap = false }, model.message),
|
|
2197
|
+
ui.text(.{ .overflow = .clip }, model.message),
|
|
2198
|
+
});
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
// ------------------------------------------------------ text size rungs
|
|
2202
|
+
|
|
2203
|
+
pub const TypeScaleMsg = union(enum) { refresh };
|
|
2204
|
+
|
|
2205
|
+
pub const TypeScaleModel = struct {
|
|
2206
|
+
stat: []const u8 = "42.7%",
|
|
2207
|
+
caption: []const u8 = "of quota used this month across every region",
|
|
2208
|
+
};
|
|
2209
|
+
|
|
2210
|
+
pub const type_scale_markup_source =
|
|
2211
|
+
\\<column gap="8" width="360">
|
|
2212
|
+
\\ <text size="heading">Usage</text>
|
|
2213
|
+
\\ <text size="display">{stat}</text>
|
|
2214
|
+
\\ <text size="display" wrap="true">{caption}</text>
|
|
2215
|
+
\\ <text size="sm">{caption}</text>
|
|
2216
|
+
\\</column>
|
|
2217
|
+
;
|
|
2218
|
+
|
|
2219
|
+
pub const TypeScaleUi = canvas.Ui(TypeScaleMsg);
|
|
2220
|
+
|
|
2221
|
+
pub fn handTypeScaleView(ui: *TypeScaleUi, model: *const TypeScaleModel) TypeScaleUi.Node {
|
|
2222
|
+
return ui.column(.{ .gap = 8, .width = 360 }, .{
|
|
2223
|
+
ui.text(.{ .size = .heading }, "Usage"),
|
|
2224
|
+
ui.text(.{ .size = .display }, model.stat),
|
|
2225
|
+
ui.text(.{ .size = .display, .wrap = true }, model.caption),
|
|
2226
|
+
ui.text(.{ .size = .sm }, model.caption),
|
|
2227
|
+
});
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
test "the wrap attribute builds the hand-written wrapped text leaf" {
|
|
2231
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2232
|
+
defer arena_state.deinit();
|
|
2233
|
+
const arena = arena_state.allocator();
|
|
2234
|
+
|
|
2235
|
+
const model = WrapModel{};
|
|
2236
|
+
const WrapMarkup = markup_view.MarkupView(WrapModel, WrapMsg);
|
|
2237
|
+
|
|
2238
|
+
var view = try WrapMarkup.init(arena, wrap_markup_source);
|
|
2239
|
+
var markup_ui = WrapUi.init(arena);
|
|
2240
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
2241
|
+
|
|
2242
|
+
var hand_ui = WrapUi.init(arena);
|
|
2243
|
+
const hand_tree = try hand_ui.finalize(handWrapView(&hand_ui, &model));
|
|
2244
|
+
|
|
2245
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2246
|
+
defer markup_ids.deinit(testing.allocator);
|
|
2247
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2248
|
+
defer hand_ids.deinit(testing.allocator);
|
|
2249
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
2250
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
2251
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
2252
|
+
|
|
2253
|
+
// wrap="true" becomes a single-span paragraph over the interpolated
|
|
2254
|
+
// text; the default stays the single-line path.
|
|
2255
|
+
const wrapped = markup_tree.root.children[0];
|
|
2256
|
+
try testing.expectEqual(@as(usize, 1), wrapped.spans.len);
|
|
2257
|
+
try testing.expectEqualStrings(model.message, wrapped.text);
|
|
2258
|
+
try testing.expect(wrapped.spans[0].text.ptr == wrapped.text.ptr);
|
|
2259
|
+
const plain = markup_tree.root.children[1];
|
|
2260
|
+
try testing.expectEqual(@as(usize, 0), plain.spans.len);
|
|
2261
|
+
try testing.expect(!plain.text_no_wrap);
|
|
2262
|
+
|
|
2263
|
+
// wrap="false" stays on the plain text path and stamps the honest
|
|
2264
|
+
// single-line mode; its overflow keeps the ellipsis default.
|
|
2265
|
+
const no_wrap = markup_tree.root.children[2];
|
|
2266
|
+
try testing.expectEqual(@as(usize, 0), no_wrap.spans.len);
|
|
2267
|
+
try testing.expect(no_wrap.text_no_wrap);
|
|
2268
|
+
try testing.expectEqualStrings(model.message, no_wrap.text);
|
|
2269
|
+
try testing.expectEqual(canvas.TextOverflow.ellipsis, no_wrap.text_overflow);
|
|
2270
|
+
try testing.expectEqual(canvas.TextOverflow.ellipsis, plain.text_overflow);
|
|
2271
|
+
|
|
2272
|
+
// overflow="clip" is the explicit hard-cut opt-out of the default.
|
|
2273
|
+
const clipped = markup_tree.root.children[3];
|
|
2274
|
+
try testing.expectEqual(@as(usize, 0), clipped.spans.len);
|
|
2275
|
+
try testing.expectEqual(canvas.TextOverflow.clip, clipped.text_overflow);
|
|
2276
|
+
|
|
2277
|
+
// The definite column width is both floor and cap.
|
|
2278
|
+
try testing.expectEqual(@as(f32, 360), markup_tree.root.layout.min_size.width);
|
|
2279
|
+
try testing.expectEqual(@as(f32, 360), markup_tree.root.layout.max_size.width);
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
// -------------------------------------------- avatar image binding fixture
|
|
2283
|
+
|
|
2284
|
+
pub const AvatarMsg = union(enum) { refresh };
|
|
2285
|
+
|
|
2286
|
+
pub const AvatarModel = struct {
|
|
2287
|
+
/// Runtime-registered ImageId kept in the model (0 = no image, the
|
|
2288
|
+
/// initials fallback) — the id only lands here on successful
|
|
2289
|
+
/// `fx.registerImageBytes`.
|
|
2290
|
+
user_image: canvas.ImageId = 0,
|
|
2291
|
+
user_name: []const u8 = "Casey Torres",
|
|
2292
|
+
|
|
2293
|
+
/// A pub fn producing an ImageId binds like a field.
|
|
2294
|
+
pub fn teammateImage(model: *const AvatarModel) canvas.ImageId {
|
|
2295
|
+
return model.user_image + 1;
|
|
2296
|
+
}
|
|
2297
|
+
};
|
|
2298
|
+
|
|
2299
|
+
pub const avatar_markup_source =
|
|
2300
|
+
\\<row gap="8" cross="center">
|
|
2301
|
+
\\ <avatar image="{user_image}" label="{user_name}">CT</avatar>
|
|
2302
|
+
\\ <avatar image="{teammateImage}">NS</avatar>
|
|
2303
|
+
\\</row>
|
|
2304
|
+
;
|
|
2305
|
+
|
|
2306
|
+
pub const AvatarUi = canvas.Ui(AvatarMsg);
|
|
2307
|
+
|
|
2308
|
+
/// The hand-written equivalent of the avatar markup: `ui.avatar` with
|
|
2309
|
+
/// `ElementOptions.image`, so parity covers the cover-fit clip too.
|
|
2310
|
+
pub fn handAvatarView(ui: *AvatarUi, model: *const AvatarModel) AvatarUi.Node {
|
|
2311
|
+
return ui.row(.{ .gap = 8, .cross = .center }, .{
|
|
2312
|
+
ui.avatar(.{ .image = model.user_image, .semantics = .{ .label = model.user_name } }, "CT"),
|
|
2313
|
+
ui.avatar(.{ .image = model.teammateImage() }, "NS"),
|
|
2314
|
+
});
|
|
2315
|
+
}
|
|
2316
|
+
|
|
2317
|
+
test "the avatar image binding resolves model fields and fns to the widget image id" {
|
|
2318
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2319
|
+
defer arena_state.deinit();
|
|
2320
|
+
const arena = arena_state.allocator();
|
|
2321
|
+
|
|
2322
|
+
const AvatarMarkup = markup_view.MarkupView(AvatarModel, AvatarMsg);
|
|
2323
|
+
const model = AvatarModel{ .user_image = 7 };
|
|
2324
|
+
|
|
2325
|
+
var view = try AvatarMarkup.init(arena, avatar_markup_source);
|
|
2326
|
+
var markup_ui = AvatarUi.init(arena);
|
|
2327
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
2328
|
+
|
|
2329
|
+
var hand_ui = AvatarUi.init(arena);
|
|
2330
|
+
const hand_tree = try hand_ui.finalize(handAvatarView(&hand_ui, &model));
|
|
2331
|
+
|
|
2332
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2333
|
+
defer markup_ids.deinit(testing.allocator);
|
|
2334
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2335
|
+
defer hand_ids.deinit(testing.allocator);
|
|
2336
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
2337
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
2338
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
2339
|
+
|
|
2340
|
+
// The field binding and the fn binding both land in image_id; the
|
|
2341
|
+
// initials stay the text content and the image clips like Ui.avatar.
|
|
2342
|
+
const field_avatar = findByText(markup_tree.root, .avatar, "CT").?;
|
|
2343
|
+
try testing.expectEqual(@as(canvas.ImageId, 7), field_avatar.image_id);
|
|
2344
|
+
try testing.expectEqual(canvas.ImageFit.cover, field_avatar.image_fit);
|
|
2345
|
+
try testing.expectEqualStrings("Casey Torres", field_avatar.semantics.label);
|
|
2346
|
+
const fn_avatar = findByText(markup_tree.root, .avatar, "NS").?;
|
|
2347
|
+
try testing.expectEqual(@as(canvas.ImageId, 8), fn_avatar.image_id);
|
|
2348
|
+
|
|
2349
|
+
// 0 is the "no image" sentinel: the widget stays on the initials
|
|
2350
|
+
// fallback path.
|
|
2351
|
+
const empty_model = AvatarModel{};
|
|
2352
|
+
var empty_ui = AvatarUi.init(arena);
|
|
2353
|
+
const empty_tree = try empty_ui.finalize(try view.build(&empty_ui, &empty_model));
|
|
2354
|
+
try testing.expectEqual(@as(canvas.ImageId, 0), findByText(empty_tree.root, .avatar, "CT").?.image_id);
|
|
2355
|
+
try testing.expectEqualStrings("CT", findByText(empty_tree.root, .avatar, "CT").?.text);
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
test "avatar image misuse fails the build with the teaching messages" {
|
|
2359
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2360
|
+
defer arena_state.deinit();
|
|
2361
|
+
const arena = arena_state.allocator();
|
|
2362
|
+
const model = AvatarModel{};
|
|
2363
|
+
const AvatarMarkup = markup_view.MarkupView(AvatarModel, AvatarMsg);
|
|
2364
|
+
|
|
2365
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
2366
|
+
.{
|
|
2367
|
+
// A literal id is not model data.
|
|
2368
|
+
.source = "<row>\n <avatar image=\"7\">CT</avatar>\n</row>",
|
|
2369
|
+
.message = canvas.ui_markup.avatar_image_message,
|
|
2370
|
+
},
|
|
2371
|
+
.{
|
|
2372
|
+
// The binding must produce an integer ImageId (user_name is text).
|
|
2373
|
+
.source = "<row>\n <avatar image=\"{user_name}\">CT</avatar>\n</row>",
|
|
2374
|
+
.message = canvas.ui_markup.avatar_image_message,
|
|
2375
|
+
},
|
|
2376
|
+
.{
|
|
2377
|
+
// Scoped to avatar: the other image elements stay Zig views.
|
|
2378
|
+
.source = "<row>\n <badge image=\"{user_image}\">3</badge>\n</row>",
|
|
2379
|
+
.message = canvas.ui_markup.avatar_image_element_message,
|
|
2380
|
+
},
|
|
2381
|
+
};
|
|
2382
|
+
for (cases) |case| {
|
|
2383
|
+
var view = try AvatarMarkup.init(arena, case.source);
|
|
2384
|
+
var ui = AvatarUi.init(arena);
|
|
2385
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
2386
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
2387
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
// ------------------------------------- text alignment and grid columns
|
|
2392
|
+
|
|
2393
|
+
pub const AlignMsg = union(enum) { refresh };
|
|
2394
|
+
|
|
2395
|
+
pub const AlignModel = struct {
|
|
2396
|
+
duration: []const u8 = "4:33",
|
|
2397
|
+
column_count: usize = 3,
|
|
2398
|
+
};
|
|
2399
|
+
|
|
2400
|
+
pub const align_markup_source =
|
|
2401
|
+
\\<column gap="8" width="360">
|
|
2402
|
+
\\ <text text-alignment="center" foreground="info">{duration}</text>
|
|
2403
|
+
\\ <grid columns="4" gap="6">
|
|
2404
|
+
\\ <text>a</text>
|
|
2405
|
+
\\ <text>b</text>
|
|
2406
|
+
\\ </grid>
|
|
2407
|
+
\\ <grid columns="{column_count}">
|
|
2408
|
+
\\ <text>c</text>
|
|
2409
|
+
\\ </grid>
|
|
2410
|
+
\\</column>
|
|
2411
|
+
;
|
|
2412
|
+
|
|
2413
|
+
pub const AlignUi = canvas.Ui(AlignMsg);
|
|
2414
|
+
|
|
2415
|
+
pub fn handAlignView(ui: *AlignUi, model: *const AlignModel) AlignUi.Node {
|
|
2416
|
+
return ui.column(.{ .gap = 8, .width = 360 }, .{
|
|
2417
|
+
ui.text(.{ .text_alignment = .center, .style_tokens = .{ .foreground = .info } }, model.duration),
|
|
2418
|
+
ui.el(.grid, .{ .columns = 4, .gap = 6 }, .{
|
|
2419
|
+
ui.text(.{}, "a"),
|
|
2420
|
+
ui.text(.{}, "b"),
|
|
2421
|
+
}),
|
|
2422
|
+
ui.el(.grid, .{ .columns = model.column_count }, .{
|
|
2423
|
+
ui.text(.{}, "c"),
|
|
2424
|
+
}),
|
|
2425
|
+
});
|
|
2426
|
+
}
|
|
2427
|
+
|
|
2428
|
+
test "text-alignment and grid columns build the hand-written tree" {
|
|
2429
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2430
|
+
defer arena_state.deinit();
|
|
2431
|
+
const arena = arena_state.allocator();
|
|
2432
|
+
|
|
2433
|
+
const model = AlignModel{};
|
|
2434
|
+
const AlignMarkup = markup_view.MarkupView(AlignModel, AlignMsg);
|
|
2435
|
+
|
|
2436
|
+
var view = try AlignMarkup.init(arena, align_markup_source);
|
|
2437
|
+
var markup_ui = AlignUi.init(arena);
|
|
2438
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
2439
|
+
|
|
2440
|
+
var hand_ui = AlignUi.init(arena);
|
|
2441
|
+
const hand_tree = try hand_ui.finalize(handAlignView(&hand_ui, &model));
|
|
2442
|
+
|
|
2443
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2444
|
+
defer markup_ids.deinit(testing.allocator);
|
|
2445
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2446
|
+
defer hand_ids.deinit(testing.allocator);
|
|
2447
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
2448
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
2449
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
2450
|
+
|
|
2451
|
+
// text-alignment lands on the widget; the default stays .start.
|
|
2452
|
+
const aligned = markup_tree.root.children[0];
|
|
2453
|
+
try testing.expectEqual(canvas.TextAlign.center, aligned.text_alignment);
|
|
2454
|
+
// The info token resolves like any other ColorTokens field.
|
|
2455
|
+
try testing.expectEqualDeep((canvas.DesignTokens{}).colors.info, aligned.style.foreground.?);
|
|
2456
|
+
|
|
2457
|
+
// columns lands in the grid layout, from a literal and from a binding.
|
|
2458
|
+
try testing.expectEqual(@as(usize, 4), markup_tree.root.children[1].layout.columns);
|
|
2459
|
+
try testing.expectEqual(@as(usize, 3), markup_tree.root.children[2].layout.columns);
|
|
2460
|
+
// The inner texts keep the .start default.
|
|
2461
|
+
try testing.expectEqual(canvas.TextAlign.start, markup_tree.root.children[1].children[0].text_alignment);
|
|
2462
|
+
|
|
2463
|
+
// The source validates cleanly.
|
|
2464
|
+
var parser = canvas.ui_markup.Parser.init(arena, align_markup_source);
|
|
2465
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
|
|
2466
|
+
}
|
|
2467
|
+
|
|
2468
|
+
test "columns off grid and misshapen alignment values fail with teaching messages" {
|
|
2469
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2470
|
+
defer arena_state.deinit();
|
|
2471
|
+
const arena = arena_state.allocator();
|
|
2472
|
+
|
|
2473
|
+
// The validator scopes columns to grid (the only layout that reads it).
|
|
2474
|
+
var parser = canvas.ui_markup.Parser.init(arena, "<column columns=\"3\">\n <text>x</text>\n</column>");
|
|
2475
|
+
const info = canvas.ui_markup.validate(try parser.parse()) orelse return error.TestUnexpectedResult;
|
|
2476
|
+
try testing.expectEqualStrings(canvas.ui_markup.grid_columns_element_message, info.message);
|
|
2477
|
+
try testing.expect(info.line > 0);
|
|
2478
|
+
|
|
2479
|
+
// The interpreter rejects bad values with its generic option messages.
|
|
2480
|
+
const model = AlignModel{};
|
|
2481
|
+
const AlignMarkup = markup_view.MarkupView(AlignModel, AlignMsg);
|
|
2482
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
2483
|
+
.{
|
|
2484
|
+
.source = "<column>\n <text text-alignment=\"middle\">x</text>\n</column>",
|
|
2485
|
+
.message = "unknown option value",
|
|
2486
|
+
},
|
|
2487
|
+
.{
|
|
2488
|
+
.source = "<column>\n <grid columns=\"{duration}\"><text>x</text></grid>\n</column>",
|
|
2489
|
+
.message = "expected a whole number",
|
|
2490
|
+
},
|
|
2491
|
+
};
|
|
2492
|
+
for (cases) |case| {
|
|
2493
|
+
var view = try AlignMarkup.init(arena, case.source);
|
|
2494
|
+
var ui = AlignUi.init(arena);
|
|
2495
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
2496
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
2497
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2500
|
+
|
|
2501
|
+
// -------------------------------------------------- split panes and trees
|
|
2502
|
+
|
|
2503
|
+
pub const Folder = struct {
|
|
2504
|
+
id: u32,
|
|
2505
|
+
name: []const u8,
|
|
2506
|
+
expanded: bool = false,
|
|
2507
|
+
|
|
2508
|
+
fn key(folder: *const Folder) canvas.UiKey {
|
|
2509
|
+
return canvas.uiKey(folder.id);
|
|
2510
|
+
}
|
|
2511
|
+
};
|
|
2512
|
+
|
|
2513
|
+
pub const PaneMsg = union(enum) {
|
|
2514
|
+
sidebar_resized: f32,
|
|
2515
|
+
select_folder: u32,
|
|
2516
|
+
toggle_folder: u32,
|
|
2517
|
+
};
|
|
2518
|
+
|
|
2519
|
+
pub const PaneModel = struct {
|
|
2520
|
+
sidebar_fraction: f32 = 0.4,
|
|
2521
|
+
pub const folders = [_]Folder{
|
|
2522
|
+
.{ .id = 1, .name = "Inbox", .expanded = true },
|
|
2523
|
+
.{ .id = 2, .name = "Archive" },
|
|
2524
|
+
};
|
|
2525
|
+
};
|
|
2526
|
+
|
|
2527
|
+
pub const PaneUi = canvas.Ui(PaneMsg);
|
|
2528
|
+
const PaneMarkup = markup_view.MarkupView(PaneModel, PaneMsg);
|
|
2529
|
+
|
|
2530
|
+
pub const pane_markup_source =
|
|
2531
|
+
\\<split value="{sidebar_fraction}" on-resize="sidebar_resized">
|
|
2532
|
+
\\ <tree label="Folders">
|
|
2533
|
+
\\ <for each="folders" key="id" as="f">
|
|
2534
|
+
\\ <panel role="treeitem" expanded="{f.expanded}" on-press="select_folder:{f.id}" on-toggle="toggle_folder:{f.id}" label="{f.name}">
|
|
2535
|
+
\\ <text>{f.name}</text>
|
|
2536
|
+
\\ </panel>
|
|
2537
|
+
\\ </for>
|
|
2538
|
+
\\ </tree>
|
|
2539
|
+
\\ <column min-width="120">
|
|
2540
|
+
\\ <text>Editor</text>
|
|
2541
|
+
\\ </column>
|
|
2542
|
+
\\</split>
|
|
2543
|
+
;
|
|
2544
|
+
|
|
2545
|
+
pub fn handPaneView(ui: *PaneUi, model: *const PaneModel) PaneUi.Node {
|
|
2546
|
+
return ui.split(.{ .value = model.sidebar_fraction, .on_resize = PaneUi.valueMsg(.sidebar_resized) }, .{
|
|
2547
|
+
ui.tree(.{ .semantics = .{ .label = "Folders" } }, ui.each(PaneModel.folders[0..], Folder.key, folderRow)),
|
|
2548
|
+
ui.column(.{ .min_width = 120 }, .{
|
|
2549
|
+
ui.text(.{}, "Editor"),
|
|
2550
|
+
}),
|
|
2551
|
+
});
|
|
2552
|
+
}
|
|
2553
|
+
|
|
2554
|
+
fn folderRow(ui: *PaneUi, folder: *const Folder) PaneUi.Node {
|
|
2555
|
+
return ui.panel(.{
|
|
2556
|
+
.expanded = folder.expanded,
|
|
2557
|
+
.on_press = PaneMsg{ .select_folder = folder.id },
|
|
2558
|
+
.on_toggle = PaneMsg{ .toggle_folder = folder.id },
|
|
2559
|
+
.semantics = .{ .role = .treeitem, .label = folder.name },
|
|
2560
|
+
}, .{
|
|
2561
|
+
ui.text(.{}, folder.name),
|
|
2562
|
+
});
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
test "markup split and tree build the hand-written view with the divider synthesized" {
|
|
2566
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2567
|
+
defer arena_state.deinit();
|
|
2568
|
+
const arena = arena_state.allocator();
|
|
2569
|
+
|
|
2570
|
+
const model = PaneModel{};
|
|
2571
|
+
var view = try PaneMarkup.init(arena, pane_markup_source);
|
|
2572
|
+
var markup_ui = PaneUi.init(arena);
|
|
2573
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
2574
|
+
|
|
2575
|
+
var hand_ui = PaneUi.init(arena);
|
|
2576
|
+
const hand_tree = try hand_ui.finalize(handPaneView(&hand_ui, &model));
|
|
2577
|
+
|
|
2578
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2579
|
+
defer markup_ids.deinit(testing.allocator);
|
|
2580
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2581
|
+
defer hand_ids.deinit(testing.allocator);
|
|
2582
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
2583
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
2584
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
2585
|
+
|
|
2586
|
+
// The synthesized divider sits between the panes in both engines.
|
|
2587
|
+
try testing.expectEqual(@as(usize, 3), markup_tree.root.children.len);
|
|
2588
|
+
try testing.expectEqual(canvas.WidgetKind.split_divider, markup_tree.root.children[1].kind);
|
|
2589
|
+
|
|
2590
|
+
// on-resize binds the f32 fraction constructor on the split.
|
|
2591
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
2592
|
+
try testing.expectEqual(@as(f32, 0.7), markup_tree.msgForResize(markup_tree.root.id, 0.7).?.sidebar_resized);
|
|
2593
|
+
|
|
2594
|
+
// Tree rows carry the treeitem role, the model-owned expanded state,
|
|
2595
|
+
// and both the press (selection) and toggle (disclosure) handlers.
|
|
2596
|
+
const row = findByKind(markup_tree.root, .panel).?;
|
|
2597
|
+
try testing.expectEqual(canvas.WidgetRole.treeitem, row.semantics.role);
|
|
2598
|
+
try testing.expectEqual(@as(?bool, true), row.state.expanded);
|
|
2599
|
+
try testing.expectEqual(@as(u32, 1), markup_tree.msgForPointer(row.id, .up).?.select_folder);
|
|
2600
|
+
try testing.expectEqual(@as(u32, 1), markup_tree.msgFor(row.id, .toggle).?.toggle_folder);
|
|
2601
|
+
|
|
2602
|
+
// min-width lands as a floor only (no definite max).
|
|
2603
|
+
const editor = markup_tree.root.children[2];
|
|
2604
|
+
try testing.expectEqual(@as(f32, 120), editor.layout.min_size.width);
|
|
2605
|
+
try testing.expectEqual(@as(f32, 0), editor.layout.max_size.width);
|
|
2606
|
+
}
|
|
2607
|
+
|
|
2608
|
+
test "split and tree misuse is validated with teaching messages" {
|
|
2609
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2610
|
+
defer arena_state.deinit();
|
|
2611
|
+
const arena = arena_state.allocator();
|
|
2612
|
+
|
|
2613
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
2614
|
+
.{
|
|
2615
|
+
.source = "<split>\n <column></column>\n</split>",
|
|
2616
|
+
.message = canvas.ui_markup.split_children_message,
|
|
2617
|
+
},
|
|
2618
|
+
.{
|
|
2619
|
+
.source = "<split>\n <column></column>\n <column></column>\n <column></column>\n</split>",
|
|
2620
|
+
.message = canvas.ui_markup.split_children_message,
|
|
2621
|
+
},
|
|
2622
|
+
.{
|
|
2623
|
+
.source = "<split>\n <if test=\"{ready}\"><column></column></if>\n <column></column>\n</split>",
|
|
2624
|
+
.message = canvas.ui_markup.split_children_message,
|
|
2625
|
+
},
|
|
2626
|
+
.{
|
|
2627
|
+
.source = "<column on-resize=\"resized\">\n</column>",
|
|
2628
|
+
.message = canvas.ui_markup.on_resize_element_message,
|
|
2629
|
+
},
|
|
2630
|
+
};
|
|
2631
|
+
for (cases) |case| {
|
|
2632
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
2633
|
+
const info = canvas.ui_markup.validate(try parser.parse()) orelse return error.TestUnexpectedResult;
|
|
2634
|
+
try testing.expectEqualStrings(case.message, info.message);
|
|
2635
|
+
try testing.expect(info.line > 0);
|
|
2636
|
+
try testing.expect(info.column > 0);
|
|
2637
|
+
}
|
|
2638
|
+
|
|
2639
|
+
// The interpreter mirrors the validator: a resize tag without an f32
|
|
2640
|
+
// payload variant fails the build with the payload teaching message.
|
|
2641
|
+
const bad_payload_source = "<split value=\"0.5\" on-resize=\"select_folder\"><column></column><column></column></split>";
|
|
2642
|
+
var view = try PaneMarkup.init(arena, bad_payload_source);
|
|
2643
|
+
var ui = PaneUi.init(arena);
|
|
2644
|
+
const model = PaneModel{};
|
|
2645
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
2646
|
+
try testing.expectEqualStrings(canvas.ui_markup.on_resize_payload_message, view.diagnostic.message);
|
|
2647
|
+
}
|
|
2648
|
+
|
|
2649
|
+
// ------------------------------------- import/slot/default fixture
|
|
2650
|
+
|
|
2651
|
+
/// A three-file document exercising the whole component surface: a
|
|
2652
|
+
/// transitive import chain (view -> components/pills.native -> base.native,
|
|
2653
|
+
/// resolved relative to each importer), an imported template using
|
|
2654
|
+
/// another imported template, a defaulted arg (tone=muted), and a slot
|
|
2655
|
+
/// whose content builds in the consumer's scope (it sees the `for` loop
|
|
2656
|
+
/// variable at the use site) — including a nested `<use>` with the
|
|
2657
|
+
/// defaulted arg omitted, and a second use with no children (empty slot).
|
|
2658
|
+
pub const import_view_sources = [_]canvas.ui_markup.SourceFile{
|
|
2659
|
+
.{ .path = "view.native", .source =
|
|
2660
|
+
\\<import src="components/pills.native"/>
|
|
2661
|
+
\\<row gap="8">
|
|
2662
|
+
\\ <use template="pill-stack" title="Top">
|
|
2663
|
+
\\ <for each="top" as="f">
|
|
2664
|
+
\\ <use template="pill" label="{f.name}" />
|
|
2665
|
+
\\ <button on-press="pick:{f.id}">{f.name}</button>
|
|
2666
|
+
\\ </for>
|
|
2667
|
+
\\ </use>
|
|
2668
|
+
\\ <use template="pill-stack" title="Bottom" />
|
|
2669
|
+
\\</row>
|
|
2670
|
+
},
|
|
2671
|
+
.{ .path = "components/pills.native", .source =
|
|
2672
|
+
\\<import src="base.native"/>
|
|
2673
|
+
\\<template name="pill-stack" args="title">
|
|
2674
|
+
\\ <column gap="4">
|
|
2675
|
+
\\ <use template="pill" label="{title}" tone="header" />
|
|
2676
|
+
\\ <slot/>
|
|
2677
|
+
\\ </column>
|
|
2678
|
+
\\</template>
|
|
2679
|
+
},
|
|
2680
|
+
.{ .path = "components/base.native", .source =
|
|
2681
|
+
\\<template name="pill" args="label tone=muted">
|
|
2682
|
+
\\ <badge radius="md">{label} {tone}</badge>
|
|
2683
|
+
\\</template>
|
|
2684
|
+
},
|
|
2685
|
+
};
|
|
2686
|
+
|
|
2687
|
+
pub fn resolveImportSet(arena: std.mem.Allocator, set: []const canvas.ui_markup.SourceFile, root_name: []const u8) !canvas.ui_markup.MarkupDocument {
|
|
2688
|
+
var loader = canvas.ui_markup.SourceSetLoader{ .set = set };
|
|
2689
|
+
const root_source = for (set) |file| {
|
|
2690
|
+
if (std.mem.eql(u8, file.path, root_name)) break file.source;
|
|
2691
|
+
} else return error.TestUnexpectedResult;
|
|
2692
|
+
var diagnostic: canvas.ui_markup.MarkupErrorInfo = .{};
|
|
2693
|
+
return canvas.ui_markup.resolveImports(arena, root_name, root_source, loader.loader(), &diagnostic);
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2696
|
+
/// The hand-written equivalent of the import fixture: expansion happens at
|
|
2697
|
+
/// the use site and slot content inlines at the slot position, so ids and
|
|
2698
|
+
/// handlers must match this exactly.
|
|
2699
|
+
pub fn handImportView(ui: *TemplateUi, model: *const TemplateModel) TemplateUi.Node {
|
|
2700
|
+
return ui.row(.{ .gap = 8 }, .{
|
|
2701
|
+
handPillStack(ui, "Top", model.top),
|
|
2702
|
+
handPillStack(ui, "Bottom", &.{}),
|
|
2703
|
+
});
|
|
2704
|
+
}
|
|
2705
|
+
|
|
2706
|
+
fn handPillStack(ui: *TemplateUi, title: []const u8, items: []const Fruit) TemplateUi.Node {
|
|
2707
|
+
var children: std.ArrayListUnmanaged(TemplateUi.Node) = .empty;
|
|
2708
|
+
children.append(ui.arena, handPill(ui, title, "header")) catch {
|
|
2709
|
+
ui.failed = true;
|
|
2710
|
+
};
|
|
2711
|
+
for (items) |*fruit| {
|
|
2712
|
+
children.append(ui.arena, handPill(ui, fruit.name, "muted")) catch {
|
|
2713
|
+
ui.failed = true;
|
|
2714
|
+
};
|
|
2715
|
+
children.append(ui.arena, ui.button(.{ .on_press = TemplateMsg{ .pick = fruit.id } }, fruit.name)) catch {
|
|
2716
|
+
ui.failed = true;
|
|
2717
|
+
};
|
|
2718
|
+
}
|
|
2719
|
+
return ui.column(.{ .gap = 4 }, @as([]const TemplateUi.Node, children.items));
|
|
2720
|
+
}
|
|
2721
|
+
|
|
2722
|
+
fn handPill(ui: *TemplateUi, label: []const u8, tone: []const u8) TemplateUi.Node {
|
|
2723
|
+
var badge = ui.el(.badge, .{ .style_tokens = .{ .radius = .md } }, .{});
|
|
2724
|
+
badge.widget.text = ui.fmt("{s} {s}", .{ label, tone });
|
|
2725
|
+
return badge;
|
|
2726
|
+
}
|
|
2727
|
+
|
|
2728
|
+
test "imported templates with slots and defaults build the hand-written tree" {
|
|
2729
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2730
|
+
defer arena_state.deinit();
|
|
2731
|
+
const arena = arena_state.allocator();
|
|
2732
|
+
|
|
2733
|
+
const model = templateTestModel();
|
|
2734
|
+
const TemplateMarkup = markup_view.MarkupView(TemplateModel, TemplateMsg);
|
|
2735
|
+
|
|
2736
|
+
const document = try resolveImportSet(arena, &import_view_sources, "view.native");
|
|
2737
|
+
var view = TemplateMarkup.fromDocument(document);
|
|
2738
|
+
var markup_ui = TemplateUi.init(arena);
|
|
2739
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
2740
|
+
|
|
2741
|
+
var hand_ui = TemplateUi.init(arena);
|
|
2742
|
+
const hand_tree = try hand_ui.finalize(handImportView(&hand_ui, &model));
|
|
2743
|
+
|
|
2744
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2745
|
+
defer markup_ids.deinit(testing.allocator);
|
|
2746
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2747
|
+
defer hand_ids.deinit(testing.allocator);
|
|
2748
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
2749
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
2750
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
2751
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
2752
|
+
|
|
2753
|
+
// The defaulted arg: explicit at the header use, defaulted inside
|
|
2754
|
+
// slot content.
|
|
2755
|
+
try testing.expect(findByText(markup_tree.root, .badge, "Top header") != null);
|
|
2756
|
+
try testing.expect(findByText(markup_tree.root, .badge, "apple muted") != null);
|
|
2757
|
+
// Slot content saw the consumer's loop variable; its handler
|
|
2758
|
+
// dispatches the loop item's payload.
|
|
2759
|
+
const pear_button = findByText(markup_tree.root, .button, "pear").?;
|
|
2760
|
+
try testing.expectEqual(@as(u32, 2), markup_tree.msgForPointer(pear_button.id, .up).?.pick);
|
|
2761
|
+
// The childless use renders an empty slot: just the header pill.
|
|
2762
|
+
const bottom = findByText(markup_tree.root, .badge, "Bottom header").?;
|
|
2763
|
+
_ = bottom;
|
|
2764
|
+
try testing.expectEqual(@as(usize, 1), markup_tree.root.children[1].children.len);
|
|
2765
|
+
}
|
|
2766
|
+
|
|
2767
|
+
test "slot, default, and component-file misuse fails the interpreter build with teaching messages" {
|
|
2768
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2769
|
+
defer arena_state.deinit();
|
|
2770
|
+
const arena = arena_state.allocator();
|
|
2771
|
+
const model = Model{};
|
|
2772
|
+
|
|
2773
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
2774
|
+
.{
|
|
2775
|
+
.source = "<template name=\"t\"><text>x</text></template>\n<row>\n <use template=\"t\"><text>y</text></use>\n</row>",
|
|
2776
|
+
.message = canvas.ui_markup.use_children_without_slot_message,
|
|
2777
|
+
},
|
|
2778
|
+
.{
|
|
2779
|
+
.source = "<template name=\"t\"><column><slot/><slot/></column></template>\n<row>\n <use template=\"t\" />\n</row>",
|
|
2780
|
+
.message = canvas.ui_markup.template_one_slot_message,
|
|
2781
|
+
},
|
|
2782
|
+
.{
|
|
2783
|
+
.source = "<template name=\"t\" args=\"tone={filter}\"><text>{tone}</text></template>\n<row>\n <use template=\"t\" />\n</row>",
|
|
2784
|
+
.message = canvas.ui_markup.template_default_literal_message,
|
|
2785
|
+
},
|
|
2786
|
+
.{
|
|
2787
|
+
// Quote characters in a default are literal text, not string
|
|
2788
|
+
// delimiters.
|
|
2789
|
+
.source = "<template name=\"t\" args=\"tone='soft'\"><text>{tone}</text></template>\n<row>\n <use template=\"t\" />\n</row>",
|
|
2790
|
+
.message = canvas.ui_markup.template_default_quoted_message,
|
|
2791
|
+
},
|
|
2792
|
+
.{
|
|
2793
|
+
// A component file (all templates) is not a view.
|
|
2794
|
+
.source = "<template name=\"t\"><text>x</text></template>",
|
|
2795
|
+
.message = canvas.ui_markup.component_file_view_message,
|
|
2796
|
+
},
|
|
2797
|
+
.{
|
|
2798
|
+
// Unresolved imports never reach the interpreter silently.
|
|
2799
|
+
.source = "<import src=\"components.native\"/>\n<row />",
|
|
2800
|
+
.message = canvas.ui_markup.import_unresolved_message,
|
|
2801
|
+
},
|
|
2802
|
+
};
|
|
2803
|
+
for (cases) |case| {
|
|
2804
|
+
var view = try InboxMarkup.init(arena, case.source);
|
|
2805
|
+
var ui = InboxUi.init(arena);
|
|
2806
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
2807
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
2808
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
2809
|
+
}
|
|
2810
|
+
}
|
|
2811
|
+
|
|
2812
|
+
test "interpreter defaults resolve per use site and literals only" {
|
|
2813
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2814
|
+
defer arena_state.deinit();
|
|
2815
|
+
const arena = arena_state.allocator();
|
|
2816
|
+
const model = Model{};
|
|
2817
|
+
|
|
2818
|
+
const source =
|
|
2819
|
+
\\<template name="tag" args="label trend=flat count=0">
|
|
2820
|
+
\\ <badge>{label} {trend} {count}</badge>
|
|
2821
|
+
\\</template>
|
|
2822
|
+
\\<row gap="4">
|
|
2823
|
+
\\ <use template="tag" label="a" />
|
|
2824
|
+
\\ <use template="tag" label="b" trend="up" count="3" />
|
|
2825
|
+
\\</row>
|
|
2826
|
+
;
|
|
2827
|
+
var view = try InboxMarkup.init(arena, source);
|
|
2828
|
+
var ui = InboxUi.init(arena);
|
|
2829
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
2830
|
+
try testing.expect(findByText(tree.root, .badge, "a flat 0") != null);
|
|
2831
|
+
try testing.expect(findByText(tree.root, .badge, "b up 3") != null);
|
|
2832
|
+
}
|
|
2833
|
+
|
|
2834
|
+
test "a bare name= declares an empty-string default that renders as empty" {
|
|
2835
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2836
|
+
defer arena_state.deinit();
|
|
2837
|
+
const arena = arena_state.allocator();
|
|
2838
|
+
const model = Model{};
|
|
2839
|
+
|
|
2840
|
+
const source =
|
|
2841
|
+
\\<template name="tag" args="label suffix=">
|
|
2842
|
+
\\ <badge>{label}{suffix}!</badge>
|
|
2843
|
+
\\</template>
|
|
2844
|
+
\\<row gap="4">
|
|
2845
|
+
\\ <use template="tag" label="a" />
|
|
2846
|
+
\\ <use template="tag" label="b" suffix="-x" />
|
|
2847
|
+
\\</row>
|
|
2848
|
+
;
|
|
2849
|
+
// The bare form passes structural validation (only quoted and
|
|
2850
|
+
// {binding} defaults are rejected)...
|
|
2851
|
+
var parser = canvas.ui_markup.Parser.init(arena, source);
|
|
2852
|
+
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
|
|
2853
|
+
// ...and the omitted arg interpolates as the empty string.
|
|
2854
|
+
var view = try InboxMarkup.init(arena, source);
|
|
2855
|
+
var ui = InboxUi.init(arena);
|
|
2856
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
2857
|
+
try testing.expect(findByText(tree.root, .badge, "a!") != null);
|
|
2858
|
+
try testing.expect(findByText(tree.root, .badge, "b-x!") != null);
|
|
2859
|
+
}
|
|
2860
|
+
|
|
2861
|
+
test "binding a TextBuffer field directly fails with the edit-model teaching message" {
|
|
2862
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2863
|
+
defer arena_state.deinit();
|
|
2864
|
+
const arena = arena_state.allocator();
|
|
2865
|
+
|
|
2866
|
+
const DraftModel = struct {
|
|
2867
|
+
draft_buffer: canvas.TextBuffer(32) = .{},
|
|
2868
|
+
|
|
2869
|
+
pub fn draft(model: *const @This()) []const u8 {
|
|
2870
|
+
return model.draft_buffer.text();
|
|
2871
|
+
}
|
|
2872
|
+
};
|
|
2873
|
+
const DraftMsg = union(enum) { input: canvas.TextInputEvent };
|
|
2874
|
+
const DraftMarkup = markup_view.MarkupView(DraftModel, DraftMsg);
|
|
2875
|
+
const DraftUi = canvas.Ui(DraftMsg);
|
|
2876
|
+
|
|
2877
|
+
var model = DraftModel{};
|
|
2878
|
+
model.draft_buffer.set("hello");
|
|
2879
|
+
|
|
2880
|
+
// The buffer is the edit model, not the text: binding it directly
|
|
2881
|
+
// teaches the pub fn accessor shape instead of the generic miss.
|
|
2882
|
+
var bad = try DraftMarkup.init(arena, "<column>\n <text>{draft_buffer}</text>\n</column>");
|
|
2883
|
+
var bad_ui = DraftUi.init(arena);
|
|
2884
|
+
try testing.expectError(error.MarkupBuild, bad.build(&bad_ui, &model));
|
|
2885
|
+
try testing.expectEqualStrings(canvas.ui_markup.binding_text_buffer_message, bad.diagnostic.message);
|
|
2886
|
+
try testing.expect(bad.diagnostic.line > 0);
|
|
2887
|
+
|
|
2888
|
+
// The taught shape works: a pub fn returning the buffer's text.
|
|
2889
|
+
var good = try DraftMarkup.init(arena, "<column>\n <text>{draft}</text>\n</column>");
|
|
2890
|
+
var good_ui = DraftUi.init(arena);
|
|
2891
|
+
const tree = try good_ui.finalize(try good.build(&good_ui, &model));
|
|
2892
|
+
try testing.expect(findByText(tree.root, .text, "hello") != null);
|
|
2893
|
+
}
|
|
2894
|
+
|
|
2895
|
+
// ------------------------------------------------------------ chart fixture
|
|
2896
|
+
|
|
2897
|
+
pub const ChartMsg = union(enum) {
|
|
2898
|
+
refresh,
|
|
2899
|
+
};
|
|
2900
|
+
|
|
2901
|
+
pub const ChartModel = struct {
|
|
2902
|
+
/// A history window with a NaN-padded leading gap: missing samples
|
|
2903
|
+
/// draw nothing, so the trace enters from the right edge (the padding
|
|
2904
|
+
/// idiom the markup docs teach as a model fn).
|
|
2905
|
+
cpu_history: [6]f32 = .{ std.math.nan(f32), std.math.nan(f32), 0.2, 0.4, 0.35, 0.8 },
|
|
2906
|
+
latency: []const f32 = &.{ 12, 18, 9, 22 },
|
|
2907
|
+
names: []const []const u8 = &.{ "cpu", "mem" },
|
|
2908
|
+
/// One category label per cpu_history sample (the x-labels channel).
|
|
2909
|
+
months: []const []const u8 = &.{ "jan", "feb", "mar", "apr", "may", "jun" },
|
|
2910
|
+
limit: f32 = 1,
|
|
2911
|
+
|
|
2912
|
+
/// Arena-computed series: the same fn shape `for each` accepts.
|
|
2913
|
+
pub fn load(model: *const ChartModel, arena: std.mem.Allocator) []const f32 {
|
|
2914
|
+
const out = arena.alloc(f32, model.latency.len) catch return &.{};
|
|
2915
|
+
for (model.latency, out) |sample, *slot| slot.* = sample / 2;
|
|
2916
|
+
return out;
|
|
2917
|
+
}
|
|
2918
|
+
};
|
|
2919
|
+
|
|
2920
|
+
pub const chart_markup_source =
|
|
2921
|
+
\\<column gap="8">
|
|
2922
|
+
\\ <chart width="240" height="48" y-min="0" y-max="{limit}" grid-lines="2" baseline="true" x-labels="{months}" y-labels="true" hover-details="true" label="CPU history">
|
|
2923
|
+
\\ <series kind="bar" values="{cpu_history}" color="accent" label="cpu" />
|
|
2924
|
+
\\ <series kind="area" values="{load}" color="info" />
|
|
2925
|
+
\\ </chart>
|
|
2926
|
+
\\ <chart grow="1" stroke-width="2">
|
|
2927
|
+
\\ <series values="{latency}" />
|
|
2928
|
+
\\ </chart>
|
|
2929
|
+
\\</column>
|
|
2930
|
+
;
|
|
2931
|
+
|
|
2932
|
+
pub const ChartUi = canvas.Ui(ChartMsg);
|
|
2933
|
+
|
|
2934
|
+
/// The hand-written equivalent of the chart markup: both engines must
|
|
2935
|
+
/// build exactly what direct `Ui.chart` calls produce (area is the markup
|
|
2936
|
+
/// spelling of a filled line).
|
|
2937
|
+
pub fn handChartView(ui: *ChartUi, model: *const ChartModel) ChartUi.Node {
|
|
2938
|
+
return ui.column(.{ .gap = 8 }, .{
|
|
2939
|
+
ui.chart(.{
|
|
2940
|
+
.width = 240,
|
|
2941
|
+
.height = 48,
|
|
2942
|
+
.y_min = 0,
|
|
2943
|
+
.y_max = model.limit,
|
|
2944
|
+
.grid_lines = 2,
|
|
2945
|
+
.baseline = true,
|
|
2946
|
+
.x_labels = model.months,
|
|
2947
|
+
.y_labels = true,
|
|
2948
|
+
.hover_details = true,
|
|
2949
|
+
.semantics = .{ .label = "CPU history" },
|
|
2950
|
+
}, &.{
|
|
2951
|
+
.{ .kind = .bar, .values = &model.cpu_history, .color = .accent, .label = "cpu" },
|
|
2952
|
+
.{ .kind = .line, .fill = true, .values = model.load(ui.arena), .color = .info },
|
|
2953
|
+
}),
|
|
2954
|
+
ui.chart(.{ .grow = 1, .stroke_width = 2 }, &.{
|
|
2955
|
+
.{ .kind = .line, .values = model.latency },
|
|
2956
|
+
}),
|
|
2957
|
+
});
|
|
2958
|
+
}
|
|
2959
|
+
|
|
2960
|
+
fn collectChartWidgets(widget: canvas.Widget, out: *std.ArrayListUnmanaged(canvas.Widget), allocator: std.mem.Allocator) !void {
|
|
2961
|
+
if (widget.kind == .chart) try out.append(allocator, widget);
|
|
2962
|
+
for (widget.children) |child| {
|
|
2963
|
+
try collectChartWidgets(child, out, allocator);
|
|
2964
|
+
}
|
|
2965
|
+
}
|
|
2966
|
+
|
|
2967
|
+
test "the chart element builds the hand-written Ui.chart tree with series bindings" {
|
|
2968
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
2969
|
+
defer arena_state.deinit();
|
|
2970
|
+
const arena = arena_state.allocator();
|
|
2971
|
+
|
|
2972
|
+
const model = ChartModel{};
|
|
2973
|
+
const ChartMarkup = markup_view.MarkupView(ChartModel, ChartMsg);
|
|
2974
|
+
|
|
2975
|
+
var view = try ChartMarkup.init(arena, chart_markup_source);
|
|
2976
|
+
var markup_ui = ChartUi.init(arena);
|
|
2977
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
2978
|
+
|
|
2979
|
+
var hand_ui = ChartUi.init(arena);
|
|
2980
|
+
const hand_tree = try hand_ui.finalize(handChartView(&hand_ui, &model));
|
|
2981
|
+
|
|
2982
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2983
|
+
defer markup_ids.deinit(testing.allocator);
|
|
2984
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
2985
|
+
defer hand_ids.deinit(testing.allocator);
|
|
2986
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
2987
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
2988
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
2989
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
2990
|
+
|
|
2991
|
+
var charts: std.ArrayListUnmanaged(canvas.Widget) = .empty;
|
|
2992
|
+
defer charts.deinit(testing.allocator);
|
|
2993
|
+
try collectChartWidgets(markup_tree.root, &charts, testing.allocator);
|
|
2994
|
+
try testing.expectEqual(@as(usize, 2), charts.items.len);
|
|
2995
|
+
|
|
2996
|
+
// The first chart: options and both series land exactly as the
|
|
2997
|
+
// builder call would set them.
|
|
2998
|
+
const cpu_chart = charts.items[0];
|
|
2999
|
+
try testing.expectEqual(@as(?f32, 0), cpu_chart.chart.y_min);
|
|
3000
|
+
try testing.expectEqual(@as(?f32, 1), cpu_chart.chart.y_max);
|
|
3001
|
+
try testing.expectEqual(@as(u8, 2), cpu_chart.chart.grid_lines);
|
|
3002
|
+
try testing.expect(cpu_chart.chart.baseline);
|
|
3003
|
+
// The axis/hover options land: x-labels bind the model's string
|
|
3004
|
+
// iterable, the flags flip their features on.
|
|
3005
|
+
try testing.expectEqual(@as(usize, 6), cpu_chart.chart.x_labels.len);
|
|
3006
|
+
try testing.expectEqualStrings("jan", cpu_chart.chart.x_labels[0]);
|
|
3007
|
+
try testing.expectEqualStrings("jun", cpu_chart.chart.x_labels[5]);
|
|
3008
|
+
try testing.expect(cpu_chart.chart.y_labels);
|
|
3009
|
+
try testing.expect(cpu_chart.chart.hover_details);
|
|
3010
|
+
try testing.expectEqualStrings("CPU history", cpu_chart.semantics.label);
|
|
3011
|
+
try testing.expectEqual(@as(usize, 2), cpu_chart.chart.series.len);
|
|
3012
|
+
const bar_series = cpu_chart.chart.series[0];
|
|
3013
|
+
try testing.expectEqual(canvas.ChartSeriesKind.bar, bar_series.kind);
|
|
3014
|
+
try testing.expectEqual(canvas.ChartSeriesColor.accent, bar_series.color);
|
|
3015
|
+
try testing.expectEqualStrings("cpu", bar_series.label);
|
|
3016
|
+
// NaN gaps pass through the binding untouched: missing samples draw
|
|
3017
|
+
// nothing instead of a zero bar.
|
|
3018
|
+
try testing.expectEqual(@as(usize, 6), bar_series.values.len);
|
|
3019
|
+
try testing.expect(std.math.isNan(bar_series.values[0]));
|
|
3020
|
+
try testing.expectEqual(@as(f32, 0.8), bar_series.values[5]);
|
|
3021
|
+
// Area is the markup spelling of a filled line, over the arena fn.
|
|
3022
|
+
const area_series = cpu_chart.chart.series[1];
|
|
3023
|
+
try testing.expectEqual(canvas.ChartSeriesKind.line, area_series.kind);
|
|
3024
|
+
try testing.expect(area_series.fill);
|
|
3025
|
+
try testing.expectEqual(canvas.ChartSeriesColor.info, area_series.color);
|
|
3026
|
+
try testing.expectEqual(@as(f32, 6), area_series.values[0]);
|
|
3027
|
+
|
|
3028
|
+
// The second chart: unlabeled charts get the builder's generated
|
|
3029
|
+
// series summary, so automation reads the data without pixels.
|
|
3030
|
+
const latency_chart = charts.items[1];
|
|
3031
|
+
var hand_charts: std.ArrayListUnmanaged(canvas.Widget) = .empty;
|
|
3032
|
+
defer hand_charts.deinit(testing.allocator);
|
|
3033
|
+
try collectChartWidgets(hand_tree.root, &hand_charts, testing.allocator);
|
|
3034
|
+
try testing.expectEqualStrings(hand_charts.items[1].semantics.label, latency_chart.semantics.label);
|
|
3035
|
+
try testing.expectEqualStrings("chart: line 4 pts last 22.00", latency_chart.semantics.label);
|
|
3036
|
+
try testing.expectEqual(@as(?f32, 2), latency_chart.style.stroke_width);
|
|
3037
|
+
}
|
|
3038
|
+
|
|
3039
|
+
test "chart misuse fails the build with teaching messages" {
|
|
3040
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3041
|
+
defer arena_state.deinit();
|
|
3042
|
+
const arena = arena_state.allocator();
|
|
3043
|
+
const model = ChartModel{};
|
|
3044
|
+
const ChartMarkup = markup_view.MarkupView(ChartModel, ChartMsg);
|
|
3045
|
+
|
|
3046
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
3047
|
+
.{
|
|
3048
|
+
// values must name an f32 iterable (names iterates strings).
|
|
3049
|
+
.source = "<column>\n <chart>\n <series values=\"{names}\" />\n </chart>\n</column>",
|
|
3050
|
+
.message = canvas.ui_markup.series_values_message,
|
|
3051
|
+
},
|
|
3052
|
+
.{
|
|
3053
|
+
// A scalar binding is not a series.
|
|
3054
|
+
.source = "<column>\n <chart>\n <series values=\"{limit}\" />\n </chart>\n</column>",
|
|
3055
|
+
.message = canvas.ui_markup.series_values_message,
|
|
3056
|
+
},
|
|
3057
|
+
.{
|
|
3058
|
+
// Missing values entirely.
|
|
3059
|
+
.source = "<column>\n <chart>\n <series kind=\"bar\" />\n </chart>\n</column>",
|
|
3060
|
+
.message = canvas.ui_markup.series_values_message,
|
|
3061
|
+
},
|
|
3062
|
+
.{
|
|
3063
|
+
// Band needs a paired lower edge: builder territory.
|
|
3064
|
+
.source = "<column>\n <chart>\n <series kind=\"band\" values=\"{latency}\" />\n </chart>\n</column>",
|
|
3065
|
+
.message = canvas.ui_markup.series_kind_message,
|
|
3066
|
+
},
|
|
3067
|
+
.{
|
|
3068
|
+
// Closed chart attribute set.
|
|
3069
|
+
.source = "<column>\n <chart gap=\"8\">\n <series values=\"{latency}\" />\n </chart>\n</column>",
|
|
3070
|
+
.message = canvas.ui_markup.chart_attr_message,
|
|
3071
|
+
},
|
|
3072
|
+
.{
|
|
3073
|
+
// Charts are display-only; presses fall through like text.
|
|
3074
|
+
.source = "<column>\n <chart on-press=\"refresh\">\n <series values=\"{latency}\" />\n </chart>\n</column>",
|
|
3075
|
+
.message = canvas.ui_markup.chart_display_only_message,
|
|
3076
|
+
},
|
|
3077
|
+
.{
|
|
3078
|
+
// A chart with no series can never draw anything.
|
|
3079
|
+
.source = "<column>\n <chart />\n</column>",
|
|
3080
|
+
.message = canvas.ui_markup.chart_series_required_message,
|
|
3081
|
+
},
|
|
3082
|
+
.{
|
|
3083
|
+
// Only series children; the series set is static.
|
|
3084
|
+
.source = "<column>\n <chart>\n <text>x</text>\n </chart>\n</column>",
|
|
3085
|
+
.message = canvas.ui_markup.chart_children_message,
|
|
3086
|
+
},
|
|
3087
|
+
.{
|
|
3088
|
+
// Series outside a chart have no plot to land in.
|
|
3089
|
+
.source = "<column>\n <series values=\"{latency}\" />\n</column>",
|
|
3090
|
+
.message = canvas.ui_markup.series_parent_message,
|
|
3091
|
+
},
|
|
3092
|
+
.{
|
|
3093
|
+
// Closed series attribute set (fill is spelled kind="area").
|
|
3094
|
+
.source = "<column>\n <chart>\n <series values=\"{latency}\" fill=\"true\" />\n </chart>\n</column>",
|
|
3095
|
+
.message = canvas.ui_markup.series_attr_message,
|
|
3096
|
+
},
|
|
3097
|
+
.{
|
|
3098
|
+
// Series colors are the closed token vocabulary.
|
|
3099
|
+
.source = "<column>\n <chart>\n <series values=\"{latency}\" color=\"magenta\" />\n </chart>\n</column>",
|
|
3100
|
+
.message = canvas.ui_markup.series_color_message,
|
|
3101
|
+
},
|
|
3102
|
+
};
|
|
3103
|
+
for (cases) |case| {
|
|
3104
|
+
var view = try ChartMarkup.init(arena, case.source);
|
|
3105
|
+
var ui = ChartUi.init(arena);
|
|
3106
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
3107
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
3108
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
3109
|
+
}
|
|
3110
|
+
}
|
|
3111
|
+
|
|
3112
|
+
// ------------------------------------------------------ input-group fixture
|
|
3113
|
+
|
|
3114
|
+
pub const ComposerMsg = union(enum) {
|
|
3115
|
+
edit: canvas.TextInputEvent,
|
|
3116
|
+
send,
|
|
3117
|
+
attach,
|
|
3118
|
+
};
|
|
3119
|
+
|
|
3120
|
+
pub const ComposerModel = struct {
|
|
3121
|
+
draft_buffer: canvas.TextBuffer(64) = .{},
|
|
3122
|
+
streaming: bool = false,
|
|
3123
|
+
|
|
3124
|
+
pub fn draft(model: *const ComposerModel) []const u8 {
|
|
3125
|
+
return model.draft_buffer.text();
|
|
3126
|
+
}
|
|
3127
|
+
};
|
|
3128
|
+
|
|
3129
|
+
pub const ComposerUi = canvas.Ui(ComposerMsg);
|
|
3130
|
+
|
|
3131
|
+
pub const composer_markup_source =
|
|
3132
|
+
\\<column gap="8">
|
|
3133
|
+
\\ <input-group label="Message composer" height="120">
|
|
3134
|
+
\\ <textarea text="{draft}" placeholder="Type a message" on-input="edit" label="Message" />
|
|
3135
|
+
\\ <input-group-actions>
|
|
3136
|
+
\\ <button icon="plus" variant="ghost" size="icon" on-press="attach" label="Attach"></button>
|
|
3137
|
+
\\ <spacer grow="1" />
|
|
3138
|
+
\\ <button icon="send" size="icon" on-press="send" label="Send"></button>
|
|
3139
|
+
\\ </input-group-actions>
|
|
3140
|
+
\\ </input-group>
|
|
3141
|
+
\\</column>
|
|
3142
|
+
;
|
|
3143
|
+
|
|
3144
|
+
/// The hand-written equivalent of the composer markup: both engines must
|
|
3145
|
+
/// build exactly what direct `Ui.inputGroup` calls produce (the entry
|
|
3146
|
+
/// first, then the accessory row — leading control, spacer, trailing
|
|
3147
|
+
/// send).
|
|
3148
|
+
pub fn handComposerView(ui: *ComposerUi, model: *const ComposerModel) ComposerUi.Node {
|
|
3149
|
+
const entry = ui.el(.textarea, .{
|
|
3150
|
+
.text = model.draft(),
|
|
3151
|
+
.placeholder = "Type a message",
|
|
3152
|
+
.on_input = ComposerUi.inputMsg(.edit),
|
|
3153
|
+
.semantics = .{ .label = "Message" },
|
|
3154
|
+
}, .{});
|
|
3155
|
+
return ui.column(.{ .gap = 8 }, .{
|
|
3156
|
+
ui.inputGroup(.{
|
|
3157
|
+
.height = 120,
|
|
3158
|
+
.semantics = .{ .label = "Message composer" },
|
|
3159
|
+
}, entry, ui.inputGroupActions(.{}, .{
|
|
3160
|
+
ui.el(.button, .{ .icon = "plus", .variant = .ghost, .size = .icon, .on_press = .attach, .semantics = .{ .label = "Attach" } }, .{}),
|
|
3161
|
+
ui.spacer(1),
|
|
3162
|
+
ui.el(.button, .{ .icon = "send", .size = .icon, .on_press = .send, .semantics = .{ .label = "Send" } }, .{}),
|
|
3163
|
+
})),
|
|
3164
|
+
});
|
|
3165
|
+
}
|
|
3166
|
+
|
|
3167
|
+
test "the input-group element builds the hand-written Ui.inputGroup tree" {
|
|
3168
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3169
|
+
defer arena_state.deinit();
|
|
3170
|
+
const arena = arena_state.allocator();
|
|
3171
|
+
|
|
3172
|
+
var model = ComposerModel{};
|
|
3173
|
+
model.draft_buffer.set("hello");
|
|
3174
|
+
const ComposerMarkup = markup_view.MarkupView(ComposerModel, ComposerMsg);
|
|
3175
|
+
|
|
3176
|
+
var view = try ComposerMarkup.init(arena, composer_markup_source);
|
|
3177
|
+
var markup_ui = ComposerUi.init(arena);
|
|
3178
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
3179
|
+
|
|
3180
|
+
var hand_ui = ComposerUi.init(arena);
|
|
3181
|
+
const hand_tree = try hand_ui.finalize(handComposerView(&hand_ui, &model));
|
|
3182
|
+
|
|
3183
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
3184
|
+
defer markup_ids.deinit(testing.allocator);
|
|
3185
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
3186
|
+
defer hand_ids.deinit(testing.allocator);
|
|
3187
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
3188
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
3189
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
3190
|
+
try testing.expectEqual(hand_tree.handlers.len, markup_tree.handlers.len);
|
|
3191
|
+
|
|
3192
|
+
// The group: one labeled field with group semantics and two children
|
|
3193
|
+
// (the entry, then the actions row).
|
|
3194
|
+
const group = findByKind(markup_tree.root, .input_group).?;
|
|
3195
|
+
try testing.expectEqualStrings("Message composer", group.semantics.label);
|
|
3196
|
+
try testing.expectEqual(canvas.WidgetRole.group, group.semantics.role);
|
|
3197
|
+
try testing.expectEqual(@as(usize, 2), group.children.len);
|
|
3198
|
+
|
|
3199
|
+
// The entry: chrome dissolved (transparent fill/border/focus ring —
|
|
3200
|
+
// the GROUP wears that chrome) and grow-stretched to absorb the
|
|
3201
|
+
// group's height; its text behavior is untouched.
|
|
3202
|
+
const entry = group.children[0];
|
|
3203
|
+
try testing.expectEqual(canvas.WidgetKind.textarea, entry.kind);
|
|
3204
|
+
try testing.expectEqualStrings("hello", entry.text);
|
|
3205
|
+
try testing.expectEqualStrings("Type a message", entry.placeholder);
|
|
3206
|
+
try testing.expectEqual(@as(f32, 1), entry.layout.grow);
|
|
3207
|
+
try testing.expectEqual(@as(u8, 0), entry.style.background.?.a);
|
|
3208
|
+
try testing.expectEqual(@as(u8, 0), entry.style.border.?.a);
|
|
3209
|
+
try testing.expectEqual(@as(u8, 0), entry.style.focus_ring.?.a);
|
|
3210
|
+
|
|
3211
|
+
// The actions row: a plain row inside the group's border with the
|
|
3212
|
+
// leading/spacer/trailing children.
|
|
3213
|
+
const actions = group.children[1];
|
|
3214
|
+
try testing.expectEqual(canvas.WidgetKind.row, actions.kind);
|
|
3215
|
+
try testing.expectEqual(@as(usize, 3), actions.children.len);
|
|
3216
|
+
try testing.expectEqualStrings("plus", actions.children[0].icon);
|
|
3217
|
+
try testing.expectEqualStrings("send", actions.children[2].icon);
|
|
3218
|
+
}
|
|
3219
|
+
|
|
3220
|
+
test "input-group misuse fails the build with teaching messages" {
|
|
3221
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3222
|
+
defer arena_state.deinit();
|
|
3223
|
+
const arena = arena_state.allocator();
|
|
3224
|
+
const model = ComposerModel{};
|
|
3225
|
+
const ComposerMarkup = markup_view.MarkupView(ComposerModel, ComposerMsg);
|
|
3226
|
+
|
|
3227
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
3228
|
+
.{
|
|
3229
|
+
// The group requires its text entry.
|
|
3230
|
+
.source = "<column>\n <input-group label=\"Composer\">\n <input-group-actions>\n <button icon=\"send\" label=\"Send\" on-press=\"send\"></button>\n </input-group-actions>\n </input-group>\n</column>",
|
|
3231
|
+
.message = canvas.ui_markup.input_group_textarea_message,
|
|
3232
|
+
},
|
|
3233
|
+
.{
|
|
3234
|
+
// An empty group has nothing to wrap.
|
|
3235
|
+
.source = "<column>\n <input-group label=\"Composer\" />\n</column>",
|
|
3236
|
+
.message = canvas.ui_markup.input_group_textarea_message,
|
|
3237
|
+
},
|
|
3238
|
+
.{
|
|
3239
|
+
// One text entry per group.
|
|
3240
|
+
.source = "<column>\n <input-group>\n <textarea label=\"A\" placeholder=\"a\" />\n <textarea label=\"B\" placeholder=\"b\" />\n </input-group>\n</column>",
|
|
3241
|
+
.message = canvas.ui_markup.input_group_children_message,
|
|
3242
|
+
},
|
|
3243
|
+
.{
|
|
3244
|
+
// Other content lives outside the group.
|
|
3245
|
+
.source = "<column>\n <input-group>\n <textarea label=\"A\" placeholder=\"a\" />\n <text>hint</text>\n </input-group>\n</column>",
|
|
3246
|
+
.message = canvas.ui_markup.input_group_children_message,
|
|
3247
|
+
},
|
|
3248
|
+
.{
|
|
3249
|
+
// Closed group attribute set.
|
|
3250
|
+
.source = "<column>\n <input-group gap=\"8\">\n <textarea label=\"A\" placeholder=\"a\" />\n </input-group>\n</column>",
|
|
3251
|
+
.message = canvas.ui_markup.input_group_attr_message,
|
|
3252
|
+
},
|
|
3253
|
+
.{
|
|
3254
|
+
// Closed actions attribute set.
|
|
3255
|
+
.source = "<column>\n <input-group>\n <textarea label=\"A\" placeholder=\"a\" />\n <input-group-actions padding=\"4\">\n <button icon=\"send\" label=\"Send\" on-press=\"send\"></button>\n </input-group-actions>\n </input-group>\n</column>",
|
|
3256
|
+
.message = canvas.ui_markup.input_group_actions_attr_message,
|
|
3257
|
+
},
|
|
3258
|
+
.{
|
|
3259
|
+
// Actions rows belong inside a group.
|
|
3260
|
+
.source = "<column>\n <input-group-actions>\n <button icon=\"send\" label=\"Send\" on-press=\"send\"></button>\n </input-group-actions>\n</column>",
|
|
3261
|
+
.message = canvas.ui_markup.input_group_actions_parent_message,
|
|
3262
|
+
},
|
|
3263
|
+
};
|
|
3264
|
+
for (cases) |case| {
|
|
3265
|
+
var view = try ComposerMarkup.init(arena, case.source);
|
|
3266
|
+
var ui = ComposerUi.init(arena);
|
|
3267
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
3268
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
3269
|
+
try testing.expect(view.diagnostic.line > 0);
|
|
3270
|
+
}
|
|
3271
|
+
}
|
|
3272
|
+
|
|
3273
|
+
test "input-group actions render conditional controls through structure tags" {
|
|
3274
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3275
|
+
defer arena_state.deinit();
|
|
3276
|
+
const arena = arena_state.allocator();
|
|
3277
|
+
const ComposerMarkup = markup_view.MarkupView(ComposerModel, ComposerMsg);
|
|
3278
|
+
|
|
3279
|
+
// Conditional content belongs INSIDE the actions row (the group's
|
|
3280
|
+
// child shape stays static): a streaming composer swaps send for
|
|
3281
|
+
// stop without changing the group's structure.
|
|
3282
|
+
const source =
|
|
3283
|
+
"<column>\n <input-group label=\"Composer\">\n <textarea label=\"Message\" placeholder=\"m\" />\n <input-group-actions>\n <if test=\"{streaming}\">\n <button icon=\"x\" label=\"Stop\" on-press=\"send\"></button>\n </if>\n <else>\n <button icon=\"send\" label=\"Send\" on-press=\"send\"></button>\n </else>\n </input-group-actions>\n </input-group>\n</column>";
|
|
3284
|
+
|
|
3285
|
+
const idle = ComposerModel{};
|
|
3286
|
+
var view = try ComposerMarkup.init(arena, source);
|
|
3287
|
+
var idle_ui = ComposerUi.init(arena);
|
|
3288
|
+
const idle_tree = try idle_ui.finalize(try view.build(&idle_ui, &idle));
|
|
3289
|
+
try testing.expectEqualStrings("send", findByKind(idle_tree.root, .button).?.icon);
|
|
3290
|
+
|
|
3291
|
+
const streaming = ComposerModel{ .streaming = true };
|
|
3292
|
+
var streaming_view = try ComposerMarkup.init(arena, source);
|
|
3293
|
+
var streaming_ui = ComposerUi.init(arena);
|
|
3294
|
+
const streaming_tree = try streaming_ui.finalize(try streaming_view.build(&streaming_ui, &streaming));
|
|
3295
|
+
try testing.expectEqualStrings("x", findByKind(streaming_tree.root, .button).?.icon);
|
|
3296
|
+
}
|
|
3297
|
+
|
|
3298
|
+
test "chart series values resolve through slice-valued template args" {
|
|
3299
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3300
|
+
defer arena_state.deinit();
|
|
3301
|
+
const arena = arena_state.allocator();
|
|
3302
|
+
|
|
3303
|
+
const model = ChartModel{};
|
|
3304
|
+
const ChartMarkup = markup_view.MarkupView(ChartModel, ChartMsg);
|
|
3305
|
+
|
|
3306
|
+
// The values binding resolves through the SAME set as `for each`:
|
|
3307
|
+
// a slice-valued template arg shadows the model here.
|
|
3308
|
+
const source =
|
|
3309
|
+
"<template name=\"spark\" args=\"data\"><chart height=\"32\"><series kind=\"area\" values=\"{data}\" /></chart></template>\n" ++
|
|
3310
|
+
"<column>\n <use template=\"spark\" data=\"{latency}\" />\n</column>";
|
|
3311
|
+
var view = try ChartMarkup.init(arena, source);
|
|
3312
|
+
var ui = ChartUi.init(arena);
|
|
3313
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
3314
|
+
const chart_widget = findByKind(tree.root, .chart).?;
|
|
3315
|
+
try testing.expectEqual(@as(usize, 1), chart_widget.chart.series.len);
|
|
3316
|
+
try testing.expectEqual(@as(f32, 12), chart_widget.chart.series[0].values[0]);
|
|
3317
|
+
try testing.expect(chart_widget.chart.series[0].fill);
|
|
3318
|
+
}
|
|
3319
|
+
|
|
3320
|
+
// ------------------------------------------------------- span paragraphs
|
|
3321
|
+
|
|
3322
|
+
pub const SpanMsg = union(enum) { noop };
|
|
3323
|
+
|
|
3324
|
+
pub const SpanModel = struct {
|
|
3325
|
+
used: []const u8 = "182 GB",
|
|
3326
|
+
total: []const u8 = "512 GB",
|
|
3327
|
+
emphasis: []const u8 = "bold",
|
|
3328
|
+
fixed_width: bool = true,
|
|
3329
|
+
title_scale: f32 = 1.3,
|
|
3330
|
+
|
|
3331
|
+
pub fn tool(model: *const SpanModel) []const u8 {
|
|
3332
|
+
_ = model;
|
|
3333
|
+
return "native doctor";
|
|
3334
|
+
}
|
|
3335
|
+
};
|
|
3336
|
+
|
|
3337
|
+
const SpanUi = canvas.Ui(SpanMsg);
|
|
3338
|
+
const SpanMarkup = markup_view.MarkupView(SpanModel, SpanMsg);
|
|
3339
|
+
|
|
3340
|
+
/// The span-paragraph fixture both engines build (the compiled parity
|
|
3341
|
+
/// suite reuses it): mixed weight/mono/italic/color runs, bindings inside
|
|
3342
|
+
/// spans, a bound weight, single-space collapsing between runs, an
|
|
3343
|
+
/// abutting punctuation run (no whitespace, no separator), and the later
|
|
3344
|
+
/// span additions — a literal-scaled run carrying a binding, an
|
|
3345
|
+
/// underlined run, and a bound scale.
|
|
3346
|
+
pub const span_markup_source =
|
|
3347
|
+
\\<column gap="8" width="360">
|
|
3348
|
+
\\ <text>
|
|
3349
|
+
\\ Disk <span weight="bold">{used}</span> of
|
|
3350
|
+
\\ <span foreground="text_muted">{total}</span> used; run
|
|
3351
|
+
\\ <span mono="{fixed_width}">{tool}</span><span italic="true">!</span>
|
|
3352
|
+
\\ </text>
|
|
3353
|
+
\\ <text label="Total line"><span weight="{emphasis}">Total</span> {used}</text>
|
|
3354
|
+
\\ <text label="Report title">
|
|
3355
|
+
\\ <span scale="1.5" weight="bold">{used}</span> free on
|
|
3356
|
+
\\ <span underline="true">{total}</span> at
|
|
3357
|
+
\\ <span scale="{title_scale}">{tool}</span>
|
|
3358
|
+
\\ </text>
|
|
3359
|
+
\\</column>
|
|
3360
|
+
;
|
|
3361
|
+
|
|
3362
|
+
/// The hand-written equivalent: ui.paragraph over the exact span list the
|
|
3363
|
+
/// markup lowers to (parser-spliced single-space separators included).
|
|
3364
|
+
pub fn handSpanView(ui: *SpanUi, model: *const SpanModel) SpanUi.Node {
|
|
3365
|
+
return ui.column(.{ .gap = 8, .width = 360 }, .{
|
|
3366
|
+
ui.paragraph(.{}, &.{
|
|
3367
|
+
.{ .text = "Disk" },
|
|
3368
|
+
.{ .text = " " },
|
|
3369
|
+
.{ .text = model.used, .weight = .bold },
|
|
3370
|
+
.{ .text = " " },
|
|
3371
|
+
.{ .text = "of" },
|
|
3372
|
+
.{ .text = " " },
|
|
3373
|
+
.{ .text = model.total, .color = .text_muted },
|
|
3374
|
+
.{ .text = " " },
|
|
3375
|
+
.{ .text = "used; run" },
|
|
3376
|
+
.{ .text = " " },
|
|
3377
|
+
.{ .text = model.tool(), .monospace = model.fixed_width },
|
|
3378
|
+
.{ .text = "!", .italic = true },
|
|
3379
|
+
}),
|
|
3380
|
+
ui.paragraph(.{ .semantics = .{ .label = "Total line" } }, &.{
|
|
3381
|
+
.{ .text = "Total", .weight = .bold },
|
|
3382
|
+
.{ .text = " " },
|
|
3383
|
+
.{ .text = model.used },
|
|
3384
|
+
}),
|
|
3385
|
+
ui.paragraph(.{ .semantics = .{ .label = "Report title" } }, &.{
|
|
3386
|
+
.{ .text = model.used, .weight = .bold, .scale = 1.5 },
|
|
3387
|
+
.{ .text = " " },
|
|
3388
|
+
.{ .text = "free on" },
|
|
3389
|
+
.{ .text = " " },
|
|
3390
|
+
.{ .text = model.total, .underline = true },
|
|
3391
|
+
.{ .text = " " },
|
|
3392
|
+
.{ .text = "at" },
|
|
3393
|
+
.{ .text = " " },
|
|
3394
|
+
.{ .text = model.tool(), .scale = model.title_scale },
|
|
3395
|
+
}),
|
|
3396
|
+
});
|
|
3397
|
+
}
|
|
3398
|
+
|
|
3399
|
+
test "markup span paragraphs build the hand-written paragraph exactly" {
|
|
3400
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3401
|
+
defer arena_state.deinit();
|
|
3402
|
+
const arena = arena_state.allocator();
|
|
3403
|
+
const model = SpanModel{};
|
|
3404
|
+
|
|
3405
|
+
var view = try SpanMarkup.init(arena, span_markup_source);
|
|
3406
|
+
var markup_ui = SpanUi.init(arena);
|
|
3407
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
3408
|
+
|
|
3409
|
+
var hand_ui = SpanUi.init(arena);
|
|
3410
|
+
const hand_tree = try hand_ui.finalize(handSpanView(&hand_ui, &model));
|
|
3411
|
+
|
|
3412
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
3413
|
+
defer markup_ids.deinit(testing.allocator);
|
|
3414
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
3415
|
+
defer hand_ids.deinit(testing.allocator);
|
|
3416
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
3417
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
3418
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
3419
|
+
|
|
3420
|
+
// Same span lists (styles and bytes) and the same concatenated
|
|
3421
|
+
// paragraph text, run for run.
|
|
3422
|
+
const markup_disk = markup_tree.root.children[0];
|
|
3423
|
+
const hand_disk = hand_tree.root.children[0];
|
|
3424
|
+
try testing.expectEqualStrings("Disk 182 GB of 512 GB used; run native doctor!", markup_disk.text);
|
|
3425
|
+
try testing.expectEqualStrings(hand_disk.text, markup_disk.text);
|
|
3426
|
+
try testing.expect(canvas.text_spans.textSpansEqual(hand_disk.spans, markup_disk.spans));
|
|
3427
|
+
try testing.expectEqual(@as(usize, 12), markup_disk.spans.len);
|
|
3428
|
+
try testing.expectEqual(canvas.TextSpanWeight.bold, markup_disk.spans[2].weight);
|
|
3429
|
+
try testing.expectEqual(@as(?canvas.TextSpanColor, .text_muted), markup_disk.spans[6].color);
|
|
3430
|
+
try testing.expect(markup_disk.spans[10].monospace);
|
|
3431
|
+
try testing.expect(markup_disk.spans[11].italic);
|
|
3432
|
+
// The abutting punctuation run: no separator between mono and "!".
|
|
3433
|
+
try testing.expectEqualStrings("native doctor!", markup_disk.text[markup_disk.text.len - 14 ..]);
|
|
3434
|
+
|
|
3435
|
+
// The bound weight resolves like any option value.
|
|
3436
|
+
const markup_total = markup_tree.root.children[1];
|
|
3437
|
+
const hand_total = hand_tree.root.children[1];
|
|
3438
|
+
try testing.expect(canvas.text_spans.textSpansEqual(hand_total.spans, markup_total.spans));
|
|
3439
|
+
try testing.expectEqual(canvas.TextSpanWeight.bold, markup_total.spans[0].weight);
|
|
3440
|
+
|
|
3441
|
+
// The later span additions, scale and underline, lower to the
|
|
3442
|
+
// engine's channels — the literal 1.5 multiplier rides a run whose
|
|
3443
|
+
// text is a binding, underline is the decoration flag, and the
|
|
3444
|
+
// bound scale resolves like any number attribute.
|
|
3445
|
+
const markup_title = markup_tree.root.children[2];
|
|
3446
|
+
const hand_title = hand_tree.root.children[2];
|
|
3447
|
+
try testing.expectEqualStrings("182 GB free on 512 GB at native doctor", markup_title.text);
|
|
3448
|
+
try testing.expect(canvas.text_spans.textSpansEqual(hand_title.spans, markup_title.spans));
|
|
3449
|
+
try testing.expectEqual(@as(usize, 9), markup_title.spans.len);
|
|
3450
|
+
try testing.expectEqual(@as(f32, 1.5), markup_title.spans[0].scale);
|
|
3451
|
+
try testing.expectEqual(canvas.TextSpanWeight.bold, markup_title.spans[0].weight);
|
|
3452
|
+
try testing.expect(markup_title.spans[4].underline);
|
|
3453
|
+
try testing.expectEqual(@as(f32, 1.3), markup_title.spans[8].scale);
|
|
3454
|
+
|
|
3455
|
+
// Accessibility pin: a span paragraph announces as ONE text run —
|
|
3456
|
+
// the widget carries the full concatenated text, no semantic
|
|
3457
|
+
// children (spans are visual), exactly like the builder paragraph.
|
|
3458
|
+
// Scaled and underlined runs change nothing here.
|
|
3459
|
+
try testing.expectEqual(@as(usize, 0), markup_disk.children.len);
|
|
3460
|
+
try testing.expectEqual(@as(usize, 0), hand_disk.children.len);
|
|
3461
|
+
try testing.expectEqual(@as(usize, 0), markup_title.children.len);
|
|
3462
|
+
try testing.expectEqualStrings("Total line", markup_total.semantics.label);
|
|
3463
|
+
try testing.expectEqualStrings("Report title", markup_title.semantics.label);
|
|
3464
|
+
}
|
|
3465
|
+
|
|
3466
|
+
test "scaled runs measure at their scaled size, so scale changes the wrap" {
|
|
3467
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3468
|
+
defer arena_state.deinit();
|
|
3469
|
+
const arena = arena_state.allocator();
|
|
3470
|
+
const model = SpanModel{};
|
|
3471
|
+
|
|
3472
|
+
// Two paragraphs with identical bytes; only the second scales its
|
|
3473
|
+
// middle run. Layout honesty: line breaking measures every piece
|
|
3474
|
+
// with the size it will draw at, so the scaled paragraph must break
|
|
3475
|
+
// where the unscaled one still fits.
|
|
3476
|
+
const source =
|
|
3477
|
+
\\<column>
|
|
3478
|
+
\\ <text label="plain">alpha beta <span>gamma delta</span></text>
|
|
3479
|
+
\\ <text label="scaled">alpha beta <span scale="2">gamma delta</span></text>
|
|
3480
|
+
\\</column>
|
|
3481
|
+
;
|
|
3482
|
+
var view = try SpanMarkup.init(arena, source);
|
|
3483
|
+
var ui = SpanUi.init(arena);
|
|
3484
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
3485
|
+
const plain = tree.root.children[0];
|
|
3486
|
+
const scaled = tree.root.children[1];
|
|
3487
|
+
|
|
3488
|
+
// Budget: exactly the unscaled paragraph's single-line advance. The
|
|
3489
|
+
// unscaled paragraph fits one line; the scaled one cannot, because
|
|
3490
|
+
// its 2x run really measures twice as wide.
|
|
3491
|
+
const options = canvas.text_spans.TextSpanLayoutOptions{ .size = 14 };
|
|
3492
|
+
const fit_width = canvas.text_spans.textSpansIntrinsicWidth(plain.spans, options);
|
|
3493
|
+
var plain_runs: [canvas.text_spans.max_text_span_runs_per_paragraph]canvas.text_spans.TextSpanRun = undefined;
|
|
3494
|
+
var scaled_runs: [canvas.text_spans.max_text_span_runs_per_paragraph]canvas.text_spans.TextSpanRun = undefined;
|
|
3495
|
+
const wrap_options = canvas.text_spans.TextSpanLayoutOptions{ .size = 14, .max_width = fit_width };
|
|
3496
|
+
const plain_layout = canvas.text_spans.layoutTextSpans(plain.spans, wrap_options, &plain_runs);
|
|
3497
|
+
const scaled_layout = canvas.text_spans.layoutTextSpans(scaled.spans, wrap_options, &scaled_runs);
|
|
3498
|
+
try testing.expectEqual(@as(usize, 1), plain_layout.line_count);
|
|
3499
|
+
try testing.expect(scaled_layout.line_count > 1);
|
|
3500
|
+
|
|
3501
|
+
// The scaled paragraph also reserves the taller uniform line: ONE
|
|
3502
|
+
// line height sized by the largest scale, shared by every run.
|
|
3503
|
+
try testing.expectEqual(@as(f32, 14 * 1.25), plain_layout.line_height);
|
|
3504
|
+
try testing.expectEqual(@as(f32, 14 * 2 * 1.25), scaled_layout.line_height);
|
|
3505
|
+
}
|
|
3506
|
+
|
|
3507
|
+
test "span misuse fails the build with the pinned teaching messages" {
|
|
3508
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3509
|
+
defer arena_state.deinit();
|
|
3510
|
+
const arena = arena_state.allocator();
|
|
3511
|
+
const model = SpanModel{};
|
|
3512
|
+
|
|
3513
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
3514
|
+
// A span with no text parent has no paragraph to style.
|
|
3515
|
+
.{ .source = "<column>\n <span>alone</span>\n</column>", .message = canvas.ui_markup.span_parent_message },
|
|
3516
|
+
// The other text leaves draw one single-style label.
|
|
3517
|
+
.{ .source = "<column>\n <badge><span>styled</span></badge>\n</column>", .message = canvas.ui_markup.span_text_only_message },
|
|
3518
|
+
// A text-or-children host flows the span to the generic walk,
|
|
3519
|
+
// whose placement hook teaches the <text> home.
|
|
3520
|
+
.{ .source = "<column>\n <list-item label=\"Row\"><span>styled</span></list-item>\n</column>", .message = canvas.ui_markup.span_parent_message },
|
|
3521
|
+
// The closed attribute set: spans are visual runs.
|
|
3522
|
+
.{ .source = "<text><span size=\"sm\">x</span></text>", .message = canvas.ui_markup.span_attr_message },
|
|
3523
|
+
.{ .source = "<text><span on-press=\"noop\">x</span></text>", .message = canvas.ui_markup.span_attr_message },
|
|
3524
|
+
// The closed weight vocabulary.
|
|
3525
|
+
.{ .source = "<text><span weight=\"heavy\">x</span></text>", .message = canvas.ui_markup.span_weight_value_message },
|
|
3526
|
+
// Scale multiplies the base size, so only positive finite
|
|
3527
|
+
// literals mean anything: zero and negatives have no rendering
|
|
3528
|
+
// (the engine would silently draw the base size), and a
|
|
3529
|
+
// non-number cannot multiply at all.
|
|
3530
|
+
.{ .source = "<text><span scale=\"0\">x</span> y</text>", .message = canvas.ui_markup.span_scale_value_message },
|
|
3531
|
+
.{ .source = "<text><span scale=\"-1.5\">x</span> y</text>", .message = canvas.ui_markup.span_scale_value_message },
|
|
3532
|
+
.{ .source = "<text><span scale=\"huge\">x</span> y</text>", .message = canvas.ui_markup.span_scale_value_message },
|
|
3533
|
+
// Spans do not nest and hold no elements.
|
|
3534
|
+
.{ .source = "<text><span><span>x</span></span></text>", .message = canvas.ui_markup.span_content_message },
|
|
3535
|
+
// An empty span is dead markup.
|
|
3536
|
+
.{ .source = "<text><span weight=\"bold\"/> x</text>", .message = canvas.ui_markup.span_content_message },
|
|
3537
|
+
// Color tokens stay a closed literal vocabulary on spans too.
|
|
3538
|
+
.{ .source = "<text><span foreground=\"reddish\">x</span></text>", .message = canvas.ui_markup.unknown_color_token_message },
|
|
3539
|
+
// Structure tags cannot sit between runs (spans are static).
|
|
3540
|
+
.{ .source = "<text><span>a</span><if test=\"{fixed_width}\"><text>b</text></if></text>", .message = canvas.ui_markup.text_inline_children_message },
|
|
3541
|
+
// Single-line policies are dead on an always-wrapping paragraph.
|
|
3542
|
+
.{ .source = "<text wrap=\"true\"><span>x</span> y</text>", .message = canvas.ui_markup.span_paragraph_wrap_message },
|
|
3543
|
+
.{ .source = "<text overflow=\"clip\"><span>x</span> y</text>", .message = canvas.ui_markup.span_paragraph_wrap_message },
|
|
3544
|
+
};
|
|
3545
|
+
for (cases) |case| {
|
|
3546
|
+
// The interpreter fails the build with the message...
|
|
3547
|
+
var view = try SpanMarkup.init(arena, case.source);
|
|
3548
|
+
var ui = SpanUi.init(arena);
|
|
3549
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
3550
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
3551
|
+
// ...and the model-agnostic validator reports the same one.
|
|
3552
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
3553
|
+
const document = try parser.parse();
|
|
3554
|
+
const info = canvas.ui_markup.validate(document) orelse return error.TestUnexpectedResult;
|
|
3555
|
+
try testing.expectEqualStrings(case.message, info.message);
|
|
3556
|
+
}
|
|
3557
|
+
}
|
|
3558
|
+
|
|
3559
|
+
test "a bound scale is held to the positive-finite bound at build" {
|
|
3560
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3561
|
+
defer arena_state.deinit();
|
|
3562
|
+
const arena = arena_state.allocator();
|
|
3563
|
+
|
|
3564
|
+
// A binding sails past the structural validator (it has no model),
|
|
3565
|
+
// so the engine holds the resolved VALUE to the same bound the
|
|
3566
|
+
// validator pins for literals — a zero multiplier is a diagnostic,
|
|
3567
|
+
// never a run silently drawn at the base size.
|
|
3568
|
+
const source = "<column>\n <text><span scale=\"{title_scale}\">x</span> y</text>\n</column>";
|
|
3569
|
+
const dead = SpanModel{ .title_scale = 0 };
|
|
3570
|
+
var view = try SpanMarkup.init(arena, source);
|
|
3571
|
+
var ui = SpanUi.init(arena);
|
|
3572
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &dead));
|
|
3573
|
+
try testing.expectEqualStrings(canvas.ui_markup.span_scale_value_message, view.diagnostic.message);
|
|
3574
|
+
|
|
3575
|
+
// A string-valued binding is the same diagnostic (a name cannot
|
|
3576
|
+
// multiply a size).
|
|
3577
|
+
const wrong_kind = "<column>\n <text><span scale=\"{emphasis}\">x</span> y</text>\n</column>";
|
|
3578
|
+
const model = SpanModel{};
|
|
3579
|
+
var kind_view = try SpanMarkup.init(arena, wrong_kind);
|
|
3580
|
+
var kind_ui = SpanUi.init(arena);
|
|
3581
|
+
try testing.expectError(error.MarkupBuild, kind_view.build(&kind_ui, &model));
|
|
3582
|
+
try testing.expectEqualStrings(canvas.ui_markup.span_scale_value_message, kind_view.diagnostic.message);
|
|
3583
|
+
}
|
|
3584
|
+
|
|
3585
|
+
test "scale and underline stay span-scoped" {
|
|
3586
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3587
|
+
defer arena_state.deinit();
|
|
3588
|
+
const arena = arena_state.allocator();
|
|
3589
|
+
const model = SpanModel{};
|
|
3590
|
+
|
|
3591
|
+
// On a span-less element they are not option attributes at all —
|
|
3592
|
+
// the run channels live on <span>, everything paragraph-wide (size
|
|
3593
|
+
// rungs, alignment, identity) stays on the enclosing text.
|
|
3594
|
+
for ([_][]const u8{
|
|
3595
|
+
"<column>\n <text scale=\"1.5\">plain</text>\n</column>",
|
|
3596
|
+
"<column>\n <text underline=\"true\">plain</text>\n</column>",
|
|
3597
|
+
"<column>\n <badge scale=\"1.5\">count</badge>\n</column>",
|
|
3598
|
+
}) |source| {
|
|
3599
|
+
var parser = canvas.ui_markup.Parser.init(arena, source);
|
|
3600
|
+
const document = try parser.parse();
|
|
3601
|
+
const info = canvas.ui_markup.validate(document) orelse return error.TestUnexpectedResult;
|
|
3602
|
+
try testing.expectEqualStrings("unknown attribute", info.message);
|
|
3603
|
+
var view = try SpanMarkup.init(arena, source);
|
|
3604
|
+
var ui = SpanUi.init(arena);
|
|
3605
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
3606
|
+
}
|
|
3607
|
+
}
|
|
3608
|
+
|
|
3609
|
+
// ------------------------------------------------------ bubble reactions
|
|
3610
|
+
|
|
3611
|
+
/// The reactions fixture both engines build (the compiled parity suite
|
|
3612
|
+
/// reuses it): a default trailing pill, a leading pill whose run carries
|
|
3613
|
+
/// a binding, and a pill-less bubble that must keep an empty chrome-text
|
|
3614
|
+
/// channel.
|
|
3615
|
+
pub const reactions_markup_source =
|
|
3616
|
+
\\<column gap="8" width="340">
|
|
3617
|
+
\\ <bubble>
|
|
3618
|
+
\\ <text wrap="true">On my way</text>
|
|
3619
|
+
\\ <reactions>+2</reactions>
|
|
3620
|
+
\\ </bubble>
|
|
3621
|
+
\\ <bubble variant="primary">
|
|
3622
|
+
\\ <text>Shipped</text>
|
|
3623
|
+
\\ <reactions text-alignment="start">{used} +1</reactions>
|
|
3624
|
+
\\ </bubble>
|
|
3625
|
+
\\ <bubble>
|
|
3626
|
+
\\ <text>quiet</text>
|
|
3627
|
+
\\ </bubble>
|
|
3628
|
+
\\</column>
|
|
3629
|
+
;
|
|
3630
|
+
|
|
3631
|
+
test "markup reactions lower onto the bubble's chrome-text channel" {
|
|
3632
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3633
|
+
defer arena_state.deinit();
|
|
3634
|
+
const arena = arena_state.allocator();
|
|
3635
|
+
const model = SpanModel{};
|
|
3636
|
+
|
|
3637
|
+
var view = try SpanMarkup.init(arena, reactions_markup_source);
|
|
3638
|
+
var ui = SpanUi.init(arena);
|
|
3639
|
+
const tree = try ui.finalize(try view.build(&ui, &model));
|
|
3640
|
+
|
|
3641
|
+
// The pill run lands on widget.text (the alert-title convention:
|
|
3642
|
+
// chrome text rides the text channel) and the reactions child is
|
|
3643
|
+
// CONSUMED — the bubble keeps exactly its message child.
|
|
3644
|
+
const received = tree.root.children[0];
|
|
3645
|
+
try testing.expectEqual(canvas.WidgetKind.bubble, received.kind);
|
|
3646
|
+
try testing.expectEqualStrings("+2", received.text);
|
|
3647
|
+
// End is the default dock: the trailing corner reactions
|
|
3648
|
+
// conventionally hang from, without an attribute.
|
|
3649
|
+
try testing.expectEqual(canvas.TextAlign.end, received.text_alignment);
|
|
3650
|
+
try testing.expectEqual(@as(usize, 1), received.children.len);
|
|
3651
|
+
try testing.expectEqual(canvas.WidgetKind.text, received.children[0].kind);
|
|
3652
|
+
|
|
3653
|
+
// An explicit start dock, and interpolation in the run like any
|
|
3654
|
+
// rendered text.
|
|
3655
|
+
const sent = tree.root.children[1];
|
|
3656
|
+
try testing.expectEqualStrings("182 GB +1", sent.text);
|
|
3657
|
+
try testing.expectEqual(canvas.TextAlign.start, sent.text_alignment);
|
|
3658
|
+
|
|
3659
|
+
// No reactions child, no pill: the chrome-text channel stays empty.
|
|
3660
|
+
const quiet = tree.root.children[2];
|
|
3661
|
+
try testing.expectEqual(@as(usize, 0), quiet.text.len);
|
|
3662
|
+
}
|
|
3663
|
+
|
|
3664
|
+
test "reactions misuse fails the build with the pinned teaching messages" {
|
|
3665
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
3666
|
+
defer arena_state.deinit();
|
|
3667
|
+
const arena = arena_state.allocator();
|
|
3668
|
+
const model = SpanModel{};
|
|
3669
|
+
|
|
3670
|
+
const cases = [_]struct { source: []const u8, message: []const u8 }{
|
|
3671
|
+
// A pill with no bubble has no edge to dock on.
|
|
3672
|
+
.{ .source = "<column>\n <reactions>+2</reactions>\n</column>", .message = canvas.ui_markup.reactions_parent_message },
|
|
3673
|
+
.{ .source = "<column>\n <card><reactions>+2</reactions></card>\n</column>", .message = canvas.ui_markup.reactions_parent_message },
|
|
3674
|
+
// One pill per bubble: it draws ONE capsule.
|
|
3675
|
+
.{ .source = "<column>\n <bubble><text>hi</text><reactions>+1</reactions><reactions>+2</reactions></bubble>\n</column>", .message = canvas.ui_markup.reactions_single_message },
|
|
3676
|
+
// The closed attribute set: the pill is bubble chrome.
|
|
3677
|
+
.{ .source = "<column>\n <bubble><text>hi</text><reactions variant=\"primary\">+2</reactions></bubble>\n</column>", .message = canvas.ui_markup.reactions_attr_message },
|
|
3678
|
+
.{ .source = "<column>\n <bubble><text>hi</text><reactions on-press=\"noop\">+2</reactions></bubble>\n</column>", .message = canvas.ui_markup.reactions_attr_message },
|
|
3679
|
+
// The dock is a literal from the TextAlign vocabulary.
|
|
3680
|
+
.{ .source = "<column>\n <bubble><text>hi</text><reactions text-alignment=\"stretch\">+2</reactions></bubble>\n</column>", .message = canvas.ui_markup.reactions_alignment_value_message },
|
|
3681
|
+
// One run of text, no element children, never empty.
|
|
3682
|
+
.{ .source = "<column>\n <bubble><text>hi</text><reactions><badge>2</badge></reactions></bubble>\n</column>", .message = canvas.ui_markup.reactions_content_message },
|
|
3683
|
+
.{ .source = "<column>\n <bubble><text>hi</text><reactions/></bubble>\n</column>", .message = canvas.ui_markup.reactions_content_message },
|
|
3684
|
+
// The bubble's chrome-text channel belongs to the pill; a bare
|
|
3685
|
+
// text attribute would silently do nothing.
|
|
3686
|
+
.{ .source = "<column>\n <bubble text=\"+2\"><text>hi</text></bubble>\n</column>", .message = canvas.ui_markup.bubble_text_attr_message },
|
|
3687
|
+
};
|
|
3688
|
+
for (cases) |case| {
|
|
3689
|
+
// The interpreter fails the build with the message...
|
|
3690
|
+
var view = try SpanMarkup.init(arena, case.source);
|
|
3691
|
+
var ui = SpanUi.init(arena);
|
|
3692
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
3693
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
3694
|
+
// ...and the model-agnostic validator reports the same one.
|
|
3695
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
3696
|
+
const document = try parser.parse();
|
|
3697
|
+
const info = canvas.ui_markup.validate(document) orelse return error.TestUnexpectedResult;
|
|
3698
|
+
try testing.expectEqualStrings(case.message, info.message);
|
|
3699
|
+
}
|
|
3700
|
+
|
|
3701
|
+
// The pill's literal run rides the tofu guard: a codepoint outside
|
|
3702
|
+
// the bundled face renders as a tofu box on the reference path, so
|
|
3703
|
+
// the validator teaches vector icons or plain words instead.
|
|
3704
|
+
const emoji = "<column>\n <bubble><text>hi</text><reactions>\u{1F44D}</reactions></bubble>\n</column>";
|
|
3705
|
+
var parser = canvas.ui_markup.Parser.init(arena, emoji);
|
|
3706
|
+
const document = try parser.parse();
|
|
3707
|
+
const info = canvas.ui_markup.validate(document) orelse return error.TestUnexpectedResult;
|
|
3708
|
+
try testing.expectEqualStrings(canvas.ui_markup.font_coverage_message, info.message);
|
|
3709
|
+
}
|