From 3864fc9ae1323983e6ba52b4e80ffd5d68b2f8cb Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 17 Feb 2025 23:44:09 +0100 Subject: [PATCH] edit and read only mode --- config.toml | 3 ++- src/client/config.rs | 11 +++++++++-- src/client/ui.rs | 27 +++++++++++++++++---------- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/config.toml b/config.toml index c668bb2..c189cba 100644 --- a/config.toml +++ b/config.toml @@ -4,4 +4,5 @@ save = [":w", "ctrl+s"] quit = [":q", "ctrl+q"] force_quit = [":q!", "ctrl+shift+q"] save_and_quit = [":wq", "ctrl+shift+s"] -toggle_edit_mode = ["i", "ctrl+e"] +enter_edit_mode = ["i", "ctrl+e"] +exit_edit_mode = ["esc", "ctrl+e"] diff --git a/src/client/config.rs b/src/client/config.rs index 9584c89..3dc02e8 100644 --- a/src/client/config.rs +++ b/src/client/config.rs @@ -64,8 +64,15 @@ impl Config { None } - pub fn is_toggle_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { - if let Some(bindings) = self.keybindings.get("toggle_edit_mode") { + pub fn is_enter_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { + if let Some(bindings) = self.keybindings.get("enter_edit_mode") { + bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers)) + } else { + false + } + } + pub fn is_exit_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { + if let Some(bindings) = self.keybindings.get("exit_edit_mode") { bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers)) } else { false diff --git a/src/client/ui.rs b/src/client/ui.rs index 58eb376..a7ef210 100644 --- a/src/client/ui.rs +++ b/src/client/ui.rs @@ -4,7 +4,7 @@ use crate::client::terminal::AppTerminal; use crate::client::components::{render_command_line, render_form, render_preview_card, render_status_line}; use crate::client::colors::Theme; use crate::client::config::Config; -use ratatui::layout::{Constraint, Direction, Layout, Rect}; +use ratatui::layout::{Constraint, Direction, Layout}; use std::env; use crate::proto::multieko2::PostAdresarRequest; @@ -101,11 +101,16 @@ pub async fn run_ui() -> Result<(), Box> { // Event handling if let Event::Key(key) = app_terminal.read_event()? { - // First check for edit mode toggle (works in any mode) - if config.is_toggle_edit_mode(key.code, key.modifiers) { - is_edit_mode = !is_edit_mode; - edit_mode_cooldown = true; // Prevent accidental double-toggles - command_message = format!("{} mode", if is_edit_mode { "Edit" } else { "Read-only" }); + // Handle enter/edit mode keys + if !is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) { + is_edit_mode = true; + edit_mode_cooldown = true; + command_message = "Edit mode".to_string(); + continue; + } else if is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) { + is_edit_mode = false; + edit_mode_cooldown = true; + command_message = "Read-only mode".to_string(); continue; } @@ -133,8 +138,11 @@ pub async fn run_ui() -> Result<(), Box> { _ => { // Block all other inputs if !edit_mode_cooldown { - command_message = format!("Read-only mode - press {} to edit", - config.keybindings.get("toggle_edit_mode").unwrap()[0]); + let default_key = "i".to_string(); + let edit_key = config.keybindings.get("enter_edit_mode") + .and_then(|keys| keys.first()) + .unwrap_or(&default_key); + command_message = format!("Read-only mode - press {} to edit", edit_key); } } } @@ -188,7 +196,7 @@ pub async fn run_ui() -> Result<(), Box> { } KeyCode::Char(c) => command_input.push(c), KeyCode::Backspace => { - command_input.pop(); // Ignore the return value + command_input.pop(); } KeyCode::Esc => { command_mode = false; @@ -218,7 +226,6 @@ pub async fn run_ui() -> Result<(), Box> { fax: fax.clone(), }; - // Pass form data to handle_command (remove &config) let (should_exit, message) = app_terminal .handle_command(action, &mut is_saved, &form_data) .await?;