@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
|
@@ -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
|
-
|
|
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
|
-
|
|
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(
|
|
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))
|
|
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(
|
|
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
|
-
|
|
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 (
|
|
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 (
|
|
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
|
|
|
@@ -192,11 +1061,19 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
192
1061
|
|
|
193
1062
|
/// Click-drag selection inside one static `.text` widget. Press
|
|
194
1063
|
/// collapses at the hit offset, drag extends from the press
|
|
195
|
-
/// anchor.
|
|
196
|
-
///
|
|
197
|
-
/// document model ordering text across widgets to extend into.
|
|
1064
|
+
/// anchor. Ordinary widgets remain independently selectable;
|
|
1065
|
+
/// bounded code paragraphs opt into one ordered source group.
|
|
198
1066
|
fn applyCanvasWidgetStaticTextPointer(self: *RuntimeView, index: usize, target_id: canvas.ObjectId, point: geometry.PointF, extend: bool) anyerror!?geometry.RectF {
|
|
199
1067
|
const widget = self.widget_layout_nodes[index].widget;
|
|
1068
|
+
if (widget.static_text_group_id != 0) {
|
|
1069
|
+
return applyCanvasWidgetStaticTextGroupPointer(
|
|
1070
|
+
self,
|
|
1071
|
+
target_id,
|
|
1072
|
+
point,
|
|
1073
|
+
extend,
|
|
1074
|
+
widget.static_text_group_id,
|
|
1075
|
+
);
|
|
1076
|
+
}
|
|
200
1077
|
if (extend and self.canvas_widget_selected_text_id != target_id) return null;
|
|
201
1078
|
const current_selection = widget.text_selection orelse canvas.TextSelection.collapsed(0);
|
|
202
1079
|
const anchor: ?usize = if (extend) current_selection.anchor else null;
|
|
@@ -205,11 +1082,130 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
205
1082
|
|
|
206
1083
|
self.widget_layout_nodes[index].widget.text_selection = next_selection;
|
|
207
1084
|
self.canvas_widget_selected_text_id = target_id;
|
|
1085
|
+
self.canvas_widget_selected_text_group_id = 0;
|
|
1086
|
+
self.canvas_widget_selected_text_group_anchor = 0;
|
|
1087
|
+
self.canvas_widget_selected_text_group_focus = 0;
|
|
208
1088
|
try self.refreshCanvasWidgetSemantics();
|
|
209
1089
|
self.widget_revision += 1;
|
|
210
1090
|
return self.canvasWidgetDirtyBounds(index, widget.frame);
|
|
211
1091
|
}
|
|
212
1092
|
|
|
1093
|
+
fn applyCanvasWidgetStaticTextGroupPointer(
|
|
1094
|
+
self: *RuntimeView,
|
|
1095
|
+
target_id: canvas.ObjectId,
|
|
1096
|
+
point: geometry.PointF,
|
|
1097
|
+
extend: bool,
|
|
1098
|
+
group_id: canvas.ObjectId,
|
|
1099
|
+
) anyerror!?geometry.RectF {
|
|
1100
|
+
if (extend and
|
|
1101
|
+
(self.canvas_widget_selected_text_id != target_id or
|
|
1102
|
+
self.canvas_widget_selected_text_group_id != group_id))
|
|
1103
|
+
{
|
|
1104
|
+
return null;
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
var point_index: ?usize = null;
|
|
1108
|
+
var point_offset: usize = 0;
|
|
1109
|
+
var best_distance = std.math.inf(f32);
|
|
1110
|
+
const layout = self.widgetLayoutTree();
|
|
1111
|
+
for (self.widget_layout_nodes[0..self.widget_layout_node_count], 0..) |node, candidate_index| {
|
|
1112
|
+
const candidate = node.widget;
|
|
1113
|
+
if (candidate.static_text_group_id != group_id or
|
|
1114
|
+
canvasWidgetLayoutNodeHidden(layout, candidate_index) or
|
|
1115
|
+
!canvas.widgetStaticTextSelectable(candidate))
|
|
1116
|
+
{
|
|
1117
|
+
continue;
|
|
1118
|
+
}
|
|
1119
|
+
const frame = candidate.frame.normalized();
|
|
1120
|
+
const dx = if (point.x < frame.x)
|
|
1121
|
+
frame.x - point.x
|
|
1122
|
+
else if (point.x > frame.maxX())
|
|
1123
|
+
point.x - frame.maxX()
|
|
1124
|
+
else
|
|
1125
|
+
0;
|
|
1126
|
+
const dy = if (point.y < frame.y)
|
|
1127
|
+
frame.y - point.y
|
|
1128
|
+
else if (point.y > frame.maxY())
|
|
1129
|
+
point.y - frame.maxY()
|
|
1130
|
+
else
|
|
1131
|
+
0;
|
|
1132
|
+
const distance = dx * dx + dy * dy;
|
|
1133
|
+
if (distance > best_distance) continue;
|
|
1134
|
+
const local = canvas.staticTextSelectionForWidgetPoint(
|
|
1135
|
+
candidate,
|
|
1136
|
+
point,
|
|
1137
|
+
null,
|
|
1138
|
+
self.widget_tokens,
|
|
1139
|
+
) orelse continue;
|
|
1140
|
+
if (distance == best_distance and point_index != null) continue;
|
|
1141
|
+
best_distance = distance;
|
|
1142
|
+
point_index = candidate_index;
|
|
1143
|
+
point_offset = local.focus;
|
|
1144
|
+
}
|
|
1145
|
+
const selected_index = point_index orelse return null;
|
|
1146
|
+
const selected_widget = self.widget_layout_nodes[selected_index].widget;
|
|
1147
|
+
const focus = selected_widget.static_text_group_offset + point_offset;
|
|
1148
|
+
const anchor = if (extend)
|
|
1149
|
+
self.canvas_widget_selected_text_group_anchor
|
|
1150
|
+
else
|
|
1151
|
+
focus;
|
|
1152
|
+
const selection_start = @min(anchor, focus);
|
|
1153
|
+
const selection_end = @max(anchor, focus);
|
|
1154
|
+
|
|
1155
|
+
var dirty: ?geometry.RectF = null;
|
|
1156
|
+
var changed = false;
|
|
1157
|
+
for (self.widget_layout_nodes[0..self.widget_layout_node_count], 0..) |*node, candidate_index| {
|
|
1158
|
+
const candidate = node.widget;
|
|
1159
|
+
if (candidate.static_text_group_id != group_id) {
|
|
1160
|
+
continue;
|
|
1161
|
+
}
|
|
1162
|
+
const source_start = candidate.static_text_group_offset;
|
|
1163
|
+
const source_end = source_start +| candidate.text.len;
|
|
1164
|
+
const next_selection: ?canvas.TextSelection = if (selection_start == selection_end)
|
|
1165
|
+
if (candidate_index == selected_index)
|
|
1166
|
+
canvas.TextSelection.collapsed(point_offset)
|
|
1167
|
+
else
|
|
1168
|
+
null
|
|
1169
|
+
else if (@max(selection_start, source_start) < @min(selection_end, source_end))
|
|
1170
|
+
.{
|
|
1171
|
+
.anchor = @max(selection_start, source_start) - source_start,
|
|
1172
|
+
.focus = @min(selection_end, source_end) - source_start,
|
|
1173
|
+
}
|
|
1174
|
+
else
|
|
1175
|
+
null;
|
|
1176
|
+
const current_selection = candidate.text_selection;
|
|
1177
|
+
const same = if (current_selection) |current|
|
|
1178
|
+
if (next_selection) |next|
|
|
1179
|
+
canvasTextSelectionsEqual(current, next)
|
|
1180
|
+
else
|
|
1181
|
+
false
|
|
1182
|
+
else
|
|
1183
|
+
next_selection == null;
|
|
1184
|
+
if (same) continue;
|
|
1185
|
+
node.widget.text_selection = next_selection;
|
|
1186
|
+
changed = true;
|
|
1187
|
+
dirty = unionRects(
|
|
1188
|
+
dirty,
|
|
1189
|
+
self.canvasWidgetDirtyBounds(candidate_index, candidate.frame),
|
|
1190
|
+
);
|
|
1191
|
+
}
|
|
1192
|
+
if (!changed and
|
|
1193
|
+
self.canvas_widget_selected_text_id == target_id and
|
|
1194
|
+
self.canvas_widget_selected_text_group_anchor == anchor and
|
|
1195
|
+
self.canvas_widget_selected_text_group_focus == focus)
|
|
1196
|
+
{
|
|
1197
|
+
return null;
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
self.canvas_widget_selected_text_id = target_id;
|
|
1201
|
+
self.canvas_widget_selected_text_group_id = group_id;
|
|
1202
|
+
self.canvas_widget_selected_text_group_anchor = anchor;
|
|
1203
|
+
self.canvas_widget_selected_text_group_focus = focus;
|
|
1204
|
+
try self.refreshCanvasWidgetSemantics();
|
|
1205
|
+
self.widget_revision += 1;
|
|
1206
|
+
return dirty;
|
|
1207
|
+
}
|
|
1208
|
+
|
|
213
1209
|
/// Drop the view's static text selection (pointer pressed
|
|
214
1210
|
/// elsewhere, or the copy source went away). Returns the dirty
|
|
215
1211
|
/// bounds of the widget that lost its highlight.
|
|
@@ -217,6 +1213,31 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
217
1213
|
const id = self.canvas_widget_selected_text_id;
|
|
218
1214
|
if (id == 0) return null;
|
|
219
1215
|
self.canvas_widget_selected_text_id = 0;
|
|
1216
|
+
const group_id = self.canvas_widget_selected_text_group_id;
|
|
1217
|
+
self.canvas_widget_selected_text_group_id = 0;
|
|
1218
|
+
self.canvas_widget_selected_text_group_anchor = 0;
|
|
1219
|
+
self.canvas_widget_selected_text_group_focus = 0;
|
|
1220
|
+
if (group_id != 0) {
|
|
1221
|
+
var dirty: ?geometry.RectF = null;
|
|
1222
|
+
var changed = false;
|
|
1223
|
+
for (self.widget_layout_nodes[0..self.widget_layout_node_count], 0..) |*node, index| {
|
|
1224
|
+
if (node.widget.static_text_group_id != group_id or
|
|
1225
|
+
node.widget.text_selection == null)
|
|
1226
|
+
{
|
|
1227
|
+
continue;
|
|
1228
|
+
}
|
|
1229
|
+
node.widget.text_selection = null;
|
|
1230
|
+
changed = true;
|
|
1231
|
+
dirty = unionRects(
|
|
1232
|
+
dirty,
|
|
1233
|
+
self.canvasWidgetDirtyBounds(index, node.widget.frame),
|
|
1234
|
+
);
|
|
1235
|
+
}
|
|
1236
|
+
if (!changed) return null;
|
|
1237
|
+
try self.refreshCanvasWidgetSemantics();
|
|
1238
|
+
self.widget_revision += 1;
|
|
1239
|
+
return dirty;
|
|
1240
|
+
}
|
|
220
1241
|
const index = self.canvasWidgetNodeIndexById(id) orelse return null;
|
|
221
1242
|
if (self.widget_layout_nodes[index].widget.text_selection == null) return null;
|
|
222
1243
|
self.widget_layout_nodes[index].widget.text_selection = null;
|
|
@@ -229,7 +1250,7 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
229
1250
|
/// focused editable widget's selection or focused terminal's
|
|
230
1251
|
/// emulator selection when it has one, else the view's static
|
|
231
1252
|
/// text selection.
|
|
232
|
-
pub fn canvasWidgetCopyText(self: *const RuntimeView) ?[]const u8 {
|
|
1253
|
+
pub fn canvasWidgetCopyText(self: *const RuntimeView, group_buffer: []u8) ?[]const u8 {
|
|
233
1254
|
if (self.canvas_widget_focused_id != 0) {
|
|
234
1255
|
if (self.canvasWidgetNodeIndexById(self.canvas_widget_focused_id)) |index| {
|
|
235
1256
|
const focused = self.widget_layout_nodes[index].widget;
|
|
@@ -242,11 +1263,49 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
242
1263
|
if (canvasWidgetSelectionSliceById(self, self.canvas_widget_focused_id, true)) |slice| return slice;
|
|
243
1264
|
}
|
|
244
1265
|
if (self.canvas_widget_selected_text_id != 0) {
|
|
1266
|
+
if (self.canvas_widget_selected_text_group_id != 0) {
|
|
1267
|
+
return canvasWidgetStaticTextGroupCopy(self, group_buffer);
|
|
1268
|
+
}
|
|
245
1269
|
if (canvasWidgetSelectionSliceById(self, self.canvas_widget_selected_text_id, false)) |slice| return slice;
|
|
246
1270
|
}
|
|
247
1271
|
return null;
|
|
248
1272
|
}
|
|
249
1273
|
|
|
1274
|
+
fn canvasWidgetStaticTextGroupCopy(self: *const RuntimeView, output: []u8) ?[]const u8 {
|
|
1275
|
+
const group_id = self.canvas_widget_selected_text_group_id;
|
|
1276
|
+
if (group_id == 0) return null;
|
|
1277
|
+
const start = @min(
|
|
1278
|
+
self.canvas_widget_selected_text_group_anchor,
|
|
1279
|
+
self.canvas_widget_selected_text_group_focus,
|
|
1280
|
+
);
|
|
1281
|
+
const end = @max(
|
|
1282
|
+
self.canvas_widget_selected_text_group_anchor,
|
|
1283
|
+
self.canvas_widget_selected_text_group_focus,
|
|
1284
|
+
);
|
|
1285
|
+
if (start >= end or end - start > output.len) return null;
|
|
1286
|
+
var copied: usize = 0;
|
|
1287
|
+
for (self.widget_layout_nodes[0..self.widget_layout_node_count]) |node| {
|
|
1288
|
+
const widget = node.widget;
|
|
1289
|
+
if (widget.static_text_group_id != group_id) {
|
|
1290
|
+
continue;
|
|
1291
|
+
}
|
|
1292
|
+
const source_start = widget.static_text_group_offset;
|
|
1293
|
+
const source_end = source_start +| widget.text.len;
|
|
1294
|
+
const copy_start = @max(start, source_start);
|
|
1295
|
+
const copy_end = @min(end, source_end);
|
|
1296
|
+
if (copy_start >= copy_end) continue;
|
|
1297
|
+
const destination_start = copy_start - start;
|
|
1298
|
+
const len = copy_end - copy_start;
|
|
1299
|
+
@memcpy(
|
|
1300
|
+
output[destination_start..][0..len],
|
|
1301
|
+
widget.text[copy_start - source_start ..][0..len],
|
|
1302
|
+
);
|
|
1303
|
+
copied += len;
|
|
1304
|
+
}
|
|
1305
|
+
if (copied != end - start) return null;
|
|
1306
|
+
return output[0 .. end - start];
|
|
1307
|
+
}
|
|
1308
|
+
|
|
250
1309
|
fn canvasWidgetSelectionSliceById(self: *const RuntimeView, id: canvas.ObjectId, editable_only: bool) ?[]const u8 {
|
|
251
1310
|
const index = self.canvasWidgetNodeIndexById(id) orelse return null;
|
|
252
1311
|
const widget = self.widget_layout_nodes[index].widget;
|
|
@@ -258,6 +1317,7 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
258
1317
|
}
|
|
259
1318
|
|
|
260
1319
|
pub fn rewriteCanvasWidgetTextStorage(self: *RuntimeView, edited_index: usize, next_state: canvas.TextEditState) anyerror!void {
|
|
1320
|
+
try validateCanvasWidgetTextStorageRewrite(self, edited_index, next_state);
|
|
261
1321
|
var temp: [max_canvas_widget_text_bytes_per_view]u8 = undefined;
|
|
262
1322
|
var text_ranges: [max_canvas_widget_nodes_per_view]WidgetTextStorageRange = undefined;
|
|
263
1323
|
var label_ranges: [max_canvas_widget_nodes_per_view]WidgetTextStorageRange = undefined;
|
|
@@ -285,17 +1345,40 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
285
1345
|
self.widget_layout_nodes[edited_index].widget.text_composition = next_state.composition;
|
|
286
1346
|
}
|
|
287
1347
|
|
|
1348
|
+
/// Prove a retained text rewrite fits before any editor history is
|
|
1349
|
+
/// forked, evicted, or cleared. The immediately following rewrite
|
|
1350
|
+
/// charges these exact three slices for every retained node.
|
|
1351
|
+
fn validateCanvasWidgetTextStorageRewrite(self: *const RuntimeView, edited_index: usize, next_state: canvas.TextEditState) anyerror!void {
|
|
1352
|
+
var required_len: usize = 0;
|
|
1353
|
+
for (self.widget_layout_nodes[0..self.widget_layout_node_count], 0..) |node, index| {
|
|
1354
|
+
const text = if (index == edited_index) next_state.text else node.widget.text;
|
|
1355
|
+
const values = [_][]const u8{
|
|
1356
|
+
text,
|
|
1357
|
+
node.widget.semantics.label,
|
|
1358
|
+
node.widget.command,
|
|
1359
|
+
};
|
|
1360
|
+
for (values) |value| {
|
|
1361
|
+
if (value.len > self.widget_text_bytes.len - required_len) return error.WidgetTextTooLarge;
|
|
1362
|
+
required_len += value.len;
|
|
1363
|
+
}
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
|
|
288
1367
|
pub fn setCanvasWidgetTextValue(self: *RuntimeView, id: canvas.ObjectId, text: []const u8) anyerror!?geometry.RectF {
|
|
289
1368
|
const index = self.canvasWidgetNodeIndexById(id) orelse return null;
|
|
290
1369
|
const widget = self.widget_layout_nodes[index].widget;
|
|
291
1370
|
if (!canvasWidgetEditableTextKind(widget.kind) or widget.state.disabled) return null;
|
|
292
1371
|
if (std.mem.eql(u8, widget.text, text) and widget.text_composition == null and textSelectionCollapsedAt(widget.text_selection, text.len)) return null;
|
|
293
1372
|
|
|
294
|
-
|
|
1373
|
+
const next_state = canvas.TextEditState{
|
|
295
1374
|
.text = text,
|
|
296
1375
|
.selection = canvas.TextSelection.collapsed(text.len),
|
|
297
1376
|
.composition = null,
|
|
298
|
-
}
|
|
1377
|
+
};
|
|
1378
|
+
try validateCanvasWidgetTextStorageRewrite(self, index, next_state);
|
|
1379
|
+
clearCanvasWidgetTextVerticalGoal(self);
|
|
1380
|
+
clearCanvasWidgetTextHistory(self, id);
|
|
1381
|
+
try self.rewriteCanvasWidgetTextStorage(index, next_state);
|
|
299
1382
|
self.scrollCanvasTextInputCaretIntoView(index);
|
|
300
1383
|
try self.refreshCanvasWidgetSemantics();
|
|
301
1384
|
self.widget_revision += 1;
|
|
@@ -303,3 +1386,148 @@ pub fn RuntimeViewCanvasWidgetText(comptime RuntimeView: type) type {
|
|
|
303
1386
|
}
|
|
304
1387
|
};
|
|
305
1388
|
}
|
|
1389
|
+
|
|
1390
|
+
const CanvasWidgetTextHistoryDelta = struct {
|
|
1391
|
+
prefix_len: usize,
|
|
1392
|
+
before_end: usize,
|
|
1393
|
+
after_end: usize,
|
|
1394
|
+
};
|
|
1395
|
+
|
|
1396
|
+
fn canvasWidgetTextHistoryDelta(before: []const u8, after: []const u8) CanvasWidgetTextHistoryDelta {
|
|
1397
|
+
var prefix_len: usize = 0;
|
|
1398
|
+
const shared_len = @min(before.len, after.len);
|
|
1399
|
+
while (prefix_len < shared_len and before[prefix_len] == after[prefix_len]) prefix_len += 1;
|
|
1400
|
+
prefix_len = @min(
|
|
1401
|
+
canvas.snapTextCaretPosition(before, .{ .offset = prefix_len }).offset,
|
|
1402
|
+
canvas.snapTextCaretPosition(after, .{ .offset = prefix_len }).offset,
|
|
1403
|
+
);
|
|
1404
|
+
|
|
1405
|
+
var suffix_len: usize = 0;
|
|
1406
|
+
while (suffix_len < before.len - prefix_len and
|
|
1407
|
+
suffix_len < after.len - prefix_len and
|
|
1408
|
+
before[before.len - suffix_len - 1] == after[after.len - suffix_len - 1])
|
|
1409
|
+
{
|
|
1410
|
+
suffix_len += 1;
|
|
1411
|
+
}
|
|
1412
|
+
// A common byte suffix can begin inside a shared UTF-8 sequence when
|
|
1413
|
+
// two codepoints share continuation bytes, or between the CR and LF
|
|
1414
|
+
// when this edit completed a CRLF. Shrink it until both replacement
|
|
1415
|
+
// ends are caret-safe boundaries.
|
|
1416
|
+
while (suffix_len > 0) {
|
|
1417
|
+
const before_end = before.len - suffix_len;
|
|
1418
|
+
const after_end = after.len - suffix_len;
|
|
1419
|
+
if (canvas.snapTextCaretPosition(before, .{ .offset = before_end }).offset == before_end and
|
|
1420
|
+
canvas.snapTextCaretPosition(after, .{ .offset = after_end }).offset == after_end)
|
|
1421
|
+
{
|
|
1422
|
+
break;
|
|
1423
|
+
}
|
|
1424
|
+
suffix_len -= 1;
|
|
1425
|
+
}
|
|
1426
|
+
return .{
|
|
1427
|
+
.prefix_len = prefix_len,
|
|
1428
|
+
.before_end = before.len - suffix_len,
|
|
1429
|
+
.after_end = after.len - suffix_len,
|
|
1430
|
+
};
|
|
1431
|
+
}
|
|
1432
|
+
|
|
1433
|
+
fn textHistoryHash(text: []const u8) u64 {
|
|
1434
|
+
return std.hash.Wyhash.hash(0, text);
|
|
1435
|
+
}
|
|
1436
|
+
|
|
1437
|
+
fn historySelectionCollapsedAt(selection: canvas.TextSelection, offset: usize) bool {
|
|
1438
|
+
return selection.anchor == offset and selection.focus == offset;
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1441
|
+
fn historySingleCodepoint(text: []const u8) bool {
|
|
1442
|
+
return text.len > 0 and canvas.snapTextOffset(text, text.len - 1) == 0;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
fn appendCanvasWidgetTextHistoryEdit(
|
|
1446
|
+
output: *[max_text_history_edits_per_shortcut]?canvas.TextInputEvent,
|
|
1447
|
+
count: *usize,
|
|
1448
|
+
edit: canvas.TextInputEvent,
|
|
1449
|
+
) void {
|
|
1450
|
+
if (count.* >= output.len) return;
|
|
1451
|
+
output[count.*] = edit;
|
|
1452
|
+
count.* += 1;
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
fn buildCanvasWidgetTextUndoEdits(
|
|
1456
|
+
entry: CanvasWidgetTextHistoryEntry,
|
|
1457
|
+
removed: []const u8,
|
|
1458
|
+
inserted: []const u8,
|
|
1459
|
+
current_selection: canvas.TextSelection,
|
|
1460
|
+
output: *[max_text_history_edits_per_shortcut]?canvas.TextInputEvent,
|
|
1461
|
+
count: *usize,
|
|
1462
|
+
) void {
|
|
1463
|
+
const prefix = entry.prefix_len;
|
|
1464
|
+
if (removed.len == 0 and
|
|
1465
|
+
historySingleCodepoint(inserted) and
|
|
1466
|
+
historySelectionCollapsedAt(current_selection, prefix + inserted.len) and
|
|
1467
|
+
canvasTextSelectionsEqual(entry.before_selection, canvas.TextSelection.collapsed(prefix)))
|
|
1468
|
+
{
|
|
1469
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .delete_backward);
|
|
1470
|
+
return;
|
|
1471
|
+
}
|
|
1472
|
+
if (inserted.len == 0 and
|
|
1473
|
+
historySelectionCollapsedAt(current_selection, prefix) and
|
|
1474
|
+
canvasTextSelectionsEqual(entry.before_selection, canvas.TextSelection.collapsed(prefix + removed.len)))
|
|
1475
|
+
{
|
|
1476
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = removed });
|
|
1477
|
+
return;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
const replacement_selection = canvas.TextSelection{ .anchor = prefix, .focus = prefix + inserted.len };
|
|
1481
|
+
if (!canvasTextSelectionsEqual(current_selection, replacement_selection)) {
|
|
1482
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = replacement_selection });
|
|
1483
|
+
}
|
|
1484
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = removed });
|
|
1485
|
+
const insertion_selection = canvas.TextSelection.collapsed(prefix + removed.len);
|
|
1486
|
+
if (!canvasTextSelectionsEqual(insertion_selection, entry.before_selection)) {
|
|
1487
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = entry.before_selection });
|
|
1488
|
+
}
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
fn buildCanvasWidgetTextRedoEdits(
|
|
1492
|
+
entry: CanvasWidgetTextHistoryEntry,
|
|
1493
|
+
removed: []const u8,
|
|
1494
|
+
inserted: []const u8,
|
|
1495
|
+
current_selection: canvas.TextSelection,
|
|
1496
|
+
output: *[max_text_history_edits_per_shortcut]?canvas.TextInputEvent,
|
|
1497
|
+
count: *usize,
|
|
1498
|
+
) void {
|
|
1499
|
+
const prefix = entry.prefix_len;
|
|
1500
|
+
if (removed.len == 0 and
|
|
1501
|
+
historySelectionCollapsedAt(current_selection, prefix) and
|
|
1502
|
+
canvasTextSelectionsEqual(entry.after_selection, canvas.TextSelection.collapsed(prefix + inserted.len)))
|
|
1503
|
+
{
|
|
1504
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = inserted });
|
|
1505
|
+
return;
|
|
1506
|
+
}
|
|
1507
|
+
if (historySingleCodepoint(removed) and
|
|
1508
|
+
inserted.len == 0 and
|
|
1509
|
+
historySelectionCollapsedAt(current_selection, prefix + removed.len) and
|
|
1510
|
+
canvasTextSelectionsEqual(entry.after_selection, canvas.TextSelection.collapsed(prefix)))
|
|
1511
|
+
{
|
|
1512
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .delete_backward);
|
|
1513
|
+
return;
|
|
1514
|
+
}
|
|
1515
|
+
if (historySingleCodepoint(removed) and
|
|
1516
|
+
inserted.len == 0 and
|
|
1517
|
+
historySelectionCollapsedAt(current_selection, prefix) and
|
|
1518
|
+
canvasTextSelectionsEqual(entry.after_selection, canvas.TextSelection.collapsed(prefix)))
|
|
1519
|
+
{
|
|
1520
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .delete_forward);
|
|
1521
|
+
return;
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1524
|
+
const replacement_selection = canvas.TextSelection{ .anchor = prefix, .focus = prefix + removed.len };
|
|
1525
|
+
if (!canvasTextSelectionsEqual(current_selection, replacement_selection)) {
|
|
1526
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = replacement_selection });
|
|
1527
|
+
}
|
|
1528
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .insert_text = inserted });
|
|
1529
|
+
const insertion_selection = canvas.TextSelection.collapsed(prefix + inserted.len);
|
|
1530
|
+
if (!canvasTextSelectionsEqual(insertion_selection, entry.after_selection)) {
|
|
1531
|
+
appendCanvasWidgetTextHistoryEdit(output, count, .{ .set_selection = entry.after_selection });
|
|
1532
|
+
}
|
|
1533
|
+
}
|