trackWork 0.15.0

A terminal-based time tracking application for managing work sessions
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::app::{App, InputMode};

/// Handle keyboard input for the Triggers sub-settings screen.
pub fn handle_triggers_input(app: &mut App, key: KeyEvent) {
    // `current_field` = event*3 + sub (0=enabled, 1=url, 2=body).
    let on_enabled = matches!(&app.input_mode, InputMode::Triggers { current_field, .. } if current_field % 3 == 0);

    match key.code {
        KeyCode::Enter => {
            let _ = app.save_triggers();
        }
        KeyCode::Esc => {
            // Discard edits, return to Settings.
            app.open_settings();
        }
        KeyCode::Tab if key.modifiers.contains(KeyModifiers::SHIFT) => app.previous_field(),
        KeyCode::Tab => app.next_field(),
        KeyCode::Up => app.previous_field(),
        KeyCode::Down => app.next_field(),
        // Enabled toggle field
        KeyCode::Left | KeyCode::Right | KeyCode::Char(' ') if on_enabled => {
            if let InputMode::Triggers { enabled, current_field, .. } = &mut app.input_mode {
                let event = *current_field / 3;
                enabled[event] = !enabled[event];
            }
        }
        KeyCode::Char(_) | KeyCode::Backspace if on_enabled => {
            // Ignore text input on the toggle field.
        }
        // Text fields (URL / body)
        KeyCode::Left => app.cursor_left(),
        KeyCode::Right => app.cursor_right(),
        KeyCode::Char('+') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.insert_trigger_variable();
        }
        KeyCode::Char(c) => app.input_char(c),
        KeyCode::Backspace => app.delete_char(),
        _ => {}
    }
}