@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,3028 @@
|
|
|
1
|
+
#import "appkit_host.h"
|
|
2
|
+
|
|
3
|
+
#import <AppKit/AppKit.h>
|
|
4
|
+
#import <CoreFoundation/CoreFoundation.h>
|
|
5
|
+
#import <ImageIO/ImageIO.h>
|
|
6
|
+
#import <Security/Security.h>
|
|
7
|
+
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
|
|
8
|
+
#include <crt_externs.h>
|
|
9
|
+
#include <stdint.h>
|
|
10
|
+
#include <stdio.h>
|
|
11
|
+
#include <stdlib.h>
|
|
12
|
+
#include <string.h>
|
|
13
|
+
|
|
14
|
+
#include "include/cef_app.h"
|
|
15
|
+
#include "include/cef_browser.h"
|
|
16
|
+
#include "include/cef_client.h"
|
|
17
|
+
#include "include/cef_command_line.h"
|
|
18
|
+
#include "include/cef_application_mac.h"
|
|
19
|
+
#include "include/cef_load_handler.h"
|
|
20
|
+
#include "include/cef_process_message.h"
|
|
21
|
+
#include "include/cef_values.h"
|
|
22
|
+
#include "include/cef_v8.h"
|
|
23
|
+
#include "include/wrapper/cef_library_loader.h"
|
|
24
|
+
#include <math.h>
|
|
25
|
+
#include <map>
|
|
26
|
+
|
|
27
|
+
#ifndef NATIVE_SDK_CEF_DIR
|
|
28
|
+
#define NATIVE_SDK_CEF_DIR "third_party/cef/macos"
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
@class NativeSdkChromiumHost;
|
|
32
|
+
|
|
33
|
+
@interface NativeSdkChromiumApplication : NSApplication <CefAppProtocol>
|
|
34
|
+
@property(nonatomic, assign) BOOL handlingSendEvent;
|
|
35
|
+
@end
|
|
36
|
+
|
|
37
|
+
namespace {
|
|
38
|
+
|
|
39
|
+
static const char *kBridgeMessageName = "native_sdk_bridge";
|
|
40
|
+
static const char *kBridgeEnabledExtraInfo = "nativeSdkBridgeEnabled";
|
|
41
|
+
static const uint32_t NativeSdkShortcutModifierPrimary = 1u << 0;
|
|
42
|
+
static const uint32_t NativeSdkShortcutModifierCommand = 1u << 1;
|
|
43
|
+
static const uint32_t NativeSdkShortcutModifierControl = 1u << 2;
|
|
44
|
+
static const uint32_t NativeSdkShortcutModifierOption = 1u << 3;
|
|
45
|
+
static const uint32_t NativeSdkShortcutModifierShift = 1u << 4;
|
|
46
|
+
static const char *NativeSdkCefBridgeScript();
|
|
47
|
+
static NSRect NativeSdkConstrainFrame(NSRect frame);
|
|
48
|
+
static NSString *NativeSdkResolvedAssetRoot(NSString *rootPath);
|
|
49
|
+
static NSURL *NativeSdkAssetEntryFileURL(NSString *rootPath, NSString *entryPath);
|
|
50
|
+
static NSString *NativeSdkSafeAssetPath(NSURL *url, NSString *entryPath);
|
|
51
|
+
static NSArray<NSString *> *NativeSdkPolicyListFromBytes(const char *bytes, size_t len, NSArray<NSString *> *fallback);
|
|
52
|
+
static NSString *NativeSdkAbsolutePath(NSString *path);
|
|
53
|
+
static NSString *NativeSdkExistingPath(NSString *path);
|
|
54
|
+
static NSString *NativeSdkCefFrameworkPath(void);
|
|
55
|
+
static NSString *NativeSdkOriginForURL(NSURL *url);
|
|
56
|
+
static BOOL NativeSdkPolicyListMatches(NSArray<NSString *> *values, NSURL *url);
|
|
57
|
+
static NSString *NativeSdkShortcutKeyForEvent(NSEvent *event);
|
|
58
|
+
static BOOL NativeSdkShortcutUsesImplicitShift(NSString *key, NSEvent *event);
|
|
59
|
+
static BOOL NativeSdkShortcutModifiersMatch(uint32_t shortcutModifiers, NSEventModifierFlags eventModifiers, BOOL allowImplicitShift);
|
|
60
|
+
|
|
61
|
+
static NSString *NativeSdkStringFromBytes(const char *bytes, size_t len) {
|
|
62
|
+
if (!bytes || len == 0) return nil;
|
|
63
|
+
return [[NSString alloc] initWithBytes:bytes length:len encoding:NSUTF8StringEncoding];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
static uint64_t NativeSdkChromiumTimestampNanoseconds(void) {
|
|
67
|
+
return (uint64_t)([[NSDate date] timeIntervalSince1970] * 1000000000.0);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
static NSString *NativeSdkPasteboardTypeForMime(const char *mime_type, size_t mime_type_len) {
|
|
71
|
+
NSString *mime = NativeSdkStringFromBytes(mime_type, mime_type_len).lowercaseString;
|
|
72
|
+
if ([mime isEqualToString:@"text"] || [mime isEqualToString:@"text/plain"]) return NSPasteboardTypeString;
|
|
73
|
+
if ([mime isEqualToString:@"text/html"]) return NSPasteboardTypeHTML;
|
|
74
|
+
if ([mime isEqualToString:@"text/rtf"] || [mime isEqualToString:@"application/rtf"]) return NSPasteboardTypeRTF;
|
|
75
|
+
return nil;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
static NSMutableDictionary *NativeSdkCredentialQuery(NSString *service, NSString *account) {
|
|
79
|
+
return [@{
|
|
80
|
+
(__bridge id)kSecClass: (__bridge id)kSecClassGenericPassword,
|
|
81
|
+
(__bridge id)kSecAttrService: service,
|
|
82
|
+
(__bridge id)kSecAttrAccount: account,
|
|
83
|
+
} mutableCopy];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
class NativeSdkCefBridgeV8Handler final : public CefV8Handler {
|
|
87
|
+
public:
|
|
88
|
+
bool Execute(const CefString& name, CefRefPtr<CefV8Value> object, const CefV8ValueList& arguments, CefRefPtr<CefV8Value>& retval, CefString& exception) override {
|
|
89
|
+
(void)object;
|
|
90
|
+
if (name == "postMessage" && arguments.size() == 1 && arguments[0]->IsString()) {
|
|
91
|
+
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(kBridgeMessageName);
|
|
92
|
+
message->GetArgumentList()->SetString(0, arguments[0]->GetStringValue());
|
|
93
|
+
CefV8Context::GetCurrentContext()->GetFrame()->SendProcessMessage(PID_BROWSER, message);
|
|
94
|
+
retval = CefV8Value::CreateBool(true);
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
exception = "Invalid native-sdk bridge message";
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private:
|
|
102
|
+
IMPLEMENT_REFCOUNTING(NativeSdkCefBridgeV8Handler);
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
class NativeSdkCefClient final : public CefClient, public CefLifeSpanHandler, public CefLoadHandler, public CefRequestHandler {
|
|
106
|
+
public:
|
|
107
|
+
explicit NativeSdkCefClient(NativeSdkChromiumHost *host, uint64_t window_id) : host_(host), window_id_(window_id) {}
|
|
108
|
+
NativeSdkCefClient(NativeSdkChromiumHost *host, uint64_t window_id, std::string webview_key, uint64_t webview_generation, bool bridge_enabled) : host_(host), window_id_(window_id), webview_key_(webview_key), webview_generation_(webview_generation), bridge_enabled_(bridge_enabled) {}
|
|
109
|
+
|
|
110
|
+
CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override {
|
|
111
|
+
return this;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
CefRefPtr<CefRequestHandler> GetRequestHandler() override {
|
|
115
|
+
return this;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
CefRefPtr<CefLoadHandler> GetLoadHandler() override {
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
|
|
123
|
+
void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
|
|
124
|
+
void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) override;
|
|
125
|
+
bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, bool user_gesture, bool is_redirect) override;
|
|
126
|
+
bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) override;
|
|
127
|
+
std::string WebViewLabel() const {
|
|
128
|
+
if (webview_key_.empty()) return "main";
|
|
129
|
+
size_t separator = webview_key_.find(':');
|
|
130
|
+
return separator == std::string::npos ? webview_key_ : webview_key_.substr(separator + 1);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
private:
|
|
134
|
+
NativeSdkChromiumHost *host_;
|
|
135
|
+
uint64_t window_id_;
|
|
136
|
+
std::string webview_key_;
|
|
137
|
+
uint64_t webview_generation_ = 0;
|
|
138
|
+
bool bridge_enabled_ = true;
|
|
139
|
+
IMPLEMENT_REFCOUNTING(NativeSdkCefClient);
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
class NativeSdkCefApp final : public CefApp, public CefRenderProcessHandler {
|
|
143
|
+
public:
|
|
144
|
+
NativeSdkCefApp() = default;
|
|
145
|
+
|
|
146
|
+
void OnBeforeCommandLineProcessing(const CefString& process_type, CefRefPtr<CefCommandLine> command_line) override {
|
|
147
|
+
(void)process_type;
|
|
148
|
+
command_line->AppendSwitchWithValue("password-store", "basic");
|
|
149
|
+
command_line->AppendSwitch("use-mock-keychain");
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() override {
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
void OnBrowserCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDictionaryValue> extra_info) override {
|
|
157
|
+
if (!browser) return;
|
|
158
|
+
bool bridge_enabled = false;
|
|
159
|
+
if (extra_info && extra_info->HasKey(kBridgeEnabledExtraInfo)) {
|
|
160
|
+
bridge_enabled = extra_info->GetBool(kBridgeEnabledExtraInfo);
|
|
161
|
+
}
|
|
162
|
+
bridge_enabled_by_browser_id_[browser->GetIdentifier()] = bridge_enabled;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
void OnBrowserDestroyed(CefRefPtr<CefBrowser> browser) override {
|
|
166
|
+
if (!browser) return;
|
|
167
|
+
bridge_enabled_by_browser_id_.erase(browser->GetIdentifier());
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
void OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context) override {
|
|
171
|
+
if (!frame || !frame->IsMain()) return;
|
|
172
|
+
const auto found = browser ? bridge_enabled_by_browser_id_.find(browser->GetIdentifier()) : bridge_enabled_by_browser_id_.end();
|
|
173
|
+
if (found == bridge_enabled_by_browser_id_.end() || !found->second) return;
|
|
174
|
+
CefRefPtr<CefV8Value> bridge = CefV8Value::CreateObject(nullptr, nullptr);
|
|
175
|
+
bridge->SetValue("postMessage", CefV8Value::CreateFunction("postMessage", new NativeSdkCefBridgeV8Handler()), V8_PROPERTY_ATTRIBUTE_READONLY);
|
|
176
|
+
context->GetGlobal()->SetValue("nativeSdkCefBridge", bridge, V8_PROPERTY_ATTRIBUTE_READONLY);
|
|
177
|
+
frame->ExecuteJavaScript(CefString(NativeSdkCefBridgeScript()), frame->GetURL(), 0);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private:
|
|
181
|
+
std::map<int, bool> bridge_enabled_by_browser_id_;
|
|
182
|
+
IMPLEMENT_REFCOUNTING(NativeSdkCefApp);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
static CefRefPtr<CefDictionaryValue> NativeSdkCefExtraInfo(bool bridge_enabled) {
|
|
186
|
+
CefRefPtr<CefDictionaryValue> extra_info = CefDictionaryValue::Create();
|
|
187
|
+
extra_info->SetBool(kBridgeEnabledExtraInfo, bridge_enabled);
|
|
188
|
+
return extra_info;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
static bool g_cef_initialized = false;
|
|
192
|
+
static bool g_cef_shutdown = false;
|
|
193
|
+
static CefScopedLibraryLoader g_cef_library_loader;
|
|
194
|
+
static bool g_cef_library_loaded = false;
|
|
195
|
+
|
|
196
|
+
static void shutdownCefIfNeeded() {
|
|
197
|
+
if (!g_cef_initialized || g_cef_shutdown) return;
|
|
198
|
+
CefShutdown();
|
|
199
|
+
g_cef_initialized = false;
|
|
200
|
+
g_cef_shutdown = true;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
static void ensureCefInitialized() {
|
|
204
|
+
if (g_cef_initialized) return;
|
|
205
|
+
g_cef_shutdown = false;
|
|
206
|
+
|
|
207
|
+
if (!g_cef_library_loaded) {
|
|
208
|
+
if (!g_cef_library_loader.LoadInMain()) {
|
|
209
|
+
fprintf(stderr, "failed to load Chromium Embedded Framework\n");
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
g_cef_library_loaded = true;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
CefMainArgs args(*_NSGetArgc(), *_NSGetArgv());
|
|
216
|
+
CefRefPtr<NativeSdkCefApp> app = new NativeSdkCefApp();
|
|
217
|
+
const int exit_code = CefExecuteProcess(args, app, nullptr);
|
|
218
|
+
if (exit_code >= 0) exit(exit_code);
|
|
219
|
+
|
|
220
|
+
CefSettings settings;
|
|
221
|
+
settings.no_sandbox = true;
|
|
222
|
+
settings.multi_threaded_message_loop = false;
|
|
223
|
+
NSString *frameworkPath = NativeSdkCefFrameworkPath();
|
|
224
|
+
NSString *resourcesPath = [frameworkPath stringByAppendingPathComponent:@"Resources"];
|
|
225
|
+
NSString *appSupport = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).firstObject ?: NSTemporaryDirectory();
|
|
226
|
+
NSString *cefDataRoot = [appSupport stringByAppendingPathComponent:@"native-sdk/CEF"];
|
|
227
|
+
NSString *cefCachePath = [cefDataRoot stringByAppendingPathComponent:@"Default"];
|
|
228
|
+
[[NSFileManager defaultManager] createDirectoryAtPath:cefCachePath withIntermediateDirectories:YES attributes:nil error:nil];
|
|
229
|
+
NSString *executablePath = [NSBundle mainBundle].executablePath ?: [[[NSProcessInfo processInfo] arguments] firstObject];
|
|
230
|
+
CefString(&settings.framework_dir_path).FromString(frameworkPath.UTF8String);
|
|
231
|
+
CefString(&settings.resources_dir_path).FromString(resourcesPath.UTF8String);
|
|
232
|
+
CefString(&settings.root_cache_path).FromString(cefDataRoot.UTF8String);
|
|
233
|
+
CefString(&settings.cache_path).FromString(cefCachePath.UTF8String);
|
|
234
|
+
if (executablePath.length > 0) {
|
|
235
|
+
CefString(&settings.browser_subprocess_path).FromString(executablePath.UTF8String);
|
|
236
|
+
}
|
|
237
|
+
if (!CefInitialize(args, settings, app, nullptr)) {
|
|
238
|
+
fprintf(stderr, "failed to initialize Chromium Embedded Framework\n");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
g_cef_initialized = true;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
static NSString *temporaryHtmlUrl(NSString *html) {
|
|
245
|
+
NSString *filename = [NSString stringWithFormat:@"native-sdk-cef-%@.html", [[NSUUID UUID] UUIDString]];
|
|
246
|
+
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
|
|
247
|
+
NSError *error = nil;
|
|
248
|
+
if (![html writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
|
|
249
|
+
NSLog(@"native-sdk: failed to write temporary CEF HTML file: %@", error);
|
|
250
|
+
return @"about:blank";
|
|
251
|
+
}
|
|
252
|
+
return [NSURL fileURLWithPath:path].absoluteString;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
static NSString *NativeSdkResolvedAssetRoot(NSString *rootPath) {
|
|
256
|
+
if (rootPath.length == 0 || [rootPath isEqualToString:@"."]) {
|
|
257
|
+
return [NSBundle mainBundle].resourcePath ?: [[NSFileManager defaultManager] currentDirectoryPath];
|
|
258
|
+
}
|
|
259
|
+
if (rootPath.isAbsolutePath) return rootPath;
|
|
260
|
+
NSString *resourcePath = [NSBundle mainBundle].resourcePath;
|
|
261
|
+
if (resourcePath.length > 0) {
|
|
262
|
+
return [resourcePath stringByAppendingPathComponent:rootPath];
|
|
263
|
+
}
|
|
264
|
+
return [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:rootPath];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
static NSURL *NativeSdkAssetEntryFileURL(NSString *rootPath, NSString *entryPath) {
|
|
268
|
+
NSString *entry = entryPath.length > 0 ? entryPath : @"index.html";
|
|
269
|
+
while ([entry hasPrefix:@"/"]) {
|
|
270
|
+
entry = [entry substringFromIndex:1];
|
|
271
|
+
}
|
|
272
|
+
return [NSURL fileURLWithPath:[NativeSdkResolvedAssetRoot(rootPath ?: @"") stringByAppendingPathComponent:entry]];
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
static BOOL NativeSdkPathHasUnsafeSegment(NSString *path) {
|
|
276
|
+
for (NSString *segment in [path componentsSeparatedByString:@"/"]) {
|
|
277
|
+
if (segment.length == 0) continue;
|
|
278
|
+
if ([segment isEqualToString:@"."] || [segment isEqualToString:@".."]) return YES;
|
|
279
|
+
if ([segment containsString:@"\\"]) return YES;
|
|
280
|
+
}
|
|
281
|
+
return NO;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
static NSString *NativeSdkSafeAssetPath(NSURL *url, NSString *entryPath) {
|
|
285
|
+
if (!url) return nil;
|
|
286
|
+
NSString *path = url.path.stringByRemovingPercentEncoding ?: url.path;
|
|
287
|
+
if (path.length == 0 || [path isEqualToString:@"/"]) return entryPath.length > 0 ? entryPath : @"index.html";
|
|
288
|
+
while ([path hasPrefix:@"/"]) {
|
|
289
|
+
path = [path substringFromIndex:1];
|
|
290
|
+
}
|
|
291
|
+
if (path.length == 0) return entryPath.length > 0 ? entryPath : @"index.html";
|
|
292
|
+
if (NativeSdkPathHasUnsafeSegment(path)) return nil;
|
|
293
|
+
return path;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
static NSString *NativeSdkAbsolutePath(NSString *path) {
|
|
297
|
+
if (path.length == 0) return [[NSFileManager defaultManager] currentDirectoryPath];
|
|
298
|
+
if (path.isAbsolutePath) return path;
|
|
299
|
+
return [[[NSFileManager defaultManager] currentDirectoryPath] stringByAppendingPathComponent:path];
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
static NSString *NativeSdkExistingPath(NSString *path) {
|
|
303
|
+
if (path.length == 0) return nil;
|
|
304
|
+
return [[NSFileManager defaultManager] fileExistsAtPath:path] ? path : nil;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
static NSString *NativeSdkCefFrameworkPath(void) {
|
|
308
|
+
NSBundle *bundle = [NSBundle mainBundle];
|
|
309
|
+
NSString *bundleFramework = [[bundle privateFrameworksPath] stringByAppendingPathComponent:@"Chromium Embedded Framework.framework"];
|
|
310
|
+
if (NativeSdkExistingPath(bundleFramework)) return bundleFramework;
|
|
311
|
+
|
|
312
|
+
NSString *bundleContentsFramework = [[bundle.bundlePath stringByAppendingPathComponent:@"Contents/Frameworks"] stringByAppendingPathComponent:@"Chromium Embedded Framework.framework"];
|
|
313
|
+
if (NativeSdkExistingPath(bundleContentsFramework)) return bundleContentsFramework;
|
|
314
|
+
|
|
315
|
+
NSString *devRoot = NativeSdkAbsolutePath(@NATIVE_SDK_CEF_DIR);
|
|
316
|
+
return [devRoot stringByAppendingPathComponent:@"Release/Chromium Embedded Framework.framework"];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
static NSRect NativeSdkConstrainFrame(NSRect frame) {
|
|
320
|
+
NSScreen *screen = [NSScreen mainScreen];
|
|
321
|
+
if (!screen) return frame;
|
|
322
|
+
NSRect visible = screen.visibleFrame;
|
|
323
|
+
if (frame.size.width > visible.size.width) frame.size.width = visible.size.width;
|
|
324
|
+
if (frame.size.height > visible.size.height) frame.size.height = visible.size.height;
|
|
325
|
+
if (NSMinX(frame) < NSMinX(visible)) frame.origin.x = NSMinX(visible);
|
|
326
|
+
if (NSMinY(frame) < NSMinY(visible)) frame.origin.y = NSMinY(visible);
|
|
327
|
+
if (NSMaxX(frame) > NSMaxX(visible)) frame.origin.x = NSMaxX(visible) - frame.size.width;
|
|
328
|
+
if (NSMaxY(frame) > NSMaxY(visible)) frame.origin.y = NSMaxY(visible) - frame.size.height;
|
|
329
|
+
return frame;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
static const char *NativeSdkCefBridgeScript() {
|
|
333
|
+
return "(function(){"
|
|
334
|
+
"if(window.zero&&window.zero.invoke){return;}"
|
|
335
|
+
"var pending=new Map();"
|
|
336
|
+
"var listeners=new Map();"
|
|
337
|
+
"var nextId=1;"
|
|
338
|
+
"function post(message){"
|
|
339
|
+
"if(window.webkit&&window.webkit.messageHandlers&&window.webkit.messageHandlers.nativeSdkBridge){window.webkit.messageHandlers.nativeSdkBridge.postMessage(message);return;}"
|
|
340
|
+
"if(window.nativeSdkCefBridge&&window.nativeSdkCefBridge.postMessage){window.nativeSdkCefBridge.postMessage(message);return;}"
|
|
341
|
+
"throw new Error('native-sdk bridge transport is unavailable');"
|
|
342
|
+
"}"
|
|
343
|
+
"function complete(response){"
|
|
344
|
+
"var id=response&&response.id!=null?String(response.id):'';"
|
|
345
|
+
"var entry=pending.get(id);"
|
|
346
|
+
"if(!entry){return;}"
|
|
347
|
+
"pending.delete(id);"
|
|
348
|
+
"if(response.ok){entry.resolve(response.result===undefined?null:response.result);return;}"
|
|
349
|
+
"var errorInfo=response.error||{};"
|
|
350
|
+
"var error=new Error(errorInfo.message||'Native command failed');"
|
|
351
|
+
"error.code=errorInfo.code||'internal_error';"
|
|
352
|
+
"entry.reject(error);"
|
|
353
|
+
"}"
|
|
354
|
+
"function invoke(command,payload){"
|
|
355
|
+
"if(typeof command!=='string'||command.length===0){return Promise.reject(new TypeError('command must be a non-empty string'));}"
|
|
356
|
+
"var id=String(nextId++);"
|
|
357
|
+
"var envelope=JSON.stringify({id:id,command:command,payload:payload===undefined?null:payload});"
|
|
358
|
+
"return new Promise(function(resolve,reject){"
|
|
359
|
+
"pending.set(id,{resolve:resolve,reject:reject});"
|
|
360
|
+
"try{post(envelope);}catch(error){pending.delete(id);reject(error);}"
|
|
361
|
+
"});"
|
|
362
|
+
"}"
|
|
363
|
+
"function selector(value){return typeof value==='number'?{id:value}:{label:String(value)};}"
|
|
364
|
+
"function ensureString(value,name){if(typeof value!=='string'||value.length===0){throw new TypeError(name+' must be a non-empty string');}return value;}"
|
|
365
|
+
"function ensureText(value,name){if(typeof value!=='string'){throw new TypeError(name+' must be a string');}return value;}"
|
|
366
|
+
"function ensureNumber(value,name){if(typeof value!=='number'||!isFinite(value)){throw new TypeError(name+' must be a finite number');}return value;}"
|
|
367
|
+
"function commandPayload(value){if(typeof value==='string'){return {name:ensureString(value,'command')};}value=value||{};var name=value.name!=null?value.name:value.id;return {name:ensureString(name,'command')};}"
|
|
368
|
+
"function validateWebViewSelector(options){if(options.label!=null){ensureString(options.label,'label');}if(options.windowId!=null&&(typeof options.windowId!=='number'||!isFinite(options.windowId)||options.windowId<0||Math.floor(options.windowId)!==options.windowId)){throw new TypeError('windowId must be a non-negative integer');}}"
|
|
369
|
+
"function framePayload(options){options=options||{};validateWebViewSelector(options);var frame=options.frame||options;return {label:options.label,windowId:options.windowId,url:options.url,frame:{x:frame.x==null?0:ensureNumber(frame.x,'frame.x'),y:frame.y==null?0:ensureNumber(frame.y,'frame.y'),width:ensureNumber(frame.width,'frame.width'),height:ensureNumber(frame.height,'frame.height')}};}"
|
|
370
|
+
"function createPayload(options){options=options||{};ensureString(options.url,'url');var payload=framePayload(options);if(options.layer!=null){payload.layer=ensureNumber(options.layer,'layer');}if(options.transparent!=null){payload.transparent=!!options.transparent;}if(options.bridge!=null){payload.bridge=!!options.bridge;}return payload;}"
|
|
371
|
+
"function navigatePayload(options){options=options||{};validateWebViewSelector(options);ensureString(options.url,'url');return {label:options.label,windowId:options.windowId,url:options.url};}"
|
|
372
|
+
"function closePayload(options){options=options||{};validateWebViewSelector(options);return {label:options.label,windowId:options.windowId};}"
|
|
373
|
+
"function webviewHandle(info){return Object.freeze(Object.assign({},info,{setFrame:function(frame){return webviews.setFrame({label:info.label,windowId:info.windowId,frame:frame});},navigate:function(url){return webviews.navigate({label:info.label,windowId:info.windowId,url:url});},setZoom:function(zoom){return webviews.setZoom({label:info.label,windowId:info.windowId,zoom:zoom});},setLayer:function(layer){return webviews.setLayer({label:info.label,windowId:info.windowId,layer:layer});},close:function(){return webviews.close({label:info.label,windowId:info.windowId});}}));}"
|
|
374
|
+
"function validateViewSelector(options){options=options||{};ensureString(options.label,'label');if(options.windowId!=null&&(typeof options.windowId!=='number'||!isFinite(options.windowId)||options.windowId<0||Math.floor(options.windowId)!==options.windowId)){throw new TypeError('windowId must be a non-negative integer');}}"
|
|
375
|
+
"function viewSelectorPayload(options){if(typeof options==='string'){return {label:ensureString(options,'label')};}options=options||{};validateViewSelector(options);return {label:options.label,windowId:options.windowId};}"
|
|
376
|
+
"function optionalFramePayload(options){var frame=options.frame||((options.x!=null||options.y!=null||options.width!=null||options.height!=null)?options:null);if(!frame){return null;}return {x:frame.x==null?0:ensureNumber(frame.x,'frame.x'),y:frame.y==null?0:ensureNumber(frame.y,'frame.y'),width:ensureNumber(frame.width,'frame.width'),height:ensureNumber(frame.height,'frame.height')};}"
|
|
377
|
+
"function viewCreatePayload(options){options=options||{};validateViewSelector(options);ensureString(options.kind,'kind');var payload={label:options.label,kind:options.kind,windowId:options.windowId};var frame=optionalFramePayload(options);if(frame){payload.frame=frame;}if(options.parent!=null){payload.parent=ensureString(options.parent,'parent');}if(options.role!=null){payload.role=ensureText(options.role,'role');}if(options.accessibilityLabel!=null){payload.accessibilityLabel=ensureText(options.accessibilityLabel,'accessibilityLabel');}if(options.text!=null){payload.text=ensureText(options.text,'text');}if(options.command!=null){payload.command=ensureText(options.command,'command');}if(options.url!=null){payload.url=ensureString(options.url,'url');}if(options.layer!=null){payload.layer=ensureNumber(options.layer,'layer');}if(options.visible!=null){payload.visible=!!options.visible;}if(options.enabled!=null){payload.enabled=!!options.enabled;}if(options.transparent!=null){payload.transparent=!!options.transparent;}if(options.bridge!=null){payload.bridge=!!options.bridge;}return payload;}"
|
|
378
|
+
"function viewPatchPayload(options){options=options||{};validateViewSelector(options);var payload={label:options.label,windowId:options.windowId};var frame=optionalFramePayload(options);if(frame){payload.frame=frame;}if(options.layer!=null){payload.layer=ensureNumber(options.layer,'layer');}if(options.visible!=null){payload.visible=!!options.visible;}if(options.enabled!=null){payload.enabled=!!options.enabled;}if(options.role!=null){payload.role=ensureText(options.role,'role');}if(options.accessibilityLabel!=null){payload.accessibilityLabel=ensureText(options.accessibilityLabel,'accessibilityLabel');}if(options.text!=null){payload.text=ensureText(options.text,'text');}if(options.command!=null){payload.command=ensureText(options.command,'command');}if(options.url!=null){payload.url=ensureString(options.url,'url');}return payload;}"
|
|
379
|
+
"function viewFramePayload(options){options=options||{};validateViewSelector(options);var frame=options.frame||options;return {label:options.label,windowId:options.windowId,frame:{x:frame.x==null?0:ensureNumber(frame.x,'frame.x'),y:frame.y==null?0:ensureNumber(frame.y,'frame.y'),width:ensureNumber(frame.width,'frame.width'),height:ensureNumber(frame.height,'frame.height')}};}"
|
|
380
|
+
"function viewVisiblePayload(options){options=options||{};validateViewSelector(options);if(options.visible==null){throw new TypeError('visible is required');}return {label:options.label,windowId:options.windowId,visible:!!options.visible};}"
|
|
381
|
+
"function viewHandle(info){return Object.freeze(Object.assign({},info,{update:function(patch){return views.update(Object.assign({},patch||{},{label:info.label,windowId:info.windowId}));},setFrame:function(frame){return views.setFrame({label:info.label,windowId:info.windowId,frame:frame});},setVisible:function(visible){return views.setVisible({label:info.label,windowId:info.windowId,visible:visible});},focus:function(){return views.focus({label:info.label,windowId:info.windowId});},close:function(){return views.close({label:info.label,windowId:info.windowId});}}));}"
|
|
382
|
+
"function on(name,callback){if(typeof callback!=='function'){throw new TypeError('callback must be a function');}var set=listeners.get(name);if(!set){set=new Set();listeners.set(name,set);}set.add(callback);return function(){off(name,callback);};}"
|
|
383
|
+
"function off(name,callback){var set=listeners.get(name);if(set){set.delete(callback);if(set.size===0){listeners.delete(name);}}}"
|
|
384
|
+
"function emit(name,detail){var set=listeners.get(name);if(set){Array.from(set).forEach(function(callback){callback(detail);});}window.dispatchEvent(new CustomEvent('native-sdk:'+name,{detail:detail}));}"
|
|
385
|
+
"var commands=Object.freeze({"
|
|
386
|
+
"invoke:function(value){return invoke('native-sdk.command.invoke',commandPayload(value));},"
|
|
387
|
+
"list:function(){return invoke('native-sdk.command.list',{});}"
|
|
388
|
+
"});"
|
|
389
|
+
"var windows=Object.freeze({"
|
|
390
|
+
"create:function(options){return invoke('native-sdk.window.create',options||{});},"
|
|
391
|
+
"list:function(){return invoke('native-sdk.window.list',{});},"
|
|
392
|
+
"focus:function(value){return invoke('native-sdk.window.focus',selector(value));},"
|
|
393
|
+
"close:function(value){return invoke('native-sdk.window.close',selector(value));}"
|
|
394
|
+
"});"
|
|
395
|
+
"var dialogs=Object.freeze({"
|
|
396
|
+
"openFile:function(options){return invoke('native-sdk.dialog.openFile',options||{});},"
|
|
397
|
+
"saveFile:function(options){return invoke('native-sdk.dialog.saveFile',options||{});},"
|
|
398
|
+
"showMessage:function(options){return invoke('native-sdk.dialog.showMessage',options||{});}"
|
|
399
|
+
"});"
|
|
400
|
+
"function clipboardReadPayload(value){value=value||{};return {mimeType:ensureString(value.mimeType||value.type||'text/plain','mimeType')};}"
|
|
401
|
+
"function clipboardWritePayload(value){if(typeof value==='string'){return {mimeType:'text/plain',data:value};}value=value||{};var data=value.data!=null?value.data:(value.text!=null?value.text:value.value);return {mimeType:ensureString(value.mimeType||value.type||'text/plain','mimeType'),data:ensureText(data,'data')};}"
|
|
402
|
+
"var clipboard=Object.freeze({"
|
|
403
|
+
"readText:function(){return invoke('native-sdk.clipboard.readText',{});},"
|
|
404
|
+
"writeText:function(value){var text=typeof value==='string'?value:(value||{}).text;return invoke('native-sdk.clipboard.writeText',{text:ensureText(text,'text')});},"
|
|
405
|
+
"read:function(value){return invoke('native-sdk.clipboard.read',clipboardReadPayload(value));},"
|
|
406
|
+
"write:function(value){return invoke('native-sdk.clipboard.write',clipboardWritePayload(value));}"
|
|
407
|
+
"});"
|
|
408
|
+
"var os=Object.freeze({"
|
|
409
|
+
"openUrl:function(value){var options=typeof value==='string'?{url:value}:(value||{});return invoke('native-sdk.os.openUrl',{url:ensureString(options.url,'url')});},"
|
|
410
|
+
"showNotification:function(value){var options=typeof value==='string'?{title:value}:(value||{});var payload={title:ensureString(options.title,'title')};if(options.subtitle!=null){payload.subtitle=ensureString(options.subtitle,'subtitle');}if(options.body!=null){payload.body=ensureString(options.body,'body');}return invoke('native-sdk.os.showNotification',payload);},"
|
|
411
|
+
"revealPath:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.revealPath',{path:ensureString(options.path,'path')});},"
|
|
412
|
+
"addRecentDocument:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.addRecentDocument',{path:ensureString(options.path,'path')});},"
|
|
413
|
+
"clearRecentDocuments:function(){return invoke('native-sdk.os.clearRecentDocuments',{});}"
|
|
414
|
+
"});"
|
|
415
|
+
"function credentialPayload(value){value=value||{};return {service:ensureString(value.service,'service'),account:ensureString(value.account,'account')};}"
|
|
416
|
+
"function credentialSetPayload(value){var payload=credentialPayload(value);payload.secret=ensureString(value.secret!=null?value.secret:value.value,'secret');return payload;}"
|
|
417
|
+
"var credentials=Object.freeze({"
|
|
418
|
+
"set:function(value){return invoke('native-sdk.credentials.set',credentialSetPayload(value));},"
|
|
419
|
+
"get:function(value){return invoke('native-sdk.credentials.get',credentialPayload(value));},"
|
|
420
|
+
"delete:function(value){return invoke('native-sdk.credentials.delete',credentialPayload(value));}"
|
|
421
|
+
"});"
|
|
422
|
+
"function platformFeaturePayload(value){if(typeof value==='string'){return {feature:ensureString(value,'feature')};}value=value||{};return {feature:ensureString(value.feature!=null?value.feature:value.name,'feature')};}"
|
|
423
|
+
"var platform=Object.freeze({"
|
|
424
|
+
"supports:function(value){return invoke('native-sdk.platform.supports',platformFeaturePayload(value));}"
|
|
425
|
+
"});"
|
|
426
|
+
"function zoomPayload(options){options=options||{};validateWebViewSelector(options);return {label:options.label,windowId:options.windowId,zoom:ensureNumber(options.zoom,'zoom')};}"
|
|
427
|
+
"function layerPayload(options){options=options||{};validateWebViewSelector(options);return {label:options.label,windowId:options.windowId,layer:ensureNumber(options.layer,'layer')};}"
|
|
428
|
+
"var webviews=Object.freeze({"
|
|
429
|
+
"create:function(options){return invoke('native-sdk.webview.create',createPayload(options)).then(webviewHandle);},"
|
|
430
|
+
"list:function(){return invoke('native-sdk.webview.list',{});},"
|
|
431
|
+
"setFrame:function(options){return invoke('native-sdk.webview.setFrame',framePayload(options));},"
|
|
432
|
+
"navigate:function(options){return invoke('native-sdk.webview.navigate',navigatePayload(options));},"
|
|
433
|
+
"setZoom:function(options){return invoke('native-sdk.webview.setZoom',zoomPayload(options));},"
|
|
434
|
+
"setLayer:function(options){return invoke('native-sdk.webview.setLayer',layerPayload(options));},"
|
|
435
|
+
"close:function(options){return invoke('native-sdk.webview.close',closePayload(options));}"
|
|
436
|
+
"});"
|
|
437
|
+
"var views=Object.freeze({"
|
|
438
|
+
"create:function(options){return invoke('native-sdk.view.create',viewCreatePayload(options)).then(viewHandle);},"
|
|
439
|
+
"list:function(){return invoke('native-sdk.view.list',{});},"
|
|
440
|
+
"update:function(options,patch){if(typeof options==='string'){return invoke('native-sdk.view.update',viewPatchPayload(Object.assign({},patch||{},{label:options}))).then(viewHandle);}return invoke('native-sdk.view.update',viewPatchPayload(options)).then(viewHandle);},"
|
|
441
|
+
"setFrame:function(options){return invoke('native-sdk.view.setFrame',viewFramePayload(options)).then(viewHandle);},"
|
|
442
|
+
"setVisible:function(options){return invoke('native-sdk.view.setVisible',viewVisiblePayload(options)).then(viewHandle);},"
|
|
443
|
+
"focus:function(options){return invoke('native-sdk.view.focus',viewSelectorPayload(options)).then(viewHandle);},"
|
|
444
|
+
"focusNext:function(options){options=options||{};return invoke('native-sdk.view.focusNext',{windowId:options.windowId}).then(viewHandle);},"
|
|
445
|
+
"focusPrevious:function(options){options=options||{};return invoke('native-sdk.view.focusPrevious',{windowId:options.windowId}).then(viewHandle);},"
|
|
446
|
+
"close:function(options){return invoke('native-sdk.view.close',viewSelectorPayload(options));}"
|
|
447
|
+
"});"
|
|
448
|
+
"Object.defineProperty(window,'zero',{value:Object.freeze({invoke:invoke,on:on,off:off,commands:commands,windows:windows,dialogs:dialogs,clipboard:clipboard,os:os,credentials:credentials,platform:platform,webviews:webviews,views:views,_complete:complete,_emit:emit}),configurable:false});"
|
|
449
|
+
"})();";
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
} // namespace
|
|
453
|
+
|
|
454
|
+
@implementation NativeSdkChromiumApplication
|
|
455
|
+
|
|
456
|
+
- (BOOL)isHandlingSendEvent {
|
|
457
|
+
return self.handlingSendEvent;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
- (void)setHandlingSendEvent:(BOOL)handlingSendEvent {
|
|
461
|
+
_handlingSendEvent = handlingSendEvent;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
- (void)sendEvent:(NSEvent *)event {
|
|
465
|
+
CefScopedSendingEvent scopedSendingEvent;
|
|
466
|
+
[super sendEvent:event];
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
@end
|
|
470
|
+
|
|
471
|
+
@interface NativeSdkChromiumWindowDelegate : NSObject <NSWindowDelegate>
|
|
472
|
+
@property(nonatomic, assign) NativeSdkChromiumHost *host;
|
|
473
|
+
@property(nonatomic, assign) uint64_t windowId;
|
|
474
|
+
/// Set for tall-titlebar windows, whose delegate KVO-observes the
|
|
475
|
+
/// window's `contentLayoutRect` (chrome re-query timing) and must
|
|
476
|
+
/// unregister before the window closes.
|
|
477
|
+
@property(nonatomic, assign) BOOL observesContentLayout;
|
|
478
|
+
@end
|
|
479
|
+
|
|
480
|
+
@interface NativeSdkChromiumShortcut : NSObject
|
|
481
|
+
@property(nonatomic, strong) NSString *identifier;
|
|
482
|
+
@property(nonatomic, strong) NSString *key;
|
|
483
|
+
@property(nonatomic, assign) uint32_t modifiers;
|
|
484
|
+
@end
|
|
485
|
+
|
|
486
|
+
/* Captures the selected item id of a context-menu popUp (NSMenuItem
|
|
487
|
+
* targets are weak; the presenter block keeps this alive). */
|
|
488
|
+
@interface NativeSdkChromiumContextMenuTarget : NSObject
|
|
489
|
+
@property(nonatomic, assign) uint32_t selectedItemId;
|
|
490
|
+
- (void)contextMenuItemClicked:(NSMenuItem *)item;
|
|
491
|
+
@end
|
|
492
|
+
|
|
493
|
+
@implementation NativeSdkChromiumContextMenuTarget
|
|
494
|
+
|
|
495
|
+
- (void)contextMenuItemClicked:(NSMenuItem *)item {
|
|
496
|
+
NSNumber *value = item.representedObject;
|
|
497
|
+
if ([value isKindOfClass:[NSNumber class]]) self.selectedItemId = value.unsignedIntValue;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
@end
|
|
501
|
+
|
|
502
|
+
@interface NativeSdkChromiumHost : NSObject
|
|
503
|
+
@property(nonatomic, strong) NSWindow *window;
|
|
504
|
+
@property(nonatomic, strong) NSView *browserContainer;
|
|
505
|
+
@property(nonatomic, strong) NativeSdkChromiumWindowDelegate *delegate;
|
|
506
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSWindow *> *windows;
|
|
507
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSView *> *browserContainers;
|
|
508
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NativeSdkChromiumWindowDelegate *> *delegates;
|
|
509
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *bridgeOrigins;
|
|
510
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *internalURLPrefixes;
|
|
511
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *assetRoots;
|
|
512
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *assetEntries;
|
|
513
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *assetOrigins;
|
|
514
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *windowLabels;
|
|
515
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *fallbackURLs;
|
|
516
|
+
@property(nonatomic, strong) NSMutableDictionary<NSString *, NSView *> *webviewViews;
|
|
517
|
+
@property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *webviewPendingURLs;
|
|
518
|
+
@property(nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *webviewPendingZooms;
|
|
519
|
+
@property(nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *webviewGenerations;
|
|
520
|
+
@property(nonatomic, strong) NSMutableSet<NSString *> *closingWebViewKeys;
|
|
521
|
+
@property(nonatomic, assign) uint64_t nextWebViewGeneration;
|
|
522
|
+
@property(nonatomic, strong) NSTimer *timer;
|
|
523
|
+
@property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSTimer *> *appTimers;
|
|
524
|
+
@property(nonatomic, strong) NSString *appName;
|
|
525
|
+
/* The human-facing app name (app.zon display_name, empty = appName):
|
|
526
|
+
* drives the application menu title and its About/Hide/Quit labels, the
|
|
527
|
+
* process name, and the About panel. */
|
|
528
|
+
@property(nonatomic, strong) NSString *displayName;
|
|
529
|
+
/* app.zon version and description for the About panel; empty when
|
|
530
|
+
* undeclared. */
|
|
531
|
+
@property(nonatomic, strong) NSString *appVersion;
|
|
532
|
+
@property(nonatomic, strong) NSString *aboutDescription;
|
|
533
|
+
@property(nonatomic, assign) native_sdk_appkit_event_callback_t callback;
|
|
534
|
+
@property(nonatomic, assign) native_sdk_appkit_bridge_callback_t bridgeCallback;
|
|
535
|
+
@property(nonatomic, assign) void *context;
|
|
536
|
+
@property(nonatomic, assign) void *bridgeContext;
|
|
537
|
+
@property(nonatomic, assign) BOOL didShutdown;
|
|
538
|
+
@property(nonatomic, assign) BOOL observesApplicationActivation;
|
|
539
|
+
@property(nonatomic, strong) id shortcutEventMonitor;
|
|
540
|
+
@property(nonatomic, strong) NSArray<NativeSdkChromiumShortcut *> *shortcuts;
|
|
541
|
+
@property(nonatomic, strong) NSStatusItem *statusItem;
|
|
542
|
+
@property(nonatomic, assign) native_sdk_appkit_tray_callback_t trayCallback;
|
|
543
|
+
@property(nonatomic, assign) void *trayContext;
|
|
544
|
+
@property(nonatomic) CefRefPtr<NativeSdkCefClient> cefClient;
|
|
545
|
+
@property(nonatomic) CefRefPtr<CefBrowser> browser;
|
|
546
|
+
@property(nonatomic, assign) std::map<uint64_t, CefRefPtr<NativeSdkCefClient>> *cefClients;
|
|
547
|
+
@property(nonatomic, assign) std::map<uint64_t, CefRefPtr<CefBrowser>> *browsers;
|
|
548
|
+
@property(nonatomic, assign) std::map<std::string, CefRefPtr<NativeSdkCefClient>> *webviewCefClients;
|
|
549
|
+
@property(nonatomic, assign) std::map<std::string, CefRefPtr<CefBrowser>> *webviewBrowsers;
|
|
550
|
+
@property(nonatomic, strong) NSArray<NSString *> *allowedNavigationOrigins;
|
|
551
|
+
@property(nonatomic, strong) NSArray<NSString *> *allowedExternalURLs;
|
|
552
|
+
@property(nonatomic, assign) NSInteger externalLinkAction;
|
|
553
|
+
- (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription title:(NSString *)title width:(double)width height:(double)height;
|
|
554
|
+
- (void)configureApplication;
|
|
555
|
+
- (void)buildMenuBar;
|
|
556
|
+
- (NSMenuItem *)menuItem:(NSString *)title action:(SEL)action key:(NSString *)key modifiers:(NSEventModifierFlags)modifiers;
|
|
557
|
+
- (BOOL)createWindowWithId:(uint64_t)windowId title:(NSString *)title label:(NSString *)label x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame resizable:(BOOL)resizable makeMain:(BOOL)makeMain;
|
|
558
|
+
- (void)focusWindowWithId:(uint64_t)windowId;
|
|
559
|
+
- (void)closeWindowWithId:(uint64_t)windowId;
|
|
560
|
+
- (void)runWithCallback:(native_sdk_appkit_event_callback_t)callback context:(void *)context;
|
|
561
|
+
- (void)stop;
|
|
562
|
+
- (void)emitEvent:(native_sdk_appkit_event_t)event;
|
|
563
|
+
- (void)startAppTimerWithId:(uint64_t)timerId intervalNs:(uint64_t)intervalNs repeats:(BOOL)repeats;
|
|
564
|
+
- (void)cancelAppTimerWithId:(uint64_t)timerId;
|
|
565
|
+
- (void)invalidateAppTimers;
|
|
566
|
+
- (void)wakeFromAnyThread;
|
|
567
|
+
- (void)startApplicationActivationObservers;
|
|
568
|
+
- (void)stopApplicationActivationObservers;
|
|
569
|
+
- (void)applicationDidBecomeActive:(NSNotification *)notification;
|
|
570
|
+
- (void)applicationDidResignActive:(NSNotification *)notification;
|
|
571
|
+
- (void)emitResize;
|
|
572
|
+
- (void)emitResizeForWindowId:(uint64_t)windowId;
|
|
573
|
+
- (void)emitWindowFrameForWindowId:(uint64_t)windowId open:(BOOL)open;
|
|
574
|
+
- (void)emitFrame;
|
|
575
|
+
- (void)emitShutdown;
|
|
576
|
+
- (void)loadSource:(NSString *)source kind:(NSInteger)kind assetRoot:(NSString *)assetRoot entry:(NSString *)entry origin:(NSString *)origin spaFallback:(BOOL)spaFallback;
|
|
577
|
+
- (void)loadSource:(NSString *)source kind:(NSInteger)kind assetRoot:(NSString *)assetRoot entry:(NSString *)entry origin:(NSString *)origin spaFallback:(BOOL)spaFallback windowId:(uint64_t)windowId;
|
|
578
|
+
- (void)setAllowedNavigationOrigins:(NSArray<NSString *> *)origins externalURLs:(NSArray<NSString *> *)externalURLs externalAction:(NSInteger)externalAction;
|
|
579
|
+
- (BOOL)isInternalURL:(NSURL *)url;
|
|
580
|
+
- (BOOL)isInternalURL:(NSURL *)url windowId:(uint64_t)windowId;
|
|
581
|
+
- (BOOL)allowsNavigationURL:(NSURL *)url;
|
|
582
|
+
- (BOOL)openExternalURLIfAllowed:(NSURL *)url;
|
|
583
|
+
- (NSString *)resolvedWebViewURLString:(NSString *)url windowId:(uint64_t)windowId;
|
|
584
|
+
- (BOOL)createWebViewInWindow:(uint64_t)windowId label:(NSString *)label url:(NSString *)url x:(double)x y:(double)y width:(double)width height:(double)height layer:(NSInteger)layer transparent:(BOOL)transparent bridgeEnabled:(BOOL)bridgeEnabled;
|
|
585
|
+
- (BOOL)setWebViewFrameInWindow:(uint64_t)windowId label:(NSString *)label x:(double)x y:(double)y width:(double)width height:(double)height;
|
|
586
|
+
- (BOOL)navigateWebViewInWindow:(uint64_t)windowId label:(NSString *)label url:(NSString *)url;
|
|
587
|
+
- (BOOL)setWebViewZoomInWindow:(uint64_t)windowId label:(NSString *)label zoom:(double)zoom;
|
|
588
|
+
- (BOOL)setWebViewLayerInWindow:(uint64_t)windowId label:(NSString *)label layer:(NSInteger)layer;
|
|
589
|
+
- (BOOL)closeWebViewInWindow:(uint64_t)windowId label:(NSString *)label;
|
|
590
|
+
- (void)closeWebViewsInWindow:(uint64_t)windowId;
|
|
591
|
+
- (NSView *)stackViewForWindowId:(uint64_t)windowId;
|
|
592
|
+
- (void)reorderWebViewsInWindow:(uint64_t)windowId;
|
|
593
|
+
- (void)setBrowser:(CefRefPtr<CefBrowser>)browser windowId:(uint64_t)windowId;
|
|
594
|
+
- (BOOL)webViewGeneration:(uint64_t)generation matchesKey:(NSString *)key;
|
|
595
|
+
- (void)setWebViewBrowser:(CefRefPtr<CefBrowser>)browser key:(NSString *)key generation:(uint64_t)generation;
|
|
596
|
+
- (void)cleanupClosedWebViewWithKey:(NSString *)key;
|
|
597
|
+
- (void)cleanupClosedWebViewWithKey:(NSString *)key generation:(uint64_t)generation;
|
|
598
|
+
- (NSString *)fallbackURLForWindowId:(uint64_t)windowId;
|
|
599
|
+
- (NSString *)bridgeOriginForWindowId:(uint64_t)windowId webViewLabel:(NSString *)webViewLabel sourceURL:(NSString *)sourceURL;
|
|
600
|
+
- (void)receiveBridgePayload:(NSString *)payload origin:(NSString *)origin windowId:(uint64_t)windowId webViewLabel:(NSString *)webViewLabel;
|
|
601
|
+
- (void)completeBridgeWithResponse:(NSString *)response;
|
|
602
|
+
- (void)completeBridgeWithResponse:(NSString *)response windowId:(uint64_t)windowId;
|
|
603
|
+
- (void)completeBridgeWithResponse:(NSString *)response windowId:(uint64_t)windowId webViewLabel:(NSString *)webViewLabel;
|
|
604
|
+
- (void)emitEventNamed:(NSString *)name detailJSON:(NSString *)detailJSON windowId:(uint64_t)windowId;
|
|
605
|
+
- (void)setShortcutsWithIds:(const char *const *)ids idLengths:(const size_t *)idLengths keys:(const char *const *)keys keyLengths:(const size_t *)keyLengths modifiers:(const uint32_t *)modifiers count:(size_t)count;
|
|
606
|
+
- (BOOL)handleShortcutEvent:(NSEvent *)event;
|
|
607
|
+
- (void)emitShortcutWithId:(NSString *)identifier key:(NSString *)key modifiers:(uint32_t)modifiers event:(NSEvent *)event;
|
|
608
|
+
- (void)trayMenuItemClicked:(NSMenuItem *)menuItem;
|
|
609
|
+
@end
|
|
610
|
+
|
|
611
|
+
@implementation NativeSdkChromiumShortcut
|
|
612
|
+
@end
|
|
613
|
+
|
|
614
|
+
@implementation NativeSdkChromiumWindowDelegate
|
|
615
|
+
|
|
616
|
+
- (void)windowDidResize:(NSNotification *)notification {
|
|
617
|
+
(void)notification;
|
|
618
|
+
[self.host emitResizeForWindowId:self.windowId];
|
|
619
|
+
[self.host emitWindowFrameForWindowId:self.windowId open:YES];
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
- (void)windowDidMove:(NSNotification *)notification {
|
|
623
|
+
(void)notification;
|
|
624
|
+
[self.host emitWindowFrameForWindowId:self.windowId open:YES];
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
- (void)windowDidBecomeKey:(NSNotification *)notification {
|
|
628
|
+
(void)notification;
|
|
629
|
+
[self.host emitWindowFrameForWindowId:self.windowId open:YES];
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// Mirror of the AppKit host: the tall-titlebar toolbar is pure geometry,
|
|
633
|
+
// and fullscreen would otherwise keep it visible as a blank band over
|
|
634
|
+
// the app's own header. DID notifications, not WILL — the system
|
|
635
|
+
// snapshots and restores toolbar visibility across the transition and
|
|
636
|
+
// stomps changes made at the WILL edge; the re-emitted resize makes the
|
|
637
|
+
// runtime re-query chrome insets after the toggle.
|
|
638
|
+
// Same discipline as the AppKit host: the tall-titlebar toolbar is pure
|
|
639
|
+
// geometry, and fullscreen keeps it visible as a blank band over the
|
|
640
|
+
// app's own header — hide at the WILL edge so the transition's resizes
|
|
641
|
+
// see it, re-assert the restore at the DID edge (the system stomps a
|
|
642
|
+
// WILL-edge restore). The chrome re-query rides the contentLayoutRect
|
|
643
|
+
// KVO below, which fires only when the band has actually relaid out.
|
|
644
|
+
- (void)windowWillEnterFullScreen:(NSNotification *)notification {
|
|
645
|
+
(void)notification;
|
|
646
|
+
[self setToolbarVisible:NO];
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
- (void)windowDidEnterFullScreen:(NSNotification *)notification {
|
|
650
|
+
(void)notification;
|
|
651
|
+
[self setToolbarVisible:NO];
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
- (void)windowWillExitFullScreen:(NSNotification *)notification {
|
|
655
|
+
(void)notification;
|
|
656
|
+
[self setToolbarVisible:YES];
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
- (void)windowDidExitFullScreen:(NSNotification *)notification {
|
|
660
|
+
(void)notification;
|
|
661
|
+
[self setToolbarVisible:YES];
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
- (void)setToolbarVisible:(BOOL)visible {
|
|
665
|
+
NSWindow *window = self.host.windows[@(self.windowId)];
|
|
666
|
+
if (!window.toolbar) return;
|
|
667
|
+
window.toolbar.visible = visible;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
671
|
+
(void)object;
|
|
672
|
+
(void)change;
|
|
673
|
+
(void)context;
|
|
674
|
+
if (![keyPath isEqualToString:@"contentLayoutRect"]) return;
|
|
675
|
+
[self.host emitResizeForWindowId:self.windowId];
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
- (void)windowWillClose:(NSNotification *)notification {
|
|
679
|
+
(void)notification;
|
|
680
|
+
if (self.observesContentLayout) {
|
|
681
|
+
NSWindow *window = self.host.windows[@(self.windowId)];
|
|
682
|
+
[window removeObserver:self forKeyPath:@"contentLayoutRect"];
|
|
683
|
+
self.observesContentLayout = NO;
|
|
684
|
+
}
|
|
685
|
+
[self.host emitWindowFrameForWindowId:self.windowId open:NO];
|
|
686
|
+
[self.host closeWebViewsInWindow:self.windowId];
|
|
687
|
+
NSNumber *key = @(self.windowId);
|
|
688
|
+
[self.host.windows removeObjectForKey:key];
|
|
689
|
+
[self.host.browserContainers removeObjectForKey:key];
|
|
690
|
+
[self.host.delegates removeObjectForKey:key];
|
|
691
|
+
[self.host.bridgeOrigins removeObjectForKey:key];
|
|
692
|
+
[self.host.internalURLPrefixes removeObjectForKey:key];
|
|
693
|
+
[self.host.assetRoots removeObjectForKey:key];
|
|
694
|
+
[self.host.assetEntries removeObjectForKey:key];
|
|
695
|
+
[self.host.assetOrigins removeObjectForKey:key];
|
|
696
|
+
[self.host.windowLabels removeObjectForKey:key];
|
|
697
|
+
[self.host.fallbackURLs removeObjectForKey:key];
|
|
698
|
+
if (self.host.browsers) self.host.browsers->erase(self.windowId);
|
|
699
|
+
if (self.host.cefClients) self.host.cefClients->erase(self.windowId);
|
|
700
|
+
if (self.host.windows.count == 0) {
|
|
701
|
+
[self.host emitShutdown];
|
|
702
|
+
[self.host stop];
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
@end
|
|
707
|
+
|
|
708
|
+
@implementation NativeSdkChromiumHost
|
|
709
|
+
|
|
710
|
+
- (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription title:(NSString *)title width:(double)width height:(double)height {
|
|
711
|
+
self = [super init];
|
|
712
|
+
if (!self) return nil;
|
|
713
|
+
|
|
714
|
+
[NativeSdkChromiumApplication sharedApplication];
|
|
715
|
+
ensureCefInitialized();
|
|
716
|
+
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
|
|
717
|
+
self.appName = appName.length > 0 ? appName : @"native-sdk";
|
|
718
|
+
self.displayName = displayName.length > 0 ? displayName : self.appName;
|
|
719
|
+
self.appVersion = version ?: @"";
|
|
720
|
+
self.aboutDescription = aboutDescription ?: @"";
|
|
721
|
+
[self configureApplication];
|
|
722
|
+
self.windows = [[NSMutableDictionary alloc] init];
|
|
723
|
+
self.browserContainers = [[NSMutableDictionary alloc] init];
|
|
724
|
+
self.delegates = [[NSMutableDictionary alloc] init];
|
|
725
|
+
self.bridgeOrigins = [[NSMutableDictionary alloc] init];
|
|
726
|
+
self.internalURLPrefixes = [[NSMutableDictionary alloc] init];
|
|
727
|
+
self.assetRoots = [[NSMutableDictionary alloc] init];
|
|
728
|
+
self.assetEntries = [[NSMutableDictionary alloc] init];
|
|
729
|
+
self.assetOrigins = [[NSMutableDictionary alloc] init];
|
|
730
|
+
self.windowLabels = [[NSMutableDictionary alloc] init];
|
|
731
|
+
self.fallbackURLs = [[NSMutableDictionary alloc] init];
|
|
732
|
+
self.webviewViews = [[NSMutableDictionary alloc] init];
|
|
733
|
+
self.webviewPendingURLs = [[NSMutableDictionary alloc] init];
|
|
734
|
+
self.webviewPendingZooms = [[NSMutableDictionary alloc] init];
|
|
735
|
+
self.webviewGenerations = [[NSMutableDictionary alloc] init];
|
|
736
|
+
self.closingWebViewKeys = [[NSMutableSet alloc] init];
|
|
737
|
+
self.appTimers = [[NSMutableDictionary alloc] init];
|
|
738
|
+
self.nextWebViewGeneration = 1;
|
|
739
|
+
self.cefClients = new std::map<uint64_t, CefRefPtr<NativeSdkCefClient>>();
|
|
740
|
+
self.browsers = new std::map<uint64_t, CefRefPtr<CefBrowser>>();
|
|
741
|
+
self.webviewCefClients = new std::map<std::string, CefRefPtr<NativeSdkCefClient>>();
|
|
742
|
+
self.webviewBrowsers = new std::map<std::string, CefRefPtr<CefBrowser>>();
|
|
743
|
+
self.allowedNavigationOrigins = @[ @"zero://app", @"zero://inline" ];
|
|
744
|
+
self.allowedExternalURLs = @[];
|
|
745
|
+
self.externalLinkAction = 0;
|
|
746
|
+
self.shortcuts = @[];
|
|
747
|
+
|
|
748
|
+
[self createWindowWithId:1 title:(title.length > 0 ? title : self.appName) label:@"main" x:0 y:0 width:width height:height restoreFrame:NO resizable:YES makeMain:YES];
|
|
749
|
+
self.didShutdown = NO;
|
|
750
|
+
self.observesApplicationActivation = NO;
|
|
751
|
+
return self;
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
- (void)configureApplication {
|
|
755
|
+
[[NSProcessInfo processInfo] setProcessName:self.displayName];
|
|
756
|
+
[self buildMenuBar];
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
- (void)buildMenuBar {
|
|
760
|
+
NSMenu *mainMenu = [[NSMenu alloc] initWithTitle:@""];
|
|
761
|
+
[NSApp setMainMenu:mainMenu];
|
|
762
|
+
|
|
763
|
+
// Every string the application menu derives \u2014 the bold menu-bar
|
|
764
|
+
// title and the About/Hide/Quit labels \u2014 reads from the one display
|
|
765
|
+
// name, never the binary name. No Settings item: the host has no
|
|
766
|
+
// settings surface to open, and a dead item is worse than none.
|
|
767
|
+
NSMenuItem *appMenuItem = [[NSMenuItem alloc] initWithTitle:self.displayName action:nil keyEquivalent:@""];
|
|
768
|
+
[mainMenu addItem:appMenuItem];
|
|
769
|
+
NSMenu *appMenu = [[NSMenu alloc] initWithTitle:self.displayName];
|
|
770
|
+
[appMenuItem setSubmenu:appMenu];
|
|
771
|
+
[appMenu addItem:[self menuItem:[NSString stringWithFormat:@"About %@", self.displayName] action:@selector(showAboutPanel:) key:@"" modifiers:0]];
|
|
772
|
+
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
773
|
+
[appMenu addItem:[self menuItem:[NSString stringWithFormat:@"Hide %@", self.displayName] action:@selector(hide:) key:@"h" modifiers:NSEventModifierFlagCommand]];
|
|
774
|
+
[appMenu addItem:[self menuItem:@"Hide Others" action:@selector(hideOtherApplications:) key:@"h" modifiers:(NSEventModifierFlagCommand | NSEventModifierFlagOption)]];
|
|
775
|
+
[appMenu addItem:[self menuItem:@"Show All" action:@selector(unhideAllApplications:) key:@"" modifiers:0]];
|
|
776
|
+
[appMenu addItem:[NSMenuItem separatorItem]];
|
|
777
|
+
[appMenu addItem:[self menuItem:[NSString stringWithFormat:@"Quit %@", self.displayName] action:@selector(terminate:) key:@"q" modifiers:NSEventModifierFlagCommand]];
|
|
778
|
+
|
|
779
|
+
NSMenuItem *fileMenuItem = [[NSMenuItem alloc] initWithTitle:@"File" action:nil keyEquivalent:@""];
|
|
780
|
+
[mainMenu addItem:fileMenuItem];
|
|
781
|
+
NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
|
|
782
|
+
[fileMenuItem setSubmenu:fileMenu];
|
|
783
|
+
[fileMenu addItem:[self menuItem:@"Close Window" action:@selector(performClose:) key:@"w" modifiers:NSEventModifierFlagCommand]];
|
|
784
|
+
|
|
785
|
+
NSMenuItem *editMenuItem = [[NSMenuItem alloc] initWithTitle:@"Edit" action:nil keyEquivalent:@""];
|
|
786
|
+
[mainMenu addItem:editMenuItem];
|
|
787
|
+
NSMenu *editMenu = [[NSMenu alloc] initWithTitle:@"Edit"];
|
|
788
|
+
[editMenuItem setSubmenu:editMenu];
|
|
789
|
+
[editMenu addItem:[self menuItem:@"Undo" action:@selector(undo:) key:@"z" modifiers:NSEventModifierFlagCommand]];
|
|
790
|
+
[editMenu addItem:[self menuItem:@"Redo" action:@selector(redo:) key:@"Z" modifiers:NSEventModifierFlagCommand]];
|
|
791
|
+
[editMenu addItem:[NSMenuItem separatorItem]];
|
|
792
|
+
[editMenu addItem:[self menuItem:@"Cut" action:@selector(cut:) key:@"x" modifiers:NSEventModifierFlagCommand]];
|
|
793
|
+
[editMenu addItem:[self menuItem:@"Copy" action:@selector(copy:) key:@"c" modifiers:NSEventModifierFlagCommand]];
|
|
794
|
+
[editMenu addItem:[self menuItem:@"Paste" action:@selector(paste:) key:@"v" modifiers:NSEventModifierFlagCommand]];
|
|
795
|
+
[editMenu addItem:[self menuItem:@"Select All" action:@selector(selectAll:) key:@"a" modifiers:NSEventModifierFlagCommand]];
|
|
796
|
+
|
|
797
|
+
NSMenuItem *viewMenuItem = [[NSMenuItem alloc] initWithTitle:@"View" action:nil keyEquivalent:@""];
|
|
798
|
+
[mainMenu addItem:viewMenuItem];
|
|
799
|
+
NSMenu *viewMenu = [[NSMenu alloc] initWithTitle:@"View"];
|
|
800
|
+
[viewMenuItem setSubmenu:viewMenu];
|
|
801
|
+
[viewMenu addItem:[self menuItem:@"Reload" action:@selector(reload:) key:@"r" modifiers:NSEventModifierFlagCommand]];
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
- (NSMenuItem *)menuItem:(NSString *)title action:(SEL)action key:(NSString *)key modifiers:(NSEventModifierFlags)modifiers {
|
|
805
|
+
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:action keyEquivalent:key ?: @""];
|
|
806
|
+
item.keyEquivalentModifierMask = modifiers;
|
|
807
|
+
if ([self respondsToSelector:action]) {
|
|
808
|
+
item.target = self;
|
|
809
|
+
}
|
|
810
|
+
return item;
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/* The standard About panel, populated explicitly so unbundled dev runs
|
|
814
|
+
* show the manifest identity a packaged bundle reads from Info.plist. */
|
|
815
|
+
- (void)showAboutPanel:(id)sender {
|
|
816
|
+
(void)sender;
|
|
817
|
+
NSMutableDictionary<NSAboutPanelOptionKey, id> *options = [[NSMutableDictionary alloc] init];
|
|
818
|
+
options[NSAboutPanelOptionApplicationName] = self.displayName;
|
|
819
|
+
if (self.appVersion.length > 0) {
|
|
820
|
+
options[NSAboutPanelOptionApplicationVersion] = self.appVersion;
|
|
821
|
+
options[NSAboutPanelOptionVersion] = @"";
|
|
822
|
+
}
|
|
823
|
+
if (self.aboutDescription.length > 0) {
|
|
824
|
+
NSDictionary<NSAttributedStringKey, id> *creditAttributes = @{
|
|
825
|
+
NSFontAttributeName : [NSFont systemFontOfSize:11],
|
|
826
|
+
NSForegroundColorAttributeName : NSColor.secondaryLabelColor,
|
|
827
|
+
};
|
|
828
|
+
options[NSAboutPanelOptionCredits] = [[NSAttributedString alloc] initWithString:self.aboutDescription attributes:creditAttributes];
|
|
829
|
+
}
|
|
830
|
+
[NSApp orderFrontStandardAboutPanelWithOptions:options];
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
- (void)reload:(id)sender {
|
|
834
|
+
(void)sender;
|
|
835
|
+
NSWindow *keyWindow = NSApp.keyWindow;
|
|
836
|
+
uint64_t windowId = 1;
|
|
837
|
+
for (NSNumber *key in self.windows) {
|
|
838
|
+
if ([self.windows[key] isEqual:keyWindow]) {
|
|
839
|
+
windowId = key.unsignedLongLongValue;
|
|
840
|
+
break;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
if (self.browsers) {
|
|
844
|
+
auto it = self.browsers->find(windowId);
|
|
845
|
+
if (it != self.browsers->end() && it->second) {
|
|
846
|
+
it->second->ReloadIgnoreCache();
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
- (void)dealloc {
|
|
852
|
+
if (self.shortcutEventMonitor) {
|
|
853
|
+
[NSEvent removeMonitor:self.shortcutEventMonitor];
|
|
854
|
+
self.shortcutEventMonitor = nil;
|
|
855
|
+
}
|
|
856
|
+
delete self.webviewCefClients;
|
|
857
|
+
delete self.webviewBrowsers;
|
|
858
|
+
delete self.cefClients;
|
|
859
|
+
delete self.browsers;
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
- (BOOL)createWindowWithId:(uint64_t)windowId title:(NSString *)title label:(NSString *)label x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame resizable:(BOOL)resizable makeMain:(BOOL)makeMain {
|
|
863
|
+
NSNumber *key = @(windowId);
|
|
864
|
+
if (self.windows[key]) return NO;
|
|
865
|
+
|
|
866
|
+
NSRect rect = restoreFrame ? NativeSdkConstrainFrame(NSMakeRect(x, y, width, height)) : NSMakeRect(0, 0, width, height);
|
|
867
|
+
NSWindowStyleMask styleMask = NSWindowStyleMaskTitled |
|
|
868
|
+
NSWindowStyleMaskClosable |
|
|
869
|
+
NSWindowStyleMaskMiniaturizable;
|
|
870
|
+
if (resizable) {
|
|
871
|
+
styleMask |= NSWindowStyleMaskResizable;
|
|
872
|
+
}
|
|
873
|
+
NSWindow *window = [[NSWindow alloc] initWithContentRect:rect
|
|
874
|
+
styleMask:styleMask
|
|
875
|
+
backing:NSBackingStoreBuffered
|
|
876
|
+
defer:NO];
|
|
877
|
+
[window setTitle:title.length > 0 ? title : @"native-sdk"];
|
|
878
|
+
if (!restoreFrame) [window center];
|
|
879
|
+
|
|
880
|
+
NSView *stackRoot = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, width, height)];
|
|
881
|
+
stackRoot.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
882
|
+
window.contentView = stackRoot;
|
|
883
|
+
|
|
884
|
+
NSView *browserContainer = [[NSView alloc] initWithFrame:stackRoot.bounds];
|
|
885
|
+
browserContainer.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
|
|
886
|
+
browserContainer.wantsLayer = YES;
|
|
887
|
+
browserContainer.layer.zPosition = 0;
|
|
888
|
+
[stackRoot addSubview:browserContainer positioned:NSWindowAbove relativeTo:nil];
|
|
889
|
+
|
|
890
|
+
NativeSdkChromiumWindowDelegate *delegate = [[NativeSdkChromiumWindowDelegate alloc] init];
|
|
891
|
+
delegate.host = self;
|
|
892
|
+
delegate.windowId = windowId;
|
|
893
|
+
window.delegate = delegate;
|
|
894
|
+
CefRefPtr<NativeSdkCefClient> client = new NativeSdkCefClient(self, windowId);
|
|
895
|
+
|
|
896
|
+
self.windows[key] = window;
|
|
897
|
+
self.browserContainers[key] = browserContainer;
|
|
898
|
+
self.delegates[key] = delegate;
|
|
899
|
+
self.windowLabels[key] = label.length > 0 ? label : (makeMain ? @"main" : @"");
|
|
900
|
+
(*self.cefClients)[windowId] = client;
|
|
901
|
+
if (makeMain) {
|
|
902
|
+
self.window = window;
|
|
903
|
+
self.browserContainer = browserContainer;
|
|
904
|
+
self.delegate = delegate;
|
|
905
|
+
self.cefClient = client;
|
|
906
|
+
} else {
|
|
907
|
+
[window makeKeyAndOrderFront:nil];
|
|
908
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
909
|
+
}
|
|
910
|
+
return YES;
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
- (void)focusWindowWithId:(uint64_t)windowId {
|
|
914
|
+
NSWindow *window = self.windows[@(windowId)];
|
|
915
|
+
if (!window) return;
|
|
916
|
+
[window makeKeyAndOrderFront:nil];
|
|
917
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
918
|
+
[self emitWindowFrameForWindowId:windowId open:YES];
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
- (void)closeWindowWithId:(uint64_t)windowId {
|
|
922
|
+
void (^closeBlock)(void) = ^{
|
|
923
|
+
NSWindow *window = self.windows[@(windowId)];
|
|
924
|
+
if (!window) {
|
|
925
|
+
return;
|
|
926
|
+
}
|
|
927
|
+
[self closeWebViewsInWindow:windowId];
|
|
928
|
+
if (self.browsers) {
|
|
929
|
+
auto it = self.browsers->find(windowId);
|
|
930
|
+
if (it != self.browsers->end() && it->second) {
|
|
931
|
+
[window orderOut:nil];
|
|
932
|
+
[self emitWindowFrameForWindowId:windowId open:NO];
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
}
|
|
936
|
+
[window close];
|
|
937
|
+
};
|
|
938
|
+
if ([NSThread isMainThread]) {
|
|
939
|
+
closeBlock();
|
|
940
|
+
} else {
|
|
941
|
+
dispatch_sync(dispatch_get_main_queue(), closeBlock);
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
- (void)runWithCallback:(native_sdk_appkit_event_callback_t)callback context:(void *)context {
|
|
946
|
+
self.callback = callback;
|
|
947
|
+
self.context = context;
|
|
948
|
+
|
|
949
|
+
[self.window makeKeyAndOrderFront:nil];
|
|
950
|
+
[NSApp activateIgnoringOtherApps:YES];
|
|
951
|
+
if (!self.shortcutEventMonitor) {
|
|
952
|
+
__weak NativeSdkChromiumHost *weakSelf = self;
|
|
953
|
+
self.shortcutEventMonitor = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent *(NSEvent *event) {
|
|
954
|
+
NativeSdkChromiumHost *strongSelf = weakSelf;
|
|
955
|
+
if (strongSelf && [strongSelf handleShortcutEvent:event]) return nil;
|
|
956
|
+
return event;
|
|
957
|
+
}];
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
[self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_START }];
|
|
961
|
+
// A failed START handler requests shutdown synchronously, before the
|
|
962
|
+
// run loop exists — [NSApp stop:] is a no-op there. Honor the request
|
|
963
|
+
// here instead of stranding a live app behind a blank window.
|
|
964
|
+
if (self.didShutdown) {
|
|
965
|
+
shutdownCefIfNeeded();
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
[self emitResize];
|
|
969
|
+
[self emitWindowFrameForWindowId:1 open:YES];
|
|
970
|
+
[self startApplicationActivationObservers];
|
|
971
|
+
self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0)
|
|
972
|
+
target:self
|
|
973
|
+
selector:@selector(emitFrame)
|
|
974
|
+
userInfo:nil
|
|
975
|
+
repeats:YES];
|
|
976
|
+
[NSApp run];
|
|
977
|
+
shutdownCefIfNeeded();
|
|
978
|
+
}
|
|
979
|
+
|
|
980
|
+
- (void)stop {
|
|
981
|
+
[self.timer invalidate];
|
|
982
|
+
self.timer = nil;
|
|
983
|
+
[self invalidateAppTimers];
|
|
984
|
+
if (self.shortcutEventMonitor) {
|
|
985
|
+
[NSEvent removeMonitor:self.shortcutEventMonitor];
|
|
986
|
+
self.shortcutEventMonitor = nil;
|
|
987
|
+
}
|
|
988
|
+
[self stopApplicationActivationObservers];
|
|
989
|
+
if (self.browsers) {
|
|
990
|
+
for (auto &entry : *self.browsers) {
|
|
991
|
+
if (entry.second) entry.second->GetHost()->CloseBrowser(true);
|
|
992
|
+
}
|
|
993
|
+
} else if (self.browser) {
|
|
994
|
+
self.browser->GetHost()->CloseBrowser(true);
|
|
995
|
+
}
|
|
996
|
+
[NSApp stop:nil];
|
|
997
|
+
NSEvent *event = [NSEvent otherEventWithType:NSEventTypeApplicationDefined
|
|
998
|
+
location:NSZeroPoint
|
|
999
|
+
modifierFlags:0
|
|
1000
|
+
timestamp:0
|
|
1001
|
+
windowNumber:0
|
|
1002
|
+
context:nil
|
|
1003
|
+
subtype:0
|
|
1004
|
+
data1:0
|
|
1005
|
+
data2:0];
|
|
1006
|
+
[NSApp postEvent:event atStart:NO];
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
- (void)emitEvent:(native_sdk_appkit_event_t)event {
|
|
1010
|
+
if (self.callback) self.callback(self.context, &event);
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
- (void)startApplicationActivationObservers {
|
|
1014
|
+
if (self.observesApplicationActivation) return;
|
|
1015
|
+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
1016
|
+
[center addObserver:self selector:@selector(applicationDidBecomeActive:) name:NSApplicationDidBecomeActiveNotification object:NSApp];
|
|
1017
|
+
[center addObserver:self selector:@selector(applicationDidResignActive:) name:NSApplicationDidResignActiveNotification object:NSApp];
|
|
1018
|
+
self.observesApplicationActivation = YES;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
- (void)stopApplicationActivationObservers {
|
|
1022
|
+
if (!self.observesApplicationActivation) return;
|
|
1023
|
+
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
|
1024
|
+
[center removeObserver:self name:NSApplicationDidBecomeActiveNotification object:NSApp];
|
|
1025
|
+
[center removeObserver:self name:NSApplicationDidResignActiveNotification object:NSApp];
|
|
1026
|
+
self.observesApplicationActivation = NO;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
- (void)applicationDidBecomeActive:(NSNotification *)notification {
|
|
1030
|
+
(void)notification;
|
|
1031
|
+
[self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_APP_ACTIVATED }];
|
|
1032
|
+
}
|
|
1033
|
+
|
|
1034
|
+
- (void)applicationDidResignActive:(NSNotification *)notification {
|
|
1035
|
+
(void)notification;
|
|
1036
|
+
[self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_APP_DEACTIVATED }];
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
- (void)emitResize {
|
|
1040
|
+
[self emitResizeForWindowId:1];
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
- (void)emitResizeForWindowId:(uint64_t)windowId {
|
|
1044
|
+
NSWindow *window = self.windows[@(windowId)] ?: self.window;
|
|
1045
|
+
CefRefPtr<CefBrowser> browser;
|
|
1046
|
+
if (self.browsers) {
|
|
1047
|
+
auto it = self.browsers->find(windowId);
|
|
1048
|
+
if (it != self.browsers->end()) browser = it->second;
|
|
1049
|
+
}
|
|
1050
|
+
NSRect bounds = window.contentView.bounds;
|
|
1051
|
+
if (browser) browser->GetHost()->WasResized();
|
|
1052
|
+
[self emitEvent:(native_sdk_appkit_event_t){
|
|
1053
|
+
.kind = NATIVE_SDK_APPKIT_EVENT_RESIZE,
|
|
1054
|
+
.window_id = windowId,
|
|
1055
|
+
.width = bounds.size.width,
|
|
1056
|
+
.height = bounds.size.height,
|
|
1057
|
+
.scale = window.backingScaleFactor,
|
|
1058
|
+
}];
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
- (void)emitWindowFrameForWindowId:(uint64_t)windowId open:(BOOL)open {
|
|
1062
|
+
NSWindow *window = self.windows[@(windowId)] ?: self.window;
|
|
1063
|
+
NSString *label = self.windowLabels[@(windowId)] ?: @"";
|
|
1064
|
+
NSRect frame = window.frame;
|
|
1065
|
+
[self emitEvent:(native_sdk_appkit_event_t){
|
|
1066
|
+
.kind = NATIVE_SDK_APPKIT_EVENT_WINDOW_FRAME,
|
|
1067
|
+
.window_id = windowId,
|
|
1068
|
+
.width = frame.size.width,
|
|
1069
|
+
.height = frame.size.height,
|
|
1070
|
+
.scale = window.backingScaleFactor,
|
|
1071
|
+
.x = frame.origin.x,
|
|
1072
|
+
.y = frame.origin.y,
|
|
1073
|
+
.open = open ? 1 : 0,
|
|
1074
|
+
.focused = window.isKeyWindow ? 1 : 0,
|
|
1075
|
+
.label = label.UTF8String,
|
|
1076
|
+
.label_len = [label lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
1077
|
+
}];
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
- (void)emitFrame {
|
|
1081
|
+
CefDoMessageLoopWork();
|
|
1082
|
+
[self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_FRAME }];
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
- (void)emitShutdown {
|
|
1086
|
+
if (self.didShutdown) return;
|
|
1087
|
+
self.didShutdown = YES;
|
|
1088
|
+
[self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_SHUTDOWN }];
|
|
1089
|
+
}
|
|
1090
|
+
|
|
1091
|
+
/* App timers and cross-thread wake, mirrored from the AppKit host: the
|
|
1092
|
+
* runtime's scheduler and effect queue rely on TIMER/WAKE events on both
|
|
1093
|
+
* engines. */
|
|
1094
|
+
- (void)startAppTimerWithId:(uint64_t)timerId intervalNs:(uint64_t)intervalNs repeats:(BOOL)repeats {
|
|
1095
|
+
NSNumber *key = @(timerId);
|
|
1096
|
+
[self.appTimers[key] invalidate];
|
|
1097
|
+
NSTimeInterval interval = (NSTimeInterval)intervalNs / 1000000000.0;
|
|
1098
|
+
// Common modes: a timer must keep firing while the user holds a menu
|
|
1099
|
+
// open or live-resizes the window (same discipline as the AppKit host).
|
|
1100
|
+
NSTimer *app_timer = [NSTimer timerWithTimeInterval:interval
|
|
1101
|
+
target:self
|
|
1102
|
+
selector:@selector(appTimerFired:)
|
|
1103
|
+
userInfo:@{ @"id": key, @"repeats": @(repeats) }
|
|
1104
|
+
repeats:repeats];
|
|
1105
|
+
[[NSRunLoop mainRunLoop] addTimer:app_timer forMode:NSRunLoopCommonModes];
|
|
1106
|
+
self.appTimers[key] = app_timer;
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
- (void)cancelAppTimerWithId:(uint64_t)timerId {
|
|
1110
|
+
NSNumber *key = @(timerId);
|
|
1111
|
+
[self.appTimers[key] invalidate];
|
|
1112
|
+
[self.appTimers removeObjectForKey:key];
|
|
1113
|
+
}
|
|
1114
|
+
|
|
1115
|
+
- (void)appTimerFired:(NSTimer *)timer {
|
|
1116
|
+
NSDictionary *info = (NSDictionary *)timer.userInfo;
|
|
1117
|
+
NSNumber *key = info[@"id"];
|
|
1118
|
+
if (!key) return;
|
|
1119
|
+
// A non-repeating timer invalidates itself after this fire; drop the
|
|
1120
|
+
// bookkeeping entry before the callback so it may start a replacement
|
|
1121
|
+
// timer with the same id.
|
|
1122
|
+
if (![info[@"repeats"] boolValue] && self.appTimers[key] == timer) {
|
|
1123
|
+
[self.appTimers removeObjectForKey:key];
|
|
1124
|
+
}
|
|
1125
|
+
[self emitEvent:(native_sdk_appkit_event_t){
|
|
1126
|
+
.kind = NATIVE_SDK_APPKIT_EVENT_TIMER,
|
|
1127
|
+
.timer_id = key.unsignedLongLongValue,
|
|
1128
|
+
.timestamp_ns = NativeSdkChromiumTimestampNanoseconds(),
|
|
1129
|
+
}];
|
|
1130
|
+
}
|
|
1131
|
+
|
|
1132
|
+
- (void)invalidateAppTimers {
|
|
1133
|
+
for (NSTimer *timer in self.appTimers.allValues) {
|
|
1134
|
+
[timer invalidate];
|
|
1135
|
+
}
|
|
1136
|
+
[self.appTimers removeAllObjects];
|
|
1137
|
+
}
|
|
1138
|
+
|
|
1139
|
+
/* Called from any thread: marshal onto the main queue and emit the WAKE
|
|
1140
|
+
* event there, so the runtime's effect-queue drain always runs on the
|
|
1141
|
+
* loop thread. */
|
|
1142
|
+
- (void)wakeFromAnyThread {
|
|
1143
|
+
__weak NativeSdkChromiumHost *weakSelf = self;
|
|
1144
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
1145
|
+
NativeSdkChromiumHost *strongSelf = weakSelf;
|
|
1146
|
+
if (!strongSelf || strongSelf.didShutdown) return;
|
|
1147
|
+
[strongSelf emitEvent:(native_sdk_appkit_event_t){
|
|
1148
|
+
.kind = NATIVE_SDK_APPKIT_EVENT_WAKE,
|
|
1149
|
+
.timestamp_ns = NativeSdkChromiumTimestampNanoseconds(),
|
|
1150
|
+
}];
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
|
|
1154
|
+
- (void)loadSource:(NSString *)source kind:(NSInteger)kind assetRoot:(NSString *)assetRoot entry:(NSString *)entry origin:(NSString *)origin spaFallback:(BOOL)spaFallback {
|
|
1155
|
+
[self loadSource:source kind:kind assetRoot:assetRoot entry:entry origin:origin spaFallback:spaFallback windowId:1];
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
- (void)loadSource:(NSString *)source kind:(NSInteger)kind assetRoot:(NSString *)assetRoot entry:(NSString *)entry origin:(NSString *)origin spaFallback:(BOOL)spaFallback windowId:(uint64_t)windowId {
|
|
1159
|
+
NSString *urlString = source;
|
|
1160
|
+
NSString *bridgeOrigin = nil;
|
|
1161
|
+
NSString *internalURLPrefix = nil;
|
|
1162
|
+
NSString *assetEntryPath = nil;
|
|
1163
|
+
if (kind == 0) {
|
|
1164
|
+
urlString = temporaryHtmlUrl(source);
|
|
1165
|
+
bridgeOrigin = @"zero://inline";
|
|
1166
|
+
internalURLPrefix = urlString;
|
|
1167
|
+
} else if (kind == 2) {
|
|
1168
|
+
NSString *resolvedRoot = NativeSdkResolvedAssetRoot(assetRoot ?: @"");
|
|
1169
|
+
NSString *assetEntry = entry.length > 0 ? entry : @"index.html";
|
|
1170
|
+
while ([assetEntry hasPrefix:@"/"]) {
|
|
1171
|
+
assetEntry = [assetEntry substringFromIndex:1];
|
|
1172
|
+
}
|
|
1173
|
+
if (assetEntry.length == 0) assetEntry = @"index.html";
|
|
1174
|
+
assetEntryPath = assetEntry;
|
|
1175
|
+
urlString = [NSURL fileURLWithPath:[resolvedRoot stringByAppendingPathComponent:assetEntry]].absoluteString;
|
|
1176
|
+
bridgeOrigin = origin.length > 0 ? origin : @"zero://app";
|
|
1177
|
+
internalURLPrefix = [NSURL fileURLWithPath:resolvedRoot isDirectory:YES].absoluteString;
|
|
1178
|
+
}
|
|
1179
|
+
NSNumber *key = @(windowId);
|
|
1180
|
+
if (bridgeOrigin) {
|
|
1181
|
+
self.bridgeOrigins[key] = bridgeOrigin;
|
|
1182
|
+
} else {
|
|
1183
|
+
[self.bridgeOrigins removeObjectForKey:key];
|
|
1184
|
+
}
|
|
1185
|
+
if (internalURLPrefix) {
|
|
1186
|
+
self.internalURLPrefixes[key] = internalURLPrefix;
|
|
1187
|
+
} else {
|
|
1188
|
+
[self.internalURLPrefixes removeObjectForKey:key];
|
|
1189
|
+
}
|
|
1190
|
+
if (kind == 2) {
|
|
1191
|
+
self.assetRoots[key] = NativeSdkResolvedAssetRoot(assetRoot ?: @"");
|
|
1192
|
+
self.assetEntries[key] = assetEntryPath.length > 0 ? assetEntryPath : @"index.html";
|
|
1193
|
+
self.assetOrigins[key] = bridgeOrigin.length > 0 ? bridgeOrigin : @"zero://app";
|
|
1194
|
+
} else {
|
|
1195
|
+
[self.assetRoots removeObjectForKey:key];
|
|
1196
|
+
[self.assetEntries removeObjectForKey:key];
|
|
1197
|
+
[self.assetOrigins removeObjectForKey:key];
|
|
1198
|
+
}
|
|
1199
|
+
if (kind == 2 && spaFallback) {
|
|
1200
|
+
self.fallbackURLs[key] = urlString;
|
|
1201
|
+
} else {
|
|
1202
|
+
[self.fallbackURLs removeObjectForKey:key];
|
|
1203
|
+
}
|
|
1204
|
+
NSView *container = self.browserContainers[@(windowId)] ?: self.browserContainer;
|
|
1205
|
+
CefRefPtr<CefBrowser> browser;
|
|
1206
|
+
if (self.browsers) {
|
|
1207
|
+
auto browser_it = self.browsers->find(windowId);
|
|
1208
|
+
if (browser_it != self.browsers->end()) browser = browser_it->second;
|
|
1209
|
+
}
|
|
1210
|
+
if (browser) {
|
|
1211
|
+
browser->GetMainFrame()->LoadURL(std::string(urlString.UTF8String));
|
|
1212
|
+
return;
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1215
|
+
CefWindowInfo windowInfo;
|
|
1216
|
+
CefRect rect(0, 0, container.bounds.size.width, container.bounds.size.height);
|
|
1217
|
+
windowInfo.SetAsChild((__bridge void *)container, rect);
|
|
1218
|
+
CefBrowserSettings browserSettings;
|
|
1219
|
+
CefRefPtr<NativeSdkCefClient> client = (*self.cefClients)[windowId];
|
|
1220
|
+
CefBrowserHost::CreateBrowser(windowInfo, client.get(), std::string(urlString.UTF8String), browserSettings, NativeSdkCefExtraInfo(true), nullptr);
|
|
1221
|
+
}
|
|
1222
|
+
|
|
1223
|
+
- (NSString *)webViewKeyForWindow:(uint64_t)windowId label:(NSString *)label {
|
|
1224
|
+
return [NSString stringWithFormat:@"%llu:%@", windowId, label ?: @""];
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
- (NSView *)stackViewForWindowId:(uint64_t)windowId {
|
|
1228
|
+
NSWindow *window = self.windows[@(windowId)] ?: (windowId == 1 ? self.window : nil);
|
|
1229
|
+
return window.contentView;
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
- (NSRect)webViewFrameForContainer:(NSView *)container x:(double)x y:(double)y width:(double)width height:(double)height {
|
|
1233
|
+
CGFloat nativeY = container.isFlipped ? y : container.bounds.size.height - y - height;
|
|
1234
|
+
return NSMakeRect(x, nativeY, width, height);
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
- (BOOL)createWebViewInWindow:(uint64_t)windowId label:(NSString *)label url:(NSString *)url x:(double)x y:(double)y width:(double)width height:(double)height layer:(NSInteger)layer transparent:(BOOL)transparent bridgeEnabled:(BOOL)bridgeEnabled {
|
|
1238
|
+
if (label.length == 0 || url.length == 0 || width <= 0 || height <= 0 || x < 0 || y < 0) return NO;
|
|
1239
|
+
NSView *container = self.browserContainers[@(windowId)] ?: (windowId == 1 ? self.browserContainer : nil);
|
|
1240
|
+
NSView *stackView = [self stackViewForWindowId:windowId];
|
|
1241
|
+
if (!container || !stackView) return NO;
|
|
1242
|
+
NSURL *targetURL = [NSURL URLWithString:url];
|
|
1243
|
+
if (!targetURL || ![self allowsNavigationURL:targetURL]) return NO;
|
|
1244
|
+
NSString *resolvedURL = [self resolvedWebViewURLString:url windowId:windowId];
|
|
1245
|
+
if (resolvedURL.length == 0) return NO;
|
|
1246
|
+
NSString *key = [self webViewKeyForWindow:windowId label:label];
|
|
1247
|
+
if (self.webviewViews[key]) return NO;
|
|
1248
|
+
|
|
1249
|
+
NSView *webview = [[NSView alloc] initWithFrame:[self webViewFrameForContainer:stackView x:x y:y width:width height:height]];
|
|
1250
|
+
webview.frame = [self webViewFrameForContainer:stackView x:x y:y width:width height:height];
|
|
1251
|
+
webview.autoresizingMask = NSViewNotSizable;
|
|
1252
|
+
webview.wantsLayer = YES;
|
|
1253
|
+
webview.layer.zPosition = layer;
|
|
1254
|
+
if (transparent) webview.layer.backgroundColor = NSColor.clearColor.CGColor;
|
|
1255
|
+
[stackView addSubview:webview positioned:NSWindowAbove relativeTo:nil];
|
|
1256
|
+
self.webviewViews[key] = webview;
|
|
1257
|
+
uint64_t generation = self.nextWebViewGeneration++;
|
|
1258
|
+
if (self.nextWebViewGeneration == 0) self.nextWebViewGeneration = 1;
|
|
1259
|
+
self.webviewGenerations[key] = @(generation);
|
|
1260
|
+
[self reorderWebViewsInWindow:windowId];
|
|
1261
|
+
|
|
1262
|
+
std::string keyString(key.UTF8String);
|
|
1263
|
+
CefRefPtr<NativeSdkCefClient> client = new NativeSdkCefClient(self, windowId, keyString, generation, bridgeEnabled);
|
|
1264
|
+
if (self.webviewCefClients) (*self.webviewCefClients)[keyString] = client;
|
|
1265
|
+
CefWindowInfo windowInfo;
|
|
1266
|
+
CefRect rect(0, 0, webview.bounds.size.width, webview.bounds.size.height);
|
|
1267
|
+
windowInfo.SetAsChild((__bridge void *)webview, rect);
|
|
1268
|
+
CefBrowserSettings browserSettings;
|
|
1269
|
+
CefBrowserHost::CreateBrowser(windowInfo, client.get(), std::string(resolvedURL.UTF8String), browserSettings, NativeSdkCefExtraInfo(bridgeEnabled), nullptr);
|
|
1270
|
+
return YES;
|
|
1271
|
+
}
|
|
1272
|
+
|
|
1273
|
+
- (BOOL)setWebViewFrameInWindow:(uint64_t)windowId label:(NSString *)label x:(double)x y:(double)y width:(double)width height:(double)height {
|
|
1274
|
+
if (label.length == 0 || width <= 0 || height <= 0 || x < 0 || y < 0) return NO;
|
|
1275
|
+
NSView *container = self.browserContainers[@(windowId)] ?: (windowId == 1 ? self.browserContainer : nil);
|
|
1276
|
+
if ([label isEqualToString:@"main"]) {
|
|
1277
|
+
if (!container) return NO;
|
|
1278
|
+
NSView *parent = [self stackViewForWindowId:windowId];
|
|
1279
|
+
if (!parent) return NO;
|
|
1280
|
+
container.autoresizingMask = NSViewNotSizable;
|
|
1281
|
+
container.frame = [self webViewFrameForContainer:parent x:x y:y width:width height:height];
|
|
1282
|
+
[self reorderWebViewsInWindow:windowId];
|
|
1283
|
+
if (self.browsers) {
|
|
1284
|
+
auto it = self.browsers->find(windowId);
|
|
1285
|
+
if (it != self.browsers->end() && it->second) it->second->GetHost()->WasResized();
|
|
1286
|
+
}
|
|
1287
|
+
return YES;
|
|
1288
|
+
}
|
|
1289
|
+
NSView *webview = self.webviewViews[[self webViewKeyForWindow:windowId label:label]];
|
|
1290
|
+
NSView *stackView = [self stackViewForWindowId:windowId];
|
|
1291
|
+
if (!container || !stackView || !webview) return NO;
|
|
1292
|
+
webview.frame = [self webViewFrameForContainer:stackView x:x y:y width:width height:height];
|
|
1293
|
+
[self reorderWebViewsInWindow:windowId];
|
|
1294
|
+
std::string keyString([self webViewKeyForWindow:windowId label:label].UTF8String);
|
|
1295
|
+
if (self.webviewBrowsers) {
|
|
1296
|
+
auto it = self.webviewBrowsers->find(keyString);
|
|
1297
|
+
if (it != self.webviewBrowsers->end() && it->second) it->second->GetHost()->WasResized();
|
|
1298
|
+
}
|
|
1299
|
+
return YES;
|
|
1300
|
+
}
|
|
1301
|
+
|
|
1302
|
+
- (BOOL)navigateWebViewInWindow:(uint64_t)windowId label:(NSString *)label url:(NSString *)url {
|
|
1303
|
+
if (label.length == 0 || url.length == 0) return NO;
|
|
1304
|
+
NSURL *targetURL = [NSURL URLWithString:url];
|
|
1305
|
+
if (!targetURL || ![self allowsNavigationURL:targetURL]) return NO;
|
|
1306
|
+
NSString *resolvedURL = [self resolvedWebViewURLString:url windowId:windowId];
|
|
1307
|
+
if (resolvedURL.length == 0) return NO;
|
|
1308
|
+
if ([label isEqualToString:@"main"]) {
|
|
1309
|
+
if (self.browsers) {
|
|
1310
|
+
auto it = self.browsers->find(windowId);
|
|
1311
|
+
if (it != self.browsers->end() && it->second) {
|
|
1312
|
+
it->second->GetMainFrame()->LoadURL(std::string(resolvedURL.UTF8String));
|
|
1313
|
+
return YES;
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
return NO;
|
|
1317
|
+
}
|
|
1318
|
+
NSView *webview = self.webviewViews[[self webViewKeyForWindow:windowId label:label]];
|
|
1319
|
+
if (!webview) return NO;
|
|
1320
|
+
std::string keyString([self webViewKeyForWindow:windowId label:label].UTF8String);
|
|
1321
|
+
if (self.webviewBrowsers) {
|
|
1322
|
+
auto it = self.webviewBrowsers->find(keyString);
|
|
1323
|
+
if (it != self.webviewBrowsers->end() && it->second) {
|
|
1324
|
+
it->second->GetMainFrame()->LoadURL(std::string(resolvedURL.UTF8String));
|
|
1325
|
+
return YES;
|
|
1326
|
+
}
|
|
1327
|
+
}
|
|
1328
|
+
self.webviewPendingURLs[[self webViewKeyForWindow:windowId label:label]] = resolvedURL;
|
|
1329
|
+
return YES;
|
|
1330
|
+
}
|
|
1331
|
+
|
|
1332
|
+
- (BOOL)setWebViewZoomInWindow:(uint64_t)windowId label:(NSString *)label zoom:(double)zoom {
|
|
1333
|
+
if (label.length == 0 || zoom < 0.25 || zoom > 5.0) return NO;
|
|
1334
|
+
const double zoomLevel = log(zoom) / log(1.2);
|
|
1335
|
+
if ([label isEqualToString:@"main"]) {
|
|
1336
|
+
if (self.browsers) {
|
|
1337
|
+
auto it = self.browsers->find(windowId);
|
|
1338
|
+
if (it != self.browsers->end() && it->second) {
|
|
1339
|
+
it->second->GetHost()->SetZoomLevel(zoomLevel);
|
|
1340
|
+
return YES;
|
|
1341
|
+
}
|
|
1342
|
+
}
|
|
1343
|
+
return NO;
|
|
1344
|
+
}
|
|
1345
|
+
NSString *key = [self webViewKeyForWindow:windowId label:label];
|
|
1346
|
+
if (!self.webviewViews[key]) return NO;
|
|
1347
|
+
std::string keyString(key.UTF8String);
|
|
1348
|
+
if (self.webviewBrowsers) {
|
|
1349
|
+
auto it = self.webviewBrowsers->find(keyString);
|
|
1350
|
+
if (it != self.webviewBrowsers->end() && it->second) {
|
|
1351
|
+
it->second->GetHost()->SetZoomLevel(zoomLevel);
|
|
1352
|
+
[self.webviewPendingZooms removeObjectForKey:key];
|
|
1353
|
+
return YES;
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
self.webviewPendingZooms[key] = @(zoom);
|
|
1357
|
+
return YES;
|
|
1358
|
+
}
|
|
1359
|
+
|
|
1360
|
+
- (BOOL)setWebViewLayerInWindow:(uint64_t)windowId label:(NSString *)label layer:(NSInteger)layer {
|
|
1361
|
+
if (label.length == 0) return NO;
|
|
1362
|
+
if ([label isEqualToString:@"main"]) {
|
|
1363
|
+
NSView *container = self.browserContainers[@(windowId)] ?: (windowId == 1 ? self.browserContainer : nil);
|
|
1364
|
+
if (!container) return NO;
|
|
1365
|
+
container.wantsLayer = YES;
|
|
1366
|
+
container.layer.zPosition = layer;
|
|
1367
|
+
[self reorderWebViewsInWindow:windowId];
|
|
1368
|
+
return YES;
|
|
1369
|
+
}
|
|
1370
|
+
NSView *webview = self.webviewViews[[self webViewKeyForWindow:windowId label:label]];
|
|
1371
|
+
if (!webview) return NO;
|
|
1372
|
+
webview.wantsLayer = YES;
|
|
1373
|
+
webview.layer.zPosition = layer;
|
|
1374
|
+
[self reorderWebViewsInWindow:windowId];
|
|
1375
|
+
return YES;
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
- (BOOL)closeWebViewInWindow:(uint64_t)windowId label:(NSString *)label {
|
|
1379
|
+
NSString *key = [self webViewKeyForWindow:windowId label:label];
|
|
1380
|
+
if ([self.closingWebViewKeys containsObject:key]) return YES;
|
|
1381
|
+
NSView *webview = self.webviewViews[key];
|
|
1382
|
+
if (!webview) return NO;
|
|
1383
|
+
std::string keyString(key.UTF8String);
|
|
1384
|
+
[self.closingWebViewKeys addObject:key];
|
|
1385
|
+
[self.webviewPendingURLs removeObjectForKey:key];
|
|
1386
|
+
[self.webviewPendingZooms removeObjectForKey:key];
|
|
1387
|
+
CefRefPtr<CefBrowser> browser;
|
|
1388
|
+
if (self.webviewBrowsers) {
|
|
1389
|
+
auto browser_it = self.webviewBrowsers->find(keyString);
|
|
1390
|
+
if (browser_it != self.webviewBrowsers->end() && browser_it->second) {
|
|
1391
|
+
browser = browser_it->second;
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
if (browser) browser->GetHost()->CloseBrowser(true);
|
|
1395
|
+
[self cleanupClosedWebViewWithKey:key];
|
|
1396
|
+
return YES;
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
- (void)closeWebViewsInWindow:(uint64_t)windowId {
|
|
1400
|
+
NSString *prefix = [NSString stringWithFormat:@"%llu:", windowId];
|
|
1401
|
+
NSArray<NSString *> *keys = [self.webviewViews.allKeys copy];
|
|
1402
|
+
for (NSString *key in keys) {
|
|
1403
|
+
if (![key hasPrefix:prefix]) continue;
|
|
1404
|
+
NSRange separator = [key rangeOfString:@":"];
|
|
1405
|
+
NSString *label = separator.location == NSNotFound ? key : [key substringFromIndex:separator.location + 1];
|
|
1406
|
+
[self closeWebViewInWindow:windowId label:label];
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
|
|
1410
|
+
- (void)reorderWebViewsInWindow:(uint64_t)windowId {
|
|
1411
|
+
NSView *stackView = [self stackViewForWindowId:windowId];
|
|
1412
|
+
if (!stackView) return;
|
|
1413
|
+
|
|
1414
|
+
NSMutableArray<NSView *> *views = [[NSMutableArray alloc] init];
|
|
1415
|
+
NSView *mainView = self.browserContainers[@(windowId)] ?: (windowId == 1 ? self.browserContainer : nil);
|
|
1416
|
+
if (mainView && mainView.superview == stackView) {
|
|
1417
|
+
mainView.wantsLayer = YES;
|
|
1418
|
+
[views addObject:mainView];
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
NSString *prefix = [NSString stringWithFormat:@"%llu:", windowId];
|
|
1422
|
+
for (NSString *key in self.webviewViews) {
|
|
1423
|
+
if (![key hasPrefix:prefix]) continue;
|
|
1424
|
+
NSView *webView = self.webviewViews[key];
|
|
1425
|
+
if (webView && webView.superview == stackView) {
|
|
1426
|
+
webView.wantsLayer = YES;
|
|
1427
|
+
[views addObject:webView];
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
[views sortUsingComparator:^NSComparisonResult(NSView *first, NSView *second) {
|
|
1432
|
+
CGFloat firstLayer = first.layer.zPosition;
|
|
1433
|
+
CGFloat secondLayer = second.layer.zPosition;
|
|
1434
|
+
if (firstLayer < secondLayer) return NSOrderedAscending;
|
|
1435
|
+
if (firstLayer > secondLayer) return NSOrderedDescending;
|
|
1436
|
+
NSUInteger firstIndex = [stackView.subviews indexOfObjectIdenticalTo:first];
|
|
1437
|
+
NSUInteger secondIndex = [stackView.subviews indexOfObjectIdenticalTo:second];
|
|
1438
|
+
if (firstIndex < secondIndex) return NSOrderedAscending;
|
|
1439
|
+
if (firstIndex > secondIndex) return NSOrderedDescending;
|
|
1440
|
+
return NSOrderedSame;
|
|
1441
|
+
}];
|
|
1442
|
+
|
|
1443
|
+
NSView *previous = nil;
|
|
1444
|
+
for (NSView *view in views) {
|
|
1445
|
+
[stackView addSubview:view positioned:NSWindowAbove relativeTo:previous];
|
|
1446
|
+
previous = view;
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
|
|
1450
|
+
- (void)setAllowedNavigationOrigins:(NSArray<NSString *> *)origins externalURLs:(NSArray<NSString *> *)externalURLs externalAction:(NSInteger)externalAction {
|
|
1451
|
+
self.allowedNavigationOrigins = origins.count > 0 ? origins : @[ @"zero://app", @"zero://inline" ];
|
|
1452
|
+
self.allowedExternalURLs = externalURLs ?: @[];
|
|
1453
|
+
self.externalLinkAction = externalAction;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
- (BOOL)isInternalURL:(NSURL *)url {
|
|
1457
|
+
NSString *absolute = url.absoluteString ?: @"";
|
|
1458
|
+
for (NSString *prefix in self.internalURLPrefixes.allValues) {
|
|
1459
|
+
if ([absolute hasPrefix:prefix]) return YES;
|
|
1460
|
+
}
|
|
1461
|
+
return NO;
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
- (BOOL)isInternalURL:(NSURL *)url windowId:(uint64_t)windowId {
|
|
1465
|
+
NSString *prefix = self.internalURLPrefixes[@(windowId)];
|
|
1466
|
+
NSString *absolute = url.absoluteString ?: @"";
|
|
1467
|
+
return prefix.length > 0 && [absolute hasPrefix:prefix];
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
- (BOOL)allowsNavigationURL:(NSURL *)url {
|
|
1471
|
+
if (!url) return YES;
|
|
1472
|
+
NSString *scheme = url.scheme.lowercaseString ?: @"";
|
|
1473
|
+
if (scheme.length == 0 || [scheme isEqualToString:@"about"]) return YES;
|
|
1474
|
+
if ([self isInternalURL:url]) return YES;
|
|
1475
|
+
return NativeSdkPolicyListMatches(self.allowedNavigationOrigins, url);
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
- (BOOL)openExternalURLIfAllowed:(NSURL *)url {
|
|
1479
|
+
if (self.externalLinkAction != 1) return NO;
|
|
1480
|
+
if (!NativeSdkPolicyListMatches(self.allowedExternalURLs, url)) return NO;
|
|
1481
|
+
[[NSWorkspace sharedWorkspace] openURL:url];
|
|
1482
|
+
return YES;
|
|
1483
|
+
}
|
|
1484
|
+
|
|
1485
|
+
- (NSString *)resolvedWebViewURLString:(NSString *)url windowId:(uint64_t)windowId {
|
|
1486
|
+
NSURL *targetURL = [NSURL URLWithString:url ?: @""];
|
|
1487
|
+
if (!targetURL) return nil;
|
|
1488
|
+
NSNumber *key = @(windowId);
|
|
1489
|
+
NSString *assetOrigin = self.assetOrigins[key];
|
|
1490
|
+
NSString *assetRoot = self.assetRoots[key];
|
|
1491
|
+
NSString *assetEntry = self.assetEntries[key];
|
|
1492
|
+
if (assetOrigin.length == 0 || assetRoot.length == 0) return url;
|
|
1493
|
+
if (![NativeSdkOriginForURL(targetURL) isEqualToString:assetOrigin]) return url;
|
|
1494
|
+
|
|
1495
|
+
NSString *relativePath = NativeSdkSafeAssetPath(targetURL, assetEntry.length > 0 ? assetEntry : @"index.html");
|
|
1496
|
+
if (!relativePath) return nil;
|
|
1497
|
+
NSURL *fileURL = [NSURL fileURLWithPath:[assetRoot stringByAppendingPathComponent:relativePath]];
|
|
1498
|
+
NSURLComponents *components = [NSURLComponents componentsWithURL:fileURL resolvingAgainstBaseURL:NO];
|
|
1499
|
+
components.query = targetURL.query;
|
|
1500
|
+
components.fragment = targetURL.fragment;
|
|
1501
|
+
return components.URL.absoluteString ?: fileURL.absoluteString;
|
|
1502
|
+
}
|
|
1503
|
+
|
|
1504
|
+
- (void)setBrowser:(CefRefPtr<CefBrowser>)browser windowId:(uint64_t)windowId {
|
|
1505
|
+
if (self.browsers) (*self.browsers)[windowId] = browser;
|
|
1506
|
+
if (windowId == 1) self.browser = browser;
|
|
1507
|
+
}
|
|
1508
|
+
|
|
1509
|
+
- (BOOL)webViewGeneration:(uint64_t)generation matchesKey:(NSString *)key {
|
|
1510
|
+
NSNumber *current = self.webviewGenerations[key];
|
|
1511
|
+
return current && current.unsignedLongLongValue == generation;
|
|
1512
|
+
}
|
|
1513
|
+
|
|
1514
|
+
- (void)setWebViewBrowser:(CefRefPtr<CefBrowser>)browser key:(NSString *)key generation:(uint64_t)generation {
|
|
1515
|
+
if (!self.webviewBrowsers || key.length == 0) return;
|
|
1516
|
+
if (![self webViewGeneration:generation matchesKey:key] || [self.closingWebViewKeys containsObject:key]) {
|
|
1517
|
+
if (browser) browser->GetHost()->CloseBrowser(true);
|
|
1518
|
+
return;
|
|
1519
|
+
}
|
|
1520
|
+
(*self.webviewBrowsers)[std::string(key.UTF8String)] = browser;
|
|
1521
|
+
NSString *pendingURL = self.webviewPendingURLs[key];
|
|
1522
|
+
if (pendingURL.length > 0 && browser) {
|
|
1523
|
+
browser->GetMainFrame()->LoadURL(std::string(pendingURL.UTF8String));
|
|
1524
|
+
[self.webviewPendingURLs removeObjectForKey:key];
|
|
1525
|
+
}
|
|
1526
|
+
NSNumber *pendingZoom = self.webviewPendingZooms[key];
|
|
1527
|
+
if (pendingZoom && browser) {
|
|
1528
|
+
browser->GetHost()->SetZoomLevel(log(pendingZoom.doubleValue) / log(1.2));
|
|
1529
|
+
[self.webviewPendingZooms removeObjectForKey:key];
|
|
1530
|
+
}
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1533
|
+
- (void)cleanupClosedWebViewWithKey:(NSString *)key {
|
|
1534
|
+
if (key.length == 0) return;
|
|
1535
|
+
std::string keyString(key.UTF8String);
|
|
1536
|
+
if (self.webviewBrowsers) self.webviewBrowsers->erase(keyString);
|
|
1537
|
+
if (self.webviewCefClients) self.webviewCefClients->erase(keyString);
|
|
1538
|
+
[self.webviewPendingURLs removeObjectForKey:key];
|
|
1539
|
+
[self.webviewPendingZooms removeObjectForKey:key];
|
|
1540
|
+
[self.webviewGenerations removeObjectForKey:key];
|
|
1541
|
+
[self.webviewViews[key] removeFromSuperview];
|
|
1542
|
+
[self.webviewViews removeObjectForKey:key];
|
|
1543
|
+
[self.closingWebViewKeys removeObject:key];
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
- (void)cleanupClosedWebViewWithKey:(NSString *)key generation:(uint64_t)generation {
|
|
1547
|
+
if (![self webViewGeneration:generation matchesKey:key]) return;
|
|
1548
|
+
[self cleanupClosedWebViewWithKey:key];
|
|
1549
|
+
}
|
|
1550
|
+
|
|
1551
|
+
- (NSString *)fallbackURLForWindowId:(uint64_t)windowId {
|
|
1552
|
+
return self.fallbackURLs[@(windowId)];
|
|
1553
|
+
}
|
|
1554
|
+
|
|
1555
|
+
- (NSString *)bridgeOriginForWindowId:(uint64_t)windowId webViewLabel:(NSString *)webViewLabel sourceURL:(NSString *)sourceURL {
|
|
1556
|
+
NSURL *url = [NSURL URLWithString:sourceURL ?: @""];
|
|
1557
|
+
NSString *label = webViewLabel.length > 0 ? webViewLabel : @"main";
|
|
1558
|
+
NSString *assetOrigin = self.assetOrigins[@(windowId)];
|
|
1559
|
+
if ([label isEqualToString:@"main"]) {
|
|
1560
|
+
NSString *origin = self.bridgeOrigins[@(windowId)];
|
|
1561
|
+
if (origin.length > 0) return origin;
|
|
1562
|
+
} else if (assetOrigin.length > 0 && [self isInternalURL:url windowId:windowId]) {
|
|
1563
|
+
return assetOrigin;
|
|
1564
|
+
}
|
|
1565
|
+
return NativeSdkOriginForURL(url);
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
- (void)receiveBridgePayload:(NSString *)payload origin:(NSString *)origin windowId:(uint64_t)windowId webViewLabel:(NSString *)webViewLabel {
|
|
1569
|
+
if (!self.bridgeCallback) return;
|
|
1570
|
+
NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding] ?: [NSData data];
|
|
1571
|
+
NSData *originData = [origin dataUsingEncoding:NSUTF8StringEncoding] ?: [NSData data];
|
|
1572
|
+
NSData *labelData = [(webViewLabel.length > 0 ? webViewLabel : @"main") dataUsingEncoding:NSUTF8StringEncoding] ?: [NSData data];
|
|
1573
|
+
self.bridgeCallback(self.bridgeContext, windowId, (const char *)labelData.bytes, labelData.length, (const char *)payloadData.bytes, payloadData.length, (const char *)originData.bytes, originData.length);
|
|
1574
|
+
}
|
|
1575
|
+
|
|
1576
|
+
- (void)completeBridgeWithResponse:(NSString *)response {
|
|
1577
|
+
[self completeBridgeWithResponse:response windowId:1 webViewLabel:@"main"];
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
- (void)completeBridgeWithResponse:(NSString *)response windowId:(uint64_t)windowId {
|
|
1581
|
+
[self completeBridgeWithResponse:response windowId:windowId webViewLabel:@"main"];
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
- (void)completeBridgeWithResponse:(NSString *)response windowId:(uint64_t)windowId webViewLabel:(NSString *)webViewLabel {
|
|
1585
|
+
CefRefPtr<CefBrowser> browser;
|
|
1586
|
+
NSString *label = webViewLabel.length > 0 ? webViewLabel : @"main";
|
|
1587
|
+
if ([label isEqualToString:@"main"] && self.browsers) {
|
|
1588
|
+
auto it = self.browsers->find(windowId);
|
|
1589
|
+
if (it != self.browsers->end()) browser = it->second;
|
|
1590
|
+
} else if (self.webviewBrowsers) {
|
|
1591
|
+
std::string key = std::string([[self webViewKeyForWindow:windowId label:label] UTF8String]);
|
|
1592
|
+
auto it = self.webviewBrowsers->find(key);
|
|
1593
|
+
if (it != self.webviewBrowsers->end()) browser = it->second;
|
|
1594
|
+
}
|
|
1595
|
+
if (!browser) return;
|
|
1596
|
+
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
|
|
1597
|
+
if (!frame) return;
|
|
1598
|
+
NSString *script = [NSString stringWithFormat:@"window.zero&&window.zero._complete(%@);", response.length > 0 ? response : @"{}"];
|
|
1599
|
+
std::string scriptString(script.UTF8String);
|
|
1600
|
+
frame->ExecuteJavaScript(scriptString, frame->GetURL(), 0);
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
- (void)emitEventNamed:(NSString *)name detailJSON:(NSString *)detailJSON windowId:(uint64_t)windowId {
|
|
1604
|
+
CefRefPtr<CefBrowser> browser;
|
|
1605
|
+
if (self.browsers) {
|
|
1606
|
+
auto it = self.browsers->find(windowId);
|
|
1607
|
+
if (it != self.browsers->end()) browser = it->second;
|
|
1608
|
+
}
|
|
1609
|
+
if (!browser) return;
|
|
1610
|
+
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
|
|
1611
|
+
if (!frame) return;
|
|
1612
|
+
NSData *nameData = [NSJSONSerialization dataWithJSONObject:name ?: @"" options:NSJSONWritingFragmentsAllowed error:nil];
|
|
1613
|
+
NSString *nameJSON = nameData ? [[NSString alloc] initWithData:nameData encoding:NSUTF8StringEncoding] : @"\"\"";
|
|
1614
|
+
NSString *detail = detailJSON.length > 0 ? detailJSON : @"null";
|
|
1615
|
+
NSString *script = [NSString stringWithFormat:@"window.zero&&window.zero._emit(%@,%@);", nameJSON, detail];
|
|
1616
|
+
frame->ExecuteJavaScript(std::string(script.UTF8String), frame->GetURL(), 0);
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
- (BOOL)handleShortcutEvent:(NSEvent *)event {
|
|
1620
|
+
if (event.type != NSEventTypeKeyDown) return NO;
|
|
1621
|
+
NSString *key = NativeSdkShortcutKeyForEvent(event);
|
|
1622
|
+
if (key.length == 0) return NO;
|
|
1623
|
+
BOOL usesImplicitShift = NativeSdkShortcutUsesImplicitShift(key, event);
|
|
1624
|
+
|
|
1625
|
+
for (NSUInteger pass = 0; pass < (usesImplicitShift ? 2 : 1); pass++) {
|
|
1626
|
+
BOOL allowImplicitShift = pass == 1;
|
|
1627
|
+
for (NativeSdkChromiumShortcut *shortcut in self.shortcuts) {
|
|
1628
|
+
if (![shortcut.key isEqualToString:key]) continue;
|
|
1629
|
+
if (!NativeSdkShortcutModifiersMatch(shortcut.modifiers, event.modifierFlags, allowImplicitShift)) continue;
|
|
1630
|
+
[self emitShortcutWithId:shortcut.identifier key:shortcut.key modifiers:shortcut.modifiers event:event];
|
|
1631
|
+
return YES;
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
return NO;
|
|
1636
|
+
}
|
|
1637
|
+
|
|
1638
|
+
- (void)emitShortcutWithId:(NSString *)identifier key:(NSString *)key modifiers:(uint32_t)modifiers event:(NSEvent *)event {
|
|
1639
|
+
uint64_t windowId = 1;
|
|
1640
|
+
NSWindow *window = event.window ?: NSApp.keyWindow;
|
|
1641
|
+
for (NSNumber *keyValue in self.windows) {
|
|
1642
|
+
if (self.windows[keyValue] == window) {
|
|
1643
|
+
windowId = keyValue.unsignedLongLongValue;
|
|
1644
|
+
break;
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
const char *identifierBytes = identifier.UTF8String ? identifier.UTF8String : "";
|
|
1648
|
+
const char *keyBytes = key.UTF8String ? key.UTF8String : "";
|
|
1649
|
+
[self emitEvent:(native_sdk_appkit_event_t){
|
|
1650
|
+
.kind = NATIVE_SDK_APPKIT_EVENT_SHORTCUT,
|
|
1651
|
+
.window_id = windowId,
|
|
1652
|
+
.shortcut_id = identifierBytes,
|
|
1653
|
+
.shortcut_id_len = [identifier lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
1654
|
+
.shortcut_key = keyBytes,
|
|
1655
|
+
.shortcut_key_len = [key lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
1656
|
+
.shortcut_modifiers = modifiers,
|
|
1657
|
+
}];
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
- (void)setShortcutsWithIds:(const char *const *)ids idLengths:(const size_t *)idLengths keys:(const char *const *)keys keyLengths:(const size_t *)keyLengths modifiers:(const uint32_t *)modifiers count:(size_t)count {
|
|
1661
|
+
NSMutableArray<NativeSdkChromiumShortcut *> *items = [[NSMutableArray alloc] initWithCapacity:count];
|
|
1662
|
+
for (size_t index = 0; index < count; index++) {
|
|
1663
|
+
NSString *identifier = ids[index] ? [[NSString alloc] initWithBytes:ids[index] length:idLengths[index] encoding:NSUTF8StringEncoding] : @"";
|
|
1664
|
+
NSString *key = keys[index] ? [[NSString alloc] initWithBytes:keys[index] length:keyLengths[index] encoding:NSUTF8StringEncoding] : @"";
|
|
1665
|
+
if (identifier.length == 0 || key.length == 0) continue;
|
|
1666
|
+
NativeSdkChromiumShortcut *shortcut = [[NativeSdkChromiumShortcut alloc] init];
|
|
1667
|
+
shortcut.identifier = identifier;
|
|
1668
|
+
shortcut.key = key.lowercaseString;
|
|
1669
|
+
shortcut.modifiers = modifiers[index];
|
|
1670
|
+
[items addObject:shortcut];
|
|
1671
|
+
}
|
|
1672
|
+
self.shortcuts = items;
|
|
1673
|
+
}
|
|
1674
|
+
|
|
1675
|
+
- (void)trayMenuItemClicked:(NSMenuItem *)menuItem {
|
|
1676
|
+
if (self.trayCallback) self.trayCallback(self.trayContext, (uint32_t)menuItem.tag);
|
|
1677
|
+
}
|
|
1678
|
+
|
|
1679
|
+
@end
|
|
1680
|
+
|
|
1681
|
+
namespace {
|
|
1682
|
+
|
|
1683
|
+
static NSArray<NSString *> *NativeSdkPolicyListFromBytes(const char *bytes, size_t len, NSArray<NSString *> *fallback) {
|
|
1684
|
+
if (!bytes || len == 0) return fallback ?: @[];
|
|
1685
|
+
NSString *joined = [[NSString alloc] initWithBytes:bytes length:len encoding:NSUTF8StringEncoding];
|
|
1686
|
+
if (joined.length == 0) return fallback ?: @[];
|
|
1687
|
+
NSMutableArray<NSString *> *values = [[NSMutableArray alloc] init];
|
|
1688
|
+
for (NSString *part in [joined componentsSeparatedByString:@"\n"]) {
|
|
1689
|
+
NSString *trimmed = [part stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet];
|
|
1690
|
+
if (trimmed.length > 0) [values addObject:trimmed];
|
|
1691
|
+
}
|
|
1692
|
+
return values.count > 0 ? values : (fallback ?: @[]);
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
static NSString *NativeSdkOriginForURL(NSURL *url) {
|
|
1696
|
+
if (!url) return @"";
|
|
1697
|
+
NSString *scheme = url.scheme.lowercaseString ?: @"";
|
|
1698
|
+
if (scheme.length == 0 || [scheme isEqualToString:@"about"]) return @"zero://inline";
|
|
1699
|
+
if ([scheme isEqualToString:@"file"]) return @"file://local";
|
|
1700
|
+
NSString *host = url.host ?: @"";
|
|
1701
|
+
if (host.length == 0) return [NSString stringWithFormat:@"%@://local", scheme];
|
|
1702
|
+
NSNumber *port = url.port;
|
|
1703
|
+
if (port) return [NSString stringWithFormat:@"%@://%@:%@", scheme, host, port];
|
|
1704
|
+
return [NSString stringWithFormat:@"%@://%@", scheme, host];
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
static NSString *NativeSdkShortcutKeyForEvent(NSEvent *event) {
|
|
1708
|
+
NSString *characters = event.charactersIgnoringModifiers ?: @"";
|
|
1709
|
+
if (characters.length == 0) return @"";
|
|
1710
|
+
unichar ch = [characters characterAtIndex:0];
|
|
1711
|
+
switch (ch) {
|
|
1712
|
+
case NSUpArrowFunctionKey: return @"arrowup";
|
|
1713
|
+
case NSDownArrowFunctionKey: return @"arrowdown";
|
|
1714
|
+
case NSLeftArrowFunctionKey: return @"arrowleft";
|
|
1715
|
+
case NSRightArrowFunctionKey: return @"arrowright";
|
|
1716
|
+
case NSDeleteFunctionKey: return @"delete";
|
|
1717
|
+
case NSHomeFunctionKey: return @"home";
|
|
1718
|
+
case NSEndFunctionKey: return @"end";
|
|
1719
|
+
case 0x1b: return @"escape";
|
|
1720
|
+
case '\r': return @"enter";
|
|
1721
|
+
case '\t': return @"tab";
|
|
1722
|
+
case NSBackTabCharacter: return @"tab";
|
|
1723
|
+
case ' ': return @"space";
|
|
1724
|
+
case 0x7f: return @"backspace";
|
|
1725
|
+
case '!': return @"1";
|
|
1726
|
+
case '@': return @"2";
|
|
1727
|
+
case '#': return @"3";
|
|
1728
|
+
case '$': return @"4";
|
|
1729
|
+
case '%': return @"5";
|
|
1730
|
+
case '^': return @"6";
|
|
1731
|
+
case '&': return @"7";
|
|
1732
|
+
case '*': return @"8";
|
|
1733
|
+
case '(': return @"9";
|
|
1734
|
+
case ')': return @"0";
|
|
1735
|
+
case '+': return @"=";
|
|
1736
|
+
case '_': return @"-";
|
|
1737
|
+
case '<': return @",";
|
|
1738
|
+
case '>': return @".";
|
|
1739
|
+
case '?': return @"/";
|
|
1740
|
+
case ':': return @";";
|
|
1741
|
+
case '"': return @"'";
|
|
1742
|
+
case '{': return @"[";
|
|
1743
|
+
case '}': return @"]";
|
|
1744
|
+
case '|': return @"\\";
|
|
1745
|
+
case '~': return @"`";
|
|
1746
|
+
default: return characters.lowercaseString;
|
|
1747
|
+
}
|
|
1748
|
+
}
|
|
1749
|
+
|
|
1750
|
+
static BOOL NativeSdkShortcutUsesImplicitShift(NSString *key, NSEvent *event) {
|
|
1751
|
+
if ((event.modifierFlags & NSEventModifierFlagShift) == 0) return NO;
|
|
1752
|
+
if (key.length != 1) return NO;
|
|
1753
|
+
unichar ch = [key characterAtIndex:0];
|
|
1754
|
+
return (ch >= '0' && ch <= '9') ||
|
|
1755
|
+
ch == '=' || ch == '-' || ch == ',' ||
|
|
1756
|
+
ch == '.' || ch == '/' || ch == ';' || ch == '\'' ||
|
|
1757
|
+
ch == '[' || ch == ']' || ch == '\\' || ch == '`';
|
|
1758
|
+
}
|
|
1759
|
+
|
|
1760
|
+
static BOOL NativeSdkShortcutModifiersMatch(uint32_t shortcutModifiers, NSEventModifierFlags eventModifiers, BOOL allowImplicitShift) {
|
|
1761
|
+
NSEventModifierFlags flags = eventModifiers & NSEventModifierFlagDeviceIndependentFlagsMask;
|
|
1762
|
+
BOOL needsCommand = (shortcutModifiers & NativeSdkShortcutModifierCommand) != 0 || (shortcutModifiers & NativeSdkShortcutModifierPrimary) != 0;
|
|
1763
|
+
BOOL needsControl = (shortcutModifiers & NativeSdkShortcutModifierControl) != 0;
|
|
1764
|
+
BOOL needsOption = (shortcutModifiers & NativeSdkShortcutModifierOption) != 0;
|
|
1765
|
+
BOOL needsShift = (shortcutModifiers & NativeSdkShortcutModifierShift) != 0;
|
|
1766
|
+
BOOL hasCommand = (flags & NSEventModifierFlagCommand) != 0;
|
|
1767
|
+
BOOL hasControl = (flags & NSEventModifierFlagControl) != 0;
|
|
1768
|
+
BOOL hasOption = (flags & NSEventModifierFlagOption) != 0;
|
|
1769
|
+
BOOL hasShift = (flags & NSEventModifierFlagShift) != 0;
|
|
1770
|
+
BOOL shiftMatches = needsShift ? hasShift : (!hasShift || allowImplicitShift);
|
|
1771
|
+
return hasCommand == needsCommand && hasControl == needsControl && hasOption == needsOption && shiftMatches;
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1774
|
+
static BOOL NativeSdkWildcardPrefixHasPath(NSString *prefix) {
|
|
1775
|
+
NSURLComponents *components = [NSURLComponents componentsWithString:prefix ?: @""];
|
|
1776
|
+
return components.scheme.length > 0 && components.host.length > 0 && components.percentEncodedPath.length > 0;
|
|
1777
|
+
}
|
|
1778
|
+
|
|
1779
|
+
static BOOL NativeSdkPolicyListMatches(NSArray<NSString *> *values, NSURL *url) {
|
|
1780
|
+
NSString *origin = NativeSdkOriginForURL(url);
|
|
1781
|
+
NSString *absolute = url.absoluteString ?: @"";
|
|
1782
|
+
for (NSString *value in values) {
|
|
1783
|
+
if ([value isEqualToString:@"*"]) return YES;
|
|
1784
|
+
if ([value isEqualToString:origin] || [value isEqualToString:absolute]) return YES;
|
|
1785
|
+
if ([value hasSuffix:@"*"]) {
|
|
1786
|
+
NSString *prefix = [value substringToIndex:value.length - 1];
|
|
1787
|
+
if (NativeSdkWildcardPrefixHasPath(prefix) && [absolute hasPrefix:prefix]) return YES;
|
|
1788
|
+
}
|
|
1789
|
+
}
|
|
1790
|
+
return NO;
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
void NativeSdkCefClient::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
|
|
1794
|
+
if (!webview_key_.empty()) {
|
|
1795
|
+
NSString *key = [[NSString alloc] initWithBytes:webview_key_.data() length:webview_key_.size() encoding:NSUTF8StringEncoding];
|
|
1796
|
+
[host_ setWebViewBrowser:browser key:key ?: @"" generation:webview_generation_];
|
|
1797
|
+
return;
|
|
1798
|
+
}
|
|
1799
|
+
[host_ setBrowser:browser windowId:window_id_];
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
void NativeSdkCefClient::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
|
|
1803
|
+
(void)browser;
|
|
1804
|
+
if (webview_key_.empty()) return;
|
|
1805
|
+
NSString *key = [[NSString alloc] initWithBytes:webview_key_.data() length:webview_key_.size() encoding:NSUTF8StringEncoding];
|
|
1806
|
+
[host_ cleanupClosedWebViewWithKey:key ?: @"" generation:webview_generation_];
|
|
1807
|
+
}
|
|
1808
|
+
|
|
1809
|
+
void NativeSdkCefClient::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) {
|
|
1810
|
+
(void)browser;
|
|
1811
|
+
(void)errorText;
|
|
1812
|
+
if (!frame || !frame->IsMain() || errorCode != ERR_FILE_NOT_FOUND) return;
|
|
1813
|
+
NSString *fallback = [host_ fallbackURLForWindowId:window_id_];
|
|
1814
|
+
if (fallback.length == 0) return;
|
|
1815
|
+
std::string failed = failedUrl.ToString();
|
|
1816
|
+
NSString *failedString = [[NSString alloc] initWithBytes:failed.data() length:failed.size() encoding:NSUTF8StringEncoding] ?: @"";
|
|
1817
|
+
if ([failedString isEqualToString:fallback]) return;
|
|
1818
|
+
frame->LoadURL(std::string(fallback.UTF8String));
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
bool NativeSdkCefClient::OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, bool user_gesture, bool is_redirect) {
|
|
1822
|
+
(void)browser;
|
|
1823
|
+
(void)user_gesture;
|
|
1824
|
+
(void)is_redirect;
|
|
1825
|
+
if (frame && !frame->IsMain()) return false;
|
|
1826
|
+
std::string url = request ? request->GetURL().ToString() : std::string();
|
|
1827
|
+
NSString *urlString = [[NSString alloc] initWithBytes:url.data() length:url.size() encoding:NSUTF8StringEncoding] ?: @"";
|
|
1828
|
+
NSURL *nsURL = [NSURL URLWithString:urlString];
|
|
1829
|
+
if ([host_ allowsNavigationURL:nsURL]) return false;
|
|
1830
|
+
if ([host_ openExternalURLIfAllowed:nsURL]) return true;
|
|
1831
|
+
return true;
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
bool NativeSdkCefClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) {
|
|
1835
|
+
(void)browser;
|
|
1836
|
+
(void)source_process;
|
|
1837
|
+
if (message->GetName() != kBridgeMessageName) return false;
|
|
1838
|
+
if (!bridge_enabled_) return true;
|
|
1839
|
+
|
|
1840
|
+
std::string payload = message->GetArgumentList()->GetString(0);
|
|
1841
|
+
std::string source_url = frame ? frame->GetURL().ToString() : std::string();
|
|
1842
|
+
std::string label = WebViewLabel();
|
|
1843
|
+
NSString *payloadString = [[NSString alloc] initWithBytes:payload.data() length:payload.size() encoding:NSUTF8StringEncoding] ?: @"{}";
|
|
1844
|
+
NSString *sourceURLString = [[NSString alloc] initWithBytes:source_url.data() length:source_url.size() encoding:NSUTF8StringEncoding] ?: @"";
|
|
1845
|
+
NSString *labelString = [[NSString alloc] initWithBytes:label.data() length:label.size() encoding:NSUTF8StringEncoding] ?: @"main";
|
|
1846
|
+
NSString *originString = [host_ bridgeOriginForWindowId:window_id_ webViewLabel:labelString sourceURL:sourceURLString];
|
|
1847
|
+
[host_ receiveBridgePayload:payloadString origin:originString windowId:window_id_ webViewLabel:labelString];
|
|
1848
|
+
return true;
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
} // namespace
|
|
1852
|
+
|
|
1853
|
+
static void NativeSdkApplyHiddenInsetTitlebar(NSWindow *window, int titlebar_style, NativeSdkChromiumWindowDelegate *delegate) {
|
|
1854
|
+
if (!window || (titlebar_style != 1 && titlebar_style != 2)) return;
|
|
1855
|
+
window.styleMask |= NSWindowStyleMaskFullSizeContentView;
|
|
1856
|
+
window.titlebarAppearsTransparent = YES;
|
|
1857
|
+
window.titleVisibility = NSWindowTitleHidden;
|
|
1858
|
+
if (titlebar_style == 2) {
|
|
1859
|
+
// hidden_inset_tall: an empty borderless toolbar switches the
|
|
1860
|
+
// titlebar to the unified-toolbar height and the system centers
|
|
1861
|
+
// the traffic lights in it (same trick as the AppKit host).
|
|
1862
|
+
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:@"native-sdk-tall-titlebar"];
|
|
1863
|
+
toolbar.allowsUserCustomization = NO;
|
|
1864
|
+
window.toolbar = toolbar;
|
|
1865
|
+
window.toolbarStyle = NSWindowToolbarStyleUnified;
|
|
1866
|
+
window.titlebarSeparatorStyle = NSTitlebarSeparatorStyleNone;
|
|
1867
|
+
if (delegate && !delegate.observesContentLayout) {
|
|
1868
|
+
// Chrome re-query timing rides the settled contentLayoutRect
|
|
1869
|
+
// (see the delegate's observeValueForKeyPath:).
|
|
1870
|
+
delegate.observesContentLayout = YES;
|
|
1871
|
+
[window addObserver:delegate forKeyPath:@"contentLayoutRect" options:0 context:NULL];
|
|
1872
|
+
}
|
|
1873
|
+
}
|
|
1874
|
+
}
|
|
1875
|
+
|
|
1876
|
+
native_sdk_appkit_host_t *native_sdk_appkit_create(const char *app_name, size_t app_name_len, const char *display_name, size_t display_name_len, const char *version, size_t version_len, const char *about_description, size_t about_description_len, int has_web_content, const char *window_title, size_t window_title_len, const char *bundle_id, size_t bundle_id_len, const char *icon_path, size_t icon_path_len, const char *window_label, size_t window_label_len, double x, double y, double width, double height, int restore_frame, int resizable, int titlebar_style, int show_policy) {
|
|
1877
|
+
@autoreleasepool {
|
|
1878
|
+
// Present-before-show is a canvas contract; the Chromium host
|
|
1879
|
+
// hosts webviews only (gpu-surface presents are unsupported on
|
|
1880
|
+
// this engine), so the policy is accepted for ABI parity and
|
|
1881
|
+
// windows show immediately — the web engine owns first paint.
|
|
1882
|
+
// has_web_content is likewise ABI parity: this host always
|
|
1883
|
+
// hosts web content, and its menus already assume it.
|
|
1884
|
+
(void)show_policy;
|
|
1885
|
+
(void)has_web_content;
|
|
1886
|
+
(void)bundle_id;
|
|
1887
|
+
(void)bundle_id_len;
|
|
1888
|
+
(void)icon_path;
|
|
1889
|
+
(void)icon_path_len;
|
|
1890
|
+
(void)window_label;
|
|
1891
|
+
(void)window_label_len;
|
|
1892
|
+
NSString *appNameString = [[NSString alloc] initWithBytes:app_name length:app_name_len encoding:NSUTF8StringEncoding] ?: @"native-sdk";
|
|
1893
|
+
NSString *displayNameString = [[NSString alloc] initWithBytes:display_name length:display_name_len encoding:NSUTF8StringEncoding] ?: @"";
|
|
1894
|
+
NSString *versionString = [[NSString alloc] initWithBytes:version length:version_len encoding:NSUTF8StringEncoding] ?: @"";
|
|
1895
|
+
NSString *aboutDescriptionString = [[NSString alloc] initWithBytes:about_description length:about_description_len encoding:NSUTF8StringEncoding] ?: @"";
|
|
1896
|
+
NSString *titleString = [[NSString alloc] initWithBytes:window_title length:window_title_len encoding:NSUTF8StringEncoding] ?: appNameString;
|
|
1897
|
+
NativeSdkChromiumHost *host = [[NativeSdkChromiumHost alloc] initWithAppName:appNameString displayName:displayNameString version:versionString aboutDescription:aboutDescriptionString title:titleString width:width height:height];
|
|
1898
|
+
if (restore_frame) {
|
|
1899
|
+
[host.window setFrame:NativeSdkConstrainFrame(NSMakeRect(x, y, width, height)) display:NO];
|
|
1900
|
+
}
|
|
1901
|
+
if (!resizable) {
|
|
1902
|
+
host.window.styleMask &= ~NSWindowStyleMaskResizable;
|
|
1903
|
+
}
|
|
1904
|
+
NativeSdkApplyHiddenInsetTitlebar(host.window, titlebar_style, host.delegates[@1]);
|
|
1905
|
+
return (__bridge_retained native_sdk_appkit_host_t *)host;
|
|
1906
|
+
}
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
void native_sdk_appkit_destroy(native_sdk_appkit_host_t *host) {
|
|
1910
|
+
if (!host) return;
|
|
1911
|
+
CFBridgingRelease(host);
|
|
1912
|
+
}
|
|
1913
|
+
|
|
1914
|
+
void native_sdk_appkit_run(native_sdk_appkit_host_t *host, native_sdk_appkit_event_callback_t callback, void *context) {
|
|
1915
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1916
|
+
[object runWithCallback:callback context:context];
|
|
1917
|
+
}
|
|
1918
|
+
|
|
1919
|
+
void native_sdk_appkit_stop(native_sdk_appkit_host_t *host) {
|
|
1920
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1921
|
+
[object emitShutdown];
|
|
1922
|
+
[object stop];
|
|
1923
|
+
}
|
|
1924
|
+
|
|
1925
|
+
void native_sdk_appkit_load_webview(native_sdk_appkit_host_t *host, const char *source, size_t source_len, int source_kind, const char *asset_root, size_t asset_root_len, const char *asset_entry, size_t asset_entry_len, const char *asset_origin, size_t asset_origin_len, int spa_fallback) {
|
|
1926
|
+
native_sdk_appkit_load_window_webview(host, 1, source, source_len, source_kind, asset_root, asset_root_len, asset_entry, asset_entry_len, asset_origin, asset_origin_len, spa_fallback);
|
|
1927
|
+
}
|
|
1928
|
+
|
|
1929
|
+
void native_sdk_appkit_load_window_webview(native_sdk_appkit_host_t *host, uint64_t window_id, const char *source, size_t source_len, int source_kind, const char *asset_root, size_t asset_root_len, const char *asset_entry, size_t asset_entry_len, const char *asset_origin, size_t asset_origin_len, int spa_fallback) {
|
|
1930
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1931
|
+
NSString *sourceString = source ? [[NSString alloc] initWithBytes:source length:source_len encoding:NSUTF8StringEncoding] : @"";
|
|
1932
|
+
NSString *assetRoot = asset_root ? [[NSString alloc] initWithBytes:asset_root length:asset_root_len encoding:NSUTF8StringEncoding] : @"";
|
|
1933
|
+
NSString *assetEntry = asset_entry ? [[NSString alloc] initWithBytes:asset_entry length:asset_entry_len encoding:NSUTF8StringEncoding] : @"";
|
|
1934
|
+
NSString *assetOrigin = asset_origin ? [[NSString alloc] initWithBytes:asset_origin length:asset_origin_len encoding:NSUTF8StringEncoding] : @"";
|
|
1935
|
+
[object loadSource:sourceString ?: @""
|
|
1936
|
+
kind:source_kind
|
|
1937
|
+
assetRoot:assetRoot ?: @""
|
|
1938
|
+
entry:assetEntry ?: @""
|
|
1939
|
+
origin:assetOrigin ?: @""
|
|
1940
|
+
spaFallback:(spa_fallback != 0)
|
|
1941
|
+
windowId:window_id];
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
void native_sdk_appkit_set_bridge_callback(native_sdk_appkit_host_t *host, native_sdk_appkit_bridge_callback_t callback, void *context) {
|
|
1945
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1946
|
+
object.bridgeCallback = callback;
|
|
1947
|
+
object.bridgeContext = context;
|
|
1948
|
+
}
|
|
1949
|
+
|
|
1950
|
+
void native_sdk_appkit_bridge_respond(native_sdk_appkit_host_t *host, const char *response, size_t response_len) {
|
|
1951
|
+
native_sdk_appkit_bridge_respond_window(host, 1, response, response_len);
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
void native_sdk_appkit_bridge_respond_window(native_sdk_appkit_host_t *host, uint64_t window_id, const char *response, size_t response_len) {
|
|
1955
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1956
|
+
NSString *responseString = response ? [[NSString alloc] initWithBytes:response length:response_len encoding:NSUTF8StringEncoding] : @"{}";
|
|
1957
|
+
[object completeBridgeWithResponse:responseString ?: @"{}" windowId:window_id];
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
void native_sdk_appkit_bridge_respond_webview(native_sdk_appkit_host_t *host, uint64_t window_id, const char *webview_label, size_t webview_label_len, const char *response, size_t response_len) {
|
|
1961
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1962
|
+
NSString *labelString = webview_label ? [[NSString alloc] initWithBytes:webview_label length:webview_label_len encoding:NSUTF8StringEncoding] : @"main";
|
|
1963
|
+
NSString *responseString = response ? [[NSString alloc] initWithBytes:response length:response_len encoding:NSUTF8StringEncoding] : @"{}";
|
|
1964
|
+
[object completeBridgeWithResponse:responseString ?: @"{}" windowId:window_id webViewLabel:labelString ?: @"main"];
|
|
1965
|
+
}
|
|
1966
|
+
|
|
1967
|
+
void native_sdk_appkit_emit_window_event(native_sdk_appkit_host_t *host, uint64_t window_id, const char *name, size_t name_len, const char *detail_json, size_t detail_json_len) {
|
|
1968
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1969
|
+
NSString *nameString = name ? [[NSString alloc] initWithBytes:name length:name_len encoding:NSUTF8StringEncoding] : @"";
|
|
1970
|
+
NSString *detailString = detail_json ? [[NSString alloc] initWithBytes:detail_json length:detail_json_len encoding:NSUTF8StringEncoding] : @"null";
|
|
1971
|
+
[object emitEventNamed:nameString ?: @"" detailJSON:detailString ?: @"null" windowId:window_id];
|
|
1972
|
+
}
|
|
1973
|
+
|
|
1974
|
+
void native_sdk_appkit_set_security_policy(native_sdk_appkit_host_t *host, const char *allowed_origins, size_t allowed_origins_len, const char *external_urls, size_t external_urls_len, int external_action) {
|
|
1975
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
1976
|
+
NSArray<NSString *> *origins = NativeSdkPolicyListFromBytes(allowed_origins, allowed_origins_len, @[ @"zero://app", @"zero://inline" ]);
|
|
1977
|
+
NSArray<NSString *> *externalURLs = NativeSdkPolicyListFromBytes(external_urls, external_urls_len, @[]);
|
|
1978
|
+
[object setAllowedNavigationOrigins:origins externalURLs:externalURLs externalAction:external_action];
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
void native_sdk_appkit_set_menus(native_sdk_appkit_host_t *host, const char *const *menu_titles, const size_t *menu_title_lens, size_t menu_count, const uint32_t *item_menu_indices, const char *const *item_labels, const size_t *item_label_lens, const char *const *item_commands, const size_t *item_command_lens, const char *const *item_keys, const size_t *item_key_lens, const uint32_t *item_modifiers, const int *item_separators, const int *item_enabled, const int *item_checked, size_t item_count) {
|
|
1982
|
+
(void)host;
|
|
1983
|
+
(void)menu_titles;
|
|
1984
|
+
(void)menu_title_lens;
|
|
1985
|
+
(void)menu_count;
|
|
1986
|
+
(void)item_menu_indices;
|
|
1987
|
+
(void)item_labels;
|
|
1988
|
+
(void)item_label_lens;
|
|
1989
|
+
(void)item_commands;
|
|
1990
|
+
(void)item_command_lens;
|
|
1991
|
+
(void)item_keys;
|
|
1992
|
+
(void)item_key_lens;
|
|
1993
|
+
(void)item_modifiers;
|
|
1994
|
+
(void)item_separators;
|
|
1995
|
+
(void)item_enabled;
|
|
1996
|
+
(void)item_checked;
|
|
1997
|
+
(void)item_count;
|
|
1998
|
+
}
|
|
1999
|
+
|
|
2000
|
+
int native_sdk_appkit_create_view(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, int kind, const char *parent, size_t parent_len, double x, double y, double width, double height, int layer, int visible, int enabled, const char *role, size_t role_len, const char *accessibility_label, size_t accessibility_label_len, const char *text, size_t text_len, const char *command, size_t command_len) {
|
|
2001
|
+
(void)host;
|
|
2002
|
+
(void)window_id;
|
|
2003
|
+
(void)label;
|
|
2004
|
+
(void)label_len;
|
|
2005
|
+
(void)kind;
|
|
2006
|
+
(void)parent;
|
|
2007
|
+
(void)parent_len;
|
|
2008
|
+
(void)x;
|
|
2009
|
+
(void)y;
|
|
2010
|
+
(void)width;
|
|
2011
|
+
(void)height;
|
|
2012
|
+
(void)layer;
|
|
2013
|
+
(void)visible;
|
|
2014
|
+
(void)enabled;
|
|
2015
|
+
(void)role;
|
|
2016
|
+
(void)role_len;
|
|
2017
|
+
(void)accessibility_label;
|
|
2018
|
+
(void)accessibility_label_len;
|
|
2019
|
+
(void)text;
|
|
2020
|
+
(void)text_len;
|
|
2021
|
+
(void)command;
|
|
2022
|
+
(void)command_len;
|
|
2023
|
+
return 0;
|
|
2024
|
+
}
|
|
2025
|
+
|
|
2026
|
+
int native_sdk_appkit_update_view(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, int has_frame, double x, double y, double width, double height, int has_layer, int layer, int has_visible, int visible, int has_enabled, int enabled, int has_role, const char *role, size_t role_len, int has_accessibility_label, const char *accessibility_label, size_t accessibility_label_len, int has_text, const char *text, size_t text_len, int has_command, const char *command, size_t command_len) {
|
|
2027
|
+
(void)host;
|
|
2028
|
+
(void)window_id;
|
|
2029
|
+
(void)label;
|
|
2030
|
+
(void)label_len;
|
|
2031
|
+
(void)has_frame;
|
|
2032
|
+
(void)x;
|
|
2033
|
+
(void)y;
|
|
2034
|
+
(void)width;
|
|
2035
|
+
(void)height;
|
|
2036
|
+
(void)has_layer;
|
|
2037
|
+
(void)layer;
|
|
2038
|
+
(void)has_visible;
|
|
2039
|
+
(void)visible;
|
|
2040
|
+
(void)has_enabled;
|
|
2041
|
+
(void)enabled;
|
|
2042
|
+
(void)has_role;
|
|
2043
|
+
(void)role;
|
|
2044
|
+
(void)role_len;
|
|
2045
|
+
(void)has_accessibility_label;
|
|
2046
|
+
(void)accessibility_label;
|
|
2047
|
+
(void)accessibility_label_len;
|
|
2048
|
+
(void)has_text;
|
|
2049
|
+
(void)text;
|
|
2050
|
+
(void)text_len;
|
|
2051
|
+
(void)has_command;
|
|
2052
|
+
(void)command;
|
|
2053
|
+
(void)command_len;
|
|
2054
|
+
return 0;
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
int native_sdk_appkit_set_view_frame(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, double x, double y, double width, double height) {
|
|
2058
|
+
(void)host;
|
|
2059
|
+
(void)window_id;
|
|
2060
|
+
(void)label;
|
|
2061
|
+
(void)label_len;
|
|
2062
|
+
(void)x;
|
|
2063
|
+
(void)y;
|
|
2064
|
+
(void)width;
|
|
2065
|
+
(void)height;
|
|
2066
|
+
return 0;
|
|
2067
|
+
}
|
|
2068
|
+
|
|
2069
|
+
int native_sdk_appkit_set_view_visible(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, int visible) {
|
|
2070
|
+
(void)host;
|
|
2071
|
+
(void)window_id;
|
|
2072
|
+
(void)label;
|
|
2073
|
+
(void)label_len;
|
|
2074
|
+
(void)visible;
|
|
2075
|
+
return 0;
|
|
2076
|
+
}
|
|
2077
|
+
|
|
2078
|
+
int native_sdk_appkit_set_view_cursor(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, int cursor) {
|
|
2079
|
+
// Native child views are system-engine-only (create_view above); no
|
|
2080
|
+
// view exists for the label, so this reports it like the other stubs.
|
|
2081
|
+
(void)host;
|
|
2082
|
+
(void)window_id;
|
|
2083
|
+
(void)label;
|
|
2084
|
+
(void)label_len;
|
|
2085
|
+
(void)cursor;
|
|
2086
|
+
return 0;
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
int native_sdk_appkit_focus_view(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len) {
|
|
2090
|
+
(void)host;
|
|
2091
|
+
(void)window_id;
|
|
2092
|
+
(void)label;
|
|
2093
|
+
(void)label_len;
|
|
2094
|
+
return 0;
|
|
2095
|
+
}
|
|
2096
|
+
|
|
2097
|
+
int native_sdk_appkit_close_view(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len) {
|
|
2098
|
+
(void)host;
|
|
2099
|
+
(void)window_id;
|
|
2100
|
+
(void)label;
|
|
2101
|
+
(void)label_len;
|
|
2102
|
+
return 0;
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
void native_sdk_appkit_set_shortcuts(native_sdk_appkit_host_t *host, const char *const *ids, const size_t *id_lens, const char *const *keys, const size_t *key_lens, const uint32_t *modifiers, size_t count) {
|
|
2106
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2107
|
+
[object setShortcutsWithIds:ids idLengths:id_lens keys:keys keyLengths:key_lens modifiers:modifiers count:count];
|
|
2108
|
+
}
|
|
2109
|
+
|
|
2110
|
+
int native_sdk_appkit_create_window(native_sdk_appkit_host_t *host, uint64_t window_id, const char *window_title, size_t window_title_len, const char *window_label, size_t window_label_len, double x, double y, double width, double height, int restore_frame, int resizable, int titlebar_style, int show_policy) {
|
|
2111
|
+
// Accepted for ABI parity; see native_sdk_appkit_create.
|
|
2112
|
+
(void)show_policy;
|
|
2113
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2114
|
+
NSString *titleString = window_title ? [[NSString alloc] initWithBytes:window_title length:window_title_len encoding:NSUTF8StringEncoding] : @"native-sdk";
|
|
2115
|
+
NSString *labelString = window_label ? [[NSString alloc] initWithBytes:window_label length:window_label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2116
|
+
if (![object createWindowWithId:window_id title:titleString ?: @"native-sdk" label:labelString ?: @"" x:x y:y width:width height:height restoreFrame:(restore_frame != 0) resizable:(resizable != 0) makeMain:NO]) return 0;
|
|
2117
|
+
NativeSdkApplyHiddenInsetTitlebar(object.windows[@(window_id)], titlebar_style, object.delegates[@(window_id)]);
|
|
2118
|
+
return 1;
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
int native_sdk_appkit_focus_window(native_sdk_appkit_host_t *host, uint64_t window_id) {
|
|
2122
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2123
|
+
if (!object.windows[@(window_id)]) return 0;
|
|
2124
|
+
[object focusWindowWithId:window_id];
|
|
2125
|
+
return 1;
|
|
2126
|
+
}
|
|
2127
|
+
|
|
2128
|
+
int native_sdk_appkit_close_window(native_sdk_appkit_host_t *host, uint64_t window_id) {
|
|
2129
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2130
|
+
if (!object.windows[@(window_id)]) return 0;
|
|
2131
|
+
[object closeWindowWithId:window_id];
|
|
2132
|
+
return 1;
|
|
2133
|
+
}
|
|
2134
|
+
|
|
2135
|
+
int native_sdk_appkit_minimize_window(native_sdk_appkit_host_t *host, uint64_t window_id) {
|
|
2136
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2137
|
+
NSWindow *window = object.windows[@(window_id)];
|
|
2138
|
+
if (!window) return 0;
|
|
2139
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
2140
|
+
[window miniaturize:nil];
|
|
2141
|
+
});
|
|
2142
|
+
return 1;
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
int native_sdk_appkit_start_window_drag(native_sdk_appkit_host_t *host, uint64_t window_id) {
|
|
2146
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2147
|
+
NSWindow *window = object.windows[@(window_id)];
|
|
2148
|
+
if (!window) return 0;
|
|
2149
|
+
NSEvent *event = NSApp.currentEvent;
|
|
2150
|
+
if (!event) return 1;
|
|
2151
|
+
if (event.type != NSEventTypeLeftMouseDown && event.type != NSEventTypeLeftMouseDragged) return 1;
|
|
2152
|
+
if (event.type == NSEventTypeLeftMouseDown && event.clickCount >= 2) {
|
|
2153
|
+
NSString *action = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleActionOnDoubleClick"] ?: @"Maximize";
|
|
2154
|
+
if ([action isEqualToString:@"Minimize"]) {
|
|
2155
|
+
[window performMiniaturize:nil];
|
|
2156
|
+
} else if (![action isEqualToString:@"None"]) {
|
|
2157
|
+
[window performZoom:nil];
|
|
2158
|
+
}
|
|
2159
|
+
return 1;
|
|
2160
|
+
}
|
|
2161
|
+
[window performWindowDragWithEvent:event];
|
|
2162
|
+
return 1;
|
|
2163
|
+
}
|
|
2164
|
+
|
|
2165
|
+
int native_sdk_appkit_window_chrome_insets(native_sdk_appkit_host_t *host, uint64_t window_id, double *top, double *left, double *bottom, double *right, double *buttons_x, double *buttons_y, double *buttons_width, double *buttons_height) {
|
|
2166
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2167
|
+
NSWindow *window = object.windows[@(window_id)];
|
|
2168
|
+
if (!window) return 0;
|
|
2169
|
+
*top = 0;
|
|
2170
|
+
*left = 0;
|
|
2171
|
+
*bottom = 0;
|
|
2172
|
+
*right = 0;
|
|
2173
|
+
*buttons_x = 0;
|
|
2174
|
+
*buttons_y = 0;
|
|
2175
|
+
*buttons_width = 0;
|
|
2176
|
+
*buttons_height = 0;
|
|
2177
|
+
if ((window.styleMask & NSWindowStyleMaskFullSizeContentView) == 0) return 1;
|
|
2178
|
+
NSView *contentView = window.contentView;
|
|
2179
|
+
if (!contentView) return 1;
|
|
2180
|
+
NSRect contentBounds = contentView.bounds;
|
|
2181
|
+
NSRect layoutRect = [contentView convertRect:window.contentLayoutRect fromView:nil];
|
|
2182
|
+
double titlebarHeight = NSMaxY(contentBounds) - NSMaxY(layoutRect);
|
|
2183
|
+
if (titlebarHeight <= 0.5) return 1;
|
|
2184
|
+
*top = titlebarHeight;
|
|
2185
|
+
NSButton *buttons[3] = {
|
|
2186
|
+
[window standardWindowButton:NSWindowCloseButton],
|
|
2187
|
+
[window standardWindowButton:NSWindowMiniaturizeButton],
|
|
2188
|
+
[window standardWindowButton:NSWindowZoomButton],
|
|
2189
|
+
};
|
|
2190
|
+
NSRect cluster = NSZeroRect;
|
|
2191
|
+
BOOL anyButtonVisible = NO;
|
|
2192
|
+
for (size_t index = 0; index < 3; index += 1) {
|
|
2193
|
+
NSButton *button = buttons[index];
|
|
2194
|
+
if (!button || button.hidden || !button.superview) continue;
|
|
2195
|
+
NSRect buttonFrame = [contentView convertRect:button.frame fromView:button.superview];
|
|
2196
|
+
cluster = anyButtonVisible ? NSUnionRect(cluster, buttonFrame) : buttonFrame;
|
|
2197
|
+
anyButtonVisible = YES;
|
|
2198
|
+
}
|
|
2199
|
+
if (!anyButtonVisible) return 1;
|
|
2200
|
+
*buttons_x = NSMinX(cluster);
|
|
2201
|
+
*buttons_y = NSMaxY(contentBounds) - NSMaxY(cluster);
|
|
2202
|
+
*buttons_width = NSWidth(cluster);
|
|
2203
|
+
*buttons_height = NSHeight(cluster);
|
|
2204
|
+
if (NSMinX(cluster) < NSMidX(contentBounds)) {
|
|
2205
|
+
*left = NSMaxX(cluster) + (NSMinX(cluster) - NSMinX(contentBounds));
|
|
2206
|
+
} else {
|
|
2207
|
+
*right = (NSMaxX(contentBounds) - NSMinX(cluster)) + (NSMaxX(contentBounds) - NSMaxX(cluster));
|
|
2208
|
+
}
|
|
2209
|
+
return 1;
|
|
2210
|
+
}
|
|
2211
|
+
|
|
2212
|
+
int native_sdk_appkit_create_webview(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, const char *url, size_t url_len, double x, double y, double width, double height, int layer, int transparent, int bridge_enabled) {
|
|
2213
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2214
|
+
NSString *labelString = label ? [[NSString alloc] initWithBytes:label length:label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2215
|
+
NSString *urlString = url ? [[NSString alloc] initWithBytes:url length:url_len encoding:NSUTF8StringEncoding] : @"";
|
|
2216
|
+
return [object createWebViewInWindow:window_id label:labelString ?: @"" url:urlString ?: @"" x:x y:y width:width height:height layer:layer transparent:transparent != 0 bridgeEnabled:bridge_enabled != 0] ? 1 : 0;
|
|
2217
|
+
}
|
|
2218
|
+
|
|
2219
|
+
int native_sdk_appkit_set_webview_frame(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, double x, double y, double width, double height) {
|
|
2220
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2221
|
+
NSString *labelString = label ? [[NSString alloc] initWithBytes:label length:label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2222
|
+
return [object setWebViewFrameInWindow:window_id label:labelString ?: @"" x:x y:y width:width height:height] ? 1 : 0;
|
|
2223
|
+
}
|
|
2224
|
+
|
|
2225
|
+
int native_sdk_appkit_navigate_webview(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, const char *url, size_t url_len) {
|
|
2226
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2227
|
+
NSString *labelString = label ? [[NSString alloc] initWithBytes:label length:label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2228
|
+
NSString *urlString = url ? [[NSString alloc] initWithBytes:url length:url_len encoding:NSUTF8StringEncoding] : @"";
|
|
2229
|
+
return [object navigateWebViewInWindow:window_id label:labelString ?: @"" url:urlString ?: @""] ? 1 : 0;
|
|
2230
|
+
}
|
|
2231
|
+
|
|
2232
|
+
int native_sdk_appkit_set_webview_zoom(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, double zoom) {
|
|
2233
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2234
|
+
NSString *labelString = label ? [[NSString alloc] initWithBytes:label length:label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2235
|
+
return [object setWebViewZoomInWindow:window_id label:labelString ?: @"" zoom:zoom] ? 1 : 0;
|
|
2236
|
+
}
|
|
2237
|
+
|
|
2238
|
+
int native_sdk_appkit_set_webview_layer(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, int layer) {
|
|
2239
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2240
|
+
NSString *labelString = label ? [[NSString alloc] initWithBytes:label length:label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2241
|
+
return [object setWebViewLayerInWindow:window_id label:labelString ?: @"" layer:layer] ? 1 : 0;
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2244
|
+
int native_sdk_appkit_close_webview(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len) {
|
|
2245
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2246
|
+
NSString *labelString = label ? [[NSString alloc] initWithBytes:label length:label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2247
|
+
return [object closeWebViewInWindow:window_id label:labelString ?: @""] ? 1 : 0;
|
|
2248
|
+
}
|
|
2249
|
+
|
|
2250
|
+
void native_sdk_appkit_start_timer(native_sdk_appkit_host_t *host, uint64_t timer_id, uint64_t interval_ns, int repeats) {
|
|
2251
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2252
|
+
[object startAppTimerWithId:timer_id intervalNs:interval_ns repeats:(repeats != 0)];
|
|
2253
|
+
}
|
|
2254
|
+
|
|
2255
|
+
void native_sdk_appkit_cancel_timer(native_sdk_appkit_host_t *host, uint64_t timer_id) {
|
|
2256
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2257
|
+
[object cancelAppTimerWithId:timer_id];
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
void native_sdk_appkit_wake(native_sdk_appkit_host_t *host) {
|
|
2261
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2262
|
+
[object wakeFromAnyThread];
|
|
2263
|
+
}
|
|
2264
|
+
|
|
2265
|
+
/* Audio playback lives in the system-engine AppKit host (AVAudioPlayer).
|
|
2266
|
+
* The Chromium host reports the feature unsupported and the Zig side
|
|
2267
|
+
* refuses before calling, so these exist only to satisfy the shared C
|
|
2268
|
+
* ABI — each answers with its honest failure code. */
|
|
2269
|
+
int native_sdk_appkit_audio_load(native_sdk_appkit_host_t *host, const char *path, size_t path_len) {
|
|
2270
|
+
(void)host;
|
|
2271
|
+
(void)path;
|
|
2272
|
+
(void)path_len;
|
|
2273
|
+
return 2;
|
|
2274
|
+
}
|
|
2275
|
+
|
|
2276
|
+
int native_sdk_appkit_audio_load_url(native_sdk_appkit_host_t *host, const char *url, size_t url_len, const char *cache_path, size_t cache_path_len, uint64_t expected_bytes) {
|
|
2277
|
+
(void)host;
|
|
2278
|
+
(void)url;
|
|
2279
|
+
(void)url_len;
|
|
2280
|
+
(void)cache_path;
|
|
2281
|
+
(void)cache_path_len;
|
|
2282
|
+
(void)expected_bytes;
|
|
2283
|
+
return 2;
|
|
2284
|
+
}
|
|
2285
|
+
|
|
2286
|
+
int native_sdk_appkit_audio_play(native_sdk_appkit_host_t *host) {
|
|
2287
|
+
(void)host;
|
|
2288
|
+
return 0;
|
|
2289
|
+
}
|
|
2290
|
+
|
|
2291
|
+
int native_sdk_appkit_audio_pause(native_sdk_appkit_host_t *host) {
|
|
2292
|
+
(void)host;
|
|
2293
|
+
return 0;
|
|
2294
|
+
}
|
|
2295
|
+
|
|
2296
|
+
int native_sdk_appkit_audio_stop(native_sdk_appkit_host_t *host) {
|
|
2297
|
+
(void)host;
|
|
2298
|
+
return 0;
|
|
2299
|
+
}
|
|
2300
|
+
|
|
2301
|
+
int native_sdk_appkit_audio_seek(native_sdk_appkit_host_t *host, uint64_t position_ms) {
|
|
2302
|
+
(void)host;
|
|
2303
|
+
(void)position_ms;
|
|
2304
|
+
return 0;
|
|
2305
|
+
}
|
|
2306
|
+
|
|
2307
|
+
int native_sdk_appkit_audio_set_volume(native_sdk_appkit_host_t *host, double volume) {
|
|
2308
|
+
(void)host;
|
|
2309
|
+
(void)volume;
|
|
2310
|
+
return 0;
|
|
2311
|
+
}
|
|
2312
|
+
|
|
2313
|
+
void native_sdk_appkit_request_frame(native_sdk_appkit_host_t *host) {
|
|
2314
|
+
// The AppKit host pauses FRAME events when idle, so a cross-thread
|
|
2315
|
+
// frame request is how the automation arrival watcher wakes it. The
|
|
2316
|
+
// Chromium host pumps FRAME unconditionally at 60Hz from its
|
|
2317
|
+
// message-loop timer (see emitFrame), so the next tick is never more
|
|
2318
|
+
// than ~16 ms away and a request has nothing to add.
|
|
2319
|
+
(void)host;
|
|
2320
|
+
}
|
|
2321
|
+
|
|
2322
|
+
/* GPU-surface compositing (pixel/packet presents, image store, scroll
|
|
2323
|
+
* drivers, widget accessibility trees) is implemented by the system-engine
|
|
2324
|
+
* AppKit host only; the Chromium host renders every window through the web
|
|
2325
|
+
* engine and creates no gpu-surface views (see native_sdk_appkit_create_view
|
|
2326
|
+
* above). These report failure through the same channel as an unknown view
|
|
2327
|
+
* so callers see an explicit error instead of silently dropped frames. */
|
|
2328
|
+
int native_sdk_appkit_adopt_view_surface(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, void *ns_view) {
|
|
2329
|
+
(void)host;
|
|
2330
|
+
(void)window_id;
|
|
2331
|
+
(void)label;
|
|
2332
|
+
(void)label_len;
|
|
2333
|
+
(void)ns_view;
|
|
2334
|
+
return 0;
|
|
2335
|
+
}
|
|
2336
|
+
|
|
2337
|
+
int native_sdk_appkit_release_view_surface(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len) {
|
|
2338
|
+
(void)host;
|
|
2339
|
+
(void)window_id;
|
|
2340
|
+
(void)label;
|
|
2341
|
+
(void)label_len;
|
|
2342
|
+
return 0;
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2345
|
+
int native_sdk_appkit_request_gpu_surface_frame(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len) {
|
|
2346
|
+
(void)host;
|
|
2347
|
+
(void)window_id;
|
|
2348
|
+
(void)label;
|
|
2349
|
+
(void)label_len;
|
|
2350
|
+
return 0;
|
|
2351
|
+
}
|
|
2352
|
+
|
|
2353
|
+
int native_sdk_appkit_note_gpu_surface_input(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len) {
|
|
2354
|
+
(void)host;
|
|
2355
|
+
(void)window_id;
|
|
2356
|
+
(void)label;
|
|
2357
|
+
(void)label_len;
|
|
2358
|
+
return 0;
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
int native_sdk_appkit_set_gpu_surface_scroll_drivers(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, const native_sdk_appkit_scroll_driver_t *drivers, size_t count) {
|
|
2362
|
+
(void)host;
|
|
2363
|
+
(void)window_id;
|
|
2364
|
+
(void)label;
|
|
2365
|
+
(void)label_len;
|
|
2366
|
+
(void)drivers;
|
|
2367
|
+
(void)count;
|
|
2368
|
+
return 0;
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
int native_sdk_appkit_present_gpu_surface_pixels(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, size_t width, size_t height, double scale, int has_dirty_rect, double dirty_x, double dirty_y, double dirty_width, double dirty_height, const uint8_t *rgba8, size_t rgba8_len) {
|
|
2372
|
+
(void)host;
|
|
2373
|
+
(void)window_id;
|
|
2374
|
+
(void)label;
|
|
2375
|
+
(void)label_len;
|
|
2376
|
+
(void)width;
|
|
2377
|
+
(void)height;
|
|
2378
|
+
(void)scale;
|
|
2379
|
+
(void)has_dirty_rect;
|
|
2380
|
+
(void)dirty_x;
|
|
2381
|
+
(void)dirty_y;
|
|
2382
|
+
(void)dirty_width;
|
|
2383
|
+
(void)dirty_height;
|
|
2384
|
+
(void)rgba8;
|
|
2385
|
+
(void)rgba8_len;
|
|
2386
|
+
return 0;
|
|
2387
|
+
}
|
|
2388
|
+
|
|
2389
|
+
int native_sdk_appkit_present_gpu_surface_packet(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, double surface_width, double surface_height, double scale, uint8_t clear_r, uint8_t clear_g, uint8_t clear_b, uint8_t clear_a, int requires_render, size_t command_count, size_t unsupported_command_count, int representable, const uint8_t *json, size_t json_len) {
|
|
2390
|
+
(void)host;
|
|
2391
|
+
(void)window_id;
|
|
2392
|
+
(void)label;
|
|
2393
|
+
(void)label_len;
|
|
2394
|
+
(void)surface_width;
|
|
2395
|
+
(void)surface_height;
|
|
2396
|
+
(void)scale;
|
|
2397
|
+
(void)clear_r;
|
|
2398
|
+
(void)clear_g;
|
|
2399
|
+
(void)clear_b;
|
|
2400
|
+
(void)clear_a;
|
|
2401
|
+
(void)requires_render;
|
|
2402
|
+
(void)command_count;
|
|
2403
|
+
(void)unsupported_command_count;
|
|
2404
|
+
(void)representable;
|
|
2405
|
+
(void)json;
|
|
2406
|
+
(void)json_len;
|
|
2407
|
+
return 0;
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
int native_sdk_appkit_present_gpu_surface_packet_binary(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, double surface_width, double surface_height, double scale, uint8_t clear_r, uint8_t clear_g, uint8_t clear_b, uint8_t clear_a, int requires_render, size_t command_count, size_t unsupported_command_count, int representable, const uint8_t *packet, size_t packet_len) {
|
|
2411
|
+
(void)host;
|
|
2412
|
+
(void)window_id;
|
|
2413
|
+
(void)label;
|
|
2414
|
+
(void)label_len;
|
|
2415
|
+
(void)surface_width;
|
|
2416
|
+
(void)surface_height;
|
|
2417
|
+
(void)scale;
|
|
2418
|
+
(void)clear_r;
|
|
2419
|
+
(void)clear_g;
|
|
2420
|
+
(void)clear_b;
|
|
2421
|
+
(void)clear_a;
|
|
2422
|
+
(void)requires_render;
|
|
2423
|
+
(void)command_count;
|
|
2424
|
+
(void)unsupported_command_count;
|
|
2425
|
+
(void)representable;
|
|
2426
|
+
(void)packet;
|
|
2427
|
+
(void)packet_len;
|
|
2428
|
+
return 0;
|
|
2429
|
+
}
|
|
2430
|
+
|
|
2431
|
+
int native_sdk_appkit_upload_gpu_surface_image(native_sdk_appkit_host_t *host, uint64_t image_id, size_t width, size_t height, const uint8_t *rgba8, size_t rgba8_len) {
|
|
2432
|
+
(void)host;
|
|
2433
|
+
(void)image_id;
|
|
2434
|
+
(void)width;
|
|
2435
|
+
(void)height;
|
|
2436
|
+
(void)rgba8;
|
|
2437
|
+
(void)rgba8_len;
|
|
2438
|
+
return 0;
|
|
2439
|
+
}
|
|
2440
|
+
|
|
2441
|
+
int native_sdk_appkit_remove_gpu_surface_image(native_sdk_appkit_host_t *host, uint64_t image_id) {
|
|
2442
|
+
(void)host;
|
|
2443
|
+
(void)image_id;
|
|
2444
|
+
return 0;
|
|
2445
|
+
}
|
|
2446
|
+
|
|
2447
|
+
int native_sdk_appkit_update_widget_accessibility(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, const native_sdk_appkit_widget_accessibility_node_t *nodes, size_t node_count) {
|
|
2448
|
+
(void)host;
|
|
2449
|
+
(void)window_id;
|
|
2450
|
+
(void)label;
|
|
2451
|
+
(void)label_len;
|
|
2452
|
+
(void)nodes;
|
|
2453
|
+
(void)node_count;
|
|
2454
|
+
return 0;
|
|
2455
|
+
}
|
|
2456
|
+
|
|
2457
|
+
/* The Chromium host has no packet text renderer and reports no host text
|
|
2458
|
+
* metrics (see native_sdk_appkit_measure_text below), so a registered
|
|
2459
|
+
* face has no host-side consumer: the engine's font-aware provider
|
|
2460
|
+
* measures registered ids and the reference path inks them. Accept the
|
|
2461
|
+
* registration so `Options.fonts` apps start identically under both
|
|
2462
|
+
* hosts; refusing here would fail startup for a face this host never
|
|
2463
|
+
* resolves. */
|
|
2464
|
+
int native_sdk_appkit_register_font(uint64_t font_id, const uint8_t *bytes, size_t bytes_len) {
|
|
2465
|
+
if (font_id == 0 || !bytes || bytes_len == 0) return 0;
|
|
2466
|
+
return 1;
|
|
2467
|
+
}
|
|
2468
|
+
|
|
2469
|
+
/* The Chromium host has no packet text renderer, so there are no host
|
|
2470
|
+
* metrics to match: return the documented negative sentinel and the canvas
|
|
2471
|
+
* provider uses its estimator (the same fallback the AppKit host takes for
|
|
2472
|
+
* invalid UTF-8). */
|
|
2473
|
+
double native_sdk_appkit_measure_text(uint64_t font_id, double size, const char *text, size_t text_len) {
|
|
2474
|
+
(void)font_id;
|
|
2475
|
+
(void)size;
|
|
2476
|
+
(void)text;
|
|
2477
|
+
(void)text_len;
|
|
2478
|
+
return -1;
|
|
2479
|
+
}
|
|
2480
|
+
|
|
2481
|
+
/* Batched advances twin of the decline above: no host metrics means no
|
|
2482
|
+
* host advances either. Returning 0 declines the batch, and the engine
|
|
2483
|
+
* takes the same estimator fallback the per-prefix seam takes — the
|
|
2484
|
+
* decline route is pinned by the text batch parity tests. */
|
|
2485
|
+
int native_sdk_appkit_measure_text_advances(uint64_t font_id, double size, const char *text, size_t text_len, float *advances) {
|
|
2486
|
+
(void)font_id;
|
|
2487
|
+
(void)size;
|
|
2488
|
+
(void)text;
|
|
2489
|
+
(void)text_len;
|
|
2490
|
+
(void)advances;
|
|
2491
|
+
return 0;
|
|
2492
|
+
}
|
|
2493
|
+
|
|
2494
|
+
/* Mirror of the AppKit host's decoder: pure CoreGraphics/ImageIO with no
|
|
2495
|
+
* host state, so both engines decode identically. See appkit_host.h for
|
|
2496
|
+
* the pixel-format and return-value contract. */
|
|
2497
|
+
int native_sdk_appkit_decode_image(const uint8_t *bytes, size_t bytes_len, uint8_t *pixels, size_t pixels_len, size_t *out_width, size_t *out_height) {
|
|
2498
|
+
if (out_width) *out_width = 0;
|
|
2499
|
+
if (out_height) *out_height = 0;
|
|
2500
|
+
if (!bytes || bytes_len == 0 || !pixels) return 0;
|
|
2501
|
+
@autoreleasepool {
|
|
2502
|
+
NSData *data = [NSData dataWithBytesNoCopy:(void *)bytes length:bytes_len freeWhenDone:NO];
|
|
2503
|
+
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
|
|
2504
|
+
if (!source) return 0;
|
|
2505
|
+
CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
|
|
2506
|
+
CFRelease(source);
|
|
2507
|
+
if (!image) return 0;
|
|
2508
|
+
|
|
2509
|
+
size_t width = CGImageGetWidth(image);
|
|
2510
|
+
size_t height = CGImageGetHeight(image);
|
|
2511
|
+
if (width == 0 || height == 0 || width > 8192 || height > 8192) {
|
|
2512
|
+
CGImageRelease(image);
|
|
2513
|
+
return 0;
|
|
2514
|
+
}
|
|
2515
|
+
if (out_width) *out_width = width;
|
|
2516
|
+
if (out_height) *out_height = height;
|
|
2517
|
+
size_t byte_len = width * height * 4;
|
|
2518
|
+
if (byte_len / 4 / height != width || pixels_len < byte_len) {
|
|
2519
|
+
CGImageRelease(image);
|
|
2520
|
+
return -1;
|
|
2521
|
+
}
|
|
2522
|
+
|
|
2523
|
+
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
|
|
2524
|
+
if (!color_space) {
|
|
2525
|
+
CGImageRelease(image);
|
|
2526
|
+
return 0;
|
|
2527
|
+
}
|
|
2528
|
+
CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, width * 4, color_space, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
|
2529
|
+
CGColorSpaceRelease(color_space);
|
|
2530
|
+
if (!context) {
|
|
2531
|
+
CGImageRelease(image);
|
|
2532
|
+
return 0;
|
|
2533
|
+
}
|
|
2534
|
+
memset(pixels, 0, byte_len);
|
|
2535
|
+
CGContextSetBlendMode(context, kCGBlendModeCopy);
|
|
2536
|
+
CGContextDrawImage(context, CGRectMake(0, 0, (CGFloat)width, (CGFloat)height), image);
|
|
2537
|
+
CGContextRelease(context);
|
|
2538
|
+
CGImageRelease(image);
|
|
2539
|
+
|
|
2540
|
+
// Un-premultiply: round to nearest so opaque pixels survive exactly.
|
|
2541
|
+
for (size_t offset = 0; offset < byte_len; offset += 4) {
|
|
2542
|
+
uint8_t alpha = pixels[offset + 3];
|
|
2543
|
+
if (alpha == 0) {
|
|
2544
|
+
pixels[offset + 0] = 0;
|
|
2545
|
+
pixels[offset + 1] = 0;
|
|
2546
|
+
pixels[offset + 2] = 0;
|
|
2547
|
+
} else if (alpha != 255) {
|
|
2548
|
+
pixels[offset + 0] = (uint8_t)MIN(255, ((size_t)pixels[offset + 0] * 255 + alpha / 2) / alpha);
|
|
2549
|
+
pixels[offset + 1] = (uint8_t)MIN(255, ((size_t)pixels[offset + 1] * 255 + alpha / 2) / alpha);
|
|
2550
|
+
pixels[offset + 2] = (uint8_t)MIN(255, ((size_t)pixels[offset + 2] * 255 + alpha / 2) / alpha);
|
|
2551
|
+
}
|
|
2552
|
+
}
|
|
2553
|
+
return 1;
|
|
2554
|
+
}
|
|
2555
|
+
}
|
|
2556
|
+
|
|
2557
|
+
size_t native_sdk_appkit_clipboard_read(native_sdk_appkit_host_t *host, char *buffer, size_t buffer_len) {
|
|
2558
|
+
return native_sdk_appkit_clipboard_read_data(host, "text/plain", strlen("text/plain"), buffer, buffer_len);
|
|
2559
|
+
}
|
|
2560
|
+
|
|
2561
|
+
void native_sdk_appkit_clipboard_write(native_sdk_appkit_host_t *host, const char *text, size_t text_len) {
|
|
2562
|
+
(void)native_sdk_appkit_clipboard_write_data(host, "text/plain", strlen("text/plain"), text, text_len);
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
size_t native_sdk_appkit_clipboard_read_data(native_sdk_appkit_host_t *host, const char *mime_type, size_t mime_type_len, char *buffer, size_t buffer_len) {
|
|
2566
|
+
(void)host;
|
|
2567
|
+
NSString *type = NativeSdkPasteboardTypeForMime(mime_type, mime_type_len);
|
|
2568
|
+
if (!type || !buffer) return 0;
|
|
2569
|
+
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
|
|
2570
|
+
NSData *data = nil;
|
|
2571
|
+
if ([type isEqualToString:NSPasteboardTypeString] || [type isEqualToString:NSPasteboardTypeHTML]) {
|
|
2572
|
+
NSString *value = [pasteboard stringForType:type] ?: @"";
|
|
2573
|
+
data = [value dataUsingEncoding:NSUTF8StringEncoding];
|
|
2574
|
+
} else {
|
|
2575
|
+
data = [pasteboard dataForType:type] ?: [NSData data];
|
|
2576
|
+
}
|
|
2577
|
+
if (data.length > buffer_len) return data.length;
|
|
2578
|
+
size_t count = data.length;
|
|
2579
|
+
memcpy(buffer, data.bytes, count);
|
|
2580
|
+
return count;
|
|
2581
|
+
}
|
|
2582
|
+
|
|
2583
|
+
int native_sdk_appkit_clipboard_write_data(native_sdk_appkit_host_t *host, const char *mime_type, size_t mime_type_len, const char *bytes, size_t bytes_len) {
|
|
2584
|
+
(void)host;
|
|
2585
|
+
NSString *type = NativeSdkPasteboardTypeForMime(mime_type, mime_type_len);
|
|
2586
|
+
if (!type || (!bytes && bytes_len > 0)) return 0;
|
|
2587
|
+
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
|
|
2588
|
+
[pasteboard clearContents];
|
|
2589
|
+
if ([type isEqualToString:NSPasteboardTypeString] || [type isEqualToString:NSPasteboardTypeHTML]) {
|
|
2590
|
+
NSString *value = [[NSString alloc] initWithBytes:bytes length:bytes_len encoding:NSUTF8StringEncoding] ?: @"";
|
|
2591
|
+
return [pasteboard setString:value forType:type] ? 1 : 0;
|
|
2592
|
+
}
|
|
2593
|
+
NSData *data = [NSData dataWithBytes:bytes length:bytes_len];
|
|
2594
|
+
return [pasteboard setData:data forType:type] ? 1 : 0;
|
|
2595
|
+
}
|
|
2596
|
+
|
|
2597
|
+
int native_sdk_appkit_show_notification(native_sdk_appkit_host_t *host, const char *title, size_t title_len, const char *subtitle, size_t subtitle_len, const char *body, size_t body_len) {
|
|
2598
|
+
(void)host;
|
|
2599
|
+
NSString *titleString = title ? [[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] : @"";
|
|
2600
|
+
if (titleString.length == 0) return 0;
|
|
2601
|
+
NSString *subtitleString = subtitle ? [[NSString alloc] initWithBytes:subtitle length:subtitle_len encoding:NSUTF8StringEncoding] : @"";
|
|
2602
|
+
NSString *bodyString = body ? [[NSString alloc] initWithBytes:body length:body_len encoding:NSUTF8StringEncoding] : @"";
|
|
2603
|
+
NSUserNotification *notification = [[NSUserNotification alloc] init];
|
|
2604
|
+
notification.title = titleString;
|
|
2605
|
+
if (subtitleString.length > 0) notification.subtitle = subtitleString;
|
|
2606
|
+
if (bodyString.length > 0) notification.informativeText = bodyString;
|
|
2607
|
+
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
|
|
2608
|
+
return 1;
|
|
2609
|
+
}
|
|
2610
|
+
|
|
2611
|
+
int native_sdk_appkit_open_external_url(native_sdk_appkit_host_t *host, const char *url, size_t url_len) {
|
|
2612
|
+
(void)host;
|
|
2613
|
+
NSString *urlString = url ? [[NSString alloc] initWithBytes:url length:url_len encoding:NSUTF8StringEncoding] : @"";
|
|
2614
|
+
if (urlString.length == 0) return 0;
|
|
2615
|
+
NSURL *target = [NSURL URLWithString:urlString];
|
|
2616
|
+
if (!target || target.scheme.length == 0) return 0;
|
|
2617
|
+
return [[NSWorkspace sharedWorkspace] openURL:target] ? 1 : 0;
|
|
2618
|
+
}
|
|
2619
|
+
|
|
2620
|
+
int native_sdk_appkit_reveal_path(native_sdk_appkit_host_t *host, const char *path, size_t path_len) {
|
|
2621
|
+
(void)host;
|
|
2622
|
+
NSString *pathString = path ? [[NSString alloc] initWithBytes:path length:path_len encoding:NSUTF8StringEncoding] : @"";
|
|
2623
|
+
if (pathString.length == 0) return 0;
|
|
2624
|
+
NSURL *fileURL = [NSURL fileURLWithPath:pathString];
|
|
2625
|
+
if (!fileURL) return 0;
|
|
2626
|
+
[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[ fileURL ]];
|
|
2627
|
+
return 1;
|
|
2628
|
+
}
|
|
2629
|
+
|
|
2630
|
+
int native_sdk_appkit_add_recent_document(native_sdk_appkit_host_t *host, const char *path, size_t path_len) {
|
|
2631
|
+
(void)host;
|
|
2632
|
+
NSString *pathString = path ? [[NSString alloc] initWithBytes:path length:path_len encoding:NSUTF8StringEncoding] : @"";
|
|
2633
|
+
if (pathString.length == 0) return 0;
|
|
2634
|
+
NSURL *fileURL = [NSURL fileURLWithPath:pathString];
|
|
2635
|
+
if (!fileURL) return 0;
|
|
2636
|
+
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:fileURL];
|
|
2637
|
+
return 1;
|
|
2638
|
+
}
|
|
2639
|
+
|
|
2640
|
+
int native_sdk_appkit_clear_recent_documents(native_sdk_appkit_host_t *host) {
|
|
2641
|
+
(void)host;
|
|
2642
|
+
[[NSDocumentController sharedDocumentController] clearRecentDocuments:nil];
|
|
2643
|
+
return 1;
|
|
2644
|
+
}
|
|
2645
|
+
|
|
2646
|
+
int native_sdk_appkit_set_credential(native_sdk_appkit_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len, const char *secret, size_t secret_len) {
|
|
2647
|
+
(void)host;
|
|
2648
|
+
@autoreleasepool {
|
|
2649
|
+
NSString *serviceString = NativeSdkStringFromBytes(service, service_len);
|
|
2650
|
+
NSString *accountString = NativeSdkStringFromBytes(account, account_len);
|
|
2651
|
+
if (serviceString.length == 0 || accountString.length == 0 || !secret || secret_len == 0) return 0;
|
|
2652
|
+
NSData *secretData = [NSData dataWithBytes:secret length:secret_len];
|
|
2653
|
+
NSMutableDictionary *query = NativeSdkCredentialQuery(serviceString, accountString);
|
|
2654
|
+
NSDictionary *update = @{ (__bridge id)kSecValueData: secretData };
|
|
2655
|
+
OSStatus status = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)update);
|
|
2656
|
+
if (status == errSecItemNotFound) {
|
|
2657
|
+
query[(__bridge id)kSecValueData] = secretData;
|
|
2658
|
+
status = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
|
|
2659
|
+
}
|
|
2660
|
+
return status == errSecSuccess ? 1 : 0;
|
|
2661
|
+
}
|
|
2662
|
+
}
|
|
2663
|
+
|
|
2664
|
+
size_t native_sdk_appkit_get_credential(native_sdk_appkit_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len, char *buffer, size_t buffer_len) {
|
|
2665
|
+
(void)host;
|
|
2666
|
+
@autoreleasepool {
|
|
2667
|
+
NSString *serviceString = NativeSdkStringFromBytes(service, service_len);
|
|
2668
|
+
NSString *accountString = NativeSdkStringFromBytes(account, account_len);
|
|
2669
|
+
if (serviceString.length == 0 || accountString.length == 0 || !buffer) return 0;
|
|
2670
|
+
NSMutableDictionary *query = NativeSdkCredentialQuery(serviceString, accountString);
|
|
2671
|
+
query[(__bridge id)kSecReturnData] = @YES;
|
|
2672
|
+
query[(__bridge id)kSecMatchLimit] = (__bridge id)kSecMatchLimitOne;
|
|
2673
|
+
CFTypeRef result = NULL;
|
|
2674
|
+
OSStatus status = SecItemCopyMatching((__bridge CFDictionaryRef)query, &result);
|
|
2675
|
+
if (status != errSecSuccess || !result) return 0;
|
|
2676
|
+
NSData *data = CFBridgingRelease(result);
|
|
2677
|
+
if (data.length > buffer_len) return data.length;
|
|
2678
|
+
memcpy(buffer, data.bytes, data.length);
|
|
2679
|
+
return data.length;
|
|
2680
|
+
}
|
|
2681
|
+
}
|
|
2682
|
+
|
|
2683
|
+
int native_sdk_appkit_delete_credential(native_sdk_appkit_host_t *host, const char *service, size_t service_len, const char *account, size_t account_len) {
|
|
2684
|
+
(void)host;
|
|
2685
|
+
@autoreleasepool {
|
|
2686
|
+
NSString *serviceString = NativeSdkStringFromBytes(service, service_len);
|
|
2687
|
+
NSString *accountString = NativeSdkStringFromBytes(account, account_len);
|
|
2688
|
+
if (serviceString.length == 0 || accountString.length == 0) return 0;
|
|
2689
|
+
NSMutableDictionary *query = NativeSdkCredentialQuery(serviceString, accountString);
|
|
2690
|
+
OSStatus status = SecItemDelete((__bridge CFDictionaryRef)query);
|
|
2691
|
+
return status == errSecSuccess ? 1 : 0;
|
|
2692
|
+
}
|
|
2693
|
+
}
|
|
2694
|
+
|
|
2695
|
+
static NSArray<NSString *> *NativeSdkParseExtensions(const char *extensions, size_t len) {
|
|
2696
|
+
if (!extensions || len == 0) return nil;
|
|
2697
|
+
NSString *str = [[NSString alloc] initWithBytes:extensions length:len encoding:NSUTF8StringEncoding];
|
|
2698
|
+
if (!str || str.length == 0) return nil;
|
|
2699
|
+
NSMutableArray<NSString *> *result = [NSMutableArray array];
|
|
2700
|
+
for (NSString *ext in [str componentsSeparatedByString:@";"]) {
|
|
2701
|
+
NSString *trimmed = [ext stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
|
|
2702
|
+
if (trimmed.length > 0) [result addObject:trimmed];
|
|
2703
|
+
}
|
|
2704
|
+
return result.count > 0 ? result : nil;
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2707
|
+
static size_t NativeSdkOverflowSize(size_t buffer_len) {
|
|
2708
|
+
return buffer_len == SIZE_MAX ? SIZE_MAX : buffer_len + 1;
|
|
2709
|
+
}
|
|
2710
|
+
|
|
2711
|
+
static void NativeSdkConfigurePanelExtensions(NSSavePanel *panel, NSArray<NSString *> *extensions) {
|
|
2712
|
+
if (!extensions || extensions.count == 0) return;
|
|
2713
|
+
if (@available(macOS 11.0, *)) {
|
|
2714
|
+
NSMutableArray *types = [NSMutableArray array];
|
|
2715
|
+
for (NSString *ext in extensions) {
|
|
2716
|
+
UTType *type = [UTType typeWithFilenameExtension:ext];
|
|
2717
|
+
if (type) [types addObject:type];
|
|
2718
|
+
}
|
|
2719
|
+
if (types.count > 0) panel.allowedContentTypes = types;
|
|
2720
|
+
}
|
|
2721
|
+
}
|
|
2722
|
+
|
|
2723
|
+
native_sdk_appkit_open_dialog_result_t native_sdk_appkit_show_open_dialog(native_sdk_appkit_host_t *host, const native_sdk_appkit_open_dialog_opts_t *opts, char *buffer, size_t buffer_len) {
|
|
2724
|
+
(void)host;
|
|
2725
|
+
native_sdk_appkit_open_dialog_result_t result = { .count = 0, .bytes_written = 0 };
|
|
2726
|
+
@autoreleasepool {
|
|
2727
|
+
NSOpenPanel *panel = [NSOpenPanel openPanel];
|
|
2728
|
+
if (opts->title && opts->title_len > 0) {
|
|
2729
|
+
panel.title = [[NSString alloc] initWithBytes:opts->title length:opts->title_len encoding:NSUTF8StringEncoding];
|
|
2730
|
+
}
|
|
2731
|
+
if (opts->default_path && opts->default_path_len > 0) {
|
|
2732
|
+
NSString *path = [[NSString alloc] initWithBytes:opts->default_path length:opts->default_path_len encoding:NSUTF8StringEncoding];
|
|
2733
|
+
panel.directoryURL = [NSURL fileURLWithPath:path];
|
|
2734
|
+
}
|
|
2735
|
+
panel.canChooseFiles = YES;
|
|
2736
|
+
panel.canChooseDirectories = opts->allow_directories != 0;
|
|
2737
|
+
panel.allowsMultipleSelection = opts->allow_multiple != 0;
|
|
2738
|
+
NativeSdkConfigurePanelExtensions(panel, NativeSdkParseExtensions(opts->extensions, opts->extensions_len));
|
|
2739
|
+
|
|
2740
|
+
if ([panel runModal] != NSModalResponseOK) return result;
|
|
2741
|
+
|
|
2742
|
+
size_t offset = 0;
|
|
2743
|
+
BOOL overflow = NO;
|
|
2744
|
+
for (NSURL *url in panel.URLs) {
|
|
2745
|
+
NSString *path = url.path;
|
|
2746
|
+
NSData *data = [path dataUsingEncoding:NSUTF8StringEncoding];
|
|
2747
|
+
if (!data) continue;
|
|
2748
|
+
size_t needed = data.length + (result.count > 0 ? 1 : 0);
|
|
2749
|
+
if (needed > buffer_len - offset) {
|
|
2750
|
+
overflow = YES;
|
|
2751
|
+
break;
|
|
2752
|
+
}
|
|
2753
|
+
if (result.count > 0) { buffer[offset] = '\n'; offset++; }
|
|
2754
|
+
memcpy(buffer + offset, data.bytes, data.length);
|
|
2755
|
+
offset += data.length;
|
|
2756
|
+
result.count++;
|
|
2757
|
+
}
|
|
2758
|
+
result.bytes_written = overflow ? NativeSdkOverflowSize(buffer_len) : offset;
|
|
2759
|
+
}
|
|
2760
|
+
return result;
|
|
2761
|
+
}
|
|
2762
|
+
|
|
2763
|
+
size_t native_sdk_appkit_show_save_dialog(native_sdk_appkit_host_t *host, const native_sdk_appkit_save_dialog_opts_t *opts, char *buffer, size_t buffer_len) {
|
|
2764
|
+
(void)host;
|
|
2765
|
+
@autoreleasepool {
|
|
2766
|
+
NSSavePanel *panel = [NSSavePanel savePanel];
|
|
2767
|
+
if (opts->title && opts->title_len > 0) {
|
|
2768
|
+
panel.title = [[NSString alloc] initWithBytes:opts->title length:opts->title_len encoding:NSUTF8StringEncoding];
|
|
2769
|
+
}
|
|
2770
|
+
if (opts->default_path && opts->default_path_len > 0) {
|
|
2771
|
+
NSString *path = [[NSString alloc] initWithBytes:opts->default_path length:opts->default_path_len encoding:NSUTF8StringEncoding];
|
|
2772
|
+
panel.directoryURL = [NSURL fileURLWithPath:path];
|
|
2773
|
+
}
|
|
2774
|
+
if (opts->default_name && opts->default_name_len > 0) {
|
|
2775
|
+
panel.nameFieldStringValue = [[NSString alloc] initWithBytes:opts->default_name length:opts->default_name_len encoding:NSUTF8StringEncoding];
|
|
2776
|
+
}
|
|
2777
|
+
NativeSdkConfigurePanelExtensions(panel, NativeSdkParseExtensions(opts->extensions, opts->extensions_len));
|
|
2778
|
+
|
|
2779
|
+
if ([panel runModal] != NSModalResponseOK) return 0;
|
|
2780
|
+
|
|
2781
|
+
NSString *path = panel.URL.path;
|
|
2782
|
+
NSData *data = [path dataUsingEncoding:NSUTF8StringEncoding];
|
|
2783
|
+
if (!data) return 0;
|
|
2784
|
+
size_t count = data.length;
|
|
2785
|
+
if (count > buffer_len) return NativeSdkOverflowSize(buffer_len);
|
|
2786
|
+
memcpy(buffer, data.bytes, count);
|
|
2787
|
+
return count;
|
|
2788
|
+
}
|
|
2789
|
+
}
|
|
2790
|
+
|
|
2791
|
+
int native_sdk_appkit_show_message_dialog(native_sdk_appkit_host_t *host, const native_sdk_appkit_message_dialog_opts_t *opts) {
|
|
2792
|
+
(void)host;
|
|
2793
|
+
@autoreleasepool {
|
|
2794
|
+
NSAlert *alert = [[NSAlert alloc] init];
|
|
2795
|
+
switch (opts->style) {
|
|
2796
|
+
case 1: alert.alertStyle = NSAlertStyleWarning; break;
|
|
2797
|
+
case 2: alert.alertStyle = NSAlertStyleCritical; break;
|
|
2798
|
+
default: alert.alertStyle = NSAlertStyleInformational; break;
|
|
2799
|
+
}
|
|
2800
|
+
if (opts->title && opts->title_len > 0) {
|
|
2801
|
+
alert.messageText = [[NSString alloc] initWithBytes:opts->title length:opts->title_len encoding:NSUTF8StringEncoding];
|
|
2802
|
+
}
|
|
2803
|
+
if (opts->message && opts->message_len > 0) {
|
|
2804
|
+
alert.informativeText = [[NSString alloc] initWithBytes:opts->message length:opts->message_len encoding:NSUTF8StringEncoding];
|
|
2805
|
+
}
|
|
2806
|
+
if (opts->informative_text && opts->informative_text_len > 0) {
|
|
2807
|
+
alert.informativeText = [[NSString alloc] initWithBytes:opts->informative_text length:opts->informative_text_len encoding:NSUTF8StringEncoding];
|
|
2808
|
+
}
|
|
2809
|
+
if (opts->primary_button && opts->primary_button_len > 0) {
|
|
2810
|
+
[alert addButtonWithTitle:[[NSString alloc] initWithBytes:opts->primary_button length:opts->primary_button_len encoding:NSUTF8StringEncoding]];
|
|
2811
|
+
} else {
|
|
2812
|
+
[alert addButtonWithTitle:@"OK"];
|
|
2813
|
+
}
|
|
2814
|
+
if (opts->secondary_button && opts->secondary_button_len > 0) {
|
|
2815
|
+
[alert addButtonWithTitle:[[NSString alloc] initWithBytes:opts->secondary_button length:opts->secondary_button_len encoding:NSUTF8StringEncoding]];
|
|
2816
|
+
}
|
|
2817
|
+
if (opts->tertiary_button && opts->tertiary_button_len > 0) {
|
|
2818
|
+
[alert addButtonWithTitle:[[NSString alloc] initWithBytes:opts->tertiary_button length:opts->tertiary_button_len encoding:NSUTF8StringEncoding]];
|
|
2819
|
+
}
|
|
2820
|
+
|
|
2821
|
+
NSModalResponse response = [alert runModal];
|
|
2822
|
+
if (response == NSAlertFirstButtonReturn) return 0;
|
|
2823
|
+
if (response == NSAlertSecondButtonReturn) return 1;
|
|
2824
|
+
return 2;
|
|
2825
|
+
}
|
|
2826
|
+
}
|
|
2827
|
+
|
|
2828
|
+
void native_sdk_appkit_create_tray(native_sdk_appkit_host_t *host, const char *icon_path, size_t icon_path_len, const char *title, size_t title_len, const char *tooltip, size_t tooltip_len) {
|
|
2829
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2830
|
+
@autoreleasepool {
|
|
2831
|
+
if (object.statusItem) {
|
|
2832
|
+
[[NSStatusBar systemStatusBar] removeStatusItem:object.statusItem];
|
|
2833
|
+
}
|
|
2834
|
+
// A titled menu-bar extra needs variable width; icon-only status
|
|
2835
|
+
// items keep the classic square well.
|
|
2836
|
+
BOOL hasTitle = title != NULL && title_len > 0;
|
|
2837
|
+
object.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:hasTitle ? NSVariableStatusItemLength : NSSquareStatusItemLength];
|
|
2838
|
+
|
|
2839
|
+
if (icon_path && icon_path_len > 0) {
|
|
2840
|
+
NSString *path = [[NSString alloc] initWithBytes:icon_path length:icon_path_len encoding:NSUTF8StringEncoding];
|
|
2841
|
+
NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
|
|
2842
|
+
if (image) {
|
|
2843
|
+
[image setTemplate:YES];
|
|
2844
|
+
image.size = NSMakeSize(18, 18);
|
|
2845
|
+
object.statusItem.button.image = image;
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
if (hasTitle) {
|
|
2849
|
+
object.statusItem.button.title = [[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] ?: @"";
|
|
2850
|
+
}
|
|
2851
|
+
if (!object.statusItem.button.image && object.statusItem.button.title.length == 0) {
|
|
2852
|
+
object.statusItem.button.title = object.appName.length > 0 ? [object.appName substringToIndex:MIN(1, object.appName.length)] : @"Z";
|
|
2853
|
+
}
|
|
2854
|
+
if (tooltip && tooltip_len > 0) {
|
|
2855
|
+
object.statusItem.button.toolTip = [[NSString alloc] initWithBytes:tooltip length:tooltip_len encoding:NSUTF8StringEncoding];
|
|
2856
|
+
}
|
|
2857
|
+
}
|
|
2858
|
+
}
|
|
2859
|
+
|
|
2860
|
+
void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, const uint32_t *item_ids, const char *const *labels, const size_t *label_lens, const int *separators, const int *enabled_flags, size_t count) {
|
|
2861
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2862
|
+
@autoreleasepool {
|
|
2863
|
+
if (!object.statusItem) return;
|
|
2864
|
+
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
|
|
2865
|
+
for (size_t i = 0; i < count; i++) {
|
|
2866
|
+
if (separators[i]) {
|
|
2867
|
+
[menu addItem:[NSMenuItem separatorItem]];
|
|
2868
|
+
continue;
|
|
2869
|
+
}
|
|
2870
|
+
NSString *label = labels[i] ? [[NSString alloc] initWithBytes:labels[i] length:label_lens[i] encoding:NSUTF8StringEncoding] : @"";
|
|
2871
|
+
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:label ?: @""
|
|
2872
|
+
action:@selector(trayMenuItemClicked:)
|
|
2873
|
+
keyEquivalent:@""];
|
|
2874
|
+
item.tag = (NSInteger)item_ids[i];
|
|
2875
|
+
item.target = object;
|
|
2876
|
+
item.enabled = enabled_flags[i] != 0;
|
|
2877
|
+
[menu addItem:item];
|
|
2878
|
+
}
|
|
2879
|
+
object.statusItem.menu = menu;
|
|
2880
|
+
}
|
|
2881
|
+
}
|
|
2882
|
+
|
|
2883
|
+
int native_sdk_appkit_set_window_content_min_size(native_sdk_appkit_host_t *host, uint64_t window_id, double min_width, double min_height) {
|
|
2884
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2885
|
+
NSWindow *window = object.windows[@(window_id)];
|
|
2886
|
+
if (!window) return 0;
|
|
2887
|
+
// The declared floor is CONTENT size (matches the frame the runtime
|
|
2888
|
+
// reasons about); AppKit adds the chrome on top. Axes <= 0 keep
|
|
2889
|
+
// AppKit's default minimum for that axis.
|
|
2890
|
+
NSSize current = window.contentMinSize;
|
|
2891
|
+
window.contentMinSize = NSMakeSize(min_width > 0 ? min_width : current.width,
|
|
2892
|
+
min_height > 0 ? min_height : current.height);
|
|
2893
|
+
return 1;
|
|
2894
|
+
}
|
|
2895
|
+
|
|
2896
|
+
void native_sdk_appkit_update_tray_title(native_sdk_appkit_host_t *host, const char *title, size_t title_len) {
|
|
2897
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2898
|
+
@autoreleasepool {
|
|
2899
|
+
if (!object.statusItem) return;
|
|
2900
|
+
BOOL hasTitle = title != NULL && title_len > 0;
|
|
2901
|
+
NSString *value = hasTitle ? ([[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
|
|
2902
|
+
object.statusItem.button.title = value;
|
|
2903
|
+
if (!object.statusItem.button.image && value.length == 0) {
|
|
2904
|
+
// Same fallback as create: a bare status item must still show
|
|
2905
|
+
// SOMETHING to stay clickable.
|
|
2906
|
+
object.statusItem.button.title = object.appName.length > 0 ? [object.appName substringToIndex:MIN(1, object.appName.length)] : @"Z";
|
|
2907
|
+
}
|
|
2908
|
+
// Titled extras need variable width; icon-only ones keep the
|
|
2909
|
+
// classic square well (mirrors create's length choice).
|
|
2910
|
+
object.statusItem.length = object.statusItem.button.title.length > 0 ? NSVariableStatusItemLength : NSSquareStatusItemLength;
|
|
2911
|
+
}
|
|
2912
|
+
}
|
|
2913
|
+
|
|
2914
|
+
void native_sdk_appkit_remove_tray(native_sdk_appkit_host_t *host) {
|
|
2915
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2916
|
+
if (object.statusItem) {
|
|
2917
|
+
[[NSStatusBar systemStatusBar] removeStatusItem:object.statusItem];
|
|
2918
|
+
object.statusItem = nil;
|
|
2919
|
+
}
|
|
2920
|
+
}
|
|
2921
|
+
|
|
2922
|
+
/* Dock icon entry points, CEF engine: the Chromium shell is still a
|
|
2923
|
+
* macOS app with a Dock tile, so the dev-run icon path (including the
|
|
2924
|
+
* Debug-only masked render of raw image sources) applies here exactly
|
|
2925
|
+
* as under the system host. The rgba form copies the caller's
|
|
2926
|
+
* straight-alpha rows into a rep the image owns (the caller frees its
|
|
2927
|
+
* buffer on return); only the NSApp adoption needs the main queue. */
|
|
2928
|
+
void native_sdk_appkit_set_dock_icon_rgba(native_sdk_appkit_host_t *host, const uint8_t *pixels, size_t width, size_t height) {
|
|
2929
|
+
(void)host;
|
|
2930
|
+
if (!pixels || width == 0 || height == 0) return;
|
|
2931
|
+
@autoreleasepool {
|
|
2932
|
+
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
|
|
2933
|
+
pixelsWide:(NSInteger)width
|
|
2934
|
+
pixelsHigh:(NSInteger)height
|
|
2935
|
+
bitsPerSample:8
|
|
2936
|
+
samplesPerPixel:4
|
|
2937
|
+
hasAlpha:YES
|
|
2938
|
+
isPlanar:NO
|
|
2939
|
+
colorSpaceName:NSCalibratedRGBColorSpace
|
|
2940
|
+
bitmapFormat:NSBitmapFormatAlphaNonpremultiplied
|
|
2941
|
+
bytesPerRow:0
|
|
2942
|
+
bitsPerPixel:32];
|
|
2943
|
+
if (!rep || !rep.bitmapData) return;
|
|
2944
|
+
const size_t source_stride = width * 4;
|
|
2945
|
+
const size_t dest_stride = (size_t)rep.bytesPerRow;
|
|
2946
|
+
unsigned char *dest = rep.bitmapData;
|
|
2947
|
+
for (size_t y = 0; y < height; y += 1) {
|
|
2948
|
+
memcpy(dest + y * dest_stride, pixels + y * source_stride, source_stride);
|
|
2949
|
+
}
|
|
2950
|
+
NSImage *icon = [[NSImage alloc] initWithSize:NSMakeSize((CGFloat)width, (CGFloat)height)];
|
|
2951
|
+
[icon addRepresentation:rep];
|
|
2952
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
2953
|
+
[NSApp setApplicationIconImage:icon];
|
|
2954
|
+
});
|
|
2955
|
+
}
|
|
2956
|
+
}
|
|
2957
|
+
|
|
2958
|
+
void native_sdk_appkit_set_dock_icon_file(native_sdk_appkit_host_t *host, const char *path, size_t path_len) {
|
|
2959
|
+
(void)host;
|
|
2960
|
+
if (!path || path_len == 0) return;
|
|
2961
|
+
@autoreleasepool {
|
|
2962
|
+
NSString *pathString = [[NSString alloc] initWithBytes:path length:path_len encoding:NSUTF8StringEncoding];
|
|
2963
|
+
if (pathString.length == 0) return;
|
|
2964
|
+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
|
|
2965
|
+
NSImage *icon = [[NSImage alloc] initWithContentsOfFile:pathString];
|
|
2966
|
+
if (!icon) return;
|
|
2967
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
2968
|
+
[NSApp setApplicationIconImage:icon];
|
|
2969
|
+
});
|
|
2970
|
+
});
|
|
2971
|
+
}
|
|
2972
|
+
}
|
|
2973
|
+
|
|
2974
|
+
void native_sdk_appkit_set_tray_callback(native_sdk_appkit_host_t *host, native_sdk_appkit_tray_callback_t callback, void *context) {
|
|
2975
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2976
|
+
object.trayCallback = callback;
|
|
2977
|
+
object.trayContext = context;
|
|
2978
|
+
}
|
|
2979
|
+
|
|
2980
|
+
/* Native context menu, CEF engine: same NSMenu presentation as the
|
|
2981
|
+
* system-engine host, anchored to the window content view (gpu-surface
|
|
2982
|
+
* views are system-engine-only, so `label` resolves to the window). */
|
|
2983
|
+
int native_sdk_appkit_show_context_menu(native_sdk_appkit_host_t *host, uint64_t window_id, const char *label, size_t label_len, double x, double y, uint64_t token, const native_sdk_appkit_context_menu_item_t *items, size_t count) {
|
|
2984
|
+
NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
|
|
2985
|
+
NSWindow *window = object.windows[@(window_id)] ?: (window_id == 1 ? object.window : nil);
|
|
2986
|
+
NSView *view = window.contentView;
|
|
2987
|
+
if (!view || count == 0) return 0;
|
|
2988
|
+
|
|
2989
|
+
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
|
|
2990
|
+
menu.autoenablesItems = NO;
|
|
2991
|
+
NativeSdkChromiumContextMenuTarget *target = [[NativeSdkChromiumContextMenuTarget alloc] init];
|
|
2992
|
+
for (size_t index = 0; index < count; index += 1) {
|
|
2993
|
+
const native_sdk_appkit_context_menu_item_t item = items[index];
|
|
2994
|
+
if (item.separator) {
|
|
2995
|
+
[menu addItem:[NSMenuItem separatorItem]];
|
|
2996
|
+
continue;
|
|
2997
|
+
}
|
|
2998
|
+
NSString *title = item.label ? [[NSString alloc] initWithBytes:item.label length:item.label_len encoding:NSUTF8StringEncoding] : @"";
|
|
2999
|
+
NSMenuItem *menuItem = [[NSMenuItem alloc] initWithTitle:title ?: @"" action:@selector(contextMenuItemClicked:) keyEquivalent:@""];
|
|
3000
|
+
menuItem.target = target;
|
|
3001
|
+
menuItem.enabled = item.enabled != 0;
|
|
3002
|
+
menuItem.representedObject = @(item.item_id);
|
|
3003
|
+
[menu addItem:menuItem];
|
|
3004
|
+
}
|
|
3005
|
+
|
|
3006
|
+
NSString *eventLabel = label ? [[NSString alloc] initWithBytes:label length:label_len encoding:NSUTF8StringEncoding] : @"";
|
|
3007
|
+
NSPoint location = NSMakePoint(x, view.isFlipped ? y : view.bounds.size.height - y);
|
|
3008
|
+
__weak NativeSdkChromiumHost *weakSelf = object;
|
|
3009
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
3010
|
+
NativeSdkChromiumHost *presentSelf = weakSelf;
|
|
3011
|
+
if (!presentSelf) return;
|
|
3012
|
+
[menu popUpMenuPositioningItem:nil atLocation:location inView:view];
|
|
3013
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
3014
|
+
NativeSdkChromiumHost *emitSelf = weakSelf;
|
|
3015
|
+
if (!emitSelf) return;
|
|
3016
|
+
const char *labelBytes = eventLabel.UTF8String ?: "";
|
|
3017
|
+
[emitSelf emitEvent:(native_sdk_appkit_event_t){
|
|
3018
|
+
.kind = NATIVE_SDK_APPKIT_EVENT_CONTEXT_MENU_ACTION,
|
|
3019
|
+
.window_id = window_id,
|
|
3020
|
+
.view_label = labelBytes,
|
|
3021
|
+
.view_label_len = [eventLabel lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
|
|
3022
|
+
.widget_id = token,
|
|
3023
|
+
.menu_item_id = target.selectedItemId,
|
|
3024
|
+
}];
|
|
3025
|
+
});
|
|
3026
|
+
});
|
|
3027
|
+
return 1;
|
|
3028
|
+
}
|