@native-sdk/cli 0.6.1 → 0.6.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/build.zig +8 -0
  2. package/native-sdk.d.ts +16 -0
  3. package/package.json +9 -9
  4. package/packages/core/package-lock.json +2 -2
  5. package/packages/core/package.json +1 -1
  6. package/packages/core/sdk/text.ts +62 -21
  7. package/src/app_runner/root.zig +19 -0
  8. package/src/platform/linux/gtk_host.c +167 -8
  9. package/src/platform/linux/gtk_host.h +3 -2
  10. package/src/platform/linux/root.zig +198 -9
  11. package/src/platform/macos/appkit_host.h +4 -2
  12. package/src/platform/macos/appkit_host.m +137 -38
  13. package/src/platform/macos/cef_host.mm +46 -11
  14. package/src/platform/macos/root.zig +150 -6
  15. package/src/platform/null_platform.zig +32 -4
  16. package/src/platform/types.zig +31 -4
  17. package/src/platform/windows/root.zig +220 -10
  18. package/src/platform/windows/webview2_host.cpp +330 -31
  19. package/src/primitives/app_manifest/comptime_scene.zig +4 -0
  20. package/src/primitives/app_manifest/types.zig +16 -0
  21. package/src/primitives/canvas/equality.zig +3 -1
  22. package/src/primitives/canvas/events.zig +4 -0
  23. package/src/primitives/canvas/root.zig +11 -0
  24. package/src/primitives/canvas/text.zig +10 -0
  25. package/src/primitives/canvas/text_interaction.zig +164 -22
  26. package/src/primitives/canvas/text_layout.zig +107 -21
  27. package/src/primitives/canvas/text_tests.zig +86 -0
  28. package/src/primitives/canvas/ui.zig +21 -16
  29. package/src/primitives/canvas/ui_markup_reflect.zig +20 -3
  30. package/src/primitives/canvas/ui_markup_view_tests.zig +16 -0
  31. package/src/primitives/canvas/ui_tests.zig +6 -0
  32. package/src/primitives/canvas/widget_render.zig +21 -2
  33. package/src/primitives/canvas/widget_runtime.zig +1 -0
  34. package/src/primitives/canvas/widget_runtime_tests.zig +51 -0
  35. package/src/primitives/canvas/widget_text_input.zig +23 -11
  36. package/src/runtime/api.zig +9 -0
  37. package/src/runtime/bridge_payload.zig +9 -0
  38. package/src/runtime/bridge_responses.zig +2 -0
  39. package/src/runtime/builtin_bridge.zig +10 -0
  40. package/src/runtime/canvas_frame_helpers.zig +1 -0
  41. package/src/runtime/canvas_limits.zig +7 -0
  42. package/src/runtime/canvas_widget_accessibility_tests.zig +27 -1
  43. package/src/runtime/canvas_widget_clipboard_tests.zig +16 -0
  44. package/src/runtime/canvas_widget_display.zig +15 -1
  45. package/src/runtime/canvas_widget_events.zig +43 -19
  46. package/src/runtime/canvas_widget_floating_tests.zig +3 -3
  47. package/src/runtime/canvas_widget_runtime.zig +67 -10
  48. package/src/runtime/canvas_widget_state.zig +1 -0
  49. package/src/runtime/canvas_widget_text_tests.zig +1878 -70
  50. package/src/runtime/effects.zig +5 -6
  51. package/src/runtime/flow.zig +45 -7
  52. package/src/runtime/gpu_surface_events.zig +46 -1
  53. package/src/runtime/runtime_event_tests.zig +9 -6
  54. package/src/runtime/shell_layout.zig +25 -11
  55. package/src/runtime/shell_layout_tests.zig +184 -0
  56. package/src/runtime/shell_view_tests.zig +9 -7
  57. package/src/runtime/state.zig +6 -0
  58. package/src/runtime/ui_app.zig +61 -14
  59. package/src/runtime/ui_app_window_tests.zig +107 -2
  60. package/src/runtime/view.zig +30 -0
  61. package/src/runtime/view_widget_text.zig +1048 -10
  62. package/src/runtime/view_widget_tree.zig +2 -0
  63. package/src/runtime/window_command_bridge_tests.zig +82 -6
  64. package/src/runtime/window_storage.zig +14 -3
  65. package/src/runtime/window_views.zig +12 -11
  66. package/src/tooling/manifest.zig +38 -2
  67. package/src/tooling/raw_manifest.zig +8 -0
  68. package/src/tooling/templates.zig +17 -0
@@ -198,6 +198,8 @@ pub const TextLayoutCachePlanner = text_model.TextLayoutCachePlanner;
198
198
  pub const TextRange = text_model.TextRange;
199
199
  pub const TextSelectionRect = text_model.TextSelectionRect;
200
200
  pub const TextSelection = text_model.TextSelection;
201
+ pub const TextCaretAffinity = text_model.TextCaretAffinity;
202
+ pub const TextCaretPosition = text_model.TextCaretPosition;
201
203
  pub const TextCaretDirection = text_model.TextCaretDirection;
202
204
  pub const TextCaretMove = text_model.TextCaretMove;
203
205
  pub const TextCompositionUpdate = text_model.TextCompositionUpdate;
@@ -661,17 +663,25 @@ pub const layoutWidgetTreeWithTokens = widget_runtime.layoutWidgetTreeWithTokens
661
663
  pub const layoutTextRun = text_model.layoutTextRun;
662
664
  pub const layoutTextRunPlan = text_model.layoutTextRunPlan;
663
665
  pub const layoutTextCaretRect = text_model.layoutTextCaretRect;
666
+ pub const layoutTextCaretRectWithAffinity = text_model.layoutTextCaretRectWithAffinity;
664
667
  pub const textCaretRectForLayout = text_model.textCaretRectForLayout;
668
+ pub const textCaretRectForLayoutWithAffinity = text_model.textCaretRectForLayoutWithAffinity;
665
669
  pub const layoutTextSelectionRects = text_model.layoutTextSelectionRects;
666
670
  pub const textSelectionRectsForLayout = text_model.textSelectionRectsForLayout;
667
671
  pub const layoutTextOffsetForPoint = text_model.layoutTextOffsetForPoint;
672
+ pub const layoutTextCaretPositionForPoint = text_model.layoutTextCaretPositionForPoint;
668
673
  pub const textOffsetForLayoutPoint = text_model.textOffsetForLayoutPoint;
674
+ pub const textCaretPositionForLayoutPoint = text_model.textCaretPositionForLayoutPoint;
669
675
  pub const applyTextInputEvent = text_model.applyTextInputEvent;
670
676
  pub const snapTextOffset = text_model.snapTextOffset;
671
677
  pub const snapTextRange = text_model.snapTextRange;
672
678
  pub const snapTextSelection = text_model.snapTextSelection;
679
+ pub const snapTextCaretPosition = text_model.snapTextCaretPosition;
680
+ pub const snapTextCaretSelection = text_model.snapTextCaretSelection;
673
681
  pub const textWordSelectionAtOffset = text_model.textWordSelectionAtOffset;
674
682
  pub const textLineSelectionAtOffset = text_model.textLineSelectionAtOffset;
683
+ pub const textLineStartOffset = text_model.textLineStartOffset;
684
+ pub const textLineEndOffset = text_model.textLineEndOffset;
675
685
 
676
686
  pub const sampleCanvasRenderAnimations = render_model.sampleCanvasRenderAnimations;
677
687
 
@@ -689,6 +699,7 @@ pub const textSelectionCommandId = widget_runtime.textSelectionCommandId;
689
699
  pub const toggleWidgetKnobTravel = widget_runtime.toggleWidgetKnobTravel;
690
700
  pub const widgetControlAimPoint = widget_runtime.widgetControlAimPoint;
691
701
  pub const textSelectionForWidgetPoint = widget_runtime.textSelectionForWidgetPoint;
702
+ pub const textCaretPositionForWidgetPoint = widget_runtime.textCaretPositionForWidgetPoint;
692
703
  pub const textOffsetForWidgetPoint = widget_runtime.textOffsetForWidgetPoint;
693
704
 
694
705
  // Static text selection (click-drag select + copy in `.text` widgets;
@@ -39,11 +39,15 @@ pub const layoutTextRunPlan = text_layout.layoutTextRunPlan;
39
39
  pub const TextLineIterator = text_layout.TextLineIterator;
40
40
  pub const textBounds = text_layout.textBounds;
41
41
  pub const layoutTextCaretRect = text_layout.layoutTextCaretRect;
42
+ pub const layoutTextCaretRectWithAffinity = text_layout.layoutTextCaretRectWithAffinity;
42
43
  pub const textCaretRectForLayout = text_layout.textCaretRectForLayout;
44
+ pub const textCaretRectForLayoutWithAffinity = text_layout.textCaretRectForLayoutWithAffinity;
43
45
  pub const layoutTextSelectionRects = text_layout.layoutTextSelectionRects;
44
46
  pub const textSelectionRectsForLayout = text_layout.textSelectionRectsForLayout;
45
47
  pub const layoutTextOffsetForPoint = text_layout.layoutTextOffsetForPoint;
48
+ pub const layoutTextCaretPositionForPoint = text_layout.layoutTextCaretPositionForPoint;
46
49
  pub const textOffsetForLayoutPoint = text_layout.textOffsetForLayoutPoint;
50
+ pub const textCaretPositionForLayoutPoint = text_layout.textCaretPositionForLayoutPoint;
47
51
  pub const nextTextLineEnd = text_layout.nextTextLineEnd;
48
52
  pub const textLineRange = text_layout.textLineRange;
49
53
  pub const textLineCaretX = text_layout.textLineCaretX;
@@ -62,6 +66,8 @@ pub const isTextBreakByte = text_layout.isTextBreakByte;
62
66
  pub const TextRange = text_interaction.TextRange;
63
67
  pub const TextSelectionRect = text_interaction.TextSelectionRect;
64
68
  pub const TextSelection = text_interaction.TextSelection;
69
+ pub const TextCaretAffinity = text_interaction.TextCaretAffinity;
70
+ pub const TextCaretPosition = text_interaction.TextCaretPosition;
65
71
  pub const TextCaretDirection = text_interaction.TextCaretDirection;
66
72
  pub const TextCaretMove = text_interaction.TextCaretMove;
67
73
  pub const TextCompositionUpdate = text_interaction.TextCompositionUpdate;
@@ -70,6 +76,8 @@ pub const TextEditState = text_interaction.TextEditState;
70
76
  pub const TextBuffer = text_interaction.TextBuffer;
71
77
  pub const applyTextInputEvent = text_interaction.applyTextInputEvent;
72
78
  pub const snapTextSelection = text_interaction.snapTextSelection;
79
+ pub const snapTextCaretPosition = text_interaction.snapTextCaretPosition;
80
+ pub const snapTextCaretSelection = text_interaction.snapTextCaretSelection;
73
81
  pub const snapTextRange = text_interaction.snapTextRange;
74
82
  pub const previousTextOffset = text_interaction.previousTextOffset;
75
83
  pub const nextTextOffset = text_interaction.nextTextOffset;
@@ -77,6 +85,8 @@ pub const previousTextWordOffset = text_interaction.previousTextWordOffset;
77
85
  pub const nextTextWordOffset = text_interaction.nextTextWordOffset;
78
86
  pub const textWordSelectionAtOffset = text_interaction.textWordSelectionAtOffset;
79
87
  pub const textLineSelectionAtOffset = text_interaction.textLineSelectionAtOffset;
88
+ pub const textLineStartOffset = text_interaction.textLineStartOffset;
89
+ pub const textLineEndOffset = text_interaction.textLineEndOffset;
80
90
  pub const snapTextOffset = text_interaction.snapTextOffset;
81
91
  pub const utf8SequenceLength = text_interaction.utf8SequenceLength;
82
92
  pub const isUtf8ContinuationByte = text_interaction.isUtf8ContinuationByte;
@@ -38,14 +38,37 @@ pub const TextSelectionRect = struct {
38
38
  rect: geometry.RectF = .{},
39
39
  };
40
40
 
41
+ /// Which painted line owns a caret whose byte offset is shared by the
42
+ /// end of one soft-wrapped line and the start of the next.
43
+ pub const TextCaretAffinity = enum {
44
+ upstream,
45
+ downstream,
46
+ };
47
+
48
+ pub const TextCaretPosition = struct {
49
+ offset: usize = 0,
50
+ affinity: TextCaretAffinity = .upstream,
51
+ };
52
+
41
53
  pub const TextSelection = struct {
42
54
  anchor: usize = 0,
43
55
  focus: usize = 0,
56
+ /// Affinity belongs to the focus end. It only changes geometry at a
57
+ /// soft-wrap boundary; ordinary offsets render identically.
58
+ affinity: TextCaretAffinity = .upstream,
44
59
 
45
60
  pub fn collapsed(offset: usize) TextSelection {
46
61
  return .{ .anchor = offset, .focus = offset };
47
62
  }
48
63
 
64
+ pub fn collapsedAt(position: TextCaretPosition) TextSelection {
65
+ return .{
66
+ .anchor = position.offset,
67
+ .focus = position.offset,
68
+ .affinity = position.affinity,
69
+ };
70
+ }
71
+
49
72
  pub fn range(self: TextSelection, text_len: usize) TextRange {
50
73
  return TextRange.init(self.anchor, self.focus).normalized(text_len);
51
74
  }
@@ -121,7 +144,7 @@ pub fn applyTextInputEvent(state: TextEditState, event: TextInputEvent, output:
121
144
  .move_caret => |move| moveTextCaret(normalized, move),
122
145
  .set_selection => |selection| .{
123
146
  .text = normalized.text,
124
- .selection = snapTextSelection(normalized.text, selection),
147
+ .selection = snapTextCaretSelection(normalized.text, selection),
125
148
  .composition = null,
126
149
  },
127
150
  .set_composition => |composition| setTextComposition(normalized, composition, output),
@@ -142,7 +165,7 @@ const TextReplaceResult = struct {
142
165
  fn normalizeTextEditState(state: TextEditState) TextEditState {
143
166
  return .{
144
167
  .text = state.text,
145
- .selection = snapTextSelection(state.text, state.selection),
168
+ .selection = snapTextCaretSelection(state.text, state.selection),
146
169
  .composition = if (state.composition) |range| snapTextRange(state.text, range) else null,
147
170
  };
148
171
  }
@@ -161,7 +184,7 @@ fn replaceTextEditRange(
161
184
  cursor_offset: usize,
162
185
  ) Error!TextEditState {
163
186
  const result = try replaceTextRange(state.text, range, replacement, output);
164
- const cursor = snapTextOffset(result.text, result.inserted_range.start + @min(cursor_offset, replacement.len));
187
+ const cursor = snapTextCaretOffset(result.text, result.inserted_range.start + @min(cursor_offset, replacement.len));
165
188
  return .{
166
189
  .text = result.text,
167
190
  .selection = TextSelection.collapsed(cursor),
@@ -171,9 +194,9 @@ fn replaceTextEditRange(
171
194
 
172
195
  fn setTextComposition(state: TextEditState, composition: TextCompositionUpdate, output: []u8) Error!TextEditState {
173
196
  const range = activeTextReplaceRange(state);
174
- const cursor = snapTextOffset(composition.text, composition.cursor orelse composition.text.len);
197
+ const cursor = snapTextCaretOffset(composition.text, composition.cursor orelse composition.text.len);
175
198
  const result = try replaceTextRange(state.text, range, composition.text, output);
176
- const absolute_cursor = snapTextOffset(result.text, result.inserted_range.start + cursor);
199
+ const absolute_cursor = snapTextCaretOffset(result.text, result.inserted_range.start + cursor);
177
200
  return .{
178
201
  .text = result.text,
179
202
  .selection = TextSelection.collapsed(absolute_cursor),
@@ -187,7 +210,7 @@ fn cancelTextComposition(state: TextEditState, output: []u8) Error!TextEditState
187
210
  const result = try replaceTextRange(state.text, range, "", output);
188
211
  return .{
189
212
  .text = result.text,
190
- .selection = TextSelection.collapsed(result.inserted_range.start),
213
+ .selection = TextSelection.collapsed(snapTextCaretOffset(result.text, result.inserted_range.start)),
191
214
  .composition = null,
192
215
  };
193
216
  }
@@ -196,25 +219,25 @@ fn deleteBackwardTextEdit(state: TextEditState, output: []u8) Error!TextEditStat
196
219
  const range = activeTextReplaceRange(state);
197
220
  if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
198
221
 
199
- const caret = snapTextOffset(state.text, state.selection.focus);
222
+ const caret = snapTextCaretOffset(state.text, state.selection.focus);
200
223
  if (caret == 0) return .{ .text = state.text, .selection = TextSelection.collapsed(0), .composition = null };
201
- return replaceTextEditRange(state, TextRange.init(previousTextOffset(state.text, caret), caret), "", output, null, 0);
224
+ return replaceTextEditRange(state, TextRange.init(previousTextCaretOffset(state.text, caret), caret), "", output, null, 0);
202
225
  }
203
226
 
204
227
  fn deleteForwardTextEdit(state: TextEditState, output: []u8) Error!TextEditState {
205
228
  const range = activeTextReplaceRange(state);
206
229
  if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
207
230
 
208
- const caret = snapTextOffset(state.text, state.selection.focus);
231
+ const caret = snapTextCaretOffset(state.text, state.selection.focus);
209
232
  if (caret >= state.text.len) return .{ .text = state.text, .selection = TextSelection.collapsed(state.text.len), .composition = null };
210
- return replaceTextEditRange(state, TextRange.init(caret, nextTextOffset(state.text, caret)), "", output, null, 0);
233
+ return replaceTextEditRange(state, TextRange.init(caret, nextTextCaretOffset(state.text, caret)), "", output, null, 0);
211
234
  }
212
235
 
213
236
  fn deleteWordBackwardTextEdit(state: TextEditState, output: []u8) Error!TextEditState {
214
237
  const range = activeTextReplaceRange(state);
215
238
  if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
216
239
 
217
- const caret = snapTextOffset(state.text, state.selection.focus);
240
+ const caret = snapTextCaretOffset(state.text, state.selection.focus);
218
241
  if (caret == 0) return .{ .text = state.text, .selection = TextSelection.collapsed(0), .composition = null };
219
242
  return replaceTextEditRange(state, TextRange.init(previousTextWordOffset(state.text, caret), caret), "", output, null, 0);
220
243
  }
@@ -223,17 +246,17 @@ fn deleteWordForwardTextEdit(state: TextEditState, output: []u8) Error!TextEditS
223
246
  const range = activeTextReplaceRange(state);
224
247
  if (!range.isCollapsed(state.text.len)) return replaceTextEditRange(state, range, "", output, null, 0);
225
248
 
226
- const caret = snapTextOffset(state.text, state.selection.focus);
249
+ const caret = snapTextCaretOffset(state.text, state.selection.focus);
227
250
  if (caret >= state.text.len) return .{ .text = state.text, .selection = TextSelection.collapsed(state.text.len), .composition = null };
228
251
  return replaceTextEditRange(state, TextRange.init(caret, nextTextWordOffset(state.text, caret)), "", output, null, 0);
229
252
  }
230
253
 
231
254
  fn moveTextCaret(state: TextEditState, move: TextCaretMove) TextEditState {
232
255
  const range = state.selection.range(state.text.len);
233
- const focus = snapTextOffset(state.text, state.selection.focus);
256
+ const focus = snapTextCaretOffset(state.text, state.selection.focus);
234
257
  const target = switch (move.direction) {
235
- .previous => if (!move.extend and !range.isCollapsed(state.text.len)) range.start else previousTextOffset(state.text, focus),
236
- .next => if (!move.extend and !range.isCollapsed(state.text.len)) range.end else nextTextOffset(state.text, focus),
258
+ .previous => if (!move.extend and !range.isCollapsed(state.text.len)) range.start else previousTextCaretOffset(state.text, focus),
259
+ .next => if (!move.extend and !range.isCollapsed(state.text.len)) range.end else nextTextCaretOffset(state.text, focus),
237
260
  .previous_word => if (!move.extend and !range.isCollapsed(state.text.len)) range.start else previousTextWordOffset(state.text, focus),
238
261
  .next_word => if (!move.extend and !range.isCollapsed(state.text.len)) range.end else nextTextWordOffset(state.text, focus),
239
262
  .start => 0,
@@ -245,7 +268,7 @@ fn moveTextCaret(state: TextEditState, move: TextCaretMove) TextEditState {
245
268
  TextSelection.collapsed(target);
246
269
  return .{
247
270
  .text = state.text,
248
- .selection = snapTextSelection(state.text, selection),
271
+ .selection = snapTextCaretSelection(state.text, selection),
249
272
  .composition = null,
250
273
  };
251
274
  }
@@ -277,9 +300,44 @@ pub fn snapTextSelection(text: []const u8, selection: TextSelection) TextSelecti
277
300
  return .{
278
301
  .anchor = snapTextOffset(text, selection.anchor),
279
302
  .focus = snapTextOffset(text, selection.focus),
303
+ .affinity = selection.affinity,
304
+ };
305
+ }
306
+
307
+ /// Snap a caret to a valid UTF-8 boundary that cannot split a CRLF hard
308
+ /// line ending. Layout represents the painted end of a CRLF line at the
309
+ /// LF-side byte offset; editing canonicalizes that position before CR.
310
+ pub fn snapTextCaretPosition(text: []const u8, position: TextCaretPosition) TextCaretPosition {
311
+ const offset = snapTextOffset(text, position.offset);
312
+ if (offset > 0 and
313
+ offset < text.len and
314
+ text[offset] == '\n' and
315
+ text[offset - 1] == '\r')
316
+ {
317
+ return .{ .offset = offset - 1, .affinity = .upstream };
318
+ }
319
+ return .{ .offset = offset, .affinity = position.affinity };
320
+ }
321
+
322
+ /// Snap both editable selection endpoints to caret-safe boundaries. This
323
+ /// is deliberately separate from `snapTextSelection`: non-editing text
324
+ /// ranges may still address either scalar byte of a CRLF delimiter.
325
+ pub fn snapTextCaretSelection(text: []const u8, selection: TextSelection) TextSelection {
326
+ const focus = snapTextCaretPosition(text, .{
327
+ .offset = selection.focus,
328
+ .affinity = selection.affinity,
329
+ });
330
+ return .{
331
+ .anchor = snapTextCaretOffset(text, selection.anchor),
332
+ .focus = focus.offset,
333
+ .affinity = focus.affinity,
280
334
  };
281
335
  }
282
336
 
337
+ fn snapTextCaretOffset(text: []const u8, offset: usize) usize {
338
+ return snapTextCaretPosition(text, .{ .offset = offset }).offset;
339
+ }
340
+
283
341
  pub fn snapTextRange(text: []const u8, range: TextRange) TextRange {
284
342
  const normalized = range.normalized(text.len);
285
343
  return TextRange.init(
@@ -312,6 +370,24 @@ pub fn nextTextOffset(text: []const u8, offset: usize) usize {
312
370
  return next;
313
371
  }
314
372
 
373
+ /// Caret editing treats CRLF as one hard-line boundary even though layout
374
+ /// still walks both scalar bytes to recognize and paint that boundary.
375
+ fn previousTextCaretOffset(text: []const u8, offset: usize) usize {
376
+ const previous = previousTextOffset(text, offset);
377
+ if (previous > 0 and text[previous] == '\n' and text[previous - 1] == '\r') {
378
+ return previous - 1;
379
+ }
380
+ return previous;
381
+ }
382
+
383
+ fn nextTextCaretOffset(text: []const u8, offset: usize) usize {
384
+ const cursor = snapTextOffset(text, offset);
385
+ if (cursor < text.len and text[cursor] == '\r' and cursor + 1 < text.len and text[cursor + 1] == '\n') {
386
+ return cursor + 2;
387
+ }
388
+ return nextTextOffset(text, cursor);
389
+ }
390
+
315
391
  pub fn previousTextWordOffset(text: []const u8, offset: usize) usize {
316
392
  var cursor = snapTextOffset(text, offset);
317
393
  while (cursor > 0) {
@@ -417,12 +493,28 @@ pub fn textWordSelectionAtOffset(text: []const u8, offset: usize) TextSelection
417
493
  /// text without a stray terminator). Scanning raw bytes for `\n` is
418
494
  /// UTF-8 safe — 0x0A never appears inside a multibyte sequence.
419
495
  pub fn textLineSelectionAtOffset(text: []const u8, offset: usize) TextSelection {
420
- const cursor = @min(offset, text.len);
421
- var start = cursor;
422
- while (start > 0 and text[start - 1] != '\n') start -= 1;
423
- var end = cursor;
424
- while (end < text.len and text[end] != '\n') end += 1;
425
- return .{ .anchor = start, .focus = end };
496
+ return .{
497
+ .anchor = textLineStartOffset(text, offset),
498
+ .focus = textLineEndOffset(text, offset),
499
+ };
500
+ }
501
+
502
+ /// Start of the hard-newline-delimited line containing `offset`. The
503
+ /// offset is snapped to a UTF-8 boundary first; scanning for ASCII LF is
504
+ /// byte-safe because it cannot occur inside a multibyte sequence.
505
+ pub fn textLineStartOffset(text: []const u8, offset: usize) usize {
506
+ var cursor = snapTextOffset(text, offset);
507
+ while (cursor > 0 and text[cursor - 1] != '\n') cursor -= 1;
508
+ return cursor;
509
+ }
510
+
511
+ /// End of the hard-newline-delimited line containing `offset`, excluding
512
+ /// its line break. CRLF documents exclude both bytes from the line.
513
+ pub fn textLineEndOffset(text: []const u8, offset: usize) usize {
514
+ var cursor = snapTextOffset(text, offset);
515
+ while (cursor < text.len and text[cursor] != '\n') cursor += 1;
516
+ if (cursor > 0 and cursor < text.len and text[cursor - 1] == '\r') return cursor - 1;
517
+ return cursor;
426
518
  }
427
519
 
428
520
  fn textOffsetStartsWord(text: []const u8, offset: usize) bool {
@@ -565,6 +657,56 @@ test "textLineSelectionAtOffset selects the newline-delimited line without its b
565
657
  try std.testing.expectEqualDeep(TextSelection{ .anchor = 18, .focus = 18 }, textLineSelectionAtOffset(text, 18));
566
658
  // The last line has no trailing newline; it still selects fully.
567
659
  try std.testing.expectEqualDeep(TextSelection{ .anchor = 19, .focus = 25 }, textLineSelectionAtOffset(text, 22));
660
+
661
+ try std.testing.expectEqual(@as(usize, 11), textLineStartOffset(text, 15));
662
+ try std.testing.expectEqual(@as(usize, 17), textLineEndOffset(text, 15));
663
+ try std.testing.expectEqual(@as(usize, 3), textLineEndOffset("one\r\ntwo", 1));
664
+ // A bare CR at EOF is content, not the first half of a CRLF break.
665
+ try std.testing.expectEqual(@as(usize, 4), textLineEndOffset("one\r", 1));
666
+ }
667
+
668
+ test "caret movement and deletion cross CRLF atomically" {
669
+ const text = "one\r\ntwo";
670
+ var scratch: [32]u8 = undefined;
671
+
672
+ // Programmatic selections and raw edit state cannot expose the byte
673
+ // between CR and LF as a caret or selection endpoint.
674
+ const inside_break = TextEditState{
675
+ .text = text,
676
+ .selection = TextSelection.collapsed(4),
677
+ };
678
+ const selected_inside_break = try inside_break.apply(.{
679
+ .set_selection = TextSelection.collapsed(4),
680
+ }, &scratch);
681
+ try std.testing.expectEqualDeep(TextSelection.collapsed(3), selected_inside_break.selection);
682
+ const inserted_inside_break = try inside_break.apply(.{ .insert_text = "X" }, &scratch);
683
+ try std.testing.expectEqualStrings("oneX\r\ntwo", inserted_inside_break.text);
684
+ try std.testing.expectEqualDeep(TextSelection.collapsed(4), inserted_inside_break.selection);
685
+
686
+ try std.testing.expectEqualDeep(
687
+ TextSelection{ .anchor = 3, .focus = 5 },
688
+ snapTextCaretSelection(text, .{ .anchor = 4, .focus = 5 }),
689
+ );
690
+
691
+ const before_break = TextEditState{
692
+ .text = text,
693
+ .selection = TextSelection.collapsed(3),
694
+ };
695
+ const moved_next = try before_break.apply(.{ .move_caret = .{ .direction = .next } }, &scratch);
696
+ try std.testing.expectEqualDeep(TextSelection.collapsed(5), moved_next.selection);
697
+ const deleted_forward = try before_break.apply(.delete_forward, &scratch);
698
+ try std.testing.expectEqualStrings("onetwo", deleted_forward.text);
699
+ try std.testing.expectEqualDeep(TextSelection.collapsed(3), deleted_forward.selection);
700
+
701
+ const after_break = TextEditState{
702
+ .text = text,
703
+ .selection = TextSelection.collapsed(5),
704
+ };
705
+ const moved_previous = try after_break.apply(.{ .move_caret = .{ .direction = .previous } }, &scratch);
706
+ try std.testing.expectEqualDeep(TextSelection.collapsed(3), moved_previous.selection);
707
+ const deleted_backward = try after_break.apply(.delete_backward, &scratch);
708
+ try std.testing.expectEqualStrings("onetwo", deleted_backward.text);
709
+ try std.testing.expectEqualDeep(TextSelection.collapsed(3), deleted_backward.selection);
568
710
  }
569
711
 
570
712
  test "TextBuffer mirrors edits, truncates at capacity, and clears" {
@@ -371,8 +371,15 @@ pub const TextLineIterator = struct {
371
371
  self.finished = true;
372
372
  } else {
373
373
  var next_start = end;
374
- if (next_start < bytes.len and bytes[next_start] == '\n') next_start += 1;
375
- while (self.options.wrap == .word and next_start < bytes.len and isTextBreakByte(bytes[next_start])) next_start += 1;
374
+ if (next_start < bytes.len and bytes[next_start] == '\n') {
375
+ // A hard break starts the next line immediately after
376
+ // the newline. Its leading whitespace is editable
377
+ // content, so it must contribute paint and caret width.
378
+ next_start += 1;
379
+ } else {
380
+ // Only soft word wraps elide their break separators.
381
+ while (self.options.wrap == .word and next_start < bytes.len and isTextBreakByte(bytes[next_start])) next_start += 1;
382
+ }
376
383
  self.cursor = next_start;
377
384
  }
378
385
  const elision = plainLineElision(bytes, start, end, self.text.font_id, self.text.size, self.options);
@@ -464,12 +471,30 @@ fn metricTextBounds(value: DrawText) ?geometry.RectF {
464
471
  /// buffer and no line-count failure mode: the caret of an arbitrarily
465
472
  /// long document always resolves.
466
473
  pub fn layoutTextCaretRect(text: DrawText, options: TextLayoutOptions, offset: usize) ?geometry.RectF {
467
- const line = streamTextLineForOffset(text, options, snapTextOffset(text.text, offset)) orelse return null;
474
+ return layoutTextCaretRectWithAffinity(text, options, offset, .upstream);
475
+ }
476
+
477
+ pub fn layoutTextCaretRectWithAffinity(
478
+ text: DrawText,
479
+ options: TextLayoutOptions,
480
+ offset: usize,
481
+ affinity: canvas.TextCaretAffinity,
482
+ ) ?geometry.RectF {
483
+ const line = streamTextLineForOffset(text, options, snapTextOffset(text.text, offset), affinity) orelse return null;
468
484
  return textCaretRectForLine(text, line, offset);
469
485
  }
470
486
 
471
487
  pub fn textCaretRectForLayout(text: DrawText, layout: TextLayout, offset: usize) ?geometry.RectF {
472
- const line = textLineForOffset(layout, text.text.len, snapTextOffset(text.text, offset)) orelse return null;
488
+ return textCaretRectForLayoutWithAffinity(text, layout, offset, .upstream);
489
+ }
490
+
491
+ pub fn textCaretRectForLayoutWithAffinity(
492
+ text: DrawText,
493
+ layout: TextLayout,
494
+ offset: usize,
495
+ affinity: canvas.TextCaretAffinity,
496
+ ) ?geometry.RectF {
497
+ const line = textLineForOffset(layout, text.text.len, snapTextOffset(text.text, offset), affinity) orelse return null;
473
498
  return textCaretRectForLine(text, line, offset);
474
499
  }
475
500
 
@@ -554,31 +579,90 @@ fn accumulateTextSelectionRect(accumulator: *TextSelectionRectAccumulator, text:
554
579
  /// Byte offset for a point, streaming the run's lines — hit testing a
555
580
  /// click in a long document has no line-count failure mode.
556
581
  pub fn layoutTextOffsetForPoint(text: DrawText, options: TextLayoutOptions, point: geometry.PointF) ?usize {
582
+ const position = layoutTextCaretPositionForPoint(text, options, point) orelse return null;
583
+ return position.offset;
584
+ }
585
+
586
+ pub fn layoutTextCaretPositionForPoint(
587
+ text: DrawText,
588
+ options: TextLayoutOptions,
589
+ point: geometry.PointF,
590
+ ) ?canvas.TextCaretPosition {
557
591
  var lines = TextLineIterator.init(text, options);
558
592
  var candidate: ?TextLine = null;
593
+ var previous: ?TextLine = null;
594
+ var candidate_previous: ?TextLine = null;
559
595
  while (lines.next()) |line| {
560
596
  candidate = line;
597
+ candidate_previous = previous;
561
598
  if (point.y < line.bounds.y + line.bounds.height) break;
599
+ previous = line;
562
600
  }
563
601
  const line = candidate orelse return null;
564
- return textLineOffsetForX(text, line, point.x);
602
+ return textCaretPositionForLine(text, line, candidate_previous, point.x);
565
603
  }
566
604
 
567
605
  pub fn textOffsetForLayoutPoint(text: DrawText, layout: TextLayout, point: geometry.PointF) ?usize {
568
- const line = textLineForPoint(layout, point) orelse return null;
569
- return textLineOffsetForX(text, line, point.x);
606
+ const position = textCaretPositionForLayoutPoint(text, layout, point) orelse return null;
607
+ return position.offset;
608
+ }
609
+
610
+ pub fn textCaretPositionForLayoutPoint(
611
+ text: DrawText,
612
+ layout: TextLayout,
613
+ point: geometry.PointF,
614
+ ) ?canvas.TextCaretPosition {
615
+ var previous: ?TextLine = null;
616
+ for (layout.lines) |line| {
617
+ if (point.y < line.bounds.y + line.bounds.height) {
618
+ return textCaretPositionForLine(text, line, previous, point.x);
619
+ }
620
+ previous = line;
621
+ }
622
+ const line = previous orelse return null;
623
+ const before = if (layout.lines.len > 1) layout.lines[layout.lines.len - 2] else null;
624
+ return textCaretPositionForLine(text, line, before, point.x);
625
+ }
626
+
627
+ fn textCaretPositionForLine(
628
+ text: DrawText,
629
+ line: TextLine,
630
+ previous: ?TextLine,
631
+ x: f32,
632
+ ) canvas.TextCaretPosition {
633
+ const offset = textLineOffsetForX(text, line, x);
634
+ const range = textLineRange(text, line);
635
+ const affinity: canvas.TextCaretAffinity = if (previous) |previous_line|
636
+ if (offset == range.start and textLineRange(text, previous_line).end == offset)
637
+ .downstream
638
+ else
639
+ .upstream
640
+ else
641
+ .upstream;
642
+ return .{ .offset = offset, .affinity = affinity };
570
643
  }
571
644
 
572
645
  /// Streaming twin of `textLineForOffset`: the line containing
573
646
  /// `offset` (already snapped), with the same neighbor semantics.
574
- fn streamTextLineForOffset(text: DrawText, options: TextLayoutOptions, offset: usize) ?TextLine {
647
+ fn streamTextLineForOffset(
648
+ text: DrawText,
649
+ options: TextLayoutOptions,
650
+ offset: usize,
651
+ affinity: canvas.TextCaretAffinity,
652
+ ) ?TextLine {
575
653
  const normalized = @min(offset, text.text.len);
576
654
  var lines = TextLineIterator.init(text, options);
577
655
  var previous: ?TextLine = null;
578
656
  while (lines.next()) |line| {
579
657
  const range = textLineRangeForLength(text.text.len, line);
580
658
  if (normalized < range.start) return previous orelse line;
581
- if (normalized <= range.end) return line;
659
+ if (normalized < range.end) return line;
660
+ if (normalized == range.end) {
661
+ if (affinity == .upstream) return line;
662
+ const next_line = lines.next() orelse return line;
663
+ const next_range = textLineRangeForLength(text.text.len, next_line);
664
+ return if (next_range.start == normalized) next_line else line;
665
+ }
582
666
  previous = line;
583
667
  }
584
668
  return previous;
@@ -748,23 +832,25 @@ fn alignTextLineBounds(bounds: geometry.RectF, options: TextLayoutOptions) geome
748
832
  return bounds.translate(geometry.OffsetF.init(dx, 0));
749
833
  }
750
834
 
751
- fn textLineForOffset(layout: TextLayout, text_len: usize, offset: usize) ?TextLine {
835
+ fn textLineForOffset(
836
+ layout: TextLayout,
837
+ text_len: usize,
838
+ offset: usize,
839
+ affinity: canvas.TextCaretAffinity,
840
+ ) ?TextLine {
752
841
  if (layout.lines.len == 0) return null;
753
842
  const normalized = @min(offset, text_len);
754
843
  var previous: ?TextLine = null;
755
- for (layout.lines) |line| {
844
+ for (layout.lines, 0..) |line, index| {
756
845
  const range = textLineRangeForLength(text_len, line);
757
846
  if (normalized < range.start) return previous orelse line;
758
- if (normalized <= range.end) return line;
759
- previous = line;
760
- }
761
- return previous;
762
- }
763
-
764
- fn textLineForPoint(layout: TextLayout, point: geometry.PointF) ?TextLine {
765
- var previous: ?TextLine = null;
766
- for (layout.lines) |line| {
767
- if (point.y < line.bounds.y + line.bounds.height) return line;
847
+ if (normalized < range.end) return line;
848
+ if (normalized == range.end) {
849
+ if (affinity == .upstream or index + 1 >= layout.lines.len) return line;
850
+ const next_line = layout.lines[index + 1];
851
+ const next_range = textLineRangeForLength(text_len, next_line);
852
+ return if (next_range.start == normalized) next_line else line;
853
+ }
768
854
  previous = line;
769
855
  }
770
856
  return previous;