From 6a31b71e33589ca7e3a0f409b030632107716d4b Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Tue, 26 Mar 2024 09:01:07 -0500 Subject: [PATCH] widgets(textinput): properly reset state Fix resetting of state when calling clearAndFree, clearRetainingCapacity, or toOwnedSlice. Signed-off-by: Tim Culverhouse --- src/widgets/TextInput.zig | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/widgets/TextInput.zig b/src/widgets/TextInput.zig index c3d5d1b..b323055 100644 --- a/src/widgets/TextInput.zig +++ b/src/widgets/TextInput.zig @@ -137,20 +137,25 @@ pub fn draw(self: *TextInput, win: Window) void { pub fn clearAndFree(self: *TextInput) void { self.buf.clearAndFree(); - self.cursor_idx = 0; - self.grapheme_count = 0; + self.reset(); } pub fn clearRetainingCapacity(self: *TextInput) void { self.buf.clearRetainingCapacity(); - self.cursor_idx = 0; - self.grapheme_count = 0; + self.reset(); } pub fn toOwnedSlice(self: *TextInput) ![]const u8 { + defer self.reset(); + return self.buf.toOwnedSlice(); +} + +fn reset(self: *TextInput) void { self.cursor_idx = 0; self.grapheme_count = 0; - return self.buf.toOwnedSlice(); + self.draw_offset = 0; + self.prev_cursor_col = 0; + self.prev_cursor_idx = 0; } // returns the number of bytes before the cursor