249 lines
9.5 KiB
Rust
249 lines
9.5 KiB
Rust
// src/modes/canvas/read_only.rs
|
|
|
|
use crate::config::binds::config::Config;
|
|
use crate::config::binds::key_sequences::KeySequenceTracker;
|
|
use crate::services::grpc_client::GrpcClient;
|
|
use crate::state::pages::{canvas_state::CanvasState, auth::RegisterState};
|
|
use crate::state::pages::auth::LoginState;
|
|
use crate::state::pages::form::FormState;
|
|
use crate::state::app::state::AppState;
|
|
use crate::functions::modes::read_only::{auth_ro, form_ro};
|
|
use crossterm::event::KeyEvent;
|
|
|
|
pub async fn handle_read_only_event(
|
|
app_state: &mut AppState,
|
|
key: KeyEvent,
|
|
config: &Config,
|
|
form_state: &mut FormState,
|
|
login_state: &mut LoginState,
|
|
register_state: &mut RegisterState,
|
|
key_sequence_tracker: &mut KeySequenceTracker,
|
|
current_position: &mut u64,
|
|
total_count: u64,
|
|
grpc_client: &mut GrpcClient,
|
|
command_message: &mut String,
|
|
edit_mode_cooldown: &mut bool,
|
|
ideal_cursor_column: &mut usize,
|
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
|
if config.is_enter_edit_mode_before(key.code, key.modifiers) {
|
|
*edit_mode_cooldown = true;
|
|
*command_message = "Entering Edit mode".to_string();
|
|
return Ok((false, command_message.clone()));
|
|
}
|
|
|
|
if config.is_enter_edit_mode_after(key.code, key.modifiers) {
|
|
let (current_input, current_pos) = if app_state.ui.show_login { // Check Login first
|
|
(
|
|
login_state.get_current_input(),
|
|
login_state.current_cursor_pos(),
|
|
)
|
|
} else if app_state.ui.show_register { // Then check Register
|
|
(
|
|
register_state.get_current_input(),
|
|
register_state.current_cursor_pos(),
|
|
)
|
|
} else {
|
|
(
|
|
form_state.get_current_input(),
|
|
form_state.current_cursor_pos(),
|
|
) // Default to Form
|
|
};
|
|
|
|
if !current_input.is_empty() && current_pos < current_input.len() {
|
|
if app_state.ui.show_login {
|
|
login_state.set_current_cursor_pos(current_pos + 1);
|
|
*ideal_cursor_column = login_state.current_cursor_pos();
|
|
} else if app_state.ui.show_register {
|
|
register_state.set_current_cursor_pos(current_pos + 1);
|
|
*ideal_cursor_column = register_state.current_cursor_pos();
|
|
} else { // Default to Form
|
|
form_state.set_current_cursor_pos(current_pos + 1);
|
|
*ideal_cursor_column = form_state.current_cursor_pos();
|
|
}
|
|
}
|
|
*edit_mode_cooldown = true;
|
|
*command_message = "Entering Edit mode (after cursor)".to_string();
|
|
return Ok((false, command_message.clone()));
|
|
}
|
|
|
|
const CONTEXT_ACTIONS_FORM: &[&str] = &[
|
|
"previous_entry",
|
|
"next_entry",
|
|
];
|
|
const CONTEXT_ACTIONS_LOGIN: &[&str] = &[
|
|
"previous_entry",
|
|
"next_entry",
|
|
];
|
|
// Add context actions specific to register if needed, otherwise reuse login/form ones
|
|
const CONTEXT_ACTIONS_REGISTER: &[&str] = &[
|
|
// Add actions like "next_field", "prev_field" if handled differently than general read-only
|
|
];
|
|
|
|
if key.modifiers.is_empty() {
|
|
key_sequence_tracker.add_key(key.code);
|
|
let sequence = key_sequence_tracker.get_sequence();
|
|
|
|
if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
|
|
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
|
crate::tui::functions::form::handle_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
|
crate::tui::functions::login::handle_action(action).await?
|
|
} else if app_state.ui.show_register{
|
|
auth_ro::execute_action(
|
|
action,
|
|
app_state,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
).await?
|
|
} else if app_state.ui.show_login {
|
|
auth_ro::execute_action(
|
|
action,
|
|
app_state,
|
|
login_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
)
|
|
.await?
|
|
} else {
|
|
form_ro::execute_action(
|
|
action,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
)
|
|
.await?
|
|
};
|
|
key_sequence_tracker.reset();
|
|
return Ok((false, result));
|
|
}
|
|
|
|
if config.is_key_sequence_prefix(&sequence) {
|
|
return Ok((false, command_message.clone()));
|
|
}
|
|
|
|
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
|
|
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
|
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
|
crate::tui::functions::form::handle_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
|
crate::tui::functions::login::handle_action(action).await?
|
|
} else if app_state.ui.show_register /* && CONTEXT_ACTIONS_REGISTER.contains(&action) */ { // Handle register general actions
|
|
auth_ro::execute_action(
|
|
action,
|
|
app_state,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
).await?
|
|
} else if app_state.ui.show_login { // Handle login general actions
|
|
auth_ro::execute_action(
|
|
action,
|
|
app_state,
|
|
login_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
)
|
|
.await?
|
|
} else {
|
|
form_ro::execute_action(
|
|
action,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
)
|
|
.await?
|
|
};
|
|
key_sequence_tracker.reset();
|
|
return Ok((false, result));
|
|
}
|
|
}
|
|
key_sequence_tracker.reset();
|
|
} else {
|
|
key_sequence_tracker.reset();
|
|
|
|
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
|
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
|
crate::tui::functions::form::handle_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
|
crate::tui::functions::login::handle_action(action).await?
|
|
} else if app_state.ui.show_register /* && CONTEXT_ACTIONS_REGISTER.contains(&action) */ { // Handle register general actions
|
|
auth_ro::execute_action(
|
|
action,
|
|
app_state,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
).await?
|
|
} else if app_state.ui.show_login { // Handle login general actions
|
|
auth_ro::execute_action(
|
|
action,
|
|
app_state,
|
|
login_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
)
|
|
.await?
|
|
} else {
|
|
form_ro::execute_action(
|
|
action,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
key_sequence_tracker,
|
|
command_message,
|
|
)
|
|
.await?
|
|
};
|
|
return Ok((false, result));
|
|
}
|
|
}
|
|
|
|
if !*edit_mode_cooldown {
|
|
let default_key = "i".to_string();
|
|
let edit_key = config
|
|
.keybindings
|
|
.read_only
|
|
.get("enter_edit_mode_before")
|
|
.and_then(|keys| keys.first())
|
|
.map(|k| k.to_string())
|
|
.unwrap_or(default_key);
|
|
*command_message = format!("Read-only mode - press {} to edit", edit_key);
|
|
}
|
|
|
|
*edit_mode_cooldown = false;
|
|
|
|
Ok((false, command_message.clone()))
|
|
}
|