@native-sdk/cli 0.9.0 → 0.9.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 (181) hide show
  1. package/build/app.zig +357 -33
  2. package/build.zig +234 -11
  3. package/native-sdk.d.ts +8 -0
  4. package/package.json +10 -10
  5. package/packages/core/compile-surface/core.ts +70 -1
  6. package/packages/core/package-lock.json +11 -11
  7. package/packages/core/package.json +2 -2
  8. package/packages/core/scripts/merge_static_archives.mjs +78 -0
  9. package/packages/core/scripts/run_external_core_compiler.mjs +52 -7
  10. package/packages/core/scripts/run_external_service_compiler.mjs +53 -13
  11. package/packages/core/scripts/stage_external_services.mjs +1 -1
  12. package/packages/core/sdk/core.d.ts +75 -0
  13. package/packages/core/sdk/core.ts +170 -13
  14. package/packages/core/sdk/events.d.ts +53 -0
  15. package/packages/core/sdk/events.ts +78 -7
  16. package/packages/core/src/checker.ts +470 -2
  17. package/packages/core/src/cli.ts +13 -0
  18. package/packages/core/src/contract.ts +3 -1
  19. package/packages/core/src/devhost.ts +30 -1
  20. package/packages/core/src/devhost_journal.mjs +10 -8
  21. package/packages/core/src/diagnostics.ts +99 -8
  22. package/packages/core/src/frontend.ts +6 -0
  23. package/packages/core/src/infer.ts +24 -0
  24. package/skill-data/automation/SKILL.md +1 -1
  25. package/skill-data/native-ui/SKILL.md +18 -12
  26. package/skill-data/ts-core/SKILL.md +54 -21
  27. package/skill-data/ts-services/SKILL.md +4 -4
  28. package/skill-data/ts-services/references/service-surface.md +2 -1
  29. package/skill-data/zig/SKILL.md +1 -1
  30. package/src/app_runner/root.zig +122 -6
  31. package/src/app_runner/ts_core_main.zig +9 -4
  32. package/src/app_runner/ts_core_mobile.zig +241 -0
  33. package/src/app_runner/window_placement.zig +63 -0
  34. package/src/automation/protocol.zig +5 -1
  35. package/src/automation/snapshot.zig +8 -6
  36. package/src/embed/app_exports.zig +3 -1
  37. package/src/embed/conversions.zig +1 -0
  38. package/src/embed/host.zig +10 -3
  39. package/src/embed/root.zig +2 -0
  40. package/src/embed/tests.zig +41 -1
  41. package/src/embed/types.zig +2 -0
  42. package/src/embed/ui_host.zig +94 -4
  43. package/src/platform/android/NativeSdkActivity.java +49 -4
  44. package/src/platform/android/android_host.c +3 -3
  45. package/src/platform/android/native_sdk_app.h +1 -1
  46. package/src/platform/ios/apple_image_fit.h +38 -0
  47. package/src/platform/ios/files.zig +1 -0
  48. package/src/platform/ios/native_sdk_app.h +1 -1
  49. package/src/platform/ios/uikit_host.m +28 -5
  50. package/src/platform/linux/cef_host.cpp +17 -3
  51. package/src/platform/linux/gtk_host.c +243 -15
  52. package/src/platform/linux/gtk_host.h +5 -4
  53. package/src/platform/linux/root.zig +91 -11
  54. package/src/platform/macos/appkit_host.h +18 -11
  55. package/src/platform/macos/appkit_host.m +402 -128
  56. package/src/platform/macos/cef_host.mm +342 -122
  57. package/src/platform/macos/image_fit_test.m +44 -0
  58. package/src/platform/macos/root.zig +231 -39
  59. package/src/platform/null_platform.zig +251 -54
  60. package/src/platform/null_platform_tests.zig +97 -4
  61. package/src/platform/root.zig +12 -0
  62. package/src/platform/types.zig +174 -35
  63. package/src/platform/windows/root.zig +42 -13
  64. package/src/platform/windows/webview2_host.cpp +72 -11
  65. package/src/primitives/app_manifest/root.zig +3 -0
  66. package/src/primitives/app_manifest/tests.zig +24 -0
  67. package/src/primitives/app_manifest/types.zig +13 -0
  68. package/src/primitives/app_manifest/validation.zig +24 -0
  69. package/src/primitives/canvas/a11y_audit.zig +9 -7
  70. package/src/primitives/canvas/a11y_audit_tests.zig +24 -0
  71. package/src/primitives/canvas/events.zig +50 -0
  72. package/src/primitives/canvas/layout_audit.zig +6 -6
  73. package/src/primitives/canvas/markdown.zig +4 -0
  74. package/src/primitives/canvas/markdown_tests.zig +1 -0
  75. package/src/primitives/canvas/png.zig +138 -0
  76. package/src/primitives/canvas/root.zig +13 -2
  77. package/src/primitives/canvas/text_span_tests.zig +5 -3
  78. package/src/primitives/canvas/text_spans.zig +2 -0
  79. package/src/primitives/canvas/text_tests.zig +20 -0
  80. package/src/primitives/canvas/ui.zig +55 -2
  81. package/src/primitives/canvas/ui_markup.zig +40 -13
  82. package/src/primitives/canvas/ui_markup_binary.zig +8 -6
  83. package/src/primitives/canvas/ui_markup_binary_tests.zig +14 -4
  84. package/src/primitives/canvas/ui_markup_compiled.zig +5 -1
  85. package/src/primitives/canvas/ui_markup_compiled_tests.zig +52 -0
  86. package/src/primitives/canvas/ui_markup_tests.zig +11 -2
  87. package/src/primitives/canvas/ui_markup_view.zig +8 -2
  88. package/src/primitives/canvas/ui_markup_view_tests.zig +49 -4
  89. package/src/primitives/canvas/ui_schema.zig +17 -13
  90. package/src/primitives/canvas/ui_tests.zig +47 -0
  91. package/src/primitives/canvas/widget_access.zig +3 -3
  92. package/src/primitives/canvas/widget_anchored_tests.zig +106 -0
  93. package/src/primitives/canvas/widget_builtin_tests.zig +580 -32
  94. package/src/primitives/canvas/widget_invalidation.zig +33 -6
  95. package/src/primitives/canvas/widget_layout.zig +333 -126
  96. package/src/primitives/canvas/widget_layout_tests.zig +169 -0
  97. package/src/primitives/canvas/widget_metrics.zig +7 -8
  98. package/src/primitives/canvas/widget_render.zig +76 -44
  99. package/src/primitives/canvas/widget_render_controls.zig +110 -32
  100. package/src/primitives/canvas/widget_render_style.zig +58 -18
  101. package/src/primitives/canvas/widget_render_surfaces.zig +45 -14
  102. package/src/primitives/canvas/widget_routing.zig +40 -34
  103. package/src/primitives/canvas/widget_runtime.zig +11 -2
  104. package/src/primitives/canvas/widget_runtime_tests.zig +10 -14
  105. package/src/primitives/canvas/widget_semantics.zig +8 -3
  106. package/src/primitives/canvas/widget_semantics_tests.zig +6 -2
  107. package/src/primitives/canvas/widget_tree.zig +99 -5
  108. package/src/primitives/canvas/widgets.zig +20 -7
  109. package/src/root.zig +10 -0
  110. package/src/runtime/api.zig +14 -0
  111. package/src/runtime/automation_commands.zig +35 -2
  112. package/src/runtime/automation_snapshot.zig +32 -24
  113. package/src/runtime/automation_widget_dispatch.zig +48 -6
  114. package/src/runtime/bridge_permission_tests.zig +35 -1
  115. package/src/runtime/bridge_responses.zig +1 -1
  116. package/src/runtime/builtin_bridge.zig +16 -3
  117. package/src/runtime/canvas_frame_packet_tests.zig +22 -13
  118. package/src/runtime/canvas_image_tests.zig +146 -2
  119. package/src/runtime/canvas_images.zig +35 -16
  120. package/src/runtime/canvas_limits.zig +13 -3
  121. package/src/runtime/canvas_widget_accessibility_tests.zig +279 -4
  122. package/src/runtime/canvas_widget_anchored_tests.zig +54 -0
  123. package/src/runtime/canvas_widget_control_tests.zig +31 -1
  124. package/src/runtime/canvas_widget_events.zig +151 -26
  125. package/src/runtime/canvas_widget_floating_tests.zig +48 -0
  126. package/src/runtime/canvas_widget_runtime.zig +322 -102
  127. package/src/runtime/canvas_widget_scroll_driver_tests.zig +34 -0
  128. package/src/runtime/canvas_widget_scroll_drivers.zig +12 -10
  129. package/src/runtime/canvas_widget_scroll_tests.zig +193 -2
  130. package/src/runtime/canvas_widget_semantics_tests.zig +337 -3
  131. package/src/runtime/canvas_widget_split_tree_tests.zig +55 -0
  132. package/src/runtime/canvas_widget_state.zig +33 -7
  133. package/src/runtime/core.zig +28 -6
  134. package/src/runtime/effects.zig +1337 -49
  135. package/src/runtime/effects_file_tests.zig +587 -0
  136. package/src/runtime/effects_image_tests.zig +91 -0
  137. package/src/runtime/effects_notification_tests.zig +11 -0
  138. package/src/runtime/file_access.zig +400 -0
  139. package/src/runtime/file_effect_crash_helper.zig +28 -0
  140. package/src/runtime/file_effect_crash_tests.zig +37 -0
  141. package/src/runtime/flow.zig +21 -9
  142. package/src/runtime/gpu_surface_events.zig +25 -2
  143. package/src/runtime/native_command_event_tests.zig +44 -2
  144. package/src/runtime/pty.zig +5 -2
  145. package/src/runtime/root.zig +7 -0
  146. package/src/runtime/session_blobs.zig +4 -1
  147. package/src/runtime/session_journal.zig +56 -5
  148. package/src/runtime/session_record.zig +38 -0
  149. package/src/runtime/session_replay.zig +110 -15
  150. package/src/runtime/session_tests.zig +192 -4
  151. package/src/runtime/shell_layout_tests.zig +12 -0
  152. package/src/runtime/state.zig +10 -0
  153. package/src/runtime/system_services.zig +129 -36
  154. package/src/runtime/ts_core_host.zig +281 -11
  155. package/src/runtime/ts_core_host_tests.zig +142 -1
  156. package/src/runtime/ts_ui_app.zig +461 -8
  157. package/src/runtime/ui_app.zig +330 -112
  158. package/src/runtime/ui_app_tests.zig +251 -25
  159. package/src/runtime/ui_app_window_tests.zig +74 -8
  160. package/src/runtime/validation.zig +20 -0
  161. package/src/runtime/view.zig +3 -0
  162. package/src/runtime/view_widget_control.zig +18 -0
  163. package/src/runtime/view_widget_scroll.zig +112 -18
  164. package/src/runtime/view_widget_tree.zig +110 -13
  165. package/src/runtime/widget_bridge.zig +2 -0
  166. package/src/runtime/window_command_bridge_tests.zig +86 -0
  167. package/src/runtime/window_storage.zig +16 -1
  168. package/src/runtime/window_views.zig +1 -0
  169. package/src/tooling/ios.zig +3 -0
  170. package/src/tooling/manifest.zig +90 -2
  171. package/src/tooling/package.zig +67 -2
  172. package/src/tooling/raw_manifest.zig +10 -0
  173. package/src/tooling/templates.zig +156 -18
  174. package/src/tooling/ts_core.zig +83 -0
  175. package/src/tooling/web_engine.zig +1 -1
  176. package/tools/corewire/emit.zig +3 -3
  177. package/tools/corewire/emit_facade.zig +65 -5
  178. package/tools/corewire/emit_profile.zig +2 -2
  179. package/tools/corewire/emit_service.zig +2 -2
  180. package/tools/corewire/service_contract.zig +4 -4
  181. package/tools/corewire/sidecar.zig +4 -4
@@ -26,6 +26,8 @@
26
26
  #include <string.h>
27
27
  #include <time.h>
28
28
 
29
+ #include "../ios/apple_image_fit.h"
30
+
29
31
  @class NativeSdkAppKitHost;
30
32
  @class NativeSdkAudioCaptureTarget;
31
33
  @class NativeSdkScreenAudioCapture;
@@ -99,7 +101,11 @@ static void *NativeSdkAppKitVideoTimeControlContext = &NativeSdkAppKitVideoTimeC
99
101
  /* Render-thread ring state for the spectrum tap; defined with the rest
100
102
  * of the spectrum machinery in the audio section below. */
101
103
  typedef struct native_sdk_spectrum_tap_state native_sdk_spectrum_tap_state_t;
102
- static NSRect constrainFrame(NSRect frame);
104
+ static NSScreen *NativeSdkPrimaryScreen(void);
105
+ static NSScreen *NativeSdkScreenForFrame(NSRect frame);
106
+ static NSRect NativeSdkConstrainFrameToScreen(NSRect frame, NSScreen *screen);
107
+ static NSRect NativeSdkConstrainFrame(NSRect frame);
108
+ static NSRect NativeSdkCenterFrameOnScreen(NSRect frame, NSScreen *screen);
103
109
  static NSString *NativeSdkAppKitBridgeScript(void);
104
110
  static NSString *NativeSdkMenuKeyEquivalent(NSString *key);
105
111
  static NSString *NativeSdkMimeTypeForPath(NSString *path);
@@ -267,6 +273,8 @@ static NSAccessibilityRole NativeSdkAccessibilityRoleForWidgetRole(NSInteger rol
267
273
  return NSAccessibilityCheckBoxRole;
268
274
  case NATIVE_SDK_APPKIT_WIDGET_ROLE_RADIO:
269
275
  return NSAccessibilityRadioButtonRole;
276
+ case NATIVE_SDK_APPKIT_WIDGET_ROLE_RADIOGROUP:
277
+ return NSAccessibilityRadioGroupRole;
270
278
  case NATIVE_SDK_APPKIT_WIDGET_ROLE_MENU:
271
279
  return NSAccessibilityMenuRole;
272
280
  case NATIVE_SDK_APPKIT_WIDGET_ROLE_MENUITEM:
@@ -470,6 +478,7 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
470
478
  @property(nonatomic, assign) uint32_t actionFlags;
471
479
  @property(nonatomic, assign) BOOL canUndo;
472
480
  @property(nonatomic, assign) BOOL canRedo;
481
+ @property(nonatomic, assign) NSRect surfaceFrame;
473
482
  - (BOOL)emitSetTextAccessibilityValue:(id)value;
474
483
  - (BOOL)emitSetSelectionAccessibilityValue:(id)value;
475
484
  @end
@@ -665,6 +674,7 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
665
674
  @property(nonatomic, assign) NSRange selectedTextRange;
666
675
  @property(nonatomic, assign) BOOL interpretedKeyEventEmittedInput;
667
676
  @property(nonatomic, strong) NSArray<NSAccessibilityElement *> *widgetAccessibilityElements;
677
+ @property(nonatomic, strong) NSArray<NSAccessibilityElement *> *widgetAccessibilityRootElements;
668
678
  @property(nonatomic, strong) NSMutableArray<NativeSdkScrollDriverView *> *scrollDrivers;
669
679
  @property(nonatomic, assign) NSPoint wheelGesturePoint;
670
680
  @property(nonatomic, assign) BOOL wheelGestureActive;
@@ -743,7 +753,7 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
743
753
  - (void)updateSurfaceTrackingArea;
744
754
  - (void)emitSelectAllTextInputCommand;
745
755
  - (void)emitTextInputEventWithKind:(NSInteger)kind text:(NSString *)text compositionCursor:(NSInteger)compositionCursor;
746
- - (NSAccessibilityElement *)focusedTextAccessibilityElement;
756
+ - (NativeSdkWidgetAccessibilityElement *)focusedTextAccessibilityElement;
747
757
  - (BOOL)emitWidgetAccessibilityActionWithId:(uint64_t)widgetId action:(NSInteger)action;
748
758
  - (BOOL)emitWidgetAccessibilityActionWithId:(uint64_t)widgetId action:(NSInteger)action text:(NSString *)text selectedRange:(NSRange)selectedRange hasSelectedRange:(BOOL)hasSelectedRange;
749
759
  - (void)setSurfaceCursor:(NSCursor *)cursor;
@@ -763,7 +773,22 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
763
773
  @property(nonatomic, assign) uint32_t modifiers;
764
774
  @end
765
775
 
766
- @interface NativeSdkAppKitHost : NSObject <WKNavigationDelegate, NSMenuDelegate>
776
+ @interface NativeSdkStatusItemEntry : NSObject
777
+ @property(nonatomic, assign) uint32_t identifier;
778
+ @property(nonatomic, strong) NSStatusItem *item;
779
+ @property(nonatomic, strong) NSImage *baseImage;
780
+ @property(nonatomic, strong) NSMenu *menu;
781
+ @property(nonatomic, strong) NSString *presentationTitle;
782
+ @property(nonatomic, assign) double presentationWidth;
783
+ @property(nonatomic, assign) int presentationTone;
784
+ @property(nonatomic, assign) double presentationIconOpacity;
785
+ @property(nonatomic, assign) BOOL presentationMonospaced;
786
+ @property(nonatomic, strong) NSString *activationCommand;
787
+ @property(nonatomic, strong) NSString *alternateActivationCommand;
788
+ @property(nonatomic, strong) NSString *openCommand;
789
+ @end
790
+
791
+ @interface NativeSdkAppKitHost : NSObject <WKNavigationDelegate, NSMenuDelegate, NSUserNotificationCenterDelegate>
767
792
  @property(nonatomic, strong) NSWindow *window;
768
793
  @property(nonatomic, strong) WKWebView *webView;
769
794
  @property(nonatomic, strong) NativeSdkWindowDelegate *delegate;
@@ -825,6 +850,12 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
825
850
  * block starts still gets its own turn. */
826
851
  @property(atomic, assign) BOOL crossThreadFramePending;
827
852
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSTimer *> *appTimers;
853
+ /* Notification userInfo persists with the delivered notification, so it
854
+ * carries only a per-process token. Commands remain in this live host;
855
+ * stale notifications from an earlier execution cannot dispatch into a
856
+ * later one. The identifier map invalidates same-id replacements. */
857
+ @property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *notificationActionCommands;
858
+ @property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *notificationActionTokensByIdentifier;
828
859
  /* The app's single audio player and its position-tick timer. One player
829
860
  * is the whole surface: a music app plays one track at a time, and a
830
861
  * second concurrent stream would be mixer design the platform seam has
@@ -986,23 +1017,14 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
986
1017
  @property(nonatomic, strong) id willTerminateObserver;
987
1018
  @property(nonatomic, strong) dispatch_source_t sigtermSource;
988
1019
  @property(nonatomic, strong) NSArray<NativeSdkShortcut *> *shortcuts;
989
- @property(nonatomic, strong) NSStatusItem *statusItem;
990
- @property(nonatomic, strong) NSImage *statusItemBaseImage;
991
- @property(nonatomic, strong) NSMenu *statusMenu;
992
- @property(nonatomic, assign) double statusPresentationWidth;
993
- @property(nonatomic, assign) int statusPresentationTone;
994
- @property(nonatomic, assign) double statusPresentationIconOpacity;
995
- @property(nonatomic, assign) BOOL statusPresentationMonospaced;
996
- @property(nonatomic, strong) NSString *statusActivationCommand;
997
- @property(nonatomic, strong) NSString *statusAlternateActivationCommand;
998
- @property(nonatomic, strong) NSString *statusOpenCommand;
1020
+ @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NativeSdkStatusItemEntry *> *statusItems;
999
1021
  @property(nonatomic, assign) native_sdk_appkit_tray_callback_t trayCallback;
1000
1022
  @property(nonatomic, assign) void *trayContext;
1001
1023
  @property(nonatomic, strong) NSArray<NSString *> *allowedNavigationOrigins;
1002
1024
  @property(nonatomic, strong) NSArray<NSString *> *allowedExternalURLs;
1003
1025
  @property(nonatomic, assign) NSInteger externalLinkAction;
1004
- - (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription hasWebContent:(BOOL)hasWebContent windowTitle:(NSString *)windowTitle bundleIdentifier:(NSString *)bundleIdentifier iconPath:(NSString *)iconPath windowLabel:(NSString *)windowLabel x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame resizable:(BOOL)resizable titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags;
1005
- - (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 titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags makeMain:(BOOL)makeMain;
1026
+ - (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription hasWebContent:(BOOL)hasWebContent dockVisible:(BOOL)dockVisible windowTitle:(NSString *)windowTitle bundleIdentifier:(NSString *)bundleIdentifier iconPath:(NSString *)iconPath windowLabel:(NSString *)windowLabel x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame initialPlacement:(int)initialPlacement restorePolicy:(int)restorePolicy resizable:(BOOL)resizable titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags;
1027
+ - (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 initialPlacement:(int)initialPlacement restorePolicy:(int)restorePolicy resizable:(BOOL)resizable titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags makeMain:(BOOL)makeMain;
1006
1028
  - (void)orderWindowForImplicitShow:(uint64_t)windowId;
1007
1029
  - (void)showDeferredWindowIfPending:(uint64_t)windowId reason:(const char *)reason;
1008
1030
  - (void)applyWindowClearColor:(uint64_t)windowId red:(uint8_t)red green:(uint8_t)green blue:(uint8_t)blue alpha:(uint8_t)alpha;
@@ -1068,7 +1090,9 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
1068
1090
  - (NSMenuItem *)commandMenuItem:(NSString *)title command:(NSString *)command key:(NSString *)key modifiers:(uint32_t)modifiers enabled:(BOOL)enabled checked:(BOOL)checked;
1069
1091
  - (void)menuCommandItemClicked:(NSMenuItem *)menuItem;
1070
1092
  - (void)menuWillOpen:(NSMenu *)menu;
1071
- - (void)emitStatusCommand:(NSString *)command;
1093
+ - (NativeSdkStatusItemEntry *)statusEntryForId:(uint32_t)identifier;
1094
+ - (NativeSdkStatusItemEntry *)statusEntryForMenu:(NSMenu *)menu;
1095
+ - (void)emitStatusCommand:(NSString *)command statusItemId:(uint32_t)statusItemId;
1072
1096
  - (void)statusItemActivated:(id)sender;
1073
1097
  - (uint64_t)activeCommandWindowId;
1074
1098
  - (void)setMenusWithTitles:(const char *const *)menuTitles titleLengths:(const size_t *)menuTitleLengths count:(size_t)menuCount itemMenuIndices:(const uint32_t *)itemMenuIndices itemLabels:(const char *const *)itemLabels itemLabelLengths:(const size_t *)itemLabelLengths itemCommands:(const char *const *)itemCommands itemCommandLengths:(const size_t *)itemCommandLengths itemKeys:(const char *const *)itemKeys itemKeyLengths:(const size_t *)itemKeyLengths itemModifiers:(const uint32_t *)itemModifiers itemSeparators:(const int *)itemSeparators itemEnabled:(const int *)itemEnabled itemChecked:(const int *)itemChecked itemCount:(size_t)itemCount;
@@ -1081,6 +1105,8 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
1081
1105
  - (void)stopApplicationActivationObservers;
1082
1106
  - (void)applicationDidBecomeActive:(NSNotification *)notification;
1083
1107
  - (void)applicationDidResignActive:(NSNotification *)notification;
1108
+ - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification;
1109
+ - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification;
1084
1110
  - (void)startAppearanceObservers;
1085
1111
  - (void)stopAppearanceObservers;
1086
1112
  - (void)emitAppearanceChanged;
@@ -2520,28 +2546,48 @@ int native_sdk_appkit_measure_text_advances(uint64_t font_id, double size, const
2520
2546
  // CGBitmapContext can render into) and is un-premultiplied in place,
2521
2547
  // because the canvas image pipeline — the reference renderer and the
2522
2548
  // packet host's kCGImageAlphaLast upload — expects straight alpha.
2523
- 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) {
2549
+ int native_sdk_appkit_decode_image(const uint8_t *bytes, size_t bytes_len, uint8_t *pixels, size_t pixels_len, size_t max_pixels, size_t *out_width, size_t *out_height) {
2524
2550
  if (out_width) *out_width = 0;
2525
2551
  if (out_height) *out_height = 0;
2526
- if (!bytes || bytes_len == 0 || !pixels) return 0;
2552
+ if (!bytes || bytes_len == 0 || !pixels || max_pixels == 0) return 0;
2527
2553
  @autoreleasepool {
2528
2554
  NSData *data = [NSData dataWithBytesNoCopy:(void *)bytes length:bytes_len freeWhenDone:NO];
2529
2555
  CGImageRef image = NULL;
2530
2556
  CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
2531
2557
  if (source) {
2532
- image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
2558
+ CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
2559
+ int64_t source_width_value = 0;
2560
+ int64_t source_height_value = 0;
2561
+ if (properties) {
2562
+ CFNumberRef width_value = CFDictionaryGetValue(properties, kCGImagePropertyPixelWidth);
2563
+ CFNumberRef height_value = CFDictionaryGetValue(properties, kCGImagePropertyPixelHeight);
2564
+ if (width_value) CFNumberGetValue(width_value, kCFNumberSInt64Type, &source_width_value);
2565
+ if (height_value) CFNumberGetValue(height_value, kCFNumberSInt64Type, &source_height_value);
2566
+ CFRelease(properties);
2567
+ }
2568
+ size_t source_width = source_width_value > 0 ? (size_t)source_width_value : 0;
2569
+ size_t source_height = source_height_value > 0 ? (size_t)source_height_value : 0;
2570
+ if (source_width > 0 && source_height > 0) {
2571
+ const size_t max_dimension = native_sdk_apple_image_thumbnail_max_dimension(source_width, source_height, max_pixels);
2572
+ NSDictionary *thumbnail_options = @{
2573
+ (NSString *)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
2574
+ (NSString *)kCGImageSourceThumbnailMaxPixelSize: @(max_dimension),
2575
+ (NSString *)kCGImageSourceCreateThumbnailWithTransform: @YES,
2576
+ };
2577
+ image = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)thumbnail_options);
2578
+ }
2533
2579
  CFRelease(source);
2534
2580
  }
2535
2581
  if (!image) {
2536
2582
  NSImage *system_image = [[NSImage alloc] initWithData:data];
2537
2583
  NSSize size = system_image ? system_image.size : NSZeroSize;
2538
- if (size.width > 0 && size.height > 0 && size.width <= 8192 && size.height <= 8192) {
2584
+ if (size.width > 0 && size.height > 0 && isfinite(size.width) && isfinite(size.height)) {
2539
2585
  // SVG is resolution-independent, so cap the proposed point
2540
2586
  // extent before NSImage selects its Retina representation.
2541
2587
  // A 256pt square becomes at most the registry's 512px square
2542
2588
  // on macOS's 2x displays instead of materializing an
2543
2589
  // attacker-declared multi-hundred-megabyte bitmap first.
2544
- const CGFloat max_raster_points = 256.0;
2590
+ const CGFloat max_raster_points = sqrt((CGFloat)max_pixels) / 2.0;
2545
2591
  const CGFloat raster_scale = MIN(1.0, MIN(max_raster_points / size.width, max_raster_points / size.height));
2546
2592
  NSRect proposed = NSMakeRect(0, 0, size.width * raster_scale, size.height * raster_scale);
2547
2593
  CGImageRef rendered = [system_image CGImageForProposedRect:&proposed context:nil hints:nil];
@@ -2552,14 +2598,14 @@ int native_sdk_appkit_decode_image(const uint8_t *bytes, size_t bytes_len, uint8
2552
2598
 
2553
2599
  size_t width = CGImageGetWidth(image);
2554
2600
  size_t height = CGImageGetHeight(image);
2555
- if (width == 0 || height == 0 || width > 8192 || height > 8192) {
2601
+ if (width == 0 || height == 0 || width > NATIVE_SDK_MAX_DECODED_IMAGE_DIMENSION || height > NATIVE_SDK_MAX_DECODED_IMAGE_DIMENSION) {
2556
2602
  CGImageRelease(image);
2557
2603
  return 0;
2558
2604
  }
2559
2605
  if (out_width) *out_width = width;
2560
2606
  if (out_height) *out_height = height;
2561
2607
  size_t byte_len = width * height * 4;
2562
- if (byte_len / 4 / height != width || pixels_len < byte_len) {
2608
+ if (width > max_pixels / height || byte_len / 4 / height != width || pixels_len < byte_len) {
2563
2609
  CGImageRelease(image);
2564
2610
  return -1;
2565
2611
  }
@@ -3736,7 +3782,7 @@ static void NativeSdkPremultiplyStraightRgba8(const uint8_t *source, uint8_t *de
3736
3782
  }
3737
3783
 
3738
3784
  - (NSArray *)accessibilityChildren {
3739
- return self.widgetAccessibilityElements ?: @[];
3785
+ return self.widgetAccessibilityRootElements ?: @[];
3740
3786
  }
3741
3787
 
3742
3788
  - (BOOL)isAvailable {
@@ -5596,11 +5642,14 @@ static BOOL NativeSdkCompositeBlurWriteRegion(NSDictionary *command, CGFloat sca
5596
5642
  - (void)updateWidgetAccessibilityWithNodes:(const native_sdk_appkit_widget_accessibility_node_t *)nodes count:(NSUInteger)count {
5597
5643
  if (!nodes || count == 0) {
5598
5644
  self.widgetAccessibilityElements = @[];
5645
+ self.widgetAccessibilityRootElements = @[];
5599
5646
  NSAccessibilityPostNotification(self, NSAccessibilityLayoutChangedNotification);
5600
5647
  return;
5601
5648
  }
5602
5649
 
5603
- NSMutableArray<NSAccessibilityElement *> *elements = [NSMutableArray arrayWithCapacity:count];
5650
+ NSMutableArray<NativeSdkWidgetAccessibilityElement *> *elements = [NSMutableArray arrayWithCapacity:count];
5651
+ NSMutableArray<NSNumber *> *parentIds = [NSMutableArray arrayWithCapacity:count];
5652
+ NSMutableDictionary<NSNumber *, NativeSdkWidgetAccessibilityElement *> *elementsById = [NSMutableDictionary dictionaryWithCapacity:count];
5604
5653
  for (NSUInteger index = 0; index < count; index++) {
5605
5654
  const native_sdk_appkit_widget_accessibility_node_t node = nodes[index];
5606
5655
  NSString *label = NativeSdkStringFromBytes(node.label, node.label_len) ?: @"";
@@ -5611,7 +5660,6 @@ static BOOL NativeSdkCompositeBlurWriteRegion(NSDictionary *command, CGFloat sca
5611
5660
  element.surfaceView = self;
5612
5661
  element.widgetId = node.id;
5613
5662
  element.actionFlags = node.action_flags;
5614
- element.accessibilityParent = self;
5615
5663
  element.accessibilityRole = NativeSdkAccessibilityRoleForWidgetRole(node.role);
5616
5664
  element.accessibilityIdentifier = [NSString stringWithFormat:@"native-sdk-widget-%llu", node.id];
5617
5665
  element.accessibilityLabel = name;
@@ -5688,10 +5736,46 @@ static BOOL NativeSdkCompositeBlurWriteRegion(NSDictionary *command, CGFloat sca
5688
5736
  element.accessibilityValueDescription = [stateDescriptions componentsJoinedByString:@", "];
5689
5737
  }
5690
5738
  CGFloat nativeY = self.bounds.size.height - node.y - node.height;
5691
- element.accessibilityFrameInParentSpace = NSMakeRect(node.x, nativeY, node.width, node.height);
5739
+ element.surfaceFrame = NSMakeRect(node.x, nativeY, node.width, node.height);
5740
+ element.accessibilityFrameInParentSpace = element.surfaceFrame;
5692
5741
  [elements addObject:element];
5742
+ [parentIds addObject:@(node.parent_id)];
5743
+ [elementsById setObject:element forKey:@(node.id)];
5744
+ }
5745
+
5746
+ NSMutableArray<NSAccessibilityElement *> *rootElements = [NSMutableArray arrayWithCapacity:count];
5747
+ NSMutableDictionary<NSNumber *, NSMutableArray<NSAccessibilityElement *> *> *childrenByParentId = [NSMutableDictionary dictionaryWithCapacity:count];
5748
+ for (NSUInteger index = 0; index < elements.count; index++) {
5749
+ NativeSdkWidgetAccessibilityElement *element = elements[index];
5750
+ NSNumber *parentId = parentIds[index];
5751
+ NativeSdkWidgetAccessibilityElement *parent = parentId.unsignedLongLongValue == 0 ? nil : [elementsById objectForKey:parentId];
5752
+ if (parent && parent != element) {
5753
+ element.accessibilityParent = parent;
5754
+ NSRect parentFrame = parent.surfaceFrame;
5755
+ NSRect childFrame = element.surfaceFrame;
5756
+ element.accessibilityFrameInParentSpace = NSMakeRect(
5757
+ childFrame.origin.x - parentFrame.origin.x,
5758
+ childFrame.origin.y - parentFrame.origin.y,
5759
+ childFrame.size.width,
5760
+ childFrame.size.height
5761
+ );
5762
+ NSMutableArray<NSAccessibilityElement *> *children = [childrenByParentId objectForKey:parentId];
5763
+ if (!children) {
5764
+ children = [NSMutableArray array];
5765
+ [childrenByParentId setObject:children forKey:parentId];
5766
+ }
5767
+ [children addObject:element];
5768
+ } else {
5769
+ element.accessibilityParent = self;
5770
+ [rootElements addObject:element];
5771
+ }
5772
+ }
5773
+ for (NSNumber *parentId in childrenByParentId) {
5774
+ NativeSdkWidgetAccessibilityElement *parent = [elementsById objectForKey:parentId];
5775
+ parent.accessibilityChildren = [childrenByParentId objectForKey:parentId];
5693
5776
  }
5694
5777
  self.widgetAccessibilityElements = elements;
5778
+ self.widgetAccessibilityRootElements = rootElements;
5695
5779
  NSAccessibilityPostNotification(self, NSAccessibilityLayoutChangedNotification);
5696
5780
  }
5697
5781
 
@@ -7066,10 +7150,10 @@ static BOOL NativeSdkScrollDriverCanConsumeHorizontally(NativeSdkScrollDriverVie
7066
7150
  }
7067
7151
 
7068
7152
  - (NSUInteger)characterIndexForPoint:(NSPoint)point {
7069
- NSAccessibilityElement *element = [self focusedTextAccessibilityElement];
7153
+ NativeSdkWidgetAccessibilityElement *element = [self focusedTextAccessibilityElement];
7070
7154
  if (!element || !self.window) return 0;
7071
7155
 
7072
- NSRect frame = element.accessibilityFrameInParentSpace;
7156
+ NSRect frame = element.surfaceFrame;
7073
7157
  if (NSIsEmptyRect(frame)) return 0;
7074
7158
 
7075
7159
  NSPoint windowPoint = [self.window convertPointFromScreen:point];
@@ -7084,10 +7168,10 @@ static BOOL NativeSdkScrollDriverCanConsumeHorizontally(NativeSdkScrollDriverVie
7084
7168
  }
7085
7169
 
7086
7170
  - (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange {
7087
- NSAccessibilityElement *element = [self focusedTextAccessibilityElement];
7171
+ NativeSdkWidgetAccessibilityElement *element = [self focusedTextAccessibilityElement];
7088
7172
  NSRect localRect = NSZeroRect;
7089
7173
  if (element) {
7090
- NSRect frame = element.accessibilityFrameInParentSpace;
7174
+ NSRect frame = element.surfaceFrame;
7091
7175
  NSInteger characterCount = MAX(0, element.accessibilityNumberOfCharacters);
7092
7176
  NSUInteger location = range.location == NSNotFound ? 0 : MIN(range.location, (NSUInteger)characterCount);
7093
7177
  NSUInteger length = range.location == NSNotFound ? 0 : MIN(range.length, (NSUInteger)characterCount - location);
@@ -7110,8 +7194,8 @@ static BOOL NativeSdkScrollDriverCanConsumeHorizontally(NativeSdkScrollDriverVie
7110
7194
  return self.window ? [self.window convertRectToScreen:windowRect] : windowRect;
7111
7195
  }
7112
7196
 
7113
- - (NSAccessibilityElement *)focusedTextAccessibilityElement {
7114
- for (NSAccessibilityElement *element in self.widgetAccessibilityElements ?: @[]) {
7197
+ - (NativeSdkWidgetAccessibilityElement *)focusedTextAccessibilityElement {
7198
+ for (NativeSdkWidgetAccessibilityElement *element in self.widgetAccessibilityElements ?: @[]) {
7115
7199
  if (!element.accessibilityFocused) continue;
7116
7200
  if ([element.accessibilityRole isEqualToString:NSAccessibilityTextFieldRole]) return element;
7117
7201
  }
@@ -7336,6 +7420,9 @@ static BOOL NativeSdkScrollDriverCanConsumeHorizontally(NativeSdkScrollDriverVie
7336
7420
  @implementation NativeSdkShortcut
7337
7421
  @end
7338
7422
 
7423
+ @implementation NativeSdkStatusItemEntry
7424
+ @end
7425
+
7339
7426
  /* ------------------------------------------------------- audio capture
7340
7427
  * Both native sources feed this one converter/quiescence target. AVAudioEngine
7341
7428
  * and ScreenCaptureKit normally hand us planar float PCM; the converter also
@@ -7665,7 +7752,7 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7665
7752
 
7666
7753
  @implementation NativeSdkAppKitHost
7667
7754
 
7668
- - (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription hasWebContent:(BOOL)hasWebContent windowTitle:(NSString *)windowTitle bundleIdentifier:(NSString *)bundleIdentifier iconPath:(NSString *)iconPath windowLabel:(NSString *)windowLabel x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame resizable:(BOOL)resizable titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags {
7755
+ - (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription hasWebContent:(BOOL)hasWebContent dockVisible:(BOOL)dockVisible windowTitle:(NSString *)windowTitle bundleIdentifier:(NSString *)bundleIdentifier iconPath:(NSString *)iconPath windowLabel:(NSString *)windowLabel x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame initialPlacement:(int)initialPlacement restorePolicy:(int)restorePolicy resizable:(BOOL)resizable titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags {
7669
7756
  self = [super init];
7670
7757
  if (!self) {
7671
7758
  return nil;
@@ -7673,7 +7760,8 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7673
7760
 
7674
7761
  NativeSdkLaunchLap("host_init");
7675
7762
  [NSApplication sharedApplication];
7676
- [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
7763
+ NSApplicationActivationPolicy policy = dockVisible ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory;
7764
+ [NSApp setActivationPolicy:policy];
7677
7765
  NativeSdkLaunchLap("nsapp_ready");
7678
7766
  NativeSdkRegisterBundledFonts();
7679
7767
  NativeSdkLaunchLap("fonts_registered");
@@ -7704,6 +7792,9 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7704
7792
  self.nativeViewExplicitTextKeys = [[NSMutableSet alloc] init];
7705
7793
  self.bridgeEnabledChildWebViewKeys = [[NSMutableSet alloc] init];
7706
7794
  self.appTimers = [[NSMutableDictionary alloc] init];
7795
+ self.notificationActionCommands = [[NSMutableDictionary alloc] init];
7796
+ self.notificationActionTokensByIdentifier = [[NSMutableDictionary alloc] init];
7797
+ self.statusItems = [[NSMutableDictionary alloc] init];
7707
7798
  self.allowedNavigationOrigins = @[ @"zero://app", @"zero://inline" ];
7708
7799
  self.allowedExternalURLs = @[];
7709
7800
  self.externalLinkAction = 0;
@@ -7711,7 +7802,7 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7711
7802
  [self configureApplication];
7712
7803
  NativeSdkLaunchLap("app_configured");
7713
7804
 
7714
- [self createWindowWithId:1 title:(windowTitle.length > 0 ? windowTitle : self.appName) label:self.windowLabel x:x y:y width:width height:height restoreFrame:restoreFrame resizable:resizable titlebarStyle:titlebarStyle showPolicy:showPolicy windowFlags:windowFlags makeMain:YES];
7805
+ [self createWindowWithId:1 title:(windowTitle.length > 0 ? windowTitle : self.appName) label:self.windowLabel x:x y:y width:width height:height restoreFrame:restoreFrame initialPlacement:initialPlacement restorePolicy:restorePolicy resizable:resizable titlebarStyle:titlebarStyle showPolicy:showPolicy windowFlags:windowFlags makeMain:YES];
7715
7806
  self.didShutdown = NO;
7716
7807
  self.pendingPreRunStop = NO;
7717
7808
  self.observesApplicationActivation = NO;
@@ -7719,16 +7810,23 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7719
7810
  return self;
7720
7811
  }
7721
7812
 
7722
- - (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 titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags makeMain:(BOOL)makeMain {
7813
+ - (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 initialPlacement:(int)initialPlacement restorePolicy:(int)restorePolicy resizable:(BOOL)resizable titlebarStyle:(int)titlebarStyle showPolicy:(int)showPolicy windowFlags:(uint32_t)windowFlags makeMain:(BOOL)makeMain {
7723
7814
  NSNumber *key = @(windowId);
7724
7815
  if (self.windows[key]) {
7725
7816
  return NO;
7726
7817
  }
7727
7818
 
7728
- NSRect rect = restoreFrame ? NSMakeRect(x, y, width, height) : NSMakeRect(0, 0, width, height);
7729
- if (restoreFrame) {
7730
- rect = constrainFrame(rect);
7731
- }
7819
+ (void)restoreFrame; // Persistence opt-in; initialPlacement says whether a frame was found.
7820
+ const BOOL restoredPlacement = initialPlacement == 0;
7821
+ // A restored center policy deliberately discards only the saved origin;
7822
+ // its saved OUTER size is reapplied after construction below. Fresh
7823
+ // frames remain content rectangles, preserving the public width/height
7824
+ // contract for authored windows.
7825
+ const BOOL centerOnPrimary = initialPlacement == 2 || (restoredPlacement && restorePolicy == 1);
7826
+ NSScreen *primaryScreen = NativeSdkPrimaryScreen();
7827
+ NSRect rect = centerOnPrimary
7828
+ ? NativeSdkConstrainFrameToScreen(NSMakeRect(0, 0, width, height), primaryScreen)
7829
+ : NativeSdkConstrainFrame(NSMakeRect(x, y, width, height));
7732
7830
  NSWindowStyleMask styleMask = NSWindowStyleMaskTitled |
7733
7831
  NSWindowStyleMaskClosable |
7734
7832
  NSWindowStyleMaskMiniaturizable;
@@ -7810,14 +7908,30 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7810
7908
  window.toolbarStyle = NSWindowToolbarStyleUnified;
7811
7909
  window.titlebarSeparatorStyle = NSTitlebarSeparatorStyleNone;
7812
7910
  }
7813
- if (!restoreFrame) {
7814
- [window center];
7911
+ if (restoredPlacement) {
7912
+ // Window-state events persist content geometry. Convert that saved
7913
+ // rect through the window's FINAL chrome, then constrain/center the
7914
+ // completed outer frame so the content rect round-trips without
7915
+ // titlebar growth while the whole window stays visible.
7916
+ NSRect restoredContentFrame = NSMakeRect(x, y, width, height);
7917
+ NSRect restoredWindowFrame = [window frameRectForContentRect:restoredContentFrame];
7918
+ restoredWindowFrame = restorePolicy == 1
7919
+ ? NativeSdkCenterFrameOnScreen(restoredWindowFrame, primaryScreen)
7920
+ : NativeSdkConstrainFrame(restoredWindowFrame);
7921
+ [window setFrame:restoredWindowFrame display:NO];
7922
+ } else if (initialPlacement == 1) {
7923
+ // Fresh authored dimensions are content size, but visibility is an
7924
+ // OUTER-frame guarantee. AppKit adds titlebar chrome during
7925
+ // construction, so constrain the completed frame as the final step.
7926
+ [window setFrame:NativeSdkConstrainFrame(window.frame) display:NO];
7927
+ } else if (centerOnPrimary) {
7928
+ [window setFrame:NativeSdkCenterFrameOnScreen(window.frame, primaryScreen) display:NO];
7815
7929
  // AppKit centers every new window by default, which leaves a
7816
7930
  // model-declared secondary window exactly covering the editor that
7817
7931
  // opened it. Cascade from the active window like Win32's default
7818
7932
  // placement so repeated Command+N windows stay visibly distinct.
7819
7933
  NSWindow *referenceWindow = NSApp.keyWindow ?: self.window;
7820
- if (!makeMain && referenceWindow) {
7934
+ if (initialPlacement == 2 && restorePolicy == 0 && !makeMain && referenceWindow) {
7821
7935
  NSRect referenceFrame = referenceWindow.frame;
7822
7936
  NSRect cascadedFrame = window.frame;
7823
7937
  cascadedFrame.origin.x = NSMinX(referenceFrame) + 24.0;
@@ -7839,7 +7953,7 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7839
7953
  }
7840
7954
  if (makeMain) NativeSdkLaunchLap("window_chrome_ready");
7841
7955
 
7842
- NSView *container = [[NSView alloc] initWithFrame:rect];
7956
+ NSView *container = [[NSView alloc] initWithFrame:window.contentView.bounds];
7843
7957
  container.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
7844
7958
  window.contentView = container;
7845
7959
  // The window's MAIN WebView is created lazily
@@ -7944,6 +8058,8 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7944
8058
  }
7945
8059
 
7946
8060
  - (void)dealloc {
8061
+ NSUserNotificationCenter *notificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
8062
+ if (notificationCenter.delegate == self) notificationCenter.delegate = nil;
7947
8063
  [self invalidateAppTimers];
7948
8064
  [self audioCaptureStopSource:0];
7949
8065
  [self audioCaptureStopSource:1];
@@ -9151,8 +9267,46 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
9151
9267
  }
9152
9268
  }
9153
9269
 
9154
- static NSRect constrainFrame(NSRect frame) {
9155
- NSScreen *screen = [NSScreen mainScreen];
9270
+ static NSScreen *NativeSdkPrimaryScreen(void) {
9271
+ // AppKit's mainScreen follows the key window. The first screen is the
9272
+ // menu-bar display: macOS's primary screen and the policy contract.
9273
+ return [NSScreen screens].firstObject ?: [NSScreen mainScreen];
9274
+ }
9275
+
9276
+ static NSScreen *NativeSdkScreenForFrame(NSRect frame) {
9277
+ NSArray<NSScreen *> *screens = [NSScreen screens];
9278
+ NSScreen *bestScreen = nil;
9279
+ CGFloat bestArea = 0;
9280
+ for (NSScreen *screen in screens) {
9281
+ NSRect intersection = NSIntersectionRect(frame, screen.visibleFrame);
9282
+ CGFloat area = NSWidth(intersection) * NSHeight(intersection);
9283
+ if (area > bestArea) {
9284
+ bestArea = area;
9285
+ bestScreen = screen;
9286
+ }
9287
+ }
9288
+ if (bestScreen) return bestScreen;
9289
+
9290
+ // A frame from a disconnected display has no intersection. Clamp it to
9291
+ // the nearest remaining display instead of always teleporting to primary.
9292
+ NSPoint center = NSMakePoint(NSMidX(frame), NSMidY(frame));
9293
+ CGFloat bestDistance = CGFLOAT_MAX;
9294
+ for (NSScreen *screen in screens) {
9295
+ NSRect visible = screen.visibleFrame;
9296
+ CGFloat nearestX = MIN(MAX(center.x, NSMinX(visible)), NSMaxX(visible));
9297
+ CGFloat nearestY = MIN(MAX(center.y, NSMinY(visible)), NSMaxY(visible));
9298
+ CGFloat dx = center.x - nearestX;
9299
+ CGFloat dy = center.y - nearestY;
9300
+ CGFloat distance = dx * dx + dy * dy;
9301
+ if (distance < bestDistance) {
9302
+ bestDistance = distance;
9303
+ bestScreen = screen;
9304
+ }
9305
+ }
9306
+ return bestScreen ?: [NSScreen mainScreen];
9307
+ }
9308
+
9309
+ static NSRect NativeSdkConstrainFrameToScreen(NSRect frame, NSScreen *screen) {
9156
9310
  if (!screen) return frame;
9157
9311
  NSRect visible = screen.visibleFrame;
9158
9312
  if (frame.size.width > visible.size.width) frame.size.width = visible.size.width;
@@ -9164,6 +9318,19 @@ static NSRect constrainFrame(NSRect frame) {
9164
9318
  return frame;
9165
9319
  }
9166
9320
 
9321
+ static NSRect NativeSdkConstrainFrame(NSRect frame) {
9322
+ return NativeSdkConstrainFrameToScreen(frame, NativeSdkScreenForFrame(frame));
9323
+ }
9324
+
9325
+ static NSRect NativeSdkCenterFrameOnScreen(NSRect frame, NSScreen *screen) {
9326
+ frame = NativeSdkConstrainFrameToScreen(frame, screen);
9327
+ if (!screen) return frame;
9328
+ NSRect visible = screen.visibleFrame;
9329
+ frame.origin.x = NSMidX(visible) - NSWidth(frame) / 2.0;
9330
+ frame.origin.y = NSMidY(visible) - NSHeight(frame) / 2.0;
9331
+ return NativeSdkConstrainFrameToScreen(frame, screen);
9332
+ }
9333
+
9167
9334
  static NSString *NativeSdkAppKitBridgeScript(void) {
9168
9335
  return @"(function(){"
9169
9336
  "if(window.zero&&window.zero.invoke){return;}"
@@ -9242,7 +9409,7 @@ static NSString *NativeSdkAppKitBridgeScript(void) {
9242
9409
  "});"
9243
9410
  "var os=Object.freeze({"
9244
9411
  "openUrl:function(value){var options=typeof value==='string'?{url:value}:(value||{});return invoke('native-sdk.os.openUrl',{url:ensureString(options.url,'url')});},"
9245
- "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);},"
9412
+ "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);},"
9246
9413
  "revealPath:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.revealPath',{path:ensureString(options.path,'path')});},"
9247
9414
  "addRecentDocument:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.addRecentDocument',{path:ensureString(options.path,'path')});},"
9248
9415
  "clearRecentDocuments:function(){return invoke('native-sdk.os.clearRecentDocuments',{});}"
@@ -9608,8 +9775,9 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
9608
9775
  }
9609
9776
 
9610
9777
  - (void)menuWillOpen:(NSMenu *)menu {
9611
- if (!self.statusItem || menu != self.statusMenu) return;
9612
- [self emitStatusCommand:self.statusOpenCommand];
9778
+ NativeSdkStatusItemEntry *entry = [self statusEntryForMenu:menu];
9779
+ if (!entry) return;
9780
+ [self emitStatusCommand:entry.openCommand statusItemId:entry.identifier];
9613
9781
  }
9614
9782
 
9615
9783
  - (void)setMenusWithTitles:(const char *const *)menuTitles titleLengths:(const size_t *)menuTitleLengths count:(size_t)menuCount itemMenuIndices:(const uint32_t *)itemMenuIndices itemLabels:(const char *const *)itemLabels itemLabelLengths:(const size_t *)itemLabelLengths itemCommands:(const char *const *)itemCommands itemCommandLengths:(const size_t *)itemCommandLengths itemKeys:(const char *const *)itemKeys itemKeyLengths:(const size_t *)itemKeyLengths itemModifiers:(const uint32_t *)itemModifiers itemSeparators:(const int *)itemSeparators itemEnabled:(const int *)itemEnabled itemChecked:(const int *)itemChecked itemCount:(size_t)itemCount {
@@ -9865,6 +10033,38 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
9865
10033
  [self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_APP_DEACTIVATED }];
9866
10034
  }
9867
10035
 
10036
+ - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
10037
+ (void)center;
10038
+ (void)notification;
10039
+ return YES;
10040
+ }
10041
+
10042
+ - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
10043
+ NSInteger activation = notification.activationType;
10044
+ if (activation != NSUserNotificationActivationTypeActionButtonClicked) return;
10045
+ NSString *token = [notification.userInfo[@"native-sdk-action-token"] isKindOfClass:[NSString class]]
10046
+ ? notification.userInfo[@"native-sdk-action-token"] : @"";
10047
+ NSString *command = token.length > 0 ? self.notificationActionCommands[token] : @"";
10048
+ if (command.length == 0) {
10049
+ [center removeDeliveredNotification:notification];
10050
+ return;
10051
+ }
10052
+ NSString *identifier = notification.identifier ?: @"";
10053
+ if (identifier.length > 0 &&
10054
+ [self.notificationActionTokensByIdentifier[identifier] isEqualToString:token]) {
10055
+ [self.notificationActionTokensByIdentifier removeObjectForKey:identifier];
10056
+ }
10057
+ [self.notificationActionCommands removeObjectForKey:token];
10058
+ const char *commandBytes = command.UTF8String ?: "";
10059
+ [self emitEvent:(native_sdk_appkit_event_t){
10060
+ .kind = NATIVE_SDK_APPKIT_EVENT_NOTIFICATION_COMMAND,
10061
+ .window_id = [self activeCommandWindowId],
10062
+ .command_name = commandBytes,
10063
+ .command_name_len = [command lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
10064
+ }];
10065
+ [center removeDeliveredNotification:notification];
10066
+ }
10067
+
9868
10068
  - (void)startAppearanceObservers {
9869
10069
  if (self.observesAppearanceChanges) {
9870
10070
  return;
@@ -9980,7 +10180,16 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
9980
10180
  - (void)emitWindowFrameForWindowId:(uint64_t)windowId open:(BOOL)open {
9981
10181
  NSWindow *window = self.windows[@(windowId)] ?: self.window;
9982
10182
  NSString *label = self.windowLabels[@(windowId)] ?: (windowId == 1 ? self.windowLabel : @"");
9983
- NSRect frame = window.frame;
10183
+ // The frame event's rect is the CONTENT rect in screen coordinates,
10184
+ // never window.frame: every consumer treats these numbers as content
10185
+ // geometry — shell layout bounds, the resize channel
10186
+ // (contentView.bounds), window-state persistence, and
10187
+ // createWindowWithId:'s initWithContentRect: round-trip — and the
10188
+ // GTK/Win32 hosts report content size on the same event. The outer
10189
+ // frame here laid shell views past the content's bottom edge on
10190
+ // open/restore and grew every restored window by the titlebar height
10191
+ // once per launch.
10192
+ NSRect frame = [window contentRectForFrameRect:window.frame];
9984
10193
  [self emitEvent:(native_sdk_appkit_event_t){
9985
10194
  .kind = NATIVE_SDK_APPKIT_EVENT_WINDOW_FRAME,
9986
10195
  .window_id = windowId,
@@ -11916,29 +12125,44 @@ static void NativeSdkVideoFittedSize(double naturalWidth, double naturalHeight,
11916
12125
 
11917
12126
  - (void)trayMenuItemClicked:(NSMenuItem *)menuItem {
11918
12127
  if (self.trayCallback) {
11919
- self.trayCallback(self.trayContext, (uint32_t)menuItem.tag);
12128
+ uint64_t packed = (uint64_t)(NSUInteger)menuItem.tag;
12129
+ self.trayCallback(self.trayContext, (uint32_t)(packed >> 32), (uint32_t)packed);
12130
+ }
12131
+ }
12132
+
12133
+ - (NativeSdkStatusItemEntry *)statusEntryForId:(uint32_t)identifier {
12134
+ return self.statusItems[@(identifier)];
12135
+ }
12136
+
12137
+ - (NativeSdkStatusItemEntry *)statusEntryForMenu:(NSMenu *)menu {
12138
+ for (NativeSdkStatusItemEntry *entry in self.statusItems.allValues) {
12139
+ if (entry.menu == menu) return entry;
11920
12140
  }
12141
+ return nil;
11921
12142
  }
11922
12143
 
11923
- - (void)emitStatusCommand:(NSString *)command {
12144
+ - (void)emitStatusCommand:(NSString *)command statusItemId:(uint32_t)statusItemId {
11924
12145
  if (command.length == 0) return;
11925
12146
  const char *bytes = command.UTF8String;
11926
12147
  [self emitEvent:(native_sdk_appkit_event_t){
11927
12148
  .kind = NATIVE_SDK_APPKIT_EVENT_TRAY_COMMAND,
11928
12149
  .window_id = [self activeCommandWindowId],
12150
+ .status_item_id = statusItemId,
11929
12151
  .command_name = bytes,
11930
12152
  .command_name_len = [command lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
11931
12153
  }];
11932
12154
  }
11933
12155
 
11934
12156
  - (void)statusItemActivated:(id)sender {
11935
- (void)sender;
11936
- if ((NSEvent.modifierFlags & NSEventModifierFlagOption) != 0 && self.statusAlternateActivationCommand.length > 0) {
11937
- [self emitStatusCommand:self.statusAlternateActivationCommand];
12157
+ NSStatusBarButton *button = [sender isKindOfClass:[NSStatusBarButton class]] ? sender : nil;
12158
+ NativeSdkStatusItemEntry *entry = button ? [self statusEntryForId:(uint32_t)button.tag] : nil;
12159
+ if (!entry) return;
12160
+ if ((NSEvent.modifierFlags & NSEventModifierFlagOption) != 0 && entry.alternateActivationCommand.length > 0) {
12161
+ [self emitStatusCommand:entry.alternateActivationCommand statusItemId:entry.identifier];
11938
12162
  return;
11939
12163
  }
11940
- [self emitStatusCommand:self.statusActivationCommand];
11941
- if (self.statusMenu) [self.statusItem popUpStatusItemMenu:self.statusMenu];
12164
+ [self emitStatusCommand:entry.activationCommand statusItemId:entry.identifier];
12165
+ if (entry.menu) [entry.item popUpStatusItemMenu:entry.menu];
11942
12166
  }
11943
12167
 
11944
12168
  @end
@@ -12137,7 +12361,7 @@ static BOOL NativeSdkPolicyListMatches(NSArray<NSString *> *values, NSURL *url)
12137
12361
  return NO;
12138
12362
  }
12139
12363
 
12140
- 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, uint32_t window_flags) {
12364
+ 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, int dock_visible, 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 initial_placement, int restore_policy, int resizable, int titlebar_style, int show_policy, uint32_t window_flags) {
12141
12365
  @autoreleasepool {
12142
12366
  NSString *appNameString = [[NSString alloc] initWithBytes:app_name length:app_name_len encoding:NSUTF8StringEncoding] ?: @"native-sdk";
12143
12367
  NSString *displayNameString = [[NSString alloc] initWithBytes:display_name length:display_name_len encoding:NSUTF8StringEncoding] ?: @"";
@@ -12147,7 +12371,7 @@ native_sdk_appkit_host_t *native_sdk_appkit_create(const char *app_name, size_t
12147
12371
  NSString *bundleIdString = [[NSString alloc] initWithBytes:bundle_id length:bundle_id_len encoding:NSUTF8StringEncoding] ?: @"dev.native_sdk.app";
12148
12372
  NSString *iconPathString = [[NSString alloc] initWithBytes:icon_path length:icon_path_len encoding:NSUTF8StringEncoding] ?: @"";
12149
12373
  NSString *windowLabelString = [[NSString alloc] initWithBytes:window_label length:window_label_len encoding:NSUTF8StringEncoding] ?: @"main";
12150
- NativeSdkAppKitHost *host = [[NativeSdkAppKitHost alloc] initWithAppName:appNameString displayName:displayNameString version:versionString aboutDescription:aboutDescriptionString hasWebContent:(has_web_content != 0) windowTitle:windowTitleString bundleIdentifier:bundleIdString iconPath:iconPathString windowLabel:windowLabelString x:x y:y width:width height:height restoreFrame:(restore_frame != 0) resizable:(resizable != 0) titlebarStyle:titlebar_style showPolicy:show_policy windowFlags:window_flags];
12374
+ NativeSdkAppKitHost *host = [[NativeSdkAppKitHost alloc] initWithAppName:appNameString displayName:displayNameString version:versionString aboutDescription:aboutDescriptionString hasWebContent:(has_web_content != 0) dockVisible:(dock_visible != 0) windowTitle:windowTitleString bundleIdentifier:bundleIdString iconPath:iconPathString windowLabel:windowLabelString x:x y:y width:width height:height restoreFrame:(restore_frame != 0) initialPlacement:initial_placement restorePolicy:restore_policy resizable:(resizable != 0) titlebarStyle:titlebar_style showPolicy:show_policy windowFlags:window_flags];
12151
12375
  return (__bridge_retained native_sdk_appkit_host_t *)host;
12152
12376
  }
12153
12377
  }
@@ -12465,11 +12689,11 @@ void native_sdk_appkit_set_shortcuts(native_sdk_appkit_host_t *host, const char
12465
12689
  [object setShortcutsWithIds:ids idLengths:id_lens keys:keys keyLengths:key_lens modifiers:modifiers count:count];
12466
12690
  }
12467
12691
 
12468
- 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, uint32_t window_flags) {
12692
+ 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 initial_placement, int restore_policy, int resizable, int titlebar_style, int show_policy, uint32_t window_flags) {
12469
12693
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
12470
12694
  NSString *titleString = window_title ? [[NSString alloc] initWithBytes:window_title length:window_title_len encoding:NSUTF8StringEncoding] : @"";
12471
12695
  NSString *labelString = window_label ? [[NSString alloc] initWithBytes:window_label length:window_label_len encoding:NSUTF8StringEncoding] : @"";
12472
- return [object createWindowWithId:window_id title:titleString ?: @"" label:labelString ?: @"" x:x y:y width:width height:height restoreFrame:(restore_frame != 0) resizable:(resizable != 0) titlebarStyle:titlebar_style showPolicy:show_policy windowFlags:window_flags makeMain:NO] ? 1 : 0;
12696
+ return [object createWindowWithId:window_id title:titleString ?: @"" label:labelString ?: @"" x:x y:y width:width height:height restoreFrame:(restore_frame != 0) initialPlacement:initial_placement restorePolicy:restore_policy resizable:(resizable != 0) titlebarStyle:titlebar_style showPolicy:show_policy windowFlags:window_flags makeMain:NO] ? 1 : 0;
12473
12697
  }
12474
12698
 
12475
12699
  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) {
@@ -12757,17 +12981,42 @@ int native_sdk_appkit_clipboard_write_data(native_sdk_appkit_host_t *host, const
12757
12981
  return [pasteboard setData:data forType:type] ? 1 : 0;
12758
12982
  }
12759
12983
 
12760
- 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) {
12761
- (void)host;
12984
+ 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, 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) {
12985
+ if (!host || ((action_label_len == 0) != (action_command_len == 0))) return 0;
12986
+ NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
12762
12987
  NSString *titleString = title ? [[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] : @"";
12763
12988
  if (titleString.length == 0) return 0;
12764
12989
  NSString *subtitleString = subtitle ? [[NSString alloc] initWithBytes:subtitle length:subtitle_len encoding:NSUTF8StringEncoding] : @"";
12765
12990
  NSString *bodyString = body ? [[NSString alloc] initWithBytes:body length:body_len encoding:NSUTF8StringEncoding] : @"";
12991
+ NSString *identifier = notification_id ? [[NSString alloc] initWithBytes:notification_id length:notification_id_len encoding:NSUTF8StringEncoding] : @"";
12992
+ NSString *actionLabel = action_label ? [[NSString alloc] initWithBytes:action_label length:action_label_len encoding:NSUTF8StringEncoding] : @"";
12993
+ NSString *actionCommand = action_command ? [[NSString alloc] initWithBytes:action_command length:action_command_len encoding:NSUTF8StringEncoding] : @"";
12994
+ if (identifier.length > 0) {
12995
+ NSString *oldToken = object.notificationActionTokensByIdentifier[identifier];
12996
+ if (oldToken.length > 0) [object.notificationActionCommands removeObjectForKey:oldToken];
12997
+ [object.notificationActionTokensByIdentifier removeObjectForKey:identifier];
12998
+ }
12766
12999
  NSUserNotification *notification = [[NSUserNotification alloc] init];
12767
13000
  notification.title = titleString;
12768
13001
  if (subtitleString.length > 0) notification.subtitle = subtitleString;
12769
13002
  if (bodyString.length > 0) notification.informativeText = bodyString;
12770
- [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
13003
+ if (identifier.length > 0) notification.identifier = identifier;
13004
+ if (actionCommand.length > 0) {
13005
+ NSString *actionToken = [NSUUID UUID].UUIDString;
13006
+ object.notificationActionCommands[actionToken] = actionCommand;
13007
+ if (identifier.length > 0) object.notificationActionTokensByIdentifier[identifier] = actionToken;
13008
+ notification.hasActionButton = YES;
13009
+ notification.actionButtonTitle = actionLabel;
13010
+ notification.userInfo = @{ @"native-sdk-action-token": actionToken };
13011
+ }
13012
+ NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
13013
+ center.delegate = object;
13014
+ if (identifier.length > 0) {
13015
+ for (NSUserNotification *delivered in center.deliveredNotifications) {
13016
+ if ([delivered.identifier isEqualToString:identifier]) [center removeDeliveredNotification:delivered];
13017
+ }
13018
+ }
13019
+ [center deliverNotification:notification];
12771
13020
  return 1;
12772
13021
  }
12773
13022
 
@@ -13028,20 +13277,21 @@ static NSImage *NativeSdkTrayImageWithOpacity(NSImage *source, double opacity) {
13028
13277
  return image;
13029
13278
  }
13030
13279
 
13031
- static void NativeSdkApplyTrayPresentation(NativeSdkAppKitHost *object, NSString *requestedTitle, double width, int tone, double iconOpacity, BOOL monospaced) {
13032
- if (!object.statusItem) return;
13033
- object.statusPresentationWidth = width;
13034
- object.statusPresentationTone = tone;
13035
- object.statusPresentationIconOpacity = iconOpacity;
13036
- object.statusPresentationMonospaced = monospaced;
13280
+ static void NativeSdkApplyTrayPresentation(NativeSdkAppKitHost *object, NativeSdkStatusItemEntry *entry, NSString *requestedTitle, double width, int tone, double iconOpacity, BOOL monospaced) {
13281
+ if (!entry.item) return;
13282
+ entry.presentationTitle = requestedTitle ?: @"";
13283
+ entry.presentationWidth = width;
13284
+ entry.presentationTone = tone;
13285
+ entry.presentationIconOpacity = iconOpacity;
13286
+ entry.presentationMonospaced = monospaced;
13037
13287
  NSString *title = requestedTitle ?: @"";
13038
- if (!object.statusItemBaseImage && title.length == 0) {
13288
+ if (!entry.baseImage && title.length == 0) {
13039
13289
  title = object.appName.length > 0 ? [object.appName substringToIndex:MIN(1, object.appName.length)] : @"Z";
13040
13290
  }
13041
13291
  NSFont *font = monospaced
13042
13292
  ? ([NSFont fontWithName:@"Geist Mono" size:11] ?: [NSFont monospacedDigitSystemFontOfSize:11 weight:NSFontWeightRegular])
13043
13293
  : [NSFont systemFontOfSize:11];
13044
- NSStatusBarButton *button = object.statusItem.button;
13294
+ NSStatusBarButton *button = entry.item.button;
13045
13295
  if (tone == 0) {
13046
13296
  button.title = title;
13047
13297
  button.font = font;
@@ -13054,43 +13304,63 @@ static void NativeSdkApplyTrayPresentation(NativeSdkAppKitHost *object, NSString
13054
13304
  }];
13055
13305
  button.contentTintColor = color;
13056
13306
  }
13057
- button.image = NativeSdkTrayImageWithOpacity(object.statusItemBaseImage, iconOpacity);
13058
- object.statusItem.length = width > 0 ? width : (requestedTitle.length > 0 ? NSVariableStatusItemLength : NSSquareStatusItemLength);
13307
+ button.image = NativeSdkTrayImageWithOpacity(entry.baseImage, iconOpacity);
13308
+ entry.item.length = width > 0 ? width : (requestedTitle.length > 0 ? NSVariableStatusItemLength : NSSquareStatusItemLength);
13059
13309
  }
13060
13310
 
13061
- 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, double width, int tone, double icon_opacity, int monospaced, 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) {
13311
+ static void NativeSdkApplyTrayShell(NativeSdkAppKitHost *object, NativeSdkStatusItemEntry *entry, const char *icon_path, size_t icon_path_len, const char *tooltip, size_t tooltip_len, int visible, 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) {
13312
+ entry.baseImage = nil;
13313
+ if (icon_path && icon_path_len > 0) {
13314
+ NSString *path = [[NSString alloc] initWithBytes:icon_path length:icon_path_len encoding:NSUTF8StringEncoding];
13315
+ NSImage *image = [[NSImage alloc] initWithContentsOfFile:NativeSdkResolvedAssetFilePath(path)];
13316
+ if (image) {
13317
+ image.template = YES;
13318
+ image.size = NSMakeSize(18, 18);
13319
+ entry.baseImage = image;
13320
+ }
13321
+ }
13322
+ entry.item.button.toolTip = tooltip && tooltip_len > 0 ? ([[NSString alloc] initWithBytes:tooltip length:tooltip_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13323
+ entry.activationCommand = activation_command ? ([[NSString alloc] initWithBytes:activation_command length:activation_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13324
+ entry.alternateActivationCommand = alternate_activation_command ? ([[NSString alloc] initWithBytes:alternate_activation_command length:alternate_activation_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13325
+ entry.openCommand = open_command ? ([[NSString alloc] initWithBytes:open_command length:open_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13326
+ NSStatusBarButton *button = entry.item.button;
13327
+ button.tag = (NSInteger)entry.identifier;
13328
+ button.target = object;
13329
+ button.action = (entry.activationCommand.length > 0 || entry.alternateActivationCommand.length > 0) ? @selector(statusItemActivated:) : NULL;
13330
+ if (button.action) [button sendActionOn:NSEventMaskLeftMouseUp];
13331
+ entry.item.visible = visible != 0;
13332
+ }
13333
+
13334
+ void native_sdk_appkit_create_tray(native_sdk_appkit_host_t *host, uint32_t status_item_id, const char *icon_path, size_t icon_path_len, const char *title, size_t title_len, const char *tooltip, size_t tooltip_len, int visible, double width, int tone, double icon_opacity, int monospaced, 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) {
13062
13335
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13063
13336
  @autoreleasepool {
13064
- if (object.statusItem) {
13065
- [[NSStatusBar systemStatusBar] removeStatusItem:object.statusItem];
13337
+ NSNumber *key = @(status_item_id);
13338
+ NativeSdkStatusItemEntry *entry = object.statusItems[key];
13339
+ if (entry) {
13340
+ [[NSStatusBar systemStatusBar] removeStatusItem:entry.item];
13341
+ } else {
13342
+ entry = [[NativeSdkStatusItemEntry alloc] init];
13343
+ entry.identifier = status_item_id;
13344
+ object.statusItems[key] = entry;
13066
13345
  }
13067
- object.statusMenu = nil;
13346
+ entry.menu = nil;
13068
13347
  BOOL hasTitle = title != NULL && title_len > 0;
13069
- object.statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:hasTitle ? NSVariableStatusItemLength : NSSquareStatusItemLength];
13070
- object.statusItemBaseImage = nil;
13071
-
13072
- if (icon_path && icon_path_len > 0) {
13073
- NSString *path = [[NSString alloc] initWithBytes:icon_path length:icon_path_len encoding:NSUTF8StringEncoding];
13074
- NSImage *image = [[NSImage alloc] initWithContentsOfFile:NativeSdkResolvedAssetFilePath(path)];
13075
- if (image) {
13076
- image.template = YES;
13077
- image.size = NSMakeSize(18, 18);
13078
- object.statusItemBaseImage = image;
13079
- }
13080
- }
13348
+ entry.item = [[NSStatusBar systemStatusBar] statusItemWithLength:hasTitle ? NSVariableStatusItemLength : NSSquareStatusItemLength];
13081
13349
  NSString *titleString = hasTitle ? ([[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13082
- if (tooltip && tooltip_len > 0) {
13083
- object.statusItem.button.toolTip = [[NSString alloc] initWithBytes:tooltip length:tooltip_len encoding:NSUTF8StringEncoding];
13084
- }
13085
- object.statusActivationCommand = activation_command ? ([[NSString alloc] initWithBytes:activation_command length:activation_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13086
- object.statusAlternateActivationCommand = alternate_activation_command ? ([[NSString alloc] initWithBytes:alternate_activation_command length:alternate_activation_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13087
- object.statusOpenCommand = open_command ? ([[NSString alloc] initWithBytes:open_command length:open_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13088
- if (object.statusActivationCommand.length > 0 || object.statusAlternateActivationCommand.length > 0) {
13089
- object.statusItem.button.target = object;
13090
- object.statusItem.button.action = @selector(statusItemActivated:);
13091
- [object.statusItem.button sendActionOn:NSEventMaskLeftMouseUp];
13092
- }
13093
- NativeSdkApplyTrayPresentation(object, titleString, width, tone, icon_opacity, monospaced != 0);
13350
+ NativeSdkApplyTrayShell(object, entry, icon_path, icon_path_len, tooltip, tooltip_len, visible, activation_command, activation_command_len, alternate_activation_command, alternate_activation_command_len, open_command, open_command_len);
13351
+ NativeSdkApplyTrayPresentation(object, entry, titleString, width, tone, icon_opacity, monospaced != 0);
13352
+ }
13353
+ }
13354
+
13355
+ void native_sdk_appkit_update_tray_shell(native_sdk_appkit_host_t *host, uint32_t status_item_id, const char *icon_path, size_t icon_path_len, const char *tooltip, size_t tooltip_len, int visible, 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) {
13356
+ NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13357
+ @autoreleasepool {
13358
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13359
+ if (!entry) return;
13360
+ NativeSdkApplyTrayShell(object, entry, icon_path, icon_path_len, tooltip, tooltip_len, visible, activation_command, activation_command_len, alternate_activation_command, alternate_activation_command_len, open_command, open_command_len);
13361
+ NativeSdkApplyTrayPresentation(object, entry, entry.presentationTitle, entry.presentationWidth, entry.presentationTone, entry.presentationIconOpacity, entry.presentationMonospaced);
13362
+ if (entry.activationCommand.length == 0 && entry.alternateActivationCommand.length == 0) entry.item.menu = entry.menu;
13363
+ else entry.item.menu = nil;
13094
13364
  }
13095
13365
  }
13096
13366
 
@@ -13183,7 +13453,7 @@ static NSView *NativeSdkTrayHeroView(NSString *headline, NSString *quota) {
13183
13453
  return view;
13184
13454
  }
13185
13455
 
13186
- static NSView *NativeSdkTrayAgentsView(NativeSdkAppKitHost *object, const uint32_t *itemIds, NSArray<NSString *> *labels, NSArray<NSString *> *states, NSArray<NSNumber *> *enabled, NSArray<NSString *> *keys, NSArray<NSNumber *> *modifiers) {
13456
+ static NSView *NativeSdkTrayAgentsView(NativeSdkAppKitHost *object, uint32_t statusItemId, const uint32_t *itemIds, NSArray<NSString *> *labels, NSArray<NSString *> *states, NSArray<NSNumber *> *enabled, NSArray<NSString *> *keys, NSArray<NSNumber *> *modifiers) {
13187
13457
  NSStackView *row = [[NSStackView alloc] initWithFrame:NSMakeRect(0, 0, 320, 32)];
13188
13458
  row.orientation = NSUserInterfaceLayoutOrientationHorizontal;
13189
13459
  row.alignment = NSLayoutAttributeCenterY;
@@ -13209,7 +13479,7 @@ static NSView *NativeSdkTrayAgentsView(NativeSdkAppKitHost *object, const uint32
13209
13479
  label.translatesAutoresizingMaskIntoConstraints = NO;
13210
13480
  label.accessibilityElement = NO;
13211
13481
  NSButton *button = [NSButton buttonWithTitle:@"" target:object action:@selector(trayMenuItemClicked:)];
13212
- button.tag = itemIds[index];
13482
+ button.tag = (NSInteger)(((uint64_t)statusItemId << 32) | itemIds[index]);
13213
13483
  button.bordered = NO;
13214
13484
  button.transparent = YES;
13215
13485
  button.enabled = enabled[index].boolValue;
@@ -13307,14 +13577,15 @@ static NSView *NativeSdkTrayReadoutView(NSString *label, NSString *value, BOOL i
13307
13577
  return row;
13308
13578
  }
13309
13579
 
13310
- 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, 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) {
13580
+ void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, uint32_t status_item_id, 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) {
13311
13581
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13312
13582
  @autoreleasepool {
13313
- if (!object.statusItem) return;
13314
- NSMenu *menu = object.statusMenu;
13583
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13584
+ if (!entry) return;
13585
+ NSMenu *menu = entry.menu;
13315
13586
  if (menu == nil) {
13316
13587
  menu = [[NSMenu alloc] initWithTitle:@""];
13317
- object.statusMenu = menu;
13588
+ entry.menu = menu;
13318
13589
  } else {
13319
13590
  [menu removeAllItems];
13320
13591
  }
@@ -13329,7 +13600,7 @@ void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, const ui
13329
13600
  NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:label ?: @""
13330
13601
  action:@selector(trayMenuItemClicked:)
13331
13602
  keyEquivalent:@""];
13332
- item.tag = (NSInteger)item_ids[i];
13603
+ item.tag = (NSInteger)(((uint64_t)status_item_id << 32) | item_ids[i]);
13333
13604
  item.target = object;
13334
13605
  item.enabled = enabled_flags[i] != 0;
13335
13606
  if (keys[i] && key_lens[i] > 0) {
@@ -13370,7 +13641,7 @@ void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, const ui
13370
13641
  // NSMenu consume it before the button can dispatch its id.
13371
13642
  item.keyEquivalent = @"";
13372
13643
  item.keyEquivalentModifierMask = 0;
13373
- item.view = NativeSdkTrayAgentsView(object, agentIds, agentLabels, agentStates, agentEnabled, agentKeys, agentModifiers);
13644
+ item.view = NativeSdkTrayAgentsView(object, status_item_id, agentIds, agentLabels, agentStates, agentEnabled, agentKeys, agentModifiers);
13374
13645
  [menu addItem:item];
13375
13646
  continue;
13376
13647
  }
@@ -13396,44 +13667,47 @@ void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, const ui
13396
13667
  for (NSView *row in readoutRows) [row.widthAnchor constraintEqualToConstant:widest].active = YES;
13397
13668
  }
13398
13669
  menu.delegate = object;
13399
- if (object.statusActivationCommand.length == 0 && object.statusAlternateActivationCommand.length == 0) {
13400
- object.statusItem.menu = menu;
13670
+ if (entry.activationCommand.length == 0 && entry.alternateActivationCommand.length == 0) {
13671
+ entry.item.menu = menu;
13401
13672
  }
13402
13673
  }
13403
13674
  }
13404
13675
 
13405
- void native_sdk_appkit_update_tray_title(native_sdk_appkit_host_t *host, const char *title, size_t title_len) {
13676
+ void native_sdk_appkit_update_tray_title(native_sdk_appkit_host_t *host, uint32_t status_item_id, const char *title, size_t title_len) {
13406
13677
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13407
13678
  @autoreleasepool {
13408
- if (!object.statusItem) return;
13679
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13680
+ if (!entry) return;
13409
13681
  NSString *value = title && title_len > 0 ? ([[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13410
13682
  NativeSdkApplyTrayPresentation(
13411
13683
  object,
13684
+ entry,
13412
13685
  value,
13413
- object.statusPresentationWidth,
13414
- object.statusPresentationTone,
13415
- object.statusPresentationIconOpacity,
13416
- object.statusPresentationMonospaced
13686
+ entry.presentationWidth,
13687
+ entry.presentationTone,
13688
+ entry.presentationIconOpacity,
13689
+ entry.presentationMonospaced
13417
13690
  );
13418
13691
  }
13419
13692
  }
13420
13693
 
13421
- void native_sdk_appkit_update_tray_presentation(native_sdk_appkit_host_t *host, const char *title, size_t title_len, double width, int tone, double icon_opacity, int monospaced) {
13694
+ void native_sdk_appkit_update_tray_presentation(native_sdk_appkit_host_t *host, uint32_t status_item_id, const char *title, size_t title_len, double width, int tone, double icon_opacity, int monospaced) {
13422
13695
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13423
13696
  @autoreleasepool {
13697
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13698
+ if (!entry) return;
13424
13699
  NSString *value = title ? ([[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13425
- NativeSdkApplyTrayPresentation(object, value, width, tone, icon_opacity, monospaced != 0);
13700
+ NativeSdkApplyTrayPresentation(object, entry, value, width, tone, icon_opacity, monospaced != 0);
13426
13701
  }
13427
13702
  }
13428
13703
 
13429
- void native_sdk_appkit_remove_tray(native_sdk_appkit_host_t *host) {
13704
+ void native_sdk_appkit_remove_tray(native_sdk_appkit_host_t *host, uint32_t status_item_id) {
13430
13705
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13431
- if (object.statusItem) {
13432
- [[NSStatusBar systemStatusBar] removeStatusItem:object.statusItem];
13433
- object.statusItem = nil;
13434
- object.statusItemBaseImage = nil;
13435
- object.statusMenu = nil;
13436
- }
13706
+ NSNumber *key = @(status_item_id);
13707
+ NativeSdkStatusItemEntry *entry = object.statusItems[key];
13708
+ if (!entry) return;
13709
+ [[NSStatusBar systemStatusBar] removeStatusItem:entry.item];
13710
+ [object.statusItems removeObjectForKey:key];
13437
13711
  }
13438
13712
 
13439
13713
  void native_sdk_appkit_set_tray_callback(native_sdk_appkit_host_t *host, native_sdk_appkit_tray_callback_t callback, void *context) {