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