@native-sdk/cli 0.0.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +50 -1
- package/app.zon +30 -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 +44 -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,284 @@
|
|
|
1
|
+
//! Window-drag region semantics through REAL dispatch: gpu-surface
|
|
2
|
+
//! pointer input enters `dispatchPlatformEvent`, the press walk resolves
|
|
3
|
+
//! drag-region vs control, and the null platform records every
|
|
4
|
+
//! `startWindowDrag` call the runtime hands it.
|
|
5
|
+
|
|
6
|
+
const support = @import("test_support.zig");
|
|
7
|
+
const std = support.std;
|
|
8
|
+
const geometry = support.geometry;
|
|
9
|
+
const canvas = support.canvas;
|
|
10
|
+
const platform = support.platform;
|
|
11
|
+
const App = support.App;
|
|
12
|
+
const Runtime = support.Runtime;
|
|
13
|
+
const Event = support.Event;
|
|
14
|
+
const TestHarness = support.TestHarness;
|
|
15
|
+
|
|
16
|
+
const DragTestApp = struct {
|
|
17
|
+
pointer_down_count: u32 = 0,
|
|
18
|
+
last_press_target_id: canvas.ObjectId = 0,
|
|
19
|
+
command_count: u32 = 0,
|
|
20
|
+
last_command: []const u8 = "",
|
|
21
|
+
|
|
22
|
+
fn app(self: *@This()) App {
|
|
23
|
+
return .{ .context = self, .name = "gpu-window-drag", .source = platform.WebViewSource.html("<h1>GPU</h1>"), .event_fn = event };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn event(context: *anyopaque, runtime: *Runtime, event_value: Event) anyerror!void {
|
|
27
|
+
_ = runtime;
|
|
28
|
+
const self: *@This() = @ptrCast(@alignCast(context));
|
|
29
|
+
switch (event_value) {
|
|
30
|
+
.canvas_widget_pointer => |pointer_event| {
|
|
31
|
+
if (pointer_event.pointer.phase != .down) return;
|
|
32
|
+
self.pointer_down_count += 1;
|
|
33
|
+
self.last_press_target_id = if (pointer_event.press_target) |target| target.id else 0;
|
|
34
|
+
},
|
|
35
|
+
.command => |command_event| {
|
|
36
|
+
self.command_count += 1;
|
|
37
|
+
self.last_command = command_event.name;
|
|
38
|
+
},
|
|
39
|
+
else => {},
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/// A hidden-titlebar canvas: header row marked `window_drag` holding a
|
|
45
|
+
/// button and plain text, a content button below the header.
|
|
46
|
+
fn installDragLayout(harness: anytype) !void {
|
|
47
|
+
_ = try harness.runtime.createView(.{
|
|
48
|
+
.window_id = 1,
|
|
49
|
+
.label = "canvas",
|
|
50
|
+
.kind = .gpu_surface,
|
|
51
|
+
.frame = geometry.RectF.init(0, 0, 320, 200),
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
const header_children = [_]canvas.Widget{
|
|
55
|
+
.{ .id = 3, .kind = .button, .frame = geometry.RectF.init(8, 6, 80, 28), .text = "Open", .command = "app.open" },
|
|
56
|
+
.{ .id = 4, .kind = .text, .frame = geometry.RectF.init(100, 10, 60, 18), .text = "Title" },
|
|
57
|
+
};
|
|
58
|
+
const children = [_]canvas.Widget{
|
|
59
|
+
.{ .id = 2, .kind = .row, .frame = geometry.RectF.init(0, 0, 320, 40), .window_drag = true, .children = &header_children },
|
|
60
|
+
.{ .id = 5, .kind = .button, .frame = geometry.RectF.init(10, 60, 96, 32), .text = "Body", .command = "app.body" },
|
|
61
|
+
};
|
|
62
|
+
const root = canvas.Widget{ .id = 1, .kind = .panel, .children = &children };
|
|
63
|
+
var nodes: [8]canvas.WidgetLayoutNode = undefined;
|
|
64
|
+
const layout = try canvas.layoutWidgetTree(root, geometry.RectF.init(0, 0, 320, 200), &nodes);
|
|
65
|
+
_ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
fn pointerDown(harness: anytype, app: App, x: f32, y: f32, button: i32) !void {
|
|
69
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
70
|
+
.window_id = 1,
|
|
71
|
+
.label = "canvas",
|
|
72
|
+
.kind = .pointer_down,
|
|
73
|
+
.x = x,
|
|
74
|
+
.y = y,
|
|
75
|
+
.button = button,
|
|
76
|
+
} });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fn pointerUp(harness: anytype, app: App, x: f32, y: f32) !void {
|
|
80
|
+
try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
|
|
81
|
+
.window_id = 1,
|
|
82
|
+
.label = "canvas",
|
|
83
|
+
.kind = .pointer_up,
|
|
84
|
+
.x = x,
|
|
85
|
+
.y = y,
|
|
86
|
+
.button = 0,
|
|
87
|
+
} });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
test "press on a window-drag region's background starts a window drag, not a widget press" {
|
|
91
|
+
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
92
|
+
defer harness.destroy(std.testing.allocator);
|
|
93
|
+
harness.null_platform.gpu_surfaces = true;
|
|
94
|
+
var app_state: DragTestApp = .{};
|
|
95
|
+
const app = app_state.app();
|
|
96
|
+
try harness.start(app);
|
|
97
|
+
try installDragLayout(harness);
|
|
98
|
+
|
|
99
|
+
// Down on the header's empty background: the walk finds the drag
|
|
100
|
+
// region, the platform records the drag, and NO widget is pressed.
|
|
101
|
+
try pointerDown(harness, app, 240, 20, 0);
|
|
102
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.window_drag_start_count);
|
|
103
|
+
try std.testing.expectEqual(@as(platform.WindowId, 1), harness.null_platform.window_drag_starts[0]);
|
|
104
|
+
try std.testing.expectEqual(@as(canvas.ObjectId, 0), harness.runtime.views[0].canvas_widget_pressed_id);
|
|
105
|
+
// The app still sees the raw pointer event — with no press target,
|
|
106
|
+
// exactly like a dead-space click.
|
|
107
|
+
try std.testing.expectEqual(@as(u32, 1), app_state.pointer_down_count);
|
|
108
|
+
try std.testing.expectEqual(@as(canvas.ObjectId, 0), app_state.last_press_target_id);
|
|
109
|
+
try pointerUp(harness, app, 240, 20);
|
|
110
|
+
try std.testing.expectEqual(@as(u32, 0), app_state.command_count);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
test "a button inside a window-drag region stays a button" {
|
|
114
|
+
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
115
|
+
defer harness.destroy(std.testing.allocator);
|
|
116
|
+
harness.null_platform.gpu_surfaces = true;
|
|
117
|
+
var app_state: DragTestApp = .{};
|
|
118
|
+
const app = app_state.app();
|
|
119
|
+
try harness.start(app);
|
|
120
|
+
try installDragLayout(harness);
|
|
121
|
+
|
|
122
|
+
// Press fall-through keeps working: the down lands on the button
|
|
123
|
+
// (claims the press) and never reaches the drag region.
|
|
124
|
+
try pointerDown(harness, app, 20, 18, 0);
|
|
125
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.window_drag_start_count);
|
|
126
|
+
try std.testing.expectEqual(@as(canvas.ObjectId, 3), harness.runtime.views[0].canvas_widget_pressed_id);
|
|
127
|
+
try std.testing.expectEqual(@as(canvas.ObjectId, 3), app_state.last_press_target_id);
|
|
128
|
+
try pointerUp(harness, app, 20, 18);
|
|
129
|
+
try std.testing.expectEqual(@as(u32, 1), app_state.command_count);
|
|
130
|
+
try std.testing.expectEqualStrings("app.open", app_state.last_command);
|
|
131
|
+
|
|
132
|
+
// A control outside the region is untouched by any of this.
|
|
133
|
+
try pointerDown(harness, app, 20, 70, 0);
|
|
134
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.window_drag_start_count);
|
|
135
|
+
try pointerUp(harness, app, 20, 70);
|
|
136
|
+
try std.testing.expectEqualStrings("app.body", app_state.last_command);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
test "plain text inside a window-drag region falls through to the drag" {
|
|
140
|
+
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
141
|
+
defer harness.destroy(std.testing.allocator);
|
|
142
|
+
harness.null_platform.gpu_surfaces = true;
|
|
143
|
+
var app_state: DragTestApp = .{};
|
|
144
|
+
const app = app_state.app();
|
|
145
|
+
try harness.start(app);
|
|
146
|
+
try installDragLayout(harness);
|
|
147
|
+
|
|
148
|
+
// Text does not claim presses, so a down on the header's title moves
|
|
149
|
+
// the window (native titlebars do not select their text) and no
|
|
150
|
+
// static-text selection starts.
|
|
151
|
+
try pointerDown(harness, app, 110, 20, 0);
|
|
152
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.window_drag_start_count);
|
|
153
|
+
try std.testing.expectEqual(@as(canvas.ObjectId, 0), harness.runtime.views[0].canvas_widget_pressed_id);
|
|
154
|
+
try std.testing.expectEqual(@as(canvas.ObjectId, 0), harness.runtime.views[0].canvas_widget_selected_text_id);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
test "double-click on a window-drag region reaches the platform as two drag starts" {
|
|
158
|
+
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
159
|
+
defer harness.destroy(std.testing.allocator);
|
|
160
|
+
harness.null_platform.gpu_surfaces = true;
|
|
161
|
+
var app_state: DragTestApp = .{};
|
|
162
|
+
const app = app_state.app();
|
|
163
|
+
try harness.start(app);
|
|
164
|
+
try installDragLayout(harness);
|
|
165
|
+
|
|
166
|
+
// The runtime forwards EVERY qualifying down; the host decides drag
|
|
167
|
+
// vs zoom from the native event's click count (macOS honors the
|
|
168
|
+
// user's titlebar double-click preference). Two downs = two calls.
|
|
169
|
+
try pointerDown(harness, app, 240, 20, 0);
|
|
170
|
+
try pointerUp(harness, app, 240, 20);
|
|
171
|
+
try pointerDown(harness, app, 240, 20, 0);
|
|
172
|
+
try pointerUp(harness, app, 240, 20);
|
|
173
|
+
try std.testing.expectEqual(@as(usize, 2), harness.null_platform.window_drag_start_count);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
test "window drag requires the primary button and an enabled region" {
|
|
177
|
+
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
178
|
+
defer harness.destroy(std.testing.allocator);
|
|
179
|
+
harness.null_platform.gpu_surfaces = true;
|
|
180
|
+
var app_state: DragTestApp = .{};
|
|
181
|
+
const app = app_state.app();
|
|
182
|
+
try harness.start(app);
|
|
183
|
+
|
|
184
|
+
_ = try harness.runtime.createView(.{
|
|
185
|
+
.window_id = 1,
|
|
186
|
+
.label = "canvas",
|
|
187
|
+
.kind = .gpu_surface,
|
|
188
|
+
.frame = geometry.RectF.init(0, 0, 320, 200),
|
|
189
|
+
});
|
|
190
|
+
const children = [_]canvas.Widget{
|
|
191
|
+
.{ .id = 2, .kind = .row, .frame = geometry.RectF.init(0, 0, 320, 40), .window_drag = true },
|
|
192
|
+
.{ .id = 3, .kind = .row, .frame = geometry.RectF.init(0, 60, 320, 40), .window_drag = true, .state = .{ .disabled = true } },
|
|
193
|
+
};
|
|
194
|
+
const root = canvas.Widget{ .id = 1, .kind = .panel, .children = &children };
|
|
195
|
+
var nodes: [4]canvas.WidgetLayoutNode = undefined;
|
|
196
|
+
const layout = try canvas.layoutWidgetTree(root, geometry.RectF.init(0, 0, 320, 200), &nodes);
|
|
197
|
+
_ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
|
|
198
|
+
|
|
199
|
+
// Secondary-button downs never start a window drag.
|
|
200
|
+
try pointerDown(harness, app, 160, 20, 1);
|
|
201
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.window_drag_start_count);
|
|
202
|
+
|
|
203
|
+
// A disabled drag region stands down like a disabled control.
|
|
204
|
+
try pointerDown(harness, app, 160, 80, 0);
|
|
205
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.window_drag_start_count);
|
|
206
|
+
|
|
207
|
+
// The enabled region still drags.
|
|
208
|
+
try pointerDown(harness, app, 160, 20, 0);
|
|
209
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.window_drag_start_count);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
test "a widget with both a press handler and window_drag keeps its press" {
|
|
213
|
+
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
214
|
+
defer harness.destroy(std.testing.allocator);
|
|
215
|
+
harness.null_platform.gpu_surfaces = true;
|
|
216
|
+
var app_state: DragTestApp = .{};
|
|
217
|
+
const app = app_state.app();
|
|
218
|
+
try harness.start(app);
|
|
219
|
+
|
|
220
|
+
_ = try harness.runtime.createView(.{
|
|
221
|
+
.window_id = 1,
|
|
222
|
+
.label = "canvas",
|
|
223
|
+
.kind = .gpu_surface,
|
|
224
|
+
.frame = geometry.RectF.init(0, 0, 320, 200),
|
|
225
|
+
});
|
|
226
|
+
const children = [_]canvas.Widget{
|
|
227
|
+
.{
|
|
228
|
+
.id = 2,
|
|
229
|
+
.kind = .row,
|
|
230
|
+
.frame = geometry.RectF.init(0, 0, 320, 40),
|
|
231
|
+
.window_drag = true,
|
|
232
|
+
.semantics = .{ .actions = .{ .press = true } },
|
|
233
|
+
},
|
|
234
|
+
};
|
|
235
|
+
const root = canvas.Widget{ .id = 1, .kind = .panel, .children = &children };
|
|
236
|
+
var nodes: [3]canvas.WidgetLayoutNode = undefined;
|
|
237
|
+
const layout = try canvas.layoutWidgetTree(root, geometry.RectF.init(0, 0, 320, 200), &nodes);
|
|
238
|
+
_ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", layout);
|
|
239
|
+
|
|
240
|
+
// Authored handlers outrank the drag surface: the press dispatches,
|
|
241
|
+
// the platform is never asked to move the window.
|
|
242
|
+
try pointerDown(harness, app, 160, 20, 0);
|
|
243
|
+
try std.testing.expectEqual(@as(usize, 0), harness.null_platform.window_drag_start_count);
|
|
244
|
+
try std.testing.expectEqual(@as(canvas.ObjectId, 2), app_state.last_press_target_id);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
test "layout installs mirror window-drag regions to hit-testing platforms" {
|
|
248
|
+
const harness = try TestHarness().create(std.testing.allocator, .{});
|
|
249
|
+
defer harness.destroy(std.testing.allocator);
|
|
250
|
+
harness.null_platform.gpu_surfaces = true;
|
|
251
|
+
var app_state: DragTestApp = .{};
|
|
252
|
+
const app = app_state.app();
|
|
253
|
+
try harness.start(app);
|
|
254
|
+
try installDragLayout(harness);
|
|
255
|
+
|
|
256
|
+
// The install pushed the mirror the Windows host consults from
|
|
257
|
+
// WM_NCHITTEST: the drag header's frame, then the press-claiming
|
|
258
|
+
// button inside it as an exclusion. Plain text claims nothing and
|
|
259
|
+
// the body button lives outside the region, so neither appears.
|
|
260
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.window_drag_region_push_count);
|
|
261
|
+
const regions = harness.null_platform.window_drag_regions[0..harness.null_platform.window_drag_region_count];
|
|
262
|
+
try std.testing.expectEqual(@as(usize, 2), regions.len);
|
|
263
|
+
const layout = try harness.runtime.canvasWidgetLayout(1, "canvas");
|
|
264
|
+
try std.testing.expect(!regions[0].exclusion);
|
|
265
|
+
try std.testing.expectEqual((layout.findById(2) orelse return error.TestUnexpectedResult).frame, regions[0].frame);
|
|
266
|
+
try std.testing.expect(regions[1].exclusion);
|
|
267
|
+
try std.testing.expectEqual((layout.findById(3) orelse return error.TestUnexpectedResult).frame, regions[1].frame);
|
|
268
|
+
|
|
269
|
+
// Re-installing an identical layout pushes nothing: the mirror only
|
|
270
|
+
// travels when it changed.
|
|
271
|
+
const header_children = [_]canvas.Widget{
|
|
272
|
+
.{ .id = 3, .kind = .button, .frame = geometry.RectF.init(8, 6, 80, 28), .text = "Open", .command = "app.open" },
|
|
273
|
+
.{ .id = 4, .kind = .text, .frame = geometry.RectF.init(100, 10, 60, 18), .text = "Title" },
|
|
274
|
+
};
|
|
275
|
+
const children = [_]canvas.Widget{
|
|
276
|
+
.{ .id = 2, .kind = .row, .frame = geometry.RectF.init(0, 0, 320, 40), .window_drag = true, .children = &header_children },
|
|
277
|
+
.{ .id = 5, .kind = .button, .frame = geometry.RectF.init(10, 60, 96, 32), .text = "Body", .command = "app.body" },
|
|
278
|
+
};
|
|
279
|
+
const root = canvas.Widget{ .id = 1, .kind = .panel, .children = &children };
|
|
280
|
+
var nodes: [8]canvas.WidgetLayoutNode = undefined;
|
|
281
|
+
const same_layout = try canvas.layoutWidgetTree(root, geometry.RectF.init(0, 0, 320, 200), &nodes);
|
|
282
|
+
_ = try harness.runtime.setCanvasWidgetLayout(1, "canvas", same_layout);
|
|
283
|
+
try std.testing.expectEqual(@as(usize, 1), harness.null_platform.window_drag_region_push_count);
|
|
284
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
//! The runtime's real clocks and the app-facing time seam.
|
|
2
|
+
//!
|
|
3
|
+
//! Zig 0.16 routes `std.time.milliTimestamp`-style reads through `std.Io`,
|
|
4
|
+
//! which `update` deliberately never sees — so every app that wanted a
|
|
5
|
+
//! ledger timestamp re-implemented `clock_gettime` per OS. These helpers
|
|
6
|
+
//! are the framework's one implementation: direct OS clock reads (no `Io`
|
|
7
|
+
//! handle, callable from `update`/`init_fx`/`view`), plus a tiny `Clock`
|
|
8
|
+
//! value type that models can hold so tests substitute a deterministic
|
|
9
|
+
//! `TestClock` — the determinism story is "real clock behind a seam you
|
|
10
|
+
//! can swap", exactly like the fake effect executor.
|
|
11
|
+
//!
|
|
12
|
+
//! Two clocks, two questions:
|
|
13
|
+
//! - wall (`nowMs`/`nowNanoseconds`): "what time is it?" — Unix epoch,
|
|
14
|
+
//! jumps when the OS clock is adjusted. For timestamps humans read.
|
|
15
|
+
//! - monotonic (`monotonicMs`/`monotonicNanoseconds`): "how long did it
|
|
16
|
+
//! take?" — an arbitrary origin that never goes backwards. For
|
|
17
|
+
//! durations and elapsed-time math.
|
|
18
|
+
//!
|
|
19
|
+
//! Unsupported targets (wasi) read 0 rather than failing — the same
|
|
20
|
+
//! honest degradation the automation timestamps always had. Freestanding
|
|
21
|
+
//! targets have no OS clock at all, so an embedding host (the docs wasm
|
|
22
|
+
//! preview) injects its monotonic time through
|
|
23
|
+
//! `setFreestandingMonotonicNanoseconds` and both monotonic reads follow
|
|
24
|
+
//! it; without a host feed they read 0, the old honest degradation.
|
|
25
|
+
|
|
26
|
+
const std = @import("std");
|
|
27
|
+
const builtin = @import("builtin");
|
|
28
|
+
const clock_module = @This();
|
|
29
|
+
|
|
30
|
+
/// Host-fed monotonic time for `freestanding` builds (single-threaded by
|
|
31
|
+
/// construction — wasm32-freestanding has no threads to race). Clamped
|
|
32
|
+
/// monotone so a jittery host feed can never run the clock backwards.
|
|
33
|
+
var freestanding_monotonic_ns: u64 = 0;
|
|
34
|
+
|
|
35
|
+
/// Feed the freestanding monotonic clock (no-op on targets with a real
|
|
36
|
+
/// OS clock). The wasm preview host calls this with the page's
|
|
37
|
+
/// `performance.now()` before dispatching input and frame events, so
|
|
38
|
+
/// runtime code reading the clock seam observes real elapsed time.
|
|
39
|
+
pub fn setFreestandingMonotonicNanoseconds(ns: u64) void {
|
|
40
|
+
if (builtin.os.tag != .freestanding) return;
|
|
41
|
+
if (ns > freestanding_monotonic_ns) freestanding_monotonic_ns = ns;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// Wall-clock nanoseconds since the Unix epoch (REALTIME). 0 when the
|
|
45
|
+
/// target has no readable clock (wasi).
|
|
46
|
+
pub fn nowNanoseconds() i128 {
|
|
47
|
+
switch (builtin.os.tag) {
|
|
48
|
+
.windows => {
|
|
49
|
+
// 100 ns intervals since 1601-01-01, rebased to the Unix epoch.
|
|
50
|
+
const epoch_ns: i96 = @as(i96, std.time.epoch.windows) * std.time.ns_per_s;
|
|
51
|
+
return @as(i96, std.os.windows.ntdll.RtlGetSystemTimePrecise()) * 100 + epoch_ns;
|
|
52
|
+
},
|
|
53
|
+
.wasi, .freestanding, .emscripten => return 0,
|
|
54
|
+
else => {
|
|
55
|
+
var ts: std.posix.timespec = undefined;
|
|
56
|
+
switch (std.posix.errno(std.posix.system.clock_gettime(.REALTIME, &ts))) {
|
|
57
|
+
.SUCCESS => return @as(i128, ts.sec) * std.time.ns_per_s + ts.nsec,
|
|
58
|
+
else => return 0,
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/// Wall-clock milliseconds since the Unix epoch — the ledger-timestamp
|
|
65
|
+
/// helper (`started_ms`/`ended_ms` fields, "what time is it?").
|
|
66
|
+
pub fn nowMs() i64 {
|
|
67
|
+
return @intCast(@divTrunc(nowNanoseconds(), std.time.ns_per_ms));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/// Monotonic nanoseconds from an arbitrary origin: never decreases, not
|
|
71
|
+
/// affected by wall-clock adjustments — the duration clock ("how long
|
|
72
|
+
/// did it take?"). 0 when the target has no readable clock (wasi).
|
|
73
|
+
pub fn monotonicNanoseconds() u64 {
|
|
74
|
+
switch (builtin.os.tag) {
|
|
75
|
+
.windows => {
|
|
76
|
+
var counter: std.os.windows.LARGE_INTEGER = undefined;
|
|
77
|
+
var frequency: std.os.windows.LARGE_INTEGER = undefined;
|
|
78
|
+
if (!std.os.windows.ntdll.RtlQueryPerformanceCounter(&counter).toBool()) return 0;
|
|
79
|
+
if (!std.os.windows.ntdll.RtlQueryPerformanceFrequency(&frequency).toBool()) return 0;
|
|
80
|
+
if (frequency <= 0) return 0;
|
|
81
|
+
const ticks: u128 = @intCast(@max(counter, 0));
|
|
82
|
+
return @intCast(ticks * std.time.ns_per_s / @as(u128, @intCast(frequency)));
|
|
83
|
+
},
|
|
84
|
+
.freestanding => return freestanding_monotonic_ns,
|
|
85
|
+
.wasi, .emscripten => return 0,
|
|
86
|
+
else => {
|
|
87
|
+
var ts: std.posix.timespec = undefined;
|
|
88
|
+
switch (std.posix.errno(std.posix.system.clock_gettime(.MONOTONIC, &ts))) {
|
|
89
|
+
.SUCCESS => return timestampToU64(@as(i128, ts.sec) * std.time.ns_per_s + ts.nsec),
|
|
90
|
+
else => return 0,
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// Monotonic milliseconds from an arbitrary origin. Subtract two reads
|
|
97
|
+
/// for an elapsed duration; the absolute value means nothing.
|
|
98
|
+
pub fn monotonicMs() u64 {
|
|
99
|
+
return monotonicNanoseconds() / std.time.ns_per_ms;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/// The time seam apps store in the model: `.system` reads the real OS
|
|
103
|
+
/// clocks; tests swap in `TestClock.clock()` and advance it by hand so
|
|
104
|
+
/// time-dependent updates stay deterministic. Copyable value type —
|
|
105
|
+
/// two function pointers and a context, nothing to deinit.
|
|
106
|
+
pub const Clock = struct {
|
|
107
|
+
context: ?*anyopaque = null,
|
|
108
|
+
wall_ns_fn: *const fn (context: ?*anyopaque) i128 = systemWallNs,
|
|
109
|
+
monotonic_ns_fn: *const fn (context: ?*anyopaque) u64 = systemMonotonicNs,
|
|
110
|
+
|
|
111
|
+
/// The real OS clocks (the default — `Clock{}` is the same value).
|
|
112
|
+
pub const system: Clock = .{};
|
|
113
|
+
|
|
114
|
+
pub fn wallNanoseconds(self: Clock) i128 {
|
|
115
|
+
return self.wall_ns_fn(self.context);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
pub fn wallMs(self: Clock) i64 {
|
|
119
|
+
return @intCast(@divTrunc(self.wallNanoseconds(), std.time.ns_per_ms));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
pub fn monotonicNanoseconds(self: Clock) u64 {
|
|
123
|
+
return self.monotonic_ns_fn(self.context);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
pub fn monotonicMs(self: Clock) u64 {
|
|
127
|
+
return self.monotonicNanoseconds() / std.time.ns_per_ms;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
fn systemWallNs(context: ?*anyopaque) i128 {
|
|
131
|
+
_ = context;
|
|
132
|
+
return clock_module.nowNanoseconds();
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
fn systemMonotonicNs(context: ?*anyopaque) u64 {
|
|
136
|
+
_ = context;
|
|
137
|
+
return clock_module.monotonicNanoseconds();
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/// A hand-cranked clock for tests: set the wall time, advance both
|
|
142
|
+
/// clocks explicitly, and hand `clock()` to the code under test. The
|
|
143
|
+
/// `TestClock` must outlive every `Clock` taken from it (it holds the
|
|
144
|
+
/// pointer).
|
|
145
|
+
pub const TestClock = struct {
|
|
146
|
+
wall_ns: i128 = 0,
|
|
147
|
+
monotonic_ns: u64 = 0,
|
|
148
|
+
|
|
149
|
+
pub fn clock(self: *TestClock) Clock {
|
|
150
|
+
return .{
|
|
151
|
+
.context = self,
|
|
152
|
+
.wall_ns_fn = readWall,
|
|
153
|
+
.monotonic_ns_fn = readMonotonic,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/// Advance both clocks together, like real time passing.
|
|
158
|
+
pub fn advanceNs(self: *TestClock, ns: u64) void {
|
|
159
|
+
self.wall_ns += ns;
|
|
160
|
+
self.monotonic_ns += ns;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
pub fn advanceMs(self: *TestClock, ms: u64) void {
|
|
164
|
+
self.advanceNs(ms * std.time.ns_per_ms);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Set the wall clock alone (an NTP-style jump); monotonic is
|
|
168
|
+
/// unaffected, exactly like the real clocks.
|
|
169
|
+
pub fn setWallMs(self: *TestClock, ms: i64) void {
|
|
170
|
+
self.wall_ns = @as(i128, ms) * std.time.ns_per_ms;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
fn readWall(context: ?*anyopaque) i128 {
|
|
174
|
+
const self: *TestClock = @ptrCast(@alignCast(context.?));
|
|
175
|
+
return self.wall_ns;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
fn readMonotonic(context: ?*anyopaque) u64 {
|
|
179
|
+
const self: *TestClock = @ptrCast(@alignCast(context.?));
|
|
180
|
+
return self.monotonic_ns;
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
pub fn timestampToU64(value: i128) u64 {
|
|
185
|
+
if (value <= 0) return 0;
|
|
186
|
+
return @intCast(@min(value, std.math.maxInt(u64)));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
pub fn automationInputTimestampNs() u64 {
|
|
190
|
+
return timestampToU64(nowNanoseconds());
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
test "the real clocks read plausible, ordered values" {
|
|
194
|
+
// Wall: after 2020-01-01 (1577836800 s) and before 2100.
|
|
195
|
+
const wall = nowNanoseconds();
|
|
196
|
+
try std.testing.expect(wall > 1_577_836_800 * @as(i128, std.time.ns_per_s));
|
|
197
|
+
try std.testing.expect(wall < 4_102_444_800 * @as(i128, std.time.ns_per_s));
|
|
198
|
+
const wall_ms = nowMs();
|
|
199
|
+
try std.testing.expect(wall_ms > 1_577_836_800_000);
|
|
200
|
+
|
|
201
|
+
// Monotonic: nonzero and never decreasing across consecutive reads.
|
|
202
|
+
const first = monotonicNanoseconds();
|
|
203
|
+
const second = monotonicNanoseconds();
|
|
204
|
+
try std.testing.expect(first > 0);
|
|
205
|
+
try std.testing.expect(second >= first);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
test "Clock.system reads the real clocks and Clock{} is the same seam" {
|
|
209
|
+
const clock: Clock = .system;
|
|
210
|
+
try std.testing.expect(clock.wallMs() > 1_577_836_800_000);
|
|
211
|
+
try std.testing.expect(clock.monotonicNanoseconds() > 0);
|
|
212
|
+
const default_clock: Clock = .{};
|
|
213
|
+
try std.testing.expect(default_clock.wallMs() > 1_577_836_800_000);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
test "TestClock is deterministic: advance moves both, setWallMs jumps wall alone" {
|
|
217
|
+
var test_clock: TestClock = .{};
|
|
218
|
+
const clock = test_clock.clock();
|
|
219
|
+
try std.testing.expectEqual(@as(i64, 0), clock.wallMs());
|
|
220
|
+
try std.testing.expectEqual(@as(u64, 0), clock.monotonicMs());
|
|
221
|
+
|
|
222
|
+
test_clock.setWallMs(1_700_000_000_000);
|
|
223
|
+
try std.testing.expectEqual(@as(i64, 1_700_000_000_000), clock.wallMs());
|
|
224
|
+
try std.testing.expectEqual(@as(u64, 0), clock.monotonicMs());
|
|
225
|
+
|
|
226
|
+
test_clock.advanceMs(250);
|
|
227
|
+
try std.testing.expectEqual(@as(i64, 1_700_000_000_250), clock.wallMs());
|
|
228
|
+
try std.testing.expectEqual(@as(u64, 250), clock.monotonicMs());
|
|
229
|
+
try std.testing.expectEqual(@as(i128, 1_700_000_000_250 * @as(i128, std.time.ns_per_ms)), clock.wallNanoseconds());
|
|
230
|
+
try std.testing.expectEqual(@as(u64, 250 * std.time.ns_per_ms), clock.monotonicNanoseconds());
|
|
231
|
+
}
|