From d609c4e0b24738683ef7191a98f9df260ee77bc7 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Thu, 14 Jul 2022 13:30:38 -0400 Subject: [PATCH] image_view: Adjust model index name Previously we used `prev_index` everywhere, which could get confusing - it's not really the "previous" index until we've moved. So, only `page_changed_cb ()` refers to it as `prev_index`. Everywhere else it's called `current_model_index`. In accordance with this change, we can get rid of a call to `current_page()` and `index_of()` since we already know the index of the current page. --- src/widgets/image_view.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/widgets/image_view.rs b/src/widgets/image_view.rs index cf377fa..ec13b5f 100644 --- a/src/widgets/image_view.rs +++ b/src/widgets/image_view.rs @@ -51,7 +51,7 @@ mod imp { pub model: RefCell>, pub filename: RefCell>, - pub prev_index: Cell, + pub current_model_index: Cell, } #[glib::object_subclass] @@ -216,15 +216,9 @@ impl LpImageView { fn navigate_to_file(&self, model: &LpFileModel, file: &gio::File) { let imp = self.imp(); let carousel = imp.carousel.get(); - let current_index = self - .current_page() - .and_then(|p| p.file()) - .and_then(|f| model.index_of(&f)) - .unwrap_or_default(); + let current_index = imp.current_model_index.get(); let new_index = model.index_of(file).unwrap_or_default(); - // Code style note: I generally don't do early returns like this - // in my rust code, but here we do it to avoid code duplication. if new_index == current_index { return; } @@ -265,7 +259,7 @@ impl LpImageView { } } - imp.prev_index.set(index); + imp.current_model_index.set(index); } // Clear the carousel, optionally preserving the current position @@ -306,7 +300,7 @@ impl LpImageView { let current = self.current_page().and_then(|p| p.file()).unwrap(); let model_index = model.index_of(¤t).unwrap(); - let prev_index = imp.prev_index.get(); + let prev_index = imp.current_model_index.get(); if model_index != prev_index { imp.filename.replace(util::get_file_display_name(¤t)); @@ -351,7 +345,7 @@ impl LpImageView { } } - imp.prev_index.set(model_index); + imp.current_model_index.set(model_index); } }