ferrite_config/
input.rs

1use egui::Key;
2use serde::{Deserialize, Serialize};
3
4use crate::{
5    defaults::controls::*,
6    error::{ConfigError, Result},
7};
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct ControlsConfig {
11    pub zoom_in_keys:   Vec<Key>,
12    pub zoom_out_keys:  Vec<Key>,
13    pub reset_zoom_key: Key,
14    pub toggle_fit_key: Key,
15    pub quit_key:       Key,
16    pub help_key:       Key,
17}
18
19impl Default for ControlsConfig {
20    fn default() -> Self {
21        Self {
22            zoom_in_keys:   vec![Key::Equals, Key::Plus, Key::W],
23            zoom_out_keys:  vec![Key::Minus, Key::S],
24            reset_zoom_key: Key::Num0,
25            toggle_fit_key: Key::F,
26            quit_key:       Key::Q,
27            help_key:       Key::H,
28        }
29    }
30}
31
32impl ControlsConfig {
33    pub fn validate(&self) -> Result<()> {
34        if self.zoom_in_keys.is_empty() {
35            return Err(ConfigError::ValidationError(
36                "No zoom in keys defined".into(),
37            ));
38        }
39        if self.zoom_out_keys.is_empty() {
40            return Err(ConfigError::ValidationError(
41                "No zoom out keys defined".into(),
42            ));
43        }
44
45        // Check for key conflicts
46        let mut all_keys = Vec::new();
47        all_keys.extend(&self.zoom_in_keys);
48        all_keys.extend(&self.zoom_out_keys);
49        all_keys.push(self.reset_zoom_key);
50        all_keys.push(self.toggle_fit_key);
51
52        let mut seen = Vec::new();
53        for key in all_keys {
54            if seen.contains(&key) {
55                return Err(ConfigError::ValidationError(format!(
56                    "Duplicate key binding found: {:?}",
57                    key
58                )));
59            }
60            seen.push(key);
61        }
62
63        Ok(())
64    }
65}