@native-sdk/cli 0.8.4 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. package/build/app.zig +1178 -74
  2. package/build.zig +715 -17
  3. package/build.zig.zon +1 -0
  4. package/native-sdk.d.ts +10 -0
  5. package/package.json +12 -11
  6. package/packages/core/compile-surface/core.ts +435 -3
  7. package/packages/core/package-lock.json +11 -11
  8. package/packages/core/package.json +2 -2
  9. package/packages/core/scripts/compiler_typecheck.mjs +116 -15
  10. package/packages/core/scripts/gen_service_surface.mjs +268 -0
  11. package/packages/core/scripts/merge_static_archives.mjs +78 -0
  12. package/packages/core/scripts/npm_static_spike.mjs +64 -0
  13. package/packages/core/scripts/run_external_core_compiler.mjs +122 -6
  14. package/packages/core/scripts/run_external_service_compiler.mjs +280 -0
  15. package/packages/core/scripts/stage_external_core.mjs +9 -1
  16. package/packages/core/scripts/stage_external_services.mjs +172 -0
  17. package/packages/core/scripts/surface_manifest_diff.mjs +253 -0
  18. package/packages/core/scripts/vendor_service_packages.mjs +189 -0
  19. package/packages/core/sdk/core.d.ts +224 -0
  20. package/packages/core/sdk/core.ts +694 -22
  21. package/packages/core/sdk/events.d.ts +89 -0
  22. package/packages/core/sdk/events.ts +128 -0
  23. package/packages/core/src/checker.ts +936 -15
  24. package/packages/core/src/cli.ts +156 -3
  25. package/packages/core/src/contract.ts +28 -5
  26. package/packages/core/src/devhost.ts +1876 -16
  27. package/packages/core/src/devhost_journal.mjs +183 -0
  28. package/packages/core/src/devhost_resolver.mjs +17 -1
  29. package/packages/core/src/devhost_service_worker.mjs +90 -0
  30. package/packages/core/src/diagnostics.ts +179 -22
  31. package/packages/core/src/frontend.ts +210 -34
  32. package/packages/core/src/infer.ts +24 -0
  33. package/packages/core/src/modules.ts +129 -80
  34. package/packages/core/src/service_contract.ts +597 -0
  35. package/packages/core/src/sqlite_cli.ts +93 -0
  36. package/packages/core/src/sqlite_codegen.ts +806 -0
  37. package/packages/core/src/sqlite_runtime_policy.ts +364 -0
  38. package/packages/core/src/typed_ast.ts +36 -8
  39. package/skill-data/automation/SKILL.md +1 -1
  40. package/skill-data/core/references/project-anatomy.md +1 -1
  41. package/skill-data/native-ui/SKILL.md +58 -5
  42. package/skill-data/ts-core/SKILL.md +123 -41
  43. package/skill-data/ts-services/SKILL.md +126 -0
  44. package/skill-data/ts-services/references/service-surface.md +604 -0
  45. package/skill-data/zig/SKILL.md +1 -1
  46. package/skills/native-sdk/SKILL.md +1 -1
  47. package/src/app_runner/no_migrations.zig +3 -0
  48. package/src/app_runner/no_services.zig +20 -0
  49. package/src/app_runner/root.zig +133 -4
  50. package/src/app_runner/ts_core_main.zig +254 -8
  51. package/src/app_runner/ts_core_mobile.zig +241 -0
  52. package/src/automation/protocol.zig +5 -1
  53. package/src/automation/snapshot.zig +31 -11
  54. package/src/embed/app_exports.zig +12 -1
  55. package/src/embed/c_api.zig +48 -2
  56. package/src/embed/conversions.zig +4 -5
  57. package/src/embed/host.zig +109 -1
  58. package/src/embed/root.zig +8 -0
  59. package/src/embed/tests.zig +241 -0
  60. package/src/embed/types.zig +51 -0
  61. package/src/embed/ui_host.zig +185 -5
  62. package/src/platform/android/NativeSdkActivity.java +131 -0
  63. package/src/platform/android/android_host.c +182 -1
  64. package/src/platform/android/native_sdk_app.h +17 -0
  65. package/src/platform/ios/native_sdk_app.h +17 -0
  66. package/src/platform/ios/uikit_host.m +115 -0
  67. package/src/platform/linux/cef_host.cpp +8 -2
  68. package/src/platform/linux/gtk_host.c +289 -22
  69. package/src/platform/linux/gtk_host.h +5 -1
  70. package/src/platform/linux/root.zig +109 -22
  71. package/src/platform/macos/appkit_host.h +24 -7
  72. package/src/platform/macos/appkit_host.m +686 -64
  73. package/src/platform/macos/cef_host.mm +400 -66
  74. package/src/platform/macos/root.zig +304 -47
  75. package/src/platform/null_platform.zig +435 -48
  76. package/src/platform/null_platform_tests.zig +113 -0
  77. package/src/platform/root.zig +19 -0
  78. package/src/platform/types.zig +281 -28
  79. package/src/platform/windows/root.zig +207 -27
  80. package/src/platform/windows/webview2_host.cpp +334 -51
  81. package/src/primitives/app_manifest/comptime_scene.zig +2 -0
  82. package/src/primitives/app_manifest/root.zig +4 -0
  83. package/src/primitives/app_manifest/tests.zig +3 -0
  84. package/src/primitives/app_manifest/types.zig +32 -0
  85. package/src/primitives/app_manifest/validation.zig +19 -1
  86. package/src/root.zig +53 -0
  87. package/src/runtime/api.zig +19 -0
  88. package/src/runtime/automation_commands.zig +35 -2
  89. package/src/runtime/automation_snapshot.zig +32 -20
  90. package/src/runtime/bridge_permission_tests.zig +58 -3
  91. package/src/runtime/bridge_responses.zig +4 -2
  92. package/src/runtime/builtin_bridge.zig +11 -1
  93. package/src/runtime/canvas_frame.zig +18 -11
  94. package/src/runtime/canvas_frame_animation_tests.zig +4 -0
  95. package/src/runtime/clock.zig +13 -1
  96. package/src/runtime/core.zig +145 -7
  97. package/src/runtime/credentials_store.zig +115 -0
  98. package/src/runtime/effects.zig +3701 -231
  99. package/src/runtime/effects_credentials_tests.zig +332 -0
  100. package/src/runtime/effects_file_tests.zig +587 -0
  101. package/src/runtime/effects_host_tests.zig +208 -1
  102. package/src/runtime/effects_notification_tests.zig +11 -0
  103. package/src/runtime/effects_relational_tests.zig +314 -0
  104. package/src/runtime/effects_store_tests.zig +211 -0
  105. package/src/runtime/file_access.zig +400 -0
  106. package/src/runtime/file_effect_crash_helper.zig +28 -0
  107. package/src/runtime/file_effect_crash_tests.zig +37 -0
  108. package/src/runtime/flow.zig +35 -10
  109. package/src/runtime/gpu_surface_events.zig +6 -1
  110. package/src/runtime/native_command_event_tests.zig +86 -2
  111. package/src/runtime/persist_store.zig +666 -0
  112. package/src/runtime/pty.zig +5 -2
  113. package/src/runtime/record_store.zig +788 -0
  114. package/src/runtime/relational_store.zig +854 -0
  115. package/src/runtime/root.zig +54 -0
  116. package/src/runtime/runtime_event_tests.zig +70 -0
  117. package/src/runtime/service_host.zig +711 -0
  118. package/src/runtime/service_pool.zig +1314 -0
  119. package/src/runtime/session_blobs.zig +4 -1
  120. package/src/runtime/session_journal.zig +158 -5
  121. package/src/runtime/session_record.zig +215 -0
  122. package/src/runtime/session_replay.zig +206 -11
  123. package/src/runtime/session_tests.zig +81 -0
  124. package/src/runtime/shell_layout.zig +1 -0
  125. package/src/runtime/shell_layout_tests.zig +120 -0
  126. package/src/runtime/sqlite_engine.zig +474 -0
  127. package/src/runtime/state.zig +16 -0
  128. package/src/runtime/system_services.zig +170 -38
  129. package/src/runtime/tests.zig +4 -0
  130. package/src/runtime/ts_core_host.zig +1067 -95
  131. package/src/runtime/ts_core_host_tests.zig +325 -11
  132. package/src/runtime/ts_ui_app.zig +953 -2
  133. package/src/runtime/ui_app.zig +434 -96
  134. package/src/runtime/ui_app_tests.zig +287 -11
  135. package/src/runtime/ui_app_window_tests.zig +61 -7
  136. package/src/runtime/validation.zig +42 -0
  137. package/src/runtime/view.zig +9 -0
  138. package/src/runtime/window_storage.zig +1 -0
  139. package/src/runtime/window_view_runtime.zig +2 -3
  140. package/src/runtime/window_views.zig +19 -0
  141. package/src/tooling/android.zig +21 -16
  142. package/src/tooling/db.zig +160 -0
  143. package/src/tooling/ios.zig +7 -1
  144. package/src/tooling/manifest.zig +214 -2
  145. package/src/tooling/package.zig +116 -0
  146. package/src/tooling/raw_manifest.zig +30 -0
  147. package/src/tooling/root.zig +1 -0
  148. package/src/tooling/templates.zig +189 -4
  149. package/src/tooling/ts_core.zig +366 -17
  150. package/src/tooling/verbs.zig +7 -0
  151. package/third_party/sqlite/README.md +13 -0
  152. package/third_party/sqlite/sqlite3.c +269649 -0
  153. package/third_party/sqlite/sqlite3.h +14349 -0
  154. package/tools/corewire/core_abi.zig +14 -2
  155. package/tools/corewire/emit.zig +98 -29
  156. package/tools/corewire/emit_facade.zig +442 -27
  157. package/tools/corewire/emit_profile.zig +6 -2
  158. package/tools/corewire/emit_service.zig +924 -0
  159. package/tools/corewire/main.zig +122 -0
  160. package/tools/corewire/service_contract.zig +250 -0
  161. package/tools/corewire/sidecar.zig +26 -15
@@ -73,6 +73,10 @@ using Microsoft::WRL::ComPtr;
73
73
  #include <mferror.h>
74
74
  #include <winhttp.h>
75
75
 
76
+ #ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION
77
+ #define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002
78
+ #endif
79
+
76
80
  /* WASAPI process-scoped loopback (the spectrum analysis capture below).
77
81
  * The activation-parameter declarations arrived with the Windows 10 2004
78
82
  * SDK in audioclientactivationparams.h; the mingw-w64 headers zig ships
@@ -134,6 +138,8 @@ enum EventKind {
134
138
  kAudio = 18,
135
139
  kContextMenuAction = 19,
136
140
  kViewFocused = 20,
141
+ kTrayCommand = 21,
142
+ kNotificationCommand = 22,
137
143
  };
138
144
 
139
145
  constexpr uint32_t kShortcutModifierPrimary = 1u << 0;
@@ -452,14 +458,17 @@ struct NativeView {
452
458
  * producer that wants a frame event — runtime frame requests, pixel
453
459
  * presents (the completion analog), the pre-first-present placeholder
454
460
  * pump — funnels through gpuSurfaceScheduleFrameEmission, which keeps
455
- * at most one emission in flight (a one-shot WM_TIMER) and fires it
456
- * on the frame-interval grid anchored at gpu_last_emit_ns. Producers
457
- * landing while one is queued fold into it: their facts (nonblank,
458
- * sample color, buffer contents) are already view state when the
459
- * emission fires. gpu_presented flips on the first present and
460
- * retires the placeholder pump — from then on frames are
461
- * demand-driven, so an idle surface emits ZERO frame events (the
462
- * idle law the macOS host enforces). */
461
+ * at most one emission in flight. A process-owned high-resolution
462
+ * waitable timer wakes an idle loop; a one-shot WM_TIMER is retained as
463
+ * the compatibility fallback, and the outer message loop also drains due
464
+ * emissions after every dispatched message so sustained input cannot
465
+ * starve the lowest-priority WM_TIMER channel. Every path fires on the
466
+ * frame-interval grid anchored at gpu_last_emit_ns. Producers landing while one is queued
467
+ * fold into it: their facts (nonblank, sample color, buffer contents)
468
+ * are already view state when the emission fires. gpu_presented flips
469
+ * on the first present and retires the placeholder pump — from then on
470
+ * frames are demand-driven, so an idle surface emits ZERO frame events
471
+ * (the idle law the macOS host enforces). */
463
472
  bool gpu_emission_scheduled = false;
464
473
  bool gpu_presented = false;
465
474
  /* One-shot: the next scheduled emission must fire at grid
@@ -543,7 +552,11 @@ struct Menu {
543
552
  struct TrayItem {
544
553
  uint32_t id = 0;
545
554
  std::string label;
555
+ std::string detail;
556
+ std::string key;
557
+ uint32_t modifiers = 0;
546
558
  uint32_t command_id = 0;
559
+ int role = 0;
547
560
  bool separator = false;
548
561
  bool enabled = true;
549
562
  };
@@ -700,9 +713,15 @@ struct Host {
700
713
  std::vector<Menu> menus;
701
714
  std::map<uint32_t, std::string> menu_commands;
702
715
  std::vector<TrayItem> tray_items;
716
+ std::string tray_activation_command;
717
+ std::string tray_alternate_activation_command;
718
+ std::string tray_open_command;
703
719
  int external_link_action = 0;
704
720
  bool app_active = false;
705
721
  bool notification_icon_added = false;
722
+ std::string notification_id;
723
+ std::string notification_action_label;
724
+ std::string notification_action_command;
706
725
  bool tray_active = false;
707
726
  AppTimer app_timers[kMaxAppTimers];
708
727
  /* Last emitted appearance values; -1 = nothing emitted yet, so the
@@ -714,6 +733,10 @@ struct Host {
714
733
  /* Whether the CoInitializeEx in native_sdk_windows_create succeeded
715
734
  * and native_sdk_windows_destroy owes the balancing CoUninitialize. */
716
735
  bool com_initialized = false;
736
+ /* Earliest retained-canvas frame deadline. The handle is lazy and only
737
+ * armed while a surface has one emission in flight; MsgWait... folds it
738
+ * into the UI-thread message pump without a helper thread or callback. */
739
+ HANDLE gpu_frame_wake_timer = nullptr;
717
740
  AudioState audio;
718
741
  AudioSpectrumState spectrum;
719
742
  AudioCaptureState captures[2];
@@ -722,6 +745,8 @@ struct Host {
722
745
  };
723
746
 
724
747
  static std::string webViewKey(uint64_t window_id, const std::string &label);
748
+ static void gpuSurfaceRefreshFrameWakeTimer(Host *host);
749
+ static void gpuSurfaceDestroyFrameWakeTimer(Host *host);
725
750
 
726
751
  static std::string slice(const char *bytes, size_t len) {
727
752
  return bytes && len > 0 ? std::string(bytes, len) : std::string();
@@ -1471,8 +1496,8 @@ static std::string shortcutKeyLabel(const std::string &key) {
1471
1496
  return key;
1472
1497
  }
1473
1498
 
1474
- static std::string menuShortcutSuffix(const MenuItem &item) {
1475
- if (item.key.empty()) return std::string();
1499
+ static std::string shortcutSuffix(const std::string &key, uint32_t modifiers) {
1500
+ if (key.empty()) return std::string();
1476
1501
  std::string suffix = "\t";
1477
1502
  bool has_prefix = false;
1478
1503
  auto append = [&](const char *value) {
@@ -1480,15 +1505,19 @@ static std::string menuShortcutSuffix(const MenuItem &item) {
1480
1505
  suffix += value;
1481
1506
  has_prefix = true;
1482
1507
  };
1483
- if ((item.modifiers & kShortcutModifierPrimary) != 0 || (item.modifiers & kShortcutModifierControl) != 0) append("Ctrl");
1484
- if ((item.modifiers & kShortcutModifierCommand) != 0) append("Win");
1485
- if ((item.modifiers & kShortcutModifierOption) != 0) append("Alt");
1486
- if ((item.modifiers & kShortcutModifierShift) != 0) append("Shift");
1508
+ if ((modifiers & kShortcutModifierPrimary) != 0 || (modifiers & kShortcutModifierControl) != 0) append("Ctrl");
1509
+ if ((modifiers & kShortcutModifierCommand) != 0) append("Win");
1510
+ if ((modifiers & kShortcutModifierOption) != 0) append("Alt");
1511
+ if ((modifiers & kShortcutModifierShift) != 0) append("Shift");
1487
1512
  if (has_prefix) suffix += "+";
1488
- suffix += shortcutKeyLabel(item.key);
1513
+ suffix += shortcutKeyLabel(key);
1489
1514
  return suffix;
1490
1515
  }
1491
1516
 
1517
+ static std::string menuShortcutSuffix(const MenuItem &item) {
1518
+ return shortcutSuffix(item.key, item.modifiers);
1519
+ }
1520
+
1492
1521
  static HMENU buildMenuBar(Host *host) {
1493
1522
  if (!host || host->menus.empty()) return nullptr;
1494
1523
  HMENU menu_bar = CreateMenu();
@@ -1542,9 +1571,9 @@ static bool emitMenuCommandForId(Host *host, HWND hwnd, uint32_t command_id) {
1542
1571
  return true;
1543
1572
  }
1544
1573
 
1545
- static bool emitTrayActionForCommandId(Host *host, uint32_t command_id) {
1574
+ static bool emitTrayActionForCommandId(Host *host, const std::vector<TrayItem> &items, uint32_t command_id) {
1546
1575
  if (!host || !host->callback) return false;
1547
- for (const TrayItem &item : host->tray_items) {
1576
+ for (const TrayItem &item : items) {
1548
1577
  if (item.separator || item.command_id != command_id) continue;
1549
1578
  WindowsEvent event = {};
1550
1579
  event.kind = kTrayAction;
@@ -1555,18 +1584,57 @@ static bool emitTrayActionForCommandId(Host *host, uint32_t command_id) {
1555
1584
  return false;
1556
1585
  }
1557
1586
 
1587
+ static bool emitStatusCommand(Host *host, HWND hwnd, const std::string &command) {
1588
+ if (!host || !host->callback || command.empty()) return false;
1589
+ const Window *window = windowForHwnd(host, hwnd);
1590
+ if (!window) return false;
1591
+ WindowsEvent event = {};
1592
+ event.kind = kTrayCommand;
1593
+ event.window_id = window->id;
1594
+ event.command_name = command.c_str();
1595
+ event.command_name_len = command.size();
1596
+ host->callback(host->callback_context, &event);
1597
+ return true;
1598
+ }
1599
+
1600
+ static bool emitNotificationCommand(Host *host, HWND hwnd) {
1601
+ if (!host || !host->callback || host->notification_action_command.empty()) return false;
1602
+ std::string command = host->notification_action_command;
1603
+ host->notification_id.clear();
1604
+ host->notification_action_label.clear();
1605
+ host->notification_action_command.clear();
1606
+ const Window *window = windowForHwnd(host, hwnd);
1607
+ WindowsEvent event = {};
1608
+ event.kind = kNotificationCommand;
1609
+ event.window_id = window ? window->id : 1;
1610
+ event.command_name = command.c_str();
1611
+ event.command_name_len = command.size();
1612
+ host->callback(host->callback_context, &event);
1613
+ return true;
1614
+ }
1615
+
1558
1616
  static void showTrayMenu(Host *host, HWND hwnd) {
1559
- if (!host || !host->tray_active || host->tray_items.empty() || !hwnd) return;
1617
+ if (!host || !host->tray_active || !hwnd) return;
1618
+ emitStatusCommand(host, hwnd, host->tray_open_command);
1619
+ // The open hook may synchronously rebuild or remove the tray.
1620
+ if (!host->tray_active || host->tray_items.empty()) return;
1621
+ // TrackPopupMenu owns a nested message loop. Timers and posted wakes can
1622
+ // rebuild host->tray_items while this menu is open, so retain the exact
1623
+ // command-id -> stable item-id mapping represented by the visible rows.
1624
+ const std::vector<TrayItem> displayed_items = host->tray_items;
1560
1625
  HMENU menu = CreatePopupMenu();
1561
1626
  if (!menu) return;
1562
- for (const TrayItem &item : host->tray_items) {
1627
+ for (const TrayItem &item : displayed_items) {
1563
1628
  if (item.separator) {
1564
1629
  AppendMenuW(menu, MF_SEPARATOR, 0, nullptr);
1565
1630
  continue;
1566
1631
  }
1567
1632
  UINT flags = MF_STRING;
1568
1633
  if (!item.enabled) flags |= MF_GRAYED;
1569
- std::wstring label = widen(item.label);
1634
+ std::string display_label = item.label;
1635
+ if (!item.detail.empty()) display_label += " — " + item.detail;
1636
+ display_label += shortcutSuffix(item.key, item.modifiers);
1637
+ std::wstring label = widen(display_label);
1570
1638
  AppendMenuW(menu, flags, item.command_id, label.c_str());
1571
1639
  }
1572
1640
  POINT cursor = {};
@@ -1574,12 +1642,12 @@ static void showTrayMenu(Host *host, HWND hwnd) {
1574
1642
  SetForegroundWindow(hwnd);
1575
1643
  UINT command_id = TrackPopupMenu(menu, TPM_RETURNCMD | TPM_NONOTIFY | TPM_RIGHTBUTTON, cursor.x, cursor.y, 0, hwnd, nullptr);
1576
1644
  DestroyMenu(menu);
1577
- if (command_id != 0) emitTrayActionForCommandId(host, command_id);
1645
+ if (command_id != 0) emitTrayActionForCommandId(host, displayed_items, command_id);
1578
1646
  PostMessageW(hwnd, WM_NULL, 0, 0);
1579
1647
  }
1580
1648
 
1581
1649
  static bool emitShortcutForWindow(Host *host, const Window *window, WPARAM wparam) {
1582
- if (!host || (host->shortcuts.empty() && host->menus.empty())) return false;
1650
+ if (!host || (host->shortcuts.empty() && host->menus.empty() && host->tray_items.empty())) return false;
1583
1651
  if (!window) return false;
1584
1652
  std::string key = shortcutKeyFromWParam(wparam);
1585
1653
  if (key.empty()) return false;
@@ -1622,6 +1690,15 @@ static bool emitShortcutForWindow(Host *host, const Window *window, WPARAM wpara
1622
1690
  return true;
1623
1691
  }
1624
1692
  }
1693
+ // Status-menu key equivalents share the focused-window key path on
1694
+ // Windows. AppKit gets this behavior from NSMenu; Win32 popup menus
1695
+ // need the binding dispatched explicitly.
1696
+ for (const TrayItem &item : host->tray_items) {
1697
+ if (item.separator || !item.enabled || item.key.empty()) continue;
1698
+ if (item.key != key) continue;
1699
+ if (!shortcutModifiersMatch(item.modifiers, allow_implicit_shift)) continue;
1700
+ return emitTrayActionForCommandId(host, host->tray_items, item.command_id);
1701
+ }
1625
1702
  }
1626
1703
  return false;
1627
1704
  }
@@ -1947,6 +2024,7 @@ static void destroyNativeViewAndChildren(Host *host, const std::string &key) {
1947
2024
  for (const std::string &child : children) destroyNativeViewAndChildren(host, child);
1948
2025
  if (found->second.hwnd) DestroyWindow(found->second.hwnd);
1949
2026
  host->native_views.erase(found);
2027
+ gpuSurfaceRefreshFrameWakeTimer(host);
1950
2028
  }
1951
2029
 
1952
2030
  static void destroyNativeViewsForWindow(Host *host, uint64_t window_id) {
@@ -1970,13 +2048,13 @@ static void destroyNativeViewsForWindow(Host *host, uint64_t window_id) {
1970
2048
  * buffer is still valid. Frame events are DEMAND-DRIVEN through one
1971
2049
  * scheduler per surface (the macOS host's design): runtime frame
1972
2050
  * requests and pixel presents each arm a single grid-anchored one-shot
1973
- * WM_TIMER emission, so an armed animation loop sees one
2051
+ * emission, so an armed animation loop sees one
1974
2052
  * gpu_surface_frame per frame interval and an idle surface sees none.
1975
2053
  * Until the first present lands, a placeholder 16 ms WM_TIMER pump arms
1976
2054
  * the same scheduler (the runtime's install choreography rides the
1977
- * first frame events), then removes itself. WM_TIMER granularity
1978
- * (~10-16 ms, coalesced under load) quantizes individual periods; the
1979
- * grid-anchored clock turns that into jitter, never drift.
2055
+ * first frame events), then removes itself. The scheduler's high-resolution
2056
+ * waitable deadline avoids WM_TIMER's ~10-16 ms quantization while the
2057
+ * grid-anchored clock still prevents drift.
1980
2058
  * gpu_surface_resize rides WM_SIZE/DPI changes plus each emission's
1981
2059
  * geometry sync. Mouse, wheel, and
1982
2060
  * key input map onto the same gpu_surface_input kinds the other hosts
@@ -2734,6 +2812,60 @@ static bool gpuSurfaceOccludedPacingActive(Host *host, const NativeView &view) {
2734
2812
  return root != nullptr && IsIconic(root);
2735
2813
  }
2736
2814
 
2815
+ /* SetTimer can round a nominal 16.67 ms frame delay onto the legacy system
2816
+ * timer grid. A high-resolution waitable timer expresses the EARLIEST pending
2817
+ * surface deadline directly and participates
2818
+ * in the UI thread's MsgWaitForMultipleObjectsEx loop. The per-HWND WM_TIMER
2819
+ * remains armed as a fallback for older Windows/builds that reject the high-
2820
+ * resolution flag; it is not the normal cadence source.
2821
+ *
2822
+ * This is one process-owned handle, not one timer per surface. Refreshing
2823
+ * scans the small native-view table and re-arms to the earliest single-flight
2824
+ * deadline. Callback-driven view destruction is safe because no NativeView
2825
+ * pointer crosses the wait. */
2826
+ static void gpuSurfaceRefreshFrameWakeTimer(Host *host) {
2827
+ if (!host) return;
2828
+ const uint64_t now = gpuTimestampNs();
2829
+ uint64_t earliest_ns = UINT64_MAX;
2830
+ for (const auto &entry : host->native_views) {
2831
+ const NativeView &view = entry.second;
2832
+ if (view.kind != kViewGpuSurface || !view.hwnd || !view.gpu_emission_scheduled) continue;
2833
+ const uint64_t pace_ns = (!view.gpu_prompt_frame_pending && gpuSurfaceOccludedPacingActive(host, view)) ? kGpuOccludedHeartbeatNs : kGpuFrameIntervalNs;
2834
+ const uint64_t due_ns = view.gpu_last_emit_ns == 0 ? now : view.gpu_last_emit_ns + pace_ns;
2835
+ earliest_ns = std::min(earliest_ns, due_ns);
2836
+ }
2837
+
2838
+ if (earliest_ns == UINT64_MAX) {
2839
+ if (host->gpu_frame_wake_timer) CancelWaitableTimer(host->gpu_frame_wake_timer);
2840
+ return;
2841
+ }
2842
+ if (!host->gpu_frame_wake_timer) {
2843
+ host->gpu_frame_wake_timer = CreateWaitableTimerExW(
2844
+ nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION,
2845
+ TIMER_MODIFY_STATE | SYNCHRONIZE);
2846
+ if (!host->gpu_frame_wake_timer) {
2847
+ host->gpu_frame_wake_timer = CreateWaitableTimerW(nullptr, FALSE, nullptr);
2848
+ }
2849
+ if (!host->gpu_frame_wake_timer) return;
2850
+ }
2851
+
2852
+ const uint64_t wait_ns = earliest_ns > now ? earliest_ns - now : 1;
2853
+ const uint64_t due_100ns = std::max<uint64_t>(1, (wait_ns + 99) / 100);
2854
+ LARGE_INTEGER due = {};
2855
+ due.QuadPart = -static_cast<LONGLONG>(std::min<uint64_t>(due_100ns, LLONG_MAX));
2856
+ if (!SetWaitableTimer(host->gpu_frame_wake_timer, &due, 0, nullptr, nullptr, FALSE)) {
2857
+ CloseHandle(host->gpu_frame_wake_timer);
2858
+ host->gpu_frame_wake_timer = nullptr;
2859
+ }
2860
+ }
2861
+
2862
+ static void gpuSurfaceDestroyFrameWakeTimer(Host *host) {
2863
+ if (!host || !host->gpu_frame_wake_timer) return;
2864
+ CancelWaitableTimer(host->gpu_frame_wake_timer);
2865
+ CloseHandle(host->gpu_frame_wake_timer);
2866
+ host->gpu_frame_wake_timer = nullptr;
2867
+ }
2868
+
2737
2869
  /* The single frame-event emission: view state (nonblank verdict, sample
2738
2870
  * color, buffer geometry) is the payload, so one event serves frame
2739
2871
  * requests and present completions alike. */
@@ -2788,8 +2920,8 @@ static void gpuSurfaceEmitFrame(Host *host, NativeView &view, HWND hwnd) {
2788
2920
  * is queued fold into it. Always fires through the message loop — a
2789
2921
  * request lands mid engine dispatch and a synchronous emission would
2790
2922
  * re-enter the engine — and the pacing clock's grid stamping keeps the
2791
- * message hop out of the period. SetTimer clamps short delays up to its
2792
- * ~10 ms floor; the clock absorbs that as jitter, not drift. */
2923
+ * message hop out of the period. The waitable timer supplies the precise
2924
+ * wake; SetTimer is only the compatibility fallback. */
2793
2925
  static void gpuSurfaceScheduleFrameEmission(Host *host, NativeView &view) {
2794
2926
  if (!view.hwnd || view.gpu_emission_scheduled) return;
2795
2927
  const uint64_t now = gpuTimestampNs();
@@ -2806,9 +2938,55 @@ static void gpuSurfaceScheduleFrameEmission(Host *host, NativeView &view) {
2806
2938
  delay_ns = view.gpu_last_emit_ns + pace_ns - now;
2807
2939
  }
2808
2940
  const UINT delay_ms = (UINT)((delay_ns + 500000ull) / 1000000ull);
2809
- if (SetTimer(view.hwnd, kGpuEmitTimerId, delay_ms, nullptr)) {
2810
- view.gpu_emission_scheduled = true;
2941
+ view.gpu_emission_scheduled = true;
2942
+ (void)SetTimer(view.hwnd, kGpuEmitTimerId, delay_ms, nullptr);
2943
+ gpuSurfaceRefreshFrameWakeTimer(host);
2944
+ }
2945
+
2946
+ /* WM_TIMER is generated only after higher-priority input/posted messages
2947
+ * have drained. A trackpad or high-rate wheel can therefore keep the
2948
+ * one-shot emit timer pending for hundreds of milliseconds even though the
2949
+ * frame deadline passed. The outer GetMessage loop calls this after EVERY
2950
+ * dispatched message: while the queue is busy, that dispatch becomes the
2951
+ * wake source; while it is idle, the existing timer wakes it. Emission stays
2952
+ * outside the platform callback that requested the frame, so the runtime is
2953
+ * never re-entered, and the existing single-flight/grid clock still owns
2954
+ * coalescing and pacing.
2955
+ *
2956
+ * Capture stable map keys before emitting. A frame callback may close its
2957
+ * own view (or another due view), so each key is re-resolved immediately
2958
+ * before use and no iterator/reference survives an engine callback. */
2959
+ static void gpuSurfaceDrainDueFrameEmissions(Host *host) {
2960
+ if (!host || !host->running) return;
2961
+ const uint64_t now = gpuTimestampNs();
2962
+ std::vector<std::string> due_keys;
2963
+ for (const auto &entry : host->native_views) {
2964
+ const NativeView &view = entry.second;
2965
+ if (view.kind != kViewGpuSurface || !view.hwnd || !view.gpu_emission_scheduled) continue;
2966
+ const uint64_t pace_ns = (!view.gpu_prompt_frame_pending && gpuSurfaceOccludedPacingActive(host, view)) ? kGpuOccludedHeartbeatNs : kGpuFrameIntervalNs;
2967
+ if (view.gpu_last_emit_ns == 0 || now >= view.gpu_last_emit_ns + pace_ns) {
2968
+ due_keys.push_back(entry.first);
2969
+ }
2970
+ }
2971
+ for (const std::string &key : due_keys) {
2972
+ /* Emission enters application code synchronously. A preceding due
2973
+ * surface may have stopped the host, so never begin another callback
2974
+ * after shutdown/failure. */
2975
+ if (!host->running) return;
2976
+ auto found = host->native_views.find(key);
2977
+ if (found == host->native_views.end()) continue;
2978
+ NativeView &view = found->second;
2979
+ if (view.kind != kViewGpuSurface || !view.hwnd || !view.gpu_emission_scheduled) continue;
2980
+ const uint64_t current_ns = gpuTimestampNs();
2981
+ const uint64_t pace_ns = (!view.gpu_prompt_frame_pending && gpuSurfaceOccludedPacingActive(host, view)) ? kGpuOccludedHeartbeatNs : kGpuFrameIntervalNs;
2982
+ if (view.gpu_last_emit_ns > 0 && current_ns < view.gpu_last_emit_ns + pace_ns) continue;
2983
+ const HWND hwnd = view.hwnd;
2984
+ KillTimer(hwnd, kGpuEmitTimerId);
2985
+ view.gpu_emission_scheduled = false;
2986
+ gpuSurfaceEmitFrame(host, view, hwnd);
2987
+ if (!host->running) return;
2811
2988
  }
2989
+ gpuSurfaceRefreshFrameWakeTimer(host);
2812
2990
  }
2813
2991
 
2814
2992
  static void paintGpuSurface(NativeView &view, HWND hwnd, HDC dc) {
@@ -3056,7 +3234,7 @@ static bool presentTransparentWindow(Host *host, Window &window) {
3056
3234
  }
3057
3235
 
3058
3236
  static void showWindowImplicit(Window &window) {
3059
- if (!window.hwnd || window.shown) return;
3237
+ if (!window.hwnd || window.shown || window.policy_hidden) return;
3060
3238
  ShowWindow(window.hwnd, window.activate_on_show ? SW_SHOW : SW_SHOWNOACTIVATE);
3061
3239
  window.shown = true;
3062
3240
  window.deferred_show_started_ms = 0;
@@ -3149,6 +3327,7 @@ static LRESULT CALLBACK gpuSurfaceProc(HWND hwnd, UINT message, WPARAM wparam, L
3149
3327
  KillTimer(hwnd, kGpuEmitTimerId);
3150
3328
  view->gpu_emission_scheduled = false;
3151
3329
  gpuSurfaceEmitFrame(host, *view, hwnd);
3330
+ if (host->running) gpuSurfaceRefreshFrameWakeTimer(host);
3152
3331
  return 0;
3153
3332
  }
3154
3333
  break;
@@ -4936,7 +5115,7 @@ static const wchar_t *nativeSdkBridgeScript() {
4936
5115
  });
4937
5116
  var os=Object.freeze({
4938
5117
  openUrl:function(value){var options=typeof value==='string'?{url:value}:(value||{});return invoke('native-sdk.os.openUrl',{url:ensureString(options.url,'url')});},
4939
- 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);},
5118
+ showNotification:function(value){var options=typeof value==='string'?{title:value}:(value||{});var payload={title:ensureString(options.title,'title')};if(options.id!=null){payload.id=ensureString(options.id,'id');}if(options.subtitle!=null){payload.subtitle=ensureString(options.subtitle,'subtitle');}if(options.body!=null){payload.body=ensureString(options.body,'body');}if(options.actionLabel!=null){payload.actionLabel=ensureString(options.actionLabel,'actionLabel');}if(options.actionCommand!=null){payload.actionCommand=ensureString(options.actionCommand,'actionCommand');}return invoke('native-sdk.os.showNotification',payload);},
4940
5119
  revealPath:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.revealPath',{path:ensureString(options.path,'path')});},
4941
5120
  addRecentDocument:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.addRecentDocument',{path:ensureString(options.path,'path')});},
4942
5121
  clearRecentDocuments:function(){return invoke('native-sdk.os.clearRecentDocuments',{});}
@@ -5529,9 +5708,22 @@ static LRESULT CALLBACK windowProc(HWND hwnd, UINT message, WPARAM wparam, LPARA
5529
5708
  if (host) showAppContextMenu(host, hwnd);
5530
5709
  return 0;
5531
5710
  case kNotificationCallbackMessage:
5532
- if (host && host->tray_active) {
5711
+ if (host) {
5533
5712
  UINT tray_event = LOWORD(lparam);
5534
- if (tray_event == WM_CONTEXTMENU || tray_event == WM_RBUTTONUP || tray_event == WM_LBUTTONUP) {
5713
+ if (tray_event == NIN_BALLOONUSERCLICK && emitNotificationCommand(host, hwnd)) {
5714
+ return 0;
5715
+ }
5716
+ if (!host->tray_active) break;
5717
+ if (tray_event == NIN_SELECT || tray_event == NIN_KEYSELECT || tray_event == WM_LBUTTONUP) {
5718
+ if (keyDown(VK_MENU) && !host->tray_alternate_activation_command.empty()) {
5719
+ emitStatusCommand(host, hwnd, host->tray_alternate_activation_command);
5720
+ return 0;
5721
+ }
5722
+ emitStatusCommand(host, hwnd, host->tray_activation_command);
5723
+ showTrayMenu(host, hwnd);
5724
+ return 0;
5725
+ }
5726
+ if (tray_event == WM_CONTEXTMENU || tray_event == WM_RBUTTONUP) {
5535
5727
  showTrayMenu(host, hwnd);
5536
5728
  return 0;
5537
5729
  }
@@ -5915,7 +6107,7 @@ static bool createNativeWindow(Host *host, Window &window) {
5915
6107
  window.hwnd = nullptr;
5916
6108
  return false;
5917
6109
  }
5918
- if (!window.show_on_first_present) {
6110
+ if (!window.show_on_first_present && !window.policy_hidden) {
5919
6111
  showWindowImplicit(window);
5920
6112
  }
5921
6113
  SetTimer(hwnd, kFrameTimerId, 16, nullptr);
@@ -5976,6 +6168,7 @@ Host *native_sdk_windows_create(const char *app_name, size_t app_name_len, const
5976
6168
  window.click_through = (window_flags & (1u << 2)) != 0;
5977
6169
  window.activate_on_show = (window_flags & (1u << 3)) == 0;
5978
6170
  window.show_on_first_present = show_policy == 1;
6171
+ window.policy_hidden = show_policy == 2;
5979
6172
  host->windows[window.id] = window;
5980
6173
  return host;
5981
6174
  }
@@ -6004,6 +6197,7 @@ void native_sdk_windows_destroy(Host *host) {
6004
6197
  audioReleaseSession(host, true);
6005
6198
  removeNotificationIcon(host);
6006
6199
  destroyAllWindows(host);
6200
+ gpuSurfaceDestroyFrameWakeTimer(host);
6007
6201
  const bool com_initialized = host->com_initialized;
6008
6202
  delete host;
6009
6203
  if (com_initialized) CoUninitialize();
@@ -6025,9 +6219,23 @@ void native_sdk_windows_run(Host *host, EventCallback callback, void *context) {
6025
6219
  emit(host, entry.second, kWindowFrame);
6026
6220
  }
6027
6221
  MSG message = {};
6028
- while (host->running && GetMessageW(&message, nullptr, 0, 0) > 0) {
6222
+ while (host->running) {
6223
+ HANDLE handles[1] = {};
6224
+ const DWORD handle_count = host->gpu_frame_wake_timer ? 1 : 0;
6225
+ if (handle_count != 0) handles[0] = host->gpu_frame_wake_timer;
6226
+ const DWORD wait_result = MsgWaitForMultipleObjectsEx(
6227
+ handle_count, handles, INFINITE, QS_ALLINPUT, MWMO_INPUTAVAILABLE);
6228
+ if (wait_result == WAIT_FAILED) break;
6229
+ if (handle_count != 0 && wait_result == WAIT_OBJECT_0) {
6230
+ gpuSurfaceDrainDueFrameEmissions(host);
6231
+ continue;
6232
+ }
6233
+ if (wait_result != WAIT_OBJECT_0 + handle_count) continue;
6234
+ const BOOL received = GetMessageW(&message, nullptr, 0, 0);
6235
+ if (received <= 0) break;
6029
6236
  TranslateMessage(&message);
6030
6237
  DispatchMessageW(&message);
6238
+ if (host->running) gpuSurfaceDrainDueFrameEmissions(host);
6031
6239
  }
6032
6240
  WindowsEvent shutdown = {};
6033
6241
  shutdown.kind = kShutdown;
@@ -6367,6 +6575,7 @@ int native_sdk_windows_create_window(Host *host, uint64_t window_id, const char
6367
6575
  window.click_through = (window_flags & (1u << 2)) != 0;
6368
6576
  window.activate_on_show = (window_flags & (1u << 3)) == 0;
6369
6577
  window.show_on_first_present = show_policy == 1;
6578
+ window.policy_hidden = show_policy == 2;
6370
6579
  /* Register BEFORE creating: createNativeWindow's post-create frame
6371
6580
  * pass (hidden titlebar styles) resolves the window through the map
6372
6581
  * by HWND, so the stored entry must be the one it mutates. */
@@ -6514,6 +6723,7 @@ int native_sdk_windows_focus_window(Host *host, uint64_t window_id) {
6514
6723
  if (!host) return 0;
6515
6724
  auto found = host->windows.find(window_id);
6516
6725
  if (found == host->windows.end() || !found->second.hwnd) return 0;
6726
+ found->second.policy_hidden = false;
6517
6727
  if (!found->second.shown) {
6518
6728
  ShowWindow(found->second.hwnd, SW_SHOW);
6519
6729
  found->second.shown = true;
@@ -6545,6 +6755,17 @@ int native_sdk_windows_minimize_window(Host *host, uint64_t window_id) {
6545
6755
  return 1;
6546
6756
  }
6547
6757
 
6758
+ int native_sdk_windows_hide_window(Host *host, uint64_t window_id) {
6759
+ if (!host) return 0;
6760
+ auto found = host->windows.find(window_id);
6761
+ if (found == host->windows.end() || !found->second.hwnd) return 0;
6762
+ found->second.policy_hidden = true;
6763
+ found->second.shown = false;
6764
+ ShowWindow(found->second.hwnd, SW_HIDE);
6765
+ emit(host, found->second, kWindowFrame);
6766
+ return 1;
6767
+ }
6768
+
6548
6769
  /* The show verb: bring the window back to the glass and foreground it —
6549
6770
  * the counterpart to a close_policy hide (and the tray-menu "Open"
6550
6771
  * consequence). Restores a minimized window on the way. */
@@ -7255,9 +7476,13 @@ int native_sdk_windows_show_message_dialog(Host *host, const WindowsMessageDialo
7255
7476
  return 0;
7256
7477
  }
7257
7478
 
7258
- int native_sdk_windows_show_notification(Host *host, const char *title, size_t title_len, const char *subtitle, size_t subtitle_len, const char *body, size_t body_len) {
7479
+ int native_sdk_windows_show_notification(Host *host, const char *title, size_t title_len, const char *subtitle, size_t subtitle_len, const char *body, size_t body_len, const char *notification_id, size_t notification_id_len, const char *action_label, size_t action_label_len, const char *action_command, size_t action_command_len) {
7259
7480
  if (!host || !title || title_len == 0) return 0;
7260
- if ((subtitle_len > 0 && !subtitle) || (body_len > 0 && !body)) return 0;
7481
+ if ((subtitle_len > 0 && !subtitle) || (body_len > 0 && !body) ||
7482
+ (notification_id_len > 0 && !notification_id) ||
7483
+ (action_label_len > 0 && !action_label) ||
7484
+ (action_command_len > 0 && !action_command) ||
7485
+ ((action_label_len == 0) != (action_command_len == 0))) return 0;
7261
7486
  if (!ensureNotificationIcon(host)) return 0;
7262
7487
 
7263
7488
  std::wstring title_wide = widen(slice(title, title_len));
@@ -7265,6 +7490,10 @@ int native_sdk_windows_show_notification(Host *host, const char *title, size_t t
7265
7490
  if (subtitle && subtitle_len > 0) body_wide += widen(slice(subtitle, subtitle_len));
7266
7491
  if (subtitle_len > 0 && body_len > 0) body_wide += L"\n";
7267
7492
  if (body && body_len > 0) body_wide += widen(slice(body, body_len));
7493
+ if (action_label_len > 0) {
7494
+ if (!body_wide.empty()) body_wide += L"\n\n";
7495
+ body_wide += L"[" + widen(slice(action_label, action_label_len)) + L"]";
7496
+ }
7268
7497
 
7269
7498
  NOTIFYICONDATAW data = notificationIconData(host);
7270
7499
  if (!data.hWnd) return 0;
@@ -7272,27 +7501,40 @@ int native_sdk_windows_show_notification(Host *host, const char *title, size_t t
7272
7501
  data.dwInfoFlags = NIIF_INFO;
7273
7502
  copyWideField(data.szInfoTitle, ARRAYSIZE(data.szInfoTitle), title_wide);
7274
7503
  copyWideField(data.szInfo, ARRAYSIZE(data.szInfo), body_wide);
7275
- return Shell_NotifyIconW(NIM_MODIFY, &data) ? 1 : 0;
7504
+ if (!Shell_NotifyIconW(NIM_MODIFY, &data)) return 0;
7505
+ host->notification_id = slice(notification_id, notification_id_len);
7506
+ host->notification_action_label = slice(action_label, action_label_len);
7507
+ host->notification_action_command = slice(action_command, action_command_len);
7508
+ return 1;
7276
7509
  }
7277
7510
 
7278
- int native_sdk_windows_create_tray(Host *host, const char *icon_path, size_t icon_path_len, const char *tooltip, size_t tooltip_len) {
7511
+ int native_sdk_windows_create_tray(Host *host, const char *icon_path, size_t icon_path_len, const char *tooltip, size_t tooltip_len, const char *activation_command, size_t activation_command_len, const char *alternate_activation_command, size_t alternate_activation_command_len, const char *open_command, size_t open_command_len) {
7279
7512
  if (!host) return 0;
7280
7513
  std::string icon = slice(icon_path, icon_path_len);
7281
7514
  std::string tip = slice(tooltip, tooltip_len);
7282
7515
  if (!setNotificationIcon(host, icon, tip, true)) return 0;
7516
+ host->tray_activation_command = slice(activation_command, activation_command_len);
7517
+ host->tray_alternate_activation_command = slice(alternate_activation_command, alternate_activation_command_len);
7518
+ host->tray_open_command = slice(open_command, open_command_len);
7283
7519
  host->tray_active = true;
7284
7520
  return 1;
7285
7521
  }
7286
7522
 
7287
- int native_sdk_windows_update_tray_menu(Host *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) {
7523
+ int native_sdk_windows_update_tray_menu(Host *host, const uint32_t *item_ids, const char *const *labels, const size_t *label_lens, const int *separators, const int *enabled_flags, const char *const *details, const size_t *detail_lens, const int *roles, const char *const *keys, const size_t *key_lens, const uint32_t *modifiers, size_t count) {
7288
7524
  if (!host || !host->tray_active) return 0;
7289
7525
  host->tray_items.clear();
7290
- if (count > 0 && (!item_ids || !labels || !label_lens || !separators || !enabled_flags)) return 0;
7526
+ if (count > 0 && (!item_ids || !labels || !label_lens || !separators || !enabled_flags || !details || !detail_lens || !roles || !keys || !key_lens || !modifiers)) return 0;
7291
7527
  host->tray_items.reserve(count);
7292
7528
  for (size_t index = 0; index < count; ++index) {
7293
7529
  TrayItem item;
7294
7530
  item.id = item_ids[index];
7295
7531
  item.label = slice(labels[index], label_lens[index]);
7532
+ item.detail = slice(details[index], detail_lens[index]);
7533
+ item.role = roles[index];
7534
+ // Shortcut validation accepts ASCII uppercase keys, while Windows key
7535
+ // events are normalized to lowercase before matching.
7536
+ item.key = lowerAscii(slice(keys[index], key_lens[index]));
7537
+ item.modifiers = modifiers[index];
7296
7538
  item.separator = separators[index] != 0;
7297
7539
  item.enabled = enabled_flags[index] != 0;
7298
7540
  if (!item.separator) item.command_id = kTrayCommandBase + static_cast<uint32_t>(index);
@@ -7305,6 +7547,9 @@ void native_sdk_windows_remove_tray(Host *host) {
7305
7547
  if (!host) return;
7306
7548
  host->tray_active = false;
7307
7549
  host->tray_items.clear();
7550
+ host->tray_activation_command.clear();
7551
+ host->tray_alternate_activation_command.clear();
7552
+ host->tray_open_command.clear();
7308
7553
  removeNotificationIcon(host);
7309
7554
  }
7310
7555
 
@@ -7341,7 +7586,8 @@ int native_sdk_windows_clear_recent_documents(Host *host) {
7341
7586
 
7342
7587
  int native_sdk_windows_set_credential(Host *host, const char *service, size_t service_len, const char *account, size_t account_len, const char *secret, size_t secret_len) {
7343
7588
  (void)host;
7344
- if (!service || service_len == 0 || !account || account_len == 0 || !secret || secret_len == 0 || secret_len > UINT32_MAX) return 0;
7589
+ if (!service || service_len == 0 || !account || account_len == 0 || (!secret && secret_len > 0) || secret_len > UINT32_MAX) return 0;
7590
+ if (secret_len > CRED_MAX_CREDENTIAL_BLOB_SIZE) return -4;
7345
7591
  HMODULE advapi = LoadLibraryW(L"advapi32.dll");
7346
7592
  if (!advapi) return 0;
7347
7593
  auto cred_write = reinterpret_cast<BOOL (WINAPI *)(PCREDENTIALW, DWORD)>(GetProcAddress(advapi, "CredWriteW"));
@@ -7363,28 +7609,32 @@ int native_sdk_windows_set_credential(Host *host, const char *service, size_t se
7363
7609
  credential.UserName = const_cast<LPWSTR>(user_name.c_str());
7364
7610
 
7365
7611
  BOOL ok = cred_write(&credential, 0);
7612
+ DWORD error = ok ? ERROR_SUCCESS : GetLastError();
7366
7613
  FreeLibrary(advapi);
7367
- return ok ? 1 : 0;
7614
+ if (ok) return 1;
7615
+ return error == ERROR_ACCESS_DENIED ? -3 : -1;
7368
7616
  }
7369
7617
 
7370
7618
  size_t native_sdk_windows_get_credential(Host *host, const char *service, size_t service_len, const char *account, size_t account_len, char *buffer, size_t buffer_len) {
7371
7619
  (void)host;
7372
- if (!service || service_len == 0 || !account || account_len == 0 || !buffer) return 0;
7620
+ if (!service || service_len == 0 || !account || account_len == 0 || (!buffer && buffer_len > 0)) return SIZE_MAX - 1;
7373
7621
  HMODULE advapi = LoadLibraryW(L"advapi32.dll");
7374
- if (!advapi) return 0;
7622
+ if (!advapi) return SIZE_MAX - 1;
7375
7623
  auto cred_read = reinterpret_cast<BOOL (WINAPI *)(LPCWSTR, DWORD, DWORD, PCREDENTIALW *)>(GetProcAddress(advapi, "CredReadW"));
7376
7624
  auto cred_free = reinterpret_cast<void (WINAPI *)(PVOID)>(GetProcAddress(advapi, "CredFree"));
7377
7625
  if (!cred_read || !cred_free) {
7378
7626
  FreeLibrary(advapi);
7379
- return 0;
7627
+ return SIZE_MAX - 1;
7380
7628
  }
7381
7629
 
7382
7630
  std::wstring target = credentialTarget(slice(service, service_len), slice(account, account_len));
7383
7631
  PCREDENTIALW credential = nullptr;
7384
7632
  BOOL ok = cred_read(target.c_str(), CRED_TYPE_GENERIC, 0, &credential);
7385
7633
  if (!ok || !credential) {
7634
+ DWORD error = GetLastError();
7386
7635
  FreeLibrary(advapi);
7387
- return 0;
7636
+ if (error == ERROR_NOT_FOUND) return SIZE_MAX;
7637
+ return error == ERROR_ACCESS_DENIED ? SIZE_MAX - 3 : SIZE_MAX - 1;
7388
7638
  }
7389
7639
 
7390
7640
  size_t secret_len = credential->CredentialBlobSize;
@@ -7403,17 +7653,50 @@ int native_sdk_windows_delete_credential(Host *host, const char *service, size_t
7403
7653
  (void)host;
7404
7654
  if (!service || service_len == 0 || !account || account_len == 0) return 0;
7405
7655
  HMODULE advapi = LoadLibraryW(L"advapi32.dll");
7406
- if (!advapi) return 0;
7656
+ if (!advapi) return -1;
7407
7657
  auto cred_delete = reinterpret_cast<BOOL (WINAPI *)(LPCWSTR, DWORD, DWORD)>(GetProcAddress(advapi, "CredDeleteW"));
7408
7658
  if (!cred_delete) {
7409
7659
  FreeLibrary(advapi);
7410
- return 0;
7660
+ return -1;
7411
7661
  }
7412
7662
 
7413
7663
  std::wstring target = credentialTarget(slice(service, service_len), slice(account, account_len));
7414
7664
  BOOL ok = cred_delete(target.c_str(), CRED_TYPE_GENERIC, 0);
7665
+ DWORD error = ok ? ERROR_SUCCESS : GetLastError();
7415
7666
  FreeLibrary(advapi);
7416
- return ok ? 1 : 0;
7667
+ if (ok) return 1;
7668
+ if (error == ERROR_NOT_FOUND) return 0;
7669
+ return error == ERROR_ACCESS_DENIED ? -3 : -1;
7670
+ }
7671
+
7672
+ size_t native_sdk_windows_format_local_time(Host *host, int64_t timestamp_ms, int style, char *buffer, size_t buffer_len) {
7673
+ (void)host;
7674
+ if (!buffer || buffer_len == 0 || style < 0 || style > 2) return 0;
7675
+
7676
+ const long double ticks_value = static_cast<long double>(timestamp_ms) * 10000.0L + 116444736000000000.0L;
7677
+ if (ticks_value < 0.0L || ticks_value > static_cast<long double>(UINT64_MAX)) return 0;
7678
+ ULARGE_INTEGER ticks = {};
7679
+ ticks.QuadPart = static_cast<uint64_t>(ticks_value);
7680
+ FILETIME utc = { ticks.LowPart, ticks.HighPart };
7681
+ SYSTEMTIME utc_system_time = {};
7682
+ SYSTEMTIME system_time = {};
7683
+ if (!FileTimeToSystemTime(&utc, &utc_system_time) ||
7684
+ !SystemTimeToTzSpecificLocalTimeEx(nullptr, &utc_system_time, &system_time)) return 0;
7685
+
7686
+ wchar_t date_text[128] = {};
7687
+ wchar_t time_text[128] = {};
7688
+ if ((style == 0 || style == 2) && GetDateFormatEx(LOCALE_NAME_USER_DEFAULT, DATE_SHORTDATE, &system_time, nullptr, date_text, ARRAYSIZE(date_text), nullptr) == 0) return 0;
7689
+ if ((style == 1 || style == 2) && GetTimeFormatEx(LOCALE_NAME_USER_DEFAULT, 0, &system_time, nullptr, time_text, ARRAYSIZE(time_text)) == 0) return 0;
7690
+
7691
+ std::wstring text;
7692
+ if (style == 0) text = date_text;
7693
+ else if (style == 1) text = time_text;
7694
+ else text = std::wstring(date_text) + L" " + time_text;
7695
+ if (text.empty()) return 0;
7696
+ const int needed = WideCharToMultiByte(CP_UTF8, 0, text.data(), static_cast<int>(text.size()), nullptr, 0, nullptr, nullptr);
7697
+ if (needed <= 0 || static_cast<size_t>(needed) > buffer_len) return 0;
7698
+ if (WideCharToMultiByte(CP_UTF8, 0, text.data(), static_cast<int>(text.size()), buffer, needed, nullptr, nullptr) != needed) return 0;
7699
+ return static_cast<size_t>(needed);
7417
7700
  }
7418
7701
 
7419
7702
  int native_sdk_windows_create_webview(Host *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) {