move matches index to navigation mod

This commit is contained in:
yggverse 2024-12-15 17:51:24 +02:00
parent f9306359c1
commit ab51b23f7c
2 changed files with 32 additions and 44 deletions

View File

@ -11,7 +11,7 @@ use gtk::{
prelude::{BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, WidgetExt}, prelude::{BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, WidgetExt},
Box, Button, Entry, Orientation, TextBuffer, TextIter, TextSearchFlags, TextTag, Box, Button, Entry, Orientation, TextBuffer, TextIter, TextSearchFlags, TextTag,
}; };
use std::{cell::Cell, rc::Rc}; use std::rc::Rc;
const MARGIN: i32 = 6; const MARGIN: i32 = 6;
@ -24,15 +24,12 @@ pub struct Find {
impl Find { impl Find {
// Construct // Construct
pub fn new(text_buffer: &TextBuffer) -> Self { pub fn new(text_buffer: &TextBuffer) -> Self {
// Init shared matches holder
let matches = Rc::new(Cell::new(Vec::<(TextIter, TextIter)>::new()));
// Init components // Init components
let close = close::new(); let close = close::new();
let entry = entry::new(); let entry = entry::new();
let match_case = match_case::new(); let match_case = match_case::new();
let navigation = Navigation::new(); let navigation = Rc::new(Navigation::new());
let tag = Tag::new(text_buffer.tag_table()); let tag = Rc::new(Tag::new(text_buffer.tag_table()));
// Init main container // Init main container
let g_box = Box::builder() let g_box = Box::builder()
@ -52,54 +49,35 @@ impl Find {
}); });
entry.connect_changed({ entry.connect_changed({
let back = navigation.back.clone();
let entry = entry.clone(); let entry = entry.clone();
let forward = navigation.forward.clone();
let found_tag = tag.found.clone();
let match_case = match_case.clone(); let match_case = match_case.clone();
let matches = matches.clone(); let navigation = navigation.clone();
let tag = tag.clone();
let text_buffer = text_buffer.clone(); let text_buffer = text_buffer.clone();
move |_| { move |_| {
// do search navigation.update(find(
let result = find(
&text_buffer, &text_buffer,
&found_tag, &tag.found,
entry.text().as_str(), entry.text().as_str(),
match_case.is_active(), match_case.is_active(),
); ));
update(&entry, &navigation);
// update components
update(&entry, &back, &forward, result.is_empty());
// update matches index
matches.replace(result);
} }
}); });
match_case.connect_toggled({ match_case.connect_toggled({
let entry = entry.clone(); let entry = entry.clone();
let found_tag = tag.found.clone(); let navigation = navigation.clone();
let matches = matches.clone(); let tag = tag.clone();
let text_buffer = text_buffer.clone(); let text_buffer = text_buffer.clone();
move |this| { move |this| {
// do search navigation.update(find(
let result = find(
&text_buffer, &text_buffer,
&found_tag, &tag.found,
entry.text().as_str(), entry.text().as_str(),
this.is_active(), this.is_active(),
); ));
update(&entry, &navigation);
// update components
update(
&entry,
&navigation.back,
&navigation.forward,
result.is_empty(),
);
// update matches index
matches.replace(result);
} }
}); });
@ -147,14 +125,14 @@ fn find(
result result
} }
fn update(entry: &Entry, back: &Button, forward: &Button, is_empty: bool) { fn update(entry: &Entry, navigation: &Rc<Navigation>) {
if is_empty { if navigation.matches.take().is_empty() {
entry.add_css_class("error"); entry.add_css_class("error");
back.set_sensitive(false); navigation.back.set_sensitive(false);
forward.set_sensitive(false); navigation.forward.set_sensitive(false);
} else { } else {
entry.remove_css_class("error"); entry.remove_css_class("error");
back.set_sensitive(false); navigation.back.set_sensitive(false);
forward.set_sensitive(true); navigation.forward.set_sensitive(true);
} }
} }

View File

@ -1,13 +1,16 @@
mod back; mod back;
mod forward; mod forward;
use std::{cell::Cell, rc::Rc};
use super::MARGIN; use super::MARGIN;
use gtk::{prelude::BoxExt, Box, Button, Orientation}; use gtk::{prelude::BoxExt, Box, Button, Orientation, TextIter};
pub struct Navigation { pub struct Navigation {
pub back: Button, pub back: Button,
pub forward: Button, pub forward: Button,
pub g_box: Box, pub g_box: Box,
pub matches: Rc<Cell<Vec<(TextIter, TextIter)>>>,
} }
impl Navigation { impl Navigation {
@ -15,6 +18,8 @@ impl Navigation {
/// Create new `Self` /// Create new `Self`
pub fn new() -> Self { pub fn new() -> Self {
let matches = Rc::new(Cell::new(Vec::new()));
// Init components // Init components
let back = back::new(); let back = back::new();
let forward = forward::new(); let forward = forward::new();
@ -35,6 +40,11 @@ impl Navigation {
back, back,
forward, forward,
g_box, g_box,
matches,
} }
} }
pub fn update(&self, matches: Vec<(TextIter, TextIter)>) {
self.matches.replace(matches);
}
} }