use super::InputFieldState;
impl InputFieldState {
pub(super) fn move_left(&mut self) {
if self.cursor > 0 {
self.cursor = self.value[..self.cursor]
.char_indices()
.last()
.map(|(i, _)| i)
.unwrap_or(0);
}
}
pub(super) fn move_right(&mut self) {
if self.cursor < self.value.len() {
self.cursor = self.value[self.cursor..]
.char_indices()
.nth(1)
.map(|(i, _)| self.cursor + i)
.unwrap_or(self.value.len());
}
}
pub(super) fn move_word_left(&mut self) {
if self.cursor == 0 {
return;
}
let before = &self.value[..self.cursor];
let chars: Vec<(usize, char)> = before.char_indices().collect();
let mut idx = chars.len() - 1;
while idx > 0 && chars[idx].1.is_whitespace() {
idx -= 1;
}
while idx > 0 && !chars[idx - 1].1.is_whitespace() {
idx -= 1;
}
self.cursor = chars.get(idx).map(|(i, _)| *i).unwrap_or(0);
}
pub(super) fn move_word_right(&mut self) {
if self.cursor >= self.value.len() {
return;
}
let after = &self.value[self.cursor..];
let chars: Vec<(usize, char)> = after.char_indices().collect();
let mut idx = 0;
while idx < chars.len() && !chars[idx].1.is_whitespace() {
idx += 1;
}
while idx < chars.len() && chars[idx].1.is_whitespace() {
idx += 1;
}
self.cursor = chars
.get(idx)
.map(|(i, _)| self.cursor + *i)
.unwrap_or(self.value.len());
}
pub(super) fn insert(&mut self, c: char) {
self.value.insert(self.cursor, c);
self.cursor += c.len_utf8();
}
pub(super) fn backspace(&mut self) -> bool {
if self.cursor > 0 {
let prev_cursor = self.cursor;
self.move_left();
self.value.drain(self.cursor..prev_cursor);
true
} else {
false
}
}
pub(super) fn delete(&mut self) -> bool {
if self.cursor < self.value.len() {
let next = self.value[self.cursor..]
.char_indices()
.nth(1)
.map(|(i, _)| self.cursor + i)
.unwrap_or(self.value.len());
self.value.drain(self.cursor..next);
true
} else {
false
}
}
pub(super) fn delete_word_back(&mut self) -> bool {
if self.cursor == 0 {
return false;
}
let end = self.cursor;
self.move_word_left();
self.value.drain(self.cursor..end);
true
}
pub(super) fn delete_word_forward(&mut self) -> bool {
if self.cursor >= self.value.len() {
return false;
}
let start = self.cursor;
self.move_word_right();
let end = self.cursor;
self.cursor = start;
self.value.drain(start..end);
true
}
}