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::{ use gtk::{
prelude::{ prelude::{
BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, TextViewExt, WidgetExt, BoxExt, ButtonExt, CheckButtonExt, EditableExt, EntryExt, TextBufferExt, TextViewExt,
WidgetExt,
}, },
Align, Box, Orientation, TextIter, TextSearchFlags, Align, Box, Orientation, TextIter, TextSearchFlags,
}; };
@ -49,7 +50,7 @@ impl Form {
let subject = subject.clone(); let subject = subject.clone();
move |_| { move |_| {
let matches = find( let matches = find(
&subject, subject.borrow().as_ref().unwrap(), // @TODO handle
input.entry.text().as_str(), input.entry.text().as_str(),
match_case.is_active(), 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({ match_case.connect_toggled({
let input = input.clone(); let input = input.clone();
let navigation = navigation.clone(); let navigation = navigation.clone();
let subject = subject.clone(); let subject = subject.clone();
move |this| { 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()); input.update(!matches.is_empty());
navigation.update(matches); navigation.update(matches);
} }
@ -123,17 +152,10 @@ impl Form {
// Tools // Tools
fn find( fn find(subject: &Subject, request: &str, is_match_case: bool) -> Vec<(TextIter, TextIter)> {
subject: &Rc<RefCell<Option<Subject>>>,
request: &str,
is_match_case: bool,
) -> Vec<(TextIter, TextIter)> {
// Init matches holder // Init matches holder
let mut result = Vec::new(); let mut result = Vec::new();
// Borrow buffer
match subject.borrow().as_ref() {
Some(subject) => {
// Get iters // Get iters
let (buffer_start, buffer_end) = subject.text_view.buffer().bounds(); let (buffer_start, buffer_end) = subject.text_view.buffer().bounds();
@ -165,7 +187,4 @@ fn find(
result.push((match_start, match_end)); result.push((match_start, match_end));
} }
result result
}
None => todo!(), // unexpected
}
} }