Skip to content

Commit

Permalink
update navigation buttons state
Browse files Browse the repository at this point in the history
  • Loading branch information
yggverse committed Dec 18, 2024
1 parent a25c171 commit dfa8c7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/app/browser/window/tab/item/page/search/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
14 changes: 10 additions & 4 deletions src/app/browser/window/tab/item/page/search/form/navigation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ pub struct Model<T> {
}

impl<T> Model<T> {
// Constructors
pub fn new(vector: Vec<T>) -> Self {
Self {
cursor: Cursor::new(vector.len()),
vector,
}
}

// Actions

pub fn back(&mut self) -> Option<&T> {
self.cursor.back();
self.vector.get(self.cursor.as_index())
Expand All @@ -24,11 +27,21 @@ impl<T> Model<T> {
self.vector.get(self.cursor.as_index())
}

// Getters

pub fn position(&self) -> Option<usize> {
self.cursor.as_position()
}

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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ pub struct Cursor {
}

impl Cursor {
// Constructors

pub fn new(len: usize) -> Self {
Self {
current: 0,
last: len,
}
}

// Actions

pub fn back(&mut self) {
self.current = if self.current > 0 {
self.current - 1
Expand All @@ -27,6 +31,8 @@ impl Cursor {
}
}

// Getters

pub fn as_index(&self) -> usize {
if self.current > 0 {
self.current - 1
Expand All @@ -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
}
}

0 comments on commit dfa8c7a

Please sign in to comment.