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"]
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"]

View File

@@ -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

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::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<dyn std::error::Error>> {
// 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<dyn std::error::Error>> {
_ => {
// 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<dyn std::error::Error>> {
}
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<dyn std::error::Error>> {
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?;