edit and read only mode

This commit is contained in:
filipriec
2025-02-17 23:44:09 +01:00
parent 14d0b5d3dc
commit 3864fc9ae1
3 changed files with 28 additions and 13 deletions

View File

@@ -4,4 +4,5 @@ save = [":w", "ctrl+s"]
quit = [":q", "ctrl+q"] quit = [":q", "ctrl+q"]
force_quit = [":q!", "ctrl+shift+q"] force_quit = [":q!", "ctrl+shift+q"]
save_and_quit = [":wq", "ctrl+shift+s"] 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"]

View File

@@ -64,8 +64,15 @@ impl Config {
None None
} }
pub fn is_toggle_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { pub fn is_enter_edit_mode(&self, key: KeyCode, modifiers: KeyModifiers) -> bool {
if let Some(bindings) = self.keybindings.get("toggle_edit_mode") { 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)) bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers))
} else { } else {
false false

View File

@@ -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::components::{render_command_line, render_form, render_preview_card, render_status_line};
use crate::client::colors::Theme; use crate::client::colors::Theme;
use crate::client::config::Config; use crate::client::config::Config;
use ratatui::layout::{Constraint, Direction, Layout, Rect}; use ratatui::layout::{Constraint, Direction, Layout};
use std::env; use std::env;
use crate::proto::multieko2::PostAdresarRequest; use crate::proto::multieko2::PostAdresarRequest;
@@ -101,11 +101,16 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
// Event handling // Event handling
if let Event::Key(key) = app_terminal.read_event()? { if let Event::Key(key) = app_terminal.read_event()? {
// First check for edit mode toggle (works in any mode) // Handle enter/edit mode keys
if config.is_toggle_edit_mode(key.code, key.modifiers) { if !is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) {
is_edit_mode = !is_edit_mode; is_edit_mode = true;
edit_mode_cooldown = true; // Prevent accidental double-toggles edit_mode_cooldown = true;
command_message = format!("{} mode", if is_edit_mode { "Edit" } else { "Read-only" }); 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; continue;
} }
@@ -133,8 +138,11 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
_ => { _ => {
// Block all other inputs // Block all other inputs
if !edit_mode_cooldown { if !edit_mode_cooldown {
command_message = format!("Read-only mode - press {} to edit", let default_key = "i".to_string();
config.keybindings.get("toggle_edit_mode").unwrap()[0]); 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<dyn std::error::Error>> {
} }
KeyCode::Char(c) => command_input.push(c), KeyCode::Char(c) => command_input.push(c),
KeyCode::Backspace => { KeyCode::Backspace => {
command_input.pop(); // Ignore the return value command_input.pop();
} }
KeyCode::Esc => { KeyCode::Esc => {
command_mode = false; command_mode = false;
@@ -218,7 +226,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
fax: fax.clone(), fax: fax.clone(),
}; };
// Pass form data to handle_command (remove &config)
let (should_exit, message) = app_terminal let (should_exit, message) = app_terminal
.handle_command(action, &mut is_saved, &form_data) .handle_command(action, &mut is_saved, &form_data)
.await?; .await?;