@native-sdk/cli 0.6.1 → 0.6.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 (68) hide show
  1. package/build.zig +8 -0
  2. package/native-sdk.d.ts +16 -0
  3. package/package.json +9 -9
  4. package/packages/core/package-lock.json +2 -2
  5. package/packages/core/package.json +1 -1
  6. package/packages/core/sdk/text.ts +62 -21
  7. package/src/app_runner/root.zig +19 -0
  8. package/src/platform/linux/gtk_host.c +167 -8
  9. package/src/platform/linux/gtk_host.h +3 -2
  10. package/src/platform/linux/root.zig +198 -9
  11. package/src/platform/macos/appkit_host.h +4 -2
  12. package/src/platform/macos/appkit_host.m +137 -38
  13. package/src/platform/macos/cef_host.mm +46 -11
  14. package/src/platform/macos/root.zig +150 -6
  15. package/src/platform/null_platform.zig +32 -4
  16. package/src/platform/types.zig +31 -4
  17. package/src/platform/windows/root.zig +220 -10
  18. package/src/platform/windows/webview2_host.cpp +330 -31
  19. package/src/primitives/app_manifest/comptime_scene.zig +4 -0
  20. package/src/primitives/app_manifest/types.zig +16 -0
  21. package/src/primitives/canvas/equality.zig +3 -1
  22. package/src/primitives/canvas/events.zig +4 -0
  23. package/src/primitives/canvas/root.zig +11 -0
  24. package/src/primitives/canvas/text.zig +10 -0
  25. package/src/primitives/canvas/text_interaction.zig +164 -22
  26. package/src/primitives/canvas/text_layout.zig +107 -21
  27. package/src/primitives/canvas/text_tests.zig +86 -0
  28. package/src/primitives/canvas/ui.zig +21 -16
  29. package/src/primitives/canvas/ui_markup_reflect.zig +20 -3
  30. package/src/primitives/canvas/ui_markup_view_tests.zig +16 -0
  31. package/src/primitives/canvas/ui_tests.zig +6 -0
  32. package/src/primitives/canvas/widget_render.zig +21 -2
  33. package/src/primitives/canvas/widget_runtime.zig +1 -0
  34. package/src/primitives/canvas/widget_runtime_tests.zig +51 -0
  35. package/src/primitives/canvas/widget_text_input.zig +23 -11
  36. package/src/runtime/api.zig +9 -0
  37. package/src/runtime/bridge_payload.zig +9 -0
  38. package/src/runtime/bridge_responses.zig +2 -0
  39. package/src/runtime/builtin_bridge.zig +10 -0
  40. package/src/runtime/canvas_frame_helpers.zig +1 -0
  41. package/src/runtime/canvas_limits.zig +7 -0
  42. package/src/runtime/canvas_widget_accessibility_tests.zig +27 -1
  43. package/src/runtime/canvas_widget_clipboard_tests.zig +16 -0
  44. package/src/runtime/canvas_widget_display.zig +15 -1
  45. package/src/runtime/canvas_widget_events.zig +43 -19
  46. package/src/runtime/canvas_widget_floating_tests.zig +3 -3
  47. package/src/runtime/canvas_widget_runtime.zig +67 -10
  48. package/src/runtime/canvas_widget_state.zig +1 -0
  49. package/src/runtime/canvas_widget_text_tests.zig +1878 -70
  50. package/src/runtime/effects.zig +5 -6
  51. package/src/runtime/flow.zig +45 -7
  52. package/src/runtime/gpu_surface_events.zig +46 -1
  53. package/src/runtime/runtime_event_tests.zig +9 -6
  54. package/src/runtime/shell_layout.zig +25 -11
  55. package/src/runtime/shell_layout_tests.zig +184 -0
  56. package/src/runtime/shell_view_tests.zig +9 -7
  57. package/src/runtime/state.zig +6 -0
  58. package/src/runtime/ui_app.zig +61 -14
  59. package/src/runtime/ui_app_window_tests.zig +107 -2
  60. package/src/runtime/view.zig +30 -0
  61. package/src/runtime/view_widget_text.zig +1048 -10
  62. package/src/runtime/view_widget_tree.zig +2 -0
  63. package/src/runtime/window_command_bridge_tests.zig +82 -6
  64. package/src/runtime/window_storage.zig +14 -3
  65. package/src/runtime/window_views.zig +12 -11
  66. package/src/tooling/manifest.zig +38 -2
  67. package/src/tooling/raw_manifest.zig +8 -0
  68. package/src/tooling/templates.zig +17 -0
@@ -748,6 +748,29 @@ test "text edit state tracks ime composition ranges" {
748
748
  try std.testing.expect(state.composition == null);
749
749
  }
750
750
 
751
+ test "ime composition owns exact bytes when its preview completes CRLF" {
752
+ var preview_storage: [64]u8 = undefined;
753
+ var update_storage: [64]u8 = undefined;
754
+ var cancel_storage: [64]u8 = undefined;
755
+
756
+ const initial = TextEditState{
757
+ .text = "a\nb",
758
+ .selection = TextSelection.collapsed(1),
759
+ };
760
+ const preview = try initial.apply(.{ .set_composition = .{ .text = "\r", .cursor = 1 } }, &preview_storage);
761
+ try std.testing.expectEqualStrings("a\r\nb", preview.text);
762
+ try std.testing.expectEqualDeep(TextRange.init(1, 2), preview.composition.?);
763
+
764
+ const updated = try preview.apply(.{ .set_composition = .{ .text = "X", .cursor = 1 } }, &update_storage);
765
+ try std.testing.expectEqualStrings("aX\nb", updated.text);
766
+ try std.testing.expectEqualDeep(TextRange.init(1, 2), updated.composition.?);
767
+
768
+ const cancelled = try preview.apply(.cancel_composition, &cancel_storage);
769
+ try std.testing.expectEqualStrings("a\nb", cancelled.text);
770
+ try std.testing.expectEqualDeep(TextSelection.collapsed(1), cancelled.selection);
771
+ try std.testing.expect(cancelled.composition == null);
772
+ }
773
+
751
774
  test "text bounds follow utf8 scalar fallback and shaped y offsets" {
752
775
  // Metric boxes inflate by the ink allowance (left/bottom 0.1em,
753
776
  // right 0.35em) so real glyph outlines never clip at the bounds.
@@ -1124,6 +1147,44 @@ test "text layout maps caret selection and points across wrapped fallback lines"
1124
1147
  try expectRectApprox(geometry.RectF.init(4, 10, 24.25, 28), folded[0].rect);
1125
1148
  }
1126
1149
 
1150
+ test "unbroken soft wraps preserve downstream caret affinity" {
1151
+ const text = DrawText{
1152
+ .font_id = 1,
1153
+ .size = 10,
1154
+ .origin = geometry.PointF.init(4, 20),
1155
+ .color = Color.rgb8(0, 0, 0),
1156
+ .text = "abcdefghijklmnopqrstuvwxyz",
1157
+ };
1158
+ const options = TextLayoutOptions{ .max_width = 30, .line_height = 14, .wrap = .word };
1159
+ var lines: [8]TextLine = undefined;
1160
+ const layout = try layoutTextRun(text, options, &lines);
1161
+ try std.testing.expect(layout.lines.len > 1);
1162
+
1163
+ const first_range = text_model.textLineRange(text, layout.lines[0]);
1164
+ const second_range = text_model.textLineRange(text, layout.lines[1]);
1165
+ try std.testing.expectEqual(first_range.end, second_range.start);
1166
+
1167
+ const point = geometry.PointF.init(
1168
+ layout.lines[1].bounds.x - 1,
1169
+ layout.lines[1].bounds.y + layout.lines[1].bounds.height * 0.5,
1170
+ );
1171
+ const streamed = canvas.layoutTextCaretPositionForPoint(text, options, point).?;
1172
+ const buffered = canvas.textCaretPositionForLayoutPoint(text, layout, point).?;
1173
+ try std.testing.expectEqualDeep(
1174
+ canvas.TextCaretPosition{ .offset = second_range.start, .affinity = .downstream },
1175
+ streamed,
1176
+ );
1177
+ try std.testing.expectEqualDeep(streamed, buffered);
1178
+
1179
+ const upstream = canvas.layoutTextCaretRectWithAffinity(text, options, streamed.offset, .upstream).?;
1180
+ const downstream = canvas.layoutTextCaretRectWithAffinity(text, options, streamed.offset, streamed.affinity).?;
1181
+ try std.testing.expectApproxEqAbs(layout.lines[0].bounds.y, upstream.y, 0.001);
1182
+ try std.testing.expectApproxEqAbs(layout.lines[1].bounds.y, downstream.y, 0.001);
1183
+
1184
+ const buffered_downstream = canvas.textCaretRectForLayoutWithAffinity(text, layout, streamed.offset, streamed.affinity).?;
1185
+ try std.testing.expectApproxEqAbs(downstream.y, buffered_downstream.y, 0.001);
1186
+ }
1187
+
1127
1188
  test "text layout maps caret selection and points across shaped glyph lines" {
1128
1189
  const glyphs = [_]Glyph{
1129
1190
  .{ .id = 1, .x = 2, .y = -3, .advance = 5 },
@@ -1510,6 +1571,31 @@ test "text layout handles newlines and shaped glyph runs" {
1510
1571
  try expectRect(geometry.RectF.init(3, 4, 19, 20), shaped.lines[0].bounds);
1511
1572
  }
1512
1573
 
1574
+ test "word-wrapped hard lines preserve leading spaces and caret advances" {
1575
+ const text = DrawText{
1576
+ .font_id = 1,
1577
+ .size = 12,
1578
+ .origin = geometry.PointF.init(0, 12),
1579
+ .color = Color.rgb8(0, 0, 0),
1580
+ .text = "One\n ",
1581
+ };
1582
+ const options = TextLayoutOptions{ .max_width = 100, .line_height = 16, .wrap = .word };
1583
+ var lines: [2]TextLine = undefined;
1584
+ const layout = try layoutTextRun(text, options, &lines);
1585
+
1586
+ try std.testing.expectEqual(@as(usize, 2), layout.lineCount());
1587
+ try std.testing.expectEqual(@as(usize, 4), layout.lines[1].text_start);
1588
+ try std.testing.expectEqual(@as(usize, 2), layout.lines[1].text_len);
1589
+
1590
+ const line_start = layoutTextCaretRect(text, options, 4).?;
1591
+ const first_space = layoutTextCaretRect(text, options, 5).?;
1592
+ const second_space = layoutTextCaretRect(text, options, 6).?;
1593
+ try std.testing.expectEqual(line_start.y, first_space.y);
1594
+ try std.testing.expectEqual(first_space.y, second_space.y);
1595
+ try std.testing.expect(first_space.x > line_start.x);
1596
+ try std.testing.expect(second_space.x > first_space.x);
1597
+ }
1598
+
1513
1599
  test "text layout bounds shaped glyph positions and vertical offsets" {
1514
1600
  const glyphs = [_]Glyph{
1515
1601
  .{ .id = 1, .x = 2, .y = -3, .advance = 5 },
@@ -1024,6 +1024,14 @@ pub fn Ui(comptime Msg: type) type {
1024
1024
  },
1025
1025
  .set_selection => |selection| blk: {
1026
1026
  const Selection = @FieldType(Payload, "set_selection");
1027
+ if (comptime @hasField(Selection, "affinity")) {
1028
+ const Affinity = @FieldType(Selection, "affinity");
1029
+ break :blk @unionInit(Payload, "set_selection", .{
1030
+ .anchor = num(@FieldType(Selection, "anchor"), selection.anchor),
1031
+ .focus = num(@FieldType(Selection, "focus"), selection.focus),
1032
+ .affinity = @field(Affinity, @tagName(selection.affinity)),
1033
+ });
1034
+ }
1027
1035
  break :blk @unionInit(Payload, "set_selection", .{
1028
1036
  .anchor = num(@FieldType(Selection, "anchor"), selection.anchor),
1029
1037
  .focus = num(@FieldType(Selection, "focus"), selection.focus),
@@ -1392,22 +1400,19 @@ pub fn Ui(comptime Msg: type) type {
1392
1400
  // consumers, tests); the textarea newline mapping
1393
1401
  // resolves BEFORE it because the generic key mapping
1394
1402
  // has no Enter case.
1395
- const edit = if (keyboard.edit != null)
1396
- keyboard.edit
1397
- else
1398
- canvas.widgetKeyboardNewlineTextEditEvent(widget.kind, keyboard) orelse keyboard.textEditEvent();
1399
- if (edit) |text_edit| {
1400
- // Single-line kinds sanitize the edit with the
1401
- // SAME rule the runtime seam applies before
1402
- // stamping (strip line breaks; suppress inserts
1403
- // that strip to nothing), so the fallback
1404
- // derivation for events that never crossed the
1405
- // runtime can never feed a model mirror bytes
1406
- // the retained editor would refuse. Stamped
1407
- // edits are already sanitized and pass through
1408
- // untouched.
1409
- if (canvas.sanitizedSingleLineTextInputEvent(widget.kind, text_edit)) |sanitized| {
1410
- if (self.msgForTextEdit(target_id, sanitized)) |msg| return msg;
1403
+ if (keyboard.edit) |stamped| {
1404
+ // Runtime stamps are authoritative. In particular,
1405
+ // history replay can restore exact model-provided
1406
+ // bytes that the new-input sanitizer would strip.
1407
+ if (self.msgForTextEdit(target_id, stamped)) |msg| return msg;
1408
+ } else {
1409
+ const locally_derived = canvas.widgetKeyboardNewlineTextEditEvent(widget.kind, keyboard) orelse keyboard.textEditEvent();
1410
+ if (locally_derived) |text_edit| {
1411
+ // Direct Tree consumers still sanitize locally:
1412
+ // these bytes have not crossed the runtime seam.
1413
+ if (canvas.sanitizedSingleLineTextInputEvent(widget.kind, text_edit)) |sanitized| {
1414
+ if (self.msgForTextEdit(target_id, sanitized)) |msg| return msg;
1415
+ }
1411
1416
  }
1412
1417
  }
1413
1418
  }
@@ -116,6 +116,10 @@ pub const text_caret_direction_members = [_][]const u8{
116
116
  "previous", "next", "previous_word", "next_word", "start", "end",
117
117
  };
118
118
 
119
+ pub const text_caret_affinity_members = [_][]const u8{
120
+ "upstream", "downstream",
121
+ };
122
+
119
123
  /// A Msg arm payload union DECLARING the text-input event shape rather than
120
124
  /// being `canvas.TextInputEvent` by identity — the transpiled-core case,
121
125
  /// where the emitted module declares its own mirror union (type identity
@@ -125,7 +129,8 @@ pub const text_caret_direction_members = [_][]const u8{
125
129
  /// - `insert_text` a bytes payload; the seven verb arms void;
126
130
  /// - `move_caret` a record of `direction` (an enum with exactly the six
127
131
  /// caret-direction member names) and `extend: bool`;
128
- /// - `set_selection` a record of numeric `anchor`/`focus`;
132
+ /// - `set_selection` a record of numeric `anchor`/`focus`, optionally
133
+ /// plus the native `upstream`/`downstream` caret-affinity enum;
129
134
  /// - `set_composition` a record of bytes `text` and optional numeric
130
135
  /// `cursor`.
131
136
  /// Numeric fields accept integer or float (the transpiler's number model
@@ -323,11 +328,23 @@ fn isSelectionRecord(comptime T: type) bool {
323
328
  .@"struct" => |s| s,
324
329
  else => return false,
325
330
  };
326
- if (info.fields.len != 2) return false;
331
+ if (info.fields.len != 2 and info.fields.len != 3) return false;
327
332
  if (!@hasField(T, "anchor") or !@hasField(T, "focus")) return false;
328
333
  inline for (info.fields) |field| {
329
- if (!isNumeric(field.type)) return false;
334
+ if (comptime std.mem.eql(u8, field.name, "affinity")) {
335
+ const members = switch (@typeInfo(field.type)) {
336
+ .@"enum" => |e| e.fields,
337
+ else => return false,
338
+ };
339
+ if (members.len != text_caret_affinity_members.len) return false;
340
+ inline for (text_caret_affinity_members) |name| {
341
+ if (!@hasField(field.type, name)) return false;
342
+ }
343
+ } else if (!isNumeric(field.type)) {
344
+ return false;
345
+ }
330
346
  }
347
+ if (info.fields.len == 3 and !@hasField(T, "affinity")) return false;
331
348
  return true;
332
349
  }
333
350
 
@@ -4428,6 +4428,8 @@ test "reactions misuse fails the build with the pinned teaching messages" {
4428
4428
  pub const MirrorCaretDirection = enum(u8) { previous = 0, next = 1, previous_word = 2, next_word = 3, start = 4, end = 5 };
4429
4429
  pub const MirrorCaretMove = struct { direction: MirrorCaretDirection, extend: bool };
4430
4430
  pub const MirrorSelection = struct { anchor: i64, focus: i64 };
4431
+ pub const MirrorCaretAffinity = enum(u8) { upstream = 0, downstream = 1 };
4432
+ pub const MirrorSelectionWithAffinity = struct { anchor: i64, focus: i64, affinity: MirrorCaretAffinity };
4431
4433
  pub const MirrorTextInputEvent = union(enum) {
4432
4434
  insert_text: []const u8,
4433
4435
  delete_backward,
@@ -4455,6 +4457,20 @@ test "declaredTextInputUnion accepts the emitted mirror shape and rejects near-m
4455
4457
  try testing.expect(markup_view.declaredTextInputUnion(MirrorTextInputEvent));
4456
4458
  // The canvas union itself matches structurally too (usize numerics).
4457
4459
  try testing.expect(markup_view.declaredTextInputUnion(canvas.TextInputEvent));
4460
+ const MirrorTextInputEventWithAffinity = union(enum) {
4461
+ insert_text: []const u8,
4462
+ delete_backward,
4463
+ delete_forward,
4464
+ delete_word_backward,
4465
+ delete_word_forward,
4466
+ clear,
4467
+ move_caret: MirrorCaretMove,
4468
+ set_selection: MirrorSelectionWithAffinity,
4469
+ set_composition: struct { text: []const u8, cursor: ?i64 },
4470
+ commit_composition,
4471
+ cancel_composition,
4472
+ };
4473
+ try testing.expect(markup_view.declaredTextInputUnion(MirrorTextInputEventWithAffinity));
4458
4474
  // Near-misses stay out: a missing arm, a non-bytes insert payload, a
4459
4475
  // wrong caret vocabulary.
4460
4476
  const MissingArm = union(enum) {
@@ -382,6 +382,12 @@ test "single-line keyboard fallback derivation sanitizes line breaks like the ru
382
382
  // A STAMPED edit (the runtime already sanitized it) passes through.
383
383
  const stamped = canvas.WidgetKeyboardEvent{ .phase = .key_down, .edit = .{ .insert_text = "clean" } };
384
384
  try testing.expectEqualStrings("clean", tree.msgForKeyboard(field.id, stamped).?.draft.insert_text);
385
+
386
+ // History replay stamps retained bytes, not new input. A raw newline
387
+ // already present in a controlled single-line value must survive the
388
+ // app-side mirror unchanged.
389
+ const stamped_history = canvas.WidgetKeyboardEvent{ .phase = .key_down, .edit = .{ .insert_text = "\n" } };
390
+ try testing.expectEqualStrings("\n", tree.msgForKeyboard(field.id, stamped_history).?.draft.insert_text);
385
391
  }
386
392
 
387
393
  test "typed handlers imply accessibility actions" {
@@ -209,7 +209,11 @@ fn emitWidgetDepthContent(builder: *Builder, widget: Widget, tokens: DesignToken
209
209
  const paint_widget = widgetWithFrame(widget, pixelSnapGeometryRect(tokens, widget.frame));
210
210
  try emitWidgetBackdropBlur(builder, paint_widget, tokens);
211
211
  switch (paint_widget.kind) {
212
- .stack, .row, .column, .grid, .list, .breadcrumb, .pagination, .radio_group, .toggle_group, .split, .tree => try emitWidgetClippedChildren(builder, paint_widget, tokens, depth),
212
+ .stack, .row, .column => {
213
+ try emitLayoutContainerBackground(builder, paint_widget);
214
+ try emitWidgetClippedChildren(builder, paint_widget, tokens, depth);
215
+ },
216
+ .grid, .list, .breadcrumb, .pagination, .radio_group, .toggle_group, .split, .tree => try emitWidgetClippedChildren(builder, paint_widget, tokens, depth),
213
217
  .button_group => try emitButtonGroupWidget(builder, paint_widget, tokens, depth),
214
218
  .table, .data_grid => {
215
219
  try emitWidgetClippedChildren(builder, paint_widget, tokens, depth);
@@ -488,7 +492,8 @@ fn emitWidgetLayoutNodeContent(
488
492
  const paint_widget = widgetWithFrame(widget, pixelSnapGeometryRect(tokens, widget.frame));
489
493
  try emitWidgetBackdropBlur(builder, paint_widget, tokens);
490
494
  switch (paint_widget.kind) {
491
- .stack, .row, .column, .breadcrumb, .button_group, .pagination, .radio_group, .toggle_group, .split, .tree => {},
495
+ .stack, .row, .column => try emitLayoutContainerBackground(builder, paint_widget),
496
+ .breadcrumb, .button_group, .pagination, .radio_group, .toggle_group, .split, .tree => {},
492
497
  .data_row => try emitDataRowWidgetWash(builder, paint_widget, tokens),
493
498
  .tabs => try widget_render_surfaces.emitTabsListWidgetChrome(builder, paint_widget, tokens),
494
499
  .table, .data_grid => {
@@ -623,6 +628,20 @@ fn emitWidgetLayoutNodeContent(
623
628
  try emitWidgetLayoutClippedChildren(builder, layout, node_index, tokens, state, paint_widget);
624
629
  }
625
630
 
631
+ /// Flow and stacking containers have no implicit surface treatment, but
632
+ /// an author background is real chrome: it fills the laid-out frame
633
+ /// before any children, with the same optional radius accepted by the
634
+ /// builder and markup grammar.
635
+ fn emitLayoutContainerBackground(builder: *Builder, widget: Widget) Error!void {
636
+ const background = widget.style.background orelse return;
637
+ try builder.fillRoundedRect(.{
638
+ .id = widgetPartId(widget.id, 1),
639
+ .rect = widget.frame,
640
+ .radius = Radius.all(nonNegative(widget.style.radius orelse 0)),
641
+ .fill = colorFill(background),
642
+ });
643
+ }
644
+
626
645
  fn emitWidgetLayoutScrollableChildren(
627
646
  builder: *Builder,
628
647
  layout: anytype,
@@ -43,6 +43,7 @@ pub const max_widget_text_range_rects: usize = 4;
43
43
  pub const widgetControlHeight = widget_metrics.widgetControlHeight;
44
44
  pub const WidgetTextGeometry = widget_text_input.WidgetTextGeometry;
45
45
  pub const textSelectionForWidgetPoint = widget_text_input.textSelectionForWidgetPoint;
46
+ pub const textCaretPositionForWidgetPoint = widget_text_input.textCaretPositionForWidgetPoint;
46
47
  pub const textOffsetForWidgetPoint = widget_text_input.textOffsetForWidgetPoint;
47
48
  pub const textInputViewportForWidget = widget_text_input.textInputViewportForWidget;
48
49
  pub const textInputClearButtonRect = widget_text_input.textInputClearButtonRect;
@@ -1352,6 +1352,57 @@ test "widget tree emits panel button text and progress commands" {
1352
1352
  }
1353
1353
  }
1354
1354
 
1355
+ test "stack row and column backgrounds paint in tree and layout emission" {
1356
+ const kinds = [_]WidgetKind{ .stack, .row, .column };
1357
+ const frame = geometry.RectF.init(10, 12, 140, 72);
1358
+ const background = Color.rgb8(18, 52, 86);
1359
+
1360
+ for (kinds, 0..) |kind, index| {
1361
+ const id: ObjectId = @intCast(index + 1);
1362
+ const widget = Widget{
1363
+ .id = id,
1364
+ .kind = kind,
1365
+ .frame = frame,
1366
+ .style = .{
1367
+ .background = background,
1368
+ .radius = 7,
1369
+ },
1370
+ };
1371
+
1372
+ var tree_commands: [1]CanvasCommand = undefined;
1373
+ var tree_builder = Builder.init(&tree_commands);
1374
+ try emitWidgetTree(&tree_builder, widget, .{});
1375
+ const tree_display_list = tree_builder.displayList();
1376
+ try std.testing.expectEqual(@as(usize, 1), tree_display_list.commandCount());
1377
+ switch (tree_display_list.commands[0]) {
1378
+ .fill_rounded_rect => |fill| {
1379
+ try std.testing.expectEqual(widgetPartId(id, 1), fill.id);
1380
+ try expectRect(frame, fill.rect);
1381
+ try std.testing.expectEqualDeep(Radius.all(7), fill.radius);
1382
+ try expectFillColor(background, fill.fill);
1383
+ },
1384
+ else => return error.TestUnexpectedResult,
1385
+ }
1386
+
1387
+ var nodes: [1]WidgetLayoutNode = undefined;
1388
+ const layout = try layoutWidgetTree(widget, frame, &nodes);
1389
+ var layout_commands: [1]CanvasCommand = undefined;
1390
+ var layout_builder = Builder.init(&layout_commands);
1391
+ try emitWidgetLayout(&layout_builder, layout, .{});
1392
+ const layout_display_list = layout_builder.displayList();
1393
+ try std.testing.expectEqual(@as(usize, 1), layout_display_list.commandCount());
1394
+ switch (layout_display_list.commands[0]) {
1395
+ .fill_rounded_rect => |fill| {
1396
+ try std.testing.expectEqual(widgetPartId(id, 1), fill.id);
1397
+ try expectRect(frame, fill.rect);
1398
+ try std.testing.expectEqualDeep(Radius.all(7), fill.radius);
1399
+ try expectFillColor(background, fill.fill);
1400
+ },
1401
+ else => return error.TestUnexpectedResult,
1402
+ }
1403
+ }
1404
+ }
1405
+
1355
1406
  test "widget tree emits backdrop blur before widget content" {
1356
1407
  const children = [_]Widget{.{
1357
1408
  .id = 2,
@@ -17,7 +17,7 @@ const TextRange = text_model.TextRange;
17
17
  const TextSelection = text_model.TextSelection;
18
18
  const DesignTokens = token_model.DesignTokens;
19
19
  const Widget = widget_model.Widget;
20
- const snapTextSelection = text_model.snapTextSelection;
20
+ const snapTextCaretSelection = text_model.snapTextCaretSelection;
21
21
  const snapTextOffset = text_model.snapTextOffset;
22
22
  const snapTextRange = text_model.snapTextRange;
23
23
  const measureTextWidthForFont = text_model.measureTextWidthForFont;
@@ -25,8 +25,8 @@ const nextTextLineEnd = text_model.nextTextLineEnd;
25
25
  const isTextBreakByte = text_model.isTextBreakByte;
26
26
  const textLineRange = text_model.textLineRange;
27
27
  const textLineCaretX = text_model.textLineCaretX;
28
- const layoutTextCaretRect = text_model.layoutTextCaretRect;
29
- const layoutTextOffsetForPoint = text_model.layoutTextOffsetForPoint;
28
+ const layoutTextCaretRectWithAffinity = text_model.layoutTextCaretRectWithAffinity;
29
+ const layoutTextCaretPositionForPoint = text_model.layoutTextCaretPositionForPoint;
30
30
  const TextLineIterator = text_model.TextLineIterator;
31
31
 
32
32
  pub fn widgetPlaceholder(widget: Widget) []const u8 {
@@ -128,15 +128,20 @@ pub fn persistWidgetTextInputPresentedText(builder: *Builder, raw: []const u8, p
128
128
  }
129
129
 
130
130
  pub fn textSelectionForWidgetPoint(widget: Widget, point: geometry.PointF, anchor: ?usize, tokens: DesignTokens) ?TextSelection {
131
- const offset = textOffsetForWidgetPoint(widget, point, tokens) orelse return null;
131
+ const position = textCaretPositionForWidgetPoint(widget, point, tokens) orelse return null;
132
132
  const selection = if (anchor) |anchor_offset|
133
- TextSelection{ .anchor = anchor_offset, .focus = offset }
133
+ TextSelection{ .anchor = anchor_offset, .focus = position.offset, .affinity = position.affinity }
134
134
  else
135
- TextSelection.collapsed(offset);
136
- return snapTextSelection(widget.text, selection);
135
+ TextSelection.collapsedAt(position);
136
+ return snapTextCaretSelection(widget.text, selection);
137
137
  }
138
138
 
139
139
  pub fn textOffsetForWidgetPoint(widget: Widget, point: geometry.PointF, tokens: DesignTokens) ?usize {
140
+ const position = textCaretPositionForWidgetPoint(widget, point, tokens) orelse return null;
141
+ return position.offset;
142
+ }
143
+
144
+ pub fn textCaretPositionForWidgetPoint(widget: Widget, point: geometry.PointF, tokens: DesignTokens) ?text_model.TextCaretPosition {
140
145
  if (!widget_access.widgetTextInputKind(widget.kind)) return null;
141
146
  if (widget.state.disabled) return null;
142
147
  const text_size = widgetTextInputSize(widget, tokens);
@@ -144,7 +149,8 @@ pub fn textOffsetForWidgetPoint(widget: Widget, point: geometry.PointF, tokens:
144
149
  const layout_options = widgetTextInputLayoutOptions(widget, tokens, text_size, text_inset);
145
150
  const origin = widgetTextInputOrigin(widget, tokens, text_size, text_inset, layout_options);
146
151
  const draw_text = widgetTextInputDrawText(widget, tokens, text_size, origin, tokens.colors.text, layout_options);
147
- return layoutTextOffsetForPoint(draw_text, layout_options, point);
152
+ const position = layoutTextCaretPositionForPoint(draw_text, layout_options, point) orelse return null;
153
+ return text_model.snapTextCaretPosition(widget.text, position);
148
154
  }
149
155
 
150
156
  pub fn widgetTextInputSize(widget: Widget, tokens: DesignTokens) f32 {
@@ -371,8 +377,13 @@ fn widgetTextInputLineCount(widget: Widget, font_id: FontId, text_size: f32, opt
371
377
  count += 1;
372
378
  if (end >= text.len) break;
373
379
  start = end;
374
- if (start < text.len and text[start] == '\n') start += 1;
375
- while (options.wrap == .word and start < text.len and isTextBreakByte(text[start])) start += 1;
380
+ if (start < text.len and text[start] == '\n') {
381
+ // Leading whitespace after a hard break belongs to the next
382
+ // editable line; only a soft wrap elides separators.
383
+ start += 1;
384
+ } else {
385
+ while (options.wrap == .word and start < text.len and isTextBreakByte(text[start])) start += 1;
386
+ }
376
387
  }
377
388
  return @max(1, count);
378
389
  }
@@ -478,7 +489,8 @@ pub fn textGeometryForWidget(widget: Widget, tokens: DesignTokens) WidgetTextGeo
478
489
 
479
490
  if (widget_access.widgetTextSelectionRange(widget)) |range| {
480
491
  if (range.isCollapsed(widget.text.len)) {
481
- value.caret_bounds = layoutTextCaretRect(draw_text, layout_options, range.start);
492
+ const affinity = if (widget.text_selection) |selection| selection.affinity else .upstream;
493
+ value.caret_bounds = layoutTextCaretRectWithAffinity(draw_text, layout_options, range.start, affinity);
482
494
  } else {
483
495
  const bounds = textRangeBounds(draw_text, layout_options, range);
484
496
  value.selection_bounds = bounds.bounds;
@@ -95,6 +95,15 @@ pub const CanvasWidgetKeyboardEvent = struct {
95
95
  /// tail so an edit that unmounts the hovered listener never
96
96
  /// strands its leave.
97
97
  standalone: bool = false,
98
+ /// Continuation edits replay an existing history entry and must not
99
+ /// themselves create a new entry.
100
+ history_replay: bool = false,
101
+ /// Stable retained-history identity for a compound undo/redo. Follow-up
102
+ /// edits are re-derived from this entry after every controlled-tree
103
+ /// rebuild instead of retaining borrowed slices into the compactable
104
+ /// history byte pool.
105
+ history_replay_serial: u64 = 0,
106
+ history_replay_redo: bool = false,
98
107
  };
99
108
 
100
109
  /// A scroll container's offset changed from a user gesture (wheel,
@@ -36,6 +36,15 @@ pub fn viewKindFromString(value: []const u8) ?platform.ViewKind {
36
36
  return null;
37
37
  }
38
38
 
39
+ pub fn windowTitlebarStyleFromString(value: []const u8) ?platform.WindowTitlebarStyle {
40
+ inline for (@typeInfo(platform.WindowTitlebarStyle).@"enum".fields) |field| {
41
+ if (std.mem.eql(u8, value, field.name)) return @field(platform.WindowTitlebarStyle, field.name);
42
+ }
43
+ if (std.mem.eql(u8, value, "hiddenInset")) return .hidden_inset;
44
+ if (std.mem.eql(u8, value, "hiddenInsetTall")) return .hidden_inset_tall;
45
+ return null;
46
+ }
47
+
39
48
  pub fn gpuSurfaceOptionsFromJson(payload: []const u8, storage: *json.StringStorage) !platform.GpuSurfaceOptions {
40
49
  var options = platform.GpuSurfaceOptions{};
41
50
  if (jsonStringField(payload, "gpuBackend", storage) orelse jsonStringField(payload, "gpu_backend", storage)) |value| {
@@ -293,6 +293,7 @@ pub fn writeWindowJsonToWriter(window: platform.WindowInfo, writer: anytype) !vo
293
293
  pub fn builtinBridgeErrorMessage(err: anyerror) []const u8 {
294
294
  return switch (err) {
295
295
  error.UnsupportedService => "Native service is not available on this platform",
296
+ error.UnsupportedWindowTransparency => "This backend cannot make window content transparent",
296
297
  error.WindowNotFound => "Window was not found",
297
298
  error.WindowLimitReached => "Window limit reached",
298
299
  error.DuplicateWindowLabel => "Window id or label already exists",
@@ -361,6 +362,7 @@ pub fn builtinBridgeErrorMessage(err: anyerror) []const u8 {
361
362
  pub fn builtinBridgeErrorCode(err: anyerror) bridge.ErrorCode {
362
363
  return switch (err) {
363
364
  error.UnsupportedService,
365
+ error.UnsupportedWindowTransparency,
364
366
  error.InvalidWindowOptions,
365
367
  error.WindowNotFound,
366
368
  error.WindowLimitReached,
@@ -22,6 +22,7 @@ const jsonBoolField = bridge_payload.jsonBoolField;
22
22
  const webViewWindowIdFromJson = bridge_payload.webViewWindowIdFromJson;
23
23
  const viewWindowIdFromJson = bridge_payload.viewWindowIdFromJson;
24
24
  const viewKindFromString = bridge_payload.viewKindFromString;
25
+ const windowTitlebarStyleFromString = bridge_payload.windowTitlebarStyleFromString;
25
26
  const gpuSurfaceOptionsFromJson = bridge_payload.gpuSurfaceOptionsFromJson;
26
27
  const viewFrameFromJson = bridge_payload.viewFrameFromJson;
27
28
  const viewLayerFromJson = bridge_payload.viewLayerFromJson;
@@ -236,11 +237,20 @@ pub fn RuntimeBuiltinBridge(comptime Runtime: type) type {
236
237
  const x = jsonNumberField(payload, "x") orelse 0;
237
238
  const y = jsonNumberField(payload, "y") orelse 0;
238
239
  const source = if (jsonStringField(payload, "url", &storage)) |url| platform.WebViewSource.url(url) else null;
240
+ const titlebar = if (jsonStringField(payload, "titlebar", &storage)) |value|
241
+ windowTitlebarStyleFromString(value) orelse return error.InvalidWindowOptions
242
+ else
243
+ .standard;
239
244
  const info = try self.createWindow(.{
240
245
  .label = label,
241
246
  .title = title,
242
247
  .default_frame = geometry.RectF.init(x, y, width, height),
243
248
  .restore_state = jsonBoolField(payload, "restoreState") orelse true,
249
+ .titlebar = titlebar,
250
+ .transparent = jsonBoolField(payload, "transparent") orelse false,
251
+ .always_on_top = jsonBoolField(payload, "alwaysOnTop") orelse false,
252
+ .click_through = jsonBoolField(payload, "clickThrough") orelse false,
253
+ .activate_on_show = jsonBoolField(payload, "activateOnShow") orelse true,
244
254
  .source = source,
245
255
  });
246
256
  return writeWindowJson(info, output);
@@ -327,6 +327,7 @@ pub fn canvasWidgetPointerEventFromGpuInput(input_event: GpuSurfaceInputEvent) ?
327
327
  .delta = geometry.OffsetF.init(input_event.delta_x, input_event.delta_y),
328
328
  .pointer_id = input_event.pointer_id,
329
329
  .button = input_event.button,
330
+ .modifiers = canvasWidgetKeyboardModifiers(input_event.modifiers),
330
331
  };
331
332
  }
332
333
 
@@ -176,6 +176,13 @@ pub const max_canvas_widget_semantics_per_view: usize = 1024;
176
176
  // a 1024-node view retains proportionally more text.
177
177
  pub const max_canvas_widget_text_bytes_per_view: usize = 65536;
178
178
  pub const max_canvas_widget_source_text_entries_per_view: usize = 256;
179
+ /// Retained editor undo/redo is delta-based: changed bytes share this
180
+ /// per-view pool and metadata stays bounded independently. Matching the
181
+ /// live text budget guarantees that clearing a maximal editor can still
182
+ /// be undone; an unusually large whole-document replacement whose removed
183
+ /// plus inserted bytes exceed the pool simply starts a fresh history.
184
+ pub const max_canvas_widget_text_history_bytes_per_view: usize = max_canvas_widget_text_bytes_per_view;
185
+ pub const max_canvas_widget_text_history_entries_per_view: usize = 128;
179
186
  // Inline styled runs retained across all `.text` widgets of a view. Each
180
187
  // span is a small struct (style flags + slices into the widget text
181
188
  // bytes); per-paragraph capacity is `canvas.max_text_spans_per_paragraph`.
@@ -846,6 +846,7 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
846
846
  .{ .id = 4, .kind = .text_field, .frame = geometry.RectF.init(12, 96, 160, 28), .text = "Search", .placeholder = "Search deployments", .text_selection = canvas.TextSelection{ .anchor = 1, .focus = 4 }, .text_composition = canvas.TextRange.init(2, 5), .state = .{ .required = true, .read_only = true, .invalid = true } },
847
847
  .{ .id = 5, .kind = .select, .frame = geometry.RectF.init(184, 96, 120, 28), .text = "Production", .state = .{ .expanded = false }, .semantics = .{ .label = "Environment" } },
848
848
  .{ .id = 10, .kind = .data_grid, .frame = geometry.RectF.init(12, 132, 220, 64), .text = "Deployments", .layout = .{ .gap = 2 }, .children = &rows },
849
+ .{ .id = 6, .kind = .input, .frame = geometry.RectF.init(184, 58, 120, 28), .text = "Draft" },
849
850
  };
850
851
  var layout_nodes: [16]canvas.WidgetLayoutNode = undefined;
851
852
  const layout = try canvas.layoutWidgetTree(.{
@@ -860,7 +861,7 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
860
861
  try std.testing.expect(platform_state.update_count >= 1);
861
862
  try std.testing.expectEqual(@as(platform.WindowId, 1), platform_state.window_id);
862
863
  try std.testing.expectEqualStrings("canvas", platform_state.view_label[0..platform_state.view_label_len]);
863
- try std.testing.expectEqual(@as(usize, 12), platform_state.node_count);
864
+ try std.testing.expectEqual(@as(usize, 13), platform_state.node_count);
864
865
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.group, platform_state.nodes[0].role);
865
866
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.button, platform_state.nodes[1].role);
866
867
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.checkbox, platform_state.nodes[2].role);
@@ -870,12 +871,15 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
870
871
  const cell_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 16).?;
871
872
  const text_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 4).?;
872
873
  const select_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 5).?;
874
+ const initial_editor_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 6).?;
873
875
  try std.testing.expectEqual(@as(?bool, false), select_node.expanded);
874
876
  try std.testing.expect(text_node.required);
875
877
  try std.testing.expect(text_node.read_only);
876
878
  try std.testing.expect(text_node.invalid);
877
879
  try std.testing.expect(!text_node.actions.set_text);
878
880
  try std.testing.expect(text_node.actions.set_selection);
881
+ try std.testing.expect(!initial_editor_node.can_undo);
882
+ try std.testing.expect(!initial_editor_node.can_redo);
879
883
  try std.testing.expectEqual(platform.WidgetAccessibilityRole.grid, grid_node.role);
880
884
  try std.testing.expectEqual(@as(?usize, 2), grid_node.grid_row_count);
881
885
  try std.testing.expectEqual(@as(?usize, 2), grid_node.grid_column_count);
@@ -900,6 +904,28 @@ test "runtime publishes canvas widget accessibility snapshots to platform" {
900
904
  try std.testing.expectEqual(@as(f32, 12), platform_state.nodes[1].bounds.x);
901
905
  try std.testing.expectEqual(@as(f32, 14), platform_state.nodes[1].bounds.y);
902
906
 
907
+ runtime.views[0].focused = true;
908
+ runtime.views[0].keyboard_active = true;
909
+ runtime.views[0].canvas_widget_focused_id = 6;
910
+ _ = try runtime.views[0].applyCanvasWidgetTextEdit(6, .{ .insert_text = "!" });
911
+ _ = try runtime.refreshCanvasWidgetDisplayListIfOwned(0);
912
+ const edited_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 6).?;
913
+ try std.testing.expect(edited_node.can_undo);
914
+ try std.testing.expect(!edited_node.can_redo);
915
+
916
+ const editor_target = runtime.views[0].widgetLayoutTree().focusTargetById(6).?;
917
+ const undo = runtime.views[0].canvasWidgetTextHistoryShortcut(editor_target, .{
918
+ .phase = .key_down,
919
+ .key = "z",
920
+ .modifiers = .{ .super = true },
921
+ }).?;
922
+ _ = try runtime.views[0].applyCanvasWidgetTextEditWithoutHistory(6, undo.edit);
923
+ runtime.views[0].commitCanvasWidgetTextHistoryReplayIfComplete(editor_target, undo.serial, undo.redo);
924
+ _ = try runtime.refreshCanvasWidgetDisplayListIfOwned(0);
925
+ const undone_node = platformWidgetAccessibilityNodeById(platform_state.nodes[0..platform_state.node_count], 6).?;
926
+ try std.testing.expect(!undone_node.can_undo);
927
+ try std.testing.expect(undone_node.can_redo);
928
+
903
929
  const published_after_layout = platform_state.update_count;
904
930
  try runtime.dispatchPlatformEvent(app_state.app(), .{ .gpu_surface_input = .{
905
931
  .window_id = 1,
@@ -483,6 +483,22 @@ test "static text drag selection highlights, copies, and clears" {
483
483
  // With nothing selected, copy leaves the clipboard untouched.
484
484
  try harness.runtime.dispatchPlatformEvent(app, keyInput("c", cmd));
485
485
  try std.testing.expectEqualStrings(body, try harness.runtime.readClipboard(&clipboard_buffer));
486
+
487
+ // Shift is an editable-text extension gesture. It must not prevent a
488
+ // fresh static-text drag from establishing its own press anchor.
489
+ try harness.runtime.dispatchPlatformEvent(app, .{ .gpu_surface_input = .{
490
+ .window_id = 1,
491
+ .label = "canvas",
492
+ .kind = .pointer_down,
493
+ .x = 13,
494
+ .y = 18,
495
+ .modifiers = .{ .shift = true },
496
+ } });
497
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_drag, 211, 75));
498
+ retained = try harness.runtime.canvasWidgetLayout(1, "canvas");
499
+ try std.testing.expectEqualDeep(canvas.TextSelection{ .anchor = 0, .focus = body.len }, retained.nodes[1].widget.text_selection.?);
500
+ try std.testing.expectEqual(@as(canvas.ObjectId, 2), harness.runtime.views[0].canvas_widget_selected_text_id);
501
+ try harness.runtime.dispatchPlatformEvent(app, pointerInput(.pointer_up, 211, 75));
486
502
  }
487
503
 
488
504
  test "span paragraph drag selection copies the concatenated bytes" {