@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
@@ -1,4 +1,5 @@
1
1
  const std = @import("std");
2
+ const builtin = @import("builtin");
2
3
  const geometry = @import("geometry");
3
4
  const canvas = @import("canvas");
4
5
  const canvas_frame_helpers = @import("canvas_frame.zig");
@@ -19,9 +20,51 @@ const canvasWidgetTextEditUnchanged = canvas_widget_runtime.canvasWidgetTextEdit
19
20
  const canvasTextSelectionsEqual = canvas_widget_runtime.canvasTextSelectionsEqual;
20
21
  const textSelectionCollapsedAt = canvas_widget_runtime.textSelectionCollapsedAt;
21
22
 
23
+ pub const CanvasWidgetTextHistoryEntry = struct {
24
+ serial: u64 = 0,
25
+ target_id: canvas.ObjectId = 0,
26
+ target_kind: canvas.WidgetKind = .text,
27
+ byte_start: usize = 0,
28
+ removed_len: usize = 0,
29
+ inserted_len: usize = 0,
30
+ prefix_len: usize = 0,
31
+ before_text_len: usize = 0,
32
+ after_text_len: usize = 0,
33
+ before_hash: u64 = 0,
34
+ after_hash: u64 = 0,
35
+ before_selection: canvas.TextSelection = .{},
36
+ after_selection: canvas.TextSelection = .{},
37
+ applied: bool = true,
38
+ /// An active IME preedit is one logical edit whose inserted bytes can
39
+ /// change many times before commit. The entry stays out of undo/redo
40
+ /// lookup until the composition resolves.
41
+ provisional_composition: bool = false,
42
+ };
43
+
44
+ pub const CanvasWidgetTextHistoryShortcutResult = struct {
45
+ edit: canvas.TextInputEvent,
46
+ serial: u64,
47
+ redo: bool,
48
+ };
49
+
50
+ pub const CanvasWidgetTextHistoryAvailability = struct {
51
+ can_undo: bool = false,
52
+ can_redo: bool = false,
53
+ };
54
+
55
+ const max_text_history_edits_per_shortcut = 3;
56
+
22
57
  pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
23
58
  return struct {
24
59
  pub fn applyCanvasWidgetTextEdit(self: *RuntimeView, target_id: canvas.ObjectId, edit: canvas.TextInputEvent) anyerror!?geometry.RectF {
60
+ return applyCanvasWidgetTextEditWithHistory(self, target_id, edit, true);
61
+ }
62
+
63
+ pub fn applyCanvasWidgetTextEditWithoutHistory(self: *RuntimeView, target_id: canvas.ObjectId, edit: canvas.TextInputEvent) anyerror!?geometry.RectF {
64
+ return applyCanvasWidgetTextEditWithHistory(self, target_id, edit, false);
65
+ }
66
+
67
+ fn applyCanvasWidgetTextEditWithHistory(self: *RuntimeView, target_id: canvas.ObjectId, edit: canvas.TextInputEvent, record_history: bool) anyerror!?geometry.RectF {
25
68
  const index = self.canvasWidgetNodeIndexById(target_id) orelse return null;
26
69
  const widget = self.widget_layout_nodes[index].widget;
27
70
  if (!canvasWidgetEditableTextKind(widget.kind) or widget.state.disabled) return null;
@@ -35,8 +78,21 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
35
78
  };
36
79
  const next_state = try current_state.apply(edit, &edit_buffer);
37
80
  if (canvasWidgetTextEditUnchanged(current_state, next_state)) return null;
81
+ try validateCanvasWidgetTextStorageRewrite(self, index, next_state);
38
82
 
39
- try self.rewriteCanvasWidgetTextStorage(index, next_state);
83
+ const starts_composition = current_state.composition == null and next_state.composition != null;
84
+ const history_recorded = record_history and
85
+ (starts_composition or
86
+ (current_state.composition == null and
87
+ !std.mem.eql(u8, current_state.text, next_state.text))) and
88
+ recordCanvasWidgetTextHistory(self, target_id, widget.kind, current_state, next_state, starts_composition);
89
+ self.rewriteCanvasWidgetTextStorage(index, next_state) catch |err| {
90
+ if (history_recorded) removeCanvasWidgetTextHistoryEntry(self, self.canvas_widget_text_history_entry_count - 1);
91
+ return err;
92
+ };
93
+ if (record_history and current_state.composition != null) {
94
+ updateCanvasWidgetTextCompositionHistory(self, target_id, next_state);
95
+ }
40
96
  self.scrollCanvasTextInputCaretIntoView(index);
41
97
  const semantics = try self.widgetLayoutTree().collectSemantics(&self.widget_semantics_nodes);
42
98
  self.widget_semantics_node_count = semantics.len;
@@ -44,11 +100,50 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
44
100
  return self.canvasWidgetDirtyBounds(index, unionRects(previous_bounds, self.widget_layout_nodes[index].frame) orelse self.widget_layout_nodes[index].frame);
45
101
  }
46
102
 
47
- pub fn canvasWidgetKeyboardTextEdit(self: *const RuntimeView, target: canvas.WidgetFocusTarget, keyboard: canvas.WidgetKeyboardEvent) ?canvas.TextInputEvent {
103
+ /// The leading edge of a non-empty selection. At a soft-wrap
104
+ /// boundary one byte offset has two painted caret stops; selected
105
+ /// text beginning there starts on the downstream visual line.
106
+ fn canvasWidgetTextSelectionStartPosition(self: *RuntimeView, widget: canvas.Widget, offset: usize) canvas.TextCaretPosition {
107
+ const upstream_position = canvas.TextCaretPosition{
108
+ .offset = offset,
109
+ .affinity = .upstream,
110
+ };
111
+ var upstream_widget = widget;
112
+ upstream_widget.text_selection = canvas.TextSelection.collapsedAt(upstream_position);
113
+ var downstream_widget = widget;
114
+ downstream_widget.text_selection = canvas.TextSelection.collapsedAt(.{
115
+ .offset = offset,
116
+ .affinity = .downstream,
117
+ });
118
+ if (canvas.textGeometryForWidget(upstream_widget, self.widget_tokens).caret_bounds) |upstream_caret| {
119
+ if (canvas.textGeometryForWidget(downstream_widget, self.widget_tokens).caret_bounds) |downstream_caret| {
120
+ if (downstream_caret.y > upstream_caret.y) {
121
+ return .{
122
+ .offset = offset,
123
+ .affinity = .downstream,
124
+ };
125
+ }
126
+ }
127
+ }
128
+ return upstream_position;
129
+ }
130
+
131
+ pub fn canvasWidgetKeyboardTextEdit(self: *RuntimeView, target: canvas.WidgetFocusTarget, keyboard: canvas.WidgetKeyboardEvent) ?canvas.TextInputEvent {
48
132
  const index = self.canvasWidgetNodeIndexById(target.id) orelse return null;
49
133
  const widget = self.widget_layout_nodes[index].widget;
50
134
  if (!canvasWidgetEditableTextKind(widget.kind) or widget.state.disabled) return null;
51
135
 
136
+ const plain_vertical_navigation_key = widget.kind == .textarea and
137
+ (keyboard.phase == .key_down or keyboard.phase == .key_up) and
138
+ keyboard.text.len == 0 and
139
+ !keyboard.modifiers.hasNavigationModifier() and
140
+ (std.ascii.eqlIgnoreCase(keyboard.key, "arrowup") or
141
+ std.ascii.eqlIgnoreCase(keyboard.key, "arrowdown"));
142
+ // A physical arrow gesture ends with key_up. Keep the preferred
143
+ // x coordinate through that release so separately pressed arrows
144
+ // form the same vertical-navigation run as auto-repeat keydowns.
145
+ if (!plain_vertical_navigation_key) clearCanvasWidgetTextVerticalGoal(self);
146
+
52
147
  if (keyboard.phase == .key_down and !keyboard.modifiers.shift and !keyboard.modifiers.hasNavigationModifier() and canvasWidgetEscapeKey(keyboard.key)) {
53
148
  if (widget.text_composition != null) return .cancel_composition;
54
149
  if (widget.kind == .search_field or widget.kind == .combobox) return .clear;
@@ -63,6 +158,204 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
63
158
  return newline_edit;
64
159
  }
65
160
 
161
+ // macOS textarea navigation differs deliberately from the
162
+ // single-line field keymap: Command+Left/Right is scoped to
163
+ // the PAINTED visual line (including soft wraps), while
164
+ // Command+Up/Down reaches the document boundary. Return an
165
+ // explicit selection for the line moves so the exact target is
166
+ // stamped onto on-input and controlled TextBuffers mirror it.
167
+ if (comptime builtin.os.tag == .macos) {
168
+ if (widget.kind == .textarea and
169
+ keyboard.phase == .key_down and
170
+ keyboard.text.len == 0 and
171
+ keyboard.modifiers.super and
172
+ !keyboard.modifiers.control and
173
+ !keyboard.modifiers.alt)
174
+ {
175
+ const selection = widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len);
176
+ const moving_left = std.ascii.eqlIgnoreCase(keyboard.key, "arrowleft");
177
+ const moving_right = std.ascii.eqlIgnoreCase(keyboard.key, "arrowright");
178
+ if (moving_left or moving_right) {
179
+ const range = selection.range(widget.text.len);
180
+ const origin_position = if (!keyboard.modifiers.shift and !selection.isCollapsed(widget.text.len))
181
+ if (moving_left)
182
+ canvasWidgetTextSelectionStartPosition(self, widget, range.start)
183
+ else
184
+ canvas.TextCaretPosition{ .offset = range.end, .affinity = .upstream }
185
+ else
186
+ canvas.TextCaretPosition{
187
+ .offset = selection.focus,
188
+ .affinity = selection.affinity,
189
+ };
190
+ var caret_widget = widget;
191
+ caret_widget.text_selection = canvas.TextSelection.collapsedAt(origin_position);
192
+ const caret = canvas.textGeometryForWidget(caret_widget, self.widget_tokens).caret_bounds orelse return null;
193
+ const frame = widget.frame.normalized();
194
+ const target_x = if (moving_left) frame.x - 1 else frame.maxX() + 1;
195
+ const target_position = canvas.textCaretPositionForWidgetPoint(
196
+ widget,
197
+ geometry.PointF.init(target_x, caret.y + caret.height * 0.5),
198
+ self.widget_tokens,
199
+ ) orelse return null;
200
+ return .{ .set_selection = if (keyboard.modifiers.shift)
201
+ .{
202
+ .anchor = selection.anchor,
203
+ .focus = target_position.offset,
204
+ .affinity = target_position.affinity,
205
+ }
206
+ else
207
+ canvas.TextSelection.collapsedAt(target_position) };
208
+ }
209
+ if (std.ascii.eqlIgnoreCase(keyboard.key, "arrowup")) {
210
+ return .{ .move_caret = .{ .direction = .start, .extend = keyboard.modifiers.shift } };
211
+ }
212
+ if (std.ascii.eqlIgnoreCase(keyboard.key, "arrowdown")) {
213
+ return .{ .move_caret = .{ .direction = .end, .extend = keyboard.modifiers.shift } };
214
+ }
215
+ }
216
+ }
217
+
218
+ // A soft-wrap boundary has two painted caret stops at one byte
219
+ // offset. Plain horizontal movement crosses that visual seam
220
+ // before advancing to another scalar: Right moves upstream ->
221
+ // downstream, and Left moves downstream -> upstream. Shift
222
+ // variants keep their ordinary byte-extending selection rule.
223
+ if (widget.kind == .textarea and
224
+ keyboard.phase == .key_down and
225
+ keyboard.text.len == 0 and
226
+ !keyboard.modifiers.shift and
227
+ !keyboard.modifiers.hasNavigationModifier())
228
+ {
229
+ const moving_left = std.ascii.eqlIgnoreCase(keyboard.key, "arrowleft");
230
+ const moving_right = std.ascii.eqlIgnoreCase(keyboard.key, "arrowright");
231
+ const selection = widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len);
232
+ if (!selection.isCollapsed(widget.text.len) and (moving_left or moving_right)) {
233
+ const range = selection.range(widget.text.len);
234
+ const target_offset = if (moving_left) range.start else range.end;
235
+ const target_position = if (moving_left)
236
+ canvasWidgetTextSelectionStartPosition(self, widget, target_offset)
237
+ else
238
+ canvas.TextCaretPosition{ .offset = target_offset, .affinity = .upstream };
239
+ return .{ .set_selection = canvas.TextSelection.collapsedAt(target_position) };
240
+ }
241
+ if (selection.isCollapsed(widget.text.len) and
242
+ ((moving_left and selection.affinity == .downstream) or
243
+ (moving_right and selection.affinity == .upstream)))
244
+ {
245
+ var current_widget = widget;
246
+ current_widget.text_selection = canvas.TextSelection.collapsedAt(.{
247
+ .offset = selection.focus,
248
+ .affinity = selection.affinity,
249
+ });
250
+ const current_caret = canvas.textGeometryForWidget(current_widget, self.widget_tokens).caret_bounds orelse return null;
251
+ const next_affinity: canvas.TextCaretAffinity = if (moving_left) .upstream else .downstream;
252
+ var next_widget = widget;
253
+ next_widget.text_selection = canvas.TextSelection.collapsedAt(.{
254
+ .offset = selection.focus,
255
+ .affinity = next_affinity,
256
+ });
257
+ const next_caret = canvas.textGeometryForWidget(next_widget, self.widget_tokens).caret_bounds orelse return null;
258
+ const crosses_wrap = if (moving_left)
259
+ next_caret.y < current_caret.y
260
+ else
261
+ next_caret.y > current_caret.y;
262
+ if (crosses_wrap) {
263
+ return .{ .set_selection = canvas.TextSelection.collapsedAt(.{
264
+ .offset = selection.focus,
265
+ .affinity = next_affinity,
266
+ }) };
267
+ }
268
+ }
269
+ if (selection.isCollapsed(widget.text.len) and moving_left) {
270
+ var movement_scratch: [1]u8 = undefined;
271
+ const moved = (canvas.TextEditState{
272
+ .text = widget.text,
273
+ .selection = selection,
274
+ .composition = widget.text_composition,
275
+ }).apply(
276
+ .{ .move_caret = .{ .direction = .previous } },
277
+ &movement_scratch,
278
+ ) catch return null;
279
+ const target_position = canvasWidgetTextSelectionStartPosition(
280
+ self,
281
+ widget,
282
+ moved.selection.focus,
283
+ );
284
+ if (target_position.affinity == .downstream) {
285
+ return .{ .set_selection = canvas.TextSelection.collapsedAt(target_position) };
286
+ }
287
+ }
288
+ }
289
+
290
+ // Plain Up/Down follows the textarea's PAINTED visual lines,
291
+ // including soft wraps. Resolve the current caret rectangle
292
+ // through the same streaming layout used to draw it, then
293
+ // hit-test the neighboring line at the caret's x coordinate.
294
+ // The explicit selection is stamped onto on-input, keeping a
295
+ // controlled TextBuffer's selection byte-identical to the
296
+ // retained editor.
297
+ if (widget.kind == .textarea and
298
+ keyboard.phase == .key_down and
299
+ keyboard.text.len == 0 and
300
+ !keyboard.modifiers.hasNavigationModifier())
301
+ {
302
+ const moving_up = std.ascii.eqlIgnoreCase(keyboard.key, "arrowup");
303
+ const moving_down = std.ascii.eqlIgnoreCase(keyboard.key, "arrowdown");
304
+ if (moving_up or moving_down) {
305
+ const selection = widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len);
306
+ const range = selection.range(widget.text.len);
307
+ const origin_position = if (!keyboard.modifiers.shift and !selection.isCollapsed(widget.text.len))
308
+ canvasWidgetTextSelectionStartPosition(self, widget, range.start)
309
+ else
310
+ canvas.TextCaretPosition{
311
+ .offset = selection.focus,
312
+ .affinity = selection.affinity,
313
+ };
314
+ var caret_widget = widget;
315
+ caret_widget.text_selection = canvas.TextSelection.collapsedAt(origin_position);
316
+ const caret = canvas.textGeometryForWidget(caret_widget, self.widget_tokens).caret_bounds orelse return null;
317
+ const vertical_witness_matches =
318
+ self.canvas_widget_text_vertical_goal_id == target.id and
319
+ self.canvas_widget_text_vertical_goal_text_len == widget.text.len and
320
+ self.canvas_widget_text_vertical_goal_focus == selection.focus and
321
+ self.canvas_widget_text_vertical_goal_affinity == selection.affinity and
322
+ self.canvas_widget_text_vertical_goal_frame.width == widget.frame.width and
323
+ self.canvas_widget_text_vertical_goal_frame.height == widget.frame.height;
324
+ const text_hash = textHistoryHash(widget.text);
325
+ const continues_vertical_navigation =
326
+ vertical_witness_matches and
327
+ self.canvas_widget_text_vertical_goal_text_hash == text_hash;
328
+ const goal_x = if (continues_vertical_navigation)
329
+ self.canvas_widget_text_vertical_goal_x
330
+ else
331
+ caret.x - widget.frame.x;
332
+ const target_y = if (moving_up)
333
+ caret.y - caret.height * 0.5
334
+ else
335
+ caret.y + caret.height * 1.5;
336
+ const target_position = canvas.textCaretPositionForWidgetPoint(
337
+ widget,
338
+ geometry.PointF.init(widget.frame.x + goal_x, target_y),
339
+ self.widget_tokens,
340
+ ) orelse return null;
341
+ self.canvas_widget_text_vertical_goal_id = target.id;
342
+ self.canvas_widget_text_vertical_goal_x = goal_x;
343
+ self.canvas_widget_text_vertical_goal_text_len = widget.text.len;
344
+ self.canvas_widget_text_vertical_goal_text_hash = text_hash;
345
+ self.canvas_widget_text_vertical_goal_focus = target_position.offset;
346
+ self.canvas_widget_text_vertical_goal_affinity = target_position.affinity;
347
+ self.canvas_widget_text_vertical_goal_frame = widget.frame;
348
+ return .{ .set_selection = if (keyboard.modifiers.shift)
349
+ .{
350
+ .anchor = selection.anchor,
351
+ .focus = target_position.offset,
352
+ .affinity = target_position.affinity,
353
+ }
354
+ else
355
+ canvas.TextSelection.collapsedAt(target_position) };
356
+ }
357
+ }
358
+
66
359
  // On a CLOSED combobox these same arrows are the trigger's
67
360
  // OPEN keys (`widgetKeyboardControlIntent`'s menu-open
68
361
  // mapping, which the app dispatch resolves BEFORE any
@@ -88,6 +381,543 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
88
381
  return keyboard.textEditEvent();
89
382
  }
90
383
 
384
+ /// Resolve Command/Ctrl+Z against the focused editor's delta
385
+ /// history. The returned first edit and replay serial express one
386
+ /// logical step as ordinary TextInputEvents so both the retained
387
+ /// editor and a controlled app-side TextBuffer reach byte-identical
388
+ /// text and selection.
389
+ pub fn canvasWidgetTextHistoryShortcut(
390
+ self: *RuntimeView,
391
+ target: canvas.WidgetFocusTarget,
392
+ keyboard: canvas.WidgetKeyboardEvent,
393
+ ) ?CanvasWidgetTextHistoryShortcutResult {
394
+ if (keyboard.phase != .key_down or
395
+ !keyboard.modifiers.super or
396
+ keyboard.modifiers.alt or
397
+ !std.ascii.eqlIgnoreCase(keyboard.key, "z"))
398
+ {
399
+ return null;
400
+ }
401
+ const node_index = self.canvasWidgetNodeIndexById(target.id) orelse return null;
402
+ const widget = self.widget_layout_nodes[node_index].widget;
403
+ if (!canvasWidgetEditableTextKind(widget.kind) or widget.state.disabled or widget.text_composition != null) return null;
404
+ clearCanvasWidgetTextVerticalGoal(self);
405
+
406
+ const redo = keyboard.modifiers.shift;
407
+ const history_index = canvasWidgetTextHistoryIndex(self, target.id, widget.kind, redo) orelse return null;
408
+ const entry = self.canvas_widget_text_history_entries[history_index];
409
+ const expected_len = if (redo) entry.before_text_len else entry.after_text_len;
410
+ const expected_hash = if (redo) entry.before_hash else entry.after_hash;
411
+ if (widget.text.len != expected_len or textHistoryHash(widget.text) != expected_hash) {
412
+ clearCanvasWidgetTextHistory(self, target.id);
413
+ return null;
414
+ }
415
+
416
+ const removed = canvasWidgetTextHistoryRemoved(self, entry);
417
+ const inserted = canvasWidgetTextHistoryInserted(self, entry);
418
+ var output: [max_text_history_edits_per_shortcut]?canvas.TextInputEvent = .{ null, null, null };
419
+ var edit_count: usize = 0;
420
+ if (redo) {
421
+ buildCanvasWidgetTextRedoEdits(entry, removed, inserted, widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len), &output, &edit_count);
422
+ } else {
423
+ buildCanvasWidgetTextUndoEdits(entry, removed, inserted, widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len), &output, &edit_count);
424
+ }
425
+ if (edit_count == 0) return null;
426
+ return .{
427
+ .edit = output[0].?,
428
+ .serial = entry.serial,
429
+ .redo = redo,
430
+ };
431
+ }
432
+
433
+ /// Availability for native Edit-menu validation. A direction is
434
+ /// exposed only when its nearest history boundary still matches the
435
+ /// retained bytes; controlled source replacement can otherwise leave
436
+ /// an entry mounted but stale until the next shortcut retires it.
437
+ pub fn canvasWidgetTextHistoryAvailability(
438
+ self: *const RuntimeView,
439
+ target_id: canvas.ObjectId,
440
+ ) CanvasWidgetTextHistoryAvailability {
441
+ const node_index = self.canvasWidgetNodeIndexById(target_id) orelse return .{};
442
+ const widget = self.widget_layout_nodes[node_index].widget;
443
+ if (!canvasWidgetEditableTextKind(widget.kind) or
444
+ widget.state.disabled or
445
+ widget.text_composition != null)
446
+ {
447
+ return .{};
448
+ }
449
+
450
+ const current_hash = textHistoryHash(widget.text);
451
+ var availability: CanvasWidgetTextHistoryAvailability = .{};
452
+ if (canvasWidgetTextHistoryIndex(self, target_id, widget.kind, false)) |history_index| {
453
+ const entry = self.canvas_widget_text_history_entries[history_index];
454
+ availability.can_undo = widget.text.len == entry.after_text_len and current_hash == entry.after_hash;
455
+ }
456
+ if (canvasWidgetTextHistoryIndex(self, target_id, widget.kind, true)) |history_index| {
457
+ const entry = self.canvas_widget_text_history_entries[history_index];
458
+ availability.can_redo = widget.text.len == entry.before_text_len and current_hash == entry.before_hash;
459
+ }
460
+ return availability;
461
+ }
462
+
463
+ /// Derive the next edit of a compound history replay from the
464
+ /// retained entry's current location. The preceding edit may have
465
+ /// rebuilt the controlled tree and compacted history bytes, so no
466
+ /// borrowed payload survives across this call boundary.
467
+ pub fn canvasWidgetTextHistoryReplayNext(
468
+ self: *RuntimeView,
469
+ target: canvas.WidgetFocusTarget,
470
+ serial: u64,
471
+ redo: bool,
472
+ ) ?canvas.TextInputEvent {
473
+ if (serial == 0 or self.canvas_widget_focused_id != target.id) {
474
+ if (serial != 0) clearCanvasWidgetTextHistory(self, target.id);
475
+ return null;
476
+ }
477
+ const node_index = self.canvasWidgetNodeIndexById(target.id) orelse return null;
478
+ const widget = self.widget_layout_nodes[node_index].widget;
479
+ if (widget.kind != target.kind or
480
+ !self.canEditCanvasWidgetText(target.id) or
481
+ widget.text_composition != null)
482
+ {
483
+ return null;
484
+ }
485
+ const history_index = canvasWidgetTextHistoryIndexForSerial(self, serial) orelse return null;
486
+ const entry = self.canvas_widget_text_history_entries[history_index];
487
+ if (entry.target_id != target.id or
488
+ entry.target_kind != target.kind or
489
+ entry.provisional_composition or
490
+ entry.applied == redo)
491
+ {
492
+ return null;
493
+ }
494
+
495
+ const selection = widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len);
496
+ const current_hash = textHistoryHash(widget.text);
497
+ const desired_len = if (redo) entry.after_text_len else entry.before_text_len;
498
+ const desired_hash = if (redo) entry.after_hash else entry.before_hash;
499
+ const desired_selection = if (redo) entry.after_selection else entry.before_selection;
500
+ if (widget.text.len == desired_len and current_hash == desired_hash) {
501
+ if (canvasTextSelectionsEqual(selection, desired_selection)) {
502
+ self.canvas_widget_text_history_entries[history_index].applied = redo;
503
+ return null;
504
+ }
505
+ return .{ .set_selection = desired_selection };
506
+ }
507
+
508
+ const source_len = if (redo) entry.before_text_len else entry.after_text_len;
509
+ const source_hash = if (redo) entry.before_hash else entry.after_hash;
510
+ if (widget.text.len != source_len or current_hash != source_hash) {
511
+ clearCanvasWidgetTextHistory(self, target.id);
512
+ return null;
513
+ }
514
+
515
+ const removed = canvasWidgetTextHistoryRemoved(self, entry);
516
+ const inserted = canvasWidgetTextHistoryInserted(self, entry);
517
+ var output: [max_text_history_edits_per_shortcut]?canvas.TextInputEvent = .{ null, null, null };
518
+ var edit_count: usize = 0;
519
+ if (redo) {
520
+ buildCanvasWidgetTextRedoEdits(entry, removed, inserted, selection, &output, &edit_count);
521
+ } else {
522
+ buildCanvasWidgetTextUndoEdits(entry, removed, inserted, selection, &output, &edit_count);
523
+ }
524
+ return if (edit_count > 0) output[0] else null;
525
+ }
526
+
527
+ /// Commit a pending undo/redo direction only after one replay edit
528
+ /// applied successfully and reached the entry's complete target
529
+ /// state. A storage-budget refusal therefore leaves `applied`
530
+ /// untouched, so the same shortcut remains available after capacity
531
+ /// is freed. Compound replays call this after every select/replace/
532
+ /// restore step; only the final one can satisfy all three witnesses.
533
+ pub fn commitCanvasWidgetTextHistoryReplayIfComplete(
534
+ self: *RuntimeView,
535
+ target: canvas.WidgetFocusTarget,
536
+ serial: u64,
537
+ redo: bool,
538
+ ) void {
539
+ if (serial == 0) return;
540
+ const node_index = self.canvasWidgetNodeIndexById(target.id) orelse return;
541
+ const widget = self.widget_layout_nodes[node_index].widget;
542
+ if (widget.kind != target.kind) return;
543
+ const history_index = canvasWidgetTextHistoryIndexForSerial(self, serial) orelse return;
544
+ const entry = self.canvas_widget_text_history_entries[history_index];
545
+ if (entry.target_id != target.id or
546
+ entry.target_kind != target.kind or
547
+ entry.provisional_composition or
548
+ entry.applied == redo)
549
+ {
550
+ return;
551
+ }
552
+ const desired_len = if (redo) entry.after_text_len else entry.before_text_len;
553
+ const desired_hash = if (redo) entry.after_hash else entry.before_hash;
554
+ const desired_selection = if (redo) entry.after_selection else entry.before_selection;
555
+ const selection = widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len);
556
+ if (widget.text.len != desired_len or
557
+ textHistoryHash(widget.text) != desired_hash or
558
+ !canvasTextSelectionsEqual(selection, desired_selection))
559
+ {
560
+ return;
561
+ }
562
+ self.canvas_widget_text_history_entries[history_index].applied = redo;
563
+ }
564
+
565
+ fn recordCanvasWidgetTextHistory(
566
+ self: *RuntimeView,
567
+ target_id: canvas.ObjectId,
568
+ target_kind: canvas.WidgetKind,
569
+ before: canvas.TextEditState,
570
+ after: canvas.TextEditState,
571
+ provisional_composition: bool,
572
+ ) bool {
573
+ const before_hash = textHistoryHash(before.text);
574
+ if (!canvasWidgetTextHistoryMatchesState(self, target_id, target_kind, before.text.len, before_hash)) {
575
+ clearCanvasWidgetTextHistory(self, target_id);
576
+ }
577
+
578
+ // The composition range identifies the inserted side even when
579
+ // the first preedit is empty (and the before/after text is
580
+ // byte-identical). The removed side is the selection the IME
581
+ // replaced when it opened.
582
+ const before_selection = canvas.snapTextCaretSelection(before.text, before.selection);
583
+ const delta = if (provisional_composition) blk: {
584
+ const inserted = after.composition orelse return false;
585
+ const removed = before_selection.range(before.text.len);
586
+ var prefix_len = removed.start;
587
+ var before_end = removed.end;
588
+ var after_end = inserted.end;
589
+ // A later preedit rewrite can complete a CRLF with either
590
+ // adjacent delimiter even when the first preview did not.
591
+ // Retain that shared context from the start so the committed
592
+ // history replacement never addresses only half of the pair.
593
+ if (prefix_len > 0 and before.text[prefix_len - 1] == '\r') {
594
+ prefix_len -= 1;
595
+ }
596
+ if (before_end < before.text.len and
597
+ after_end < after.text.len and
598
+ before.text[before_end] == '\n' and
599
+ after.text[after_end] == '\n')
600
+ {
601
+ before_end += 1;
602
+ after_end += 1;
603
+ }
604
+ break :blk CanvasWidgetTextHistoryDelta{
605
+ .prefix_len = prefix_len,
606
+ .before_end = before_end,
607
+ .after_end = after_end,
608
+ };
609
+ } else canvasWidgetTextHistoryDelta(before.text, after.text);
610
+ const removed_len = delta.before_end - delta.prefix_len;
611
+ const inserted_len = delta.after_end - delta.prefix_len;
612
+ const byte_len = removed_len + inserted_len;
613
+ if (byte_len == 0 and !provisional_composition) return false;
614
+
615
+ // A completed new edit forks this widget's history: only its
616
+ // redo branch disappears; other editors in the same view keep
617
+ // theirs. A provisional composition defers the fork until
618
+ // commit, so canceling a no-op preedit preserves Redo too.
619
+ if (!provisional_composition) {
620
+ var cursor = self.canvas_widget_text_history_entry_count;
621
+ while (cursor > 0) {
622
+ cursor -= 1;
623
+ const entry = self.canvas_widget_text_history_entries[cursor];
624
+ if (entry.target_id == target_id and !entry.applied) {
625
+ removeCanvasWidgetTextHistoryEntry(self, cursor);
626
+ }
627
+ }
628
+ }
629
+
630
+ if (byte_len > self.canvas_widget_text_history_bytes.len) {
631
+ clearCanvasWidgetTextHistory(self, target_id);
632
+ return false;
633
+ }
634
+ while (self.canvas_widget_text_history_entry_count >= self.canvas_widget_text_history_entries.len or
635
+ self.canvas_widget_text_history_byte_count + byte_len > self.canvas_widget_text_history_bytes.len)
636
+ {
637
+ if (self.canvas_widget_text_history_entry_count == 0) return false;
638
+ removeCanvasWidgetTextHistoryEntry(self, 0);
639
+ }
640
+
641
+ const byte_start = self.canvas_widget_text_history_byte_count;
642
+ const removed_end = byte_start + removed_len;
643
+ const inserted_end = removed_end + inserted_len;
644
+ @memcpy(
645
+ self.canvas_widget_text_history_bytes[byte_start..removed_end],
646
+ before.text[delta.prefix_len..delta.before_end],
647
+ );
648
+ @memcpy(
649
+ self.canvas_widget_text_history_bytes[removed_end..inserted_end],
650
+ after.text[delta.prefix_len..delta.after_end],
651
+ );
652
+ self.canvas_widget_text_history_byte_count = inserted_end;
653
+ const after_hash = textHistoryHash(after.text);
654
+ const serial = self.canvas_widget_text_history_next_serial;
655
+ self.canvas_widget_text_history_next_serial +%= 1;
656
+ if (self.canvas_widget_text_history_next_serial == 0) self.canvas_widget_text_history_next_serial = 1;
657
+ self.canvas_widget_text_history_entries[self.canvas_widget_text_history_entry_count] = .{
658
+ .serial = serial,
659
+ .target_id = target_id,
660
+ .target_kind = target_kind,
661
+ .byte_start = byte_start,
662
+ .removed_len = removed_len,
663
+ .inserted_len = inserted_len,
664
+ .prefix_len = delta.prefix_len,
665
+ .before_text_len = before.text.len,
666
+ .after_text_len = after.text.len,
667
+ .before_hash = before_hash,
668
+ .after_hash = after_hash,
669
+ .before_selection = before_selection,
670
+ .after_selection = after.selection,
671
+ .provisional_composition = provisional_composition,
672
+ };
673
+ self.canvas_widget_text_history_entry_count += 1;
674
+ return true;
675
+ }
676
+
677
+ /// A controlled source replacement can change the retained buffer
678
+ /// without flowing through the text editor. Before a subsequent
679
+ /// edit forks redo, verify that the applied history boundary still
680
+ /// describes that buffer; otherwise retire only this widget's stale
681
+ /// timeline and let the new edit begin a fresh one.
682
+ fn canvasWidgetTextHistoryMatchesState(
683
+ self: *const RuntimeView,
684
+ target_id: canvas.ObjectId,
685
+ target_kind: canvas.WidgetKind,
686
+ text_len: usize,
687
+ text_hash: u64,
688
+ ) bool {
689
+ var latest_applied: ?usize = null;
690
+ for (self.canvas_widget_text_history_entries[0..self.canvas_widget_text_history_entry_count], 0..) |entry, index| {
691
+ if (entry.target_id != target_id) continue;
692
+ if (entry.target_kind != target_kind) return false;
693
+ if (entry.provisional_composition) return false;
694
+ if (entry.applied) latest_applied = index;
695
+ }
696
+ if (latest_applied) |index| {
697
+ const entry = self.canvas_widget_text_history_entries[index];
698
+ return text_len == entry.after_text_len and text_hash == entry.after_hash;
699
+ }
700
+ for (self.canvas_widget_text_history_entries[0..self.canvas_widget_text_history_entry_count]) |entry| {
701
+ if (entry.target_id != target_id or entry.target_kind != target_kind or entry.provisional_composition or entry.applied) continue;
702
+ return text_len == entry.before_text_len and text_hash == entry.before_hash;
703
+ }
704
+ return true;
705
+ }
706
+
707
+ /// Rewrite the inserted side of the active composition's single
708
+ /// provisional entry. The original removed bytes stay untouched,
709
+ /// so commit becomes one undo step and a no-op cancel simply drops
710
+ /// the provisional entry while all older edits remain available.
711
+ fn updateCanvasWidgetTextCompositionHistory(
712
+ self: *RuntimeView,
713
+ target_id: canvas.ObjectId,
714
+ after: canvas.TextEditState,
715
+ ) void {
716
+ var provisional_index = canvasWidgetTextCompositionHistoryIndex(self, target_id) orelse return;
717
+ var provisional = self.canvas_widget_text_history_entries[provisional_index];
718
+ if (provisional.prefix_len + provisional.removed_len > provisional.before_text_len) {
719
+ removeCanvasWidgetTextHistoryEntry(self, provisional_index);
720
+ return;
721
+ }
722
+ const suffix_len = provisional.before_text_len - provisional.prefix_len - provisional.removed_len;
723
+ if (after.text.len < provisional.prefix_len + suffix_len) {
724
+ removeCanvasWidgetTextHistoryEntry(self, provisional_index);
725
+ return;
726
+ }
727
+ const after_end = after.text.len - suffix_len;
728
+ const inserted = after.text[provisional.prefix_len..after_end];
729
+ const new_inserted_len = inserted.len;
730
+ const new_entry_byte_len = provisional.removed_len + new_inserted_len;
731
+ if (new_entry_byte_len > self.canvas_widget_text_history_bytes.len) {
732
+ removeCanvasWidgetTextHistoryEntry(self, provisional_index);
733
+ return;
734
+ }
735
+
736
+ while (self.canvas_widget_text_history_byte_count - provisional.inserted_len + new_inserted_len >
737
+ self.canvas_widget_text_history_bytes.len)
738
+ {
739
+ if (provisional_index == 0) {
740
+ removeCanvasWidgetTextHistoryEntry(self, provisional_index);
741
+ return;
742
+ }
743
+ removeCanvasWidgetTextHistoryEntry(self, 0);
744
+ provisional_index = canvasWidgetTextCompositionHistoryIndex(self, target_id) orelse return;
745
+ provisional = self.canvas_widget_text_history_entries[provisional_index];
746
+ }
747
+
748
+ const inserted_start = provisional.byte_start + provisional.removed_len;
749
+ const old_tail_start = inserted_start + provisional.inserted_len;
750
+ const old_byte_count = self.canvas_widget_text_history_byte_count;
751
+ if (new_inserted_len > provisional.inserted_len) {
752
+ const growth = new_inserted_len - provisional.inserted_len;
753
+ std.mem.copyBackwards(
754
+ u8,
755
+ self.canvas_widget_text_history_bytes[old_tail_start + growth .. old_byte_count + growth],
756
+ self.canvas_widget_text_history_bytes[old_tail_start..old_byte_count],
757
+ );
758
+ for (self.canvas_widget_text_history_entries[provisional_index + 1 .. self.canvas_widget_text_history_entry_count]) |*entry| {
759
+ entry.byte_start += growth;
760
+ }
761
+ self.canvas_widget_text_history_byte_count += growth;
762
+ } else if (new_inserted_len < provisional.inserted_len) {
763
+ const shrink = provisional.inserted_len - new_inserted_len;
764
+ std.mem.copyForwards(
765
+ u8,
766
+ self.canvas_widget_text_history_bytes[old_tail_start - shrink .. old_byte_count - shrink],
767
+ self.canvas_widget_text_history_bytes[old_tail_start..old_byte_count],
768
+ );
769
+ for (self.canvas_widget_text_history_entries[provisional_index + 1 .. self.canvas_widget_text_history_entry_count]) |*entry| {
770
+ entry.byte_start -= shrink;
771
+ }
772
+ self.canvas_widget_text_history_byte_count -= shrink;
773
+ }
774
+ @memcpy(
775
+ self.canvas_widget_text_history_bytes[inserted_start .. inserted_start + new_inserted_len],
776
+ inserted,
777
+ );
778
+
779
+ const after_hash = textHistoryHash(after.text);
780
+ self.canvas_widget_text_history_entries[provisional_index].inserted_len = new_inserted_len;
781
+ self.canvas_widget_text_history_entries[provisional_index].after_text_len = after.text.len;
782
+ self.canvas_widget_text_history_entries[provisional_index].after_hash = after_hash;
783
+ self.canvas_widget_text_history_entries[provisional_index].after_selection = after.selection;
784
+ if (after.composition != null) return;
785
+ if (after.text.len == provisional.before_text_len and after_hash == provisional.before_hash) {
786
+ removeCanvasWidgetTextHistoryEntry(self, provisional_index);
787
+ return;
788
+ }
789
+
790
+ // The composition committed a net edit, so it now forks Redo.
791
+ // Remove that branch before exposing the provisional entry to
792
+ // shortcut lookup; compaction can move the entry, hence refind.
793
+ var cursor = self.canvas_widget_text_history_entry_count;
794
+ while (cursor > 0) {
795
+ cursor -= 1;
796
+ const entry = self.canvas_widget_text_history_entries[cursor];
797
+ if (entry.target_id == target_id and !entry.applied) {
798
+ removeCanvasWidgetTextHistoryEntry(self, cursor);
799
+ }
800
+ }
801
+ provisional_index = canvasWidgetTextCompositionHistoryIndex(self, target_id) orelse return;
802
+ self.canvas_widget_text_history_entries[provisional_index].provisional_composition = false;
803
+ }
804
+
805
+ fn canvasWidgetTextCompositionHistoryIndex(self: *const RuntimeView, target_id: canvas.ObjectId) ?usize {
806
+ var index = self.canvas_widget_text_history_entry_count;
807
+ while (index > 0) {
808
+ index -= 1;
809
+ const entry = self.canvas_widget_text_history_entries[index];
810
+ if (entry.target_id == target_id and entry.provisional_composition) return index;
811
+ }
812
+ return null;
813
+ }
814
+
815
+ fn canvasWidgetTextHistoryIndex(self: *const RuntimeView, target_id: canvas.ObjectId, target_kind: canvas.WidgetKind, redo: bool) ?usize {
816
+ if (redo) {
817
+ for (self.canvas_widget_text_history_entries[0..self.canvas_widget_text_history_entry_count], 0..) |entry, index| {
818
+ if (entry.target_id == target_id and entry.target_kind == target_kind and !entry.applied and !entry.provisional_composition) return index;
819
+ }
820
+ return null;
821
+ }
822
+ var index = self.canvas_widget_text_history_entry_count;
823
+ while (index > 0) {
824
+ index -= 1;
825
+ const entry = self.canvas_widget_text_history_entries[index];
826
+ if (entry.target_id == target_id and entry.target_kind == target_kind and entry.applied and !entry.provisional_composition) return index;
827
+ }
828
+ return null;
829
+ }
830
+
831
+ fn canvasWidgetTextHistoryIndexForSerial(self: *const RuntimeView, serial: u64) ?usize {
832
+ for (self.canvas_widget_text_history_entries[0..self.canvas_widget_text_history_entry_count], 0..) |entry, index| {
833
+ if (entry.serial == serial) return index;
834
+ }
835
+ return null;
836
+ }
837
+
838
+ fn canvasWidgetTextHistoryRemoved(self: *const RuntimeView, entry: CanvasWidgetTextHistoryEntry) []const u8 {
839
+ return self.canvas_widget_text_history_bytes[entry.byte_start .. entry.byte_start + entry.removed_len];
840
+ }
841
+
842
+ fn canvasWidgetTextHistoryInserted(self: *const RuntimeView, entry: CanvasWidgetTextHistoryEntry) []const u8 {
843
+ const start = entry.byte_start + entry.removed_len;
844
+ return self.canvas_widget_text_history_bytes[start .. start + entry.inserted_len];
845
+ }
846
+
847
+ fn clearCanvasWidgetTextHistory(self: *RuntimeView, target_id: canvas.ObjectId) void {
848
+ var index = self.canvas_widget_text_history_entry_count;
849
+ while (index > 0) {
850
+ index -= 1;
851
+ if (self.canvas_widget_text_history_entries[index].target_id == target_id) {
852
+ removeCanvasWidgetTextHistoryEntry(self, index);
853
+ }
854
+ }
855
+ }
856
+
857
+ /// History belongs to one mounted editor incarnation. Widget ids are
858
+ /// stable structural handles, but an id can disappear or return as a
859
+ /// different kind across rebuilds; neither case may inherit edits
860
+ /// from the retired control.
861
+ pub fn pruneCanvasWidgetTextHistory(self: *RuntimeView) void {
862
+ var index = self.canvas_widget_text_history_entry_count;
863
+ while (index > 0) {
864
+ index -= 1;
865
+ const entry = self.canvas_widget_text_history_entries[index];
866
+ const node_index = self.canvasWidgetNodeIndexById(entry.target_id) orelse {
867
+ removeCanvasWidgetTextHistoryEntry(self, index);
868
+ continue;
869
+ };
870
+ const widget = self.widget_layout_nodes[node_index].widget;
871
+ if (widget.kind != entry.target_kind or !canvasWidgetEditableTextKind(widget.kind)) {
872
+ removeCanvasWidgetTextHistoryEntry(self, index);
873
+ }
874
+ }
875
+ if (self.canvas_widget_text_vertical_goal_id != 0) {
876
+ const node_index = self.canvasWidgetNodeIndexById(self.canvas_widget_text_vertical_goal_id) orelse {
877
+ clearCanvasWidgetTextVerticalGoal(self);
878
+ return;
879
+ };
880
+ if (self.widget_layout_nodes[node_index].widget.kind != .textarea) {
881
+ clearCanvasWidgetTextVerticalGoal(self);
882
+ }
883
+ }
884
+ }
885
+
886
+ fn removeCanvasWidgetTextHistoryEntry(self: *RuntimeView, remove_index: usize) void {
887
+ if (remove_index >= self.canvas_widget_text_history_entry_count) return;
888
+ var write_byte: usize = 0;
889
+ var write_entry: usize = 0;
890
+ for (self.canvas_widget_text_history_entries[0..self.canvas_widget_text_history_entry_count], 0..) |entry, index| {
891
+ if (index == remove_index) continue;
892
+ const entry_byte_len = entry.removed_len + entry.inserted_len;
893
+ const source = self.canvas_widget_text_history_bytes[entry.byte_start .. entry.byte_start + entry_byte_len];
894
+ if (write_byte != entry.byte_start) {
895
+ std.mem.copyForwards(
896
+ u8,
897
+ self.canvas_widget_text_history_bytes[write_byte .. write_byte + entry_byte_len],
898
+ source,
899
+ );
900
+ }
901
+ var moved = entry;
902
+ moved.byte_start = write_byte;
903
+ self.canvas_widget_text_history_entries[write_entry] = moved;
904
+ write_byte += entry_byte_len;
905
+ write_entry += 1;
906
+ }
907
+ self.canvas_widget_text_history_entry_count = write_entry;
908
+ self.canvas_widget_text_history_byte_count = write_byte;
909
+ }
910
+
911
+ pub fn clearCanvasWidgetTextVerticalGoal(self: *RuntimeView) void {
912
+ self.canvas_widget_text_vertical_goal_id = 0;
913
+ self.canvas_widget_text_vertical_goal_x = 0;
914
+ self.canvas_widget_text_vertical_goal_text_len = 0;
915
+ self.canvas_widget_text_vertical_goal_text_hash = 0;
916
+ self.canvas_widget_text_vertical_goal_focus = 0;
917
+ self.canvas_widget_text_vertical_goal_affinity = .upstream;
918
+ self.canvas_widget_text_vertical_goal_frame = .{};
919
+ }
920
+
91
921
  pub fn canEditCanvasWidgetText(self: *const RuntimeView, id: canvas.ObjectId) bool {
92
922
  const index = self.canvasWidgetNodeIndexById(id) orelse return false;
93
923
  const layout = self.widgetLayoutTree();
@@ -97,15 +927,33 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
97
927
  return canvasWidgetEditableTextKind(widget.kind) and !widget.state.disabled;
98
928
  }
99
929
 
100
- pub fn applyCanvasWidgetTextPointer(self: *RuntimeView, target_id: canvas.ObjectId, point: geometry.PointF, extend: bool, click_count: u8) anyerror!?geometry.RectF {
930
+ pub fn applyCanvasWidgetTextPointer(
931
+ self: *RuntimeView,
932
+ target_id: canvas.ObjectId,
933
+ point: geometry.PointF,
934
+ dragging: bool,
935
+ shift_extend: bool,
936
+ click_count: u8,
937
+ ) anyerror!?geometry.RectF {
938
+ clearCanvasWidgetTextVerticalGoal(self);
101
939
  const index = self.canvasWidgetNodeIndexById(target_id) orelse return null;
102
940
  const widget = self.widget_layout_nodes[index].widget;
103
941
  if (widget.state.disabled) return null;
104
- if (canvas.widgetStaticTextSelectable(widget)) return applyCanvasWidgetStaticTextPointer(self, index, target_id, point, extend);
942
+ if (canvas.widgetStaticTextSelectable(widget)) {
943
+ return applyCanvasWidgetStaticTextPointer(self, index, target_id, point, dragging);
944
+ }
105
945
  if (!canvasWidgetEditableTextKind(widget.kind)) return null;
106
946
 
107
947
  const current_selection = widget.text_selection orelse canvas.TextSelection.collapsed(widget.text.len);
108
- const next_selection = canvasWidgetEditableTextPointerSelection(self, widget, point, extend, click_count, current_selection) orelse return null;
948
+ const next_selection = canvasWidgetEditableTextPointerSelection(
949
+ self,
950
+ widget,
951
+ point,
952
+ dragging,
953
+ shift_extend,
954
+ click_count,
955
+ current_selection,
956
+ ) orelse return null;
109
957
  // A widget with NO stored selection must store one even when
110
958
  // it matches the implied default: the emitters draw a caret
111
959
  // only for a present selection, so short-circuiting here left
@@ -115,6 +963,13 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
115
963
 
116
964
  self.widget_layout_nodes[index].widget.text_selection = next_selection;
117
965
  self.widget_layout_nodes[index].widget.text_composition = null;
966
+ if (widget.text_composition != null) {
967
+ updateCanvasWidgetTextCompositionHistory(self, target_id, .{
968
+ .text = widget.text,
969
+ .selection = next_selection,
970
+ .composition = null,
971
+ });
972
+ }
118
973
  // A pointer-placed caret is a caret change like any other: a
119
974
  // drag past a scrolled single-line field's edge lands on an
120
975
  // off-screen offset, and the field follows it.
@@ -140,20 +995,34 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
140
995
  self: *RuntimeView,
141
996
  widget: canvas.Widget,
142
997
  point: geometry.PointF,
143
- extend: bool,
998
+ dragging: bool,
999
+ shift_extend: bool,
144
1000
  click_count: u8,
145
1001
  current_selection: canvas.TextSelection,
146
1002
  ) ?canvas.TextSelection {
147
1003
  if (click_count >= 2) {
148
1004
  const offset = canvas.textOffsetForWidgetPoint(widget, point, self.widget_tokens) orelse return null;
149
1005
  const unit = canvasWidgetMultiClickUnitSelection(widget, offset, click_count);
150
- if (!extend) {
1006
+ if (shift_extend) {
1007
+ // Shift+double/triple-click starts a fresh unit-wise
1008
+ // extension from the standing selection anchor. It is
1009
+ // not a drag continuation: the old multi-click anchor
1010
+ // may belong to an unrelated gesture or widget.
1011
+ const anchor_unit = canvasWidgetMultiClickUnitSelection(
1012
+ widget,
1013
+ current_selection.anchor,
1014
+ click_count,
1015
+ ).range(widget.text.len);
1016
+ self.canvas_widget_multi_click_anchor = anchor_unit;
1017
+ return canvasWidgetMultiClickDragSelection(anchor_unit, unit, widget.text.len);
1018
+ }
1019
+ if (!dragging) {
151
1020
  self.canvas_widget_multi_click_anchor = unit.range(widget.text.len);
152
1021
  return unit;
153
1022
  }
154
1023
  return canvasWidgetMultiClickDragSelection(self.canvas_widget_multi_click_anchor, unit, widget.text.len);
155
1024
  }
156
- const anchor: ?usize = if (extend) current_selection.anchor else null;
1025
+ const anchor: ?usize = if (dragging or shift_extend) current_selection.anchor else null;
157
1026
  return canvas.textSelectionForWidgetPoint(widget, point, anchor, self.widget_tokens);
158
1027
  }
159
1028
 
@@ -258,6 +1127,7 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
258
1127
  }
259
1128
 
260
1129
  pub fn rewriteCanvasWidgetTextStorage(self: *RuntimeView, edited_index: usize, next_state: canvas.TextEditState) anyerror!void {
1130
+ try validateCanvasWidgetTextStorageRewrite(self, edited_index, next_state);
261
1131
  var temp: [max_canvas_widget_text_bytes_per_view]u8 = undefined;
262
1132
  var text_ranges: [max_canvas_widget_nodes_per_view]WidgetTextStorageRange = undefined;
263
1133
  var label_ranges: [max_canvas_widget_nodes_per_view]WidgetTextStorageRange = undefined;
@@ -285,17 +1155,40 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
285
1155
  self.widget_layout_nodes[edited_index].widget.text_composition = next_state.composition;
286
1156
  }
287
1157
 
1158
+ /// Prove a retained text rewrite fits before any editor history is
1159
+ /// forked, evicted, or cleared. The immediately following rewrite
1160
+ /// charges these exact three slices for every retained node.
1161
+ fn validateCanvasWidgetTextStorageRewrite(self: *const RuntimeView, edited_index: usize, next_state: canvas.TextEditState) anyerror!void {
1162
+ var required_len: usize = 0;
1163
+ for (self.widget_layout_nodes[0..self.widget_layout_node_count], 0..) |node, index| {
1164
+ const text = if (index == edited_index) next_state.text else node.widget.text;
1165
+ const values = [_][]const u8{
1166
+ text,
1167
+ node.widget.semantics.label,
1168
+ node.widget.command,
1169
+ };
1170
+ for (values) |value| {
1171
+ if (value.len > self.widget_text_bytes.len - required_len) return error.WidgetTextTooLarge;
1172
+ required_len += value.len;
1173
+ }
1174
+ }
1175
+ }
1176
+
288
1177
  pub fn setCanvasWidgetTextValue(self: *RuntimeView, id: canvas.ObjectId, text: []const u8) anyerror!?geometry.RectF {
289
1178
  const index = self.canvasWidgetNodeIndexById(id) orelse return null;
290
1179
  const widget = self.widget_layout_nodes[index].widget;
291
1180
  if (!canvasWidgetEditableTextKind(widget.kind) or widget.state.disabled) return null;
292
1181
  if (std.mem.eql(u8, widget.text, text) and widget.text_composition == null and textSelectionCollapsedAt(widget.text_selection, text.len)) return null;
293
1182
 
294
- try self.rewriteCanvasWidgetTextStorage(index, .{
1183
+ const next_state = canvas.TextEditState{
295
1184
  .text = text,
296
1185
  .selection = canvas.TextSelection.collapsed(text.len),
297
1186
  .composition = null,
298
- });
1187
+ };
1188
+ try validateCanvasWidgetTextStorageRewrite(self, index, next_state);
1189
+ clearCanvasWidgetTextVerticalGoal(self);
1190
+ clearCanvasWidgetTextHistory(self, id);
1191
+ try self.rewriteCanvasWidgetTextStorage(index, next_state);
299
1192
  self.scrollCanvasTextInputCaretIntoView(index);
300
1193
  try self.refreshCanvasWidgetSemantics();
301
1194
  self.widget_revision += 1;
@@ -303,3 +1196,148 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
303
1196
  }
304
1197
  };
305
1198
  }
1199
+
1200
+ const CanvasWidgetTextHistoryDelta = struct {
1201
+ prefix_len: usize,
1202
+ before_end: usize,
1203
+ after_end: usize,
1204
+ };
1205
+
1206
+ fn canvasWidgetTextHistoryDelta(before: []const u8, after: []const u8) CanvasWidgetTextHistoryDelta {
1207
+ var prefix_len: usize = 0;
1208
+ const shared_len = @min(before.len, after.len);
1209
+ while (prefix_len < shared_len and before[prefix_len] == after[prefix_len]) prefix_len += 1;
1210
+ prefix_len = @min(
1211
+ canvas.snapTextCaretPosition(before, .{ .offset = prefix_len }).offset,
1212
+ canvas.snapTextCaretPosition(after, .{ .offset = prefix_len }).offset,
1213
+ );
1214
+
1215
+ var suffix_len: usize = 0;
1216
+ while (suffix_len < before.len - prefix_len and
1217
+ suffix_len < after.len - prefix_len and
1218
+ before[before.len - suffix_len - 1] == after[after.len - suffix_len - 1])
1219
+ {
1220
+ suffix_len += 1;
1221
+ }
1222
+ // A common byte suffix can begin inside a shared UTF-8 sequence when
1223
+ // two codepoints share continuation bytes, or between the CR and LF
1224
+ // when this edit completed a CRLF. Shrink it until both replacement
1225
+ // ends are caret-safe boundaries.
1226
+ while (suffix_len > 0) {
1227
+ const before_end = before.len - suffix_len;
1228
+ const after_end = after.len - suffix_len;
1229
+ if (canvas.snapTextCaretPosition(before, .{ .offset = before_end }).offset == before_end and
1230
+ canvas.snapTextCaretPosition(after, .{ .offset = after_end }).offset == after_end)
1231
+ {
1232
+ break;
1233
+ }
1234
+ suffix_len -= 1;
1235
+ }
1236
+ return .{
1237
+ .prefix_len = prefix_len,
1238
+ .before_end = before.len - suffix_len,
1239
+ .after_end = after.len - suffix_len,
1240
+ };
1241
+ }
1242
+
1243
+ fn textHistoryHash(text: []const u8) u64 {
1244
+ return std.hash.Wyhash.hash(0, text);
1245
+ }
1246
+
1247
+ fn historySelectionCollapsedAt(selection: canvas.TextSelection, offset: usize) bool {
1248
+ return selection.anchor == offset and selection.focus == offset;
1249
+ }
1250
+
1251
+ fn historySingleCodepoint(text: []const u8) bool {
1252
+ return text.len > 0 and canvas.snapTextOffset(text, text.len - 1) == 0;
1253
+ }
1254
+
1255
+ fn appendCanvasWidgetTextHistoryEdit(
1256
+ output: *[max_text_history_edits_per_shortcut]?canvas.TextInputEvent,
1257
+ count: *usize,
1258
+ edit: canvas.TextInputEvent,
1259
+ ) void {
1260
+ if (count.* >= output.len) return;
1261
+ output[count.*] = edit;
1262
+ count.* += 1;
1263
+ }
1264
+
1265
+ fn buildCanvasWidgetTextUndoEdits(
1266
+ entry: CanvasWidgetTextHistoryEntry,
1267
+ removed: []const u8,
1268
+ inserted: []const u8,
1269
+ current_selection: canvas.TextSelection,
1270
+ output: *[max_text_history_edits_per_shortcut]?canvas.TextInputEvent,
1271
+ count: *usize,
1272
+ ) void {
1273
+ const prefix = entry.prefix_len;
1274
+ if (removed.len == 0 and
1275
+ historySingleCodepoint(inserted) and
1276
+ historySelectionCollapsedAt(current_selection, prefix + inserted.len) and
1277
+ canvasTextSelectionsEqual(entry.before_selection, canvas.TextSelection.collapsed(prefix)))
1278
+ {
1279
+ appendCanvasWidgetTextHistoryEdit(output, count, .delete_backward);
1280
+ return;
1281
+ }
1282
+ if (inserted.len == 0 and
1283
+ historySelectionCollapsedAt(current_selection, prefix) and
1284
+ canvasTextSelectionsEqual(entry.before_selection, canvas.TextSelection.collapsed(prefix + removed.len)))
1285
+ {
1286
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = removed });
1287
+ return;
1288
+ }
1289
+
1290
+ const replacement_selection = canvas.TextSelection{ .anchor = prefix, .focus = prefix + inserted.len };
1291
+ if (!canvasTextSelectionsEqual(current_selection, replacement_selection)) {
1292
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = replacement_selection });
1293
+ }
1294
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = removed });
1295
+ const insertion_selection = canvas.TextSelection.collapsed(prefix + removed.len);
1296
+ if (!canvasTextSelectionsEqual(insertion_selection, entry.before_selection)) {
1297
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = entry.before_selection });
1298
+ }
1299
+ }
1300
+
1301
+ fn buildCanvasWidgetTextRedoEdits(
1302
+ entry: CanvasWidgetTextHistoryEntry,
1303
+ removed: []const u8,
1304
+ inserted: []const u8,
1305
+ current_selection: canvas.TextSelection,
1306
+ output: *[max_text_history_edits_per_shortcut]?canvas.TextInputEvent,
1307
+ count: *usize,
1308
+ ) void {
1309
+ const prefix = entry.prefix_len;
1310
+ if (removed.len == 0 and
1311
+ historySelectionCollapsedAt(current_selection, prefix) and
1312
+ canvasTextSelectionsEqual(entry.after_selection, canvas.TextSelection.collapsed(prefix + inserted.len)))
1313
+ {
1314
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = inserted });
1315
+ return;
1316
+ }
1317
+ if (historySingleCodepoint(removed) and
1318
+ inserted.len == 0 and
1319
+ historySelectionCollapsedAt(current_selection, prefix + removed.len) and
1320
+ canvasTextSelectionsEqual(entry.after_selection, canvas.TextSelection.collapsed(prefix)))
1321
+ {
1322
+ appendCanvasWidgetTextHistoryEdit(output, count, .delete_backward);
1323
+ return;
1324
+ }
1325
+ if (historySingleCodepoint(removed) and
1326
+ inserted.len == 0 and
1327
+ historySelectionCollapsedAt(current_selection, prefix) and
1328
+ canvasTextSelectionsEqual(entry.after_selection, canvas.TextSelection.collapsed(prefix)))
1329
+ {
1330
+ appendCanvasWidgetTextHistoryEdit(output, count, .delete_forward);
1331
+ return;
1332
+ }
1333
+
1334
+ const replacement_selection = canvas.TextSelection{ .anchor = prefix, .focus = prefix + removed.len };
1335
+ if (!canvasTextSelectionsEqual(current_selection, replacement_selection)) {
1336
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = replacement_selection });
1337
+ }
1338
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = inserted });
1339
+ const insertion_selection = canvas.TextSelection.collapsed(prefix + inserted.len);
1340
+ if (!canvasTextSelectionsEqual(insertion_selection, entry.after_selection)) {
1341
+ appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = entry.after_selection });
1342
+ }
1343
+ }