@native-sdk/cli 0.6.3 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -9
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/skill-data/native-ui/SKILL.md +17 -2
- package/src/primitives/canvas/code.zig +590 -0
- package/src/primitives/canvas/code_tests.zig +1062 -0
- package/src/primitives/canvas/layout_audit.zig +3 -1
- package/src/primitives/canvas/markdown.zig +13 -11
- package/src/primitives/canvas/markdown_tests.zig +167 -4
- package/src/primitives/canvas/root.zig +4 -2
- package/src/primitives/canvas/tests.zig +1 -0
- package/src/primitives/canvas/text_measure_cache.zig +4 -4
- package/src/primitives/canvas/text_span_tests.zig +254 -0
- package/src/primitives/canvas/text_spans.zig +556 -32
- package/src/primitives/canvas/themes/geist.zig +29 -0
- package/src/primitives/canvas/tokens.zig +72 -2
- package/src/primitives/canvas/ui.zig +304 -5
- package/src/primitives/canvas/ui_markup.zig +59 -2
- package/src/primitives/canvas/ui_markup_compiled.zig +76 -0
- package/src/primitives/canvas/ui_markup_compiled_tests.zig +33 -0
- package/src/primitives/canvas/ui_markup_contract.zig +34 -0
- package/src/primitives/canvas/ui_markup_view.zig +73 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +85 -0
- package/src/primitives/canvas/ui_schema.zig +17 -6
- package/src/primitives/canvas/ui_schema_tests.zig +7 -6
- package/src/primitives/canvas/widget_invalidation.zig +3 -0
- package/src/primitives/canvas/widget_layout.zig +57 -8
- package/src/primitives/canvas/widget_layout_tests.zig +35 -0
- package/src/primitives/canvas/widget_metrics.zig +30 -1
- package/src/primitives/canvas/widget_render.zig +454 -9
- package/src/primitives/canvas/widget_text_select.zig +2 -2
- package/src/primitives/canvas/widgets.zig +13 -0
- package/src/root.zig +1 -0
- package/src/runtime/canvas_widget_clipboard_tests.zig +130 -0
- package/src/runtime/canvas_widget_context_menu.zig +12 -2
- package/src/runtime/canvas_widget_events.zig +7 -1
- package/src/runtime/canvas_widget_runtime.zig +7 -2
- package/src/runtime/canvas_widget_text_tests.zig +57 -0
- package/src/runtime/view.zig +7 -3
- package/src/runtime/view_widget_text.zig +194 -4
|
@@ -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,79 @@ 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, "line-numbers") or
|
|
792
|
+
std.mem.eql(u8, attribute.name, "wrap") or
|
|
793
|
+
std.mem.eql(u8, attribute.name, "width") or
|
|
794
|
+
std.mem.eql(u8, attribute.name, "height") or
|
|
795
|
+
std.mem.eql(u8, attribute.name, "min-width") or
|
|
796
|
+
std.mem.eql(u8, attribute.name, "grow") or
|
|
797
|
+
std.mem.eql(u8, attribute.name, "label") or
|
|
798
|
+
std.mem.eql(u8, attribute.name, "key") or
|
|
799
|
+
std.mem.eql(u8, attribute.name, "global-key");
|
|
800
|
+
if (!known) fail(node, markup.code_attr_message);
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
const source_path = comptime blk: {
|
|
804
|
+
const raw = node.attr("source") orelse fail(node, markup.code_source_message);
|
|
805
|
+
const expression = markup.parseAttrExpression(raw) orelse fail(node, markup.code_source_message);
|
|
806
|
+
if (expression != .binding) fail(node, markup.code_source_message);
|
|
807
|
+
break :blk expression.binding;
|
|
808
|
+
};
|
|
809
|
+
comptime requireVariant(pathVariant(node, entries, source_path, true), &.{.string}, node, markup.code_source_message);
|
|
810
|
+
const source = switch (bindingValue(node, entries, source_path, ui, model, scope, true)) {
|
|
811
|
+
.string => |text| text,
|
|
812
|
+
else => runtimeFail([]const u8, ui),
|
|
813
|
+
};
|
|
814
|
+
|
|
815
|
+
var options: Ui.CodeOptions = .{};
|
|
816
|
+
if (comptime (node.attr("language") != null)) {
|
|
817
|
+
const name = comptime blk: {
|
|
818
|
+
const expression = markup.parseAttrExpression(node.attr("language").?) orelse fail(node, markup.code_language_message);
|
|
819
|
+
if (expression != .literal) fail(node, markup.code_language_message);
|
|
820
|
+
if (!canvas.code.isLanguageName(expression.literal)) fail(node, markup.code_language_message);
|
|
821
|
+
break :blk expression.literal;
|
|
822
|
+
};
|
|
823
|
+
options.language = canvas.code.languageFromName(name);
|
|
824
|
+
}
|
|
825
|
+
if (comptime (node.attr("line-numbers") != null)) {
|
|
826
|
+
options.line_numbers = videoFlagValue(node, entries, comptime node.attr("line-numbers").?, ui, model, scope);
|
|
827
|
+
}
|
|
828
|
+
if (comptime (node.attr("wrap") != null)) {
|
|
829
|
+
options.wrap = videoFlagValue(node, entries, comptime node.attr("wrap").?, ui, model, scope);
|
|
830
|
+
}
|
|
831
|
+
if (comptime (node.attr("width") != null)) {
|
|
832
|
+
options.width = floatAttr(node, entries, comptime node.attr("width").?, ui, model, scope);
|
|
833
|
+
}
|
|
834
|
+
if (comptime (node.attr("height") != null)) {
|
|
835
|
+
options.height = floatAttr(node, entries, comptime node.attr("height").?, ui, model, scope);
|
|
836
|
+
}
|
|
837
|
+
if (comptime (node.attr("min-width") != null)) {
|
|
838
|
+
options.min_width = floatAttr(node, entries, comptime node.attr("min-width").?, ui, model, scope);
|
|
839
|
+
}
|
|
840
|
+
if (comptime (node.attr("grow") != null)) {
|
|
841
|
+
options.grow = floatAttr(node, entries, comptime node.attr("grow").?, ui, model, scope);
|
|
842
|
+
}
|
|
843
|
+
if (comptime (node.attr("label") != null)) {
|
|
844
|
+
options.semantics.label = stringAttr(node, entries, comptime node.attr("label").?, ui, model, scope, "label expects text");
|
|
845
|
+
}
|
|
846
|
+
if (comptime (node.attr("key") != null)) {
|
|
847
|
+
options.key = attrKey(node, entries, comptime node.attr("key").?, ui, model, scope, "keys must be integers or strings");
|
|
848
|
+
}
|
|
849
|
+
if (comptime (node.attr("global-key") != null)) {
|
|
850
|
+
options.global_key = attrKey(node, entries, comptime node.attr("global-key").?, ui, model, scope, "keys must be integers or strings");
|
|
851
|
+
}
|
|
852
|
+
return ui.code(options, source);
|
|
853
|
+
}
|
|
854
|
+
|
|
779
855
|
fn markdownLinkConstructor(comptime node: markup.MarkupNode, comptime raw: []const u8) Ui.LinkMsgFn {
|
|
780
856
|
comptime {
|
|
781
857
|
@setEvalBranchQuota(10_000);
|
|
@@ -1089,6 +1089,39 @@ 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
|
+
|
|
1092
1125
|
// ------------------------------------------- template/use + style parity
|
|
1093
1126
|
|
|
1094
1127
|
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,39 @@ 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 std.mem.eql(u8, attribute.name, "wrap")) {
|
|
1352
|
+
if (attribute.value.len == 0) continue;
|
|
1353
|
+
_ = try self.attrKind(node, attribute, attribute.value);
|
|
1354
|
+
continue;
|
|
1355
|
+
}
|
|
1356
|
+
if (std.mem.eql(u8, attribute.name, "width") or
|
|
1357
|
+
std.mem.eql(u8, attribute.name, "height") or
|
|
1358
|
+
std.mem.eql(u8, attribute.name, "min-width") or
|
|
1359
|
+
std.mem.eql(u8, attribute.name, "grow"))
|
|
1360
|
+
{
|
|
1361
|
+
try self.checkClassAttr(node, attribute, .number);
|
|
1362
|
+
continue;
|
|
1363
|
+
}
|
|
1364
|
+
if (std.mem.eql(u8, attribute.name, "key") or std.mem.eql(u8, attribute.name, "global-key")) {
|
|
1365
|
+
try self.checkKeyAttr(node, attribute);
|
|
1366
|
+
continue;
|
|
1367
|
+
}
|
|
1368
|
+
if (std.mem.eql(u8, attribute.name, "label")) {
|
|
1369
|
+
const kind = try self.attrKind(node, attribute, attribute.value);
|
|
1370
|
+
try self.requireAttrKind(node, attribute, kind, &.{.string}, label_attr_message);
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
}
|
|
1374
|
+
|
|
1341
1375
|
fn checkStepper(self: *Checker, node: markup.MarkupNode) CheckErr!void {
|
|
1342
1376
|
for (node.attrs) |attribute| {
|
|
1343
1377
|
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,76 @@ 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, "wrap")) {
|
|
833
|
+
options.wrap = try self.codeFlagAttr(scope, node, attribute);
|
|
834
|
+
continue;
|
|
835
|
+
}
|
|
836
|
+
if (std.mem.eql(u8, attribute.name, "width")) {
|
|
837
|
+
options.width = try self.floatAttr(scope, node, attribute);
|
|
838
|
+
continue;
|
|
839
|
+
}
|
|
840
|
+
if (std.mem.eql(u8, attribute.name, "height")) {
|
|
841
|
+
options.height = try self.floatAttr(scope, node, attribute);
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
if (std.mem.eql(u8, attribute.name, "min-width")) {
|
|
845
|
+
options.min_width = try self.floatAttr(scope, node, attribute);
|
|
846
|
+
continue;
|
|
847
|
+
}
|
|
848
|
+
if (std.mem.eql(u8, attribute.name, "grow")) {
|
|
849
|
+
options.grow = try self.floatAttr(scope, node, attribute);
|
|
850
|
+
continue;
|
|
851
|
+
}
|
|
852
|
+
if (std.mem.eql(u8, attribute.name, "key")) {
|
|
853
|
+
options.key = try self.attrKey(scope, node, attribute);
|
|
854
|
+
continue;
|
|
855
|
+
}
|
|
856
|
+
if (std.mem.eql(u8, attribute.name, "global-key")) {
|
|
857
|
+
options.global_key = try self.attrKey(scope, node, attribute);
|
|
858
|
+
continue;
|
|
859
|
+
}
|
|
860
|
+
if (std.mem.eql(u8, attribute.name, "label")) {
|
|
861
|
+
options.semantics.label = try self.stringAttr(scope, node, attribute, "label expects text");
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
return self.failNode(node, markup.code_attr_message);
|
|
865
|
+
}
|
|
866
|
+
return ui.code(options, source_text orelse return self.failNode(node, markup.code_source_message));
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
fn codeFlagAttr(self: *Self, scope: *Scope, node: markup.MarkupNode, attribute: markup.MarkupAttr) BuildError!bool {
|
|
870
|
+
if (attribute.value.len == 0) return true;
|
|
871
|
+
return (try self.evalAttrExpression(scope, node, attribute)).truthy();
|
|
872
|
+
}
|
|
873
|
+
|
|
801
874
|
// ------------------------------------------------ stepper/timeline
|
|
802
875
|
|
|
803
876
|
/// `<stepper active="{stage_index}"><step>Work</step>...</stepper>`:
|
|
@@ -1926,6 +1926,91 @@ test "markdown misuse is caught by the model-agnostic validator with positions"
|
|
|
1926
1926
|
try testing.expectEqual(@as(?canvas.ui_markup.MarkupErrorInfo, null), canvas.ui_markup.validate(try parser.parse()));
|
|
1927
1927
|
}
|
|
1928
1928
|
|
|
1929
|
+
// ------------------------------------------------------- code component
|
|
1930
|
+
|
|
1931
|
+
pub const CodeMsg = union(enum) { noop };
|
|
1932
|
+
|
|
1933
|
+
pub const CodeModel = struct {
|
|
1934
|
+
snippet: []const u8 =
|
|
1935
|
+
\\<Accordion defaultValue={["item-1"]}>
|
|
1936
|
+
\\ <AccordionTrigger>Accessible?</AccordionTrigger>
|
|
1937
|
+
\\</Accordion>
|
|
1938
|
+
,
|
|
1939
|
+
show_lines: bool = true,
|
|
1940
|
+
wrap_code: bool = false,
|
|
1941
|
+
count: usize = 3,
|
|
1942
|
+
};
|
|
1943
|
+
|
|
1944
|
+
pub const code_markup_source =
|
|
1945
|
+
\\<code source="{snippet}" language="tsx" line-numbers="{show_lines}" wrap="{wrap_code}" width="240" label="Example code" />
|
|
1946
|
+
;
|
|
1947
|
+
|
|
1948
|
+
pub const CodeUi = canvas.Ui(CodeMsg);
|
|
1949
|
+
|
|
1950
|
+
pub fn handCodeView(ui: *CodeUi, model: *const CodeModel) CodeUi.Node {
|
|
1951
|
+
return ui.code(.{
|
|
1952
|
+
.language = .html,
|
|
1953
|
+
.line_numbers = model.show_lines,
|
|
1954
|
+
.wrap = model.wrap_code,
|
|
1955
|
+
.width = 240,
|
|
1956
|
+
.semantics = .{ .label = "Example code" },
|
|
1957
|
+
}, model.snippet);
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
test "code markup builds the reusable component with opt-in numbers and horizontal scrolling" {
|
|
1961
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1962
|
+
defer arena_state.deinit();
|
|
1963
|
+
const arena = arena_state.allocator();
|
|
1964
|
+
const model = CodeModel{};
|
|
1965
|
+
const CodeMarkup = markup_view.MarkupView(CodeModel, CodeMsg);
|
|
1966
|
+
|
|
1967
|
+
var view = try CodeMarkup.init(arena, code_markup_source);
|
|
1968
|
+
var markup_ui = CodeUi.init(arena);
|
|
1969
|
+
const markup_tree = try markup_ui.finalize(try view.build(&markup_ui, &model));
|
|
1970
|
+
var hand_ui = CodeUi.init(arena);
|
|
1971
|
+
const hand_tree = try hand_ui.finalize(handCodeView(&hand_ui, &model));
|
|
1972
|
+
|
|
1973
|
+
var markup_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
1974
|
+
defer markup_ids.deinit(testing.allocator);
|
|
1975
|
+
var hand_ids: std.ArrayListUnmanaged(canvas.ObjectId) = .empty;
|
|
1976
|
+
defer hand_ids.deinit(testing.allocator);
|
|
1977
|
+
try collectIds(markup_tree.root, &markup_ids, testing.allocator);
|
|
1978
|
+
try collectIds(hand_tree.root, &hand_ids, testing.allocator);
|
|
1979
|
+
try testing.expectEqualSlices(canvas.ObjectId, hand_ids.items, markup_ids.items);
|
|
1980
|
+
try testing.expectEqual(canvas.ScrollAxes.horizontal, findByKind(markup_tree.root, .scroll_view).?.scroll_axes);
|
|
1981
|
+
const source = findByText(markup_tree.root, .text, model.snippet).?;
|
|
1982
|
+
try testing.expectEqual(@as(u8, 1), source.code_line_number_digits);
|
|
1983
|
+
try testing.expectEqualStrings("Example code", markup_tree.root.semantics.label);
|
|
1984
|
+
}
|
|
1985
|
+
|
|
1986
|
+
test "code markup misuse reports the component's closed contract" {
|
|
1987
|
+
var arena_state = std.heap.ArenaAllocator.init(testing.allocator);
|
|
1988
|
+
defer arena_state.deinit();
|
|
1989
|
+
const arena = arena_state.allocator();
|
|
1990
|
+
const model = CodeModel{};
|
|
1991
|
+
const CodeMarkup = markup_view.MarkupView(CodeModel, CodeMsg);
|
|
1992
|
+
const cases = [_]struct { source: []const u8, message: []const u8, model_agnostic: bool = true }{
|
|
1993
|
+
.{ .source = "<code language=\"zig\" />", .message = canvas.ui_markup.code_source_message },
|
|
1994
|
+
.{ .source = "<code source=\"literal\" />", .message = canvas.ui_markup.code_source_message },
|
|
1995
|
+
.{ .source = "<code source=\"{count}\" />", .message = canvas.ui_markup.code_source_message, .model_agnostic = false },
|
|
1996
|
+
.{ .source = "<code source=\"{snippet}\" language=\"brainwave\" />", .message = canvas.ui_markup.code_language_message },
|
|
1997
|
+
.{ .source = "<code source=\"{snippet}\" padding=\"8\" />", .message = canvas.ui_markup.code_attr_message },
|
|
1998
|
+
.{ .source = "<code source=\"{snippet}\">text</code>", .message = canvas.ui_markup.code_children_message },
|
|
1999
|
+
};
|
|
2000
|
+
for (cases) |case| {
|
|
2001
|
+
var view = try CodeMarkup.init(arena, case.source);
|
|
2002
|
+
var ui = CodeUi.init(arena);
|
|
2003
|
+
try testing.expectError(error.MarkupBuild, view.build(&ui, &model));
|
|
2004
|
+
try testing.expectEqualStrings(case.message, view.diagnostic.message);
|
|
2005
|
+
|
|
2006
|
+
if (case.model_agnostic) {
|
|
2007
|
+
var parser = canvas.ui_markup.Parser.init(arena, case.source);
|
|
2008
|
+
const info = canvas.ui_markup.validate(try parser.parse()) orelse return error.TestUnexpectedResult;
|
|
2009
|
+
try testing.expectEqualStrings(case.message, info.message);
|
|
2010
|
+
}
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
|
|
1929
2014
|
// -------------------------------------------------- component catalog fixture
|
|
1930
2015
|
|
|
1931
2016
|
pub const CatalogRow = struct {
|
|
@@ -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,11 @@ 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 },
|
|
556
566
|
};
|
|
557
567
|
|
|
558
568
|
// ----------------------------------------------------------------- events
|
|
@@ -597,12 +607,13 @@ pub const events = [_]EventInfo{
|
|
|
597
607
|
// tests in ui_markup_view_tests.zig hold them equal to the live structs).
|
|
598
608
|
|
|
599
609
|
pub const color_token_names = [_][]const u8{
|
|
600
|
-
"background",
|
|
601
|
-
"text",
|
|
602
|
-
"
|
|
603
|
-
"
|
|
604
|
-
"
|
|
605
|
-
"
|
|
610
|
+
"background", "surface", "surface_subtle", "surface_pressed",
|
|
611
|
+
"text", "text_muted", "syntax_plain", "syntax_comment",
|
|
612
|
+
"syntax_keyword", "syntax_literal", "syntax_function", "syntax_property",
|
|
613
|
+
"syntax_constant", "border", "accent", "accent_text",
|
|
614
|
+
"destructive", "destructive_text", "success", "success_text",
|
|
615
|
+
"warning", "warning_text", "info", "info_text",
|
|
616
|
+
"focus_ring", "shadow", "scrim", "disabled",
|
|
606
617
|
};
|
|
607
618
|
|
|
608
619
|
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,
|
|
25
|
-
try testing.expectEqual(@as(usize,
|
|
24
|
+
try testing.expectEqual(@as(usize, 70), schema.elements.len);
|
|
25
|
+
try testing.expectEqual(@as(usize, 91), schema.attrs.len);
|
|
26
26
|
try testing.expectEqual(@as(usize, 13), 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,
|
|
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,9 +42,10 @@ 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) and line-numbers (91) declarations.
|
|
46
47
|
try testing.expectEqual(
|
|
47
|
-
@as(u64,
|
|
48
|
+
@as(u64, 0x8c9311ac81d2a800),
|
|
48
49
|
tableFingerprint(schema.AttrInfo, &schema.attrs),
|
|
49
50
|
);
|
|
50
51
|
// The event table runs through the pointer-hover containment pair
|
|
@@ -265,9 +265,12 @@ fn widgetChange(previous: WidgetLayoutNode, next: WidgetLayoutNode, previous_ind
|
|
|
265
265
|
previous.depth != next.depth or
|
|
266
266
|
previous.parent_index != next.parent_index or
|
|
267
267
|
!rectsEqual(previous.frame, next.frame) or
|
|
268
|
+
previous.widget.code_line_number_digits != next.widget.code_line_number_digits or
|
|
268
269
|
!widgetLayoutStylesEqual(previous.widget.layout, next.widget.layout);
|
|
269
270
|
const content_dirty = !std.mem.eql(u8, previous.widget.text, next.widget.text) or
|
|
270
271
|
!textSpansEqual(previous.widget.spans, next.widget.spans) or
|
|
272
|
+
previous.widget.static_text_group_id != next.widget.static_text_group_id or
|
|
273
|
+
previous.widget.static_text_group_offset != next.widget.static_text_group_offset or
|
|
271
274
|
!chartDataEqual(previous.widget.chart, next.widget.chart) or
|
|
272
275
|
!std.mem.eql(u8, previous.widget.placeholder, next.widget.placeholder) or
|
|
273
276
|
!std.mem.eql(u8, previous.widget.icon, next.widget.icon) or
|
|
@@ -42,6 +42,7 @@ const widgetControlInset = widget_metrics.widgetControlInset;
|
|
|
42
42
|
const widgetSizedDensityValue = widget_metrics.widgetSizedDensityValue;
|
|
43
43
|
const densityValue = widget_metrics.densityValue;
|
|
44
44
|
const widgetControlHeight = widget_metrics.widgetControlHeight;
|
|
45
|
+
const widgetCodeLineNumberGutterWidth = widget_metrics.widgetCodeLineNumberGutterWidth;
|
|
45
46
|
const widgetStatusBarPadding = widget_render.widgetStatusBarPadding;
|
|
46
47
|
const controlStrokeWidth = widget_render.controlStrokeWidth;
|
|
47
48
|
const componentControlVisualTokens = widget_render.componentControlVisualTokens;
|
|
@@ -103,7 +104,7 @@ pub fn layoutWidgetDepth(
|
|
|
103
104
|
.scroll_view => if (widget.layout.virtualized)
|
|
104
105
|
try layoutVirtualVerticalChildren(widget.children, content, index, depth, output, len, widget.value, widget.layout, tokens)
|
|
105
106
|
else
|
|
106
|
-
try layoutScrollChildren(widget.children, content, index, depth, output, len, scrollLayoutOffset(widget), tokens),
|
|
107
|
+
try layoutScrollChildren(widget.children, content, index, depth, output, len, widget.scroll_axes, scrollLayoutOffset(widget), tokens),
|
|
107
108
|
.list => if (widget.layout.virtualized)
|
|
108
109
|
try layoutVirtualVerticalChildren(widget.children, content, index, depth, output, len, widget.value, widget.layout, tokens)
|
|
109
110
|
else
|
|
@@ -825,7 +826,11 @@ fn rowChildWidth(row: Widget, available_width: f32, index: usize, tokens: Design
|
|
|
825
826
|
fn spanParagraphHeight(widget: Widget, width: f32, tokens: DesignTokens) f32 {
|
|
826
827
|
return text_spans_model.textSpansWrappedHeight(
|
|
827
828
|
widget.spans,
|
|
828
|
-
widgetTextSpanLayoutOptions(
|
|
829
|
+
widgetTextSpanLayoutOptions(
|
|
830
|
+
widget,
|
|
831
|
+
tokens,
|
|
832
|
+
@max(0, width - widgetCodeLineNumberGutterWidth(widget, tokens)),
|
|
833
|
+
),
|
|
829
834
|
);
|
|
830
835
|
}
|
|
831
836
|
|
|
@@ -836,7 +841,7 @@ fn spanParagraphHeight(widget: Widget, width: f32, tokens: DesignTokens) f32 {
|
|
|
836
841
|
/// an empty frame (never hit-testable).
|
|
837
842
|
fn layoutTextSpanLinkChildren(
|
|
838
843
|
widget: Widget,
|
|
839
|
-
|
|
844
|
+
raw_content: geometry.RectF,
|
|
840
845
|
parent_index: usize,
|
|
841
846
|
depth: usize,
|
|
842
847
|
output: []WidgetLayoutNode,
|
|
@@ -845,6 +850,12 @@ fn layoutTextSpanLinkChildren(
|
|
|
845
850
|
) Error!void {
|
|
846
851
|
if (widget.children.len == 0) return;
|
|
847
852
|
if (widget.spans.len == 0) return;
|
|
853
|
+
var content = raw_content;
|
|
854
|
+
if (widget.kind == .text) {
|
|
855
|
+
const gutter = @min(content.width, widgetCodeLineNumberGutterWidth(widget, tokens));
|
|
856
|
+
content.x += gutter;
|
|
857
|
+
content.width -= gutter;
|
|
858
|
+
}
|
|
848
859
|
|
|
849
860
|
var runs: [text_spans_model.max_text_span_runs_per_paragraph]text_spans_model.TextSpanRun = undefined;
|
|
850
861
|
const layout = text_spans_model.layoutTextSpans(
|
|
@@ -1051,13 +1062,22 @@ fn layoutScrollChildren(
|
|
|
1051
1062
|
depth: usize,
|
|
1052
1063
|
output: []WidgetLayoutNode,
|
|
1053
1064
|
len: *usize,
|
|
1065
|
+
axes: canvas.ScrollAxes,
|
|
1054
1066
|
scroll_offset: geometry.OffsetF,
|
|
1055
1067
|
tokens: DesignTokens,
|
|
1056
1068
|
) Error!void {
|
|
1057
1069
|
const scrolled_content = content.translate(geometry.OffsetF.init(-scroll_offset.dx, -scroll_offset.dy));
|
|
1058
1070
|
for (children) |child| {
|
|
1059
1071
|
if (child.layout.anchor != null) continue;
|
|
1060
|
-
|
|
1072
|
+
var child_frame = stackChildFrame(scrolled_content, child);
|
|
1073
|
+
// A `both` region must retain the same intrinsic width as a
|
|
1074
|
+
// horizontal-only shelf; otherwise its horizontal axis has no
|
|
1075
|
+
// content range even when its child paints a long no-wrap line.
|
|
1076
|
+
if (axes.scrollsHorizontally() and child.frame.width <= 0) {
|
|
1077
|
+
const intrinsic = intrinsicChildSize(child, tokens, depth + 1);
|
|
1078
|
+
child_frame.width = @max(child_frame.width, intrinsic.width);
|
|
1079
|
+
}
|
|
1080
|
+
_ = try layoutWidgetDepth(child, child_frame, parent_index, depth + 1, output, len, tokens);
|
|
1061
1081
|
}
|
|
1062
1082
|
}
|
|
1063
1083
|
|
|
@@ -1615,8 +1635,11 @@ fn intrinsicWidgetSizeDepth(widget: Widget, tokens: DesignTokens, depth: usize)
|
|
|
1615
1635
|
.dialog, .drawer, .sheet => intrinsicModalSurfaceWidgetSize(widget, tokens, depth),
|
|
1616
1636
|
// Containers measure their children (matching the stacking axis the
|
|
1617
1637
|
// layout pass uses), bounded by the widget depth cap. Scroll
|
|
1618
|
-
// viewports and virtualized containers stay zero
|
|
1619
|
-
// allowed to overflow the space they're
|
|
1638
|
+
// viewports and virtualized containers stay zero on each axis they
|
|
1639
|
+
// scroll: their content is allowed to overflow the space they're
|
|
1640
|
+
// given. A horizontal-only viewport still reports its child's
|
|
1641
|
+
// height, so an inline code scroller hugs its rows instead of
|
|
1642
|
+
// collapsing to a zero-height clip.
|
|
1620
1643
|
.row, .breadcrumb, .button_group, .pagination, .radio_group, .tabs, .toggle_group => intrinsicAxisChildrenSize(widget, tokens, .horizontal, depth),
|
|
1621
1644
|
.column, .menu_surface, .dropdown_menu, .input_group => intrinsicAxisChildrenSize(widget, tokens, .vertical, depth),
|
|
1622
1645
|
.list, .data_grid, .table => if (widget.layout.virtualized)
|
|
@@ -1640,7 +1663,10 @@ fn intrinsicWidgetSizeDepth(widget: Widget, tokens: DesignTokens, depth: usize)
|
|
|
1640
1663
|
// Terminals join them: the grid derives its cols/rows FROM the
|
|
1641
1664
|
// space it is given (the runtime resizes the pty to fit), so
|
|
1642
1665
|
// reporting an intrinsic size would invert the contract.
|
|
1643
|
-
.scroll_view
|
|
1666
|
+
.scroll_view => if (!widget.layout.virtualized and widget.scroll_axes == .horizontal) blk: {
|
|
1667
|
+
break :blk intrinsicHorizontalScrollSize(widget, tokens, depth);
|
|
1668
|
+
} else geometry.SizeF.zero(),
|
|
1669
|
+
.image, .split, .media_surface, .terminal => geometry.SizeF.zero(),
|
|
1644
1670
|
};
|
|
1645
1671
|
}
|
|
1646
1672
|
|
|
@@ -1723,6 +1749,28 @@ fn intrinsicOverlayChildrenSize(widget: Widget, tokens: DesignTokens, depth: usi
|
|
|
1723
1749
|
return paddedIntrinsicSize(widget, geometry.SizeF.init(width_max, height_max));
|
|
1724
1750
|
}
|
|
1725
1751
|
|
|
1752
|
+
/// A horizontal viewport stays width-neutral but hugs the full vertical
|
|
1753
|
+
/// extent of its unwrapped content. Span paragraphs report a one-line
|
|
1754
|
+
/// intrinsic height, so measure each child through the width-aware seam at
|
|
1755
|
+
/// its natural width; explicit newlines then contribute every painted row.
|
|
1756
|
+
fn intrinsicHorizontalScrollSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
|
|
1757
|
+
if (depth >= max_widget_depth or widget.children.len == 0) {
|
|
1758
|
+
return geometry.SizeF.init(0, intrinsicOwnMinSize(widget).height);
|
|
1759
|
+
}
|
|
1760
|
+
var height_max: f32 = 0;
|
|
1761
|
+
for (widget.children) |child| {
|
|
1762
|
+
if (child.layout.anchor != null) continue;
|
|
1763
|
+
const intrinsic = intrinsicChildSize(child, tokens, depth + 1);
|
|
1764
|
+
const child_width = if (child.frame.width > 0) child.frame.width else intrinsic.width;
|
|
1765
|
+
height_max = @max(
|
|
1766
|
+
height_max,
|
|
1767
|
+
wrappedVerticalExtentForWidth(child, child_width, tokens, depth + 1),
|
|
1768
|
+
);
|
|
1769
|
+
}
|
|
1770
|
+
const padded = paddedIntrinsicSize(widget, geometry.SizeF.init(0, height_max));
|
|
1771
|
+
return geometry.SizeF.init(0, padded.height);
|
|
1772
|
+
}
|
|
1773
|
+
|
|
1726
1774
|
fn intrinsicGridChildrenSize(widget: Widget, tokens: DesignTokens, depth: usize) geometry.SizeF {
|
|
1727
1775
|
if (depth >= max_widget_depth or widget.children.len == 0) return intrinsicOwnMinSize(widget);
|
|
1728
1776
|
var cell_width: f32 = 0;
|
|
@@ -1761,7 +1809,8 @@ fn intrinsicTextWidgetSize(widget: Widget, tokens: DesignTokens, text_size: f32)
|
|
|
1761
1809
|
if (widgetIsSpanParagraph(widget)) {
|
|
1762
1810
|
const options = widgetTextSpanLayoutOptions(widget, tokens, 0);
|
|
1763
1811
|
return geometry.SizeF.init(
|
|
1764
|
-
text_spans_model.textSpansIntrinsicWidth(widget.spans, options)
|
|
1812
|
+
text_spans_model.textSpansIntrinsicWidth(widget.spans, options) +
|
|
1813
|
+
widgetCodeLineNumberGutterWidth(widget, tokens),
|
|
1765
1814
|
widgetLineHeight(text_size * text_spans_model.textSpansMaxScale(widget.spans)),
|
|
1766
1815
|
);
|
|
1767
1816
|
}
|
|
@@ -2669,6 +2669,41 @@ fn doubledTextMeasure(context: ?*anyopaque, font_id: FontId, size: f32, text: []
|
|
|
2669
2669
|
|
|
2670
2670
|
const doubled_text_measure = support.TextMeasureProvider{ .measure_fn = doubledTextMeasure };
|
|
2671
2671
|
|
|
2672
|
+
fn countingTextMeasure(context: ?*anyopaque, font_id: FontId, size: f32, text: []const u8) f32 {
|
|
2673
|
+
const calls: *usize = @ptrCast(@alignCast(context.?));
|
|
2674
|
+
calls.* += 1;
|
|
2675
|
+
_ = font_id;
|
|
2676
|
+
return size * @as(f32, @floatFromInt(text.len));
|
|
2677
|
+
}
|
|
2678
|
+
|
|
2679
|
+
test "vertical scroll layout does not intrinsically remeasure its child" {
|
|
2680
|
+
var calls: usize = 0;
|
|
2681
|
+
const measure = support.TextMeasureProvider{
|
|
2682
|
+
.context = &calls,
|
|
2683
|
+
.measure_fn = countingTextMeasure,
|
|
2684
|
+
};
|
|
2685
|
+
const children = [_]Widget{.{
|
|
2686
|
+
.id = 2,
|
|
2687
|
+
.kind = .text,
|
|
2688
|
+
.text = "transcript row",
|
|
2689
|
+
}};
|
|
2690
|
+
const scroll = Widget{
|
|
2691
|
+
.id = 1,
|
|
2692
|
+
.kind = .scroll_view,
|
|
2693
|
+
.scroll_axes = .vertical,
|
|
2694
|
+
.children = &children,
|
|
2695
|
+
};
|
|
2696
|
+
|
|
2697
|
+
var nodes: [2]WidgetLayoutNode = undefined;
|
|
2698
|
+
_ = try layoutWidgetTreeWithTokens(
|
|
2699
|
+
scroll,
|
|
2700
|
+
geometry.RectF.init(0, 0, 240, 120),
|
|
2701
|
+
.{ .text_measure = &measure },
|
|
2702
|
+
&nodes,
|
|
2703
|
+
);
|
|
2704
|
+
try std.testing.expectEqual(@as(usize, 0), calls);
|
|
2705
|
+
}
|
|
2706
|
+
|
|
2672
2707
|
test "intrinsic text sizing defaults to the estimator and honors an injected provider" {
|
|
2673
2708
|
const widget = Widget{ .id = 1, .kind = .text, .text = "Refresh dashboard" };
|
|
2674
2709
|
const default_tokens = DesignTokens{};
|