toggle on and off mode

This commit is contained in:
filipriec
2025-02-17 23:26:53 +01:00
parent 4cea540cae
commit 14d0b5d3dc
5 changed files with 194 additions and 131 deletions

View File

@@ -4,3 +4,4 @@ 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"]

View File

@@ -15,6 +15,7 @@ pub fn render_form(
current_field: &usize,
inputs: &[&String],
theme: &Theme,
is_edit_mode: bool,
) {
// Create Adresar card
let adresar_card = Block::default()
@@ -40,7 +41,11 @@ pub fn render_form(
// Create compact input container
let input_container = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.accent))
.border_style(if is_edit_mode {
Style::default().fg(theme.accent)
} else {
Style::default().fg(theme.secondary)
})
.style(Style::default().bg(theme.bg));
// Place the input container at the top
@@ -89,7 +94,7 @@ pub fn render_form(
f.render_widget(input_display, input_rows[i]);
// Position cursor at the correct position in the active field
if is_active {
if is_active && is_edit_mode { // Move cursor logic inside the loop
let cursor_x = input_rows[i].x + input.len() as u16;
let cursor_y = input_rows[i].y;
f.set_cursor(cursor_x, cursor_y);

View File

@@ -8,23 +8,27 @@ use ratatui::{
};
use crate::client::colors::Theme;
pub fn render_status_line(f: &mut Frame, area: Rect, current_dir: &str, theme: &Theme) {
pub fn render_status_line(
f: &mut Frame,
area: Rect,
current_dir: &str,
theme: &Theme,
is_edit_mode: bool
) {
// Program name and version
let program_info = format!("multieko2 v{}", env!("CARGO_PKG_VERSION"));
// Create the status line text
let status_line = Line::from(vec![
Span::styled(current_dir, Style::default().fg(theme.fg)),
Span::styled(
program_info,
Style::default().fg(theme.secondary).add_modifier(ratatui::style::Modifier::BOLD), // Use `secondary` color
),
]);
let mode_text = if is_edit_mode {
"[EDIT]"
} else {
"[READ-ONLY]"
};
// Render the status line
let paragraph = Paragraph::new(status_line)
.block(Block::default().style(Style::default().bg(theme.bg)))
.alignment(Alignment::Left);
let text = format!(" {} | {}", mode_text, current_dir);
let paragraph = Paragraph::new(text)
.style(Style::default().fg(theme.fg).bg(theme.bg));
f.render_widget(paragraph, area);
}

View File

@@ -63,4 +63,12 @@ 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") {
bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers))
} else {
false
}
}
}

View File

@@ -48,6 +48,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
// Track the current message to display in the command line
let mut command_message = String::new();
let mut is_edit_mode = false;
let mut edit_mode_cooldown = false;
loop {
app_terminal.draw(|f| {
@@ -77,6 +79,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&ucet, &skladm, &ico, &kontakt, &telefon, &skladu, &fax,
],
&theme,
is_edit_mode,
);
// Right panel - Preview Card
@@ -90,7 +93,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
);
// Status line
render_status_line(f, root[1], &current_dir, &theme);
render_status_line(f, root[1], &current_dir, &theme, is_edit_mode);
// Command line
render_command_line(f, root[2], &command_input, command_mode, &theme, &command_message);
@@ -98,6 +101,45 @@ 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" });
continue;
}
if !is_edit_mode {
// Read-only mode handling
match key.code {
KeyCode::Char(':') => {
command_mode = true;
command_input.clear();
command_message.clear();
}
KeyCode::Esc => {
command_mode = false;
command_input.clear();
command_message.clear();
}
// Allow navigation but prevent editing
KeyCode::Tab | KeyCode::BackTab | KeyCode::Down | KeyCode::Up => {
if key.modifiers.contains(KeyModifiers::SHIFT) {
current_field = current_field.saturating_sub(1);
} else {
current_field = (current_field + 1) % fields.len();
}
}
_ => {
// 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]);
}
}
}
} else {
// Existing edit mode handling
if command_mode {
match key.code {
KeyCode::Enter => {
@@ -248,4 +290,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
}
}
}
edit_mode_cooldown = false;
}
}