diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs index 02198d5c..80a9bf60 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget.rs @@ -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) diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find.rs index 9e53cea2..5798b26f 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find.rs @@ -16,8 +16,9 @@ use std::rc::Rc; pub struct Find { pub close: Button, - pub input: Rc, pub g_box: Box, + pub input: Rc, + pub navigation: Rc, } impl Find { @@ -83,8 +84,9 @@ impl Find { // Done Self { close, - input, g_box, + input, + navigation, } } } diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find/navigation.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find/navigation.rs index 70b45cf2..f72761d1 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find/navigation.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/widget/find/navigation.rs @@ -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>, matches: Rc>>, } @@ -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 +}