move actions connect into the search component

This commit is contained in:
yggverse 2024-12-17 02:52:21 +02:00
parent 8b65df99f4
commit dd5d79bac4
6 changed files with 125 additions and 129 deletions

View file

@ -11,11 +11,13 @@ use gtk::{
prelude::{
BoxExt, ButtonExt, CheckButtonExt, EditableExt, TextBufferExt, TextViewExt, WidgetExt,
},
Align, Box, Orientation, TextIter, TextSearchFlags,
Align, Box, Button, Orientation, TextIter, TextSearchFlags,
};
use std::{cell::RefCell, rc::Rc};
pub struct Form {
pub close: Button,
pub input: Rc<Input>,
pub g_box: Box,
}
@ -23,7 +25,7 @@ impl Form {
// Constructors
/// Create new `Self`
pub fn new(buffer: &Rc<RefCell<Option<Subject>>>) -> Self {
pub fn new(subject: &Rc<RefCell<Option<Subject>>>) -> Self {
// Init components
let close = close::new();
let input = Rc::new(Input::new());
@ -53,10 +55,10 @@ impl Form {
let input = input.clone();
let match_case = match_case.clone();
let navigation = navigation.clone();
let buffer = buffer.clone();
let subject = subject.clone();
move |_| {
navigation.update(find(
&buffer,
&subject,
input.entry.text().as_str(),
match_case.is_active(),
));
@ -67,22 +69,63 @@ impl Form {
match_case.connect_toggled({
let input = input.clone();
let navigation = navigation.clone();
let buffer = buffer.clone();
let subject = subject.clone();
move |this| {
navigation.update(find(&buffer, input.entry.text().as_str(), this.is_active()));
navigation.update(find(
&subject,
input.entry.text().as_str(),
this.is_active(),
));
input.update(navigation.is_match());
}
});
// Connect events
navigation.back.button.connect_clicked({
let subject = subject.clone();
let navigation = navigation.clone();
move |_| match subject.borrow().as_ref() {
Some(subject) => match navigation.back(subject) {
Some((mut start, _)) => {
subject
.text_view
.scroll_to_iter(&mut start, 0.0, true, 0.0, 0.0);
}
None => todo!(),
},
None => todo!(),
}
});
navigation.forward.button.connect_clicked({
let subject = subject.clone();
let navigation = navigation.clone();
move |_| match subject.borrow().as_ref() {
Some(subject) => match navigation.forward(subject) {
Some((mut start, _)) => {
subject
.text_view
.scroll_to_iter(&mut start, 0.0, true, 0.0, 0.0);
}
None => todo!(),
},
None => todo!(),
}
});
// Done
Self { g_box }
Self {
close,
g_box,
input,
}
}
// Actions
pub fn show(&self) {
//self.buffer.get_mut().is_none()
self.g_box.set_visible(true)
self.g_box.set_visible(true);
self.input.entry.grab_focus();
}
pub fn hide(&self) {

View file

@ -4,9 +4,10 @@ mod forward;
use back::Back;
use forward::Forward;
use super::Subject;
use gtk::{
prelude::{BoxExt, TextBufferExt},
Box, Orientation, TextBuffer, TextIter, TextTag,
prelude::{BoxExt, TextBufferExt, TextViewExt},
Box, Orientation, TextIter,
};
use std::{
cell::{Cell, RefCell},
@ -68,55 +69,60 @@ impl Navigation {
self.back.update(self.is_match());
self.forward.update(self.is_match());
}
/*
pub fn back(&self) -> Option<(TextIter, TextIter)> {
self.text_buffer.remove_tag(
&self.current_tag,
&self.text_buffer.start_iter(),
&self.text_buffer.end_iter(),
);
let index = self.index.take();
match self.matches.borrow().get(back(index)) {
Some((start, end)) => {
self.text_buffer.apply_tag(&self.current_tag, start, end);
self.index.replace(if index == 0 {
len_to_index(self.matches.borrow().len())
} else {
index
});
Some((*start, *end))
}
None => {
self.index
.replace(len_to_index(self.matches.borrow().len())); // go last
None
}
}
}
pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {
let buffer = subject.text_view.buffer();
pub fn forward(&self) -> Option<(TextIter, TextIter)> {
self.text_buffer.remove_tag(
&self.current_tag,
&self.text_buffer.start_iter(),
&self.text_buffer.end_iter(),
);
buffer.remove_tag(
&subject.tag.current,
&buffer.start_iter(),
&buffer.end_iter(),
);
let index = self.index.take();
match self.matches.borrow().get(back(index)) {
Some((start, end)) => {
buffer.apply_tag(&subject.tag.current, start, end);
self.index.replace(if index == 0 {
len_to_index(self.matches.borrow().len())
} else {
index
});
Some((*start, *end))
}
None => {
self.index
.replace(len_to_index(self.matches.borrow().len())); // go last
None
}
}
}
pub fn forward(&self, subject: &Subject) -> Option<(TextIter, TextIter)> {
let buffer = subject.text_view.buffer();
buffer.remove_tag(
&subject.tag.current,
&buffer.start_iter(),
&buffer.end_iter(),
);
let index = self.index.take();
let next = forward(index);
match self.matches.borrow().get(next) {
Some((start, end)) => {
buffer.apply_tag(&subject.tag.current, start, end);
self.index.replace(next);
Some((*start, *end))
}
None => {
self.index.replace(0);
None
}
}
}
let index = self.index.take();
let next = forward(index);
match self.matches.borrow().get(next) {
Some((start, end)) => {
self.text_buffer.apply_tag(&self.current_tag, start, end);
self.index.replace(next);
Some((*start, *end))
}
None => {
self.index.replace(0);
None
}
}
}
*/
// Getters
pub fn is_match(&self) -> bool {