@native-sdk/cli 0.6.0 → 0.6.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/build.zig +58 -33
  2. package/native-sdk.d.ts +16 -0
  3. package/package.json +9 -9
  4. package/packages/core/package-lock.json +2 -2
  5. package/packages/core/package.json +1 -1
  6. package/src/app_runner/root.zig +19 -0
  7. package/src/platform/linux/gtk_host.c +167 -8
  8. package/src/platform/linux/gtk_host.h +3 -2
  9. package/src/platform/linux/root.zig +198 -9
  10. package/src/platform/macos/appkit_host.h +2 -2
  11. package/src/platform/macos/appkit_host.m +186 -36
  12. package/src/platform/macos/cef_host.mm +46 -11
  13. package/src/platform/macos/root.zig +143 -5
  14. package/src/platform/null_platform.zig +32 -4
  15. package/src/platform/types.zig +29 -4
  16. package/src/platform/windows/root.zig +220 -10
  17. package/src/platform/windows/webview2_host.cpp +330 -31
  18. package/src/primitives/app_manifest/comptime_scene.zig +4 -0
  19. package/src/primitives/app_manifest/types.zig +16 -0
  20. package/src/primitives/canvas/widget_render.zig +21 -2
  21. package/src/primitives/canvas/widget_runtime_tests.zig +51 -0
  22. package/src/runtime/api.zig +6 -5
  23. package/src/runtime/bridge_payload.zig +9 -0
  24. package/src/runtime/bridge_responses.zig +2 -0
  25. package/src/runtime/builtin_bridge.zig +10 -0
  26. package/src/runtime/canvas_widget_events.zig +28 -2
  27. package/src/runtime/canvas_widget_floating_tests.zig +3 -3
  28. package/src/runtime/effects.zig +5 -6
  29. package/src/runtime/flow.zig +45 -7
  30. package/src/runtime/gpu_surface_events.zig +10 -3
  31. package/src/runtime/runtime_event_tests.zig +9 -6
  32. package/src/runtime/session_tests.zig +5 -4
  33. package/src/runtime/shell_layout.zig +25 -11
  34. package/src/runtime/shell_layout_tests.zig +184 -0
  35. package/src/runtime/shell_view_tests.zig +9 -7
  36. package/src/runtime/state.zig +6 -0
  37. package/src/runtime/ui_app.zig +66 -18
  38. package/src/runtime/ui_app_window_tests.zig +107 -2
  39. package/src/runtime/window_command_bridge_tests.zig +82 -6
  40. package/src/runtime/window_storage.zig +14 -3
  41. package/src/runtime/window_views.zig +12 -11
  42. package/src/tooling/manifest.zig +38 -2
  43. package/src/tooling/raw_manifest.zig +8 -0
  44. package/src/tooling/templates.zig +17 -0
@@ -336,6 +336,17 @@ struct Window {
336
336
  * the natural minimum (WM_GETMINMAXINFO applies the floor). */
337
337
  double min_width = 0;
338
338
  double min_height = 0;
339
+ /* Creation-time overlay presentation. Installed before first show. */
340
+ bool transparent = false;
341
+ bool always_on_top = false;
342
+ bool click_through = false;
343
+ bool activate_on_show = true;
344
+ bool show_on_first_present = false;
345
+ bool shown = false;
346
+ /* GetTickCount64 value captured when an on-first-present window is
347
+ * created. The per-window frame timer owns the one-second safety
348
+ * reveal; immediate/already shown windows do not consult it. */
349
+ ULONGLONG deferred_show_started_ms = 0;
339
350
  /* Last DWMWA_CAPTION_COLOR pushed for the hidden styles (sampled
340
351
  * from the presented header pixels so the DWM caption material
341
352
  * behind the button cluster matches the app's header). */
@@ -512,6 +523,7 @@ constexpr UINT_PTR kAppTimerIdBase = 0x1000;
512
523
  /* The 16 ms per-window frame-pump timer (SetTimer id on each top-level
513
524
  * window; distinct from the app-timer id range). */
514
525
  constexpr UINT_PTR kFrameTimerId = 1;
526
+ constexpr ULONGLONG kDeferredShowDeadlineMs = 1000;
515
527
 
516
528
  struct AppTimer {
517
529
  uint64_t id = 0;
@@ -2698,6 +2710,191 @@ static void paintGpuSurface(NativeView &view, HWND hwnd, HDC dc) {
2698
2710
  StretchDIBits(dc, 0, 0, client_width, client_height, 0, 0, view.gpu_buf_width, view.gpu_buf_height, view.gpu_bgra.data(), &info, DIB_RGB_COLORS, SRCCOPY);
2699
2711
  }
2700
2712
 
2713
+ static constexpr uint8_t compositePremultipliedChannel(uint8_t source, uint8_t source_alpha, uint8_t destination) {
2714
+ return static_cast<uint8_t>(source +
2715
+ (static_cast<uint32_t>(destination) * (255u - source_alpha) + 127u) / 255u);
2716
+ }
2717
+
2718
+ static_assert(
2719
+ compositePremultipliedChannel(128, 128, 64) == 160 &&
2720
+ compositePremultipliedChannel(128, 128, 128) == 192,
2721
+ "premultiplied source-over must preserve destination contribution");
2722
+
2723
+ /* User32 excludes alpha-zero pixels before WM_NCHITTEST reaches a
2724
+ * per-pixel layered window. Keep the real WS_THICKFRAME resize bands at
2725
+ * the smallest nonzero alpha so they remain pointer targets without
2726
+ * making the transparent frame visibly opaque. Client pixels stay zero
2727
+ * until a canvas paints them, preserving intentional transparent holes.
2728
+ * click_through still wins in windowProc by returning HTTRANSPARENT. */
2729
+ static constexpr uint8_t kTransparentResizeHitAlpha = 1;
2730
+
2731
+ static constexpr bool outsideTransparentClientBounds(
2732
+ int x,
2733
+ int y,
2734
+ int client_left,
2735
+ int client_top,
2736
+ int client_right,
2737
+ int client_bottom) {
2738
+ return x < client_left || x >= client_right || y < client_top || y >= client_bottom;
2739
+ }
2740
+
2741
+ static_assert(
2742
+ outsideTransparentClientBounds(6, 100, 7, 7, 713, 513) &&
2743
+ !outsideTransparentClientBounds(7, 100, 7, 7, 713, 513),
2744
+ "the non-client resize band must stop exactly where the client begins");
2745
+
2746
+ static void seedTransparentResizeHitFrame(
2747
+ uint8_t *target,
2748
+ int outer_width,
2749
+ int outer_height,
2750
+ int client_left,
2751
+ int client_top,
2752
+ int client_right,
2753
+ int client_bottom) {
2754
+ const int left = std::max(0, std::min(outer_width, client_left));
2755
+ const int top = std::max(0, std::min(outer_height, client_top));
2756
+ const int right = std::max(left, std::min(outer_width, client_right));
2757
+ const int bottom = std::max(top, std::min(outer_height, client_bottom));
2758
+ const auto seed_span = [&](int y, int begin, int end) {
2759
+ for (int x = begin; x < end; ++x) {
2760
+ target[((size_t)y * (size_t)outer_width + (size_t)x) * 4 + 3] = kTransparentResizeHitAlpha;
2761
+ }
2762
+ };
2763
+ for (int y = 0; y < top; ++y) seed_span(y, 0, outer_width);
2764
+ for (int y = top; y < bottom; ++y) {
2765
+ seed_span(y, 0, left);
2766
+ seed_span(y, right, outer_width);
2767
+ }
2768
+ for (int y = bottom; y < outer_height; ++y) seed_span(y, 0, outer_width);
2769
+ }
2770
+
2771
+ /* Per-pixel-alpha presentation for transparent top-level windows.
2772
+ * Layered windows do not compose child HWND redirection surfaces, so
2773
+ * blend every visible canvas's premultiplied BGRA buffer into one
2774
+ * outer-window DIB in native layer order and submit it atomically.
2775
+ * This happens before a deferred first show, which makes the first
2776
+ * visible frame both nonblank and alpha-correct. */
2777
+ static bool presentTransparentWindow(Host *host, Window &window) {
2778
+ if (!host || !window.transparent || !window.hwnd) return false;
2779
+ RECT outer = {};
2780
+ if (!GetWindowRect(window.hwnd, &outer)) return false;
2781
+ const int outer_width = outer.right - outer.left;
2782
+ const int outer_height = outer.bottom - outer.top;
2783
+ if (outer_width <= 0 || outer_height <= 0) return false;
2784
+ RECT client = {};
2785
+ if (!GetClientRect(window.hwnd, &client)) return false;
2786
+ POINT client_origin = { client.left, client.top };
2787
+ if (!ClientToScreen(window.hwnd, &client_origin)) return false;
2788
+ const int client_left = client_origin.x - outer.left;
2789
+ const int client_top = client_origin.y - outer.top;
2790
+ const int client_right = client_left + (client.right - client.left);
2791
+ const int client_bottom = client_top + (client.bottom - client.top);
2792
+
2793
+ BITMAPINFO target_info = {};
2794
+ target_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
2795
+ target_info.bmiHeader.biWidth = outer_width;
2796
+ target_info.bmiHeader.biHeight = -outer_height;
2797
+ target_info.bmiHeader.biPlanes = 1;
2798
+ target_info.bmiHeader.biBitCount = 32;
2799
+ target_info.bmiHeader.biCompression = BI_RGB;
2800
+ void *target_bits = nullptr;
2801
+ HDC screen = GetDC(nullptr);
2802
+ HDC memory = screen ? CreateCompatibleDC(screen) : nullptr;
2803
+ HBITMAP bitmap = screen ? CreateDIBSection(screen, &target_info, DIB_RGB_COLORS, &target_bits, nullptr, 0) : nullptr;
2804
+ if (!screen || !memory || !bitmap || !target_bits) {
2805
+ if (bitmap) DeleteObject(bitmap);
2806
+ if (memory) DeleteDC(memory);
2807
+ if (screen) ReleaseDC(nullptr, screen);
2808
+ return false;
2809
+ }
2810
+ memset(target_bits, 0, (size_t)outer_width * (size_t)outer_height * 4);
2811
+ uint8_t *target = reinterpret_cast<uint8_t *>(target_bits);
2812
+ if (window.resizable) {
2813
+ seedTransparentResizeHitFrame(
2814
+ target,
2815
+ outer_width,
2816
+ outer_height,
2817
+ client_left,
2818
+ client_top,
2819
+ client_right,
2820
+ client_bottom);
2821
+ }
2822
+
2823
+ std::vector<NativeView *> surfaces;
2824
+ for (auto &entry : host->native_views) {
2825
+ NativeView &view = entry.second;
2826
+ if (view.window_id != window.id || view.kind != kViewGpuSurface ||
2827
+ !view.hwnd || !view.visible || view.gpu_bgra.empty()) continue;
2828
+ surfaces.push_back(&view);
2829
+ }
2830
+ std::sort(surfaces.begin(), surfaces.end(), [](const NativeView *a, const NativeView *b) {
2831
+ if (a->layer != b->layer) return a->layer < b->layer;
2832
+ return a->creation_order < b->creation_order;
2833
+ });
2834
+
2835
+ for (const NativeView *view : surfaces) {
2836
+ RECT child = {};
2837
+ if (!GetWindowRect(view->hwnd, &child)) continue;
2838
+ const int child_width = child.right - child.left;
2839
+ const int child_height = child.bottom - child.top;
2840
+ if (child_width <= 0 || child_height <= 0) continue;
2841
+ POINT child_origin = { child.left, child.top };
2842
+ ScreenToClient(window.hwnd, &child_origin);
2843
+ const int content_x = client_origin.x - outer.left + child_origin.x;
2844
+ const int content_y = client_origin.y - outer.top + child_origin.y;
2845
+
2846
+ for (int y = 0; y < child_height; ++y) {
2847
+ const int target_y = content_y + y;
2848
+ if (target_y < 0 || target_y >= outer_height) continue;
2849
+ const int source_y = (int)((int64_t)y * view->gpu_buf_height / child_height);
2850
+ for (int x = 0; x < child_width; ++x) {
2851
+ const int target_x = content_x + x;
2852
+ if (target_x < 0 || target_x >= outer_width) continue;
2853
+ const int source_x = (int)((int64_t)x * view->gpu_buf_width / child_width);
2854
+ const size_t source_offset = ((size_t)source_y * (size_t)view->gpu_buf_width + (size_t)source_x) * 4;
2855
+ const size_t target_offset = ((size_t)target_y * (size_t)outer_width + (size_t)target_x) * 4;
2856
+ const uint8_t *source_pixel = view->gpu_bgra.data() + source_offset;
2857
+ uint8_t *target_pixel = target + target_offset;
2858
+ /* Premultiplied source-over: src + dst * (1 - src.a).
2859
+ * The channel sum is intrinsically <= 255 because both
2860
+ * inputs obey the premultiplied invariant. */
2861
+ for (size_t channel = 0; channel < 4; channel++) {
2862
+ target_pixel[channel] = compositePremultipliedChannel(
2863
+ source_pixel[channel],
2864
+ source_pixel[3],
2865
+ target_pixel[channel]);
2866
+ }
2867
+ }
2868
+ }
2869
+ }
2870
+
2871
+ HGDIOBJ previous = SelectObject(memory, bitmap);
2872
+ POINT destination = { outer.left, outer.top };
2873
+ SIZE size = { outer_width, outer_height };
2874
+ POINT source = { 0, 0 };
2875
+ BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };
2876
+ const BOOL updated = UpdateLayeredWindow(window.hwnd, screen, &destination, &size, memory, &source, 0, &blend, ULW_ALPHA);
2877
+ SelectObject(memory, previous);
2878
+ DeleteObject(bitmap);
2879
+ DeleteDC(memory);
2880
+ ReleaseDC(nullptr, screen);
2881
+ return updated != FALSE;
2882
+ }
2883
+
2884
+ static void showWindowImplicit(Window &window) {
2885
+ if (!window.hwnd || window.shown) return;
2886
+ ShowWindow(window.hwnd, window.activate_on_show ? SW_SHOW : SW_SHOWNOACTIVATE);
2887
+ window.shown = true;
2888
+ window.deferred_show_started_ms = 0;
2889
+ UpdateWindow(window.hwnd);
2890
+ }
2891
+
2892
+ static void showDeferredWindowIfDeadlinePassed(Window &window) {
2893
+ if (!window.hwnd || window.shown || !window.show_on_first_present) return;
2894
+ const ULONGLONG elapsed_ms = GetTickCount64() - window.deferred_show_started_ms;
2895
+ if (elapsed_ms >= kDeferredShowDeadlineMs) showWindowImplicit(window);
2896
+ }
2897
+
2701
2898
  /* Key names match shortcutKeyFromWParam (which mirrors the GTK/AppKit gpu
2702
2899
  * key set) plus the navigation keys the canvas text editor understands. */
2703
2900
  static std::string gpuSurfaceKeyName(WPARAM wparam) {
@@ -4873,6 +5070,8 @@ static LRESULT CALLBACK windowProc(HWND hwnd, UINT message, WPARAM wparam, LPARA
4873
5070
  SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(host));
4874
5071
  }
4875
5072
  Host *host = hostFromWindow(hwnd);
5073
+ const Window *input_window = windowForHwnd(host, hwnd);
5074
+ if (input_window && input_window->click_through && message == WM_NCHITTEST) return HTTRANSPARENT;
4876
5075
  /* Hidden-titlebar (custom frame) windows first: DwmDefWindowProc
4877
5076
  * gets FIRST CLAIM on every message — it owns the DWM-drawn caption
4878
5077
  * buttons over the extended frame (hit-test, hover wash, press), and
@@ -5179,6 +5378,12 @@ static LRESULT CALLBACK windowProc(HWND hwnd, UINT message, WPARAM wparam, LPARA
5179
5378
  if (host && handleAppTimerMessage(host, wparam)) return 0;
5180
5379
  if (host && handleAudioTimerMessage(host, wparam)) return 0;
5181
5380
  if (host && wparam == kFrameTimerId) {
5381
+ for (auto &entry : host->windows) {
5382
+ if (entry.second.hwnd == hwnd) {
5383
+ showDeferredWindowIfDeadlinePassed(entry.second);
5384
+ break;
5385
+ }
5386
+ }
5182
5387
  for (auto &entry : host->windows) emit(host, entry.second, kFrame);
5183
5388
  }
5184
5389
  return 0;
@@ -5290,7 +5495,25 @@ static ATOM registerClass(Host *host) {
5290
5495
  return RegisterClassExW(&wc);
5291
5496
  }
5292
5497
 
5498
+ static DWORD windowExtendedStyle(const Window &window) {
5499
+ DWORD style = 0;
5500
+ /* WS_EX_TRANSPARENT passes input through to other processes only
5501
+ * for a layered window. Click-through is orthogonal to per-pixel
5502
+ * transparency, so an otherwise opaque click-through window still
5503
+ * uses a layered surface whose constant alpha is fixed at 255 below. */
5504
+ if (window.transparent || window.click_through) style |= WS_EX_LAYERED;
5505
+ if (window.always_on_top) style |= WS_EX_TOPMOST;
5506
+ if (window.click_through) style |= WS_EX_TRANSPARENT;
5507
+ return style;
5508
+ }
5509
+
5293
5510
  static bool createNativeWindow(Host *host, Window &window) {
5511
+ /* UpdateLayeredWindow replaces the complete top-level image: Win32
5512
+ * non-client chrome and HMENU pixels are not redirected into it.
5513
+ * Canvas children have an explicit software composition path below,
5514
+ * but accepting either kind of OS chrome would create an invisible
5515
+ * titlebar/menu with live hit targets. */
5516
+ if (window.transparent && (!windowIsChromeless(window) || !host->menus.empty())) return false;
5294
5517
  registerClass(host);
5295
5518
  std::wstring title = widen(window.title.empty() ? host->window_title : window.title);
5296
5519
  /* ALL titlebar styles keep the full overlapped frame. The hidden
@@ -5334,7 +5557,7 @@ static bool createNativeWindow(Host *host, Window &window) {
5334
5557
  outer_height = outer.cy;
5335
5558
  }
5336
5559
  HWND hwnd = CreateWindowExW(
5337
- 0,
5560
+ windowExtendedStyle(window),
5338
5561
  L"NativeSdkWindowsHost",
5339
5562
  title.c_str(),
5340
5563
  style,
@@ -5349,6 +5572,13 @@ static bool createNativeWindow(Host *host, Window &window) {
5349
5572
  if (!hwnd) return false;
5350
5573
  DragAcceptFiles(hwnd, TRUE);
5351
5574
  window.hwnd = hwnd;
5575
+ window.deferred_show_started_ms = window.show_on_first_present ? GetTickCount64() : 0;
5576
+ if (window.click_through && !window.transparent &&
5577
+ !SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA)) {
5578
+ DestroyWindow(hwnd);
5579
+ window.hwnd = nullptr;
5580
+ return false;
5581
+ }
5352
5582
  applyMenusToWindow(host, window);
5353
5583
  /* Before the first show, so a dark-scheme launch never flashes a
5354
5584
  * light caption. */
@@ -5361,8 +5591,17 @@ static bool createNativeWindow(Host *host, Window &window) {
5361
5591
  * `window` referencing the stored map entry for exactly this. */
5362
5592
  SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED);
5363
5593
  }
5364
- ShowWindow(hwnd, SW_SHOW);
5365
- UpdateWindow(hwnd);
5594
+ /* Initialize WS_EX_LAYERED through the same per-pixel path used by
5595
+ * canvas presents. A layered HWND is otherwise not displayable until
5596
+ * SetLayeredWindowAttributes or UpdateLayeredWindow has succeeded. */
5597
+ if (window.transparent && !presentTransparentWindow(host, window)) {
5598
+ DestroyWindow(hwnd);
5599
+ window.hwnd = nullptr;
5600
+ return false;
5601
+ }
5602
+ if (!window.show_on_first_present) {
5603
+ showWindowImplicit(window);
5604
+ }
5366
5605
  SetTimer(hwnd, kFrameTimerId, 16, nullptr);
5367
5606
  return true;
5368
5607
  }
@@ -5371,14 +5610,14 @@ static bool createNativeWindow(Host *host, Window &window) {
5371
5610
 
5372
5611
  extern "C" {
5373
5612
 
5374
- void native_sdk_windows_load_window_webview(Host *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);
5613
+ int native_sdk_windows_load_window_webview(Host *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);
5375
5614
  void native_sdk_windows_bridge_respond_window(Host *host, uint64_t window_id, const char *response, size_t response_len);
5376
5615
  void native_sdk_windows_bridge_respond_webview(Host *host, uint64_t window_id, const char *webview_label, size_t webview_label_len, const char *response, size_t response_len);
5377
5616
  size_t native_sdk_windows_clipboard_read_data(Host *host, const char *mime_type, size_t mime_type_len, char *buffer, size_t buffer_len);
5378
5617
  int native_sdk_windows_clipboard_write_data(Host *host, const char *mime_type, size_t mime_type_len, const char *bytes, size_t bytes_len);
5379
5618
  void native_sdk_windows_cancel_timer(Host *host, uint64_t timer_id);
5380
5619
 
5381
- Host *native_sdk_windows_create(const char *app_name, size_t app_name_len, 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, double min_width, double min_height) {
5620
+ Host *native_sdk_windows_create(const char *app_name, size_t app_name_len, 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, double min_width, double min_height, int show_policy, uint32_t window_flags) {
5382
5621
  (void)restore_frame;
5383
5622
  INITCOMMONCONTROLSEX controls = {};
5384
5623
  controls.dwSize = sizeof(controls);
@@ -5411,6 +5650,11 @@ Host *native_sdk_windows_create(const char *app_name, size_t app_name_len, const
5411
5650
  window.titlebar_style = titlebar_style;
5412
5651
  window.min_width = min_width;
5413
5652
  window.min_height = min_height;
5653
+ window.transparent = (window_flags & (1u << 0)) != 0;
5654
+ window.always_on_top = (window_flags & (1u << 1)) != 0;
5655
+ window.click_through = (window_flags & (1u << 2)) != 0;
5656
+ window.activate_on_show = (window_flags & (1u << 3)) == 0;
5657
+ window.show_on_first_present = show_policy == 1;
5414
5658
  host->windows[window.id] = window;
5415
5659
  return host;
5416
5660
  }
@@ -5563,10 +5807,18 @@ int native_sdk_windows_decode_image(const uint8_t *bytes, size_t bytes_len, uint
5563
5807
  }
5564
5808
 
5565
5809
  void native_sdk_windows_load_webview(Host *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) {
5566
- native_sdk_windows_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);
5810
+ (void)native_sdk_windows_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);
5567
5811
  }
5568
5812
 
5569
- void native_sdk_windows_load_window_webview(Host *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) {
5813
+ int native_sdk_windows_load_window_webview(Host *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) {
5814
+ if (!host) return 0;
5815
+ auto window = host->windows.find(window_id);
5816
+ if (window == host->windows.end() || !window->second.hwnd) return 0;
5817
+ /* A child WebView cannot participate in UpdateLayeredWindow's
5818
+ * top-level alpha bitmap. Preserve a distinct return so the Zig
5819
+ * service reports UnsupportedWindowTransparency, not the generic
5820
+ * CreateFailed used for an absent window. */
5821
+ if (window->second.transparent) return -1;
5570
5822
  #if !NATIVE_SDK_HAS_WEBVIEW2
5571
5823
  (void)spa_fallback;
5572
5824
  (void)source;
@@ -5578,14 +5830,9 @@ void native_sdk_windows_load_window_webview(Host *host, uint64_t window_id, cons
5578
5830
  (void)asset_entry_len;
5579
5831
  (void)asset_origin;
5580
5832
  (void)asset_origin_len;
5581
- if (!host) return;
5582
- auto found = host->windows.find(window_id);
5583
- if (found != host->windows.end()) emit(host, found->second, kWindowFrame);
5833
+ emit(host, window->second, kWindowFrame);
5834
+ return 1;
5584
5835
  #else
5585
- if (!host) return;
5586
- auto window = host->windows.find(window_id);
5587
- if (window == host->windows.end() || !window->second.hwnd) return;
5588
-
5589
5836
  std::string key = webViewKey(window_id, "main");
5590
5837
  auto found = host->webviews.find(key);
5591
5838
  if (found == host->webviews.end()) {
@@ -5604,7 +5851,7 @@ void native_sdk_windows_load_window_webview(Host *host, uint64_t window_id, cons
5604
5851
  nullptr,
5605
5852
  host->instance,
5606
5853
  nullptr);
5607
- if (!hwnd) return;
5854
+ if (!hwnd) return 0;
5608
5855
 
5609
5856
  ChildWebView webview;
5610
5857
  webview.window_id = window_id;
@@ -5628,7 +5875,7 @@ void native_sdk_windows_load_window_webview(Host *host, uint64_t window_id, cons
5628
5875
  if (!createChildWebView(host, key)) {
5629
5876
  DestroyWindow(hwnd);
5630
5877
  host->webviews.erase(key);
5631
- return;
5878
+ return 0;
5632
5879
  }
5633
5880
  }
5634
5881
 
@@ -5642,6 +5889,7 @@ void native_sdk_windows_load_window_webview(Host *host, uint64_t window_id, cons
5642
5889
  webview.spa_fallback = spa_fallback != 0;
5643
5890
  loadWebViewSource(webview);
5644
5891
  emit(host, window->second, kWindowFrame);
5892
+ return 1;
5645
5893
  #endif
5646
5894
  }
5647
5895
 
@@ -5709,12 +5957,20 @@ void native_sdk_windows_set_security_policy(Host *host, const char *allowed_orig
5709
5957
  host->external_link_action = external_action;
5710
5958
  }
5711
5959
 
5712
- void native_sdk_windows_set_menus(Host *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) {
5713
- if (!host) return;
5960
+ int native_sdk_windows_set_menus(Host *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) {
5961
+ if (!host) return 0;
5962
+ /* Menus may be configured after a runtime alpha window exists.
5963
+ * Refuse the whole update before mutating the live menu model; an
5964
+ * HMENU cannot be represented by that window's layered bitmap. */
5965
+ if (menu_count > 0) {
5966
+ for (const auto &entry : host->windows) {
5967
+ if (entry.second.transparent) return 0;
5968
+ }
5969
+ }
5714
5970
  host->menus.clear();
5715
5971
  host->menu_commands.clear();
5716
5972
 
5717
- if (menu_count > 0 && (!menu_titles || !menu_title_lens)) return;
5973
+ if (menu_count > 0 && (!menu_titles || !menu_title_lens)) return 0;
5718
5974
  host->menus.reserve(menu_count);
5719
5975
  for (size_t index = 0; index < menu_count; ++index) {
5720
5976
  Menu menu;
@@ -5722,7 +5978,7 @@ void native_sdk_windows_set_menus(Host *host, const char *const *menu_titles, co
5722
5978
  host->menus.push_back(menu);
5723
5979
  }
5724
5980
 
5725
- if (item_count > 0 && (!item_menu_indices || !item_labels || !item_label_lens || !item_commands || !item_command_lens || !item_keys || !item_key_lens || !item_modifiers || !item_separators || !item_enabled || !item_checked)) return;
5981
+ if (item_count > 0 && (!item_menu_indices || !item_labels || !item_label_lens || !item_commands || !item_command_lens || !item_keys || !item_key_lens || !item_modifiers || !item_separators || !item_enabled || !item_checked)) return 0;
5726
5982
  uint32_t next_command_id = kMenuCommandBase;
5727
5983
  for (size_t index = 0; index < item_count; ++index) {
5728
5984
  uint32_t menu_index = item_menu_indices[index];
@@ -5748,6 +6004,7 @@ void native_sdk_windows_set_menus(Host *host, const char *const *menu_titles, co
5748
6004
  for (auto &entry : host->windows) {
5749
6005
  if (entry.second.hwnd) applyMenusToWindow(host, entry.second);
5750
6006
  }
6007
+ return 1;
5751
6008
  }
5752
6009
 
5753
6010
  void native_sdk_windows_set_shortcuts(Host *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) {
@@ -5768,7 +6025,7 @@ void native_sdk_windows_set_shortcuts(Host *host, const char *const *ids, const
5768
6025
  }
5769
6026
  }
5770
6027
 
5771
- int native_sdk_windows_create_window(Host *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, double min_width, double min_height) {
6028
+ int native_sdk_windows_create_window(Host *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, double min_width, double min_height, int show_policy, uint32_t window_flags) {
5772
6029
  (void)restore_frame;
5773
6030
  if (!host || host->windows.find(window_id) != host->windows.end()) return 0;
5774
6031
  Window window;
@@ -5783,6 +6040,11 @@ int native_sdk_windows_create_window(Host *host, uint64_t window_id, const char
5783
6040
  window.titlebar_style = titlebar_style;
5784
6041
  window.min_width = min_width;
5785
6042
  window.min_height = min_height;
6043
+ window.transparent = (window_flags & (1u << 0)) != 0;
6044
+ window.always_on_top = (window_flags & (1u << 1)) != 0;
6045
+ window.click_through = (window_flags & (1u << 2)) != 0;
6046
+ window.activate_on_show = (window_flags & (1u << 3)) == 0;
6047
+ window.show_on_first_present = show_policy == 1;
5786
6048
  /* Register BEFORE creating: createNativeWindow's post-create frame
5787
6049
  * pass (hidden titlebar styles) resolves the window through the map
5788
6050
  * by HWND, so the stored entry must be the one it mutates. */
@@ -5930,6 +6192,11 @@ int native_sdk_windows_focus_window(Host *host, uint64_t window_id) {
5930
6192
  if (!host) return 0;
5931
6193
  auto found = host->windows.find(window_id);
5932
6194
  if (found == host->windows.end() || !found->second.hwnd) return 0;
6195
+ if (!found->second.shown) {
6196
+ ShowWindow(found->second.hwnd, SW_SHOW);
6197
+ found->second.shown = true;
6198
+ }
6199
+ found->second.deferred_show_started_ms = 0;
5933
6200
  SetForegroundWindow(found->second.hwnd);
5934
6201
  SetFocus(found->second.hwnd);
5935
6202
  return 1;
@@ -5964,7 +6231,15 @@ int native_sdk_windows_show_window(Host *host, uint64_t window_id) {
5964
6231
  auto found = host->windows.find(window_id);
5965
6232
  if (found == host->windows.end() || !found->second.hwnd) return 0;
5966
6233
  found->second.policy_hidden = false;
5967
- ShowWindow(found->second.hwnd, IsIconic(found->second.hwnd) ? SW_RESTORE : SW_SHOW);
6234
+ if (found->second.activate_on_show) {
6235
+ ShowWindow(found->second.hwnd, IsIconic(found->second.hwnd) ? SW_RESTORE : SW_SHOW);
6236
+ SetForegroundWindow(found->second.hwnd);
6237
+ SetFocus(found->second.hwnd);
6238
+ } else {
6239
+ ShowWindow(found->second.hwnd, SW_SHOWNOACTIVATE);
6240
+ }
6241
+ found->second.shown = true;
6242
+ found->second.deferred_show_started_ms = 0;
5968
6243
  /* Re-show returns full frame cadence without dropping a beat, the
5969
6244
  * same supersede the top-level WM_SIZE handler runs on restore:
5970
6245
  * SW_SHOW on a same-size window dispatches no WM_SIZE, so a
@@ -5980,8 +6255,6 @@ int native_sdk_windows_show_window(Host *host, uint64_t window_id) {
5980
6255
  surface.gpu_emission_scheduled = false;
5981
6256
  gpuSurfaceScheduleFrameEmission(host, surface);
5982
6257
  }
5983
- SetForegroundWindow(found->second.hwnd);
5984
- SetFocus(found->second.hwnd);
5985
6258
  emit(host, found->second, kWindowFrame);
5986
6259
  return 1;
5987
6260
  }
@@ -6001,6 +6274,11 @@ int native_sdk_windows_create_view(Host *host, uint64_t window_id, const char *l
6001
6274
  if (!host || label_len == 0 || !isSupportedNativeViewKind(kind) || !validNativeViewFrame(x, y, width, height)) return 0;
6002
6275
  auto window = host->windows.find(window_id);
6003
6276
  if (window == host->windows.end() || !window->second.hwnd) return 0;
6277
+ /* UpdateLayeredWindow owns the complete top-level bitmap and cannot
6278
+ * redirect ordinary child HWNDs into it. Canvas surfaces are composed
6279
+ * explicitly by presentTransparentWindow; reject every other native
6280
+ * kind instead of accepting content Windows would leave invisible. */
6281
+ if (window->second.transparent && kind != kViewGpuSurface) return 0;
6004
6282
 
6005
6283
  std::string label_string = slice(label, label_len);
6006
6284
  std::string key = nativeViewKey(window_id, label_string);
@@ -6130,7 +6408,7 @@ int native_sdk_windows_create_view(Host *host, uint64_t window_id, const char *l
6130
6408
  * after the first present; see the frame-scheduler comments). */
6131
6409
  SetWindowLongPtrW(hwnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(host));
6132
6410
  SetTimer(hwnd, kGpuFrameTimerId, 16, nullptr);
6133
- SetFocus(hwnd);
6411
+ if (window->second.activate_on_show) SetFocus(hwnd);
6134
6412
  }
6135
6413
  return 1;
6136
6414
  }
@@ -6214,6 +6492,8 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
6214
6492
  if (width > INT_MAX || height > INT_MAX) return 0;
6215
6493
  if (rgba8_len != width * height * 4) return 0;
6216
6494
  NativeView &view = found->second;
6495
+ auto owner = host->windows.find(view.window_id);
6496
+ const bool transparent_window = owner != host->windows.end() && owner->second.transparent;
6217
6497
 
6218
6498
  /* Straight RGBA8 -> top-down BGRA rows for a BI_RGB 32bpp DIB. The
6219
6499
  * surface is opaque (alpha_mode "opaque"), so no premultiply is
@@ -6228,17 +6508,17 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
6228
6508
  const size_t pixel_count = width * height;
6229
6509
  for (size_t index = 0; index < pixel_count; index++) {
6230
6510
  const uint8_t *src = rgba8 + index * 4;
6231
- dst[index * 4 + 0] = src[2];
6232
- dst[index * 4 + 1] = src[1];
6233
- dst[index * 4 + 2] = src[0];
6234
- dst[index * 4 + 3] = 255;
6511
+ const uint32_t alpha = transparent_window ? src[3] : 255;
6512
+ dst[index * 4 + 0] = transparent_window ? (uint8_t)((src[2] * alpha + 127) / 255) : src[2];
6513
+ dst[index * 4 + 1] = transparent_window ? (uint8_t)((src[1] * alpha + 127) / 255) : src[1];
6514
+ dst[index * 4 + 2] = transparent_window ? (uint8_t)((src[0] * alpha + 127) / 255) : src[0];
6515
+ dst[index * 4 + 3] = (uint8_t)alpha;
6235
6516
  }
6236
6517
  view.gpu_buf_width = (int)width;
6237
6518
  view.gpu_buf_height = (int)height;
6238
6519
 
6239
6520
  /* Hidden-titlebar windows: keep the DWM caption material behind the
6240
6521
  * button cluster matched to the header the app just presented. */
6241
- auto owner = host->windows.find(view.window_id);
6242
6522
  if (owner != host->windows.end()) syncHiddenCaptionColor(host, owner->second, view, rgba8, width, height);
6243
6523
 
6244
6524
  const size_t sample_index = ((height / 2) * width + width / 2) * 4;
@@ -6251,7 +6531,8 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
6251
6531
  view.gpu_sample_color = ((uint32_t)sa << 24) | ((uint32_t)sr << 16) | ((uint32_t)sg << 8) | (uint32_t)sb;
6252
6532
  }
6253
6533
 
6254
- InvalidateRect(view.hwnd, nullptr, FALSE);
6534
+ const bool layered_presented = owner != host->windows.end() && presentTransparentWindow(host, owner->second);
6535
+ if (!layered_presented) InvalidateRect(view.hwnd, nullptr, FALSE);
6255
6536
  /* A present is the completion producer on the surface's single
6256
6537
  * frame-event scheduler: the completion event it arms is what
6257
6538
  * drives the runtime's frame loop (an armed animation presents,
@@ -6262,6 +6543,13 @@ int native_sdk_windows_present_gpu_surface_pixels(Host *host, uint64_t window_id
6262
6543
  * presents keep the heartbeat — they ARE the spin being throttled. */
6263
6544
  const bool first_present = !view.gpu_presented;
6264
6545
  view.gpu_presented = true;
6546
+ /* Transparent windows reveal only after the outer alpha bitmap was
6547
+ * accepted. A failed UpdateLayeredWindow can recover on a later
6548
+ * present; the deadline remains the final safety net. */
6549
+ if (owner != host->windows.end() && !owner->second.shown &&
6550
+ (!owner->second.transparent || layered_presented)) {
6551
+ showWindowImplicit(owner->second);
6552
+ }
6265
6553
  if (first_present) view.gpu_prompt_frame_pending = true;
6266
6554
  gpuSurfaceScheduleFrameEmission(host, view);
6267
6555
  return 1;
@@ -6298,6 +6586,12 @@ int native_sdk_windows_update_view(Host *host, uint64_t window_id, const char *l
6298
6586
  std::string display_text = has_text ? view.text : nativeViewDisplayText(view);
6299
6587
  if (has_visible || has_enabled || has_role || has_accessibility_label || update_text) applyNativeViewState(view, update_text, display_text);
6300
6588
  if (has_layer) reorderWindowChildren(host, window_id);
6589
+ if (view.kind == kViewGpuSurface && (has_frame || has_layer || has_visible)) {
6590
+ auto window = host->windows.find(window_id);
6591
+ if (window != host->windows.end() && window->second.transparent) {
6592
+ (void)presentTransparentWindow(host, window->second);
6593
+ }
6594
+ }
6301
6595
  return 1;
6302
6596
  }
6303
6597
 
@@ -6336,6 +6630,10 @@ int native_sdk_windows_close_view(Host *host, uint64_t window_id, const char *la
6336
6630
  if (host->native_views.find(key) == host->native_views.end()) return 0;
6337
6631
  destroyNativeViewAndChildren(host, key);
6338
6632
  reorderWindowChildren(host, window_id);
6633
+ auto window = host->windows.find(window_id);
6634
+ if (window != host->windows.end() && window->second.transparent) {
6635
+ (void)presentTransparentWindow(host, window->second);
6636
+ }
6339
6637
  return 1;
6340
6638
  }
6341
6639
 
@@ -6685,6 +6983,7 @@ int native_sdk_windows_create_webview(Host *host, uint64_t window_id, const char
6685
6983
  if (!host || label_len == 0 || url_len == 0 || !validChildWebViewFrame(x, y, width, height)) return 0;
6686
6984
  auto window = host->windows.find(window_id);
6687
6985
  if (window == host->windows.end() || !window->second.hwnd) return 0;
6986
+ if (window->second.transparent) return 0;
6688
6987
  std::string label_string = slice(label, label_len);
6689
6988
  std::string key = webViewKey(window_id, label_string);
6690
6989
  if (host->webviews.find(key) != host->webviews.end()) return 0;
@@ -81,6 +81,10 @@ fn shellWindowFrom(comptime window: anytype) types.ShellWindow {
81
81
  if (@hasField(@TypeOf(window), "titlebar")) {
82
82
  out.titlebar = enumField(types.WindowTitlebarStyle, window.titlebar, "titlebar");
83
83
  }
84
+ if (@hasField(@TypeOf(window), "transparent")) out.transparent = window.transparent;
85
+ if (@hasField(@TypeOf(window), "always_on_top")) out.always_on_top = window.always_on_top;
86
+ if (@hasField(@TypeOf(window), "click_through")) out.click_through = window.click_through;
87
+ if (@hasField(@TypeOf(window), "activate_on_show")) out.activate_on_show = window.activate_on_show;
84
88
  if (@hasField(@TypeOf(window), "min_width")) out.min_width = window.min_width;
85
89
  if (@hasField(@TypeOf(window), "min_height")) out.min_height = window.min_height;
86
90
  if (@hasField(@TypeOf(window), "close_policy")) {
@@ -349,6 +349,18 @@ pub const Window = struct {
349
349
  /// threaded through the platform create call, so the main window
350
350
  /// can hide its titlebar before the scene loads.
351
351
  titlebar: WindowTitlebarStyle = .standard,
352
+ /// Alpha-capable top-level window. Pair canvas windows with a
353
+ /// non-opaque `gpu_alpha_mode` so transparent pixels reach through
354
+ /// to the desktop.
355
+ transparent: bool = false,
356
+ /// Keep this window above ordinary application windows.
357
+ always_on_top: bool = false,
358
+ /// Let all pointer input pass through the window to whatever is
359
+ /// underneath it.
360
+ click_through: bool = false,
361
+ /// Show without activating the app or taking keyboard focus when
362
+ /// false. An explicit focus action can still activate the window.
363
+ activate_on_show: bool = true,
352
364
  /// Content min-size floor the window itself enforces (macOS
353
365
  /// `contentMinSize`): the resize stops at the floor instead of the
354
366
  /// layout clamping/clipping panes below it. 0 = no floor.
@@ -478,6 +490,10 @@ pub const ShellWindow = struct {
478
490
  /// create, and the scene's first window here should declare the
479
491
  /// SAME style so the two never disagree.
480
492
  titlebar: WindowTitlebarStyle = .standard,
493
+ transparent: bool = false,
494
+ always_on_top: bool = false,
495
+ click_through: bool = false,
496
+ activate_on_show: bool = true,
481
497
  /// Content min-size floor the window itself enforces (macOS
482
498
  /// `contentMinSize`): the resize stops at the floor instead of the
483
499
  /// layout clamping/clipping panes below it. 0 = no floor. Like