@native-sdk/cli 0.5.3 → 0.6.0

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 (172) hide show
  1. package/build/app.zig +91 -2
  2. package/build.zig +756 -5
  3. package/build.zig.zon +7 -0
  4. package/native-sdk.d.ts +19 -1
  5. package/package.json +9 -9
  6. package/packages/core/package-lock.json +2 -2
  7. package/packages/core/package.json +1 -1
  8. package/packages/core/rt/rt.zig +416 -9
  9. package/packages/core/sdk/core.ts +706 -5
  10. package/packages/core/sdk/events.ts +17 -8
  11. package/packages/core/src/checker.ts +305 -2
  12. package/packages/core/src/diagnostics.ts +12 -0
  13. package/packages/core/src/emitter.ts +3187 -264
  14. package/packages/core/src/typed_ast.ts +51 -4
  15. package/packages/core/src/types.ts +34 -1
  16. package/skill-data/native-ui/SKILL.md +42 -4
  17. package/skill-data/ts-core/SKILL.md +13 -3
  18. package/src/app_runner/root.zig +112 -10
  19. package/src/app_runner/ts_core_main.zig +4 -0
  20. package/src/automation/layout_fingerprint.zig +160 -0
  21. package/src/automation/protocol.zig +82 -33
  22. package/src/automation/root.zig +1 -0
  23. package/src/automation/server.zig +1 -1
  24. package/src/automation/snapshot.zig +68 -9
  25. package/src/automation/watcher.zig +1 -1
  26. package/src/embed/host.zig +5 -0
  27. package/src/embed/tests.zig +119 -1
  28. package/src/embed/ui_host.zig +13 -0
  29. package/src/platform/linux/gtk_host.c +639 -16
  30. package/src/platform/linux/gtk_host.h +25 -0
  31. package/src/platform/linux/root.zig +351 -7
  32. package/src/platform/macos/appkit_host.h +152 -8
  33. package/src/platform/macos/appkit_host.m +1816 -100
  34. package/src/platform/macos/cef_host.mm +372 -11
  35. package/src/platform/macos/root.zig +379 -11
  36. package/src/platform/null_platform.zig +696 -9
  37. package/src/platform/null_platform_tests.zig +35 -0
  38. package/src/platform/root.zig +42 -0
  39. package/src/platform/types.zig +524 -18
  40. package/src/platform/windows/root.zig +432 -9
  41. package/src/platform/windows/webview2_host.cpp +482 -45
  42. package/src/primitives/app_manifest/comptime_scene.zig +5 -0
  43. package/src/primitives/app_manifest/root.zig +1 -0
  44. package/src/primitives/app_manifest/types.zig +32 -0
  45. package/src/primitives/app_manifest/validation.zig +18 -0
  46. package/src/primitives/canvas/a11y_audit.zig +33 -11
  47. package/src/primitives/canvas/a11y_audit_tests.zig +20 -0
  48. package/src/primitives/canvas/drawing.zig +25 -0
  49. package/src/primitives/canvas/equality.zig +8 -0
  50. package/src/primitives/canvas/events.zig +103 -16
  51. package/src/primitives/canvas/frame.zig +125 -1
  52. package/src/primitives/canvas/layout_audit.zig +22 -5
  53. package/src/primitives/canvas/markdown_hostile_tests.zig +8 -4
  54. package/src/primitives/canvas/render_tests.zig +13 -10
  55. package/src/primitives/canvas/root.zig +48 -0
  56. package/src/primitives/canvas/terminal_box.zig +471 -0
  57. package/src/primitives/canvas/terminal_grid.zig +978 -0
  58. package/src/primitives/canvas/terminal_grid_tests.zig +1093 -0
  59. package/src/primitives/canvas/test_support.zig +1 -0
  60. package/src/primitives/canvas/tests.zig +2 -0
  61. package/src/primitives/canvas/text_tests.zig +39 -4
  62. package/src/primitives/canvas/tokens.zig +115 -12
  63. package/src/primitives/canvas/ui.zig +535 -12
  64. package/src/primitives/canvas/ui_markup.zig +253 -36
  65. package/src/primitives/canvas/ui_markup_binary.zig +4 -1
  66. package/src/primitives/canvas/ui_markup_compiled.zig +195 -9
  67. package/src/primitives/canvas/ui_markup_compiled_tests.zig +188 -8
  68. package/src/primitives/canvas/ui_markup_contract.zig +89 -6
  69. package/src/primitives/canvas/ui_markup_contract_tests.zig +65 -1
  70. package/src/primitives/canvas/ui_markup_huge_document_tests.zig +154 -0
  71. package/src/primitives/canvas/ui_markup_huge_model_tests.zig +1 -0
  72. package/src/primitives/canvas/ui_markup_reflect.zig +75 -24
  73. package/src/primitives/canvas/ui_markup_tests.zig +178 -9
  74. package/src/primitives/canvas/ui_markup_view.zig +220 -11
  75. package/src/primitives/canvas/ui_markup_view_tests.zig +553 -28
  76. package/src/primitives/canvas/ui_schema.zig +110 -17
  77. package/src/primitives/canvas/ui_schema_tests.zig +18 -10
  78. package/src/primitives/canvas/ui_tests.zig +141 -9
  79. package/src/primitives/canvas/widget_access.zig +28 -2
  80. package/src/primitives/canvas/widget_anchored_tests.zig +48 -0
  81. package/src/primitives/canvas/widget_invalidation.zig +81 -1
  82. package/src/primitives/canvas/widget_layout.zig +59 -11
  83. package/src/primitives/canvas/widget_layout_tests.zig +125 -4
  84. package/src/primitives/canvas/widget_render.zig +272 -18
  85. package/src/primitives/canvas/widget_render_scroll.zig +108 -27
  86. package/src/primitives/canvas/widget_routing.zig +59 -7
  87. package/src/primitives/canvas/widget_runtime.zig +23 -0
  88. package/src/primitives/canvas/widget_runtime_tests.zig +149 -0
  89. package/src/primitives/canvas/widget_semantics.zig +119 -11
  90. package/src/primitives/canvas/widget_semantics_tests.zig +409 -0
  91. package/src/primitives/canvas/widget_tree.zig +17 -0
  92. package/src/primitives/canvas/widgets.zig +132 -0
  93. package/src/root.zig +23 -0
  94. package/src/runtime/api.zig +76 -5
  95. package/src/runtime/automation_commands.zig +20 -1
  96. package/src/runtime/automation_snapshot.zig +25 -1
  97. package/src/runtime/automation_widget_dispatch.zig +73 -8
  98. package/src/runtime/bridge_payload.zig +8 -0
  99. package/src/runtime/bridge_responses.zig +5 -0
  100. package/src/runtime/builtin_bridge.zig +9 -4
  101. package/src/runtime/canvas_font_tests.zig +439 -2
  102. package/src/runtime/canvas_fonts.zig +61 -6
  103. package/src/runtime/canvas_frame.zig +62 -7
  104. package/src/runtime/canvas_frame_animation_tests.zig +5 -4
  105. package/src/runtime/canvas_frame_helpers.zig +190 -1
  106. package/src/runtime/canvas_frame_invalidation_tests.zig +531 -0
  107. package/src/runtime/canvas_frame_patch_tests.zig +161 -10
  108. package/src/runtime/canvas_frame_retained_tests.zig +5 -2
  109. package/src/runtime/canvas_image_tests.zig +195 -0
  110. package/src/runtime/canvas_images.zig +45 -12
  111. package/src/runtime/canvas_widget_accessibility_tests.zig +1 -1
  112. package/src/runtime/canvas_widget_clipboard_tests.zig +40 -0
  113. package/src/runtime/canvas_widget_context_menu.zig +269 -32
  114. package/src/runtime/canvas_widget_context_menu_tests.zig +640 -14
  115. package/src/runtime/canvas_widget_display.zig +1 -1
  116. package/src/runtime/canvas_widget_event_tests.zig +1823 -0
  117. package/src/runtime/canvas_widget_events.zig +240 -24
  118. package/src/runtime/canvas_widget_runtime.zig +213 -29
  119. package/src/runtime/canvas_widget_scroll_driver_tests.zig +170 -7
  120. package/src/runtime/canvas_widget_scroll_drivers.zig +151 -17
  121. package/src/runtime/canvas_widget_scroll_tests.zig +435 -23
  122. package/src/runtime/canvas_widget_state.zig +6 -0
  123. package/src/runtime/canvas_widget_text_tests.zig +1 -1
  124. package/src/runtime/core.zig +131 -13
  125. package/src/runtime/effects.zig +8136 -180
  126. package/src/runtime/effects_channel_tests.zig +3519 -0
  127. package/src/runtime/effects_fetch_tests.zig +89 -1
  128. package/src/runtime/effects_file_tests.zig +41 -1
  129. package/src/runtime/effects_image_tests.zig +1304 -0
  130. package/src/runtime/effects_pty_tests.zig +1362 -0
  131. package/src/runtime/effects_tests.zig +167 -9
  132. package/src/runtime/effects_video_tests.zig +3609 -0
  133. package/src/runtime/flow.zig +42 -4
  134. package/src/runtime/gpu_surface_events.zig +473 -13
  135. package/src/runtime/media_surface.zig +116 -11
  136. package/src/runtime/media_surface_tests.zig +61 -0
  137. package/src/runtime/pty.zig +1252 -0
  138. package/src/runtime/pty_windows.zig +1780 -0
  139. package/src/runtime/root.zig +36 -0
  140. package/src/runtime/runtime_event_tests.zig +20 -1
  141. package/src/runtime/session_blobs.zig +689 -0
  142. package/src/runtime/session_journal.zig +432 -62
  143. package/src/runtime/session_record.zig +43 -2
  144. package/src/runtime/session_replay.zig +556 -20
  145. package/src/runtime/session_tests.zig +2087 -1
  146. package/src/runtime/shell_layout.zig +7 -0
  147. package/src/runtime/terminal_session.zig +1569 -0
  148. package/src/runtime/terminal_session_tests.zig +450 -0
  149. package/src/runtime/terminal_vt_ghostty.zig +15 -0
  150. package/src/runtime/terminal_vt_stub.zig +13 -0
  151. package/src/runtime/testdata/fonts/NotoSansSC-Receipt.ttf +0 -0
  152. package/src/runtime/testdata/fonts/OFL.txt +93 -0
  153. package/src/runtime/tests.zig +8 -0
  154. package/src/runtime/ts_core_host.zig +1320 -45
  155. package/src/runtime/ts_core_host_tests.zig +1100 -1
  156. package/src/runtime/ts_ui_app.zig +23 -5
  157. package/src/runtime/ui_app.zig +2444 -69
  158. package/src/runtime/ui_app_tests.zig +3171 -744
  159. package/src/runtime/ui_app_virtual_variable_tests.zig +4 -2
  160. package/src/runtime/ui_app_window_tests.zig +736 -0
  161. package/src/runtime/view.zig +190 -2
  162. package/src/runtime/view_widget_scroll.zig +218 -68
  163. package/src/runtime/view_widget_text.zig +11 -2
  164. package/src/runtime/view_widget_tree.zig +111 -3
  165. package/src/runtime/window_command_bridge_tests.zig +117 -0
  166. package/src/runtime/window_storage.zig +77 -2
  167. package/src/runtime/window_view_runtime.zig +34 -2
  168. package/src/runtime/window_views.zig +62 -1
  169. package/src/terminal_session_tests_root.zig +15 -0
  170. package/src/tooling/manifest.zig +75 -0
  171. package/src/tooling/raw_manifest.zig +2 -0
  172. package/src/tooling/templates.zig +244 -8
@@ -16,6 +16,7 @@
16
16
  #include "include/cef_client.h"
17
17
  #include "include/cef_command_line.h"
18
18
  #include "include/cef_application_mac.h"
19
+ #include "include/cef_focus_handler.h"
19
20
  #include "include/cef_load_handler.h"
20
21
  #include "include/cef_process_message.h"
21
22
  #include "include/cef_values.h"
@@ -102,7 +103,7 @@ private:
102
103
  IMPLEMENT_REFCOUNTING(NativeSdkCefBridgeV8Handler);
103
104
  };
104
105
 
105
- class NativeSdkCefClient final : public CefClient, public CefLifeSpanHandler, public CefLoadHandler, public CefRequestHandler {
106
+ class NativeSdkCefClient final : public CefClient, public CefLifeSpanHandler, public CefLoadHandler, public CefRequestHandler, public CefFocusHandler {
106
107
  public:
107
108
  explicit NativeSdkCefClient(NativeSdkChromiumHost *host, uint64_t window_id) : host_(host), window_id_(window_id) {}
108
109
  NativeSdkCefClient(NativeSdkChromiumHost *host, uint64_t window_id, std::string webview_key, uint64_t webview_generation, bool bridge_enabled) : host_(host), window_id_(window_id), webview_key_(webview_key), webview_generation_(webview_generation), bridge_enabled_(bridge_enabled) {}
@@ -119,8 +120,13 @@ public:
119
120
  return this;
120
121
  }
121
122
 
123
+ CefRefPtr<CefFocusHandler> GetFocusHandler() override {
124
+ return this;
125
+ }
126
+
122
127
  void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
123
128
  void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
129
+ void OnGotFocus(CefRefPtr<CefBrowser> browser) override;
124
130
  void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) override;
125
131
  bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, bool user_gesture, bool is_redirect) override;
126
132
  bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId source_process, CefRefPtr<CefProcessMessage> message) override;
@@ -477,6 +483,13 @@ static const char *NativeSdkCefBridgeScript() {
477
483
  @property(nonatomic, assign) BOOL observesContentLayout;
478
484
  @end
479
485
 
486
+ /// NSApp's delegate (unretained by NSApp; the host keeps the strong
487
+ /// ref). One job: the Dock-icon reopen re-shows windows hidden by
488
+ /// their close_policy — mirrors the AppKit host's delegate.
489
+ @interface NativeSdkChromiumAppDelegate : NSObject <NSApplicationDelegate>
490
+ @property(nonatomic, assign) NativeSdkChromiumHost *host;
491
+ @end
492
+
480
493
  @interface NativeSdkChromiumShortcut : NSObject
481
494
  @property(nonatomic, strong) NSString *identifier;
482
495
  @property(nonatomic, strong) NSString *key;
@@ -513,6 +526,11 @@ static const char *NativeSdkCefBridgeScript() {
513
526
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *assetOrigins;
514
527
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *windowLabels;
515
528
  @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSString *> *fallbackURLs;
529
+ /// close_policy per window (0 = quit, 1 = hide) and the windows that
530
+ /// policy currently hides — mirrors the AppKit host's bookkeeping.
531
+ @property(nonatomic, strong) NSMutableDictionary<NSNumber *, NSNumber *> *windowClosePolicies;
532
+ @property(nonatomic, strong) NSMutableSet<NSNumber *> *policyHiddenWindows;
533
+ @property(nonatomic, strong) id reopenDelegate;
516
534
  @property(nonatomic, strong) NSMutableDictionary<NSString *, NSView *> *webviewViews;
517
535
  @property(nonatomic, strong) NSMutableDictionary<NSString *, NSString *> *webviewPendingURLs;
518
536
  @property(nonatomic, strong) NSMutableDictionary<NSString *, NSNumber *> *webviewPendingZooms;
@@ -535,6 +553,11 @@ static const char *NativeSdkCefBridgeScript() {
535
553
  @property(nonatomic, assign) void *context;
536
554
  @property(nonatomic, assign) void *bridgeContext;
537
555
  @property(nonatomic, assign) BOOL didShutdown;
556
+ /* A quit verb that arrived before [NSApp run] (see
557
+ * native_sdk_appkit_request_stop): parked here, drained at top level
558
+ * by runWithCallback's drainPendingPreRunStop calls once the pre-run
559
+ * dispatch that carried it has returned. */
560
+ @property(nonatomic, assign) BOOL pendingPreRunStop;
538
561
  @property(nonatomic, assign) BOOL observesApplicationActivation;
539
562
  @property(nonatomic, strong) id shortcutEventMonitor;
540
563
  @property(nonatomic, strong) NSArray<NativeSdkChromiumShortcut *> *shortcuts;
@@ -557,8 +580,13 @@ static const char *NativeSdkCefBridgeScript() {
557
580
  - (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
581
  - (void)focusWindowWithId:(uint64_t)windowId;
559
582
  - (void)closeWindowWithId:(uint64_t)windowId;
583
+ - (void)hideWindowWithId:(uint64_t)windowId;
584
+ - (void)showWindowWithId:(uint64_t)windowId;
585
+ - (void)miniaturizeWindowWithId:(uint64_t)windowId;
586
+ - (BOOL)reopenPolicyHiddenWindows;
560
587
  - (void)runWithCallback:(native_sdk_appkit_event_callback_t)callback context:(void *)context;
561
588
  - (void)stop;
589
+ - (BOOL)drainPendingPreRunStop;
562
590
  - (void)emitEvent:(native_sdk_appkit_event_t)event;
563
591
  - (void)startAppTimerWithId:(uint64_t)timerId intervalNs:(uint64_t)intervalNs repeats:(BOOL)repeats;
564
592
  - (void)cancelAppTimerWithId:(uint64_t)timerId;
@@ -573,6 +601,7 @@ static const char *NativeSdkCefBridgeScript() {
573
601
  - (void)emitWindowFrameForWindowId:(uint64_t)windowId open:(BOOL)open;
574
602
  - (void)emitFrame;
575
603
  - (void)emitShutdown;
604
+ - (void)emitViewFocusedForWindowId:(uint64_t)windowId label:(NSString *)label;
576
605
  - (void)loadSource:(NSString *)source kind:(NSInteger)kind assetRoot:(NSString *)assetRoot entry:(NSString *)entry origin:(NSString *)origin spaFallback:(BOOL)spaFallback;
577
606
  - (void)loadSource:(NSString *)source kind:(NSInteger)kind assetRoot:(NSString *)assetRoot entry:(NSString *)entry origin:(NSString *)origin spaFallback:(BOOL)spaFallback windowId:(uint64_t)windowId;
578
607
  - (void)setAllowedNavigationOrigins:(NSArray<NSString *> *)origins externalURLs:(NSArray<NSString *> *)externalURLs externalAction:(NSInteger)externalAction;
@@ -675,6 +704,18 @@ static const char *NativeSdkCefBridgeScript() {
675
704
  [self.host emitResizeForWindowId:self.windowId];
676
705
  }
677
706
 
707
+ // close_policy .hide: the user's close affordance hides the window
708
+ // instead of closing it (mirrors the AppKit host). Runtime closes
709
+ // never route through performClose: here, so they bypass this hook by
710
+ // construction.
711
+ - (BOOL)windowShouldClose:(NSWindow *)sender {
712
+ (void)sender;
713
+ NSNumber *policy = self.host.windowClosePolicies[@(self.windowId)];
714
+ if (policy.intValue != 1) return YES;
715
+ [self.host hideWindowWithId:self.windowId];
716
+ return NO;
717
+ }
718
+
678
719
  - (void)windowWillClose:(NSNotification *)notification {
679
720
  (void)notification;
680
721
  if (self.observesContentLayout) {
@@ -682,9 +723,13 @@ static const char *NativeSdkCefBridgeScript() {
682
723
  [window removeObserver:self forKeyPath:@"contentLayoutRect"];
683
724
  self.observesContentLayout = NO;
684
725
  }
726
+ // A really-closing window is closed, not hidden: leave the set
727
+ // before the emit so open=false never carries hidden=true.
728
+ [self.host.policyHiddenWindows removeObject:@(self.windowId)];
685
729
  [self.host emitWindowFrameForWindowId:self.windowId open:NO];
686
730
  [self.host closeWebViewsInWindow:self.windowId];
687
731
  NSNumber *key = @(self.windowId);
732
+ [self.host.windowClosePolicies removeObjectForKey:key];
688
733
  [self.host.windows removeObjectForKey:key];
689
734
  [self.host.browserContainers removeObjectForKey:key];
690
735
  [self.host.delegates removeObjectForKey:key];
@@ -705,6 +750,18 @@ static const char *NativeSdkCefBridgeScript() {
705
750
 
706
751
  @end
707
752
 
753
+ @implementation NativeSdkChromiumAppDelegate
754
+
755
+ // Dock reopen with no visible windows: re-show policy-hidden windows
756
+ // and answer NO (handled); otherwise keep AppKit's default behavior.
757
+ - (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag {
758
+ (void)sender;
759
+ if (flag) return YES;
760
+ return ![self.host reopenPolicyHiddenWindows];
761
+ }
762
+
763
+ @end
764
+
708
765
  @implementation NativeSdkChromiumHost
709
766
 
710
767
  - (instancetype)initWithAppName:(NSString *)appName displayName:(NSString *)displayName version:(NSString *)version aboutDescription:(NSString *)aboutDescription title:(NSString *)title width:(double)width height:(double)height {
@@ -729,6 +786,8 @@ static const char *NativeSdkCefBridgeScript() {
729
786
  self.assetOrigins = [[NSMutableDictionary alloc] init];
730
787
  self.windowLabels = [[NSMutableDictionary alloc] init];
731
788
  self.fallbackURLs = [[NSMutableDictionary alloc] init];
789
+ self.windowClosePolicies = [[NSMutableDictionary alloc] init];
790
+ self.policyHiddenWindows = [[NSMutableSet alloc] init];
732
791
  self.webviewViews = [[NSMutableDictionary alloc] init];
733
792
  self.webviewPendingURLs = [[NSMutableDictionary alloc] init];
734
793
  self.webviewPendingZooms = [[NSMutableDictionary alloc] init];
@@ -747,6 +806,7 @@ static const char *NativeSdkCefBridgeScript() {
747
806
 
748
807
  [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
808
  self.didShutdown = NO;
809
+ self.pendingPreRunStop = NO;
750
810
  self.observesApplicationActivation = NO;
751
811
  return self;
752
812
  }
@@ -918,6 +978,77 @@ static const char *NativeSdkCefBridgeScript() {
918
978
  [self emitWindowFrameForWindowId:windowId open:YES];
919
979
  }
920
980
 
981
+ // close_policy .hide, the hiding half (mirrors the AppKit host): order
982
+ // the window out, keep every host record, report the state on the
983
+ // frame channel. The windows dictionary still owns the window, so the
984
+ // count never reaches zero and no shutdown emits.
985
+ - (void)hideWindowWithId:(uint64_t)windowId {
986
+ NSWindow *window = self.windows[@(windowId)];
987
+ if (!window) return;
988
+ [self.policyHiddenWindows addObject:@(windowId)];
989
+ [window orderOut:nil];
990
+ [self emitWindowFrameForWindowId:windowId open:YES];
991
+ }
992
+
993
+ // The counterpart show verb: back to the glass, app activated.
994
+ // SYNCHRONOUS on the main thread, exactly closeWindowWithId:'s
995
+ // discipline (direct on main, dispatch_sync hop otherwise) — never a
996
+ // queued dispatch_async. A show that returns success while its block
997
+ // is still queued leaves the window in policyHiddenWindows during the
998
+ // runtime's post-success bookkeeping, so frame emits in that gap carry
999
+ // {focused:true, hidden:true}; and a show-then-close in one dispatch
1000
+ // re-orders — the queued show lands AFTER the synchronous close and
1001
+ // puts a retained (ordered-out) closed window back on the glass. The
1002
+ // window lookup lives INSIDE the block: for an off-main caller the
1003
+ // main thread may close the window before the hop lands, and the show
1004
+ // of a window that just left the table must be a no-op, not a
1005
+ // resurrection.
1006
+ - (void)showWindowWithId:(uint64_t)windowId {
1007
+ void (^showBlock)(void) = ^{
1008
+ NSWindow *window = self.windows[@(windowId)];
1009
+ if (!window) return;
1010
+ [self.policyHiddenWindows removeObject:@(windowId)];
1011
+ if (window.miniaturized) [window deminiaturize:nil];
1012
+ [window makeKeyAndOrderFront:nil];
1013
+ [NSApp activateIgnoringOtherApps:YES];
1014
+ [self emitWindowFrameForWindowId:windowId open:YES];
1015
+ };
1016
+ if ([NSThread isMainThread]) {
1017
+ showBlock();
1018
+ } else {
1019
+ dispatch_sync(dispatch_get_main_queue(), showBlock);
1020
+ }
1021
+ }
1022
+
1023
+ // The real OS minimize verb, same synchronous main-thread discipline
1024
+ // as showWindowWithId: above (and for the same reason: a queued
1025
+ // miniaturize captured past a synchronous close genies an app-closed,
1026
+ // ordered-out window into the Dock). No frame emit — minimize moves no
1027
+ // runtime-mirrored window state; the host's occlusion reporting
1028
+ // follows from the OS state itself.
1029
+ - (void)miniaturizeWindowWithId:(uint64_t)windowId {
1030
+ void (^miniaturizeBlock)(void) = ^{
1031
+ NSWindow *window = self.windows[@(windowId)];
1032
+ if (!window) return;
1033
+ [window miniaturize:nil];
1034
+ };
1035
+ if ([NSThread isMainThread]) {
1036
+ miniaturizeBlock();
1037
+ } else {
1038
+ dispatch_sync(dispatch_get_main_queue(), miniaturizeBlock);
1039
+ }
1040
+ }
1041
+
1042
+ // Dock reopen consequence: re-show every policy-hidden window.
1043
+ // Returns whether any was re-shown.
1044
+ - (BOOL)reopenPolicyHiddenWindows {
1045
+ if (self.policyHiddenWindows.count == 0) return NO;
1046
+ for (NSNumber *key in [self.policyHiddenWindows copy]) {
1047
+ [self showWindowWithId:key.unsignedLongLongValue];
1048
+ }
1049
+ return YES;
1050
+ }
1051
+
921
1052
  - (void)closeWindowWithId:(uint64_t)windowId {
922
1053
  void (^closeBlock)(void) = ^{
923
1054
  NSWindow *window = self.windows[@(windowId)];
@@ -928,6 +1059,19 @@ static const char *NativeSdkCefBridgeScript() {
928
1059
  if (self.browsers) {
929
1060
  auto it = self.browsers->find(windowId);
930
1061
  if (it != self.browsers->end() && it->second) {
1062
+ // This branch is a close EXIT that never reaches
1063
+ // windowWillClose (the window is ordered out, not
1064
+ // NSWindow-closed), so it must do the delegate's
1065
+ // policy-hidden cleanup itself: leave the set BEFORE the
1066
+ // emit — the frame encoder derives hidden from set
1067
+ // membership, and open=false must never carry
1068
+ // hidden=true — and so the Dock-reopen handler (which
1069
+ // re-shows every set member) can never resurrect a
1070
+ // window the app closed. After this, set membership
1071
+ // implies a live, policy-hidden window: only
1072
+ // hideWindowWithId adds (the user close of a live
1073
+ // window), and every close exit removes.
1074
+ [self.policyHiddenWindows removeObject:@(windowId)];
931
1075
  [window orderOut:nil];
932
1076
  [self emitWindowFrameForWindowId:windowId open:NO];
933
1077
  return;
@@ -946,6 +1090,14 @@ static const char *NativeSdkCefBridgeScript() {
946
1090
  self.callback = callback;
947
1091
  self.context = context;
948
1092
 
1093
+ // The Dock-reopen delegate (close_policy .hide's re-show path).
1094
+ if (!NSApp.delegate) {
1095
+ NativeSdkChromiumAppDelegate *reopenDelegate = [[NativeSdkChromiumAppDelegate alloc] init];
1096
+ reopenDelegate.host = self;
1097
+ self.reopenDelegate = reopenDelegate;
1098
+ NSApp.delegate = reopenDelegate;
1099
+ }
1100
+
949
1101
  [self.window makeKeyAndOrderFront:nil];
950
1102
  [NSApp activateIgnoringOtherApps:YES];
951
1103
  if (!self.shortcutEventMonitor) {
@@ -958,15 +1110,34 @@ static const char *NativeSdkCefBridgeScript() {
958
1110
  }
959
1111
 
960
1112
  [self emitEvent:(native_sdk_appkit_event_t){ .kind = NATIVE_SDK_APPKIT_EVENT_START }];
1113
+ // A VALID quit can arrive during the START dispatch (an update that
1114
+ // returns fx.quitApp() at boot). Pre-run there is no queue turn to
1115
+ // defer it to, so native_sdk_appkit_request_stop parked it — and
1116
+ // THIS is its drain point: emitShutdown + stop run at top level,
1117
+ // after the START dispatch has committed to the session recorder,
1118
+ // never nested inside it (a nested emit seals the journal before
1119
+ // the start record commits and replay refuses the recording).
1120
+ [self drainPendingPreRunStop];
961
1121
  // A failed START handler requests shutdown synchronously, before the
962
1122
  // 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.
1123
+ // here instead of stranding a live app behind a blank window. (That
1124
+ // request is the HOST side's native_sdk_appkit_stop, which keeps its
1125
+ // inline pre-run emit — distinct from the quit verb's pending flag
1126
+ // drained above.)
964
1127
  if (self.didShutdown) {
965
1128
  shutdownCefIfNeeded();
966
1129
  return;
967
1130
  }
968
1131
  [self emitResize];
969
1132
  [self emitWindowFrameForWindowId:1 open:YES];
1133
+ // The resize/window-frame emits above are pre-run dispatches too —
1134
+ // a boot command's update can return the quit verb during either.
1135
+ // Drain the parked quit here at top level before the run-loop
1136
+ // machinery arms; when it drains, the app never enters [NSApp run].
1137
+ if ([self drainPendingPreRunStop]) {
1138
+ shutdownCefIfNeeded();
1139
+ return;
1140
+ }
970
1141
  [self startApplicationActivationObservers];
971
1142
  self.timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / 60.0)
972
1143
  target:self
@@ -1006,10 +1177,38 @@ static const char *NativeSdkCefBridgeScript() {
1006
1177
  [NSApp postEvent:event atStart:NO];
1007
1178
  }
1008
1179
 
1180
+ /* Drain the quit verb a pre-run dispatch parked (see
1181
+ * native_sdk_appkit_request_stop): emitShutdown + stop at TOP LEVEL,
1182
+ * strictly after the requesting dispatch returned, so the session
1183
+ * recorder commits the requesting event before the shutdown record
1184
+ * seals the journal. Answers YES only when it drained; a request that
1185
+ * raced an already-emitted shutdown (a failed emit's inline stop) is
1186
+ * consumed without a second emit. */
1187
+ - (BOOL)drainPendingPreRunStop {
1188
+ if (!self.pendingPreRunStop) return NO;
1189
+ self.pendingPreRunStop = NO;
1190
+ if (self.didShutdown) return NO;
1191
+ [self emitShutdown];
1192
+ [self stop];
1193
+ return YES;
1194
+ }
1195
+
1009
1196
  - (void)emitEvent:(native_sdk_appkit_event_t)event {
1010
1197
  if (self.callback) self.callback(self.context, &event);
1011
1198
  }
1012
1199
 
1200
+ - (void)emitViewFocusedForWindowId:(uint64_t)windowId label:(NSString *)label {
1201
+ NSWindow *window = self.windows[@(windowId)] ?: (windowId == 1 ? self.window : nil);
1202
+ if (!window || label.length == 0) return;
1203
+ const char *labelBytes = label.UTF8String ?: "";
1204
+ [self emitEvent:(native_sdk_appkit_event_t){
1205
+ .kind = NATIVE_SDK_APPKIT_EVENT_VIEW_FOCUSED,
1206
+ .window_id = windowId,
1207
+ .view_label = labelBytes,
1208
+ .view_label_len = strlen(labelBytes),
1209
+ }];
1210
+ }
1211
+
1013
1212
  - (void)startApplicationActivationObservers {
1014
1213
  if (self.observesApplicationActivation) return;
1015
1214
  NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
@@ -1072,6 +1271,9 @@ static const char *NativeSdkCefBridgeScript() {
1072
1271
  .y = frame.origin.y,
1073
1272
  .open = open ? 1 : 0,
1074
1273
  .focused = window.isKeyWindow ? 1 : 0,
1274
+ // Host truth: any frame emit while the window sits in the
1275
+ // policy-hidden set carries the hidden flag.
1276
+ .hidden = [self.policyHiddenWindows containsObject:@(windowId)] ? 1 : 0,
1075
1277
  .label = label.UTF8String,
1076
1278
  .label_len = [label lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
1077
1279
  }];
@@ -1716,6 +1918,21 @@ static NSString *NativeSdkShortcutKeyForEvent(NSEvent *event) {
1716
1918
  case NSDeleteFunctionKey: return @"delete";
1717
1919
  case NSHomeFunctionKey: return @"home";
1718
1920
  case NSEndFunctionKey: return @"end";
1921
+ case NSPageUpFunctionKey: return @"pageup";
1922
+ case NSPageDownFunctionKey: return @"pagedown";
1923
+ case NSInsertFunctionKey: return @"insert";
1924
+ case NSF1FunctionKey: return @"f1";
1925
+ case NSF2FunctionKey: return @"f2";
1926
+ case NSF3FunctionKey: return @"f3";
1927
+ case NSF4FunctionKey: return @"f4";
1928
+ case NSF5FunctionKey: return @"f5";
1929
+ case NSF6FunctionKey: return @"f6";
1930
+ case NSF7FunctionKey: return @"f7";
1931
+ case NSF8FunctionKey: return @"f8";
1932
+ case NSF9FunctionKey: return @"f9";
1933
+ case NSF10FunctionKey: return @"f10";
1934
+ case NSF11FunctionKey: return @"f11";
1935
+ case NSF12FunctionKey: return @"f12";
1719
1936
  case 0x1b: return @"escape";
1720
1937
  case '\r': return @"enter";
1721
1938
  case '\t': return @"tab";
@@ -1806,6 +2023,18 @@ void NativeSdkCefClient::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
1806
2023
  [host_ cleanupClosedWebViewWithKey:key ?: @"" generation:webview_generation_];
1807
2024
  }
1808
2025
 
2026
+ void NativeSdkCefClient::OnGotFocus(CefRefPtr<CefBrowser> browser) {
2027
+ (void)browser;
2028
+ if (!host_) return;
2029
+ if (!webview_key_.empty()) {
2030
+ NSString *key = [[NSString alloc] initWithBytes:webview_key_.data() length:webview_key_.size() encoding:NSUTF8StringEncoding];
2031
+ if (![host_ webViewGeneration:webview_generation_ matchesKey:key ?: @""]) return;
2032
+ }
2033
+ const std::string label = WebViewLabel();
2034
+ NSString *labelString = [[NSString alloc] initWithBytes:label.data() length:label.size() encoding:NSUTF8StringEncoding];
2035
+ [host_ emitViewFocusedForWindowId:window_id_ label:labelString ?: @""];
2036
+ }
2037
+
1809
2038
  void NativeSdkCefClient::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) {
1810
2039
  (void)browser;
1811
2040
  (void)errorText;
@@ -1918,8 +2147,48 @@ void native_sdk_appkit_run(native_sdk_appkit_host_t *host, native_sdk_appkit_eve
1918
2147
 
1919
2148
  void native_sdk_appkit_stop(native_sdk_appkit_host_t *host) {
1920
2149
  NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
1921
- [object emitShutdown];
1922
- [object stop];
2150
+ /* The HOST side's shutdown request — a failed event emit asks for
2151
+ * the teardown (see RunState.emit's catch in macos/root.zig). Not
2152
+ * the quit verb's landing point: that is
2153
+ * native_sdk_appkit_request_stop below. Queued while the run loop
2154
+ * is live so the emit lands on the next turn at top level. Before
2155
+ * the run loop exists (a failed START handler requests shutdown
2156
+ * synchronously — runWithCallback's didShutdown check) the inline
2157
+ * emit stays, or the request would strand. */
2158
+ if (!NSApp.running) {
2159
+ [object emitShutdown];
2160
+ [object stop];
2161
+ return;
2162
+ }
2163
+ dispatch_async(dispatch_get_main_queue(), ^{
2164
+ [object emitShutdown];
2165
+ [object stop];
2166
+ });
2167
+ }
2168
+
2169
+ void native_sdk_appkit_request_stop(native_sdk_appkit_host_t *host) {
2170
+ NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
2171
+ /* The quit VERB's landing point (fx.quitApp), same contract as the
2172
+ * AppKit host's: the verb lands mid dispatch, and a synchronous
2173
+ * SHUTDOWN emit would nest the shutdown dispatch inside the
2174
+ * requesting command's — the session recorder seals the journal on
2175
+ * the nested commit and loses the command record (replay
2176
+ * diverges). While the run loop is live the main-queue hop defers
2177
+ * the emit to the next turn. Before [NSApp run] a quit from
2178
+ * App.start's update must NOT fall back to an inline emit (the
2179
+ * same nesting bug, just pre-run) — the request parks in
2180
+ * pendingPreRunStop and runWithCallback drains it at top level
2181
+ * once the pre-run dispatch that carried it returns. (The inline
2182
+ * pre-run emit lives only in native_sdk_appkit_stop above, for the
2183
+ * host-side failed-START request.) */
2184
+ if (!NSApp.running) {
2185
+ object.pendingPreRunStop = YES;
2186
+ return;
2187
+ }
2188
+ dispatch_async(dispatch_get_main_queue(), ^{
2189
+ [object emitShutdown];
2190
+ [object stop];
2191
+ });
1923
2192
  }
1924
2193
 
1925
2194
  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 +2403,26 @@ int native_sdk_appkit_close_window(native_sdk_appkit_host_t *host, uint64_t wind
2134
2403
 
2135
2404
  int native_sdk_appkit_minimize_window(native_sdk_appkit_host_t *host, uint64_t window_id) {
2136
2405
  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
- });
2406
+ if (!object.windows[@(window_id)]) return 0;
2407
+ [object miniaturizeWindowWithId:window_id];
2408
+ return 1;
2409
+ }
2410
+
2411
+ int native_sdk_appkit_show_window(native_sdk_appkit_host_t *host, uint64_t window_id) {
2412
+ NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
2413
+ if (!object.windows[@(window_id)]) return 0;
2414
+ // Synchronous by the time it returns 1: the method owns the
2415
+ // on-main/off-main split (closeWindowWithId:'s shape), so the
2416
+ // runtime's post-success focus bookkeeping never runs against a
2417
+ // still-queued show.
2418
+ [object showWindowWithId:window_id];
2419
+ return 1;
2420
+ }
2421
+
2422
+ int native_sdk_appkit_set_window_close_policy(native_sdk_appkit_host_t *host, uint64_t window_id, int close_policy) {
2423
+ NativeSdkChromiumHost *object = (__bridge NativeSdkChromiumHost *)host;
2424
+ if (!object.windows[@(window_id)]) return 0;
2425
+ object.windowClosePolicies[@(window_id)] = @(close_policy);
2142
2426
  return 1;
2143
2427
  }
2144
2428
 
@@ -2310,6 +2594,69 @@ int native_sdk_appkit_audio_set_volume(native_sdk_appkit_host_t *host, double vo
2310
2594
  return 0;
2311
2595
  }
2312
2596
 
2597
+ /* Video playback lives in the system-engine AppKit host (AVFoundation).
2598
+ * The Chromium host reports the feature unsupported and the Zig side
2599
+ * refuses before calling, so these exist only to satisfy the shared C
2600
+ * ABI — each answers with its honest failure code. */
2601
+ int native_sdk_appkit_video_load(native_sdk_appkit_host_t *host, const char *path, size_t path_len, uint64_t token, native_sdk_appkit_video_sink_push_t push_fn, void *push_context) {
2602
+ (void)host;
2603
+ (void)path;
2604
+ (void)path_len;
2605
+ (void)token;
2606
+ (void)push_fn;
2607
+ (void)push_context;
2608
+ return 2;
2609
+ }
2610
+
2611
+ int native_sdk_appkit_video_load_url(native_sdk_appkit_host_t *host, const char *url, size_t url_len, uint64_t token, native_sdk_appkit_video_sink_push_t push_fn, void *push_context) {
2612
+ (void)host;
2613
+ (void)url;
2614
+ (void)url_len;
2615
+ (void)token;
2616
+ (void)push_fn;
2617
+ (void)push_context;
2618
+ return 2;
2619
+ }
2620
+
2621
+ int native_sdk_appkit_video_play(native_sdk_appkit_host_t *host) {
2622
+ (void)host;
2623
+ return 0;
2624
+ }
2625
+
2626
+ int native_sdk_appkit_video_pause(native_sdk_appkit_host_t *host) {
2627
+ (void)host;
2628
+ return 0;
2629
+ }
2630
+
2631
+ int native_sdk_appkit_video_stop(native_sdk_appkit_host_t *host) {
2632
+ (void)host;
2633
+ return 0;
2634
+ }
2635
+
2636
+ int native_sdk_appkit_video_seek(native_sdk_appkit_host_t *host, uint64_t position_ms) {
2637
+ (void)host;
2638
+ (void)position_ms;
2639
+ return 0;
2640
+ }
2641
+
2642
+ int native_sdk_appkit_video_set_volume(native_sdk_appkit_host_t *host, double volume) {
2643
+ (void)host;
2644
+ (void)volume;
2645
+ return 0;
2646
+ }
2647
+
2648
+ int native_sdk_appkit_video_set_muted(native_sdk_appkit_host_t *host, int muted) {
2649
+ (void)host;
2650
+ (void)muted;
2651
+ return 0;
2652
+ }
2653
+
2654
+ int native_sdk_appkit_video_set_loop(native_sdk_appkit_host_t *host, int loop) {
2655
+ (void)host;
2656
+ (void)loop;
2657
+ return 0;
2658
+ }
2659
+
2313
2660
  void native_sdk_appkit_request_frame(native_sdk_appkit_host_t *host) {
2314
2661
  // The AppKit host pauses FRAME events when idle, so a cross-thread
2315
2662
  // frame request is how the automation arrival watcher wakes it. The
@@ -2461,8 +2808,22 @@ int native_sdk_appkit_update_widget_accessibility(native_sdk_appkit_host_t *host
2461
2808
  * registration so `Options.fonts` apps start identically under both
2462
2809
  * hosts; refusing here would fail startup for a face this host never
2463
2810
  * 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;
2811
+ int native_sdk_appkit_register_font(uint64_t font_id, const uint8_t *bytes, size_t bytes_len, uint64_t *out_token) {
2812
+ /* Token 0 is the honest report for a stateless accept: nothing was
2813
+ * installed, so there is no registration a token could own and
2814
+ * nothing a later unregister could remove. */
2815
+ if (out_token) *out_token = 0;
2816
+ if (font_id == 0 || !bytes || bytes_len == 0 || !out_token) return 0;
2817
+ return 1;
2818
+ }
2819
+
2820
+ /* Teardown twin of the stateless accept above: registration retained no
2821
+ * host state here, so Runtime.deinit's per-id unregister has nothing to
2822
+ * return — accept it (whatever token rides it) for the same
2823
+ * start-identically reason. */
2824
+ int native_sdk_appkit_unregister_font(uint64_t font_id, uint64_t token) {
2825
+ (void)token;
2826
+ if (font_id == 0) return 0;
2466
2827
  return 1;
2467
2828
  }
2468
2829