apply current tag if position active only, expect match results > 0 on request navigation act

This commit is contained in:
yggverse 2024-12-18 15:01:41 +02:00
parent 2a5fb0ba14
commit b9bdaaa495
2 changed files with 48 additions and 30 deletions

View File

@ -80,9 +80,10 @@ impl Form {
let navigation = navigation.clone(); let navigation = navigation.clone();
let result = result.clone(); let result = result.clone();
let subject = subject.clone(); let subject = subject.clone();
move |_| match subject.borrow().as_ref() { move |this| match subject.borrow().as_ref() {
Some(subject) => { Some(subject) => {
if let Some((mut start, _)) = navigation.forward(subject) { if !this.text().is_empty() {
let (mut start, _) = navigation.forward(subject);
result.update(navigation.position(), navigation.total()); result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start) scroll_to_iter(&subject.text_view, &mut start)
} }
@ -121,13 +122,11 @@ impl Form {
let result = result.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) => match navigation.back(subject) { Some(subject) => {
Some((mut start, _)) => { let (mut start, _) = navigation.back(subject);
result.update(navigation.position(), navigation.total()); result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start) scroll_to_iter(&subject.text_view, &mut start)
} }
None => todo!(),
},
None => todo!(), None => todo!(),
} }
}); });
@ -137,13 +136,11 @@ impl Form {
let result = result.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) => match navigation.forward(subject) { Some(subject) => {
Some((mut start, _)) => { let (mut start, _) = navigation.forward(subject);
result.update(navigation.position(), navigation.total()); result.update(navigation.position(), navigation.total());
scroll_to_iter(&subject.text_view, &mut start) scroll_to_iter(&subject.text_view, &mut start)
} }
None => todo!(),
},
None => todo!(), None => todo!(),
} }
}); });

View File

@ -53,13 +53,18 @@ impl Navigation {
// Actions // Actions
/// Update widget state, including child components
pub fn update(&self, matches: Vec<(TextIter, TextIter)>) { pub fn update(&self, matches: Vec<(TextIter, TextIter)>) {
self.back.update(!matches.is_empty()); self.back.update(!matches.is_empty());
self.forward.update(!matches.is_empty()); self.forward.update(!matches.is_empty());
self.model.replace(Model::new(matches)); self.model.replace(Model::new(matches));
} }
pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { /// Navigate back in matches, apply tags to buffer
/// * return `start`/`end` iters to scroll up the widget
/// * user should not activate this function on empty results
/// expected all actions / buttons deactivated in this case
pub fn back(&self, subject: &Subject) -> (TextIter, TextIter) {
let buffer = subject.text_view.buffer(); let buffer = subject.text_view.buffer();
buffer.remove_tag( buffer.remove_tag(
@ -68,16 +73,25 @@ impl Navigation {
&buffer.end_iter(), &buffer.end_iter(),
); );
match self.model.borrow_mut().back() { let mut model = self.model.borrow_mut();
Some((start, end)) => {
buffer.apply_tag(&subject.tag.current, start, end); let (start, end) = match model.back() {
Some((*start, *end)) Some((start, end)) => (*start, *end),
} None => todo!(), // unexpected
None => None, };
if model.position().is_some() {
buffer.apply_tag(&subject.tag.current, &start, &end);
} }
(start, end)
} }
pub fn forward(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { /// Navigate forward in matches, apply tags to buffer
/// * return `start`/`end` iters to scroll down the widget
/// * user should not activate this function on empty results
/// expected all actions / buttons deactivated in this case
pub fn forward(&self, subject: &Subject) -> (TextIter, TextIter) {
let buffer = subject.text_view.buffer(); let buffer = subject.text_view.buffer();
buffer.remove_tag( buffer.remove_tag(
@ -86,15 +100,22 @@ impl Navigation {
&buffer.end_iter(), &buffer.end_iter(),
); );
match self.model.borrow_mut().next() { let mut model = self.model.borrow_mut();
Some((start, end)) => {
buffer.apply_tag(&subject.tag.current, start, end); let (start, end) = match model.next() {
Some((*start, *end)) Some((start, end)) => (*start, *end),
} None => todo!(), // unexpected
None => None, };
if model.position().is_some() {
buffer.apply_tag(&subject.tag.current, &start, &end);
} }
(start, end)
} }
// Getters
pub fn position(&self) -> Option<usize> { pub fn position(&self) -> Option<usize> {
self.model.borrow().position() self.model.borrow().position()
} }