From 8cf8d2b298b93315a05b8fd13f9a8a72977e4936 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 17 Dec 2024 10:27:35 +0200 Subject: [PATCH] use cycle iter --- .../tab/item/page/search/form/navigation.rs | 18 ++++---- .../item/page/search/form/navigation/iter.rs | 45 ------------------- 2 files changed, 8 insertions(+), 55 deletions(-) delete mode 100644 src/app/browser/window/tab/item/page/search/form/navigation/iter.rs 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 779c2a0d..9fd338dc 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,17 +1,15 @@ mod back; mod forward; -mod iter; use back::Back; use forward::Forward; -use iter::Iter; use super::Subject; use gtk::{ prelude::{BoxExt, TextBufferExt, TextViewExt}, Box, Orientation, TextIter, }; -use std::cell::RefCell; +use std::{cell::RefCell, iter::Cycle, vec::IntoIter}; const MARGIN: i32 = 6; @@ -19,7 +17,7 @@ pub struct Navigation { pub back: Back, pub forward: Forward, pub g_box: Box, - iter: RefCell>, + iter: RefCell>>>, } impl Navigation { @@ -56,7 +54,7 @@ impl Navigation { pub fn update(&self, matches: Vec<(TextIter, TextIter)>) { self.back.update(!matches.is_empty()); self.forward.update(!matches.is_empty()); - self.iter.replace(Some(Iter::new(matches))); + self.iter.replace(Some(matches.into_iter().cycle())); } pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { @@ -69,15 +67,15 @@ impl Navigation { ); match self.iter.borrow_mut().as_mut() { - Some(iter) => match iter.back() { + Some(iter) => match iter.next() { Some((start, end)) => { buffer.apply_tag(&subject.tag.current, &start, &end); Some((start, end)) } - None => iter.reset(), + None => None, }, None => todo!(), // unexpected - } + } // @TODO reverse } pub fn forward(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { @@ -90,12 +88,12 @@ impl Navigation { ); match self.iter.borrow_mut().as_mut() { - Some(iter) => match iter.forward() { + Some(iter) => match iter.next() { Some((start, end)) => { buffer.apply_tag(&subject.tag.current, &start, &end); Some((start, end)) } - None => iter.reset(), + None => None, }, None => todo!(), // unexpected } diff --git a/src/app/browser/window/tab/item/page/search/form/navigation/iter.rs b/src/app/browser/window/tab/item/page/search/form/navigation/iter.rs deleted file mode 100644 index 28359746..00000000 --- a/src/app/browser/window/tab/item/page/search/form/navigation/iter.rs +++ /dev/null @@ -1,45 +0,0 @@ -use gtk::TextIter; - -pub struct Iter { - value: Vec<(TextIter, TextIter)>, - index: Option, -} - -impl Iter { - pub fn new(value: Vec<(TextIter, TextIter)>) -> Self { - Self { index: None, value } - } - - pub fn back(&mut self) -> Option<(TextIter, TextIter)> { - self.index = match self.index { - Some(index) => { - if index > 0 { - Some(index - 1) - } else { - Some(self.value.len()) - } - } - None => Some(self.value.len()), // init - }; - self.value.get(self.index.unwrap_or_default()).copied() - } - - pub fn forward(&mut self) -> Option<(TextIter, TextIter)> { - self.index = match self.index { - Some(index) => { - if index < self.value.len() { - Some(index + 1) - } else { - Some(0) - } - } - None => Some(0), // init - }; - self.value.get(self.index.unwrap_or_default()).copied() - } - - pub fn reset(&mut self) -> Option<(TextIter, TextIter)> { - self.index = None; - self.forward() - } -}