From 6203ef5b28c9b28669da6fd469549703b75c1cf6 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 09:25:24 +0200 Subject: [PATCH] implement match totals update --- .../window/tab/item/page/search/form.rs | 27 +++++++++++++------ .../tab/item/page/search/form/navigation.rs | 8 ++++++ .../item/page/search/form/navigation/model.rs | 8 ++++++ .../search/form/navigation/model/cursor.rs | 4 +++ .../tab/item/page/search/form/result.rs | 8 ++++-- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/src/app/browser/window/tab/item/page/search/form.rs b/src/app/browser/window/tab/item/page/search/form.rs index 573f2775..4ac23bcc 100644 --- a/src/app/browser/window/tab/item/page/search/form.rs +++ b/src/app/browser/window/tab/item/page/search/form.rs @@ -64,7 +64,7 @@ impl Form { match_case.is_active(), ); if !this.text().is_empty() { - result.update(0, matches.len()); + result.update(navigation.position(), navigation.total()); result.label.set_visible(true); separator.set_visible(true); } else { @@ -78,10 +78,12 @@ impl Form { input.entry.connect_activate({ let navigation = navigation.clone(); + let result = result.clone(); let subject = subject.clone(); move |_| match subject.borrow().as_ref() { Some(subject) => { if let Some((mut start, _)) = navigation.forward(subject) { + result.update(navigation.position(), navigation.total()); scroll_to_iter(&subject.text_view, &mut start) } } @@ -91,9 +93,10 @@ impl Form { match_case.connect_toggled({ let input = input.clone(); - let navigation = navigation.clone(); - let subject = subject.clone(); let input = input.clone(); + let navigation = navigation.clone(); + let result = result.clone(); + let subject = subject.clone(); move |this| { let matches = find( subject.borrow().as_ref().unwrap(), // @TODO handle @@ -101,7 +104,7 @@ impl Form { this.is_active(), ); if !input.entry.text().is_empty() { - result.update(0, matches.len()); + result.update(navigation.position(), navigation.total()); result.label.set_visible(true); separator.set_visible(true); } else { @@ -114,11 +117,15 @@ impl Form { }); navigation.back.button.connect_clicked({ - let subject = subject.clone(); let navigation = navigation.clone(); + let result = result.clone(); + let subject = subject.clone(); move |_| match subject.borrow().as_ref() { 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!(), @@ -126,11 +133,15 @@ impl Form { }); navigation.forward.button.connect_clicked({ - let subject = subject.clone(); let navigation = navigation.clone(); + let result = result.clone(); + let subject = subject.clone(); move |_| match subject.borrow().as_ref() { 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!(), diff --git a/src/app/browser/window/tab/item/page/search/form/navigation.rs b/src/app/browser/window/tab/item/page/search/form/navigation.rs index 795a6cb7..c1d993e5 100644 --- a/src/app/browser/window/tab/item/page/search/form/navigation.rs +++ b/src/app/browser/window/tab/item/page/search/form/navigation.rs @@ -94,4 +94,12 @@ impl Navigation { None => None, } } + + pub fn position(&self) -> usize { + self.model.borrow().position() + } + + pub fn total(&self) -> usize { + self.model.borrow().total() + } } diff --git a/src/app/browser/window/tab/item/page/search/form/navigation/model.rs b/src/app/browser/window/tab/item/page/search/form/navigation/model.rs index 5b93bf34..180556de 100644 --- a/src/app/browser/window/tab/item/page/search/form/navigation/model.rs +++ b/src/app/browser/window/tab/item/page/search/form/navigation/model.rs @@ -23,4 +23,12 @@ impl Model { self.cursor.next(); self.vector.get(self.cursor.as_index()) } + + pub fn position(&self) -> usize { + self.cursor.as_position() + } + + pub fn total(&self) -> usize { + self.vector.len() + } } diff --git a/src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.rs b/src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.rs index 45a52b26..3330812c 100644 --- a/src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.rs +++ b/src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.rs @@ -34,4 +34,8 @@ impl Cursor { 0 } } + + pub fn as_position(&self) -> usize { + self.current + } } diff --git a/src/app/browser/window/tab/item/page/search/form/result.rs b/src/app/browser/window/tab/item/page/search/form/result.rs index e9939152..e6c305ad 100644 --- a/src/app/browser/window/tab/item/page/search/form/result.rs +++ b/src/app/browser/window/tab/item/page/search/form/result.rs @@ -24,8 +24,12 @@ impl Result { pub fn update(&self, current: usize, total: usize) { if total > 0 { - self.label - .set_label(&format!("{current} if {total} matches")); + if current > 0 { + self.label + .set_label(&format!("{current} of {total} matches")); + } else { + self.label.set_label(&format!("{total} matches")); + } self.label.remove_css_class("error"); } else { self.label.set_label(&format!("Phrase not found"));