implement search begin / continue on activate entry

This commit is contained in:
yggverse 2024-12-17 07:16:48 +02:00
parent e6583a602d
commit 093ee87b1f

View File

@ -8,7 +8,8 @@ use navigation::Navigation;
use gtk::{
prelude::{
BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, TextViewExt, WidgetExt,
BoxExt, ButtonExt, CheckButtonExt, EditableExt, EntryExt, TextBufferExt, TextViewExt,
WidgetExt,
},
Align, Box, Orientation, TextIter, TextSearchFlags,
};
@ -49,7 +50,7 @@ impl Form {
let subject = subject.clone();
move |_| {
let matches = find(
&subject,
subject.borrow().as_ref().unwrap(), // @TODO handle
input.entry.text().as_str(),
match_case.is_active(),
);
@ -58,12 +59,40 @@ impl Form {
}
});
input.entry.connect_activate({
let input = input.clone();
let match_case = match_case.clone();
let navigation = navigation.clone();
let subject = subject.clone();
move |_| {
// try continue
if navigation
.forward(subject.borrow().as_ref().unwrap()) // @TODO handle
.is_none()
{
// begin new search
let matches = find(
subject.borrow().as_ref().unwrap(), // @TODO handle
input.entry.text().as_str(),
match_case.is_active(),
);
input.update(!matches.is_empty());
navigation.update(matches);
navigation.forward(subject.borrow().as_ref().unwrap()); // @TODO handle
}
}
});
match_case.connect_toggled({
let input = input.clone();
let navigation = navigation.clone();
let subject = subject.clone();
move |this| {
let matches = find(&subject, input.entry.text().as_str(), this.is_active());
let matches = find(
subject.borrow().as_ref().unwrap(), // @TODO handle
input.entry.text().as_str(),
this.is_active(),
);
input.update(!matches.is_empty());
navigation.update(matches);
}
@ -123,17 +152,10 @@ impl Form {
// Tools
fn find(
subject: &Rc<RefCell<Option<Subject>>>,
request: &str,
is_match_case: bool,
) -> Vec<(TextIter, TextIter)> {
fn find(subject: &Subject, request: &str, is_match_case: bool) -> Vec<(TextIter, TextIter)> {
// Init matches holder
let mut result = Vec::new();
// Borrow buffer
match subject.borrow().as_ref() {
Some(subject) => {
// Get iters
let (buffer_start, buffer_end) = subject.text_view.buffer().bounds();
@ -166,6 +188,3 @@ fn find(
}
result
}
None => todo!(), // unexpected
}
}