@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,889 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
pub const Rounding = enum {
|
|
4
|
+
truncate,
|
|
5
|
+
floor,
|
|
6
|
+
ceil,
|
|
7
|
+
round,
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
pub const Edge = enum {
|
|
11
|
+
left,
|
|
12
|
+
right,
|
|
13
|
+
top,
|
|
14
|
+
bottom,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
pub const PointF = Point(f32);
|
|
18
|
+
pub const SizeF = Size(f32);
|
|
19
|
+
pub const RectF = Rect(f32);
|
|
20
|
+
pub const InsetsF = Insets(f32);
|
|
21
|
+
pub const OffsetF = Offset(f32);
|
|
22
|
+
pub const ScaleF = Scale(f32);
|
|
23
|
+
pub const ConstraintsF = Constraints(f32);
|
|
24
|
+
|
|
25
|
+
pub const PointD = Point(f64);
|
|
26
|
+
pub const SizeD = Size(f64);
|
|
27
|
+
pub const RectD = Rect(f64);
|
|
28
|
+
pub const InsetsD = Insets(f64);
|
|
29
|
+
pub const OffsetD = Offset(f64);
|
|
30
|
+
pub const ScaleD = Scale(f64);
|
|
31
|
+
pub const ConstraintsD = Constraints(f64);
|
|
32
|
+
|
|
33
|
+
pub const PointI = Point(i32);
|
|
34
|
+
pub const SizeI = Size(i32);
|
|
35
|
+
pub const RectI = Rect(i32);
|
|
36
|
+
pub const InsetsI = Insets(i32);
|
|
37
|
+
pub const OffsetI = Offset(i32);
|
|
38
|
+
pub const ScaleI = Scale(i32);
|
|
39
|
+
pub const ConstraintsI = Constraints(i32);
|
|
40
|
+
|
|
41
|
+
pub const PointU = Point(u32);
|
|
42
|
+
pub const SizeU = Size(u32);
|
|
43
|
+
pub const RectU = Rect(u32);
|
|
44
|
+
pub const InsetsU = Insets(u32);
|
|
45
|
+
pub const OffsetU = Offset(u32);
|
|
46
|
+
pub const ScaleU = Scale(u32);
|
|
47
|
+
pub const ConstraintsU = Constraints(u32);
|
|
48
|
+
|
|
49
|
+
pub fn Point(comptime T: type) type {
|
|
50
|
+
requireScalar(T);
|
|
51
|
+
|
|
52
|
+
return struct {
|
|
53
|
+
const Self = @This();
|
|
54
|
+
|
|
55
|
+
x: T = 0,
|
|
56
|
+
y: T = 0,
|
|
57
|
+
|
|
58
|
+
pub fn init(x: T, y: T) Self {
|
|
59
|
+
return .{ .x = x, .y = y };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
pub fn zero() Self {
|
|
63
|
+
return .{};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
pub fn all(value: T) Self {
|
|
67
|
+
return .{ .x = value, .y = value };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
pub fn translate(self: Self, offset: Offset(T)) Self {
|
|
71
|
+
return .{
|
|
72
|
+
.x = self.x + offset.dx,
|
|
73
|
+
.y = self.y + offset.dy,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
pub fn scale(self: Self, factor: Scale(T)) Self {
|
|
78
|
+
return .{
|
|
79
|
+
.x = self.x * factor.x,
|
|
80
|
+
.y = self.y * factor.y,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
pub fn convert(self: Self, comptime U: type, rounding: Rounding) Point(U) {
|
|
85
|
+
return .{
|
|
86
|
+
.x = convertScalar(self.x, U, rounding),
|
|
87
|
+
.y = convertScalar(self.y, U, rounding),
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pub fn Size(comptime T: type) type {
|
|
94
|
+
requireScalar(T);
|
|
95
|
+
|
|
96
|
+
return struct {
|
|
97
|
+
const Self = @This();
|
|
98
|
+
|
|
99
|
+
width: T = 0,
|
|
100
|
+
height: T = 0,
|
|
101
|
+
|
|
102
|
+
pub fn init(width: T, height: T) Self {
|
|
103
|
+
return .{ .width = width, .height = height };
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
pub fn zero() Self {
|
|
107
|
+
return .{};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
pub fn all(value: T) Self {
|
|
111
|
+
return .{ .width = value, .height = value };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
pub fn isEmpty(self: Self) bool {
|
|
115
|
+
return isEmptyExtent(self.width) or isEmptyExtent(self.height);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
pub fn clamp(self: Self, constraints: Constraints(T)) Self {
|
|
119
|
+
return constraints.clampSize(self);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
pub fn scale(self: Self, factor: Scale(T)) Self {
|
|
123
|
+
return .{
|
|
124
|
+
.width = self.width * factor.x,
|
|
125
|
+
.height = self.height * factor.y,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
pub fn convert(self: Self, comptime U: type, rounding: Rounding) Size(U) {
|
|
130
|
+
return .{
|
|
131
|
+
.width = convertScalar(self.width, U, rounding),
|
|
132
|
+
.height = convertScalar(self.height, U, rounding),
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
pub fn Rect(comptime T: type) type {
|
|
139
|
+
requireScalar(T);
|
|
140
|
+
|
|
141
|
+
return struct {
|
|
142
|
+
const Self = @This();
|
|
143
|
+
|
|
144
|
+
x: T = 0,
|
|
145
|
+
y: T = 0,
|
|
146
|
+
width: T = 0,
|
|
147
|
+
height: T = 0,
|
|
148
|
+
|
|
149
|
+
pub fn init(x: T, y: T, width: T, height: T) Self {
|
|
150
|
+
return .{ .x = x, .y = y, .width = width, .height = height };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
pub fn zero() Self {
|
|
154
|
+
return .{};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
pub fn all(value: T) Self {
|
|
158
|
+
return .{ .x = value, .y = value, .width = value, .height = value };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
pub fn fromSize(size_value: Size(T)) Self {
|
|
162
|
+
return .{ .width = size_value.width, .height = size_value.height };
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
pub fn fromPoints(a: Point(T), b: Point(T)) Self {
|
|
166
|
+
const x0 = @min(a.x, b.x);
|
|
167
|
+
const y0 = @min(a.y, b.y);
|
|
168
|
+
const x1 = @max(a.x, b.x);
|
|
169
|
+
const y1 = @max(a.y, b.y);
|
|
170
|
+
return .{
|
|
171
|
+
.x = x0,
|
|
172
|
+
.y = y0,
|
|
173
|
+
.width = x1 - x0,
|
|
174
|
+
.height = y1 - y0,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
pub fn minX(self: Self) T {
|
|
179
|
+
return self.x;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
pub fn maxX(self: Self) T {
|
|
183
|
+
return self.x + self.width;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
pub fn minY(self: Self) T {
|
|
187
|
+
return self.y;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
pub fn maxY(self: Self) T {
|
|
191
|
+
return self.y + self.height;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
pub fn size(self: Self) Size(T) {
|
|
195
|
+
return .{ .width = self.width, .height = self.height };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
pub fn topLeft(self: Self) Point(T) {
|
|
199
|
+
return .{ .x = self.x, .y = self.y };
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
pub fn topRight(self: Self) Point(T) {
|
|
203
|
+
return .{ .x = self.maxX(), .y = self.y };
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
pub fn bottomLeft(self: Self) Point(T) {
|
|
207
|
+
return .{ .x = self.x, .y = self.maxY() };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
pub fn bottomRight(self: Self) Point(T) {
|
|
211
|
+
return .{ .x = self.maxX(), .y = self.maxY() };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
pub fn center(self: Self) Point(T) {
|
|
215
|
+
return .{
|
|
216
|
+
.x = self.x + halfScalar(self.width),
|
|
217
|
+
.y = self.y + halfScalar(self.height),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
pub fn hasNegativeSize(self: Self) bool {
|
|
222
|
+
if (comptime canBeNegative(T)) {
|
|
223
|
+
return self.width < 0 or self.height < 0;
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
pub fn normalized(self: Self) Self {
|
|
229
|
+
if (comptime !canBeNegative(T)) {
|
|
230
|
+
return self;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
var result = self;
|
|
234
|
+
if (result.width < 0) {
|
|
235
|
+
result.x += result.width;
|
|
236
|
+
result.width = -result.width;
|
|
237
|
+
}
|
|
238
|
+
if (result.height < 0) {
|
|
239
|
+
result.y += result.height;
|
|
240
|
+
result.height = -result.height;
|
|
241
|
+
}
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
pub fn isEmpty(self: Self) bool {
|
|
246
|
+
return isEmptyExtent(self.width) or isEmptyExtent(self.height);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
pub fn containsPoint(self: Self, point: Point(T)) bool {
|
|
250
|
+
self.assertNormalized();
|
|
251
|
+
return !self.isEmpty() and
|
|
252
|
+
point.x >= self.minX() and point.x < self.maxX() and
|
|
253
|
+
point.y >= self.minY() and point.y < self.maxY();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
pub fn containsRect(self: Self, other: Self) bool {
|
|
257
|
+
self.assertNormalized();
|
|
258
|
+
other.assertNormalized();
|
|
259
|
+
return !self.isEmpty() and !other.isEmpty() and
|
|
260
|
+
other.minX() >= self.minX() and
|
|
261
|
+
other.maxX() <= self.maxX() and
|
|
262
|
+
other.minY() >= self.minY() and
|
|
263
|
+
other.maxY() <= self.maxY();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
pub fn intersects(self: Self, other: Self) bool {
|
|
267
|
+
return !Self.intersection(self, other).isEmpty();
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
pub fn intersection(a: Self, b: Self) Self {
|
|
271
|
+
a.assertNormalized();
|
|
272
|
+
b.assertNormalized();
|
|
273
|
+
|
|
274
|
+
const x0 = @max(a.minX(), b.minX());
|
|
275
|
+
const y0 = @max(a.minY(), b.minY());
|
|
276
|
+
const x1 = @min(a.maxX(), b.maxX());
|
|
277
|
+
const y1 = @min(a.maxY(), b.maxY());
|
|
278
|
+
|
|
279
|
+
if (x1 <= x0 or y1 <= y0) {
|
|
280
|
+
return .{ .x = x0, .y = y0 };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return .{
|
|
284
|
+
.x = x0,
|
|
285
|
+
.y = y0,
|
|
286
|
+
.width = x1 - x0,
|
|
287
|
+
.height = y1 - y0,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
pub fn unionWith(a: Self, b: Self) Self {
|
|
292
|
+
a.assertNormalized();
|
|
293
|
+
b.assertNormalized();
|
|
294
|
+
|
|
295
|
+
if (a.isEmpty() and b.isEmpty()) return .{};
|
|
296
|
+
if (a.isEmpty()) return b;
|
|
297
|
+
if (b.isEmpty()) return a;
|
|
298
|
+
|
|
299
|
+
const x0 = @min(a.minX(), b.minX());
|
|
300
|
+
const y0 = @min(a.minY(), b.minY());
|
|
301
|
+
const x1 = @max(a.maxX(), b.maxX());
|
|
302
|
+
const y1 = @max(a.maxY(), b.maxY());
|
|
303
|
+
|
|
304
|
+
return .{
|
|
305
|
+
.x = x0,
|
|
306
|
+
.y = y0,
|
|
307
|
+
.width = x1 - x0,
|
|
308
|
+
.height = y1 - y0,
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
pub fn translate(self: Self, offset: Offset(T)) Self {
|
|
313
|
+
return .{
|
|
314
|
+
.x = self.x + offset.dx,
|
|
315
|
+
.y = self.y + offset.dy,
|
|
316
|
+
.width = self.width,
|
|
317
|
+
.height = self.height,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
pub fn scale(self: Self, factor: Scale(T)) Self {
|
|
322
|
+
return .{
|
|
323
|
+
.x = self.x * factor.x,
|
|
324
|
+
.y = self.y * factor.y,
|
|
325
|
+
.width = self.width * factor.x,
|
|
326
|
+
.height = self.height * factor.y,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
pub fn inflate(self: Self, insets: Insets(T)) Self {
|
|
331
|
+
self.assertNormalized();
|
|
332
|
+
return .{
|
|
333
|
+
.x = subFloorZeroIfUnsigned(self.x, insets.left),
|
|
334
|
+
.y = subFloorZeroIfUnsigned(self.y, insets.top),
|
|
335
|
+
.width = self.width + insets.left + insets.right,
|
|
336
|
+
.height = self.height + insets.top + insets.bottom,
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
pub fn deflate(self: Self, insets: Insets(T)) Self {
|
|
341
|
+
self.assertNormalized();
|
|
342
|
+
|
|
343
|
+
const move_x = @min(insets.left, self.width);
|
|
344
|
+
const move_y = @min(insets.top, self.height);
|
|
345
|
+
|
|
346
|
+
return .{
|
|
347
|
+
.x = self.x + move_x,
|
|
348
|
+
.y = self.y + move_y,
|
|
349
|
+
.width = shrinkExtent(self.width, insets.left, insets.right),
|
|
350
|
+
.height = shrinkExtent(self.height, insets.top, insets.bottom),
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
pub fn inset(self: Self, insets: Insets(T)) Self {
|
|
355
|
+
return self.deflate(insets);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
pub fn outset(self: Self, insets: Insets(T)) Self {
|
|
359
|
+
return self.inflate(insets);
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
pub fn clampSize(self: Self, constraints: Constraints(T)) Self {
|
|
363
|
+
const clamped = constraints.clampSize(self.size());
|
|
364
|
+
return .{
|
|
365
|
+
.x = self.x,
|
|
366
|
+
.y = self.y,
|
|
367
|
+
.width = clamped.width,
|
|
368
|
+
.height = clamped.height,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
pub fn split(self: Self, edge: Edge, amount: T) [2]Self {
|
|
373
|
+
self.assertNormalized();
|
|
374
|
+
|
|
375
|
+
return switch (edge) {
|
|
376
|
+
.left => blk: {
|
|
377
|
+
const clamped = clampExtent(amount, self.width);
|
|
378
|
+
break :blk .{
|
|
379
|
+
.{ .x = self.x, .y = self.y, .width = clamped, .height = self.height },
|
|
380
|
+
.{ .x = self.x + clamped, .y = self.y, .width = self.width - clamped, .height = self.height },
|
|
381
|
+
};
|
|
382
|
+
},
|
|
383
|
+
.right => blk: {
|
|
384
|
+
const clamped = clampExtent(amount, self.width);
|
|
385
|
+
break :blk .{
|
|
386
|
+
.{ .x = self.maxX() - clamped, .y = self.y, .width = clamped, .height = self.height },
|
|
387
|
+
.{ .x = self.x, .y = self.y, .width = self.width - clamped, .height = self.height },
|
|
388
|
+
};
|
|
389
|
+
},
|
|
390
|
+
.top => blk: {
|
|
391
|
+
const clamped = clampExtent(amount, self.height);
|
|
392
|
+
break :blk .{
|
|
393
|
+
.{ .x = self.x, .y = self.y, .width = self.width, .height = clamped },
|
|
394
|
+
.{ .x = self.x, .y = self.y + clamped, .width = self.width, .height = self.height - clamped },
|
|
395
|
+
};
|
|
396
|
+
},
|
|
397
|
+
.bottom => blk: {
|
|
398
|
+
const clamped = clampExtent(amount, self.height);
|
|
399
|
+
break :blk .{
|
|
400
|
+
.{ .x = self.x, .y = self.maxY() - clamped, .width = self.width, .height = clamped },
|
|
401
|
+
.{ .x = self.x, .y = self.y, .width = self.width, .height = self.height - clamped },
|
|
402
|
+
};
|
|
403
|
+
},
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
pub fn splitProportion(self: Self, edge: Edge, proportion: f32) [2]Self {
|
|
408
|
+
const clamped_proportion = std.math.clamp(proportion, 0, 1);
|
|
409
|
+
const extent = switch (edge) {
|
|
410
|
+
.left, .right => self.width,
|
|
411
|
+
.top, .bottom => self.height,
|
|
412
|
+
};
|
|
413
|
+
const amount = convertScalar(scalarToF64(extent) * clamped_proportion, T, .round);
|
|
414
|
+
return self.split(edge, amount);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
pub fn convert(self: Self, comptime U: type, rounding: Rounding) Rect(U) {
|
|
418
|
+
return .{
|
|
419
|
+
.x = convertScalar(self.x, U, rounding),
|
|
420
|
+
.y = convertScalar(self.y, U, rounding),
|
|
421
|
+
.width = convertScalar(self.width, U, rounding),
|
|
422
|
+
.height = convertScalar(self.height, U, rounding),
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
pub fn snapOut(self: Self, comptime U: type) Rect(U) {
|
|
427
|
+
self.assertNormalized();
|
|
428
|
+
const x0 = convertScalar(self.minX(), U, .floor);
|
|
429
|
+
const y0 = convertScalar(self.minY(), U, .floor);
|
|
430
|
+
const x1 = convertScalar(self.maxX(), U, .ceil);
|
|
431
|
+
const y1 = convertScalar(self.maxY(), U, .ceil);
|
|
432
|
+
return .{
|
|
433
|
+
.x = x0,
|
|
434
|
+
.y = y0,
|
|
435
|
+
.width = x1 - x0,
|
|
436
|
+
.height = y1 - y0,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
pub fn snapIn(self: Self, comptime U: type) Rect(U) {
|
|
441
|
+
self.assertNormalized();
|
|
442
|
+
const x0 = convertScalar(self.minX(), U, .ceil);
|
|
443
|
+
const y0 = convertScalar(self.minY(), U, .ceil);
|
|
444
|
+
const x1 = convertScalar(self.maxX(), U, .floor);
|
|
445
|
+
const y1 = convertScalar(self.maxY(), U, .floor);
|
|
446
|
+
if (x1 <= x0 or y1 <= y0) {
|
|
447
|
+
return .{ .x = x0, .y = y0 };
|
|
448
|
+
}
|
|
449
|
+
return .{
|
|
450
|
+
.x = x0,
|
|
451
|
+
.y = y0,
|
|
452
|
+
.width = x1 - x0,
|
|
453
|
+
.height = y1 - y0,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
fn assertNormalized(self: Self) void {
|
|
458
|
+
std.debug.assert(!self.hasNegativeSize());
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
pub fn Insets(comptime T: type) type {
|
|
464
|
+
requireScalar(T);
|
|
465
|
+
|
|
466
|
+
return struct {
|
|
467
|
+
const Self = @This();
|
|
468
|
+
|
|
469
|
+
top: T = 0,
|
|
470
|
+
right: T = 0,
|
|
471
|
+
bottom: T = 0,
|
|
472
|
+
left: T = 0,
|
|
473
|
+
|
|
474
|
+
pub fn init(top: T, right: T, bottom: T, left: T) Self {
|
|
475
|
+
return .{ .top = top, .right = right, .bottom = bottom, .left = left };
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
pub fn zero() Self {
|
|
479
|
+
return .{};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
pub fn all(value: T) Self {
|
|
483
|
+
return .{ .top = value, .right = value, .bottom = value, .left = value };
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
pub fn symmetric(vertical_value: T, horizontal_value: T) Self {
|
|
487
|
+
return .{
|
|
488
|
+
.top = vertical_value,
|
|
489
|
+
.right = horizontal_value,
|
|
490
|
+
.bottom = vertical_value,
|
|
491
|
+
.left = horizontal_value,
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
pub fn horizontal(self: Self) T {
|
|
496
|
+
return self.left + self.right;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
pub fn vertical(self: Self) T {
|
|
500
|
+
return self.top + self.bottom;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
pub fn convert(self: Self, comptime U: type, rounding: Rounding) Insets(U) {
|
|
504
|
+
return .{
|
|
505
|
+
.top = convertScalar(self.top, U, rounding),
|
|
506
|
+
.right = convertScalar(self.right, U, rounding),
|
|
507
|
+
.bottom = convertScalar(self.bottom, U, rounding),
|
|
508
|
+
.left = convertScalar(self.left, U, rounding),
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
pub fn Offset(comptime T: type) type {
|
|
515
|
+
requireScalar(T);
|
|
516
|
+
|
|
517
|
+
return struct {
|
|
518
|
+
const Self = @This();
|
|
519
|
+
|
|
520
|
+
dx: T = 0,
|
|
521
|
+
dy: T = 0,
|
|
522
|
+
|
|
523
|
+
pub fn init(dx: T, dy: T) Self {
|
|
524
|
+
return .{ .dx = dx, .dy = dy };
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
pub fn zero() Self {
|
|
528
|
+
return .{};
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
pub fn all(value: T) Self {
|
|
532
|
+
return .{ .dx = value, .dy = value };
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
pub fn convert(self: Self, comptime U: type, rounding: Rounding) Offset(U) {
|
|
536
|
+
return .{
|
|
537
|
+
.dx = convertScalar(self.dx, U, rounding),
|
|
538
|
+
.dy = convertScalar(self.dy, U, rounding),
|
|
539
|
+
};
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
pub fn Scale(comptime T: type) type {
|
|
545
|
+
requireScalar(T);
|
|
546
|
+
|
|
547
|
+
return struct {
|
|
548
|
+
const Self = @This();
|
|
549
|
+
|
|
550
|
+
x: T = 1,
|
|
551
|
+
y: T = 1,
|
|
552
|
+
|
|
553
|
+
pub fn init(x: T, y: T) Self {
|
|
554
|
+
return .{ .x = x, .y = y };
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
pub fn identity() Self {
|
|
558
|
+
return .{};
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
pub fn uniform(value: T) Self {
|
|
562
|
+
return .{ .x = value, .y = value };
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
pub fn convert(self: Self, comptime U: type, rounding: Rounding) Scale(U) {
|
|
566
|
+
return .{
|
|
567
|
+
.x = convertScalar(self.x, U, rounding),
|
|
568
|
+
.y = convertScalar(self.y, U, rounding),
|
|
569
|
+
};
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
pub fn Constraints(comptime T: type) type {
|
|
575
|
+
requireScalar(T);
|
|
576
|
+
|
|
577
|
+
return struct {
|
|
578
|
+
const Self = @This();
|
|
579
|
+
|
|
580
|
+
min_width: T = 0,
|
|
581
|
+
min_height: T = 0,
|
|
582
|
+
max_width: T = scalarMax(T),
|
|
583
|
+
max_height: T = scalarMax(T),
|
|
584
|
+
|
|
585
|
+
pub fn init(min_size: Size(T), max_size: Size(T)) Self {
|
|
586
|
+
return .{
|
|
587
|
+
.min_width = min_size.width,
|
|
588
|
+
.min_height = min_size.height,
|
|
589
|
+
.max_width = max_size.width,
|
|
590
|
+
.max_height = max_size.height,
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
pub fn unconstrained() Self {
|
|
595
|
+
return .{};
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
pub fn tight(size_value: Size(T)) Self {
|
|
599
|
+
return .{
|
|
600
|
+
.min_width = size_value.width,
|
|
601
|
+
.min_height = size_value.height,
|
|
602
|
+
.max_width = size_value.width,
|
|
603
|
+
.max_height = size_value.height,
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
pub fn loose(max_size: Size(T)) Self {
|
|
608
|
+
return .{
|
|
609
|
+
.max_width = max_size.width,
|
|
610
|
+
.max_height = max_size.height,
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
pub fn clampSize(self: Self, size_value: Size(T)) Size(T) {
|
|
615
|
+
return .{
|
|
616
|
+
.width = std.math.clamp(size_value.width, self.min_width, self.max_width),
|
|
617
|
+
.height = std.math.clamp(size_value.height, self.min_height, self.max_height),
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
pub fn logicalToPhysical(rect: RectF, scale_factor: f32) RectI {
|
|
624
|
+
return rect.scale(ScaleF.uniform(scale_factor)).snapOut(i32);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
pub fn physicalToLogical(rect: RectI, scale_factor: f32) RectF {
|
|
628
|
+
std.debug.assert(scale_factor != 0);
|
|
629
|
+
return rect.convert(f32, .round).scale(ScaleF.uniform(1 / scale_factor));
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
fn requireScalar(comptime T: type) void {
|
|
633
|
+
switch (@typeInfo(T)) {
|
|
634
|
+
.int, .float => {},
|
|
635
|
+
else => @compileError("geometry scalar types must be concrete ints or floats"),
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
fn canBeNegative(comptime T: type) bool {
|
|
640
|
+
return switch (@typeInfo(T)) {
|
|
641
|
+
.float => true,
|
|
642
|
+
.int => |info| info.signedness == .signed,
|
|
643
|
+
else => false,
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
fn scalarMax(comptime T: type) T {
|
|
648
|
+
return switch (@typeInfo(T)) {
|
|
649
|
+
.float => std.math.inf(T),
|
|
650
|
+
.int => std.math.maxInt(T),
|
|
651
|
+
else => unreachable,
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
fn isEmptyExtent(value: anytype) bool {
|
|
656
|
+
const T = @TypeOf(value);
|
|
657
|
+
if (comptime canBeNegative(T)) {
|
|
658
|
+
return value <= 0;
|
|
659
|
+
}
|
|
660
|
+
return value == 0;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
fn halfScalar(value: anytype) @TypeOf(value) {
|
|
664
|
+
return switch (@typeInfo(@TypeOf(value))) {
|
|
665
|
+
.float => value / 2,
|
|
666
|
+
.int => @divTrunc(value, 2),
|
|
667
|
+
else => unreachable,
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
fn clampExtent(value: anytype, extent: @TypeOf(value)) @TypeOf(value) {
|
|
672
|
+
if (value <= 0) return 0;
|
|
673
|
+
if (value >= extent) return extent;
|
|
674
|
+
return value;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
fn shrinkExtent(value: anytype, start: @TypeOf(value), end_value: @TypeOf(value)) @TypeOf(value) {
|
|
678
|
+
const T = @TypeOf(value);
|
|
679
|
+
if (comptime canBeNegative(T)) {
|
|
680
|
+
return @max(0, value - start - end_value);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
if (start >= value) return 0;
|
|
684
|
+
const remaining = value - start;
|
|
685
|
+
if (end_value >= remaining) return 0;
|
|
686
|
+
return remaining - end_value;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
fn subFloorZeroIfUnsigned(value: anytype, amount: @TypeOf(value)) @TypeOf(value) {
|
|
690
|
+
const T = @TypeOf(value);
|
|
691
|
+
if (comptime canBeNegative(T)) {
|
|
692
|
+
return value - amount;
|
|
693
|
+
}
|
|
694
|
+
if (amount >= value) return 0;
|
|
695
|
+
return value - amount;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
fn scalarToF64(value: anytype) f64 {
|
|
699
|
+
return switch (@typeInfo(@TypeOf(value))) {
|
|
700
|
+
.float => @floatCast(value),
|
|
701
|
+
.int => @floatFromInt(value),
|
|
702
|
+
else => unreachable,
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
fn convertScalar(value: anytype, comptime U: type, rounding: Rounding) U {
|
|
707
|
+
requireScalar(U);
|
|
708
|
+
|
|
709
|
+
const Source = @TypeOf(value);
|
|
710
|
+
return switch (@typeInfo(U)) {
|
|
711
|
+
.float => switch (@typeInfo(Source)) {
|
|
712
|
+
.float => @floatCast(value),
|
|
713
|
+
.int => @floatFromInt(value),
|
|
714
|
+
else => unreachable,
|
|
715
|
+
},
|
|
716
|
+
.int => switch (@typeInfo(Source)) {
|
|
717
|
+
.float => @intFromFloat(roundFloat(value, rounding)),
|
|
718
|
+
.int => @intCast(value),
|
|
719
|
+
else => unreachable,
|
|
720
|
+
},
|
|
721
|
+
else => unreachable,
|
|
722
|
+
};
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
fn roundFloat(value: anytype, rounding: Rounding) @TypeOf(value) {
|
|
726
|
+
return switch (rounding) {
|
|
727
|
+
.truncate => @trunc(value),
|
|
728
|
+
.floor => @floor(value),
|
|
729
|
+
.ceil => @ceil(value),
|
|
730
|
+
.round => @round(value),
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
test "aliases compile and expose zero values" {
|
|
735
|
+
try std.testing.expectEqualDeep(PointF.zero(), PointF.init(0, 0));
|
|
736
|
+
try std.testing.expectEqualDeep(SizeD.zero(), SizeD.init(0, 0));
|
|
737
|
+
try std.testing.expectEqualDeep(RectI.zero(), RectI.init(0, 0, 0, 0));
|
|
738
|
+
try std.testing.expectEqualDeep(InsetsU.zero(), InsetsU.init(0, 0, 0, 0));
|
|
739
|
+
try std.testing.expectEqualDeep(OffsetF.zero(), OffsetF.init(0, 0));
|
|
740
|
+
try std.testing.expectEqualDeep(ScaleI.identity(), ScaleI.init(1, 1));
|
|
741
|
+
try std.testing.expectEqualDeep(ConstraintsU.unconstrained().min_width, 0);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
test "rect accessors and constructors" {
|
|
745
|
+
const rect = RectI.init(10, 20, 30, 40);
|
|
746
|
+
|
|
747
|
+
try std.testing.expectEqual(10, rect.minX());
|
|
748
|
+
try std.testing.expectEqual(40, rect.maxX());
|
|
749
|
+
try std.testing.expectEqual(20, rect.minY());
|
|
750
|
+
try std.testing.expectEqual(60, rect.maxY());
|
|
751
|
+
try std.testing.expectEqualDeep(SizeI.init(30, 40), rect.size());
|
|
752
|
+
try std.testing.expectEqualDeep(PointI.init(10, 20), rect.topLeft());
|
|
753
|
+
try std.testing.expectEqualDeep(PointI.init(40, 60), rect.bottomRight());
|
|
754
|
+
try std.testing.expectEqualDeep(PointI.init(25, 40), rect.center());
|
|
755
|
+
try std.testing.expectEqualDeep(RectI.init(0, 0, 30, 40), RectI.fromSize(rect.size()));
|
|
756
|
+
try std.testing.expectEqualDeep(RectI.init(10, 20, 30, 40), RectI.fromPoints(.{ .x = 40, .y = 60 }, .{ .x = 10, .y = 20 }));
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
test "rect containment is half open" {
|
|
760
|
+
const rect = RectI.init(10, 20, 30, 40);
|
|
761
|
+
|
|
762
|
+
try std.testing.expect(rect.containsPoint(.{ .x = 10, .y = 20 }));
|
|
763
|
+
try std.testing.expect(rect.containsPoint(.{ .x = 39, .y = 59 }));
|
|
764
|
+
try std.testing.expect(!rect.containsPoint(.{ .x = 40, .y = 59 }));
|
|
765
|
+
try std.testing.expect(!rect.containsPoint(.{ .x = 39, .y = 60 }));
|
|
766
|
+
try std.testing.expect(!rect.containsPoint(.{ .x = 9, .y = 20 }));
|
|
767
|
+
try std.testing.expect(!rect.containsPoint(.{ .x = 10, .y = 19 }));
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
test "rect contains rect uses inclusive far edge for contained geometry" {
|
|
771
|
+
const outer = RectI.init(0, 0, 100, 100);
|
|
772
|
+
|
|
773
|
+
try std.testing.expect(outer.containsRect(.{ .x = 0, .y = 0, .width = 100, .height = 100 }));
|
|
774
|
+
try std.testing.expect(outer.containsRect(.{ .x = 10, .y = 10, .width = 20, .height = 20 }));
|
|
775
|
+
try std.testing.expect(!outer.containsRect(.{ .x = 90, .y = 90, .width = 20, .height = 20 }));
|
|
776
|
+
try std.testing.expect(!outer.containsRect(.{ .x = 10, .y = 10, .width = 0, .height = 20 }));
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
test "empty rectangles" {
|
|
780
|
+
try std.testing.expect(RectI.init(0, 0, 0, 10).isEmpty());
|
|
781
|
+
try std.testing.expect(RectI.init(0, 0, 10, 0).isEmpty());
|
|
782
|
+
try std.testing.expect(RectI.init(0, 0, -1, 10).isEmpty());
|
|
783
|
+
try std.testing.expect(RectF.init(0, 0, -0.5, 10).isEmpty());
|
|
784
|
+
try std.testing.expect(RectU.init(0, 0, 0, 10).isEmpty());
|
|
785
|
+
try std.testing.expect(!RectU.init(0, 0, 1, 10).isEmpty());
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
test "normalized handles negative extents" {
|
|
789
|
+
try std.testing.expectEqualDeep(RectI.init(5, 10, 10, 20), RectI.init(15, 30, -10, -20).normalized());
|
|
790
|
+
try std.testing.expectEqualDeep(RectF.init(5, 10, 10, 20), RectF.init(15, 30, -10, -20).normalized());
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
test "intersection covers overlap touching containment and no overlap" {
|
|
794
|
+
const a = RectI.init(0, 0, 100, 100);
|
|
795
|
+
|
|
796
|
+
try std.testing.expectEqualDeep(RectI.init(50, 50, 50, 50), RectI.intersection(a, .{ .x = 50, .y = 50, .width = 100, .height = 100 }));
|
|
797
|
+
try std.testing.expectEqualDeep(RectI.init(100, 20, 0, 0), RectI.intersection(a, .{ .x = 100, .y = 20, .width = 10, .height = 10 }));
|
|
798
|
+
try std.testing.expectEqualDeep(RectI.init(10, 10, 20, 20), RectI.intersection(a, .{ .x = 10, .y = 10, .width = 20, .height = 20 }));
|
|
799
|
+
try std.testing.expectEqualDeep(RectI.init(200, 200, 0, 0), RectI.intersection(a, .{ .x = 200, .y = 200, .width = 10, .height = 10 }));
|
|
800
|
+
try std.testing.expect(a.intersects(.{ .x = 99, .y = 99, .width = 1, .height = 1 }));
|
|
801
|
+
try std.testing.expect(!a.intersects(.{ .x = 100, .y = 99, .width = 1, .height = 1 }));
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
test "union skips empty rectangles" {
|
|
805
|
+
const a = RectI.init(0, 0, 10, 10);
|
|
806
|
+
const b = RectI.init(5, 20, 10, 10);
|
|
807
|
+
|
|
808
|
+
try std.testing.expectEqualDeep(RectI.init(0, 0, 15, 30), RectI.unionWith(a, b));
|
|
809
|
+
try std.testing.expectEqualDeep(a, RectI.unionWith(a, .{}));
|
|
810
|
+
try std.testing.expectEqualDeep(b, RectI.unionWith(.{}, b));
|
|
811
|
+
try std.testing.expectEqualDeep(RectI.zero(), RectI.unionWith(.{}, .{}));
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
test "insets inflate and deflate" {
|
|
815
|
+
const rect = RectI.init(10, 10, 100, 50);
|
|
816
|
+
const insets = InsetsI.init(5, 10, 15, 20);
|
|
817
|
+
|
|
818
|
+
try std.testing.expectEqual(30, insets.horizontal());
|
|
819
|
+
try std.testing.expectEqual(20, insets.vertical());
|
|
820
|
+
try std.testing.expectEqualDeep(RectI.init(30, 15, 70, 30), rect.deflate(insets));
|
|
821
|
+
try std.testing.expectEqualDeep(RectI.init(-10, 5, 130, 70), rect.inflate(insets));
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
test "insets can collapse a rect" {
|
|
825
|
+
const rect = RectI.init(10, 10, 20, 20);
|
|
826
|
+
|
|
827
|
+
try std.testing.expectEqualDeep(RectI.init(30, 30, 0, 0), rect.deflate(InsetsI.all(50)));
|
|
828
|
+
try std.testing.expectEqualDeep(RectU.init(20, 20, 0, 0), RectU.init(10, 10, 10, 10).deflate(InsetsU.all(20)));
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
test "translate scale and point operations" {
|
|
832
|
+
const point = PointI.init(2, 3);
|
|
833
|
+
const rect = RectI.init(1, 2, 3, 4);
|
|
834
|
+
|
|
835
|
+
try std.testing.expectEqualDeep(PointI.init(7, 1), point.translate(.{ .dx = 5, .dy = -2 }));
|
|
836
|
+
try std.testing.expectEqualDeep(PointI.init(4, 9), point.scale(ScaleI.init(2, 3)));
|
|
837
|
+
try std.testing.expectEqualDeep(RectI.init(6, 0, 3, 4), rect.translate(.{ .dx = 5, .dy = -2 }));
|
|
838
|
+
try std.testing.expectEqualDeep(RectI.init(2, 6, 6, 12), rect.scale(.{ .x = 2, .y = 3 }));
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
test "constraints clamp sizes" {
|
|
842
|
+
const constraints = ConstraintsI.init(SizeI.init(10, 20), SizeI.init(100, 200));
|
|
843
|
+
|
|
844
|
+
try std.testing.expectEqualDeep(SizeI.init(10, 20), constraints.clampSize(.{ .width = 5, .height = 10 }));
|
|
845
|
+
try std.testing.expectEqualDeep(SizeI.init(50, 60), constraints.clampSize(.{ .width = 50, .height = 60 }));
|
|
846
|
+
try std.testing.expectEqualDeep(SizeI.init(100, 200), constraints.clampSize(.{ .width = 150, .height = 300 }));
|
|
847
|
+
try std.testing.expectEqualDeep(SizeI.init(42, 64), ConstraintsI.tight(.{ .width = 42, .height = 64 }).clampSize(.{ .width = 1, .height = 2 }));
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
test "split by edge and proportion" {
|
|
851
|
+
const rect = RectI.init(0, 0, 100, 50);
|
|
852
|
+
|
|
853
|
+
try std.testing.expectEqualDeep([2]RectI{ RectI.init(0, 0, 25, 50), RectI.init(25, 0, 75, 50) }, rect.split(.left, 25));
|
|
854
|
+
try std.testing.expectEqualDeep([2]RectI{ RectI.init(75, 0, 25, 50), RectI.init(0, 0, 75, 50) }, rect.split(.right, 25));
|
|
855
|
+
try std.testing.expectEqualDeep([2]RectI{ RectI.init(0, 0, 100, 10), RectI.init(0, 10, 100, 40) }, rect.split(.top, 10));
|
|
856
|
+
try std.testing.expectEqualDeep([2]RectI{ RectI.init(0, 40, 100, 10), RectI.init(0, 0, 100, 40) }, rect.split(.bottom, 10));
|
|
857
|
+
try std.testing.expectEqualDeep([2]RectI{ RectI.init(0, 0, 25, 50), RectI.init(25, 0, 75, 50) }, rect.splitProportion(.left, 0.25));
|
|
858
|
+
try std.testing.expectEqualDeep([2]RectI{ RectI.init(0, 0, 100, 50), RectI.init(100, 0, 0, 50) }, rect.split(.left, 200));
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
test "conversion and deterministic rounding" {
|
|
862
|
+
const rect = RectF.init(1.25, 2.75, 10.5, 20.25);
|
|
863
|
+
|
|
864
|
+
try std.testing.expectEqualDeep(RectI.init(1, 2, 10, 20), rect.convert(i32, .floor));
|
|
865
|
+
try std.testing.expectEqualDeep(RectI.init(2, 3, 11, 21), rect.convert(i32, .ceil));
|
|
866
|
+
try std.testing.expectEqualDeep(RectI.init(1, 3, 11, 20), rect.convert(i32, .round));
|
|
867
|
+
try std.testing.expectEqualDeep(RectI.init(1, 2, 10, 20), rect.convert(i32, .truncate));
|
|
868
|
+
try std.testing.expectEqualDeep(RectF.init(1, 2, 10, 20), RectI.init(1, 2, 10, 20).convert(f32, .round));
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
test "snap out and snap in use rect edges" {
|
|
872
|
+
const rect = RectF.init(1.25, 2.75, 10.5, 20.25);
|
|
873
|
+
|
|
874
|
+
try std.testing.expectEqualDeep(RectI.init(1, 2, 11, 21), rect.snapOut(i32));
|
|
875
|
+
try std.testing.expectEqualDeep(RectI.init(2, 3, 9, 20), rect.snapIn(i32));
|
|
876
|
+
try std.testing.expectEqualDeep(RectI.init(2, 3, 0, 0), RectF.init(1.25, 2.75, 0.5, 0.1).snapIn(i32));
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
test "logical and physical pixel conversion is explicit" {
|
|
880
|
+
const logical = RectF.init(0.25, 1.25, 10.25, 20.25);
|
|
881
|
+
const physical = logicalToPhysical(logical, 2);
|
|
882
|
+
|
|
883
|
+
try std.testing.expectEqualDeep(RectI.init(0, 2, 21, 41), physical);
|
|
884
|
+
try std.testing.expectEqualDeep(RectF.init(0, 1, 10.5, 20.5), physicalToLogical(physical, 2));
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
test {
|
|
888
|
+
std.testing.refAllDecls(@This());
|
|
889
|
+
}
|