@native-sdk/cli 0.7.0 → 0.7.2

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 (81) hide show
  1. package/build/app.zig +8 -0
  2. package/build.zig +114 -103
  3. package/native-sdk.d.ts +6 -3
  4. package/package.json +9 -9
  5. package/packages/core/package-lock.json +2 -2
  6. package/packages/core/package.json +1 -1
  7. package/packages/core/sdk/text.ts +25 -14
  8. package/packages/core/src/checker.ts +39 -0
  9. package/packages/core/src/emitter.ts +125 -1
  10. package/skill-data/native-ui/SKILL.md +11 -7
  11. package/src/platform/macos/appkit_host.m +76 -4
  12. package/src/platform/macos/cef_host.mm +24 -2
  13. package/src/platform/macos/root.zig +13 -0
  14. package/src/platform/types.zig +4 -0
  15. package/src/platform/windows/gpu_surface_renderer.cpp +2280 -0
  16. package/src/platform/windows/gpu_surface_renderer.h +86 -0
  17. package/src/platform/windows/root.zig +298 -20
  18. package/src/platform/windows/webview2_host.cpp +343 -33
  19. package/src/primitives/canvas/code.zig +501 -33
  20. package/src/primitives/canvas/code_tests.zig +1029 -6
  21. package/src/primitives/canvas/events.zig +111 -1
  22. package/src/primitives/canvas/frame.zig +45 -1
  23. package/src/primitives/canvas/gpu.zig +6 -0
  24. package/src/primitives/canvas/markdown_tests.zig +17 -18
  25. package/src/primitives/canvas/render_tests.zig +93 -0
  26. package/src/primitives/canvas/root.zig +6 -0
  27. package/src/primitives/canvas/serialization.zig +150 -21
  28. package/src/primitives/canvas/text.zig +6 -0
  29. package/src/primitives/canvas/themes/geist.zig +51 -5
  30. package/src/primitives/canvas/tokens.zig +33 -4
  31. package/src/primitives/canvas/ui.zig +160 -36
  32. package/src/primitives/canvas/ui_markup.zig +60 -9
  33. package/src/primitives/canvas/ui_markup_compiled.zig +41 -0
  34. package/src/primitives/canvas/ui_markup_compiled_tests.zig +12 -0
  35. package/src/primitives/canvas/ui_markup_contract.zig +17 -1
  36. package/src/primitives/canvas/ui_markup_view.zig +44 -0
  37. package/src/primitives/canvas/ui_markup_view_tests.zig +80 -6
  38. package/src/primitives/canvas/ui_schema.zig +18 -0
  39. package/src/primitives/canvas/ui_schema_tests.zig +10 -7
  40. package/src/primitives/canvas/ui_tests.zig +40 -0
  41. package/src/primitives/canvas/widget_builtin_tests.zig +513 -13
  42. package/src/primitives/canvas/widget_invalidation.zig +12 -1
  43. package/src/primitives/canvas/widget_layout.zig +260 -21
  44. package/src/primitives/canvas/widget_metrics.zig +47 -6
  45. package/src/primitives/canvas/widget_render.zig +849 -78
  46. package/src/primitives/canvas/widget_render_controls.zig +83 -25
  47. package/src/primitives/canvas/widget_render_style.zig +52 -8
  48. package/src/primitives/canvas/widget_runtime.zig +2 -0
  49. package/src/primitives/canvas/widget_text_input.zig +167 -8
  50. package/src/primitives/canvas/widget_tree.zig +6 -5
  51. package/src/primitives/canvas/widgets.zig +78 -2
  52. package/src/runtime/canvas_budget_edge_tests.zig +3 -2
  53. package/src/runtime/canvas_frame.zig +60 -7
  54. package/src/runtime/canvas_frame_invalidation_tests.zig +80 -0
  55. package/src/runtime/canvas_frame_packet_tests.zig +83 -9
  56. package/src/runtime/canvas_frame_patch_tests.zig +16 -0
  57. package/src/runtime/canvas_images.zig +8 -2
  58. package/src/runtime/canvas_limits.zig +13 -6
  59. package/src/runtime/canvas_widget_clipboard_tests.zig +8 -5
  60. package/src/runtime/canvas_widget_context_menu.zig +7 -2
  61. package/src/runtime/canvas_widget_event_tests.zig +18 -14
  62. package/src/runtime/canvas_widget_events.zig +51 -20
  63. package/src/runtime/canvas_widget_runtime.zig +108 -4
  64. package/src/runtime/canvas_widget_scroll_driver_tests.zig +59 -0
  65. package/src/runtime/canvas_widget_split_tree_tests.zig +100 -0
  66. package/src/runtime/canvas_widget_state.zig +27 -0
  67. package/src/runtime/canvas_widget_text_tests.zig +326 -0
  68. package/src/runtime/core.zig +15 -5
  69. package/src/runtime/effects.zig +6 -0
  70. package/src/runtime/effects_tests.zig +30 -0
  71. package/src/runtime/gpu_surface_events.zig +50 -19
  72. package/src/runtime/media_surface_tests.zig +1 -1
  73. package/src/runtime/runtime_event_tests.zig +74 -0
  74. package/src/runtime/ui_app.zig +10 -8
  75. package/src/runtime/ui_app_tests.zig +67 -0
  76. package/src/runtime/view.zig +60 -16
  77. package/src/runtime/view_canvas.zig +15 -0
  78. package/src/runtime/view_widget_scroll.zig +48 -18
  79. package/src/runtime/view_widget_text.zig +59 -16
  80. package/src/runtime/window_view_runtime.zig +10 -0
  81. package/src/tooling/templates.zig +9 -6
@@ -304,8 +304,8 @@ pub const VirtualWindowRecord = struct {
304
304
 
305
305
  pub const UiHandlerEvent = enum {
306
306
  press,
307
- /// A multi-click press (click_count >= 2 on the release): the
308
- /// double-click channel. Resolution lives in `msgForPointerClick`:
307
+ /// The second release in a multi-click chain: the double-click
308
+ /// channel. Resolution lives in `msgForPointerClick`:
309
309
  /// a double-click release prefers this handler and falls back to
310
310
  /// `.press`, so binding it is strictly additive — the first click
311
311
  /// of every double-click still dispatches the single-press message
@@ -558,6 +558,10 @@ pub fn Ui(comptime Msg: type) type {
558
558
  /// via `on_toggle`). Model-owned: the view renders child
559
559
  /// rows only while expanded.
560
560
  expanded: ?bool = null,
561
+ /// One-based logical level for a flat tree-row sequence
562
+ /// (markup: `tree-level`). Zero derives hierarchy from widget
563
+ /// nesting, preserving the original structural tree contract.
564
+ tree_level: u16 = 0,
561
565
  disabled: bool = false,
562
566
  /// Image resource reference for image-bearing widgets
563
567
  /// (`image`, `icon_button`, `avatar`): a `canvas.ImageId` the
@@ -762,14 +766,14 @@ pub fn Ui(comptime Msg: type) type {
762
766
  /// the live bottom. Meaningless on every other element.
763
767
  scrollback: u32 = 0,
764
768
  on_press: ?Msg = null,
765
- /// Double-click Msg (builder-only): dispatched on a release
766
- /// whose click count reached 2, in place of `on_press` for
767
- /// that release. The FIRST click of the double still
768
- /// dispatches `on_press` on its own release, so the natural
769
- /// pairing is select-on-press + act-on-double-press (a list
770
- /// row that selects on click and opens/plays on double
771
- /// click). Like `on_press`, binding it makes the element a
772
- /// hit target and press claimer.
769
+ /// Double-click Msg (markup: `on-double-press`): dispatched
770
+ /// on a release whose click count reached 2, in place of
771
+ /// `on_press` for that release. The FIRST click of the double
772
+ /// still dispatches `on_press` on its own release, so the
773
+ /// natural pairing is select-on-press + act-on-double-press
774
+ /// (a list row that selects on click and opens/plays on double
775
+ /// click). Like `on_press`, binding it makes the element a hit
776
+ /// target and press claimer.
773
777
  on_double_press: ?Msg = null,
774
778
  on_toggle: ?Msg = null,
775
779
  on_change: ?Msg = null,
@@ -1359,14 +1363,14 @@ pub fn Ui(comptime Msg: type) type {
1359
1363
  }
1360
1364
 
1361
1365
  /// `msgForPointer` with the pointer's click count: a release
1362
- /// whose count reached 2 prefers the widget's `on_double_press`
1366
+ /// whose count is exactly 2 prefers the widget's `on_double_press`
1363
1367
  /// handler and falls back to the ordinary press resolution
1364
1368
  /// (so a double-click on a widget without the double channel
1365
1369
  /// behaves exactly like two single clicks). This is the
1366
1370
  /// dispatcher the runtime pointer path uses; the two-argument
1367
1371
  /// form stays the single-click entry point.
1368
1372
  pub fn msgForPointerClick(self: Tree, target_id: ObjectId, phase: canvas.WidgetPointerPhase, click_count: u8) ?Msg {
1369
- if (phase == .up and click_count >= 2) {
1373
+ if (phase == .up and click_count == 2) {
1370
1374
  if (self.msgFor(target_id, .double_press)) |msg| return msg;
1371
1375
  }
1372
1376
  return self.msgForPointer(target_id, phase);
@@ -1377,6 +1381,14 @@ pub fn Ui(comptime Msg: type) type {
1377
1381
  /// entry widgets.
1378
1382
  pub fn msgForKeyboard(self: Tree, target_id: ObjectId, keyboard: canvas.WidgetKeyboardEvent) ?Msg {
1379
1383
  const widget = self.findWidget(target_id) orelse return null;
1384
+ // Tree focus navigation is selection without activation.
1385
+ // `on_change` gives apps a distinct model message for that
1386
+ // path (a file browser can move its tree cursor without
1387
+ // opening a preview); rows that do not bind it retain the
1388
+ // original selection-follows-focus `on_press` behavior.
1389
+ if (widget.semantics.role == .treeitem and keyboard.focus_moved) {
1390
+ if (self.msgFor(target_id, .change)) |msg| return msg;
1391
+ }
1380
1392
  // A list row prefers a bound submit handler on plain
1381
1393
  // Enter: Enter is the row's PRIMARY action (open the
1382
1394
  // record, play the track — the desktop list convention),
@@ -1410,7 +1422,9 @@ pub fn Ui(comptime Msg: type) type {
1410
1422
  // bytes that the new-input sanitizer would strip.
1411
1423
  if (self.msgForTextEdit(target_id, stamped)) |msg| return msg;
1412
1424
  } else {
1413
- const locally_derived = canvas.widgetKeyboardNewlineTextEditEvent(widget.kind, keyboard) orelse keyboard.textEditEvent();
1425
+ const locally_derived = canvas.widgetCodeTabTextEditEvent(widget, keyboard) orelse
1426
+ canvas.widgetKeyboardNewlineTextEditEvent(widget.kind, keyboard) orelse
1427
+ keyboard.textEditEvent();
1414
1428
  if (locally_derived) |text_edit| {
1415
1429
  // Direct Tree consumers still sanitize locally:
1416
1430
  // these bytes have not crossed the runtime seam.
@@ -1865,11 +1879,12 @@ pub fn Ui(comptime Msg: type) type {
1865
1879
  /// Disclosure-tree container: descendant rows carrying
1866
1880
  /// `role = .treeitem` (at any nesting depth) form one roving
1867
1881
  /// keyboard focus set with the ARIA tree keymap — Up/Down walk
1868
- /// visible rows (selection follows focus through each row's
1869
- /// `on_press`), Left collapses or moves to the parent row,
1882
+ /// visible rows (selection follows focus through `on_change` when
1883
+ /// bound, else `on_press`), Left collapses or moves to the parent row,
1870
1884
  /// Right expands or moves to the first child row, Home/End jump
1871
- /// to the edges. Expansion is model-owned: expandable rows set
1872
- /// `expanded` and bind `on_toggle`.
1885
+ /// to the edges. Flat loop-rendered rows set `tree_level`; nested
1886
+ /// rows derive the same hierarchy structurally. Expansion is
1887
+ /// model-owned: expandable rows set `expanded` and bind `on_toggle`.
1873
1888
  pub fn tree(self: *Self, options: ElementOptions, children: anytype) Node {
1874
1889
  return self.el(.tree, options, children);
1875
1890
  }
@@ -2297,28 +2312,116 @@ pub fn Ui(comptime Msg: type) type {
2297
2312
  key: ?UiKey = null,
2298
2313
  global_key: ?UiKey = null,
2299
2314
  language: code_model.Language = .plain,
2315
+ /// Opt into the engine's multiline editor behavior while
2316
+ /// retaining code highlighting. Read-only is the default.
2317
+ editable: bool = false,
2318
+ /// Receives each edit when `editable` is true. The source is
2319
+ /// controlled in exactly the same way as a textarea value.
2320
+ on_input: ?InputMsgFn = null,
2300
2321
  /// Prefix each logical source line with a muted, monospace
2301
2322
  /// number. Off by default.
2302
2323
  line_numbers: bool = false,
2324
+ /// One-based logical source lines rendered with Geist Code
2325
+ /// Block's green added-line wash and a renderer-owned `+`.
2326
+ added_lines: []const usize = &.{},
2327
+ /// One-based logical source lines rendered with Geist Code
2328
+ /// Block's red removed-line wash and a renderer-owned `-`.
2329
+ removed_lines: []const usize = &.{},
2303
2330
  /// Word-wrap long source lines. `false` keeps logical lines
2304
2331
  /// intact inside one horizontal scroll region.
2305
2332
  wrap: bool = true,
2306
2333
  width: f32 = 0,
2307
- /// Definite surface height. Overflow scrolls vertically;
2308
- /// no-wrap surfaces scroll on both axes.
2334
+ /// Definite code-region height. Overflow scrolls vertically;
2335
+ /// no-wrap regions scroll on both axes.
2309
2336
  height: f32 = 0,
2310
2337
  min_width: f32 = 0,
2311
2338
  grow: f32 = 0,
2312
2339
  semantics: canvas.WidgetSemantics = .{},
2313
2340
  };
2314
2341
 
2315
- /// A themed source-code surface with bounded syntax highlighting.
2342
+ /// Bare source-code content with bounded, themed syntax highlighting.
2343
+ /// The component owns layout, clipping, and scrolling, but deliberately
2344
+ /// supplies no background, border, radius, shadow, or padding. Wrap it
2345
+ /// in a panel or card when those presentation choices are wanted.
2316
2346
  /// Markdown fences lower through this same component.
2317
2347
  pub fn code(self: *Self, options: CodeOptions, source: []const u8) Node {
2318
- const line_count = codeLineCount(source);
2319
- const numbered = options.line_numbers and line_count <= max_code_lines;
2320
- const content = if (numbered)
2321
- self.numberedCodeParagraph(source, options.language, options.wrap, line_count)
2348
+ const line_count = codeLineCount(source, options.editable);
2349
+ const terminal_editor_line = options.editable and source.len > 0 and source[source.len - 1] == '\n';
2350
+ const numbered = options.line_numbers and line_count - @intFromBool(terminal_editor_line) <=
2351
+ (if (options.editable) max_editable_code_lines else max_code_lines);
2352
+ const added_lines = self.codeLineMask(options.added_lines);
2353
+ const removed_lines = self.codeLineMask(options.removed_lines);
2354
+ if (added_lines & removed_lines != 0) self.failed = true;
2355
+ // Read-only presentation stays one bounded paragraph only up to
2356
+ // the same logical-line ceiling as its renderer-owned numbers.
2357
+ // Longer sources keep every byte and omit diff decoration.
2358
+ const diff_bounded = options.editable or line_count <= max_code_lines;
2359
+ const displayed_added_lines = if (diff_bounded) added_lines else 0;
2360
+ const displayed_removed_lines = if (diff_bounded) removed_lines else 0;
2361
+ const decorated = displayed_added_lines != 0 or displayed_removed_lines != 0;
2362
+ const diff_lines: ?canvas.CodeDiffLines = if (decorated) .{
2363
+ .added = displayed_added_lines,
2364
+ .removed = displayed_removed_lines,
2365
+ } else null;
2366
+ if (options.editable) {
2367
+ const retained = if (options.wrap) blk: {
2368
+ // Wrapped editor geometry still uses the bounded span
2369
+ // paragraph path; no-wrap editors use the scalable
2370
+ // viewport tokenizer below.
2371
+ var state: code_model.HighlightState = .{};
2372
+ break :blk self.codeParagraphWithState(
2373
+ source,
2374
+ options.language,
2375
+ true,
2376
+ options.grow,
2377
+ &state,
2378
+ null,
2379
+ );
2380
+ } else blk: {
2381
+ const source_span = [_]canvas.TextSpan{.{
2382
+ .text = source,
2383
+ .monospace = true,
2384
+ .color = .syntax_plain,
2385
+ }};
2386
+ break :blk self.paragraph(
2387
+ .{ .wrap = false, .grow = options.grow },
2388
+ &source_span,
2389
+ );
2390
+ };
2391
+ var editor = self.el(.textarea, .{
2392
+ .key = options.key,
2393
+ .global_key = options.global_key,
2394
+ .width = options.width,
2395
+ .height = options.height,
2396
+ .min_width = options.min_width,
2397
+ .grow = options.grow,
2398
+ .semantics = options.semantics,
2399
+ .on_input = options.on_input,
2400
+ }, .{});
2401
+ // Retain the exact source once. Paint tokenizes visible
2402
+ // logical lines with `code_language`, keeping syntax
2403
+ // highlighting independent of document length.
2404
+ editor.widget.text = retained.widget.text;
2405
+ editor.widget.spans = retained.widget.spans;
2406
+ editor.widget.text_no_wrap = !options.wrap;
2407
+ editor.widget.code_line_number_digits = if (numbered)
2408
+ @intCast(decimalDigits(line_count))
2409
+ else
2410
+ 0;
2411
+ if (diff_lines) |lines| editor.widget.setCodeDiffLines(lines);
2412
+ editor.widget.code_editor = true;
2413
+ editor.widget.code_language = options.language;
2414
+ editor.widget.layout.clip_content = true;
2415
+ return editor;
2416
+ }
2417
+ const content = if (numbered or decorated)
2418
+ self.decoratedCodeParagraph(
2419
+ source,
2420
+ options.language,
2421
+ options.wrap,
2422
+ if (numbered) @intCast(decimalDigits(line_count)) else 0,
2423
+ diff_lines,
2424
+ )
2322
2425
  else
2323
2426
  self.codeParagraphChunks(source, options.language, options.wrap);
2324
2427
  const body = if (options.wrap)
@@ -2347,38 +2450,51 @@ pub fn Ui(comptime Msg: type) type {
2347
2450
  .grow = 1,
2348
2451
  }, .{track});
2349
2452
  };
2350
- var surface = self.el(.panel, .{
2453
+ var root = self.el(.column, .{
2351
2454
  .key = options.key,
2352
2455
  .global_key = options.global_key,
2353
2456
  .width = options.width,
2354
2457
  .height = options.height,
2355
2458
  .min_width = options.min_width,
2356
2459
  .grow = options.grow,
2357
- .padding = 12,
2358
- .style_tokens = .{ .background = .surface_subtle },
2359
2460
  .semantics = options.semantics,
2360
2461
  }, .{body});
2361
- surface.widget.layout.clip_content = true;
2362
- return surface;
2462
+ root.widget.layout.clip_content = true;
2463
+ return root;
2363
2464
  }
2364
2465
 
2365
2466
  /// Keep numbered source in one selectable paragraph. The renderer
2366
2467
  /// owns its muted gutter, so marker digits never enter retained
2367
2468
  /// text or clipboard bytes; it derives each marker baseline from
2368
2469
  /// this paragraph's real wrapped layout.
2369
- fn numberedCodeParagraph(
2470
+ fn decoratedCodeParagraph(
2370
2471
  self: *Self,
2371
2472
  source: []const u8,
2372
2473
  language: code_model.Language,
2373
2474
  wrap: bool,
2374
- line_count: usize,
2475
+ line_number_digits: u8,
2476
+ diff_lines: ?canvas.CodeDiffLines,
2375
2477
  ) Node {
2376
2478
  var state: code_model.HighlightState = .{};
2377
2479
  var source_node = self.codeParagraphWithState(source, language, wrap, 1, &state, null);
2378
- source_node.widget.code_line_number_digits = @intCast(decimalDigits(line_count));
2480
+ source_node.widget.code_line_number_digits = line_number_digits;
2481
+ if (diff_lines) |lines| source_node.widget.setCodeDiffLines(lines);
2379
2482
  return source_node;
2380
2483
  }
2381
2484
 
2485
+ fn codeLineMask(self: *Self, lines: []const usize) u128 {
2486
+ var mask: u128 = 0;
2487
+ for (lines) |line| {
2488
+ if (line == 0 or line > code_model.max_diff_lines) {
2489
+ self.failed = true;
2490
+ continue;
2491
+ }
2492
+ const shift: u7 = @intCast(line - 1);
2493
+ mask |= @as(u128, 1) << shift;
2494
+ }
2495
+ return mask;
2496
+ }
2497
+
2382
2498
  /// Keep ordinary code in one text widget so static selection and copy
2383
2499
  /// span logical lines. Only split when the paragraph layout's bounded
2384
2500
  /// line capacity requires it; every chunk remains independently
@@ -2447,7 +2563,7 @@ pub fn Ui(comptime Msg: type) type {
2447
2563
  budget,
2448
2564
  );
2449
2565
  chunks[chunk_index].static_text_group_fingerprint = group_fingerprint;
2450
- chunks[chunk_index].widget.static_text_group_offset = chunk_start;
2566
+ chunks[chunk_index].widget.static_text_group_offset = @intCast(chunk_start);
2451
2567
  chunk_index += 1;
2452
2568
  chunk_start = chunk_end;
2453
2569
  }
@@ -2492,6 +2608,10 @@ pub fn Ui(comptime Msg: type) type {
2492
2608
  /// Upper bound for formatting renderer-owned logical-line markers.
2493
2609
  /// Sources above it keep every source byte and omit the gutter.
2494
2610
  pub const max_code_lines: usize = 128;
2611
+ /// Editable code paints only its viewport, so its line-number gutter
2612
+ /// can scale far beyond the bounded paragraph layout used by
2613
+ /// read-only snippets.
2614
+ pub const max_editable_code_lines: usize = 10_000;
2495
2615
  pub const max_code_spans_per_surface: usize = 512;
2496
2616
 
2497
2617
  const CodeSpanBudget = struct {
@@ -2566,10 +2686,10 @@ pub fn Ui(comptime Msg: type) type {
2566
2686
  const max_code_logical_lines_per_paragraph: usize =
2567
2687
  canvas.text_spans.max_text_span_lines_per_paragraph;
2568
2688
 
2569
- fn codeLineCount(source: []const u8) usize {
2689
+ fn codeLineCount(source: []const u8, include_terminal_line: bool) usize {
2570
2690
  if (source.len == 0) return 1;
2571
2691
  return std.mem.count(u8, source, "\n") +
2572
- @intFromBool(source[source.len - 1] != '\n');
2692
+ @intFromBool(include_terminal_line or source[source.len - 1] != '\n');
2573
2693
  }
2574
2694
 
2575
2695
  fn decimalDigits(value: usize) usize {
@@ -3494,7 +3614,10 @@ pub fn Ui(comptime Msg: type) type {
3494
3614
  /// default gap. Explicit spacing always wins for its own field.
3495
3615
  fn applyKindDefaultLayout(kind: WidgetKind, options: ElementOptions, layout: *canvas.WidgetLayoutStyle) void {
3496
3616
  const defaults = canvas.widgetKindDefaultLayout(kind, options.size) orelse return;
3497
- if (options.padding == 0) layout.padding = defaults.padding;
3617
+ if (options.padding == 0) {
3618
+ layout.padding = defaults.padding;
3619
+ layout.padding_is_kind_default = true;
3620
+ }
3498
3621
  if (options.gap == 0) layout.gap = defaults.gap;
3499
3622
  if (layout.cross_alignment == .stretch and defaults.cross_alignment != .stretch) {
3500
3623
  layout.cross_alignment = defaults.cross_alignment;
@@ -3520,6 +3643,7 @@ pub fn Ui(comptime Msg: type) type {
3520
3643
  .image_id = options.image,
3521
3644
  .value = options.value,
3522
3645
  .value_x = options.value_x,
3646
+ .tree_level = options.tree_level,
3523
3647
  .terminal = .{ .pty = options.pty, .scrollback = options.scrollback },
3524
3648
  .scroll_axes = options.axis,
3525
3649
  .variant = options.variant,
@@ -1148,10 +1148,11 @@ pub const anchor_offset_value_message = "anchor-offset takes a literal number: t
1148
1148
  pub const anchor_dependent_attr_message = "anchor-alignment and anchor-offset only apply together with anchor - add anchor=\"below\" (or \"above\") to float this surface";
1149
1149
 
1150
1150
  /// Elements whose widget KIND the engine never hit-tests: layout and
1151
- /// decoration only. A bound `on-press`/`on-toggle` makes any element a
1152
- /// hit target (widget-level: the handler stamps the press/toggle action,
1153
- /// and presses on non-interactive content inside it fall through to it),
1154
- /// so those two are legal everywhere; the remaining value/text handlers
1151
+ /// decoration only. A bound `on-press`/`on-double-press`/`on-toggle`
1152
+ /// makes any element a hit target (widget-level: the handler stamps the
1153
+ /// press/toggle action, and presses on non-interactive content inside it
1154
+ /// fall through to it), so those three are legal everywhere; the
1155
+ /// remaining value/text handlers
1155
1156
  /// (`on-change`/`on-submit`/`on-input`) have no behavior to bind to on
1156
1157
  /// these elements and stay validation errors. Registry-derived from the
1157
1158
  /// `hit_target` element predicate, which mirrors the engine's kind
@@ -1173,7 +1174,7 @@ pub fn deadHandlerOnNonHitTarget(attr_name: []const u8) bool {
1173
1174
 
1174
1175
  pub const autofocus_element_message = "autofocus is only supported on focusable controls (text fields, buttons, checkboxes, ...) - it moves keyboard focus to the element when it mounts or when the flag turns on, and nothing about this element can take focus";
1175
1176
 
1176
- pub const non_hit_target_handler_message = "on-change/on-submit/on-input never fire here: this element has no control or text behavior - put them on a control (input, checkbox, slider) inside it (on-press/on-toggle are fine anywhere: a bound press handler makes any element pressable, and clicks on plain text or icons inside it fall through to it)";
1177
+ pub const non_hit_target_handler_message = "on-change/on-submit/on-input never fire here: this element has no control or text behavior - put them on a control (input, checkbox, slider) inside it (on-press/on-double-press/on-toggle are fine anywhere: a bound press handler makes any element pressable, and clicks on plain text or icons inside it fall through to it)";
1177
1178
 
1178
1179
  /// Elements whose widget kind layers its children on top of each other
1179
1180
  /// (every child gets the full content box), so `gap` can never space
@@ -1867,8 +1868,9 @@ pub const markdown_on_details_message = "on-details takes a bare Msg tag whose p
1867
1868
  pub const markdown_details_expanded_message = "details-expanded takes one {binding} naming a []const bool iterable (a model field, pub decl, or fn - the same sources for each accepts)";
1868
1869
  pub const code_source_message = "code requires a source attribute with one {binding} naming the source text (a []const u8 field or fn - arena fns work)";
1869
1870
  pub const code_children_message = "code takes no children or text content - the source binding provides the code";
1870
- pub const code_attr_message = "unknown attribute for code - it takes source, language, line-numbers, wrap, width, height, min-width, grow, key, global-key, and label";
1871
- pub const code_language_message = "language takes a literal lexer name: plain, zig, javascript/js, typescript/ts, jsx/tsx, json, shell/sh/bash/zsh, python/py, rust/rs, c/cpp/c++/csharp/java/kotlin/swift, go, html/xml/svg, css/scss/less, or sql";
1871
+ pub const code_attr_message = "unknown attribute for code - it takes source, language, editable, on-input, line-numbers, added-lines, removed-lines, wrap, width, height, min-width, grow, key, global-key, and label";
1872
+ pub const code_language_message = "language takes a literal lexer name: plain, zig, javascript/js/mjs, typescript/ts, jsx/tsx, json, yaml/yml, shell/sh/bash/zsh, python/py, rust/rs, c/cpp/c++/csharp/java/kotlin/swift, go, html/xml/svg, css/scss/less, sql, or markdown/md";
1873
+ pub const code_diff_lines_message = "added-lines and removed-lines take one-based lines/ranges up to 128 (for example \"2-4, 7\") or one text {binding} producing that form";
1872
1874
  pub const stepper_active_message = "stepper requires an active attribute (a number or one {binding}) naming the active step index";
1873
1875
  pub const stepper_attr_message = "unknown attribute for stepper - it takes active, key, global-key, and label";
1874
1876
  pub const stepper_children_message = "stepper takes only step children (each step is a text leaf: <step>Work</step>)";
@@ -2220,7 +2222,11 @@ fn validateMarkdown(node: MarkupNode) ?MarkupErrorInfo {
2220
2222
  }
2221
2223
 
2222
2224
  fn codeLanguageName(name: []const u8) bool {
2223
- const names = "plain text zig js javascript jsx ts typescript tsx json jsonc sh bash zsh shell py python rs rust c h cc cpp c++ cs csharp java kotlin swift go golang html xml svg css scss less sql";
2225
+ // Keep this tooling-safe vocabulary synchronized with
2226
+ // code.isLanguageName. ui_markup is also compiled as a standalone CLI
2227
+ // module, where importing the complete canvas lexer graph would make its
2228
+ // source files belong to multiple Zig modules.
2229
+ const names = "plain text zig js javascript mjs jsx ts typescript tsx json jsonc yaml yml sh bash zsh shell py python rs rust c h cc cpp c++ cs csharp java kotlin swift go golang html xml svg css scss less sql md markdown";
2224
2230
  var known = std.mem.tokenizeScalar(u8, names, ' ');
2225
2231
  while (known.next()) |candidate| {
2226
2232
  if (std.ascii.eqlIgnoreCase(name, candidate)) return true;
@@ -2228,6 +2234,27 @@ fn codeLanguageName(name: []const u8) bool {
2228
2234
  return false;
2229
2235
  }
2230
2236
 
2237
+ fn codeLineNumberSpec(spec_raw: []const u8) bool {
2238
+ const spec = std.mem.trim(u8, spec_raw, " \t\r\n");
2239
+ if (spec.len == 0) return true;
2240
+ var pieces = std.mem.splitScalar(u8, spec, ',');
2241
+ while (pieces.next()) |piece_raw| {
2242
+ const piece = std.mem.trim(u8, piece_raw, " \t\r\n");
2243
+ if (piece.len == 0) return false;
2244
+ const dash = std.mem.indexOfScalar(u8, piece, '-');
2245
+ const first_text = std.mem.trim(u8, if (dash) |index| piece[0..index] else piece, " \t");
2246
+ const last_text = if (dash) |index| std.mem.trim(u8, piece[index + 1 ..], " \t") else first_text;
2247
+ if (first_text.len == 0 or last_text.len == 0) return false;
2248
+ if (dash) |index| {
2249
+ if (std.mem.indexOfScalar(u8, piece[index + 1 ..], '-') != null) return false;
2250
+ }
2251
+ const first = std.fmt.parseInt(usize, first_text, 10) catch return false;
2252
+ const last = std.fmt.parseInt(usize, last_text, 10) catch return false;
2253
+ if (first == 0 or last < first or last > 128) return false;
2254
+ }
2255
+ return true;
2256
+ }
2257
+
2231
2258
  /// `<code>` is a source-bound leaf lowered through `Ui.code`: syntax
2232
2259
  /// language is static markup, while flags and layout values can bind.
2233
2260
  fn validateCode(node: MarkupNode) ?MarkupErrorInfo {
@@ -2247,13 +2274,37 @@ fn validateCode(node: MarkupNode) ?MarkupErrorInfo {
2247
2274
  }
2248
2275
  continue;
2249
2276
  }
2250
- if (std.mem.eql(u8, attribute.name, "line-numbers") or std.mem.eql(u8, attribute.name, "wrap")) {
2277
+ if (std.mem.eql(u8, attribute.name, "added-lines") or
2278
+ std.mem.eql(u8, attribute.name, "removed-lines"))
2279
+ {
2280
+ if (attrExpressionError(attribute.value, code_diff_lines_message)) |message| {
2281
+ return attrError(node, attribute, message);
2282
+ }
2283
+ const expression = parseAttrExpression(attribute.value);
2284
+ if (expression == null or expression.? == .equals) {
2285
+ return attrError(node, attribute, code_diff_lines_message);
2286
+ }
2287
+ if (expression.? == .literal and !codeLineNumberSpec(expression.?.literal)) {
2288
+ return attrError(node, attribute, code_diff_lines_message);
2289
+ }
2290
+ continue;
2291
+ }
2292
+ if (std.mem.eql(u8, attribute.name, "line-numbers") or
2293
+ std.mem.eql(u8, attribute.name, "wrap") or
2294
+ std.mem.eql(u8, attribute.name, "editable"))
2295
+ {
2251
2296
  if (attribute.value.len == 0) continue;
2252
2297
  if (attrExpressionError(attribute.value, invalid_expression_message)) |message| {
2253
2298
  return attrError(node, attribute, message);
2254
2299
  }
2255
2300
  continue;
2256
2301
  }
2302
+ if (std.mem.eql(u8, attribute.name, "on-input")) {
2303
+ if (parseMessageExpression(attribute.value) == null) {
2304
+ return attrError(node, attribute, "invalid message expression: on-* takes a Msg tag (\"add\") or tag with one binding payload (\"toggle:{item.id}\")");
2305
+ }
2306
+ continue;
2307
+ }
2257
2308
  const known = std.mem.eql(u8, attribute.name, "width") or
2258
2309
  std.mem.eql(u8, attribute.name, "height") or
2259
2310
  std.mem.eql(u8, attribute.name, "min-width") or
@@ -788,7 +788,11 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
788
788
  if (std.mem.eql(u8, attribute.name, "kind")) continue;
789
789
  const known = std.mem.eql(u8, attribute.name, "source") or
790
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
791
793
  std.mem.eql(u8, attribute.name, "line-numbers") or
794
+ std.mem.eql(u8, attribute.name, "added-lines") or
795
+ std.mem.eql(u8, attribute.name, "removed-lines") or
792
796
  std.mem.eql(u8, attribute.name, "wrap") or
793
797
  std.mem.eql(u8, attribute.name, "width") or
794
798
  std.mem.eql(u8, attribute.name, "height") or
@@ -813,6 +817,8 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
813
817
  };
814
818
 
815
819
  var options: Ui.CodeOptions = .{};
820
+ var added_lines_storage: [canvas.code.max_diff_lines]usize = undefined;
821
+ var removed_lines_storage: [canvas.code.max_diff_lines]usize = undefined;
816
822
  if (comptime (node.attr("language") != null)) {
817
823
  const name = comptime blk: {
818
824
  const expression = markup.parseAttrExpression(node.attr("language").?) orelse fail(node, markup.code_language_message);
@@ -825,6 +831,29 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
825
831
  if (comptime (node.attr("line-numbers") != null)) {
826
832
  options.line_numbers = videoFlagValue(node, entries, comptime node.attr("line-numbers").?, ui, model, scope);
827
833
  }
834
+ if (comptime (node.attr("added-lines") != null)) {
835
+ const spec = codeLineSpecAttr(node, entries, comptime node.attr("added-lines").?, ui, model, scope);
836
+ options.added_lines = canvas.code.parseLineNumberSpec(spec, &added_lines_storage) orelse blk: {
837
+ ui.failed = true;
838
+ break :blk added_lines_storage[0..0];
839
+ };
840
+ }
841
+ if (comptime (node.attr("removed-lines") != null)) {
842
+ const spec = codeLineSpecAttr(node, entries, comptime node.attr("removed-lines").?, ui, model, scope);
843
+ options.removed_lines = canvas.code.parseLineNumberSpec(spec, &removed_lines_storage) orelse blk: {
844
+ ui.failed = true;
845
+ break :blk removed_lines_storage[0..0];
846
+ };
847
+ }
848
+ if (comptime (node.attr("editable") != null)) {
849
+ options.editable = videoFlagValue(node, entries, comptime node.attr("editable").?, ui, model, scope);
850
+ }
851
+ if (comptime (node.attr("on-input") != null)) {
852
+ const expression = comptime (markup.parseMessageExpression(node.attr("on-input").?) orelse
853
+ fail(node, "invalid message expression: on-* takes a Msg tag (\"add\") or tag with one binding payload (\"toggle:{item.id}\")"));
854
+ options.on_input = comptime (inputConstructor(expression.tag) orelse
855
+ fail(node, "on-input tag must carry a TextInputEvent payload"));
856
+ }
828
857
  if (comptime (node.attr("wrap") != null)) {
829
858
  options.wrap = videoFlagValue(node, entries, comptime node.attr("wrap").?, ui, model, scope);
830
859
  }
@@ -852,6 +881,16 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
852
881
  return ui.code(options, source);
853
882
  }
854
883
 
884
+ fn codeLineSpecAttr(comptime node: markup.MarkupNode, comptime entries: []const ScopeEntry, comptime raw: []const u8, ui: *Ui, model: *const ModelT, scope: anytype) []const u8 {
885
+ const expression = comptime (markup.parseAttrExpression(raw) orelse fail(node, markup.code_diff_lines_message));
886
+ if (comptime expression == .literal) return expression.literal;
887
+ comptime requireVariant(exprVariant(node, entries, raw), &.{.string}, node, markup.code_diff_lines_message);
888
+ return switch (evalExpr(node, entries, raw, ui, model, scope)) {
889
+ .string => |text| text,
890
+ else => runtimeFail([]const u8, ui),
891
+ };
892
+ }
893
+
855
894
  fn markdownLinkConstructor(comptime node: markup.MarkupNode, comptime raw: []const u8) Ui.LinkMsgFn {
856
895
  comptime {
857
896
  @setEvalBranchQuota(10_000);
@@ -2091,6 +2130,8 @@ fn CompiledMarkupEngine(comptime ModelT: type, comptime MsgT: type, comptime res
2091
2130
  const msg = constructMessage(node, expression, entries, ui, model, scope);
2092
2131
  if (comptime std.mem.eql(u8, event, "press")) {
2093
2132
  options.on_press = msg;
2133
+ } else if (comptime std.mem.eql(u8, event, "double-press")) {
2134
+ options.on_double_press = msg;
2094
2135
  } else if (comptime std.mem.eql(u8, event, "toggle")) {
2095
2136
  options.on_toggle = msg;
2096
2137
  } else if (comptime std.mem.eql(u8, event, "change")) {
@@ -1120,6 +1120,18 @@ test "compiled code element matches the interpreter and Ui.code" {
1120
1120
  const wrapped = try wrapped_ui.finalize(CodeCompiled.build(&wrapped_ui, &model));
1121
1121
  try testing.expect(fixture.findByKind(wrapped.root, .scroll_view) == null);
1122
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);
1123
1135
  }
1124
1136
 
1125
1137
  // ------------------------------------------- template/use + style parity
@@ -1348,11 +1348,27 @@ const Checker = struct {
1348
1348
  try self.requireAttrKind(node, attribute, resolved.kind, &.{.string}, markup.code_source_message);
1349
1349
  continue;
1350
1350
  }
1351
- if (std.mem.eql(u8, attribute.name, "line-numbers") or std.mem.eql(u8, attribute.name, "wrap")) {
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
+ {
1352
1355
  if (attribute.value.len == 0) continue;
1353
1356
  _ = try self.attrKind(node, attribute, attribute.value);
1354
1357
  continue;
1355
1358
  }
1359
+ if (std.mem.eql(u8, attribute.name, "added-lines") or
1360
+ std.mem.eql(u8, attribute.name, "removed-lines"))
1361
+ {
1362
+ const expression = markup.parseAttrExpression(attribute.value) orelse continue;
1363
+ if (expression == .literal) continue;
1364
+ const kind = try self.attrKind(node, attribute, attribute.value);
1365
+ try self.requireAttrKind(node, attribute, kind, &.{.string}, markup.code_diff_lines_message);
1366
+ continue;
1367
+ }
1368
+ if (std.mem.eql(u8, attribute.name, "on-input")) {
1369
+ try self.checkMessageAttr(node, attribute);
1370
+ continue;
1371
+ }
1356
1372
  if (std.mem.eql(u8, attribute.name, "width") or
1357
1373
  std.mem.eql(u8, attribute.name, "height") or
1358
1374
  std.mem.eql(u8, attribute.name, "min-width") or