@native-sdk/cli 0.6.3 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/build.zig +65 -91
  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/text.ts +25 -14
  6. package/packages/core/src/checker.ts +39 -0
  7. package/packages/core/src/emitter.ts +125 -1
  8. package/skill-data/native-ui/SKILL.md +23 -6
  9. package/src/platform/macos/appkit_host.m +25 -1
  10. package/src/platform/macos/cef_host.mm +24 -2
  11. package/src/platform/macos/root.zig +13 -0
  12. package/src/primitives/canvas/code.zig +1008 -0
  13. package/src/primitives/canvas/code_tests.zig +1912 -0
  14. package/src/primitives/canvas/events.zig +111 -1
  15. package/src/primitives/canvas/layout_audit.zig +3 -1
  16. package/src/primitives/canvas/markdown.zig +13 -11
  17. package/src/primitives/canvas/markdown_tests.zig +168 -6
  18. package/src/primitives/canvas/root.zig +8 -2
  19. package/src/primitives/canvas/tests.zig +1 -0
  20. package/src/primitives/canvas/text.zig +6 -0
  21. package/src/primitives/canvas/text_measure_cache.zig +4 -4
  22. package/src/primitives/canvas/text_span_tests.zig +254 -0
  23. package/src/primitives/canvas/text_spans.zig +556 -32
  24. package/src/primitives/canvas/themes/geist.zig +29 -0
  25. package/src/primitives/canvas/tokens.zig +72 -2
  26. package/src/primitives/canvas/ui.zig +400 -22
  27. package/src/primitives/canvas/ui_markup.zig +78 -7
  28. package/src/primitives/canvas/ui_markup_compiled.zig +89 -0
  29. package/src/primitives/canvas/ui_markup_compiled_tests.zig +45 -0
  30. package/src/primitives/canvas/ui_markup_contract.zig +41 -0
  31. package/src/primitives/canvas/ui_markup_view.zig +89 -0
  32. package/src/primitives/canvas/ui_markup_view_tests.zig +154 -3
  33. package/src/primitives/canvas/ui_schema.zig +30 -6
  34. package/src/primitives/canvas/ui_schema_tests.zig +13 -10
  35. package/src/primitives/canvas/ui_tests.zig +26 -0
  36. package/src/primitives/canvas/widget_invalidation.zig +4 -0
  37. package/src/primitives/canvas/widget_layout.zig +57 -8
  38. package/src/primitives/canvas/widget_layout_tests.zig +35 -0
  39. package/src/primitives/canvas/widget_metrics.zig +35 -1
  40. package/src/primitives/canvas/widget_render.zig +1077 -11
  41. package/src/primitives/canvas/widget_render_controls.zig +4 -4
  42. package/src/primitives/canvas/widget_runtime.zig +2 -0
  43. package/src/primitives/canvas/widget_text_input.zig +165 -8
  44. package/src/primitives/canvas/widget_text_select.zig +2 -2
  45. package/src/primitives/canvas/widget_tree.zig +6 -5
  46. package/src/primitives/canvas/widgets.zig +38 -0
  47. package/src/root.zig +1 -0
  48. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  49. package/src/runtime/canvas_limits.zig +13 -6
  50. package/src/runtime/canvas_widget_clipboard_tests.zig +137 -4
  51. package/src/runtime/canvas_widget_context_menu.zig +17 -2
  52. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  53. package/src/runtime/canvas_widget_events.zig +58 -21
  54. package/src/runtime/canvas_widget_runtime.zig +112 -6
  55. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  56. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  57. package/src/runtime/canvas_widget_state.zig +27 -0
  58. package/src/runtime/canvas_widget_text_tests.zig +383 -0
  59. package/src/runtime/core.zig +15 -5
  60. package/src/runtime/gpu_surface_events.zig +12 -12
  61. package/src/runtime/view.zig +61 -19
  62. package/src/runtime/view_widget_scroll.zig +48 -18
  63. package/src/runtime/view_widget_text.zig +248 -17
  64. package/src/runtime/window_view_runtime.zig +9 -0
@@ -263,6 +263,9 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
263
263
  if (comptime std.mem.eql(u8, node.name, "markdown")) {
264
264
  return buildMarkdown(node, entries, ui, model, scope);
265
265
  }
266
+ if (comptime std.mem.eql(u8, node.name, "code")) {
267
+ return buildCode(node, entries, ui, model, scope);
268
+ }
266
269
  if (comptime std.mem.eql(u8, node.name, "stepper")) {
267
270
  return buildStepper(node, entries, ui, model, scope);
268
271
  }
@@ -776,6 +779,90 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
776
779
  return Md.view(ui, source_text, options);
777
780
  }
778
781
 
782
+ // ----------------------------------------------------------- code
783
+
784
+ fn buildCode(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, ui: *Ui, model: *const ModelT, scope: anytype) Ui.Node {
785
+ comptime {
786
+ if (node.children.len != 0) fail(node.children[0], markup.code_children_message);
787
+ for (node.attrs) |attribute| {
788
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
789
+ const known = std.mem.eql(u8, attribute.name, "source") or
790
+ std.mem.eql(u8, attribute.name, "language") or
791
+ std.mem.eql(u8, attribute.name, "editable") or
792
+ std.mem.eql(u8, attribute.name, "on-input") or
793
+ std.mem.eql(u8, attribute.name, "line-numbers") or
794
+ std.mem.eql(u8, attribute.name, "wrap") or
795
+ std.mem.eql(u8, attribute.name, "width") or
796
+ std.mem.eql(u8, attribute.name, "height") or
797
+ std.mem.eql(u8, attribute.name, "min-width") or
798
+ std.mem.eql(u8, attribute.name, "grow") or
799
+ std.mem.eql(u8, attribute.name, "label") or
800
+ std.mem.eql(u8, attribute.name, "key") or
801
+ std.mem.eql(u8, attribute.name, "global-key");
802
+ if (!known) fail(node, markup.code_attr_message);
803
+ }
804
+ }
805
+ const source_path = comptime blk: {
806
+ const raw = node.attr("source") orelse fail(node, markup.code_source_message);
807
+ const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.code_source_message);
808
+ if (expression != .binding) fail(node, markup.code_source_message);
809
+ break :blk expression.binding;
810
+ };
811
+ comptime requireVariant(pathVariant(node, entries, source_path, true), &.{.string}, node, markup.code_source_message);
812
+ const source = switch (bindingValue(node, entries, source_path, ui, model, scope, true)) {
813
+ .string => |text| text,
814
+ else => runtimeFail([]const u8, ui),
815
+ };
816
+
817
+ var options: Ui.CodeOptions = .{};
818
+ if (comptime (node.attr("language") != null)) {
819
+ const name = comptime blk: {
820
+ const expression = markup.parseAttrExpression(node.attr("language").?) orelse fail(node, markup.code_language_message);
821
+ if (expression != .literal) fail(node, markup.code_language_message);
822
+ if (!canvas.code.isLanguageName(expression.literal)) fail(node, markup.code_language_message);
823
+ break :blk expression.literal;
824
+ };
825
+ options.language = canvas.code.languageFromName(name);
826
+ }
827
+ if (comptime (node.attr("line-numbers") != null)) {
828
+ options.line_numbers = videoFlagValue(node, entries, comptime node.attr("line-numbers").?, ui, model, scope);
829
+ }
830
+ if (comptime (node.attr("editable") != null)) {
831
+ options.editable = videoFlagValue(node, entries, comptime node.attr("editable").?, ui, model, scope);
832
+ }
833
+ if (comptime (node.attr("on-input") != null)) {
834
+ const expression = comptime (markup.parseMessageExpression(node.attr("on-input").?) orelse
835
+ fail(node, "invalid message expression: on-* takes a Msg tag (\"add\") or tag with one binding payload (\"toggle:{item.id}\")"));
836
+ options.on_input = comptime (inputConstructor(expression.tag) orelse
837
+ fail(node, "on-input tag must carry a TextInputEvent payload"));
838
+ }
839
+ if (comptime (node.attr("wrap") != null)) {
840
+ options.wrap = videoFlagValue(node, entries, comptime node.attr("wrap").?, ui, model, scope);
841
+ }
842
+ if (comptime (node.attr("width") != null)) {
843
+ options.width = floatAttr(node, entries, comptime node.attr("width").?, ui, model, scope);
844
+ }
845
+ if (comptime (node.attr("height") != null)) {
846
+ options.height = floatAttr(node, entries, comptime node.attr("height").?, ui, model, scope);
847
+ }
848
+ if (comptime (node.attr("min-width") != null)) {
849
+ options.min_width = floatAttr(node, entries, comptime node.attr("min-width").?, ui, model, scope);
850
+ }
851
+ if (comptime (node.attr("grow") != null)) {
852
+ options.grow = floatAttr(node, entries, comptime node.attr("grow").?, ui, model, scope);
853
+ }
854
+ if (comptime (node.attr("label") != null)) {
855
+ options.semantics.label = stringAttr(node, entries, comptime node.attr("label").?, ui, model, scope, "label expects text");
856
+ }
857
+ if (comptime (node.attr("key") != null)) {
858
+ options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
859
+ }
860
+ if (comptime (node.attr("global-key") != null)) {
861
+ options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
862
+ }
863
+ return ui.code(options, source);
864
+ }
865
+
779
866
  fn markdownLinkConstructor(comptime node: markup.MarkupNode, comptime raw: []const u8) Ui.LinkMsgFn {
780
867
  comptime {
781
868
  @setEvalBranchQuota(10_000);
@@ -2015,6 +2102,8 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
2015
2102
  const msg = constructMessage(node, expression, entries, ui, model, scope);
2016
2103
  if (comptime std.mem.eql(u8, event, "press")) {
2017
2104
  options.on_press = msg;
2105
+ } else if (comptime std.mem.eql(u8, event, "double-press")) {
2106
+ options.on_double_press = msg;
2018
2107
  } else if (comptime std.mem.eql(u8, event, "toggle")) {
2019
2108
  options.on_toggle = msg;
2020
2109
  } else if (comptime std.mem.eql(u8, event, "change")) {
@@ -1089,6 +1089,51 @@ test "compiled markdown element matches the interpreter and the hand-written Md.
1089
1089
  try testing.expectEqual(summary_item.id, fixture.findByKind(expanded_compiled.root, .list_item).?.id);
1090
1090
  }
1091
1091
 
1092
+ // ------------------------------------------------------ code element parity
1093
+
1094
+ const CodeUi = fixture.CodeUi;
1095
+ const CodeInterpreter = markup_view.MarkupView(fixture.CodeModel, fixture.CodeMsg);
1096
+ const CodeCompiled = canvas.CompiledMarkupView(fixture.CodeModel, fixture.CodeMsg, fixture.code_markup_source);
1097
+
1098
+ test "compiled code element matches the interpreter and Ui.code" {
1099
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
1100
+ defer arena_state.deinit();
1101
+ const arena = arena_state.allocator();
1102
+ var model = fixture.CodeModel{};
1103
+
1104
+ var view = try CodeInterpreter.init(arena, fixture.code_markup_source);
1105
+ var interpreted_ui = CodeUi.init(arena);
1106
+ const interpreted = try interpreted_ui.finalize(try view.build(&interpreted_ui, &model));
1107
+ var compiled_ui = CodeUi.init(arena);
1108
+ const compiled = try compiled_ui.finalize(CodeCompiled.build(&compiled_ui, &model));
1109
+ var hand_ui = CodeUi.init(arena);
1110
+ const hand = try hand_ui.finalize(fixture.handCodeView(&hand_ui, &model));
1111
+
1112
+ try expectSameTree(fixture.CodeMsg, hand, interpreted);
1113
+ try expectSameTree(fixture.CodeMsg, hand, compiled);
1114
+ try expectSameTexts(interpreted.root, compiled.root);
1115
+ try testing.expectEqual(canvas.ScrollAxes.horizontal, fixture.findByKind(compiled.root, .scroll_view).?.scroll_axes);
1116
+
1117
+ model.wrap_code = true;
1118
+ model.show_lines = false;
1119
+ var wrapped_ui = CodeUi.init(arena);
1120
+ const wrapped = try wrapped_ui.finalize(CodeCompiled.build(&wrapped_ui, &model));
1121
+ try testing.expect(fixture.findByKind(wrapped.root, .scroll_view) == null);
1122
+ try testing.expect(fixture.findByText(wrapped.root, .text, "1") == null);
1123
+
1124
+ model.editable_code = true;
1125
+ var editable_compiled_ui = CodeUi.init(arena);
1126
+ const editable_compiled = try editable_compiled_ui.finalize(CodeCompiled.build(&editable_compiled_ui, &model));
1127
+ var editable_interpreted_ui = CodeUi.init(arena);
1128
+ const editable_interpreted = try editable_interpreted_ui.finalize(try view.build(&editable_interpreted_ui, &model));
1129
+ var editable_hand_ui = CodeUi.init(arena);
1130
+ const editable_hand = try editable_hand_ui.finalize(fixture.handCodeView(&editable_hand_ui, &model));
1131
+ try expectSameTree(fixture.CodeMsg, editable_hand, editable_interpreted);
1132
+ try expectSameTree(fixture.CodeMsg, editable_hand, editable_compiled);
1133
+ try testing.expectEqual(canvas.WidgetKind.textarea, editable_compiled.root.kind);
1134
+ try testing.expect(editable_compiled.root.code_editor);
1135
+ }
1136
+
1092
1137
  // ------------------------------------------- template/use + style parity
1093
1138
 
1094
1139
  fn expectSameStyles(expected: canvas.Widget, actual: canvas.Widget) !void {
@@ -1185,6 +1185,7 @@ const Checker = struct {
1185
1185
  fn checkElement(self: *Checker, node: markup.MarkupNode) CheckErr!void {
1186
1186
  if (std.mem.eql(u8, node.name, "span")) return self.checkSpan(node);
1187
1187
  if (std.mem.eql(u8, node.name, "markdown")) return self.checkMarkdown(node);
1188
+ if (std.mem.eql(u8, node.name, "code")) return self.checkCode(node);
1188
1189
  if (std.mem.eql(u8, node.name, "stepper")) return self.checkStepper(node);
1189
1190
  if (std.mem.eql(u8, node.name, "timeline-item")) return self.checkTimelineItem(node);
1190
1191
  if (std.mem.eql(u8, node.name, "chart")) return self.checkChart(node);
@@ -1338,6 +1339,46 @@ const Checker = struct {
1338
1339
  }
1339
1340
  }
1340
1341
 
1342
+ fn checkCode(self: *Checker, node: markup.MarkupNode) CheckErr!void {
1343
+ for (node.attrs) |attribute| {
1344
+ if (std.mem.eql(u8, attribute.name, "source")) {
1345
+ const expression = markup.parseAttrExpression(attribute.value) orelse continue;
1346
+ if (expression != .binding) continue;
1347
+ const resolved = try self.resolveBinding(node, expression.binding, true);
1348
+ try self.requireAttrKind(node, attribute, resolved.kind, &.{.string}, markup.code_source_message);
1349
+ continue;
1350
+ }
1351
+ if (std.mem.eql(u8, attribute.name, "line-numbers") or
1352
+ std.mem.eql(u8, attribute.name, "wrap") or
1353
+ std.mem.eql(u8, attribute.name, "editable"))
1354
+ {
1355
+ if (attribute.value.len == 0) continue;
1356
+ _ = try self.attrKind(node, attribute, attribute.value);
1357
+ continue;
1358
+ }
1359
+ if (std.mem.eql(u8, attribute.name, "on-input")) {
1360
+ try self.checkMessageAttr(node, attribute);
1361
+ continue;
1362
+ }
1363
+ if (std.mem.eql(u8, attribute.name, "width") or
1364
+ std.mem.eql(u8, attribute.name, "height") or
1365
+ std.mem.eql(u8, attribute.name, "min-width") or
1366
+ std.mem.eql(u8, attribute.name, "grow"))
1367
+ {
1368
+ try self.checkClassAttr(node, attribute, .number);
1369
+ continue;
1370
+ }
1371
+ if (std.mem.eql(u8, attribute.name, "key") or std.mem.eql(u8, attribute.name, "global-key")) {
1372
+ try self.checkKeyAttr(node, attribute);
1373
+ continue;
1374
+ }
1375
+ if (std.mem.eql(u8, attribute.name, "label")) {
1376
+ const kind = try self.attrKind(node, attribute, attribute.value);
1377
+ try self.requireAttrKind(node, attribute, kind, &.{.string}, label_attr_message);
1378
+ }
1379
+ }
1380
+ }
1381
+
1341
1382
  fn checkStepper(self: *Checker, node: markup.MarkupNode) CheckErr!void {
1342
1383
  for (node.attrs) |attribute| {
1343
1384
  if (std.mem.eql(u8, attribute.name, "active")) {
@@ -213,6 +213,9 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
213
213
  if (std.mem.eql(u8, node.name, "markdown")) {
214
214
  return self.buildMarkdown(ui, scope, node);
215
215
  }
216
+ if (std.mem.eql(u8, node.name, "code")) {
217
+ return self.buildCode(ui, scope, node);
218
+ }
216
219
  if (std.mem.eql(u8, node.name, "stepper")) {
217
220
  return self.buildStepper(ui, scope, node);
218
221
  }
@@ -798,6 +801,90 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
798
801
  return Md.view(ui, source_value, options);
799
802
  }
800
803
 
804
+ // ----------------------------------------------------------- code
805
+
806
+ fn buildCode(self: *Self, ui: *Ui, scope: *Scope, node: markup.MarkupNode) BuildError!Ui.Node {
807
+ if (node.children.len != 0) return self.failNode(node.children[0], markup.code_children_message);
808
+ var options: Ui.CodeOptions = .{};
809
+ var source_text: ?[]const u8 = null;
810
+ for (node.attrs) |attribute| {
811
+ if (std.mem.eql(u8, attribute.name, "kind")) continue;
812
+ if (std.mem.eql(u8, attribute.name, "source")) {
813
+ const typed = markup.attrTyped(attribute);
814
+ if (typed != .binding) return self.failNode(node, markup.code_source_message);
815
+ source_text = switch (try self.evalBinding(scope, node, typed.binding, true)) {
816
+ .string => |text| text,
817
+ else => return self.failNode(node, markup.code_source_message),
818
+ };
819
+ continue;
820
+ }
821
+ if (std.mem.eql(u8, attribute.name, "language")) {
822
+ const typed = markup.attrTyped(attribute);
823
+ if (typed != .literal) return self.failNode(node, markup.code_language_message);
824
+ if (!canvas.code.isLanguageName(typed.literal)) return self.failNode(node, markup.code_language_message);
825
+ options.language = canvas.code.languageFromName(typed.literal);
826
+ continue;
827
+ }
828
+ if (std.mem.eql(u8, attribute.name, "line-numbers")) {
829
+ options.line_numbers = try self.codeFlagAttr(scope, node, attribute);
830
+ continue;
831
+ }
832
+ if (std.mem.eql(u8, attribute.name, "editable")) {
833
+ options.editable = try self.codeFlagAttr(scope, node, attribute);
834
+ continue;
835
+ }
836
+ if (std.mem.eql(u8, attribute.name, "on-input")) {
837
+ const typed = markup.attrTyped(attribute);
838
+ if (typed != .message) {
839
+ return self.failNode(node, "invalid message expression: on-* takes a Msg tag (\"add\") or tag with one binding payload (\"toggle:{item.id}\")");
840
+ }
841
+ options.on_input = inputConstructor(typed.message.tag) orelse {
842
+ return self.failNode(node, "on-input tag must carry a TextInputEvent payload");
843
+ };
844
+ continue;
845
+ }
846
+ if (std.mem.eql(u8, attribute.name, "wrap")) {
847
+ options.wrap = try self.codeFlagAttr(scope, node, attribute);
848
+ continue;
849
+ }
850
+ if (std.mem.eql(u8, attribute.name, "width")) {
851
+ options.width = try self.floatAttr(scope, node, attribute);
852
+ continue;
853
+ }
854
+ if (std.mem.eql(u8, attribute.name, "height")) {
855
+ options.height = try self.floatAttr(scope, node, attribute);
856
+ continue;
857
+ }
858
+ if (std.mem.eql(u8, attribute.name, "min-width")) {
859
+ options.min_width = try self.floatAttr(scope, node, attribute);
860
+ continue;
861
+ }
862
+ if (std.mem.eql(u8, attribute.name, "grow")) {
863
+ options.grow = try self.floatAttr(scope, node, attribute);
864
+ continue;
865
+ }
866
+ if (std.mem.eql(u8, attribute.name, "key")) {
867
+ options.key = try self.attrKey(scope, node, attribute);
868
+ continue;
869
+ }
870
+ if (std.mem.eql(u8, attribute.name, "global-key")) {
871
+ options.global_key = try self.attrKey(scope, node, attribute);
872
+ continue;
873
+ }
874
+ if (std.mem.eql(u8, attribute.name, "label")) {
875
+ options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
876
+ continue;
877
+ }
878
+ return self.failNode(node, markup.code_attr_message);
879
+ }
880
+ return ui.code(options, source_text orelse return self.failNode(node, markup.code_source_message));
881
+ }
882
+
883
+ fn codeFlagAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!bool {
884
+ if (attribute.value.len == 0) return true;
885
+ return (try self.evalAttrExpression(scope, node, attribute)).truthy();
886
+ }
887
+
801
888
  // ------------------------------------------------ stepper/timeline
802
889
 
803
890
  /// `<stepper active="{stage_index}"><step>Work</step>...</stepper>`:
@@ -1963,6 +2050,8 @@ pub fn MarkupView(comptime ModelT: type, comptime MsgT: type) type {
1963
2050
  const msg = try self.constructMessage(scope, node, expression);
1964
2051
  if (std.mem.eql(u8, event, "press")) {
1965
2052
  options.on_press = msg;
2053
+ } else if (std.mem.eql(u8, event, "double-press")) {
2054
+ options.on_double_press = msg;
1966
2055
  } else if (std.mem.eql(u8, event, "toggle")) {
1967
2056
  options.on_toggle = msg;
1968
2057
  } else if (std.mem.eql(u8, event, "change")) {
@@ -1,5 +1,7 @@
1
1
  const std = @import("std");
2
+ const geometry = @import("geometry");
2
3
  const canvas = @import("root.zig");
4
+ const code_model = @import("code.zig");
3
5
  const markup_view = @import("ui_markup_view.zig");
4
6
 
5
7
  const testing = std.testing;
@@ -1082,7 +1084,7 @@ pub const picker_markup_source =
1082
1084
  \\ <menu-item on-press="toggle:{open_count}">Alpha</menu-item>
1083
1085
  \\ </dropdown-menu>
1084
1086
  \\ </stack>
1085
- \\ <button on-press="add" on-hold="add">Crumb</button>
1087
+ \\ <button on-press="add" on-double-press="toggle:{open_count}" on-hold="add">Crumb</button>
1086
1088
  \\</column>
1087
1089
  ;
1088
1090
 
@@ -1131,7 +1133,7 @@ test "markup binds the hover pair and makes listeners hover-hittable, never pres
1131
1133
  try testing.expect(!row.children[0].hover_msgs);
1132
1134
  }
1133
1135
 
1134
- test "markup anchors dropdown-menus and binds dismiss and hold handlers" {
1136
+ test "markup anchors dropdown-menus and binds dismiss, hold, and double-press handlers" {
1135
1137
  var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
1136
1138
  defer arena_state.deinit();
1137
1139
  const arena = arena_state.allocator();
@@ -1152,6 +1154,9 @@ test "markup anchors dropdown-menus and binds dismiss and hold handlers" {
1152
1154
 
1153
1155
  const crumb = tree.root.children[1];
1154
1156
  try testing.expect(tree.msgFor(crumb.id, .hold) != null);
1157
+ try testing.expectEqual(Msg.add, tree.msgForPointerClick(crumb.id, .up, 1).?);
1158
+ try testing.expectEqual(@as(u32, 0), tree.msgForPointerClick(crumb.id, .up, 2).?.toggle);
1159
+ try testing.expectEqual(Msg.add, tree.msgForPointerClick(crumb.id, .up, 3).?);
1155
1160
  // A hold handler makes the element pressable, like on-press.
1156
1161
  try testing.expect(crumb.semantics.actions.press);
1157
1162
 
@@ -1926,6 +1931,143 @@ test "markdown misuse is caught by the model-agnostic validator with positions"
1926
1931
  try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
1927
1932
  }
1928
1933
 
1934
+ // ------------------------------------------------------- code component
1935
+
1936
+ pub const CodeMsg = union(enum) {
1937
+ noop,
1938
+ edit: canvas.TextInputEvent,
1939
+ };
1940
+
1941
+ pub const CodeModel = struct {
1942
+ snippet: []const u8 =
1943
+ \\<Accordion defaultValue={["item-1"]}>
1944
+ \\ <AccordionTrigger>Accessible?</AccordionTrigger>
1945
+ \\</Accordion>
1946
+ ,
1947
+ show_lines: bool = true,
1948
+ wrap_code: bool = false,
1949
+ editable_code: bool = false,
1950
+ count: usize = 3,
1951
+ };
1952
+
1953
+ pub const code_markup_source =
1954
+ \\<code source="{snippet}" language="tsx" editable="{editable_code}" on-input="edit" line-numbers="{show_lines}" wrap="{wrap_code}" width="240" label="Example code" />
1955
+ ;
1956
+
1957
+ pub const CodeUi = canvas.Ui(CodeMsg);
1958
+
1959
+ pub fn handCodeView(ui: *CodeUi, model: *const CodeModel) CodeUi.Node {
1960
+ return ui.code(.{
1961
+ .language = .html,
1962
+ .editable = model.editable_code,
1963
+ .on_input = CodeUi.inputMsg(.edit),
1964
+ .line_numbers = model.show_lines,
1965
+ .wrap = model.wrap_code,
1966
+ .width = 240,
1967
+ .semantics = .{ .label = "Example code" },
1968
+ }, model.snippet);
1969
+ }
1970
+
1971
+ test "code markup builds the reusable component with opt-in numbers and horizontal scrolling" {
1972
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
1973
+ defer arena_state.deinit();
1974
+ const arena = arena_state.allocator();
1975
+ const model = CodeModel{};
1976
+ const CodeMarkup = markup_view.MarkupView(CodeModel, CodeMsg);
1977
+
1978
+ var view = try CodeMarkup.init(arena, code_markup_source);
1979
+ var markup_ui = CodeUi.init(arena);
1980
+ const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
1981
+ var hand_ui = CodeUi.init(arena);
1982
+ const hand_tree = try hand_ui.finalize(handCodeView(&hand_ui, &model));
1983
+
1984
+ var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
1985
+ defer markup_ids.deinit(testing.allocator);
1986
+ var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
1987
+ defer hand_ids.deinit(testing.allocator);
1988
+ try collectIds(markup_tree.root, &markup_ids, testing.allocator);
1989
+ try collectIds(hand_tree.root, &hand_ids, testing.allocator);
1990
+ try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
1991
+ try testing.expectEqual(canvas.WidgetKind.column, markup_tree.root.kind);
1992
+ try testing.expectEqual(geometry.InsetsF{}, markup_tree.root.layout.padding);
1993
+ try testing.expect(markup_tree.root.style.background == null);
1994
+ try testing.expect(markup_tree.root.style.border == null);
1995
+ try testing.expect(markup_tree.root.style.radius == null);
1996
+ try testing.expectEqual(canvas.ScrollAxes.horizontal, findByKind(markup_tree.root, .scroll_view).?.scroll_axes);
1997
+ const source = findByText(markup_tree.root, .text, model.snippet).?;
1998
+ try testing.expectEqual(@as(u8, 1), source.code_line_number_digits);
1999
+ try testing.expectEqualStrings("Example code", markup_tree.root.semantics.label);
2000
+
2001
+ var editable_model = model;
2002
+ editable_model.editable_code = true;
2003
+ var editable_ui = CodeUi.init(arena);
2004
+ const editable_tree = try editable_ui.finalize(try view.build(&editable_ui, &editable_model));
2005
+ try testing.expectEqual(canvas.WidgetKind.textarea, editable_tree.root.kind);
2006
+ try testing.expect(editable_tree.root.code_editor);
2007
+ try testing.expectEqual(@as(usize, 1), editable_tree.root.spans.len);
2008
+ try testing.expectEqual(code_model.Language.tsx, editable_tree.root.code_language);
2009
+ try testing.expect(editable_tree.root.text_no_wrap);
2010
+ try testing.expect(canvas.widgetScrollsAxis(editable_tree.root, .horizontal));
2011
+ try testing.expect(canvas.widgetScrollsAxis(editable_tree.root, .vertical));
2012
+ try testing.expectEqual(geometry.InsetsF{}, editable_tree.root.layout.padding);
2013
+ try testing.expect(editable_tree.root.style.background == null);
2014
+ try testing.expect(editable_tree.root.style.border == null);
2015
+ try testing.expect(editable_tree.root.style.radius == null);
2016
+ try testing.expect(editable_tree.root.semantics.actions.set_text);
2017
+ const edit = editable_tree.msgForTextEdit(editable_tree.root.id, .{ .insert_text = "x" }).?;
2018
+ try testing.expectEqualStrings("x", edit.edit.insert_text);
2019
+ }
2020
+
2021
+ test "code markup accepts lexer aliases from the shared language registry" {
2022
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
2023
+ defer arena_state.deinit();
2024
+ const arena = arena_state.allocator();
2025
+ const model = CodeModel{};
2026
+ const CodeMarkup = markup_view.MarkupView(CodeModel, CodeMsg);
2027
+ const cases = [_][]const u8{
2028
+ "<code source=\"{snippet}\" language=\"mjs\" />",
2029
+ "<code source=\"{snippet}\" language=\"yaml\" />",
2030
+ "<code source=\"{snippet}\" language=\"yml\" />",
2031
+ };
2032
+
2033
+ for (cases) |source| {
2034
+ var parser = canvas.ui_markup.Parser.init(arena, source);
2035
+ try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
2036
+
2037
+ var view = try CodeMarkup.init(arena, source);
2038
+ var ui = CodeUi.init(arena);
2039
+ _ = try ui.finalize(try view.build(&ui, &model));
2040
+ }
2041
+ }
2042
+
2043
+ test "code markup misuse reports the component's closed contract" {
2044
+ var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
2045
+ defer arena_state.deinit();
2046
+ const arena = arena_state.allocator();
2047
+ const model = CodeModel{};
2048
+ const CodeMarkup = markup_view.MarkupView(CodeModel, CodeMsg);
2049
+ const cases = [_]struct { source: []const u8, message: []const u8, model_agnostic: bool = true }{
2050
+ .{ .source = "<code language=\"zig\" />", .message = canvas.ui_markup.code_source_message },
2051
+ .{ .source = "<code source=\"literal\" />", .message = canvas.ui_markup.code_source_message },
2052
+ .{ .source = "<code source=\"{count}\" />", .message = canvas.ui_markup.code_source_message, .model_agnostic = false },
2053
+ .{ .source = "<code source=\"{snippet}\" language=\"brainwave\" />", .message = canvas.ui_markup.code_language_message },
2054
+ .{ .source = "<code source=\"{snippet}\" padding=\"8\" />", .message = canvas.ui_markup.code_attr_message },
2055
+ .{ .source = "<code source=\"{snippet}\">text</code>", .message = canvas.ui_markup.code_children_message },
2056
+ };
2057
+ for (cases) |case| {
2058
+ var view = try CodeMarkup.init(arena, case.source);
2059
+ var ui = CodeUi.init(arena);
2060
+ try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
2061
+ try testing.expectEqualStrings(case.message, view.diagnostic.message);
2062
+
2063
+ if (case.model_agnostic) {
2064
+ var parser = canvas.ui_markup.Parser.init(arena, case.source);
2065
+ const info = canvas.ui_markup.validate(try parser.parse()) orelse return error.TestUnexpectedResult;
2066
+ try testing.expectEqualStrings(case.message, info.message);
2067
+ }
2068
+ }
2069
+ }
2070
+
1929
2071
  // -------------------------------------------------- component catalog fixture
1930
2072
 
1931
2073
  pub const CatalogRow = struct {
@@ -3155,6 +3297,7 @@ test "columns off grid and misshapen alignment values fail with teaching message
3155
3297
  pub const Folder = struct {
3156
3298
  id: u32,
3157
3299
  name: []const u8,
3300
+ level: u16 = 1,
3158
3301
  expanded: bool = false,
3159
3302
 
3160
3303
  fn key(folder: *const Folder) canvas.UiKey {
@@ -3183,7 +3326,7 @@ pub const pane_markup_source =
3183
3326
  \\<split value="{sidebar_fraction}" on-resize="sidebar_resized">
3184
3327
  \\ <tree label="Folders">
3185
3328
  \\ <for each="folders" key="id" as="f">
3186
- \\ <panel role="treeitem" expanded="{f.expanded}" on-press="select_folder:{f.id}" on-toggle="toggle_folder:{f.id}" label="{f.name}">
3329
+ \\ <panel role="treeitem" tree-level="{f.level}" expanded="{f.expanded}" on-press="select_folder:{f.id}" on-change="select_folder:{f.id}" on-toggle="toggle_folder:{f.id}" label="{f.name}">
3187
3330
  \\ <text>{f.name}</text>
3188
3331
  \\ </panel>
3189
3332
  \\ </for>
@@ -3206,7 +3349,9 @@ pub fn handPaneView(ui: *PaneUi, model: *const PaneModel) PaneUi.Node {
3206
3349
  fn folderRow(ui: *PaneUi, folder: *const Folder) PaneUi.Node {
3207
3350
  return ui.panel(.{
3208
3351
  .expanded = folder.expanded,
3352
+ .tree_level = folder.level,
3209
3353
  .on_press = PaneMsg{ .select_folder = folder.id },
3354
+ .on_change = PaneMsg{ .select_folder = folder.id },
3210
3355
  .on_toggle = PaneMsg{ .toggle_folder = folder.id },
3211
3356
  .semantics = .{ .role = .treeitem, .label = folder.name },
3212
3357
  }, .{
@@ -3247,8 +3392,14 @@ test "markup split and tree build the hand-written view with the divider synthes
3247
3392
  // and both the press (selection) and toggle (disclosure) handlers.
3248
3393
  const row = findByKind(markup_tree.root, .panel).?;
3249
3394
  try testing.expectEqual(canvas.WidgetRole.treeitem, row.semantics.role);
3395
+ try testing.expectEqual(@as(u16, 1), row.tree_level);
3250
3396
  try testing.expectEqual(@as(?bool, true), row.state.expanded);
3251
3397
  try testing.expectEqual(@as(u32, 1), markup_tree.msgForPointer(row.id, .up).?.select_folder);
3398
+ try testing.expectEqual(@as(u32, 1), markup_tree.msgForKeyboard(row.id, .{
3399
+ .phase = .key_down,
3400
+ .key = "arrowdown",
3401
+ .focus_moved = true,
3402
+ }).?.select_folder);
3252
3403
  try testing.expectEqual(@as(u32, 1), markup_tree.msgFor(row.id, .toggle).?.toggle_folder);
3253
3404
 
3254
3405
  // min-width lands as a floor only (no definite max).
@@ -353,6 +353,11 @@ pub const elements = [_]ElementInfo{
353
353
  // owns the keyboard, so it must carry a name — the runtime then
354
354
  // announces the live screen text as its content.
355
355
  .{ .code = 69, .name = "terminal", .widget_kind = "terminal", .a11y_name = .control },
356
+ // The reusable code surface: a source-bound composite that lowers
357
+ // through `Ui.code` to a highlighted panel, optionally with logical
358
+ // line numbers or one horizontal scroll region for unwrapped lines.
359
+ // Markdown fences use the same builder component.
360
+ .{ .code = 70, .name = "code", .rule_hook = "code", .hit_target = false },
356
361
  };
357
362
 
358
363
  // ------------------------------------------------------------- attributes
@@ -553,6 +558,19 @@ pub const attrs = [_]AttrInfo{
553
558
  // back here and user scrollback survives rebuilds; move it
554
559
  // model-side to scroll programmatically. 0 is pinned to the bottom.
555
560
  .{ .code = 89, .name = "scrollback", .class = .whole, .group = .option, .field = "scrollback" },
561
+ // Code-surface declarations (the <code> composite; its rule hook
562
+ // owns the closed set). Language is a literal lexer name and line
563
+ // numbers are opt-in; wrapping reuses generic attr code 16.
564
+ .{ .code = 90, .name = "language", .class = .option, .group = .composite },
565
+ .{ .code = 91, .name = "line-numbers", .class = .flag, .group = .composite },
566
+ // Editable code is a composite-only opt-in: the default code surface
567
+ // remains selectable/read-only, while `editable` lowers it through
568
+ // the textarea behavior with code-specific bare rendering.
569
+ .{ .code = 92, .name = "editable", .class = .flag, .group = .composite },
570
+ // Flat loop-rendered tree rows can declare their one-based logical
571
+ // hierarchy without manufacturing nested layout containers. Zero (the
572
+ // default when absent) keeps structural widget nesting as the source.
573
+ .{ .code = 93, .name = "tree-level", .class = .whole, .group = .option, .field = "tree_level" },
556
574
  };
557
575
 
558
576
  // ----------------------------------------------------------------- events
@@ -586,6 +604,11 @@ pub const events = [_]EventInfo{
586
604
  // `scrollback` back (the on-scroll reconcile shape) and render
587
605
  // honest status. Emulator internals never ride it.
588
606
  .{ .code = 13, .name = "terminal", .payload = .terminal_state, .only_on_element = "terminal" },
607
+ // Double press is the second half of the desktop select-then-act
608
+ // gesture. The first release still dispatches `press`; a release
609
+ // whose runtime-derived click count reached two dispatches this
610
+ // channel in place of a second press.
611
+ .{ .code = 14, .name = "double-press" },
589
612
  };
590
613
 
591
614
  // ------------------------------------------------------- token vocabulary
@@ -597,12 +620,13 @@ pub const events = [_]EventInfo{
597
620
  // tests in ui_markup_view_tests.zig hold them equal to the live structs).
598
621
 
599
622
  pub const color_token_names = [_][]const u8{
600
- "background", "surface", "surface_subtle", "surface_pressed",
601
- "text", "text_muted", "border", "accent",
602
- "accent_text", "destructive", "destructive_text", "success",
603
- "success_text", "warning", "warning_text", "info",
604
- "info_text", "focus_ring", "shadow", "scrim",
605
- "disabled",
623
+ "background", "surface", "surface_subtle", "surface_pressed",
624
+ "text", "text_muted", "syntax_plain", "syntax_comment",
625
+ "syntax_keyword", "syntax_literal", "syntax_function", "syntax_property",
626
+ "syntax_constant", "border", "accent", "accent_text",
627
+ "destructive", "destructive_text", "success", "success_text",
628
+ "warning", "warning_text", "info", "info_text",
629
+ "focus_ring", "shadow", "scrim", "disabled",
606
630
  };
607
631
 
608
632
  pub const radius_token_names = [_][]const u8{ "sm", "md", "lg", "xl" };
@@ -21,15 +21,15 @@ test "registry codes are stable: assigned at birth, never renumbered or renamed"
21
21
  // (append or slot them anywhere — order carries no meaning) and pin
22
22
  // the new fingerprint ONLY for additions; renames/renumbers are
23
23
  // schema-version-bump events, not silent edits.
24
- try testing.expectEqual(@as(usize, 69), schema.elements.len);
25
- try testing.expectEqual(@as(usize, 89), schema.attrs.len);
26
- try testing.expectEqual(@as(usize, 13), schema.events.len);
24
+ try testing.expectEqual(@as(usize, 70), schema.elements.len);
25
+ try testing.expectEqual(@as(usize, 93), schema.attrs.len);
26
+ try testing.expectEqual(@as(usize, 14), schema.events.len);
27
27
  // The element table runs through the span composite (64), the
28
28
  // bubble-reactions composite (65), the media surface (66), the
29
29
  // runtime-image leaf (67), the video playback composite (68), and
30
- // the terminal leaf (69).
30
+ // the terminal leaf (69), and the reusable code composite (70).
31
31
  try testing.expectEqual(
32
- @as(u64, 0x439b5e520bedc352),
32
+ @as(u64, 0x180108eb2382ba60),
33
33
  tableFingerprint(schema.ElementInfo, &schema.elements),
34
34
  );
35
35
  // The attr table runs through the split layout-tween attributes
@@ -42,16 +42,19 @@ test "registry codes are stable: assigned at birth, never renumbered or renamed"
42
42
  // surface (81), the video element attributes controls (82),
43
43
  // autoplay (83), loop (84), and muted (85), the scroll-axis
44
44
  // attributes axis (86) and value-x (87), and the terminal
45
- // attributes pty (88) and scrollback (89).
45
+ // attributes pty (88) and scrollback (89), and the code language
46
+ // (90), line-numbers (91), and editable-code (92) declarations,
47
+ // plus the flat disclosure-tree hierarchy level tree-level (93).
46
48
  try testing.expectEqual(
47
- @as(u64, 0x60e430e0cf2cc4f9),
49
+ @as(u64, 0x590814a0a23b9935),
48
50
  tableFingerprint(schema.AttrInfo, &schema.attrs),
49
51
  );
50
52
  // The event table runs through the pointer-hover containment pair
51
- // hover-enter (11) and hover-leave (12) and the terminal view-state
52
- // echo terminal (13).
53
+ // hover-enter (11) and hover-leave (12), the terminal view-state
54
+ // echo terminal (13), and the desktop gesture channel
55
+ // double-press (14).
53
56
  try testing.expectEqual(
54
- @as(u64, 0x62d715c2db7951d6),
57
+ @as(u64, 0x0cdda4a2d5a85101),
55
58
  tableFingerprint(schema.EventInfo, &schema.events),
56
59
  );
57
60
  }