@native-sdk/cli 0.5.3 → 0.5.4

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 (79) hide show
  1. package/build/app.zig +41 -1
  2. package/build.zig +223 -3
  3. package/native-sdk.d.ts +17 -1
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/rt/rt.zig +138 -3
  8. package/packages/core/sdk/core.ts +225 -3
  9. package/packages/core/src/emitter.ts +2586 -259
  10. package/skill-data/native-ui/SKILL.md +3 -2
  11. package/skill-data/ts-core/SKILL.md +9 -1
  12. package/src/app_runner/root.zig +81 -1
  13. package/src/app_runner/ts_core_main.zig +4 -0
  14. package/src/embed/tests.zig +119 -1
  15. package/src/platform/linux/gtk_host.c +112 -3
  16. package/src/platform/linux/root.zig +71 -0
  17. package/src/platform/macos/appkit_host.h +48 -2
  18. package/src/platform/macos/appkit_host.m +533 -34
  19. package/src/platform/macos/cef_host.mm +262 -10
  20. package/src/platform/macos/root.zig +92 -4
  21. package/src/platform/null_platform.zig +298 -5
  22. package/src/platform/root.zig +33 -0
  23. package/src/platform/types.zig +138 -4
  24. package/src/platform/windows/root.zig +134 -0
  25. package/src/platform/windows/webview2_host.cpp +152 -40
  26. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  27. package/src/primitives/app_manifest/root.zig +1 -0
  28. package/src/primitives/app_manifest/types.zig +32 -0
  29. package/src/primitives/canvas/tests.zig +1 -0
  30. package/src/primitives/canvas/ui.zig +11 -7
  31. package/src/primitives/canvas/ui_markup.zig +58 -34
  32. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  33. package/src/primitives/canvas/ui_markup_compiled.zig +23 -8
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +55 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +1 -1
  36. package/src/primitives/canvas/ui_markup_contract_tests.zig +26 -0
  37. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  38. package/src/primitives/canvas/ui_markup_tests.zig +29 -9
  39. package/src/primitives/canvas/ui_markup_view.zig +29 -11
  40. package/src/primitives/canvas/ui_markup_view_tests.zig +150 -9
  41. package/src/primitives/canvas/ui_schema.zig +13 -0
  42. package/src/primitives/canvas/ui_schema_tests.zig +5 -4
  43. package/src/root.zig +7 -0
  44. package/src/runtime/api.zig +6 -4
  45. package/src/runtime/bridge_payload.zig +7 -0
  46. package/src/runtime/bridge_responses.zig +5 -0
  47. package/src/runtime/builtin_bridge.zig +9 -4
  48. package/src/runtime/canvas_font_tests.zig +439 -2
  49. package/src/runtime/canvas_fonts.zig +61 -6
  50. package/src/runtime/canvas_image_tests.zig +195 -0
  51. package/src/runtime/canvas_images.zig +45 -12
  52. package/src/runtime/core.zig +58 -11
  53. package/src/runtime/effects.zig +1664 -43
  54. package/src/runtime/effects_fetch_tests.zig +89 -1
  55. package/src/runtime/effects_file_tests.zig +41 -1
  56. package/src/runtime/effects_image_tests.zig +1304 -0
  57. package/src/runtime/effects_tests.zig +153 -0
  58. package/src/runtime/flow.zig +17 -0
  59. package/src/runtime/root.zig +11 -0
  60. package/src/runtime/session_blobs.zig +689 -0
  61. package/src/runtime/session_journal.zig +85 -21
  62. package/src/runtime/session_record.zig +27 -2
  63. package/src/runtime/session_replay.zig +168 -16
  64. package/src/runtime/session_tests.zig +1992 -0
  65. package/src/runtime/shell_layout.zig +7 -0
  66. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  67. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  68. package/src/runtime/tests.zig +2 -0
  69. package/src/runtime/ts_core_host.zig +439 -4
  70. package/src/runtime/ts_core_host_tests.zig +41 -0
  71. package/src/runtime/ts_ui_app.zig +7 -0
  72. package/src/runtime/ui_app.zig +111 -6
  73. package/src/runtime/ui_app_window_tests.zig +468 -0
  74. package/src/runtime/window_command_bridge_tests.zig +117 -0
  75. package/src/runtime/window_storage.zig +32 -0
  76. package/src/runtime/window_views.zig +61 -1
  77. package/src/tooling/manifest.zig +75 -0
  78. package/src/tooling/raw_manifest.zig +2 -0
  79. package/src/tooling/templates.zig +219 -1
@@ -477,6 +477,13 @@ static const char *NativeSdkCefBridgeScript() {
477
477
  @property(nonatomic, assign) BOOL observesContentLayout;
478
478
  @end
479
479
 
480
+ /// NSApp's delegate (unretained by NSApp; the host keeps the strong
481
+ /// ref). One job: the Dock-icon reopen re-shows windows hidden by
482
+ /// their close_policy — mirrors the AppKit host's delegate.
483
+ @interface NativeSdkChromiumAppDelegate : NSObject <NSApplicationDelegate>
484
+ @property(nonatomic, assign) NativeSdkChromiumHost *host;
485
+ @end
486
+
480
487
  @interface NativeSdkChromiumShortcut : NSObject
481
488
  @property(nonatomic, strong) NSString *identifier;
482
489
  @property(nonatomic, strong) NSString *key;
@@ -513,6 +520,11 @@ static const char *NativeSdkCefBridgeScript() {
513
520
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *assetOrigins;
514
521
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *windowLabels;
515
522
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *fallbackURLs;
523
+ /// close_policy per window (0 = quit, 1 = hide) and the windows that
524
+ /// policy currently hides — mirrors the AppKit host's bookkeeping.
525
+ @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSNumber *> *windowClosePolicies;
526
+ @property(nonatomic, strong) NSMutableSet<NSNumber *> *policyHiddenWindows;
527
+ @property(nonatomic, strong) id reopenDelegate;
516
528
  @property(nonatomic, strong) NSMutableDictionary<NSString *, NSView *> *webviewViews;
517
529
  @property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *webviewPendingURLs;
518
530
  @property(nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *webviewPendingZooms;
@@ -535,6 +547,11 @@ static const char *NativeSdkCefBridgeScript() {
535
547
  @property(nonatomic, assign) void *context;
536
548
  @property(nonatomic, assign) void *bridgeContext;
537
549
  @property(nonatomic, assign) BOOL didShutdown;
550
+ /* A quit verb that arrived before [NSApp run] (see
551
+ * native_sdk_appkit_request_stop): parked here, drained at top level
552
+ * by runWithCallback's drainPendingPreRunStop calls once the pre-run
553
+ * dispatch that carried it has returned. */
554
+ @property(nonatomic, assign) BOOL pendingPreRunStop;
538
555
  @property(nonatomic, assign) BOOL observesApplicationActivation;
539
556
  @property(nonatomic, strong) id shortcutEventMonitor;
540
557
  @property(nonatomic, strong) NSArray<NativeSdkChromiumShortcut *> *shortcuts;
@@ -557,8 +574,13 @@ static const char *NativeSdkCefBridgeScript() {
557
574
  - (BOOL)createWindowWithId:(uint64_t)windowId title:(NSString *)title label:(NSString *)label x:(double)x y:(double)y width:(double)width height:(double)height restoreFrame:(BOOL)restoreFrame resizable:(BOOL)resizable makeMain:(BOOL)makeMain;
558
575
  - (void)focusWindowWithId:(uint64_t)windowId;
559
576
  - (void)closeWindowWithId:(uint64_t)windowId;
577
+ - (void)hideWindowWithId:(uint64_t)windowId;
578
+ - (void)showWindowWithId:(uint64_t)windowId;
579
+ - (void)miniaturizeWindowWithId:(uint64_t)windowId;
580
+ - (BOOL)reopenPolicyHiddenWindows;
560
581
  - (void)runWithCallback:(native_sdk_appkit_event_callback_t)callback context:(void *)context;
561
582
  - (void)stop;
583
+ - (BOOL)drainPendingPreRunStop;
562
584
  - (void)emitEvent:(native_sdk_appkit_event_t)event;
563
585
  - (void)startAppTimerWithId:(uint64_t)timerId intervalNs:(uint64_t)intervalNs repeats:(BOOL)repeats;
564
586
  - (void)cancelAppTimerWithId:(uint64_t)timerId;
@@ -675,6 +697,18 @@ static const char *NativeSdkCefBridgeScript() {
675
697
  [self.host emitResizeForWindowId:self.windowId];
676
698
  }
677
699
 
700
+ // close_policy .hide: the user's close affordance hides the window
701
+ // instead of closing it (mirrors the AppKit host). Runtime closes
702
+ // never route through performClose: here, so they bypass this hook by
703
+ // construction.
704
+ - (BOOL)windowShouldClose:(NSWindow *)sender {
705
+ (void)sender;
706
+ NSNumber *policy = self.host.windowClosePolicies[@(self.windowId)];
707
+ if (policy.intValue != 1) return YES;
708
+ [self.host hideWindowWithId:self.windowId];
709
+ return NO;
710
+ }
711
+
678
712
  - (void)windowWillClose:(NSNotification *)notification {
679
713
  (void)notification;
680
714
  if (self.observesContentLayout) {
@@ -682,9 +716,13 @@ static const char *NativeSdkCefBridgeScript() {
682
716
  [window removeObserver:self forKeyPath:@"contentLayoutRect"];
683
717
  self.observesContentLayout = NO;
684
718
  }
719
+ // A really-closing window is closed, not hidden: leave the set
720
+ // before the emit so open=false never carries hidden=true.
721
+ [self.host.policyHiddenWindows removeObject:@(self.windowId)];
685
722
  [self.host emitWindowFrameForWindowId:self.windowId open:NO];
686
723
  [self.host closeWebViewsInWindow:self.windowId];
687
724
  NSNumber *key = @(self.windowId);
725
+ [self.host.windowClosePolicies removeObjectForKey:key];
688
726
  [self.host.windows removeObjectForKey:key];
689
727
  [self.host.browserContainers removeObjectForKey:key];
690
728
  [self.host.delegates removeObjectForKey:key];
@@ -705,6 +743,18 @@ static const char *NativeSdkCefBridgeScript() {
705
743
 
706
744
  @end
707
745
 
746
+ @implementation NativeSdkChromiumAppDelegate
747
+
748
+ // Dock reopen with no visible windows: re-show policy-hidden windows
749
+ // and answer NO (handled); otherwise keep AppKit's default behavior.
750
+ - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag {
751
+ (void)sender;
752
+ if (flag) return YES;
753
+ return ![self.host reopenPolicyHiddenWindows];
754
+ }
755
+
756
+ @end
757
+
708
758
  @implementation NativeSdkChromiumHost
709
759
 
710
760
  - (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription title:(NSString *)title width:(double)width height:(double)height {
@@ -729,6 +779,8 @@ static const char *NativeSdkCefBridgeScript() {
729
779
  self.assetOrigins = [[NSMutableDictionary alloc] init];
730
780
  self.windowLabels = [[NSMutableDictionary alloc] init];
731
781
  self.fallbackURLs = [[NSMutableDictionary alloc] init];
782
+ self.windowClosePolicies = [[NSMutableDictionary alloc] init];
783
+ self.policyHiddenWindows = [[NSMutableSet alloc] init];
732
784
  self.webviewViews = [[NSMutableDictionary alloc] init];
733
785
  self.webviewPendingURLs = [[NSMutableDictionary alloc] init];
734
786
  self.webviewPendingZooms = [[NSMutableDictionary alloc] init];
@@ -747,6 +799,7 @@ static const char *NativeSdkCefBridgeScript() {
747
799
 
748
800
  [self createWindowWithId:1 title:(title.length > 0 ? title : self.appName) label:@"main" x:0 y:0 width:width height:height restoreFrame:NO resizable:YES makeMain:YES];
749
801
  self.didShutdown = NO;
802
+ self.pendingPreRunStop = NO;
750
803
  self.observesApplicationActivation = NO;
751
804
  return self;
752
805
  }
@@ -918,6 +971,77 @@ static const char *NativeSdkCefBridgeScript() {
918
971
  [self emitWindowFrameForWindowId:windowId open:YES];
919
972
  }
920
973
 
974
+ // close_policy .hide, the hiding half (mirrors the AppKit host): order
975
+ // the window out, keep every host record, report the state on the
976
+ // frame channel. The windows dictionary still owns the window, so the
977
+ // count never reaches zero and no shutdown emits.
978
+ - (void)hideWindowWithId:(uint64_t)windowId {
979
+ NSWindow *window = self.windows[@(windowId)];
980
+ if (!window) return;
981
+ [self.policyHiddenWindows addObject:@(windowId)];
982
+ [window orderOut:nil];
983
+ [self emitWindowFrameForWindowId:windowId open:YES];
984
+ }
985
+
986
+ // The counterpart show verb: back to the glass, app activated.
987
+ // SYNCHRONOUS on the main thread, exactly closeWindowWithId:'s
988
+ // discipline (direct on main, dispatch_sync hop otherwise) — never a
989
+ // queued dispatch_async. A show that returns success while its block
990
+ // is still queued leaves the window in policyHiddenWindows during the
991
+ // runtime's post-success bookkeeping, so frame emits in that gap carry
992
+ // {focused:true, hidden:true}; and a show-then-close in one dispatch
993
+ // re-orders — the queued show lands AFTER the synchronous close and
994
+ // puts a retained (ordered-out) closed window back on the glass. The
995
+ // window lookup lives INSIDE the block: for an off-main caller the
996
+ // main thread may close the window before the hop lands, and the show
997
+ // of a window that just left the table must be a no-op, not a
998
+ // resurrection.
999
+ - (void)showWindowWithId:(uint64_t)windowId {
1000
+ void (^showBlock)(void) = ^{
1001
+ NSWindow *window = self.windows[@(windowId)];
1002
+ if (!window) return;
1003
+ [self.policyHiddenWindows removeObject:@(windowId)];
1004
+ if (window.miniaturized) [window deminiaturize:nil];
1005
+ [window makeKeyAndOrderFront:nil];
1006
+ [NSApp activateIgnoringOtherApps:YES];
1007
+ [self emitWindowFrameForWindowId:windowId open:YES];
1008
+ };
1009
+ if ([NSThread isMainThread]) {
1010
+ showBlock();
1011
+ } else {
1012
+ dispatch_sync(dispatch_get_main_queue(), showBlock);
1013
+ }
1014
+ }
1015
+
1016
+ // The real OS minimize verb, same synchronous main-thread discipline
1017
+ // as showWindowWithId: above (and for the same reason: a queued
1018
+ // miniaturize captured past a synchronous close genies an app-closed,
1019
+ // ordered-out window into the Dock). No frame emit — minimize moves no
1020
+ // runtime-mirrored window state; the host's occlusion reporting
1021
+ // follows from the OS state itself.
1022
+ - (void)miniaturizeWindowWithId:(uint64_t)windowId {
1023
+ void (^miniaturizeBlock)(void) = ^{
1024
+ NSWindow *window = self.windows[@(windowId)];
1025
+ if (!window) return;
1026
+ [window miniaturize:nil];
1027
+ };
1028
+ if ([NSThread isMainThread]) {
1029
+ miniaturizeBlock();
1030
+ } else {
1031
+ dispatch_sync(dispatch_get_main_queue(), miniaturizeBlock);
1032
+ }
1033
+ }
1034
+
1035
+ // Dock reopen consequence: re-show every policy-hidden window.
1036
+ // Returns whether any was re-shown.
1037
+ - (BOOL)reopenPolicyHiddenWindows {
1038
+ if (self.policyHiddenWindows.count == 0) return NO;
1039
+ for (NSNumber *key in [self.policyHiddenWindows copy]) {
1040
+ [self showWindowWithId:key.unsignedLongLongValue];
1041
+ }
1042
+ return YES;
1043
+ }
1044
+
921
1045
  - (void)closeWindowWithId:(uint64_t)windowId {
922
1046
  void (^closeBlock)(void) = ^{
923
1047
  NSWindow *window = self.windows[@(windowId)];
@@ -928,6 +1052,19 @@ static const char *NativeSdkCefBridgeScript() {
928
1052
  if (self.browsers) {
929
1053
  auto it = self.browsers->find(windowId);
930
1054
  if (it != self.browsers->end() && it->second) {
1055
+ // This branch is a close EXIT that never reaches
1056
+ // windowWillClose (the window is ordered out, not
1057
+ // NSWindow-closed), so it must do the delegate's
1058
+ // policy-hidden cleanup itself: leave the set BEFORE the
1059
+ // emit — the frame encoder derives hidden from set
1060
+ // membership, and open=false must never carry
1061
+ // hidden=true — and so the Dock-reopen handler (which
1062
+ // re-shows every set member) can never resurrect a
1063
+ // window the app closed. After this, set membership
1064
+ // implies a live, policy-hidden window: only
1065
+ // hideWindowWithId adds (the user close of a live
1066
+ // window), and every close exit removes.
1067
+ [self.policyHiddenWindows removeObject:@(windowId)];
931
1068
  [window orderOut:nil];
932
1069
  [self emitWindowFrameForWindowId:windowId open:NO];
933
1070
  return;
@@ -946,6 +1083,14 @@ static const char *NativeSdkCefBridgeScript() {
946
1083
  self.callback = callback;
947
1084
  self.context = context;
948
1085
 
1086
+ // The Dock-reopen delegate (close_policy .hide's re-show path).
1087
+ if (!NSApp.delegate) {
1088
+ NativeSdkChromiumAppDelegate *reopenDelegate = [[NativeSdkChromiumAppDelegate alloc] init];
1089
+ reopenDelegate.host = self;
1090
+ self.reopenDelegate = reopenDelegate;
1091
+ NSApp.delegate = reopenDelegate;
1092
+ }
1093
+
949
1094
  [self.window makeKeyAndOrderFront:nil];
950
1095
  [NSApp activateIgnoringOtherApps:YES];
951
1096
  if (!self.shortcutEventMonitor) {
@@ -958,15 +1103,34 @@ static const char *NativeSdkCefBridgeScript() {
958
1103
  }
959
1104
 
960
1105
  [self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_START }];
1106
+ // A VALID quit can arrive during the START dispatch (an update that
1107
+ // returns fx.quitApp() at boot). Pre-run there is no queue turn to
1108
+ // defer it to, so native_sdk_appkit_request_stop parked it — and
1109
+ // THIS is its drain point: emitShutdown + stop run at top level,
1110
+ // after the START dispatch has committed to the session recorder,
1111
+ // never nested inside it (a nested emit seals the journal before
1112
+ // the start record commits and replay refuses the recording).
1113
+ [self drainPendingPreRunStop];
961
1114
  // A failed START handler requests shutdown synchronously, before the
962
1115
  // run loop exists — [NSApp stop:] is a no-op there. Honor the request
963
- // here instead of stranding a live app behind a blank window.
1116
+ // here instead of stranding a live app behind a blank window. (That
1117
+ // request is the HOST side's native_sdk_appkit_stop, which keeps its
1118
+ // inline pre-run emit — distinct from the quit verb's pending flag
1119
+ // drained above.)
964
1120
  if (self.didShutdown) {
965
1121
  shutdownCefIfNeeded();
966
1122
  return;
967
1123
  }
968
1124
  [self emitResize];
969
1125
  [self emitWindowFrameForWindowId:1 open:YES];
1126
+ // The resize/window-frame emits above are pre-run dispatches too —
1127
+ // a boot command's update can return the quit verb during either.
1128
+ // Drain the parked quit here at top level before the run-loop
1129
+ // machinery arms; when it drains, the app never enters [NSApp run].
1130
+ if ([self drainPendingPreRunStop]) {
1131
+ shutdownCefIfNeeded();
1132
+ return;
1133
+ }
970
1134
  [self startApplicationActivationObservers];
971
1135
  self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0)
972
1136
  target:self
@@ -1006,6 +1170,22 @@ static const char *NativeSdkCefBridgeScript() {
1006
1170
  [NSApp postEvent:event atStart:NO];
1007
1171
  }
1008
1172
 
1173
+ /* Drain the quit verb a pre-run dispatch parked (see
1174
+ * native_sdk_appkit_request_stop): emitShutdown + stop at TOP LEVEL,
1175
+ * strictly after the requesting dispatch returned, so the session
1176
+ * recorder commits the requesting event before the shutdown record
1177
+ * seals the journal. Answers YES only when it drained; a request that
1178
+ * raced an already-emitted shutdown (a failed emit's inline stop) is
1179
+ * consumed without a second emit. */
1180
+ - (BOOL)drainPendingPreRunStop {
1181
+ if (!self.pendingPreRunStop) return NO;
1182
+ self.pendingPreRunStop = NO;
1183
+ if (self.didShutdown) return NO;
1184
+ [self emitShutdown];
1185
+ [self stop];
1186
+ return YES;
1187
+ }
1188
+
1009
1189
  - (void)emitEvent:(native_sdk_appkit_event_t)event {
1010
1190
  if (self.callback) self.callback(self.context, &event);
1011
1191
  }
@@ -1072,6 +1252,9 @@ static const char *NativeSdkCefBridgeScript() {
1072
1252
  .y = frame.origin.y,
1073
1253
  .open = open ? 1 : 0,
1074
1254
  .focused = window.isKeyWindow ? 1 : 0,
1255
+ // Host truth: any frame emit while the window sits in the
1256
+ // policy-hidden set carries the hidden flag.
1257
+ .hidden = [self.policyHiddenWindows containsObject:@(windowId)] ? 1 : 0,
1075
1258
  .label = label.UTF8String,
1076
1259
  .label_len = [label lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
1077
1260
  }];
@@ -1918,8 +2101,48 @@ void native_sdk_appkit_run(native_sdk_appkit_host_t *host, native_sdk_appkit_eve
1918
2101
 
1919
2102
  void native_sdk_appkit_stop(native_sdk_appkit_host_t *host) {
1920
2103
  NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
1921
- [object emitShutdown];
1922
- [object stop];
2104
+ /* The HOST side's shutdown request — a failed event emit asks for
2105
+ * the teardown (see RunState.emit's catch in macos/root.zig). Not
2106
+ * the quit verb's landing point: that is
2107
+ * native_sdk_appkit_request_stop below. Queued while the run loop
2108
+ * is live so the emit lands on the next turn at top level. Before
2109
+ * the run loop exists (a failed START handler requests shutdown
2110
+ * synchronously — runWithCallback's didShutdown check) the inline
2111
+ * emit stays, or the request would strand. */
2112
+ if (!NSApp.running) {
2113
+ [object emitShutdown];
2114
+ [object stop];
2115
+ return;
2116
+ }
2117
+ dispatch_async(dispatch_get_main_queue(), ^{
2118
+ [object emitShutdown];
2119
+ [object stop];
2120
+ });
2121
+ }
2122
+
2123
+ void native_sdk_appkit_request_stop(native_sdk_appkit_host_t *host) {
2124
+ NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
2125
+ /* The quit VERB's landing point (fx.quitApp), same contract as the
2126
+ * AppKit host's: the verb lands mid dispatch, and a synchronous
2127
+ * SHUTDOWN emit would nest the shutdown dispatch inside the
2128
+ * requesting command's — the session recorder seals the journal on
2129
+ * the nested commit and loses the command record (replay
2130
+ * diverges). While the run loop is live the main-queue hop defers
2131
+ * the emit to the next turn. Before [NSApp run] a quit from
2132
+ * App.start's update must NOT fall back to an inline emit (the
2133
+ * same nesting bug, just pre-run) — the request parks in
2134
+ * pendingPreRunStop and runWithCallback drains it at top level
2135
+ * once the pre-run dispatch that carried it returns. (The inline
2136
+ * pre-run emit lives only in native_sdk_appkit_stop above, for the
2137
+ * host-side failed-START request.) */
2138
+ if (!NSApp.running) {
2139
+ object.pendingPreRunStop = YES;
2140
+ return;
2141
+ }
2142
+ dispatch_async(dispatch_get_main_queue(), ^{
2143
+ [object emitShutdown];
2144
+ [object stop];
2145
+ });
1923
2146
  }
1924
2147
 
1925
2148
  void native_sdk_appkit_load_webview(native_sdk_appkit_host_t *host, const char *source, size_t source_len, int source_kind, const char *asset_root, size_t asset_root_len, const char *asset_entry, size_t asset_entry_len, const char *asset_origin, size_t asset_origin_len, int spa_fallback) {
@@ -2134,11 +2357,26 @@ int native_sdk_appkit_close_window(native_sdk_appkit_host_t *host, uint64_t wind
2134
2357
 
2135
2358
  int native_sdk_appkit_minimize_window(native_sdk_appkit_host_t *host, uint64_t window_id) {
2136
2359
  NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
2137
- NSWindow *window = object.windows[@(window_id)];
2138
- if (!window) return 0;
2139
- dispatch_async(dispatch_get_main_queue(), ^{
2140
- [window miniaturize:nil];
2141
- });
2360
+ if (!object.windows[@(window_id)]) return 0;
2361
+ [object miniaturizeWindowWithId:window_id];
2362
+ return 1;
2363
+ }
2364
+
2365
+ int native_sdk_appkit_show_window(native_sdk_appkit_host_t *host, uint64_t window_id) {
2366
+ NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
2367
+ if (!object.windows[@(window_id)]) return 0;
2368
+ // Synchronous by the time it returns 1: the method owns the
2369
+ // on-main/off-main split (closeWindowWithId:'s shape), so the
2370
+ // runtime's post-success focus bookkeeping never runs against a
2371
+ // still-queued show.
2372
+ [object showWindowWithId:window_id];
2373
+ return 1;
2374
+ }
2375
+
2376
+ int native_sdk_appkit_set_window_close_policy(native_sdk_appkit_host_t *host, uint64_t window_id, int close_policy) {
2377
+ NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
2378
+ if (!object.windows[@(window_id)]) return 0;
2379
+ object.windowClosePolicies[@(window_id)] = @(close_policy);
2142
2380
  return 1;
2143
2381
  }
2144
2382
 
@@ -2461,8 +2699,22 @@ int native_sdk_appkit_update_widget_accessibility(native_sdk_appkit_host_t *host
2461
2699
  * registration so `Options.fonts` apps start identically under both
2462
2700
  * hosts; refusing here would fail startup for a face this host never
2463
2701
  * resolves. */
2464
- int native_sdk_appkit_register_font(uint64_t font_id, const uint8_t *bytes, size_t bytes_len) {
2465
- if (font_id == 0 || !bytes || bytes_len == 0) return 0;
2702
+ int native_sdk_appkit_register_font(uint64_t font_id, const uint8_t *bytes, size_t bytes_len, uint64_t *out_token) {
2703
+ /* Token 0 is the honest report for a stateless accept: nothing was
2704
+ * installed, so there is no registration a token could own and
2705
+ * nothing a later unregister could remove. */
2706
+ if (out_token) *out_token = 0;
2707
+ if (font_id == 0 || !bytes || bytes_len == 0 || !out_token) return 0;
2708
+ return 1;
2709
+ }
2710
+
2711
+ /* Teardown twin of the stateless accept above: registration retained no
2712
+ * host state here, so Runtime.deinit's per-id unregister has nothing to
2713
+ * return — accept it (whatever token rides it) for the same
2714
+ * start-identically reason. */
2715
+ int native_sdk_appkit_unregister_font(uint64_t font_id, uint64_t token) {
2716
+ (void)token;
2717
+ if (font_id == 0) return 0;
2466
2718
  return 1;
2467
2719
  }
2468
2720
 
@@ -51,6 +51,9 @@ const AppKitEvent = extern struct {
51
51
  y: f64,
52
52
  open: c_int,
53
53
  focused: c_int,
54
+ /// WINDOW_FRAME: nonzero while the window is alive but hidden by
55
+ /// its close_policy (`open` stays 1 for the whole hidden stretch).
56
+ hidden: c_int,
54
57
  label: [*]const u8,
55
58
  label_len: usize,
56
59
  shortcut_id: [*]const u8,
@@ -131,6 +134,7 @@ extern fn native_sdk_appkit_set_dock_icon_rgba(host: *AppKitHost, pixels: [*]con
131
134
  extern fn native_sdk_appkit_set_dock_icon_file(host: *AppKitHost, path: [*]const u8, path_len: usize) void;
132
135
  extern fn native_sdk_appkit_run(host: *AppKitHost, callback: AppKitCallback, context: ?*anyopaque) void;
133
136
  extern fn native_sdk_appkit_stop(host: *AppKitHost) void;
137
+ extern fn native_sdk_appkit_request_stop(host: *AppKitHost) void;
134
138
  extern fn native_sdk_appkit_load_webview(host: *AppKitHost, source: [*]const u8, source_len: usize, source_kind: c_int, asset_root: [*]const u8, asset_root_len: usize, asset_entry: [*]const u8, asset_entry_len: usize, asset_origin: [*]const u8, asset_origin_len: usize, spa_fallback: c_int) void;
135
139
  extern fn native_sdk_appkit_load_window_webview(host: *AppKitHost, window_id: u64, source: [*]const u8, source_len: usize, source_kind: c_int, asset_root: [*]const u8, asset_root_len: usize, asset_entry: [*]const u8, asset_entry_len: usize, asset_origin: [*]const u8, asset_origin_len: usize, spa_fallback: c_int) void;
136
140
  extern fn native_sdk_appkit_set_bridge_callback(host: *AppKitHost, callback: AppKitBridgeCallback, context: ?*anyopaque) void;
@@ -147,6 +151,8 @@ extern fn native_sdk_appkit_set_window_content_min_size(host: *AppKitHost, windo
147
151
  extern fn native_sdk_appkit_focus_window(host: *AppKitHost, window_id: u64) c_int;
148
152
  extern fn native_sdk_appkit_close_window(host: *AppKitHost, window_id: u64) c_int;
149
153
  extern fn native_sdk_appkit_minimize_window(host: *AppKitHost, window_id: u64) c_int;
154
+ extern fn native_sdk_appkit_show_window(host: *AppKitHost, window_id: u64) c_int;
155
+ extern fn native_sdk_appkit_set_window_close_policy(host: *AppKitHost, window_id: u64, close_policy: c_int) c_int;
150
156
  extern fn native_sdk_appkit_start_window_drag(host: *AppKitHost, window_id: u64) c_int;
151
157
  extern fn native_sdk_appkit_window_chrome_insets(host: *AppKitHost, window_id: u64, top: *f64, left: *f64, bottom: *f64, right: *f64, buttons_x: *f64, buttons_y: *f64, buttons_width: *f64, buttons_height: *f64) c_int;
152
158
  extern fn native_sdk_appkit_create_view(host: *AppKitHost, window_id: u64, label: [*]const u8, label_len: usize, kind: c_int, parent: [*]const u8, parent_len: usize, x: f64, y: f64, width: f64, height: f64, layer: c_int, visible: c_int, enabled: c_int, role: [*]const u8, role_len: usize, accessibility_label: [*]const u8, accessibility_label_len: usize, text: [*]const u8, text_len: usize, command: [*]const u8, command_len: usize) c_int;
@@ -187,7 +193,8 @@ extern fn native_sdk_appkit_close_webview(host: *AppKitHost, window_id: u64, lab
187
193
  extern fn native_sdk_appkit_clipboard_read(host: *AppKitHost, buffer: [*]u8, buffer_len: usize) usize;
188
194
  extern fn native_sdk_appkit_measure_text(font_id: u64, size: f64, text: [*]const u8, text_len: usize) f64;
189
195
  extern fn native_sdk_appkit_measure_text_advances(font_id: u64, size: f64, text: [*]const u8, text_len: usize, advances: [*]f32) c_int;
190
- extern fn native_sdk_appkit_register_font(font_id: u64, bytes: [*]const u8, bytes_len: usize) c_int;
196
+ extern fn native_sdk_appkit_register_font(font_id: u64, bytes: [*]const u8, bytes_len: usize, out_token: *u64) c_int;
197
+ extern fn native_sdk_appkit_unregister_font(font_id: u64, token: u64) c_int;
191
198
  extern fn native_sdk_appkit_register_bundled_fonts() void;
192
199
  extern fn native_sdk_appkit_decode_image(bytes: [*]const u8, bytes_len: usize, pixels: [*]u8, pixels_len: usize, out_width: *usize, out_height: *usize) c_int;
193
200
  extern fn native_sdk_appkit_clipboard_write(host: *AppKitHost, text: [*]const u8, text_len: usize) void;
@@ -545,6 +552,10 @@ pub const MacPlatform = struct {
545
552
  // (AppKit `contentMinSize`); the create call above registers
546
553
  // the window under its id, so the floor applies right after.
547
554
  applyWindowContentMinSize(host, window_options.id, window_options.min_width, window_options.min_height);
555
+ // Same timing for the declared close policy: the manifest's
556
+ // .hide threads through here so the STARTUP window's red
557
+ // button hides from the first frame on.
558
+ applyWindowClosePolicy(host, window_options.id, window_options.close_policy);
548
559
  return .{
549
560
  .host = host,
550
561
  .web_engine = web_engine,
@@ -583,6 +594,8 @@ pub const MacPlatform = struct {
583
594
  .focus_window_fn = focusWindow,
584
595
  .close_window_fn = closeWindow,
585
596
  .minimize_window_fn = minimizeWindow,
597
+ .show_window_fn = showWindow,
598
+ .quit_app_fn = quitApp,
586
599
  .start_window_drag_fn = startWindowDrag,
587
600
  .window_chrome_fn = windowChrome,
588
601
  .create_view_fn = createView,
@@ -640,6 +653,7 @@ pub const MacPlatform = struct {
640
653
  .upload_gpu_surface_image_fn = uploadGpuSurfaceImage,
641
654
  .remove_gpu_surface_image_fn = removeGpuSurfaceImage,
642
655
  .register_gpu_surface_font_fn = registerGpuSurfaceFont,
656
+ .unregister_gpu_surface_font_fn = unregisterGpuSurfaceFont,
643
657
  .update_widget_accessibility_fn = updateWidgetAccessibility,
644
658
  .measure_text_fn = measureText,
645
659
  .measure_text_advances_fn = measureTextAdvances,
@@ -665,6 +679,10 @@ pub const MacPlatform = struct {
665
679
  .recent_documents,
666
680
  .credentials,
667
681
  .app_activation_events,
682
+ // close_policy .hide: windowShouldClose orders the window
683
+ // out instead of closing, the Dock reopen re-shows it —
684
+ // both hosts (AppKit and the CEF variant) implement it.
685
+ .window_hide_on_close,
668
686
  => true,
669
687
  .context_menus => true,
670
688
  .native_views,
@@ -769,6 +787,7 @@ fn appkitCallback(context: ?*anyopaque, event: *const AppKitEvent) callconv(.c)
769
787
  .scale_factor = @floatCast(event.scale),
770
788
  .open = event.open != 0,
771
789
  .focused = event.focused != 0,
790
+ .hidden = event.hidden != 0,
772
791
  } });
773
792
  },
774
793
  .shortcut => state.emit(.{ .shortcut = .{
@@ -932,6 +951,16 @@ pub fn installHeadlessTextServices(services: *platform_mod.PlatformServices) voi
932
951
  services.measure_text_fn = measureText;
933
952
  services.measure_text_advances_fn = measureTextAdvances;
934
953
  services.register_gpu_surface_font_fn = registerGpuSurfaceFont;
954
+ services.unregister_gpu_surface_font_fn = unregisterGpuSurfaceFont;
955
+ }
956
+
957
+ /// Headless image codec for session replay: the SAME CGImageSource
958
+ /// decode a live host serves (see `decodeImage` below — a context-free
959
+ /// bytes-to-pixels call, no window, no run loop), so journaled image
960
+ /// bytes re-register the identical pixels under a headless replay on
961
+ /// the same platform.
962
+ pub fn installHeadlessImageCodec(services: *platform_mod.PlatformServices) void {
963
+ services.decode_image_fn = decodeImage;
935
964
  }
936
965
 
937
966
  fn measureText(context: ?*anyopaque, font_id: u64, size: f32, text: []const u8) f32 {
@@ -1043,6 +1072,22 @@ fn showModeInt(mode: platform_mod.WindowShowMode) c_int {
1043
1072
  };
1044
1073
  }
1045
1074
 
1075
+ fn closePolicyInt(policy: platform_mod.WindowClosePolicy) c_int {
1076
+ return switch (policy) {
1077
+ .quit => 0,
1078
+ .hide => 1,
1079
+ };
1080
+ }
1081
+
1082
+ /// Register a window's declared close policy with the host, right
1083
+ /// after create (the min-size-floor pattern: close handling is host
1084
+ /// window state fixed for the window's life). `.quit` skips the call —
1085
+ /// it IS the host default.
1086
+ fn applyWindowClosePolicy(host: *AppKitHost, window_id: u64, policy: platform_mod.WindowClosePolicy) void {
1087
+ if (policy == .quit) return;
1088
+ _ = native_sdk_appkit_set_window_close_policy(host, window_id, closePolicyInt(policy));
1089
+ }
1090
+
1046
1091
  /// Apply a declared content min-size floor to a created window
1047
1092
  /// (AppKit `contentMinSize`). Zero/negative/non-finite floors are the
1048
1093
  /// "no floor" sentinel and skip the call — the window keeps AppKit's
@@ -1060,6 +1105,7 @@ fn createWindow(context: ?*anyopaque, options: platform_mod.WindowOptions) anyer
1060
1105
  const frame = options.default_frame;
1061
1106
  if (native_sdk_appkit_create_window(self.host, options.id, title.ptr, title.len, options.label.ptr, options.label.len, frame.x, frame.y, frame.width, frame.height, if (options.restore_state) 1 else 0, if (options.resizable) 1 else 0, titlebarStyleInt(options.titlebar), showModeInt(options.show)) == 0) return error.CreateFailed;
1062
1107
  applyWindowContentMinSize(self.host, options.id, options.min_width, options.min_height);
1108
+ applyWindowClosePolicy(self.host, options.id, options.close_policy);
1063
1109
  return .{
1064
1110
  .id = options.id,
1065
1111
  .label = options.label,
@@ -1086,6 +1132,28 @@ fn minimizeWindow(context: ?*anyopaque, window_id: platform_mod.WindowId) anyerr
1086
1132
  if (native_sdk_appkit_minimize_window(self.host, window_id) == 0) return error.WindowNotFound;
1087
1133
  }
1088
1134
 
1135
+ fn showWindow(context: ?*anyopaque, window_id: platform_mod.WindowId) anyerror!void {
1136
+ const self: *MacPlatform = @ptrCast(@alignCast(context.?));
1137
+ if (native_sdk_appkit_show_window(self.host, window_id) == 0) return error.WindowNotFound;
1138
+ }
1139
+
1140
+ /// The graceful quit: the same emitShutdown + stop the last-window
1141
+ /// close runs, ALWAYS deferred past the dispatch that requested it —
1142
+ /// this verb arrives mid dispatch (the command whose update returned
1143
+ /// it is still being dispatched), and `app_shutdown` must emit only
1144
+ /// after that dispatch returns, or a recording session seals its
1145
+ /// journal before the requesting command commits (the command record
1146
+ /// is lost and replay diverges). While the run loop is live the host
1147
+ /// queues one loop turn; before [NSApp run] (a quit from App.start's
1148
+ /// update, or from a boot command during the synchronous first canvas
1149
+ /// frame) it parks the request and drains it at top level — never the
1150
+ /// inline emit `native_sdk_appkit_stop` keeps for the host-side
1151
+ /// failed-START request.
1152
+ fn quitApp(context: ?*anyopaque) anyerror!void {
1153
+ const self: *MacPlatform = @ptrCast(@alignCast(context.?));
1154
+ native_sdk_appkit_request_stop(self.host);
1155
+ }
1156
+
1089
1157
  fn startWindowDrag(context: ?*anyopaque, window_id: platform_mod.WindowId) anyerror!void {
1090
1158
  const self: *MacPlatform = @ptrCast(@alignCast(context.?));
1091
1159
  if (native_sdk_appkit_start_window_drag(self.host, window_id) == 0) return error.WindowNotFound;
@@ -1480,10 +1548,30 @@ fn removeGpuSurfaceImage(context: ?*anyopaque, id: u64) anyerror!void {
1480
1548
 
1481
1549
  /// Registered canvas fonts feed the host's single font-resolution seam
1482
1550
  /// (measurement AND packet text drawing), which exists on both web
1483
- /// engines, so this is deliberately not gated on `web_engine`.
1484
- fn registerGpuSurfaceFont(context: ?*anyopaque, font: platform_mod.GpuSurfaceFontData) anyerror!void {
1551
+ /// engines, so this is deliberately not gated on `web_engine`. Returns
1552
+ /// the registration's ownership token: the AppKit host mints one per
1553
+ /// registration (font state there is per-process, so the token is what
1554
+ /// lets teardown remove exactly this registration under a shared id);
1555
+ /// the Chromium host retains no font state and reports 0 — nothing to
1556
+ /// own, nothing for an unregister to remove.
1557
+ fn registerGpuSurfaceFont(context: ?*anyopaque, font: platform_mod.GpuSurfaceFontData) anyerror!u64 {
1558
+ _ = context;
1559
+ var token: u64 = 0;
1560
+ if (native_sdk_appkit_register_font(font.id, font.ttf.ptr, font.ttf.len, &token) == 0) return error.InvalidGpuSurfaceFont;
1561
+ return token;
1562
+ }
1563
+
1564
+ /// Teardown twin of the registration above: drop the host's per-id font
1565
+ /// state (the CoreText descriptor and its caches) when the runtime that
1566
+ /// registered the id deinits — but only while the id's current
1567
+ /// registration still carries `token`, so an older runtime's deinit
1568
+ /// never removes a face a newer runtime re-registered under the same
1569
+ /// id. Same seam as registration, so deliberately not gated on
1570
+ /// `web_engine` either; unregistering an id the host never saw, or with
1571
+ /// a stale token, is a no-op accept.
1572
+ fn unregisterGpuSurfaceFont(context: ?*anyopaque, id: u64, token: u64) anyerror!void {
1485
1573
  _ = context;
1486
- if (native_sdk_appkit_register_font(font.id, font.ttf.ptr, font.ttf.len) == 0) return error.InvalidGpuSurfaceFont;
1574
+ if (native_sdk_appkit_unregister_font(id, token) == 0) return error.InvalidGpuSurfaceFont;
1487
1575
  }
1488
1576
 
1489
1577
  fn updateWidgetAccessibility(context: ?*anyopaque, snapshot: platform_mod.WidgetAccessibilitySnapshot) anyerror!void {