draft search navigation buttons

This commit is contained in:
yggverse 2024-12-16 11:43:47 +02:00
parent f62ea22387
commit 6150386856
3 changed files with 77 additions and 6 deletions

View File

@ -56,6 +56,26 @@ impl Widget {
}
});
find.navigation.back.button.connect_clicked({
let text_view = text_view.clone();
let navigation = find.navigation.clone();
move |_| {
if let Some((mut start, _)) = navigation.back() {
text_view.scroll_to_iter(&mut start, 0.0, false, 0.0, 0.0);
}
}
});
find.navigation.forward.button.connect_clicked({
let text_view = text_view.clone();
let navigation = find.navigation.clone();
move |_| {
if let Some((mut start, _)) = navigation.forward() {
text_view.scroll_to_iter(&mut start, 0.0, false, 0.0, 0.0);
}
}
});
find.close.connect_clicked({
let text_view = text_view.clone();
move |_| text_view.set_gutter(TextWindowType::Bottom, gtk::Widget::NONE)

View File

@ -16,8 +16,9 @@ use std::rc::Rc;
pub struct Find {
pub close: Button,
pub input: Rc<Input>,
pub g_box: Box,
pub input: Rc<Input>,
pub navigation: Rc<Navigation>,
}
impl Find {
@ -83,8 +84,9 @@ impl Find {
// Done
Self {
close,
input,
g_box,
input,
navigation,
}
}
}

View File

@ -5,7 +5,10 @@ use back::Back;
use forward::Forward;
use gtk::{prelude::BoxExt, Box, Orientation, TextIter};
use std::{cell::RefCell, rc::Rc};
use std::{
cell::{Cell, RefCell},
rc::Rc,
};
const MARGIN: i32 = 6;
@ -13,6 +16,7 @@ pub struct Navigation {
pub back: Back,
pub forward: Forward,
pub g_box: Box,
index: Rc<Cell<usize>>,
matches: Rc<RefCell<Vec<(TextIter, TextIter)>>>,
}
@ -22,6 +26,7 @@ impl Navigation {
/// Create new `Self`
pub fn new() -> Self {
// Init shared matches holder
let index = Rc::new(Cell::new(0));
let matches = Rc::new(RefCell::new(Vec::new()));
// Init components
@ -44,6 +49,7 @@ impl Navigation {
back,
forward,
g_box,
index,
matches,
}
}
@ -53,15 +59,46 @@ impl Navigation {
pub fn update(&self, matches: Vec<(TextIter, TextIter)>) {
// Update self
self.matches.replace(matches);
self.index.replace(0); // reset
// Update child components
self.back.update(false);
self.back.update(self.is_match());
self.forward.update(self.is_match());
}
// pub fn back(&self) {}
pub fn back(&self) -> Option<(TextIter, TextIter)> {
let index = self.index.take();
match self.matches.borrow().get(back(index)) {
Some((start, end)) => {
self.index.replace(if index == 0 {
len_to_index(self.matches.borrow().len())
} else {
index
});
Some((start.clone(), end.clone()))
}
None => {
self.index
.replace(len_to_index(self.matches.borrow().len())); // go last
None
}
}
}
// pub fn forward(&self) {}
pub fn forward(&self) -> Option<(TextIter, TextIter)> {
let index = self.index.take();
let next = forward(index);
match self.matches.borrow().get(next) {
Some((start, end)) => {
self.index.replace(next);
Some((start.clone(), end.clone()))
}
None => {
self.index.replace(0);
None
}
}
}
// Getters
@ -69,3 +106,15 @@ impl Navigation {
!self.matches.borrow().is_empty()
}
}
fn back(index: usize) -> usize {
index - 1
}
fn forward(index: usize) -> usize {
index + 1
}
fn len_to_index(len: usize) -> usize {
len - 1
}