@native-sdk/cli 0.9.0 → 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 (92) hide show
  1. package/build/app.zig +331 -24
  2. package/build.zig +179 -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 +167 -12
  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 +11 -5
  26. package/skill-data/ts-core/SKILL.md +53 -20
  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 +29 -0
  31. package/src/app_runner/ts_core_main.zig +5 -0
  32. package/src/app_runner/ts_core_mobile.zig +241 -0
  33. package/src/automation/protocol.zig +5 -1
  34. package/src/automation/snapshot.zig +8 -6
  35. package/src/embed/app_exports.zig +1 -0
  36. package/src/embed/root.zig +1 -0
  37. package/src/embed/tests.zig +28 -0
  38. package/src/embed/ui_host.zig +68 -2
  39. package/src/platform/linux/cef_host.cpp +7 -1
  40. package/src/platform/linux/gtk_host.c +200 -5
  41. package/src/platform/linux/gtk_host.h +2 -1
  42. package/src/platform/linux/root.zig +69 -4
  43. package/src/platform/macos/appkit_host.h +11 -7
  44. package/src/platform/macos/appkit_host.m +210 -88
  45. package/src/platform/macos/cef_host.mm +210 -88
  46. package/src/platform/macos/root.zig +77 -30
  47. package/src/platform/null_platform.zig +234 -51
  48. package/src/platform/null_platform_tests.zig +39 -0
  49. package/src/platform/root.zig +10 -0
  50. package/src/platform/types.zig +105 -16
  51. package/src/platform/windows/root.zig +20 -6
  52. package/src/platform/windows/webview2_host.cpp +41 -5
  53. package/src/root.zig +7 -0
  54. package/src/runtime/api.zig +8 -0
  55. package/src/runtime/automation_commands.zig +35 -2
  56. package/src/runtime/automation_snapshot.zig +32 -24
  57. package/src/runtime/bridge_permission_tests.zig +35 -1
  58. package/src/runtime/bridge_responses.zig +1 -1
  59. package/src/runtime/builtin_bridge.zig +11 -1
  60. package/src/runtime/core.zig +12 -6
  61. package/src/runtime/effects.zig +1323 -35
  62. package/src/runtime/effects_file_tests.zig +587 -0
  63. package/src/runtime/effects_notification_tests.zig +11 -0
  64. package/src/runtime/file_access.zig +400 -0
  65. package/src/runtime/file_effect_crash_helper.zig +28 -0
  66. package/src/runtime/file_effect_crash_tests.zig +37 -0
  67. package/src/runtime/flow.zig +21 -9
  68. package/src/runtime/native_command_event_tests.zig +44 -2
  69. package/src/runtime/pty.zig +5 -2
  70. package/src/runtime/root.zig +5 -0
  71. package/src/runtime/session_blobs.zig +4 -1
  72. package/src/runtime/session_journal.zig +56 -5
  73. package/src/runtime/session_record.zig +38 -0
  74. package/src/runtime/session_replay.zig +97 -7
  75. package/src/runtime/session_tests.zig +81 -0
  76. package/src/runtime/state.zig +10 -0
  77. package/src/runtime/system_services.zig +129 -36
  78. package/src/runtime/ts_core_host.zig +281 -11
  79. package/src/runtime/ts_core_host_tests.zig +95 -1
  80. package/src/runtime/ts_ui_app.zig +461 -8
  81. package/src/runtime/ui_app.zig +321 -109
  82. package/src/runtime/ui_app_tests.zig +132 -6
  83. package/src/runtime/ui_app_window_tests.zig +61 -7
  84. package/src/runtime/validation.zig +20 -0
  85. package/src/tooling/templates.zig +28 -0
  86. package/src/tooling/ts_core.zig +83 -0
  87. package/tools/corewire/emit.zig +3 -3
  88. package/tools/corewire/emit_facade.zig +65 -5
  89. package/tools/corewire/emit_profile.zig +2 -2
  90. package/tools/corewire/emit_service.zig +2 -2
  91. package/tools/corewire/service_contract.zig +4 -4
  92. package/tools/corewire/sidecar.zig +4 -4
@@ -763,7 +763,22 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
763
763
  @property(nonatomic, assign) uint32_t modifiers;
764
764
  @end
765
765
 
766
- @interface NativeSdkAppKitHost : NSObject <WKNavigationDelegate, NSMenuDelegate>
766
+ @interface NativeSdkStatusItemEntry : NSObject
767
+ @property(nonatomic, assign) uint32_t identifier;
768
+ @property(nonatomic, strong) NSStatusItem *item;
769
+ @property(nonatomic, strong) NSImage *baseImage;
770
+ @property(nonatomic, strong) NSMenu *menu;
771
+ @property(nonatomic, strong) NSString *presentationTitle;
772
+ @property(nonatomic, assign) double presentationWidth;
773
+ @property(nonatomic, assign) int presentationTone;
774
+ @property(nonatomic, assign) double presentationIconOpacity;
775
+ @property(nonatomic, assign) BOOL presentationMonospaced;
776
+ @property(nonatomic, strong) NSString *activationCommand;
777
+ @property(nonatomic, strong) NSString *alternateActivationCommand;
778
+ @property(nonatomic, strong) NSString *openCommand;
779
+ @end
780
+
781
+ @interface NativeSdkAppKitHost : NSObject <WKNavigationDelegate, NSMenuDelegate, NSUserNotificationCenterDelegate>
767
782
  @property(nonatomic, strong) NSWindow *window;
768
783
  @property(nonatomic, strong) WKWebView *webView;
769
784
  @property(nonatomic, strong) NativeSdkWindowDelegate *delegate;
@@ -825,6 +840,12 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
825
840
  * block starts still gets its own turn. */
826
841
  @property(atomic, assign) BOOL crossThreadFramePending;
827
842
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSTimer *> *appTimers;
843
+ /* Notification userInfo persists with the delivered notification, so it
844
+ * carries only a per-process token. Commands remain in this live host;
845
+ * stale notifications from an earlier execution cannot dispatch into a
846
+ * later one. The identifier map invalidates same-id replacements. */
847
+ @property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *notificationActionCommands;
848
+ @property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *notificationActionTokensByIdentifier;
828
849
  /* The app's single audio player and its position-tick timer. One player
829
850
  * is the whole surface: a music app plays one track at a time, and a
830
851
  * second concurrent stream would be mixer design the platform seam has
@@ -986,16 +1007,7 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
986
1007
  @property(nonatomic, strong) id willTerminateObserver;
987
1008
  @property(nonatomic, strong) dispatch_source_t sigtermSource;
988
1009
  @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;
1010
+ @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NativeSdkStatusItemEntry *> *statusItems;
999
1011
  @property(nonatomic, assign) native_sdk_appkit_tray_callback_t trayCallback;
1000
1012
  @property(nonatomic, assign) void *trayContext;
1001
1013
  @property(nonatomic, strong) NSArray<NSString *> *allowedNavigationOrigins;
@@ -1068,7 +1080,9 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
1068
1080
  - (NSMenuItem *)commandMenuItem:(NSString *)title command:(NSString *)command key:(NSString *)key modifiers:(uint32_t)modifiers enabled:(BOOL)enabled checked:(BOOL)checked;
1069
1081
  - (void)menuCommandItemClicked:(NSMenuItem *)menuItem;
1070
1082
  - (void)menuWillOpen:(NSMenu *)menu;
1071
- - (void)emitStatusCommand:(NSString *)command;
1083
+ - (NativeSdkStatusItemEntry *)statusEntryForId:(uint32_t)identifier;
1084
+ - (NativeSdkStatusItemEntry *)statusEntryForMenu:(NSMenu *)menu;
1085
+ - (void)emitStatusCommand:(NSString *)command statusItemId:(uint32_t)statusItemId;
1072
1086
  - (void)statusItemActivated:(id)sender;
1073
1087
  - (uint64_t)activeCommandWindowId;
1074
1088
  - (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 +1095,8 @@ static int NativeSdkCredentialStatus(OSStatus status, int missingCode) {
1081
1095
  - (void)stopApplicationActivationObservers;
1082
1096
  - (void)applicationDidBecomeActive:(NSNotification *)notification;
1083
1097
  - (void)applicationDidResignActive:(NSNotification *)notification;
1098
+ - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification;
1099
+ - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification;
1084
1100
  - (void)startAppearanceObservers;
1085
1101
  - (void)stopAppearanceObservers;
1086
1102
  - (void)emitAppearanceChanged;
@@ -7336,6 +7352,9 @@ static BOOL NativeSdkScrollDriverCanConsumeHorizontally(NativeSdkScrollDriverVie
7336
7352
  @implementation NativeSdkShortcut
7337
7353
  @end
7338
7354
 
7355
+ @implementation NativeSdkStatusItemEntry
7356
+ @end
7357
+
7339
7358
  /* ------------------------------------------------------- audio capture
7340
7359
  * Both native sources feed this one converter/quiescence target. AVAudioEngine
7341
7360
  * and ScreenCaptureKit normally hand us planar float PCM; the converter also
@@ -7704,6 +7723,9 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7704
7723
  self.nativeViewExplicitTextKeys = [[NSMutableSet alloc] init];
7705
7724
  self.bridgeEnabledChildWebViewKeys = [[NSMutableSet alloc] init];
7706
7725
  self.appTimers = [[NSMutableDictionary alloc] init];
7726
+ self.notificationActionCommands = [[NSMutableDictionary alloc] init];
7727
+ self.notificationActionTokensByIdentifier = [[NSMutableDictionary alloc] init];
7728
+ self.statusItems = [[NSMutableDictionary alloc] init];
7707
7729
  self.allowedNavigationOrigins = @[ @"zero://app", @"zero://inline" ];
7708
7730
  self.allowedExternalURLs = @[];
7709
7731
  self.externalLinkAction = 0;
@@ -7944,6 +7966,8 @@ static float NativeSdkCaptureReadRemixedSample(const AudioBufferList *buffers, c
7944
7966
  }
7945
7967
 
7946
7968
  - (void)dealloc {
7969
+ NSUserNotificationCenter *notificationCenter = [NSUserNotificationCenter defaultUserNotificationCenter];
7970
+ if (notificationCenter.delegate == self) notificationCenter.delegate = nil;
7947
7971
  [self invalidateAppTimers];
7948
7972
  [self audioCaptureStopSource:0];
7949
7973
  [self audioCaptureStopSource:1];
@@ -9242,7 +9266,7 @@ static NSString *NativeSdkAppKitBridgeScript(void) {
9242
9266
  "});"
9243
9267
  "var os=Object.freeze({"
9244
9268
  "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);},"
9269
+ "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
9270
  "revealPath:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.revealPath',{path:ensureString(options.path,'path')});},"
9247
9271
  "addRecentDocument:function(value){var options=typeof value==='string'?{path:value}:(value||{});return invoke('native-sdk.os.addRecentDocument',{path:ensureString(options.path,'path')});},"
9248
9272
  "clearRecentDocuments:function(){return invoke('native-sdk.os.clearRecentDocuments',{});}"
@@ -9608,8 +9632,9 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
9608
9632
  }
9609
9633
 
9610
9634
  - (void)menuWillOpen:(NSMenu *)menu {
9611
- if (!self.statusItem || menu != self.statusMenu) return;
9612
- [self emitStatusCommand:self.statusOpenCommand];
9635
+ NativeSdkStatusItemEntry *entry = [self statusEntryForMenu:menu];
9636
+ if (!entry) return;
9637
+ [self emitStatusCommand:entry.openCommand statusItemId:entry.identifier];
9613
9638
  }
9614
9639
 
9615
9640
  - (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 +9890,38 @@ static void NativeSdkApplyProcessDisplayName(NSString *displayName) {
9865
9890
  [self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_APP_DEACTIVATED }];
9866
9891
  }
9867
9892
 
9893
+ - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification {
9894
+ (void)center;
9895
+ (void)notification;
9896
+ return YES;
9897
+ }
9898
+
9899
+ - (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification {
9900
+ NSInteger activation = notification.activationType;
9901
+ if (activation != NSUserNotificationActivationTypeActionButtonClicked) return;
9902
+ NSString *token = [notification.userInfo[@"native-sdk-action-token"] isKindOfClass:[NSString class]]
9903
+ ? notification.userInfo[@"native-sdk-action-token"] : @"";
9904
+ NSString *command = token.length > 0 ? self.notificationActionCommands[token] : @"";
9905
+ if (command.length == 0) {
9906
+ [center removeDeliveredNotification:notification];
9907
+ return;
9908
+ }
9909
+ NSString *identifier = notification.identifier ?: @"";
9910
+ if (identifier.length > 0 &&
9911
+ [self.notificationActionTokensByIdentifier[identifier] isEqualToString:token]) {
9912
+ [self.notificationActionTokensByIdentifier removeObjectForKey:identifier];
9913
+ }
9914
+ [self.notificationActionCommands removeObjectForKey:token];
9915
+ const char *commandBytes = command.UTF8String ?: "";
9916
+ [self emitEvent:(native_sdk_appkit_event_t){
9917
+ .kind = NATIVE_SDK_APPKIT_EVENT_NOTIFICATION_COMMAND,
9918
+ .window_id = [self activeCommandWindowId],
9919
+ .command_name = commandBytes,
9920
+ .command_name_len = [command lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
9921
+ }];
9922
+ [center removeDeliveredNotification:notification];
9923
+ }
9924
+
9868
9925
  - (void)startAppearanceObservers {
9869
9926
  if (self.observesAppearanceChanges) {
9870
9927
  return;
@@ -11916,29 +11973,44 @@ static void NativeSdkVideoFittedSize(double naturalWidth, double naturalHeight,
11916
11973
 
11917
11974
  - (void)trayMenuItemClicked:(NSMenuItem *)menuItem {
11918
11975
  if (self.trayCallback) {
11919
- self.trayCallback(self.trayContext, (uint32_t)menuItem.tag);
11976
+ uint64_t packed = (uint64_t)(NSUInteger)menuItem.tag;
11977
+ self.trayCallback(self.trayContext, (uint32_t)(packed >> 32), (uint32_t)packed);
11920
11978
  }
11921
11979
  }
11922
11980
 
11923
- - (void)emitStatusCommand:(NSString *)command {
11981
+ - (NativeSdkStatusItemEntry *)statusEntryForId:(uint32_t)identifier {
11982
+ return self.statusItems[@(identifier)];
11983
+ }
11984
+
11985
+ - (NativeSdkStatusItemEntry *)statusEntryForMenu:(NSMenu *)menu {
11986
+ for (NativeSdkStatusItemEntry *entry in self.statusItems.allValues) {
11987
+ if (entry.menu == menu) return entry;
11988
+ }
11989
+ return nil;
11990
+ }
11991
+
11992
+ - (void)emitStatusCommand:(NSString *)command statusItemId:(uint32_t)statusItemId {
11924
11993
  if (command.length == 0) return;
11925
11994
  const char *bytes = command.UTF8String;
11926
11995
  [self emitEvent:(native_sdk_appkit_event_t){
11927
11996
  .kind = NATIVE_SDK_APPKIT_EVENT_TRAY_COMMAND,
11928
11997
  .window_id = [self activeCommandWindowId],
11998
+ .status_item_id = statusItemId,
11929
11999
  .command_name = bytes,
11930
12000
  .command_name_len = [command lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
11931
12001
  }];
11932
12002
  }
11933
12003
 
11934
12004
  - (void)statusItemActivated:(id)sender {
11935
- (void)sender;
11936
- if ((NSEvent.modifierFlags & NSEventModifierFlagOption) != 0 && self.statusAlternateActivationCommand.length > 0) {
11937
- [self emitStatusCommand:self.statusAlternateActivationCommand];
12005
+ NSStatusBarButton *button = [sender isKindOfClass:[NSStatusBarButton class]] ? sender : nil;
12006
+ NativeSdkStatusItemEntry *entry = button ? [self statusEntryForId:(uint32_t)button.tag] : nil;
12007
+ if (!entry) return;
12008
+ if ((NSEvent.modifierFlags & NSEventModifierFlagOption) != 0 && entry.alternateActivationCommand.length > 0) {
12009
+ [self emitStatusCommand:entry.alternateActivationCommand statusItemId:entry.identifier];
11938
12010
  return;
11939
12011
  }
11940
- [self emitStatusCommand:self.statusActivationCommand];
11941
- if (self.statusMenu) [self.statusItem popUpStatusItemMenu:self.statusMenu];
12012
+ [self emitStatusCommand:entry.activationCommand statusItemId:entry.identifier];
12013
+ if (entry.menu) [entry.item popUpStatusItemMenu:entry.menu];
11942
12014
  }
11943
12015
 
11944
12016
  @end
@@ -12757,17 +12829,42 @@ int native_sdk_appkit_clipboard_write_data(native_sdk_appkit_host_t *host, const
12757
12829
  return [pasteboard setData:data forType:type] ? 1 : 0;
12758
12830
  }
12759
12831
 
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;
12832
+ 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) {
12833
+ if (!host || ((action_label_len == 0) != (action_command_len == 0))) return 0;
12834
+ NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
12762
12835
  NSString *titleString = title ? [[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] : @"";
12763
12836
  if (titleString.length == 0) return 0;
12764
12837
  NSString *subtitleString = subtitle ? [[NSString alloc] initWithBytes:subtitle length:subtitle_len encoding:NSUTF8StringEncoding] : @"";
12765
12838
  NSString *bodyString = body ? [[NSString alloc] initWithBytes:body length:body_len encoding:NSUTF8StringEncoding] : @"";
12839
+ NSString *identifier = notification_id ? [[NSString alloc] initWithBytes:notification_id length:notification_id_len encoding:NSUTF8StringEncoding] : @"";
12840
+ NSString *actionLabel = action_label ? [[NSString alloc] initWithBytes:action_label length:action_label_len encoding:NSUTF8StringEncoding] : @"";
12841
+ NSString *actionCommand = action_command ? [[NSString alloc] initWithBytes:action_command length:action_command_len encoding:NSUTF8StringEncoding] : @"";
12842
+ if (identifier.length > 0) {
12843
+ NSString *oldToken = object.notificationActionTokensByIdentifier[identifier];
12844
+ if (oldToken.length > 0) [object.notificationActionCommands removeObjectForKey:oldToken];
12845
+ [object.notificationActionTokensByIdentifier removeObjectForKey:identifier];
12846
+ }
12766
12847
  NSUserNotification *notification = [[NSUserNotification alloc] init];
12767
12848
  notification.title = titleString;
12768
12849
  if (subtitleString.length > 0) notification.subtitle = subtitleString;
12769
12850
  if (bodyString.length > 0) notification.informativeText = bodyString;
12770
- [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
12851
+ if (identifier.length > 0) notification.identifier = identifier;
12852
+ if (actionCommand.length > 0) {
12853
+ NSString *actionToken = [NSUUID UUID].UUIDString;
12854
+ object.notificationActionCommands[actionToken] = actionCommand;
12855
+ if (identifier.length > 0) object.notificationActionTokensByIdentifier[identifier] = actionToken;
12856
+ notification.hasActionButton = YES;
12857
+ notification.actionButtonTitle = actionLabel;
12858
+ notification.userInfo = @{ @"native-sdk-action-token": actionToken };
12859
+ }
12860
+ NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
12861
+ center.delegate = object;
12862
+ if (identifier.length > 0) {
12863
+ for (NSUserNotification *delivered in center.deliveredNotifications) {
12864
+ if ([delivered.identifier isEqualToString:identifier]) [center removeDeliveredNotification:delivered];
12865
+ }
12866
+ }
12867
+ [center deliverNotification:notification];
12771
12868
  return 1;
12772
12869
  }
12773
12870
 
@@ -13028,20 +13125,21 @@ static NSImage *NativeSdkTrayImageWithOpacity(NSImage *source, double opacity) {
13028
13125
  return image;
13029
13126
  }
13030
13127
 
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;
13128
+ static void NativeSdkApplyTrayPresentation(NativeSdkAppKitHost *object, NativeSdkStatusItemEntry *entry, NSString *requestedTitle, double width, int tone, double iconOpacity, BOOL monospaced) {
13129
+ if (!entry.item) return;
13130
+ entry.presentationTitle = requestedTitle ?: @"";
13131
+ entry.presentationWidth = width;
13132
+ entry.presentationTone = tone;
13133
+ entry.presentationIconOpacity = iconOpacity;
13134
+ entry.presentationMonospaced = monospaced;
13037
13135
  NSString *title = requestedTitle ?: @"";
13038
- if (!object.statusItemBaseImage && title.length == 0) {
13136
+ if (!entry.baseImage && title.length == 0) {
13039
13137
  title = object.appName.length > 0 ? [object.appName substringToIndex:MIN(1, object.appName.length)] : @"Z";
13040
13138
  }
13041
13139
  NSFont *font = monospaced
13042
13140
  ? ([NSFont fontWithName:@"Geist Mono" size:11] ?: [NSFont monospacedDigitSystemFontOfSize:11 weight:NSFontWeightRegular])
13043
13141
  : [NSFont systemFontOfSize:11];
13044
- NSStatusBarButton *button = object.statusItem.button;
13142
+ NSStatusBarButton *button = entry.item.button;
13045
13143
  if (tone == 0) {
13046
13144
  button.title = title;
13047
13145
  button.font = font;
@@ -13054,43 +13152,63 @@ static void NativeSdkApplyTrayPresentation(NativeSdkAppKitHost *object, NSString
13054
13152
  }];
13055
13153
  button.contentTintColor = color;
13056
13154
  }
13057
- button.image = NativeSdkTrayImageWithOpacity(object.statusItemBaseImage, iconOpacity);
13058
- object.statusItem.length = width > 0 ? width : (requestedTitle.length > 0 ? NSVariableStatusItemLength : NSSquareStatusItemLength);
13155
+ button.image = NativeSdkTrayImageWithOpacity(entry.baseImage, iconOpacity);
13156
+ entry.item.length = width > 0 ? width : (requestedTitle.length > 0 ? NSVariableStatusItemLength : NSSquareStatusItemLength);
13059
13157
  }
13060
13158
 
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) {
13159
+ 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) {
13160
+ entry.baseImage = nil;
13161
+ if (icon_path && icon_path_len > 0) {
13162
+ NSString *path = [[NSString alloc] initWithBytes:icon_path length:icon_path_len encoding:NSUTF8StringEncoding];
13163
+ NSImage *image = [[NSImage alloc] initWithContentsOfFile:NativeSdkResolvedAssetFilePath(path)];
13164
+ if (image) {
13165
+ image.template = YES;
13166
+ image.size = NSMakeSize(18, 18);
13167
+ entry.baseImage = image;
13168
+ }
13169
+ }
13170
+ entry.item.button.toolTip = tooltip && tooltip_len > 0 ? ([[NSString alloc] initWithBytes:tooltip length:tooltip_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13171
+ entry.activationCommand = activation_command ? ([[NSString alloc] initWithBytes:activation_command length:activation_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13172
+ entry.alternateActivationCommand = alternate_activation_command ? ([[NSString alloc] initWithBytes:alternate_activation_command length:alternate_activation_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13173
+ entry.openCommand = open_command ? ([[NSString alloc] initWithBytes:open_command length:open_command_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13174
+ NSStatusBarButton *button = entry.item.button;
13175
+ button.tag = (NSInteger)entry.identifier;
13176
+ button.target = object;
13177
+ button.action = (entry.activationCommand.length > 0 || entry.alternateActivationCommand.length > 0) ? @selector(statusItemActivated:) : NULL;
13178
+ if (button.action) [button sendActionOn:NSEventMaskLeftMouseUp];
13179
+ entry.item.visible = visible != 0;
13180
+ }
13181
+
13182
+ 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
13183
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13063
13184
  @autoreleasepool {
13064
- if (object.statusItem) {
13065
- [[NSStatusBar systemStatusBar] removeStatusItem:object.statusItem];
13185
+ NSNumber *key = @(status_item_id);
13186
+ NativeSdkStatusItemEntry *entry = object.statusItems[key];
13187
+ if (entry) {
13188
+ [[NSStatusBar systemStatusBar] removeStatusItem:entry.item];
13189
+ } else {
13190
+ entry = [[NativeSdkStatusItemEntry alloc] init];
13191
+ entry.identifier = status_item_id;
13192
+ object.statusItems[key] = entry;
13066
13193
  }
13067
- object.statusMenu = nil;
13194
+ entry.menu = nil;
13068
13195
  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
- }
13196
+ entry.item = [[NSStatusBar systemStatusBar] statusItemWithLength:hasTitle ? NSVariableStatusItemLength : NSSquareStatusItemLength];
13081
13197
  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);
13198
+ 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);
13199
+ NativeSdkApplyTrayPresentation(object, entry, titleString, width, tone, icon_opacity, monospaced != 0);
13200
+ }
13201
+ }
13202
+
13203
+ 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) {
13204
+ NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13205
+ @autoreleasepool {
13206
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13207
+ if (!entry) return;
13208
+ 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);
13209
+ NativeSdkApplyTrayPresentation(object, entry, entry.presentationTitle, entry.presentationWidth, entry.presentationTone, entry.presentationIconOpacity, entry.presentationMonospaced);
13210
+ if (entry.activationCommand.length == 0 && entry.alternateActivationCommand.length == 0) entry.item.menu = entry.menu;
13211
+ else entry.item.menu = nil;
13094
13212
  }
13095
13213
  }
13096
13214
 
@@ -13183,7 +13301,7 @@ static NSView *NativeSdkTrayHeroView(NSString *headline, NSString *quota) {
13183
13301
  return view;
13184
13302
  }
13185
13303
 
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) {
13304
+ 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
13305
  NSStackView *row = [[NSStackView alloc] initWithFrame:NSMakeRect(0, 0, 320, 32)];
13188
13306
  row.orientation = NSUserInterfaceLayoutOrientationHorizontal;
13189
13307
  row.alignment = NSLayoutAttributeCenterY;
@@ -13209,7 +13327,7 @@ static NSView *NativeSdkTrayAgentsView(NativeSdkAppKitHost *object, const uint32
13209
13327
  label.translatesAutoresizingMaskIntoConstraints = NO;
13210
13328
  label.accessibilityElement = NO;
13211
13329
  NSButton *button = [NSButton buttonWithTitle:@"" target:object action:@selector(trayMenuItemClicked:)];
13212
- button.tag = itemIds[index];
13330
+ button.tag = (NSInteger)(((uint64_t)statusItemId << 32) | itemIds[index]);
13213
13331
  button.bordered = NO;
13214
13332
  button.transparent = YES;
13215
13333
  button.enabled = enabled[index].boolValue;
@@ -13307,14 +13425,15 @@ static NSView *NativeSdkTrayReadoutView(NSString *label, NSString *value, BOOL i
13307
13425
  return row;
13308
13426
  }
13309
13427
 
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) {
13428
+ 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
13429
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13312
13430
  @autoreleasepool {
13313
- if (!object.statusItem) return;
13314
- NSMenu *menu = object.statusMenu;
13431
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13432
+ if (!entry) return;
13433
+ NSMenu *menu = entry.menu;
13315
13434
  if (menu == nil) {
13316
13435
  menu = [[NSMenu alloc] initWithTitle:@""];
13317
- object.statusMenu = menu;
13436
+ entry.menu = menu;
13318
13437
  } else {
13319
13438
  [menu removeAllItems];
13320
13439
  }
@@ -13329,7 +13448,7 @@ void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, const ui
13329
13448
  NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:label ?: @""
13330
13449
  action:@selector(trayMenuItemClicked:)
13331
13450
  keyEquivalent:@""];
13332
- item.tag = (NSInteger)item_ids[i];
13451
+ item.tag = (NSInteger)(((uint64_t)status_item_id << 32) | item_ids[i]);
13333
13452
  item.target = object;
13334
13453
  item.enabled = enabled_flags[i] != 0;
13335
13454
  if (keys[i] && key_lens[i] > 0) {
@@ -13370,7 +13489,7 @@ void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, const ui
13370
13489
  // NSMenu consume it before the button can dispatch its id.
13371
13490
  item.keyEquivalent = @"";
13372
13491
  item.keyEquivalentModifierMask = 0;
13373
- item.view = NativeSdkTrayAgentsView(object, agentIds, agentLabels, agentStates, agentEnabled, agentKeys, agentModifiers);
13492
+ item.view = NativeSdkTrayAgentsView(object, status_item_id, agentIds, agentLabels, agentStates, agentEnabled, agentKeys, agentModifiers);
13374
13493
  [menu addItem:item];
13375
13494
  continue;
13376
13495
  }
@@ -13396,44 +13515,47 @@ void native_sdk_appkit_update_tray_menu(native_sdk_appkit_host_t *host, const ui
13396
13515
  for (NSView *row in readoutRows) [row.widthAnchor constraintEqualToConstant:widest].active = YES;
13397
13516
  }
13398
13517
  menu.delegate = object;
13399
- if (object.statusActivationCommand.length == 0 && object.statusAlternateActivationCommand.length == 0) {
13400
- object.statusItem.menu = menu;
13518
+ if (entry.activationCommand.length == 0 && entry.alternateActivationCommand.length == 0) {
13519
+ entry.item.menu = menu;
13401
13520
  }
13402
13521
  }
13403
13522
  }
13404
13523
 
13405
- void native_sdk_appkit_update_tray_title(native_sdk_appkit_host_t *host, const char *title, size_t title_len) {
13524
+ 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
13525
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13407
13526
  @autoreleasepool {
13408
- if (!object.statusItem) return;
13527
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13528
+ if (!entry) return;
13409
13529
  NSString *value = title && title_len > 0 ? ([[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13410
13530
  NativeSdkApplyTrayPresentation(
13411
13531
  object,
13532
+ entry,
13412
13533
  value,
13413
- object.statusPresentationWidth,
13414
- object.statusPresentationTone,
13415
- object.statusPresentationIconOpacity,
13416
- object.statusPresentationMonospaced
13534
+ entry.presentationWidth,
13535
+ entry.presentationTone,
13536
+ entry.presentationIconOpacity,
13537
+ entry.presentationMonospaced
13417
13538
  );
13418
13539
  }
13419
13540
  }
13420
13541
 
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) {
13542
+ 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
13543
  NativeSdkAppKitHost *object = (__bridge NativeSdkAppKitHost *)host;
13423
13544
  @autoreleasepool {
13545
+ NativeSdkStatusItemEntry *entry = [object statusEntryForId:status_item_id];
13546
+ if (!entry) return;
13424
13547
  NSString *value = title ? ([[NSString alloc] initWithBytes:title length:title_len encoding:NSUTF8StringEncoding] ?: @"") : @"";
13425
- NativeSdkApplyTrayPresentation(object, value, width, tone, icon_opacity, monospaced != 0);
13548
+ NativeSdkApplyTrayPresentation(object, entry, value, width, tone, icon_opacity, monospaced != 0);
13426
13549
  }
13427
13550
  }
13428
13551
 
13429
- void native_sdk_appkit_remove_tray(native_sdk_appkit_host_t *host) {
13552
+ void native_sdk_appkit_remove_tray(native_sdk_appkit_host_t *host, uint32_t status_item_id) {
13430
13553
  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
- }
13554
+ NSNumber *key = @(status_item_id);
13555
+ NativeSdkStatusItemEntry *entry = object.statusItems[key];
13556
+ if (!entry) return;
13557
+ [[NSStatusBar systemStatusBar] removeStatusItem:entry.item];
13558
+ [object.statusItems removeObjectForKey:key];
13437
13559
  }
13438
13560
 
13439
13561
  void native_sdk_appkit_set_tray_callback(native_sdk_appkit_host_t *host, native_sdk_appkit_tray_callback_t callback, void *context) {