Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/components/text_area.zig
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub const TextArea = struct {

switch (key.key) {
.char => |c| self.insertChar(c),
.paste => |text| self.insertText(text),
.enter => self.insertNewline(),
.backspace => self.deleteBackward(),
.delete => self.deleteForward(),
Expand Down Expand Up @@ -224,6 +225,42 @@ pub const TextArea = struct {
self.cursor_col = pos + len;
}

fn insertText(self: *TextArea, text: []const u8) void {
var i: usize = 0;
while (i < text.len) {
if (text[i] == '\r') {
self.insertNewline();
if (i + 1 < text.len and text[i + 1] == '\n') i += 1;
i += 1;
continue;
}
if (text[i] == '\n') {
self.insertNewline();
i += 1;
continue;
}

const len = std.unicode.utf8ByteSequenceLength(text[i]) catch {
self.insertChar(text[i]);
i += 1;
continue;
};
if (i + len > text.len) {
self.insertChar(text[i]);
i += 1;
continue;
}

const codepoint = std.unicode.utf8Decode(text[i .. i + len]) catch {
self.insertChar(text[i]);
i += 1;
continue;
};
self.insertChar(codepoint);
i += len;
}
}

fn insertNewline(self: *TextArea) void {
if (self.max_lines) |max| {
if (self.lines.items.len >= max) return;
Expand Down
30 changes: 30 additions & 0 deletions src/components/text_input.zig
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub const TextInput = struct {

switch (key.key) {
.char => |c| self.insertChar(c),
.paste => |text| self.insertText(text),
.backspace => self.deleteBackward(),
.delete => self.deleteForward(),
.left => self.moveCursorLeft(),
Expand Down Expand Up @@ -248,6 +249,35 @@ pub const TextInput = struct {
self.cursor += len;
}

fn insertText(self: *TextInput, text: []const u8) void {
var i: usize = 0;
while (i < text.len) {
if (text[i] == '\r' or text[i] == '\n') {
i += 1;
continue;
}

const len = std.unicode.utf8ByteSequenceLength(text[i]) catch {
self.insertChar(text[i]);
i += 1;
continue;
};
if (i + len > text.len) {
self.insertChar(text[i]);
i += 1;
continue;
}

const codepoint = std.unicode.utf8Decode(text[i .. i + len]) catch {
self.insertChar(text[i]);
i += 1;
continue;
};
self.insertChar(codepoint);
i += len;
}
}

fn deleteBackward(self: *TextInput) void {
if (self.cursor == 0) return;

Expand Down
20 changes: 20 additions & 0 deletions tests/input_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,23 @@ test "parseAll multiple keys" {
try testing.expect(events[0] == .key);
try testing.expectEqual(@as(u21, 'a'), events[0].key.key.char);
}

test "TextInput accepts multi-character paste commits (IME-like)" {
var input = zz.TextInput.init(testing.allocator);
defer input.deinit();

input.handleKey(.{ .key = .{ .paste = "中文输入" } });

try testing.expectEqualStrings("中文输入", input.getValue());
}

test "TextArea accepts multi-character paste commits (IME-like)" {
var area = zz.TextArea.init(testing.allocator);
defer area.deinit();

area.handleKey(.{ .key = .{ .paste = "中文输入" } });

const value = try area.getValue(testing.allocator);
defer testing.allocator.free(value);
try testing.expectEqualStrings("中文输入", value);
}