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

View File

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