@native-sdk/cli 0.5.2 → 0.5.3

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 (82) hide show
  1. package/build.zig +41 -0
  2. package/package.json +9 -9
  3. package/packages/core/package-lock.json +2 -2
  4. package/packages/core/package.json +1 -1
  5. package/packages/core/sdk/events.ts +39 -2
  6. package/packages/core/src/checker.ts +1 -1
  7. package/packages/core/src/emitter.ts +39 -3
  8. package/packages/core/src/infer.ts +53 -7
  9. package/skill-data/automation/SKILL.md +7 -4
  10. package/skill-data/core/references/app-model-runtime.md +3 -0
  11. package/skill-data/native-ui/SKILL.md +1 -0
  12. package/src/app_runner/root.zig +25 -0
  13. package/src/automation/protocol.zig +20 -1
  14. package/src/embed/host.zig +27 -0
  15. package/src/embed/tests.zig +69 -0
  16. package/src/embed/ui_host.zig +4 -0
  17. package/src/platform/macos/appkit_host.h +11 -0
  18. package/src/platform/macos/appkit_host.m +130 -0
  19. package/src/platform/macos/root.zig +34 -0
  20. package/src/platform/null_platform_tests.zig +39 -0
  21. package/src/platform/root.zig +3 -0
  22. package/src/platform/types.zig +109 -4
  23. package/src/primitives/canvas/chart.zig +7 -7
  24. package/src/primitives/canvas/chart_tests.zig +66 -0
  25. package/src/primitives/canvas/commands.zig +48 -0
  26. package/src/primitives/canvas/events.zig +161 -0
  27. package/src/primitives/canvas/font_ttf.zig +155 -8
  28. package/src/primitives/canvas/font_ttf_tests.zig +686 -0
  29. package/src/primitives/canvas/reference.zig +67 -8
  30. package/src/primitives/canvas/render_fingerprints.zig +8 -0
  31. package/src/primitives/canvas/root.zig +25 -0
  32. package/src/primitives/canvas/ui.zig +35 -2
  33. package/src/primitives/canvas/ui_markup.zig +24 -0
  34. package/src/primitives/canvas/ui_markup_binary_tests.zig +25 -0
  35. package/src/primitives/canvas/ui_markup_compiled.zig +40 -3
  36. package/src/primitives/canvas/ui_markup_compiled_tests.zig +33 -0
  37. package/src/primitives/canvas/ui_markup_contract.zig +9 -0
  38. package/src/primitives/canvas/ui_markup_contract_tests.zig +24 -0
  39. package/src/primitives/canvas/ui_markup_tests.zig +26 -0
  40. package/src/primitives/canvas/ui_markup_view.zig +53 -4
  41. package/src/primitives/canvas/ui_markup_view_tests.zig +73 -2
  42. package/src/primitives/canvas/ui_schema.zig +16 -0
  43. package/src/primitives/canvas/ui_schema_tests.zig +10 -11
  44. package/src/primitives/canvas/ui_tests.zig +31 -0
  45. package/src/primitives/canvas/vector.zig +258 -141
  46. package/src/primitives/canvas/widget_access.zig +1 -1
  47. package/src/primitives/canvas/widget_builtin_tests.zig +129 -0
  48. package/src/primitives/canvas/widget_layout.zig +9 -4
  49. package/src/primitives/canvas/widget_render.zig +78 -40
  50. package/src/primitives/canvas/widget_render_controls.zig +15 -4
  51. package/src/primitives/canvas/widget_semantics.zig +6 -1
  52. package/src/primitives/canvas/widget_semantics_tests.zig +168 -0
  53. package/src/primitives/canvas/widget_text_input.zig +118 -10
  54. package/src/primitives/canvas/widgets.zig +16 -0
  55. package/src/root.zig +5 -0
  56. package/src/runtime/api.zig +13 -0
  57. package/src/runtime/automation_commands.zig +33 -0
  58. package/src/runtime/automation_widget_dispatch.zig +64 -0
  59. package/src/runtime/canvas_font_tests.zig +272 -3
  60. package/src/runtime/canvas_fonts.zig +60 -20
  61. package/src/runtime/canvas_frame_helpers.zig +15 -0
  62. package/src/runtime/canvas_image_tests.zig +26 -0
  63. package/src/runtime/canvas_images.zig +33 -7
  64. package/src/runtime/canvas_limits.zig +44 -8
  65. package/src/runtime/canvas_widget_clipboard_tests.zig +123 -0
  66. package/src/runtime/canvas_widget_context_menu_tests.zig +55 -0
  67. package/src/runtime/canvas_widget_events.zig +24 -4
  68. package/src/runtime/canvas_widget_runtime.zig +24 -7
  69. package/src/runtime/canvas_widget_state.zig +6 -1
  70. package/src/runtime/canvas_widget_text_tests.zig +277 -0
  71. package/src/runtime/core.zig +98 -6
  72. package/src/runtime/flow.zig +27 -0
  73. package/src/runtime/gpu_surface_events.zig +7 -0
  74. package/src/runtime/media_surface.zig +726 -0
  75. package/src/runtime/media_surface_tests.zig +1476 -0
  76. package/src/runtime/root.zig +9 -0
  77. package/src/runtime/session_journal.zig +30 -3
  78. package/src/runtime/session_tests.zig +126 -4
  79. package/src/runtime/tests.zig +1 -0
  80. package/src/runtime/ts_ui_app.zig +56 -0
  81. package/src/runtime/ui_app.zig +58 -0
  82. package/src/runtime/ui_app_tests.zig +334 -0
@@ -76,6 +76,17 @@ typedef enum {
76
76
  NATIVE_SDK_APPKIT_GPU_INPUT_IME_COMMIT_COMPOSITION = 9,
77
77
  NATIVE_SDK_APPKIT_GPU_INPUT_IME_CANCEL_COMPOSITION = 10,
78
78
  NATIVE_SDK_APPKIT_GPU_INPUT_POINTER_CANCEL = 11,
79
+ /* Trackpad pinch phases (magnifyWithEvent:). The event's `scale`
80
+ * field carries the per-event magnification DELTA on PINCH_CHANGE —
81
+ * raw NSEvent.magnification, which IS the multiplicative per-event
82
+ * delta per the browser-engine convention (see the doctrine note at
83
+ * magnifyWithEvent: in appkit_host.m); cumulative gesture scale is
84
+ * the product of (1 + delta); 0 on begin/end. The pointer anchor rides
85
+ * x/y (the event's locationInWindow — gesture events report the
86
+ * pointer location, not a midpoint between the fingers). */
87
+ NATIVE_SDK_APPKIT_GPU_INPUT_PINCH_BEGIN = 12,
88
+ NATIVE_SDK_APPKIT_GPU_INPUT_PINCH_CHANGE = 13,
89
+ NATIVE_SDK_APPKIT_GPU_INPUT_PINCH_END = 14,
79
90
  } native_sdk_appkit_gpu_input_kind_t;
80
91
 
81
92
  typedef enum {
@@ -569,6 +569,7 @@ static NSMutableDictionary *NativeSdkCredentialQuery(NSString *service, NSString
569
569
  @property(nonatomic, assign) double pendingScrollDriverOffsetY;
570
570
  @property(nonatomic, assign) uint64_t scrollDriverEventLastEmitNs;
571
571
  @property(nonatomic, assign) BOOL controlClickActive;
572
+ @property(nonatomic, assign) BOOL pinchGestureActive;
572
573
  - (void)configureWithHost:(NativeSdkAppKitHost *)host windowId:(uint64_t)windowId label:(NSString *)label;
573
574
  - (BOOL)isAvailable;
574
575
  - (void)updateDrawableSize;
@@ -613,6 +614,8 @@ static NSMutableDictionary *NativeSdkCredentialQuery(NSString *service, NSString
613
614
  - (void)emitQueuedPointerMotionInputEvent;
614
615
  - (void)queueScrollInputEvent:(NSEvent *)event deltaX:(double)deltaX deltaY:(double)deltaY;
615
616
  - (void)emitQueuedScrollInputEvent;
617
+ - (void)emitPinchInputEventWithKind:(NSInteger)kind event:(NSEvent *)event magnification:(double)magnification;
618
+ - (void)emitPinchChangeForEvent:(NSEvent *)event;
616
619
  - (void)emitInputEventWithKind:(NSInteger)kind point:(NSPoint)point timestampNs:(uint64_t)timestampNs modifiers:(uint32_t)modifiers keyText:(NSString *)keyText inputText:(NSString *)inputText button:(NSInteger)button deltaX:(double)deltaX deltaY:(double)deltaY;
617
620
  - (void)emitSyntheticKeyDownWithKey:(NSString *)key modifiers:(uint32_t)modifiers;
618
621
  - (void)updateSurfaceTrackingArea;
@@ -5530,6 +5533,80 @@ static BOOL NativeSdkCompositeBlurWriteRegion(NSDictionary *command, CGFloat sca
5530
5533
  [self queueScrollInputEvent:event deltaX:-event.scrollingDeltaX deltaY:-event.scrollingDeltaY];
5531
5534
  }
5532
5535
 
5536
+ - (void)magnifyWithEvent:(NSEvent *)event {
5537
+ // Trackpad pinch, phase-explicit. A cancelled gesture folds into
5538
+ // PINCH_END: pinch delivers incremental deltas the app applies as
5539
+ // they arrive, so there is no un-committed transient to roll back
5540
+ // the way pointer_cancel rolls back an in-flight press —
5541
+ // cancellation just means "the delta stream stops here".
5542
+ //
5543
+ // Change deltas emit uncoalesced (no per-frame queue like scroll):
5544
+ // the cumulative gesture scale is the PRODUCT of (1 + delta), and
5545
+ // summing coalesced deltas would drift from what the fingers did.
5546
+ //
5547
+ // HOW TO READ NSEvent.magnification — settled; do not "fix" again.
5548
+ // Apple's documentation contradicts itself: the NSEvent API
5549
+ // reference's prose says to ADD each event's magnification to your
5550
+ // scale factor, while Apple's own Event Handling Guide example
5551
+ // MULTIPLIES (currentSize *= 1 + event.magnification). The de facto
5552
+ // platform behavior is what the browser engines ship, and every
5553
+ // engine reads raw magnification as the MULTIPLICATIVE per-event
5554
+ // delta:
5555
+ // - WebKit, ViewGestureControllerMac.mm:
5556
+ // m_magnification += m_magnification * scaleWithResistance;
5557
+ // i.e. zoom *= 1 + event.magnification, per event.
5558
+ // - Chromium's mac gesture-event builder emits
5559
+ // pinch_update.scale = event.magnification + 1.0;
5560
+ // compounded multiplicatively downstream.
5561
+ // Ruling: raw event.magnification IS the multiplicative per-event
5562
+ // delta. This handler forwards it untransformed (per-event floor
5563
+ // aside — see emitPinchChangeForEvent:), and the app-side product
5564
+ // of (1 + scale) is the zoom users already experience for the same
5565
+ // gesture in Safari and Chrome. Do NOT reintroduce a running-sum
5566
+ // "additive normalization" here: it matches neither Apple's example
5567
+ // nor any engine — a raw +0.25, +0.20 stream must land on
5568
+ // 1.25 * 1.20 = 1.5, where a sum-normalized stream lands on 1.45.
5569
+ //
5570
+ // EVERY magnifyWithEvent: carries the magnification since the
5571
+ // previous event — the terminal (Ended/Cancelled) one included —
5572
+ // so each branch below must forward a nonzero magnification as a
5573
+ // PINCH_CHANGE or the cumulative product diverges from what the
5574
+ // OS delivered.
5575
+ const NSEventPhase phase = event.phase;
5576
+ if (phase & NSEventPhaseBegan) {
5577
+ [self emitQueuedPointerMotionInputEvent];
5578
+ self.pinchGestureActive = YES;
5579
+ [self emitPinchInputEventWithKind:NATIVE_SDK_APPKIT_GPU_INPUT_PINCH_BEGIN event:event magnification:0];
5580
+ [self emitPinchChangeForEvent:event];
5581
+ return;
5582
+ }
5583
+ if (phase & NSEventPhaseChanged) {
5584
+ if (!self.pinchGestureActive) {
5585
+ // A gesture already in flight when this view became the
5586
+ // hit target skips Began; open the stream cleanly anyway.
5587
+ self.pinchGestureActive = YES;
5588
+ [self emitPinchInputEventWithKind:NATIVE_SDK_APPKIT_GPU_INPUT_PINCH_BEGIN event:event magnification:0];
5589
+ }
5590
+ [self emitPinchChangeForEvent:event];
5591
+ return;
5592
+ }
5593
+ if (phase & (NSEventPhaseEnded | NSEventPhaseCancelled)) {
5594
+ if (!self.pinchGestureActive) return;
5595
+ self.pinchGestureActive = NO;
5596
+ // The terminal event's nonzero magnification emits as a final
5597
+ // PINCH_CHANGE before the end marker (a zero-magnification
5598
+ // terminal event emits only PINCH_END). Cancelled takes the
5599
+ // same path deliberately: our model applies deltas
5600
+ // incrementally with no rollback (see above), so the honest
5601
+ // stream reports every delta the OS measured, then says the
5602
+ // gesture is over.
5603
+ [self emitPinchChangeForEvent:event];
5604
+ [self emitPinchInputEventWithKind:NATIVE_SDK_APPKIT_GPU_INPUT_PINCH_END event:event magnification:0];
5605
+ return;
5606
+ }
5607
+ // NSEventPhaseMayBegin / stationary phases carry no gesture fact.
5608
+ }
5609
+
5533
5610
  - (void)keyDown:(NSEvent *)event {
5534
5611
  if ([self focusedTextAccessibilityElement]) {
5535
5612
  self.interpretedKeyEventEmittedInput = NO;
@@ -5710,6 +5787,59 @@ static BOOL NativeSdkCompositeBlurWriteRegion(NSDictionary *command, CGFloat sca
5710
5787
  deltaY:deltaY];
5711
5788
  }
5712
5789
 
5790
+ // --- Pinch change forwarding ---------------------------------------------
5791
+ // Raw event.magnification IS the wire's multiplicative per-event delta
5792
+ // (see the doctrine note at magnifyWithEvent:); it forwards untransformed.
5793
+ // One guard stands between the OS and the wire: a single event whose
5794
+ // magnification is at or below -1 would put a factor (1 + delta) <= 0 on
5795
+ // the wire — a zoom inverting through zero scale, which no physical pinch
5796
+ // can perform (only a driver/toolkit glitch could report it), and which
5797
+ // downstream products cannot recover from. Clamp PER EVENT to a floor
5798
+ // just above -1; the epsilon is 2^-10 (~0.001, exactly representable in
5799
+ // f32 and f64), so a glitched event's factor bottoms out at 2^-10 and
5800
+ // every emitted factor stays > 0.
5801
+ static const double NativeSdkPinchMagnificationFloor = -1.0 + 0x1p-10;
5802
+
5803
+ static double NativeSdkClampedPinchMagnification(double magnification) {
5804
+ return magnification < NativeSdkPinchMagnificationFloor ? NativeSdkPinchMagnificationFloor : magnification;
5805
+ }
5806
+
5807
+ // Forward one magnify event's raw magnification as a PINCH_CHANGE
5808
+ // (nothing to emit when the event measured no magnification).
5809
+ - (void)emitPinchChangeForEvent:(NSEvent *)event {
5810
+ const double magnification = NativeSdkClampedPinchMagnification(event.magnification);
5811
+ if (magnification == 0) return;
5812
+ [self emitPinchInputEventWithKind:NATIVE_SDK_APPKIT_GPU_INPUT_PINCH_CHANGE event:event magnification:magnification];
5813
+ }
5814
+
5815
+ // Pinch input: the shared input-emit shape (converted point, top-left
5816
+ // origin flip, host timestamp, modifier flags) with the magnification
5817
+ // delta riding the event's `scale` field — unused (zero) on every other
5818
+ // input kind, so the runtime reads it unconditionally. The x/y point is
5819
+ // the POINTER anchor: AppKit reports gesture events at locationInWindow
5820
+ // (the pointer location), never a midpoint between the fingers — NSTouch
5821
+ // positions are trackpad-normalized and have no view-space meaning, and
5822
+ // zoom-at-cursor is the anchoring apps want.
5823
+ - (void)emitPinchInputEventWithKind:(NSInteger)kind event:(NSEvent *)event magnification:(double)magnification {
5824
+ if (!self.host || self.surfaceLabel.length == 0 || !event) return;
5825
+ NSPoint point = [self convertPoint:event.locationInWindow fromView:nil];
5826
+ CGFloat y = self.bounds.size.height - point.y;
5827
+ const char *labelBytes = self.surfaceLabel.UTF8String ?: "";
5828
+ [self.host emitEvent:(native_sdk_appkit_event_t){
5829
+ .kind = NATIVE_SDK_APPKIT_EVENT_GPU_SURFACE_INPUT,
5830
+ .window_id = self.windowId,
5831
+ .timestamp_ns = NativeSdkTimestampNanoseconds(),
5832
+ .x = point.x,
5833
+ .y = y,
5834
+ .scale = magnification,
5835
+ .view_label = labelBytes,
5836
+ .view_label_len = [self.surfaceLabel lengthOfBytesUsingEncoding:NSUTF8StringEncoding],
5837
+ .shortcut_modifiers = NativeSdkModifierFlagsForEvent(event),
5838
+ .input_kind = (int)kind,
5839
+ }];
5840
+ [self requestRetainedCanvasFrame];
5841
+ }
5842
+
5713
5843
  // --- Native scroll drivers ---------------------------------------------
5714
5844
  // Each scrollable canvas region gets an invisible NSScrollView subview
5715
5845
  // sized to the region and backed by a flipped document view sized to the
@@ -906,6 +906,9 @@ fn gpuSurfaceInputEventFromAppKitEvent(event: *const AppKitEvent) platform_mod.G
906
906
  .text = event.input_text[0..event.input_text_len],
907
907
  .composition_cursor = if (event.has_composition_cursor != 0) event.composition_cursor else null,
908
908
  .modifiers = shortcutModifiersFromFlags(event.shortcut_modifiers),
909
+ // The pinch magnification delta rides the ABI event's `scale`
910
+ // field (zero on every non-pinch input emission).
911
+ .scale = @floatCast(event.scale),
909
912
  };
910
913
  }
911
914
 
@@ -1813,6 +1816,9 @@ fn gpuSurfaceInputKindFromInt(value: c_int) platform_mod.GpuSurfaceInputKind {
1813
1816
  9 => .ime_commit_composition,
1814
1817
  10 => .ime_cancel_composition,
1815
1818
  11 => .pointer_cancel,
1819
+ 12 => .pinch_begin,
1820
+ 13 => .pinch_change,
1821
+ 14 => .pinch_end,
1816
1822
  else => .pointer_move,
1817
1823
  };
1818
1824
  }
@@ -2242,6 +2248,34 @@ test "mac gpu surface input maps pointer cancel" {
2242
2248
  try std.testing.expectEqual(platform_mod.GpuSurfaceInputKind.pointer_cancel, input.kind);
2243
2249
  }
2244
2250
 
2251
+ test "mac gpu surface input maps pinch phases and carries the magnification delta" {
2252
+ const label = "timeline-canvas";
2253
+ var event = std.mem.zeroes(AppKitEvent);
2254
+ event.view_label = label.ptr;
2255
+ event.view_label_len = label.len;
2256
+ event.input_kind = 12;
2257
+ try std.testing.expectEqual(platform_mod.GpuSurfaceInputKind.pinch_begin, gpuSurfaceInputEventFromAppKitEvent(&event).kind);
2258
+
2259
+ // The magnification delta rides the ABI event's `scale` field with
2260
+ // the pointer anchor on x/y (the host's converted, top-left-origin
2261
+ // pointer location).
2262
+ event.input_kind = 13;
2263
+ event.x = 160;
2264
+ event.y = 120;
2265
+ event.scale = 0.25;
2266
+ const change = gpuSurfaceInputEventFromAppKitEvent(&event);
2267
+ try std.testing.expectEqual(platform_mod.GpuSurfaceInputKind.pinch_change, change.kind);
2268
+ try std.testing.expectEqual(@as(f32, 0.25), change.scale);
2269
+ try std.testing.expectEqual(@as(f32, 160), change.x);
2270
+ try std.testing.expectEqual(@as(f32, 120), change.y);
2271
+
2272
+ event.input_kind = 14;
2273
+ event.scale = 0;
2274
+ const end = gpuSurfaceInputEventFromAppKitEvent(&event);
2275
+ try std.testing.expectEqual(platform_mod.GpuSurfaceInputKind.pinch_end, end.kind);
2276
+ try std.testing.expectEqual(@as(f32, 0), end.scale);
2277
+ }
2278
+
2245
2279
  test "mac appearance event maps color scheme" {
2246
2280
  try std.testing.expectEqual(platform_mod.ColorScheme.light, appKitColorScheme(0));
2247
2281
  try std.testing.expectEqual(platform_mod.ColorScheme.dark, appKitColorScheme(1));
@@ -1,5 +1,6 @@
1
1
  const std = @import("std");
2
2
  const geometry = @import("geometry");
3
+ const canvas = @import("canvas");
3
4
  const types = @import("types.zig");
4
5
  const null_backend = @import("null_platform.zig");
5
6
  const default_gpu_frame_interval_ns = types.default_gpu_frame_interval_ns;
@@ -558,6 +559,44 @@ test "null platform records gpu surface image upload lifecycle" {
558
559
  try std.testing.expectError(error.UnsupportedService, services.uploadGpuSurfaceImage(.{ .id = 7, .width = 1, .height = 1, .rgba8 = &red }));
559
560
  }
560
561
 
562
+ test "gpu surface image upload bounds are honest per id namespace" {
563
+ var null_platform = NullPlatform.init(.{});
564
+ null_platform.gpu_surfaces = true;
565
+ const services = null_platform.platform().services;
566
+
567
+ // This validation is the one gate in front of EVERY host's upload
568
+ // seam (the AppKit store included — macos/root.zig forwards whatever
569
+ // passes here), so the bounds are pinned at this seam.
570
+ const media_bit = canvas.media_surface_image_id_bit;
571
+ const buffer = try std.testing.allocator.alloc(u8, types.max_gpu_surface_media_image_pixel_bytes + 4);
572
+ defer std.testing.allocator.free(buffer);
573
+ @memset(buffer, 128);
574
+
575
+ // Ordinary registered-image ids keep the registry's avatar-scale
576
+ // bound: exactly 1 MiB (512x512 RGBA8) passes, one pixel past it is
577
+ // an engine bug and refuses loudly.
578
+ try services.uploadGpuSurfaceImage(.{ .id = 7, .width = 512, .height = 512, .rgba8 = buffer[0..types.max_gpu_surface_image_pixel_bytes] });
579
+ try std.testing.expectError(error.InvalidGpuSurfaceImage, services.uploadGpuSurfaceImage(.{
580
+ .id = 7,
581
+ .width = types.max_gpu_surface_image_pixel_bytes / 4 + 1,
582
+ .height = 1,
583
+ .rgba8 = buffer[0 .. types.max_gpu_surface_image_pixel_bytes + 4],
584
+ }));
585
+
586
+ // Media-surface texture ids are video-scale: the flagship 1920x1080
587
+ // RGBA8 frame (8,294,400 bytes — 8x the ordinary bound) passes, the
588
+ // exact frame budget passes, one pixel past the budget refuses.
589
+ try services.uploadGpuSurfaceImage(.{ .id = media_bit | 5, .width = 1920, .height = 1080, .rgba8 = buffer[0 .. 1920 * 1080 * 4] });
590
+ try std.testing.expectEqual(@as(usize, 1920 * 1080 * 4), null_platform.gpu_surface_image_upload_byte_len);
591
+ try services.uploadGpuSurfaceImage(.{ .id = media_bit | 5, .width = 2048, .height = 1024, .rgba8 = buffer[0..types.max_gpu_surface_media_image_pixel_bytes] });
592
+ try std.testing.expectError(error.InvalidGpuSurfaceImage, services.uploadGpuSurfaceImage(.{
593
+ .id = media_bit | 5,
594
+ .width = types.max_gpu_surface_media_image_pixel_bytes / 4 + 1,
595
+ .height = 1,
596
+ .rgba8 = buffer,
597
+ }));
598
+ }
599
+
561
600
  test "null platform preserves shifted webview storage after close" {
562
601
  var null_platform = NullPlatform.init(.{});
563
602
  const services = null_platform.platform().services;
@@ -61,6 +61,7 @@ pub const max_gpu_surface_packet_json_bytes = types.max_gpu_surface_packet_json_
61
61
  pub const max_gpu_surface_packet_binary_bytes = types.max_gpu_surface_packet_binary_bytes;
62
62
  pub const max_gpu_present_fallback_detail_bytes = types.max_gpu_present_fallback_detail_bytes;
63
63
  pub const max_gpu_surface_image_pixel_bytes = types.max_gpu_surface_image_pixel_bytes;
64
+ pub const max_gpu_surface_media_image_pixel_bytes = types.max_gpu_surface_media_image_pixel_bytes;
64
65
  pub const max_gpu_surface_font_bytes = types.max_gpu_surface_font_bytes;
65
66
  pub const ShortcutModifiers = types.ShortcutModifiers;
66
67
  pub const Shortcut = types.Shortcut;
@@ -139,6 +140,8 @@ pub const GpuSurfaceFrameEvent = types.GpuSurfaceFrameEvent;
139
140
  pub const GpuSurfaceResizeEvent = types.GpuSurfaceResizeEvent;
140
141
  pub const GpuSurfaceInputKind = types.GpuSurfaceInputKind;
141
142
  pub const GpuSurfaceInputEvent = types.GpuSurfaceInputEvent;
143
+ pub const PinchPhase = types.PinchPhase;
144
+ pub const PinchEvent = types.PinchEvent;
142
145
  pub const max_gpu_surface_scroll_drivers = types.max_gpu_surface_scroll_drivers;
143
146
  pub const GpuSurfaceScrollDriver = types.GpuSurfaceScrollDriver;
144
147
  pub const GpuSurfaceScrollDriverEvent = types.GpuSurfaceScrollDriverEvent;
@@ -1,5 +1,6 @@
1
1
  const std = @import("std");
2
2
  const geometry = @import("geometry");
3
+ const canvas = @import("canvas");
3
4
  const security = @import("../security/root.zig");
4
5
 
5
6
  pub const default_gpu_frame_interval_ns: u64 = 16_666_667;
@@ -256,12 +257,27 @@ pub const max_gpu_surface_packet_binary_bytes: usize = 512 * 1024;
256
257
  pub const max_gpu_present_fallback_detail_bytes: usize = 32;
257
258
  /// Per-image bound for the binary gpu-surface image upload side-channel;
258
259
  /// matches the runtime registry's per-slot bound
259
- /// (`canvas_limits.max_registered_canvas_image_pixel_bytes`).
260
+ /// (`canvas_limits.max_registered_canvas_image_pixel_bytes`). Applies to
261
+ /// ORDINARY registered-image ids only: the registry refuses anything
262
+ /// larger at registration, so an over-bound upload of a registered image
263
+ /// can only be an engine bug and is refused loudly here rather than
264
+ /// copied into host allocations.
260
265
  pub const max_gpu_surface_image_pixel_bytes: usize = 1024 * 1024;
266
+ /// Per-image bound for uploads in the media-surface texture namespace
267
+ /// (ids with `canvas.media_surface_image_id_bit` set — producer-pushed
268
+ /// dynamic textures, structurally disjoint from registered-image ids):
269
+ /// video scale, not avatar scale. Matches the producer channel's
270
+ /// per-frame budget (`canvas_limits.max_media_surface_pixel_bytes`; a
271
+ /// lockstep test in media_surface_tests.zig pins the pair), so a frame
272
+ /// the producer accepted — a full 1920x1080 RGBA8 frame included — can
273
+ /// never be refused between adoption and host presentation.
274
+ pub const max_gpu_surface_media_image_pixel_bytes: usize = 8 * 1024 * 1024;
261
275
  /// Per-font bound for the gpu-surface font registration side-channel;
262
276
  /// matches the runtime registry's per-slot bound
263
- /// (`canvas_limits.max_registered_canvas_font_bytes`).
264
- pub const max_gpu_surface_font_bytes: usize = 2 * 1024 * 1024;
277
+ /// (`canvas_limits.max_registered_canvas_font_bytes`), sized so full
278
+ /// CJK faces register (Noto Sans SC's TrueType build, the largest
279
+ /// measured, is 17.8 MB).
280
+ pub const max_gpu_surface_font_bytes: usize = 24 * 1024 * 1024;
265
281
 
266
282
  pub const ShortcutModifiers = struct {
267
283
  primary: bool = false,
@@ -1550,6 +1566,16 @@ pub const GpuSurfaceInputKind = enum {
1550
1566
  ime_set_composition,
1551
1567
  ime_commit_composition,
1552
1568
  ime_cancel_composition,
1569
+ // Trackpad gestures are phase-explicit (every source models phases:
1570
+ // NSEvent.phase on macOS, GTK GestureZoom begin/scale-changed/end,
1571
+ // Windows precision-touchpad gesture messages), and the family stays
1572
+ // open for rotate/smart-zoom later. Only the macOS host emits these
1573
+ // today; Windows (precision touchpad) and Linux (GTK GestureZoom)
1574
+ // are staged follow-ups — on those platforms the kinds simply never
1575
+ // arrive.
1576
+ pinch_begin,
1577
+ pinch_change,
1578
+ pinch_end,
1553
1579
  };
1554
1580
 
1555
1581
  pub const GpuSurfaceInputEvent = struct {
@@ -1568,6 +1594,73 @@ pub const GpuSurfaceInputEvent = struct {
1568
1594
  text: []const u8 = "",
1569
1595
  composition_cursor: ?usize = null,
1570
1596
  modifiers: ShortcutModifiers = .{},
1597
+ /// Pinch magnification DELTA for this event: nonzero only on
1598
+ /// `pinch_change`, 0 on begin/end. The delta is MULTIPLICATIVE: the
1599
+ /// cumulative gesture scale is the running product of `(1 + scale)`
1600
+ /// across the gesture's change events. On macOS this is AppKit's
1601
+ /// raw per-event `NSEvent.magnification`, forwarded untransformed —
1602
+ /// raw magnification IS the multiplicative per-event delta, the
1603
+ /// convention every browser engine ships (see the doctrine note at
1604
+ /// `magnifyWithEvent:` in appkit_host.m), so the product matches
1605
+ /// the zoom the same gesture performs in Safari and Chrome. The
1606
+ /// host guarantees `1 + scale > 0` on every event (a per-event
1607
+ /// floor clamps the physically impossible). The pointer anchor rides
1608
+ /// `x`/`y` (view-local, same space as pointer events) — the pointer
1609
+ /// location during the gesture, NOT a midpoint between the fingers:
1610
+ /// hosts report gesture events at the pointer (AppKit's
1611
+ /// `locationInWindow`), and zoom-at-cursor is the anchoring apps
1612
+ /// want.
1613
+ scale: f32 = 0,
1614
+ };
1615
+
1616
+ /// Pinch gesture phase for the app-level pinch channel (`Options.on_pinch`
1617
+ /// / the TS core's `pinchMsg` export). Every source is phase-explicit
1618
+ /// (NSEvent phases, GTK GestureZoom, Windows precision-touchpad gestures);
1619
+ /// a host-cancelled gesture folds into `.end` — pinch delivers incremental
1620
+ /// deltas the app applies as they arrive, so there is no transient state
1621
+ /// to roll back the way `pointer_cancel` rolls back an in-flight press.
1622
+ /// A terminal host event (Ended or Cancelled) that still measured a
1623
+ /// nonzero magnification delivers that delta as a final `.change` before
1624
+ /// the `.end`, so the cumulative product always matches what the OS
1625
+ /// reported.
1626
+ pub const PinchPhase = enum {
1627
+ begin,
1628
+ change,
1629
+ end,
1630
+ };
1631
+
1632
+ /// The pinch channel's app-facing record, derived from the raw
1633
+ /// `gpu_surface_input` pinch events. Pinch bypasses the widget pipeline
1634
+ /// deliberately: it is a view-global gesture (timeline/canvas zoom), the
1635
+ /// `on_key`-fallback shape rather than a widget-targeted event.
1636
+ pub const PinchEvent = struct {
1637
+ /// Source identity: which window and gpu-surface view the gesture
1638
+ /// happened on (the `GpuFrame` identity shape, because `x`/`y` are
1639
+ /// view-local — a coordinate without its view is not a position).
1640
+ /// Multi-window and multi-view apps tell pinches apart by these.
1641
+ /// `label` is borrowed for the callback, like every event slice:
1642
+ /// a model that keeps it must copy.
1643
+ window_id: WindowId = 1,
1644
+ label: []const u8 = "",
1645
+ phase: PinchPhase,
1646
+ /// Magnification DELTA for this event: nonzero only on `.change`,
1647
+ /// 0 on begin/end. The delta is MULTIPLICATIVE — the cumulative
1648
+ /// gesture scale is the running product of `(1 + scale)` across the
1649
+ /// gesture's change events; apply it memorylessly
1650
+ /// (`zoom *= 1 + scale`), no gesture-start bookkeeping. On macOS
1651
+ /// this is AppKit's raw per-event `NSEvent.magnification`, which IS
1652
+ /// that multiplicative delta per the browser-engine convention, so
1653
+ /// the product matches the zoom the same gesture performs in Safari
1654
+ /// and Chrome. Every event keeps `1 + scale > 0`.
1655
+ scale: f32 = 0,
1656
+ /// The pointer anchor, view-local canvas points (the pointer-event
1657
+ /// space): where the zoom should anchor. This is the POINTER
1658
+ /// location during the gesture (AppKit reports gesture events at
1659
+ /// `locationInWindow`), not a midpoint between the fingers — raw
1660
+ /// touch positions are trackpad-normalized and never reach view
1661
+ /// space, and zoom-at-cursor is what apps want anyway.
1662
+ x: f32 = 0,
1663
+ y: f32 = 0,
1571
1664
  };
1572
1665
 
1573
1666
  /// Upper bound on native scroll drivers per gpu-surface view (one per
@@ -2579,7 +2672,19 @@ pub const PlatformServices = struct {
2579
2672
  pub fn uploadGpuSurfaceImage(self: PlatformServices, image: GpuSurfaceImagePixels) anyerror!void {
2580
2673
  const expected = image.expectedByteLen() orelse return error.InvalidGpuSurfaceImage;
2581
2674
  if (image.rgba8.len != expected) return error.InvalidGpuSurfaceImage;
2582
- if (image.rgba8.len > max_gpu_surface_image_pixel_bytes) return error.InvalidGpuSurfaceImage;
2675
+ // Two honest bounds, keyed by the id namespace: hosts copy every
2676
+ // upload into host-owned allocations (AppKit's NSData + NSImage
2677
+ // store), so each bound teaches the real cost of its id space.
2678
+ // Ordinary registered images are avatar-scale by the registry's
2679
+ // own slot bound; media-surface textures (the reserved high-bit
2680
+ // namespace) are video-scale by the producer channel's frame
2681
+ // budget — the bound the producer already enforced, so nothing a
2682
+ // producer staged can fail here.
2683
+ const bound = if ((image.id & canvas.media_surface_image_id_bit) != 0)
2684
+ max_gpu_surface_media_image_pixel_bytes
2685
+ else
2686
+ max_gpu_surface_image_pixel_bytes;
2687
+ if (image.rgba8.len > bound) return error.InvalidGpuSurfaceImage;
2583
2688
  const upload_fn = self.upload_gpu_surface_image_fn orelse return error.UnsupportedService;
2584
2689
  return upload_fn(self.context, image);
2585
2690
  }
@@ -44,13 +44,13 @@ pub const max_chart_points_per_series: usize = 256;
44
44
  /// filled series — or dozens of sparkline-sized ones — per frame.
45
45
  pub const max_chart_path_elements_per_frame: usize = 2048;
46
46
 
47
- /// Per-frame byte budget for the formatted tick/tooltip label scratch
48
- /// (threadlocal frame-lifetime storage: the emitted `drawText` commands
49
- /// slice into it until the runtime copies the display list). A y axis
50
- /// labels at most a handful of ticks and a
51
- /// hover tooltip a row per series, each `max_chart_value_label_bytes`
52
- /// long, so 2 KiB holds dozens of charts per frame; overflow fails
53
- /// loudly with `ChartLabelBytesFull`.
47
+ /// Byte budget for the display-list builder's own formatted tick/tooltip
48
+ /// label store (`Builder.allocChartLabelBytes` — builder-owned like the
49
+ /// path-element store, so the emitted `drawText` commands keep their
50
+ /// label bytes for the builder's lifetime). A y axis labels at most a
51
+ /// handful of ticks and a hover tooltip a row per series, each
52
+ /// `max_chart_value_label_bytes` long, so 2 KiB holds dozens of charts
53
+ /// per frame; overflow fails loudly with `ChartLabelBytesFull`.
54
54
  pub const max_chart_label_bytes_per_frame: usize = 2048;
55
55
 
56
56
  /// Longest formatted value label: the integer digits of f32's full
@@ -436,6 +436,72 @@ test "axis labels draw muted in reserved gutters and thin to fit" {
436
436
  try testing.expect(narrow_plot.maxY() < widget.frame.maxY());
437
437
  }
438
438
 
439
+ test "emitted tick labels survive a later emit into another builder" {
440
+ // The builder contract (the `allocPathElements` rule): a display
441
+ // list may be HELD while another builder emits, and it may
442
+ // accumulate several emit calls — either way every emitted command
443
+ // stays intact. Label bytes live in the BUILDER's own store, so a
444
+ // second chart's labels can never overwrite a held list's text the
445
+ // way a per-emit-reset scratch pool would.
446
+ var widget = Widget{
447
+ .id = 93,
448
+ .kind = WidgetKind.chart,
449
+ .frame = geometry.RectF.init(0, 0, 240, 120),
450
+ .chart = .{
451
+ .series = &.{.{ .kind = .line, .values = &[_]f32{ 0.2, 0.8 } }},
452
+ .y_min = 0,
453
+ .y_max = 1,
454
+ .grid_lines = 1,
455
+ .y_labels = true,
456
+ },
457
+ };
458
+ const tokens = DesignTokens{};
459
+ var commands: [32]CanvasCommand = undefined;
460
+ var builder = Builder.init(&commands);
461
+ try emitWidgetTree(&builder, widget, tokens);
462
+
463
+ // A second chart over a DIFFERENT domain emits different label
464
+ // bytes into another builder while the first list is held.
465
+ widget.chart.y_min = 0;
466
+ widget.chart.y_max = 4;
467
+ var other_commands: [32]CanvasCommand = undefined;
468
+ var other_builder = Builder.init(&other_commands);
469
+ try emitWidgetTree(&other_builder, widget, tokens);
470
+
471
+ // The held list still reads the FIRST chart's lattice ("0", "0.5",
472
+ // "1"), not the second's ("0", "2", "4") or a byte mix.
473
+ try expectChartTickLabels(builder.displayList(), &.{ "0", "0.5", "1" });
474
+ try expectChartTickLabels(other_builder.displayList(), &.{ "0", "2", "4" });
475
+
476
+ // Accumulating both charts into ONE builder keeps both intact too.
477
+ var accumulated_commands: [64]CanvasCommand = undefined;
478
+ var accumulated = Builder.init(&accumulated_commands);
479
+ widget.chart.y_min = 0;
480
+ widget.chart.y_max = 1;
481
+ try emitWidgetTree(&accumulated, widget, tokens);
482
+ var second = widget;
483
+ second.id = 94;
484
+ second.chart.y_min = 0;
485
+ second.chart.y_max = 4;
486
+ try emitWidgetTree(&accumulated, second, tokens);
487
+ try expectChartTickLabels(accumulated.displayList(), &.{ "0", "0.5", "1", "0", "2", "4" });
488
+ }
489
+
490
+ fn expectChartTickLabels(display_list: DisplayList, expected: []const []const u8) !void {
491
+ var index: usize = 0;
492
+ for (display_list.commands) |command| {
493
+ switch (command) {
494
+ .draw_text => |text| {
495
+ try testing.expect(index < expected.len);
496
+ try testing.expectEqualStrings(expected[index], text.text);
497
+ index += 1;
498
+ },
499
+ else => {},
500
+ }
501
+ }
502
+ try testing.expectEqual(expected.len, index);
503
+ }
504
+
439
505
  test "hover-detail chrome renders only under interaction and is deterministic" {
440
506
  const values = [_]f32{ 0.2, 0.8, 0.4, 0.6 };
441
507
  const labels = [_][]const u8{ "q1", "q2", "q3", "q4" };
@@ -370,6 +370,13 @@ fn nonNegative(value: f32) f32 {
370
370
  return @max(0, value);
371
371
  }
372
372
 
373
+ /// Byte budget for the builder-owned presented-text store
374
+ /// (`Builder.text_bytes`). Mirrors the runtime's per-view
375
+ /// `max_canvas_text_bytes_per_view` draw-text budget — a lockstep test
376
+ /// keeps the two from drifting — so the store can only overflow on a
377
+ /// frame the per-view display-list copy would refuse anyway.
378
+ pub const max_display_list_text_bytes: usize = 32768;
379
+
373
380
  pub const Builder = struct {
374
381
  commands: []CanvasCommand,
375
382
  len: usize = 0,
@@ -387,6 +394,24 @@ pub const Builder = struct {
387
394
  /// anyway.
388
395
  path_elements: [chart_model.max_chart_path_elements_per_frame]drawing_model.PathElement = undefined,
389
396
  path_element_len: usize = 0,
397
+ /// Builder-owned storage for text bytes FORMATTED at emit time
398
+ /// (chart y-tick labels and hover-detail title/value rows): the
399
+ /// `path_elements` lifetime rule applied to label text, so emitted
400
+ /// `draw_text` commands survive accumulation across emit calls and
401
+ /// other builders' emissions instead of slicing per-emit-reset
402
+ /// thread scratch. Overflow fails loudly by the chart label budget's
403
+ /// name, exactly as the scratch it replaces did.
404
+ label_bytes: [chart_model.max_chart_label_bytes_per_frame]u8 = undefined,
405
+ label_byte_len: usize = 0,
406
+ /// Builder-owned storage for single-line PRESENTED values (a
407
+ /// line-broken value painted with breaks-as-spaces): same lifetime
408
+ /// rule again. Sized to the runtime's per-view draw-text budget (a
409
+ /// lockstep test keeps the two equal), so a frame that overflows
410
+ /// this store was already over the per-view copy budget; the
411
+ /// presenting emitter falls back to the raw view-owned value and
412
+ /// its forced clip contains the fallback.
413
+ text_bytes: [max_display_list_text_bytes]u8 = undefined,
414
+ text_byte_len: usize = 0,
390
415
 
391
416
  pub fn init(commands: []CanvasCommand) Builder {
392
417
  return .{ .commands = commands };
@@ -395,6 +420,8 @@ pub const Builder = struct {
395
420
  pub fn reset(self: *Builder) void {
396
421
  self.len = 0;
397
422
  self.path_element_len = 0;
423
+ self.label_byte_len = 0;
424
+ self.text_byte_len = 0;
398
425
  }
399
426
 
400
427
  /// Reserve `count` path elements in the builder-owned store. The
@@ -408,6 +435,27 @@ pub const Builder = struct {
408
435
  return self.path_elements[start..self.path_element_len];
409
436
  }
410
437
 
438
+ /// Persist a formatted chart label into the builder-owned label
439
+ /// store (same lifetime contract as `allocPathElements`).
440
+ pub fn allocChartLabelBytes(self: *Builder, text: []const u8) error{ChartLabelBytesFull}![]const u8 {
441
+ if (self.label_byte_len + text.len > self.label_bytes.len) return error.ChartLabelBytesFull;
442
+ const start = self.label_byte_len;
443
+ self.label_byte_len += text.len;
444
+ @memcpy(self.label_bytes[start..self.label_byte_len], text);
445
+ return self.label_bytes[start..self.label_byte_len];
446
+ }
447
+
448
+ /// Persist emit-built text bytes (presented single-line values) into
449
+ /// the builder-owned text store (same lifetime contract as
450
+ /// `allocPathElements`).
451
+ pub fn allocTextBytes(self: *Builder, text: []const u8) error{DisplayListTextBytesFull}![]const u8 {
452
+ if (self.text_byte_len + text.len > self.text_bytes.len) return error.DisplayListTextBytesFull;
453
+ const start = self.text_byte_len;
454
+ self.text_byte_len += text.len;
455
+ @memcpy(self.text_bytes[start..self.text_byte_len], text);
456
+ return self.text_bytes[start..self.text_byte_len];
457
+ }
458
+
411
459
  pub fn displayList(self: *const Builder) DisplayList {
412
460
  return .{ .commands = self.commands[0..self.len] };
413
461
  }