Skip to content

Commit

Permalink
widgets(textinput): properly reset state
Browse files Browse the repository at this point in the history
Fix resetting of state when calling clearAndFree,
clearRetainingCapacity, or toOwnedSlice.

Signed-off-by: Tim Culverhouse <[email protected]>
  • Loading branch information
rockorager committed Mar 26, 2024
1 parent e377909 commit 6a31b71
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/widgets/TextInput.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6a31b71

Please sign in to comment.