From c521ae4f5dc4e962cc90b559f11fe060860d7790 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 18 Dec 2024 06:15:09 +0200 Subject: [PATCH] move cursor into separated mod --- .../tab/item/page/search/form/navigation.rs | 4 +- .../item/page/search/form/navigation/model.rs | 37 ++----------------- .../search/form/navigation/model/cursor.rs | 37 +++++++++++++++++++ 3 files changed, 42 insertions(+), 36 deletions(-) create mode 100644 src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.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 03cbce92..795a6cb7 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 @@ -47,7 +47,7 @@ impl Navigation { back, forward, g_box, - model: RefCell::new(Model::new(Vec::new())), // @TODO + model: RefCell::new(Model::new(Vec::new())), // @TODO option? } } @@ -56,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.model.replace(Model::new(matches)); + self.model.replace(Model::new(matches)); } pub fn back(&self, subject: &Subject) -> Option<(TextIter, TextIter)> { 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 index 12faf135..5b93bf34 100644 --- 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 @@ -1,33 +1,5 @@ -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 - } - } -} +mod cursor; +use cursor::Cursor; pub struct Model { cursor: Cursor, @@ -37,10 +9,7 @@ pub struct Model { impl Model { pub fn new(vector: Vec) -> Self { Self { - cursor: Cursor { - current: 0, - last: vector.len(), - }, + cursor: Cursor::new(vector.len()), vector, } } diff --git a/src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.rs b/src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.rs new file mode 100644 index 00000000..45a52b26 --- /dev/null +++ b/src/app/browser/window/tab/item/page/search/form/navigation/model/cursor.rs @@ -0,0 +1,37 @@ +pub struct Cursor { + current: usize, + last: usize, +} + +impl Cursor { + pub fn new(len: usize) -> Self { + Self { + current: 0, + last: len, + } + } + + 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 + } + } +}