image_view: Rename N_PAGES to BUFFER

This is for better readability. Also changes the comment,
which I forgot to update while reworking how we handle the
model.
This commit is contained in:
Christopher Davis 2022-06-29 18:25:45 -04:00
parent 8c56e2c66a
commit ddb02d0eb7

View file

@ -36,9 +36,9 @@ use crate::thumbnail::Thumbnail;
use crate::util; use crate::util;
use crate::widgets::LpImagePage; use crate::widgets::LpImagePage;
// Maximum number of pages to load // The number of pages we want to buffer
// at any given time // on either side of the current page.
const N_PAGES: u32 = 2; const BUFFER: u32 = 2;
mod imp { mod imp {
use super::*; use super::*;
@ -253,13 +253,13 @@ impl LpImageView {
let imp = self.imp(); let imp = self.imp();
let carousel = imp.carousel.get(); let carousel = imp.carousel.get();
for i in 1..=N_PAGES { for i in 1..=BUFFER {
if let Some(ref file) = model.file(index + i) { if let Some(ref file) = model.file(index + i) {
carousel.append(&LpImagePage::from_file(file)) carousel.append(&LpImagePage::from_file(file))
} }
} }
for i in 1..=N_PAGES { for i in 1..=BUFFER {
if let Some(ref file) = index.checked_sub(i).and_then(|i| model.file(i)) { if let Some(ref file) = index.checked_sub(i).and_then(|i| model.file(i)) {
carousel.prepend(&LpImagePage::from_file(file)) carousel.prepend(&LpImagePage::from_file(file))
} }
@ -321,14 +321,14 @@ impl LpImageView {
if let Some(diff) = model_index.checked_sub(prev_index) { if let Some(diff) = model_index.checked_sub(prev_index) {
for i in 0..diff { for i in 0..diff {
if prev_index if prev_index
.checked_sub(N_PAGES) .checked_sub(BUFFER)
.and_then(|r| r.checked_sub(i)) .and_then(|r| r.checked_sub(i))
.is_some() .is_some()
{ {
carousel.remove(&carousel.nth_page(0)); carousel.remove(&carousel.nth_page(0));
} }
let s = prev_index + N_PAGES + i + 1; let s = prev_index + BUFFER + i + 1;
if s <= model.n_items() { if s <= model.n_items() {
if let Some(ref file) = model.file(s) { if let Some(ref file) = model.file(s) {
carousel.append(&LpImagePage::from_file(file)); carousel.append(&LpImagePage::from_file(file));
@ -340,13 +340,13 @@ impl LpImageView {
// We've moved backward // We've moved backward
if let Some(diff) = prev_index.checked_sub(model_index) { if let Some(diff) = prev_index.checked_sub(model_index) {
for i in 0..diff { for i in 0..diff {
let s = prev_index + N_PAGES + i + 1; let s = prev_index + BUFFER + i + 1;
if s <= model.n_items() { if s <= model.n_items() {
carousel.remove(&carousel.nth_page(carousel.n_pages() - 1)); carousel.remove(&carousel.nth_page(carousel.n_pages() - 1));
} }
if let Some(ref file) = prev_index if let Some(ref file) = prev_index
.checked_sub(N_PAGES) .checked_sub(BUFFER)
.and_then(|d| d.checked_sub(i + 1)) .and_then(|d| d.checked_sub(i + 1))
.and_then(|d| model.file(d)) .and_then(|d| model.file(d))
{ {