@native-sdk/cli 0.6.2 → 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/build.zig +8 -0
- package/package.json +9 -9
- package/packages/core/package-lock.json +2 -2
- package/packages/core/package.json +1 -1
- package/packages/core/sdk/text.ts +62 -21
- package/skill-data/native-ui/SKILL.md +17 -2
- package/src/platform/macos/appkit_host.h +2 -0
- package/src/platform/macos/appkit_host.m +36 -10
- package/src/platform/macos/root.zig +7 -1
- package/src/platform/types.zig +2 -0
- package/src/primitives/canvas/code.zig +590 -0
- package/src/primitives/canvas/code_tests.zig +1062 -0
- package/src/primitives/canvas/equality.zig +3 -1
- package/src/primitives/canvas/events.zig +4 -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 +15 -2
- package/src/primitives/canvas/tests.zig +1 -0
- package/src/primitives/canvas/text.zig +10 -0
- package/src/primitives/canvas/text_interaction.zig +164 -22
- package/src/primitives/canvas/text_layout.zig +107 -21
- 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/text_tests.zig +86 -0
- package/src/primitives/canvas/themes/geist.zig +29 -0
- package/src/primitives/canvas/tokens.zig +72 -2
- package/src/primitives/canvas/ui.zig +325 -21
- 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_reflect.zig +20 -3
- package/src/primitives/canvas/ui_markup_view.zig +73 -0
- package/src/primitives/canvas/ui_markup_view_tests.zig +101 -0
- package/src/primitives/canvas/ui_schema.zig +17 -6
- package/src/primitives/canvas/ui_schema_tests.zig +7 -6
- package/src/primitives/canvas/ui_tests.zig +6 -0
- 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_runtime.zig +1 -0
- package/src/primitives/canvas/widget_text_input.zig +23 -11
- 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/api.zig +9 -0
- package/src/runtime/canvas_frame_helpers.zig +1 -0
- package/src/runtime/canvas_limits.zig +7 -0
- package/src/runtime/canvas_widget_accessibility_tests.zig +27 -1
- package/src/runtime/canvas_widget_clipboard_tests.zig +146 -0
- package/src/runtime/canvas_widget_context_menu.zig +12 -2
- package/src/runtime/canvas_widget_display.zig +15 -1
- package/src/runtime/canvas_widget_events.zig +50 -20
- package/src/runtime/canvas_widget_runtime.zig +74 -12
- package/src/runtime/canvas_widget_state.zig +1 -0
- package/src/runtime/canvas_widget_text_tests.zig +1935 -70
- package/src/runtime/gpu_surface_events.zig +38 -0
- package/src/runtime/view.zig +37 -3
- package/src/runtime/view_widget_text.zig +1242 -14
- package/src/runtime/view_widget_tree.zig +2 -0
|
@@ -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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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(
|
|
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 =
|
|
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,
|
|
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 =
|
|
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 =
|
|
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 =
|
|
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
|
|
236
|
-
.next => if (!move.extend and !range.isCollapsed(state.text.len)) range.end else
|
|
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 =
|
|
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
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
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')
|
|
375
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
569
|
-
return
|
|
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(
|
|
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
|
|
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(
|
|
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
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
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;
|
|
@@ -72,11 +72,11 @@ pub const max_cached_advance_run_bytes: usize = 2048;
|
|
|
72
72
|
/// the peek/fetch split.
|
|
73
73
|
pub const advance_cache_capacity: usize = 256;
|
|
74
74
|
|
|
75
|
-
/// Oversize scratch: one slot covering runs up to the
|
|
76
|
-
/// budget (`canvas_limits.
|
|
77
|
-
/// long still gets ONE batched call per fetch; it just is not retained
|
|
75
|
+
/// Oversize scratch: one slot covering runs up to the canvas-widget source
|
|
76
|
+
/// budget (`canvas_limits.max_canvas_widget_text_bytes_per_view`). A run
|
|
77
|
+
/// this long still gets ONE batched call per fetch; it just is not retained
|
|
78
78
|
/// across other fetches.
|
|
79
|
-
pub const max_batched_advance_run_bytes: usize =
|
|
79
|
+
pub const max_batched_advance_run_bytes: usize = 65536;
|
|
80
80
|
|
|
81
81
|
/// Process-wide invalidation stamp for everything keyed on measured
|
|
82
82
|
/// text: cached advances here and retained wrap results downstream.
|