widgets(textinput): move key handling to ifelse block
Signed-off-by: Tim Culverhouse <tim@timculverhouse.com>
This commit is contained in:
parent
b57342726f
commit
8d496ffcdd
1 changed files with 7 additions and 11 deletions
|
@ -35,24 +35,20 @@ pub fn deinit(self: *TextInput) void {
|
||||||
pub fn update(self: *TextInput, event: Event) !void {
|
pub fn update(self: *TextInput, event: Event) !void {
|
||||||
switch (event) {
|
switch (event) {
|
||||||
.key_press => |key| {
|
.key_press => |key| {
|
||||||
if (key.text) |text| {
|
|
||||||
try self.buf.insertSlice(self.byteOffsetToCursor(), text);
|
|
||||||
self.cursor_idx += 1;
|
|
||||||
self.grapheme_count += 1;
|
|
||||||
}
|
|
||||||
if (key.matches(Key.backspace, .{})) {
|
if (key.matches(Key.backspace, .{})) {
|
||||||
if (self.cursor_idx == 0) return;
|
if (self.cursor_idx == 0) return;
|
||||||
self.deleteBeforeCursor();
|
self.deleteBeforeCursor();
|
||||||
}
|
} else if (key.matches(Key.delete, .{})) {
|
||||||
if (key.matches(Key.delete, .{})) {
|
|
||||||
if (self.cursor_idx == self.grapheme_count) return;
|
if (self.cursor_idx == self.grapheme_count) return;
|
||||||
self.deleteAtCursor();
|
self.deleteAtCursor();
|
||||||
}
|
} else if (key.matches(Key.left, .{})) {
|
||||||
if (key.matches(Key.left, .{})) {
|
|
||||||
if (self.cursor_idx > 0) self.cursor_idx -= 1;
|
if (self.cursor_idx > 0) self.cursor_idx -= 1;
|
||||||
}
|
} else if (key.matches(Key.right, .{})) {
|
||||||
if (key.matches(Key.right, .{})) {
|
|
||||||
if (self.cursor_idx < self.grapheme_count) self.cursor_idx += 1;
|
if (self.cursor_idx < self.grapheme_count) self.cursor_idx += 1;
|
||||||
|
} else if (key.text) |text| {
|
||||||
|
try self.buf.insertSlice(self.byteOffsetToCursor(), text);
|
||||||
|
self.cursor_idx += 1;
|
||||||
|
self.grapheme_count += 1;
|
||||||
}
|
}
|
||||||
// TODO: readline bindings
|
// TODO: readline bindings
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue