ferrite_config/
zoom.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::{
    defaults::zoom::*,
    error::{ConfigError, Result},
};
use serde::{Deserialize, Serialize};

/// Validated, ordered set of zoom steps
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ZoomSteps(Vec<f64>);

impl ZoomSteps {
    pub fn new(mut steps: Vec<f64>) -> Result<Self> {
        if steps.is_empty() {
            return Err(ConfigError::ValidationError(
                "Zoom steps cannot be empty".into(),
            ));
        }
        steps.sort_by(|a, b| {
            a.partial_cmp(b)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        steps.dedup_by(|a, b| (*a - *b).abs() < f64::EPSILON);
        Ok(Self(steps))
    }

    pub fn as_slice(&self) -> &[f64] {
        &self.0
    }
}

impl Default for ZoomSteps {
    fn default() -> Self {
        Self::new(DEFAULT_ZOOM_STEPS.to_vec())
            .expect("Default zoom steps must be valid")
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum FitMode {
    /// Display image at actual size (100% zoom)
    OneToOne,
    /// Fit image to window, scaling by the longer dimension
    FitLonger,
    /// Fit image to window, scaling by the shorter dimension
    FitShorter,
    /// Use custom zoom level
    Custom,
}

impl Default for FitMode {
    fn default() -> Self {
        FitMode::FitLonger
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ZoomConfig {
    pub min_zoom:              f64,
    pub max_zoom:              f64,
    pub default_zoom:          f64,
    pub zoom_step:             f64,
    pub use_predefined_steps:  bool,
    pub zoom_steps:            ZoomSteps,
    pub focal_point_enabled:   bool,
    pub transition_enabled:    bool,
    pub transition_duration:   f64,
    pub fit_to_window:         bool,
    pub maintain_aspect_ratio: bool,
    pub default_fit_mode:      FitMode,
}

impl Default for ZoomConfig {
    fn default() -> Self {
        Self {
            min_zoom:              MIN_ZOOM,
            max_zoom:              MAX_ZOOM,
            default_zoom:          DEFAULT_ZOOM,
            zoom_step:             ZOOM_STEP,
            use_predefined_steps:  USE_PREDEFINED_STEPS,
            zoom_steps:            ZoomSteps::default(),
            focal_point_enabled:   FOCAL_POINT_ENABLED,
            transition_enabled:    TRANSITION_ENABLED,
            transition_duration:   TRANSITION_DURATION,
            fit_to_window:         FIT_TO_WINDOW,
            maintain_aspect_ratio: MAINTAIN_ASPECT_RATIO,
            default_fit_mode:      FitMode::default(),
        }
    }
}

impl ZoomConfig {
    pub fn validate(&self) -> Result<()> {
        if self.min_zoom <= 0.0 {
            return Err(ConfigError::ValidationError(
                "min_zoom must be positive".into(),
            ));
        }

        if self.max_zoom <= self.min_zoom {
            return Err(ConfigError::ValidationError(format!(
                "max_zoom ({}) must be greater than min_zoom ({})",
                self.max_zoom, self.min_zoom
            )));
        }

        if self.default_zoom < self.min_zoom
            || self.default_zoom > self.max_zoom
        {
            return Err(ConfigError::ValidationError(format!(
                "default_zoom must be between {} and {}",
                self.min_zoom, self.max_zoom
            )));
        }

        if self.zoom_step <= 0.0 {
            return Err(ConfigError::ValidationError(
                "zoom_step must be positive".into(),
            ));
        }

        if self.transition_duration < 0.0 {
            return Err(ConfigError::ValidationError(
                "transition_duration cannot be negative".into(),
            ));
        }

        if self.use_predefined_steps {
            for &step in self.zoom_steps.as_slice() {
                if step < self.min_zoom || step > self.max_zoom {
                    return Err(ConfigError::ValidationError(format!(
                        "zoom step {} is outside allowed range [{}, {}]",
                        step, self.min_zoom, self.max_zoom
                    )));
                }
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_zoom_steps_ordering() {
        let steps = ZoomSteps::new(vec![2.0, 1.0, 3.0]).unwrap();
        assert_eq!(steps.as_slice(), &[1.0, 2.0, 3.0]);
    }

    #[test]
    fn test_zoom_validation() {
        let config = ZoomConfig::default();
        assert!(config.validate().is_ok());

        let mut invalid = config.clone();
        invalid.min_zoom = -1.0;
        assert!(invalid.validate().is_err());
    }
}