pub struct FocusManager {
field_count: usize,
current_index: usize,
submit_button_focused: bool,
}
impl FocusManager {
pub fn new(field_count: usize) -> Self {
Self {
field_count,
current_index: 0,
submit_button_focused: false,
}
}
pub fn current_index(&self) -> usize {
self.current_index
}
pub fn is_submit_focused(&self) -> bool {
self.submit_button_focused
}
pub fn focus_next(&mut self) {
if self.submit_button_focused {
self.submit_button_focused = false;
self.current_index = 0;
} else if self.current_index + 1 >= self.field_count {
self.submit_button_focused = true;
} else {
self.current_index += 1;
}
}
pub fn focus_previous(&mut self) {
if self.submit_button_focused {
self.submit_button_focused = false;
self.current_index = self.field_count.saturating_sub(1);
} else if self.current_index > 0 {
self.current_index -= 1;
} else {
self.submit_button_focused = true;
}
}
pub fn set_field_count(&mut self, count: usize) {
self.field_count = count;
if self.current_index >= count {
self.current_index = count.saturating_sub(1);
}
}
pub fn focus_field(&mut self, index: usize) {
if index < self.field_count {
self.current_index = index;
self.submit_button_focused = false;
}
}
pub fn focus_submit(&mut self) {
self.submit_button_focused = true;
}
}