edit to read_only mode without save

This commit is contained in:
filipriec
2025-04-07 10:13:39 +02:00
parent 803c748738
commit 5a3067c8e5

View File

@@ -1,9 +1,7 @@
// src/modes/handlers/event.rs
use crossterm::event::Event;
use crossterm::cursor::SetCursorStyle;
use crate::tui::terminal::{
core::TerminalCore,
};
use crate::tui::terminal::core::TerminalCore;
use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient;
use crate::modes::common::commands::CommandHandler;
@@ -13,7 +11,7 @@ use crate::state::pages::auth::AuthState;
use crate::state::canvas_state::CanvasState;
use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::{
common::{command_mode},
common::command_mode,
canvas::{edit, read_only, common_mode},
general::navigation,
};
@@ -28,7 +26,6 @@ pub struct EventHandler {
pub edit_mode_cooldown: bool,
pub ideal_cursor_column: usize,
pub key_sequence_tracker: KeySequenceTracker,
// pub auth_state: AuthState, // Removed
pub auth_client: AuthClient,
}
@@ -42,7 +39,6 @@ impl EventHandler {
edit_mode_cooldown: false,
ideal_cursor_column: 0,
key_sequence_tracker: KeySequenceTracker::new(800),
// auth_state: AuthState::new(), // Removed
auth_client: AuthClient::new().await?,
})
}
@@ -55,12 +51,11 @@ impl EventHandler {
grpc_client: &mut GrpcClient,
command_handler: &mut CommandHandler,
form_state: &mut FormState,
auth_state: &mut AuthState, // Added
auth_state: &mut AuthState,
app_state: &mut crate::state::state::AppState,
total_count: u64,
current_position: &mut u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
// Determine current mode based on app state and event handler state
let current_mode = ModeManager::derive_mode(app_state, self);
app_state.update_mode(current_mode);
@@ -68,14 +63,12 @@ impl EventHandler {
let key_code = key.code;
let modifiers = key.modifiers;
// Handle common actions across all modes
if UiStateHandler::toggle_sidebar(&mut app_state.ui, config, key_code, modifiers) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
// Mode-specific handling
match current_mode {
AppMode::General => {
return navigation::handle_navigation_event(
@@ -90,7 +83,6 @@ impl EventHandler {
},
AppMode::ReadOnly => {
// Check for mode transitions first
if config.is_enter_edit_mode_before(key_code, modifiers) &&
ModeManager::can_enter_edit_mode(current_mode) {
self.is_edit_mode = true;
@@ -129,7 +121,6 @@ impl EventHandler {
return Ok((false, self.command_message.clone()));
}
// Check for entering command mode
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
if action == "enter_command_mode" && ModeManager::can_enter_command_mode(current_mode) {
self.command_mode = true;
@@ -139,7 +130,6 @@ impl EventHandler {
}
}
// Check for core application actions (save, quit, etc.)
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
@@ -150,7 +140,7 @@ impl EventHandler {
return common_mode::handle_core_action(
action,
form_state,
auth_state, // Changed
auth_state,
grpc_client,
&mut self.auth_client,
terminal,
@@ -163,13 +153,12 @@ impl EventHandler {
}
}
// Let read_only mode handle its own actions
return read_only::handle_read_only_event(
&app_state,
key,
config,
form_state,
auth_state, // Changed
auth_state,
&mut self.key_sequence_tracker,
current_position,
total_count,
@@ -181,21 +170,22 @@ impl EventHandler {
},
AppMode::Edit => {
// Check for exiting edit mode
if config.is_exit_edit_mode(key_code, modifiers) {
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
let has_changes = if app_state.ui.show_login {
auth_state.has_unsaved_changes()
} else {
form_state.has_unsaved_changes()
};
if has_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
return Ok((false, self.command_message.clone()));
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
self.command_message = if has_changes {
"Exited edit mode (unsaved changes remain)".to_string()
} else {
"Read-only mode".to_string()
};
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
let current_input = if app_state.ui.show_login {
@@ -222,7 +212,6 @@ impl EventHandler {
return Ok((false, self.command_message.clone()));
}
// Check for core application actions (save, quit, etc.)
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
@@ -230,29 +219,28 @@ impl EventHandler {
) {
match action {
"save" | "force_quit" | "save_and_quit" | "revert" => {
return common_mode::handle_core_action(
action,
form_state,
auth_state, // Changed
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,
total_count,
).await;
},
return common_mode::handle_core_action(
action,
form_state,
auth_state,
grpc_client,
&mut self.auth_client,
terminal,
app_state,
current_position,
total_count,
).await;
},
_ => {}
}
}
// Let edit mode handle its own actions
let result = edit::handle_edit_event(
app_state.ui.show_login,
key,
config,
form_state,
auth_state, // Changed
auth_state,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.ui.is_saved,
@@ -288,9 +276,7 @@ impl EventHandler {
}
}
// Non-key events or if no specific handler was matched
self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone()))
}
}