update navigation buttons state

This commit is contained in:
yggverse 2024-12-18 15:40:13 +02:00
parent a25c171e0e
commit dfa8c7a998
4 changed files with 41 additions and 6 deletions

View File

@ -64,7 +64,7 @@ impl Form {
match_case.is_active(),
);
input.update(!matches.is_empty());
navigation.update(matches);
navigation.renew(matches);
if !this.text().is_empty() {
result.update(navigation.position(), navigation.total());
result.label.set_visible(true);
@ -109,7 +109,7 @@ impl Form {
this.is_active(),
);
input.update(!matches.is_empty());
navigation.update(matches);
navigation.renew(matches);
if !input.entry.text().is_empty() {
result.update(navigation.position(), navigation.total());
result.label.set_visible(true);
@ -129,6 +129,7 @@ impl Form {
Some(subject) => {
match navigation.back(subject) {
Some((mut start, _)) => {
navigation.update();
result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start)
}
@ -146,6 +147,7 @@ impl Form {
move |_| match subject.borrow().as_ref() {
Some(subject) => match navigation.forward(subject) {
Some((mut start, _)) => {
navigation.update();
result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start)
}

View File

@ -53,11 +53,17 @@ impl Navigation {
// Actions
/// Update widget state, including child components
pub fn update(&self, matches: Vec<(TextIter, TextIter)>) {
self.back.update(!matches.is_empty());
self.forward.update(!matches.is_empty());
/// Update navigation model
pub fn renew(&self, matches: Vec<(TextIter, TextIter)>) {
self.model.replace(Model::new(matches));
self.update();
}
/// Update widget including child components
pub fn update(&self) {
let model = self.model.borrow();
self.back.update(model.is_back());
self.forward.update(model.is_next());
}
/// Navigate back in matches, apply tags to the buffer

View File

@ -7,6 +7,7 @@ pub struct Model<T> {
}
impl<T> Model<T> {
// Constructors
pub fn new(vector: Vec<T>) -> Self {
Self {
cursor: Cursor::new(vector.len()),
@ -14,6 +15,8 @@ impl<T> Model<T> {
}
}
// Actions
pub fn back(&mut self) -> Option<&T> {
self.cursor.back();
self.vector.get(self.cursor.as_index())
@ -24,6 +27,8 @@ impl<T> Model<T> {
self.vector.get(self.cursor.as_index())
}
// Getters
pub fn position(&self) -> Option<usize> {
self.cursor.as_position()
}
@ -31,4 +36,12 @@ impl<T> Model<T> {
pub fn total(&self) -> usize {
self.vector.len()
}
pub fn is_back(&self) -> bool {
self.cursor.is_back()
}
pub fn is_next(&self) -> bool {
self.cursor.is_next()
}
}

View File

@ -4,6 +4,8 @@ pub struct Cursor {
}
impl Cursor {
// Constructors
pub fn new(len: usize) -> Self {
Self {
current: 0,
@ -11,6 +13,8 @@ impl Cursor {
}
}
// Actions
pub fn back(&mut self) {
self.current = if self.current > 0 {
self.current - 1
@ -27,6 +31,8 @@ impl Cursor {
}
}
// Getters
pub fn as_index(&self) -> usize {
if self.current > 0 {
self.current - 1
@ -42,4 +48,12 @@ impl Cursor {
None
}
}
pub fn is_back(&self) -> bool {
self.current > 0
}
pub fn is_next(&self) -> bool {
self.current < self.last
}
}