220 lines
7.7 KiB
Rust
220 lines
7.7 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::canvas_state::CanvasState;
|
|
use crate::state::pages::auth::AuthState;
|
|
use crate::state::pages::form::FormState;
|
|
use crate::functions::modes::read_only::{auth_ro, form_ro};
|
|
use crossterm::event::KeyEvent;
|
|
|
|
pub async fn handle_read_only_event(
|
|
app_state: &crate::state::state::AppState,
|
|
key: KeyEvent,
|
|
config: &Config,
|
|
form_state: &mut FormState,
|
|
auth_state: &mut AuthState,
|
|
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 {
|
|
(
|
|
auth_state.get_current_input(),
|
|
auth_state.current_cursor_pos(),
|
|
)
|
|
} else {
|
|
(
|
|
form_state.get_current_input(),
|
|
form_state.current_cursor_pos(),
|
|
)
|
|
};
|
|
|
|
if !current_input.is_empty() && current_pos < current_input.len() {
|
|
if app_state.ui.show_login {
|
|
auth_state.set_current_cursor_pos(current_pos + 1);
|
|
*ideal_cursor_column = auth_state.current_cursor_pos();
|
|
} else {
|
|
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",
|
|
];
|
|
|
|
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) {
|
|
crate::tui::functions::login::handle_action(
|
|
action,
|
|
auth_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_login {
|
|
auth_ro::execute_action(
|
|
action,
|
|
auth_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) {
|
|
crate::tui::functions::login::handle_action(
|
|
action,
|
|
auth_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_login {
|
|
auth_ro::execute_action(
|
|
action,
|
|
auth_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) {
|
|
crate::tui::functions::login::handle_action(
|
|
action,
|
|
auth_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_login {
|
|
auth_ro::execute_action(
|
|
action,
|
|
auth_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()))
|
|
}
|