diff --git a/src/app/browser/window/tab/item/page/search/form/navigation.rs b/src/app/browser/window/tab/item/page/search/form/navigation.rs index 99c7611f..03cbce92 100644 --- a/src/app/browser/window/tab/item/page/search/form/navigation.rs +++ b/src/app/browser/window/tab/item/page/search/form/navigation.rs @@ -1,15 +1,17 @@ mod back; mod forward; +mod model; use back::Back; use forward::Forward; +use model::Model; use super::Subject; use gtk::{ prelude::{BoxExt, TextBufferExt, TextViewExt}, Box, Orientation, TextIter, }; -use std::{cell::RefCell, iter::Cycle, vec::IntoIter}; +use std::cell::RefCell; const MARGIN: i32 = 6; @@ -17,7 +19,7 @@ pub struct Navigation { pub back: Back, pub forward: Forward, pub g_box: Box, - iter: RefCell>>, + model: RefCell>, } impl Navigation { @@ -45,7 +47,7 @@ impl Navigation { back, forward, g_box, - iter: RefCell::new(Vec::new().into_iter().cycle()), + model: RefCell::new(Model::new(Vec::new())), // @TODO } } @@ -54,7 +56,7 @@ impl Navigation { pub fn update(&self, matches: Vec<(TextIter, TextIter)>) { self.back.update(!matches.is_empty()); self.forward.update(!matches.is_empty()); - let _ = self.iter.replace(matches.into_iter().cycle()); + let _ = self.model.replace(Model::new(matches)); } pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { @@ -66,13 +68,13 @@ impl Navigation { &buffer.end_iter(), ); - match self.iter.borrow_mut().next() { + match self.model.borrow_mut().back() { Some((start, end)) => { - buffer.apply_tag(&subject.tag.current, &start, &end); - Some((start, end)) + buffer.apply_tag(&subject.tag.current, start, end); + Some((*start, *end)) } None => None, - } // @TODO reverse + } } pub fn forward(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { @@ -84,10 +86,10 @@ impl Navigation { &buffer.end_iter(), ); - match self.iter.borrow_mut().next() { + match self.model.borrow_mut().next() { Some((start, end)) => { - buffer.apply_tag(&subject.tag.current, &start, &end); - Some((start, end)) + buffer.apply_tag(&subject.tag.current, start, end); + Some((*start, *end)) } None => None, } diff --git a/src/app/browser/window/tab/item/page/search/form/navigation/model.rs b/src/app/browser/window/tab/item/page/search/form/navigation/model.rs new file mode 100644 index 00000000..12faf135 --- /dev/null +++ b/src/app/browser/window/tab/item/page/search/form/navigation/model.rs @@ -0,0 +1,57 @@ +struct Cursor { + current: usize, + last: usize, +} + +impl Cursor { + pub fn back(&mut self) { + self.current = if self.current > 0 { + self.current - 1 + } else { + self.last + } + } + + pub fn next(&mut self) { + self.current = if self.current < self.last { + self.current + 1 + } else { + 0 + } + } + + pub fn as_index(&self) -> usize { + if self.current > 0 { + self.current - 1 + } else { + 0 + } + } +} + +pub struct Model { + cursor: Cursor, + vector: Vec, +} + +impl Model { + pub fn new(vector: Vec) -> Self { + Self { + cursor: Cursor { + current: 0, + last: vector.len(), + }, + vector, + } + } + + pub fn back(&mut self) -> Option<&T> { + self.cursor.back(); + self.vector.get(self.cursor.as_index()) + } + + pub fn next(&mut self) -> Option<&T> { + self.cursor.next(); + self.vector.get(self.cursor.as_index()) + } +}