ferrite_navigation/
manager.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use crate::error::{NavigationError, Result};
use ferrite_image::SupportedFormats;
use std::{
    fs,
    path::{Path, PathBuf},
};
use tracing::info;

pub struct NavigationManager {
    directory_images: Vec<PathBuf>,
    current_index:    usize,
}

impl NavigationManager {
    pub fn new() -> Self {
        Self {
            directory_images: Vec::new(), current_index: 0
        }
    }

    pub fn load_current_directory(&mut self, image_path: &Path) -> Result<()> {
        let absolute_path = fs::canonicalize(image_path)
            .map_err(NavigationError::DirectoryAccess)?;

        let parent_dir = absolute_path.parent().ok_or_else(|| {
            NavigationError::InvalidPath(image_path.to_path_buf())
        })?;

        info!("Loading images from directory: {}", parent_dir.display());

        // Collect valid image paths
        self.directory_images = fs::read_dir(parent_dir)
            .map_err(NavigationError::DirectoryAccess)?
            .filter_map(|entry| {
                entry.ok().and_then(|e| {
                    let path = e.path();
                    if path.is_file()
                        && SupportedFormats::is_supported(path.extension())
                    {
                        Some(path)
                    } else {
                        None
                    }
                })
            })
            .collect();

        self.directory_images.sort();

        self.current_index = self
            .directory_images
            .iter()
            .position(|p| p == &absolute_path)
            .unwrap_or(0);

        info!(
            "Found {} images in directory, current image at index {}",
            self.directory_images.len(),
            self.current_index
        );

        Ok(())
    }

    pub fn get_nearby_paths(
        &self,
        count: usize,
    ) -> (Vec<PathBuf>, Vec<PathBuf>) {
        if self.directory_images.is_empty() {
            return (Vec::new(), Vec::new());
        }

        let total_images = self.directory_images.len();
        let mut next_paths = Vec::with_capacity(count);
        let mut prev_paths = Vec::with_capacity(count);

        for i in 1..=count {
            // Get next images circularly
            let next_index = (self.current_index + i) % total_images;
            if next_index != self.current_index {
                next_paths.push(self.directory_images[next_index].clone());
            }

            // Get previous images circularly
            let prev_index = if i <= self.current_index {
                self.current_index - i
            } else {
                total_images - (i - self.current_index)
            };
            if prev_index < total_images && prev_index != self.current_index {
                prev_paths.push(self.directory_images[prev_index].clone());
            }
        }

        (prev_paths, next_paths)
    }

    pub fn next_image(&mut self) -> Option<PathBuf> {
        if self.directory_images.is_empty() {
            return None;
        }
        self.current_index =
            (self.current_index + 1) % self.directory_images.len();
        Some(self.directory_images[self.current_index].clone())
    }

    pub fn previous_image(&mut self) -> Option<PathBuf> {
        if self.directory_images.is_empty() {
            return None;
        }
        self.current_index = if self.current_index == 0 {
            self.directory_images.len() - 1
        } else {
            self.current_index - 1
        };
        Some(self.directory_images[self.current_index].clone())
    }
}