implement match totals update

This commit is contained in:
yggverse 2024-12-18 09:25:24 +02:00
parent b1bf9e76ad
commit 6203ef5b28
5 changed files with 45 additions and 10 deletions

View File

@ -64,7 +64,7 @@ impl Form {
match_case.is_active(), match_case.is_active(),
); );
if !this.text().is_empty() { if !this.text().is_empty() {
result.update(0, matches.len()); result.update(navigation.position(), navigation.total());
result.label.set_visible(true); result.label.set_visible(true);
separator.set_visible(true); separator.set_visible(true);
} else { } else {
@ -78,10 +78,12 @@ impl Form {
input.entry.connect_activate({ input.entry.connect_activate({
let navigation = navigation.clone(); let navigation = navigation.clone();
let result = result.clone();
let subject = subject.clone(); let subject = subject.clone();
move |_| match subject.borrow().as_ref() { move |_| match subject.borrow().as_ref() {
Some(subject) => { Some(subject) => {
if let Some((mut start, _)) = navigation.forward(subject) { if let Some((mut start, _)) = navigation.forward(subject) {
result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start) scroll_to_iter(&subject.text_view, &mut start)
} }
} }
@ -91,9 +93,10 @@ impl Form {
match_case.connect_toggled({ match_case.connect_toggled({
let input = input.clone(); let input = input.clone();
let navigation = navigation.clone();
let subject = subject.clone();
let input = input.clone(); let input = input.clone();
let navigation = navigation.clone();
let result = result.clone();
let subject = subject.clone();
move |this| { move |this| {
let matches = find( let matches = find(
subject.borrow().as_ref().unwrap(), // @TODO handle subject.borrow().as_ref().unwrap(), // @TODO handle
@ -101,7 +104,7 @@ impl Form {
this.is_active(), this.is_active(),
); );
if !input.entry.text().is_empty() { if !input.entry.text().is_empty() {
result.update(0, matches.len()); result.update(navigation.position(), navigation.total());
result.label.set_visible(true); result.label.set_visible(true);
separator.set_visible(true); separator.set_visible(true);
} else { } else {
@ -114,11 +117,15 @@ impl Form {
}); });
navigation.back.button.connect_clicked({ navigation.back.button.connect_clicked({
let subject = subject.clone();
let navigation = navigation.clone(); let navigation = navigation.clone();
let result = result.clone();
let subject = subject.clone();
move |_| match subject.borrow().as_ref() { move |_| match subject.borrow().as_ref() {
Some(subject) => match navigation.back(subject) { Some(subject) => match navigation.back(subject) {
Some((mut start, _)) => scroll_to_iter(&subject.text_view, &mut start), Some((mut start, _)) => {
result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start)
}
None => todo!(), None => todo!(),
}, },
None => todo!(), None => todo!(),
@ -126,11 +133,15 @@ impl Form {
}); });
navigation.forward.button.connect_clicked({ navigation.forward.button.connect_clicked({
let subject = subject.clone();
let navigation = navigation.clone(); let navigation = navigation.clone();
let result = result.clone();
let subject = subject.clone();
move |_| match subject.borrow().as_ref() { move |_| match subject.borrow().as_ref() {
Some(subject) => match navigation.forward(subject) { Some(subject) => match navigation.forward(subject) {
Some((mut start, _)) => scroll_to_iter(&subject.text_view, &mut start), Some((mut start, _)) => {
result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start)
}
None => todo!(), None => todo!(),
}, },
None => todo!(), None => todo!(),

View File

@ -94,4 +94,12 @@ impl Navigation {
None => None, None => None,
} }
} }
pub fn position(&self) -> usize {
self.model.borrow().position()
}
pub fn total(&self) -> usize {
self.model.borrow().total()
}
} }

View File

@ -23,4 +23,12 @@ impl<T> Model<T> {
self.cursor.next(); self.cursor.next();
self.vector.get(self.cursor.as_index()) self.vector.get(self.cursor.as_index())
} }
pub fn position(&self) -> usize {
self.cursor.as_position()
}
pub fn total(&self) -> usize {
self.vector.len()
}
} }

View File

@ -34,4 +34,8 @@ impl Cursor {
0 0
} }
} }
pub fn as_position(&self) -> usize {
self.current
}
} }

View File

@ -24,8 +24,12 @@ impl Result {
pub fn update(&self, current: usize, total: usize) { pub fn update(&self, current: usize, total: usize) {
if total > 0 { if total > 0 {
if current > 0 {
self.label self.label
.set_label(&format!("{current} if {total} matches")); .set_label(&format!("{current} of {total} matches"));
} else {
self.label.set_label(&format!("{total} matches"));
}
self.label.remove_css_class("error"); self.label.remove_css_class("error");
} else { } else {
self.label.set_label(&format!("Phrase not found")); self.label.set_label(&format!("Phrase not found"));