update tag on navigate

This commit is contained in:
yggverse 2024-12-16 14:56:11 +02:00
parent 5240aa1879
commit d41667f229
3 changed files with 26 additions and 7 deletions

View File

@ -28,8 +28,8 @@ impl Search {
let close = close::new(); let close = close::new();
let input = Rc::new(Input::new()); let input = Rc::new(Input::new());
let match_case = match_case::new(); let match_case = match_case::new();
let navigation = Rc::new(Navigation::new());
let tag = Rc::new(Tag::new(text_buffer.tag_table())); let tag = Rc::new(Tag::new(text_buffer.tag_table()));
let navigation = Rc::new(Navigation::new(text_buffer.clone(), tag.current.clone()));
// Init main container // Init main container
let g_box = Box::builder() let g_box = Box::builder()

View File

@ -4,7 +4,10 @@ mod forward;
use back::Back; use back::Back;
use forward::Forward; use forward::Forward;
use gtk::{prelude::BoxExt, Box, Orientation, TextIter}; use gtk::{
prelude::{BoxExt, TextBufferExt},
Box, Orientation, TextBuffer, TextIter, TextTag,
};
use std::{ use std::{
cell::{Cell, RefCell}, cell::{Cell, RefCell},
rc::Rc, rc::Rc,
@ -18,13 +21,15 @@ pub struct Navigation {
pub g_box: Box, pub g_box: Box,
index: Rc<Cell<usize>>, index: Rc<Cell<usize>>,
matches: Rc<RefCell<Vec<(TextIter, TextIter)>>>, matches: Rc<RefCell<Vec<(TextIter, TextIter)>>>,
text_buffer: TextBuffer,
current_tag: TextTag,
} }
impl Navigation { impl Navigation {
// Constructors // Constructors
/// Create new `Self` /// Create new `Self`
pub fn new() -> Self { pub fn new(text_buffer: TextBuffer, current_tag: TextTag) -> Self {
// Init shared matches holder // Init shared matches holder
let index = Rc::new(Cell::new(0)); let index = Rc::new(Cell::new(0));
let matches = Rc::new(RefCell::new(Vec::new())); let matches = Rc::new(RefCell::new(Vec::new()));
@ -51,6 +56,8 @@ impl Navigation {
g_box, g_box,
index, index,
matches, matches,
text_buffer,
current_tag,
} }
} }
@ -67,9 +74,16 @@ impl Navigation {
} }
pub fn back(&self) -> Option<(TextIter, TextIter)> { 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(); let index = self.index.take();
match self.matches.borrow().get(back(index)) { match self.matches.borrow().get(back(index)) {
Some((start, end)) => { Some((start, end)) => {
self.text_buffer.apply_tag(&self.current_tag, &start, &end);
self.index.replace(if index == 0 { self.index.replace(if index == 0 {
len_to_index(self.matches.borrow().len()) len_to_index(self.matches.borrow().len())
} else { } else {
@ -86,10 +100,17 @@ impl Navigation {
} }
pub fn forward(&self) -> Option<(TextIter, TextIter)> { 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(),
);
let index = self.index.take(); let index = self.index.take();
let next = forward(index); let next = forward(index);
match self.matches.borrow().get(next) { match self.matches.borrow().get(next) {
Some((start, end)) => { Some((start, end)) => {
self.text_buffer.apply_tag(&self.current_tag, &start, &end);
self.index.replace(next); self.index.replace(next);
Some((*start, *end)) Some((*start, *end))
} }

View File

@ -4,7 +4,7 @@ mod found;
use gtk::{TextTag, TextTagTable}; use gtk::{TextTag, TextTagTable};
pub struct Tag { pub struct Tag {
// pub current: TextTag, pub current: TextTag,
pub found: TextTag, pub found: TextTag,
} }
@ -20,8 +20,6 @@ impl Tag {
tag_table.add(&current); tag_table.add(&current);
tag_table.add(&found); tag_table.add(&found);
Self { Self { current, found }
/*current,*/ found,
}
} }
} }