Skip to main content

fresh/app/
calibration_actions.rs

1//! Calibration wizard action handling
2//!
3//! This module provides the action handlers for the input calibration wizard.
4
5use super::calibration_wizard::{CalibrationWizard, WizardAction};
6use super::Editor;
7use crate::input::handler::InputResult;
8use crossterm::event::KeyEvent;
9use rust_i18n::t;
10
11impl Editor {
12    /// Open the calibration wizard
13    pub fn open_calibration_wizard(&mut self) {
14        self.calibration_wizard = Some(CalibrationWizard::new());
15        self.set_status_message(t!("calibration.started").to_string());
16    }
17
18    /// Save calibration and close wizard
19    pub fn save_calibration(&mut self, wizard: CalibrationWizard) {
20        let translator = wizard.build_translator();
21        let count = translator.len();
22
23        // Save to config file
24        if let Err(e) = translator.save_to_config_dir(&self.dir_context.config_dir) {
25            tracing::error!("Failed to save key calibration: {}", e);
26            self.set_status_message(
27                t!("calibration.save_error", error = e.to_string()).to_string(),
28            );
29            return;
30        }
31
32        // Update the active translator
33        self.key_translator = translator;
34
35        self.set_status_message(t!("calibration.saved", count = count).to_string());
36    }
37
38    /// Handle input when calibration wizard is active
39    pub fn handle_calibration_input(&mut self, event: &KeyEvent) -> InputResult {
40        // Take the wizard temporarily to avoid borrowing issues
41        let mut wizard = match self.calibration_wizard.take() {
42            Some(w) => w,
43            None => return InputResult::Ignored,
44        };
45
46        // Handle the key based on current state
47        let action = if wizard.has_pending_confirmation() {
48            wizard.handle_confirmation_key(*event)
49        } else if wizard.is_verify_phase() {
50            wizard.handle_verify_key(*event)
51        } else {
52            wizard.handle_capture_key(*event)
53        };
54
55        // Update status message from wizard
56        if let Some(msg) = wizard.status_message.take() {
57            self.set_status_message(msg);
58        }
59
60        // Process the action, deciding what to do with the wizard
61        match action {
62            WizardAction::Continue
63            | WizardAction::GoBack
64            | WizardAction::SkipGroup
65            | WizardAction::KeyCaptured
66            | WizardAction::KeyVerified
67            | WizardAction::ReservedKey
68            | WizardAction::ShowConfirmation => {
69                // Put wizard back and continue
70                self.calibration_wizard = Some(wizard);
71            }
72            WizardAction::Abort => {
73                // Drop wizard (don't put back), show message
74                self.set_status_message(t!("calibration.aborted").to_string());
75            }
76            WizardAction::Save => {
77                // Pass wizard to save - compiler enforces we have it
78                self.save_calibration(wizard);
79            }
80            WizardAction::Restart => {
81                // Restart already called by handle_confirmation_key
82                self.calibration_wizard = Some(wizard);
83            }
84        }
85
86        InputResult::Consumed
87    }
88
89    /// Check if calibration wizard is active
90    pub fn is_calibration_active(&self) -> bool {
91        self.calibration_wizard.is_some()
92    }
93}