From dfa8c7a998246f1dc068d3b099190413fc9f7a14 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 15:40:13 +0200 Subject: [PATCH] update navigation buttons state --- .../browser/window/tab/item/page/search/form.rs | 6 ++++-- .../window/tab/item/page/search/form/navigation.rs | 14 ++++++++++---- .../tab/item/page/search/form/navigation/model.rs | 13 +++++++++++++ .../page/search/form/navigation/model/cursor.rs | 14 ++++++++++++++ 4 files changed, 41 insertions(+), 6 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 516cd757..cf6ab680 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(), ); 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) } 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 3721b247..c52d7c06 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 @@ -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 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 744afa54..02fcde6a 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 @@ -7,6 +7,7 @@ pub struct Model { } impl Model { + // Constructors pub fn new(vector: Vec) -> Self { Self { cursor: Cursor::new(vector.len()), @@ -14,6 +15,8 @@ impl Model { } } + // Actions + pub fn back(&mut self) -> Option<&T> { self.cursor.back(); self.vector.get(self.cursor.as_index()) @@ -24,6 +27,8 @@ impl Model { self.vector.get(self.cursor.as_index()) } + // Getters + pub fn position(&self) -> Option { self.cursor.as_position() } @@ -31,4 +36,12 @@ impl Model { 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() + } } 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 877500ce..12129659 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 @@ -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 + } }