This commit is contained in:
filipriec
2025-04-10 16:05:06 +02:00
parent 6b241304fb
commit e729ed9df3
2 changed files with 50 additions and 15 deletions

View File

@@ -3,8 +3,11 @@
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
use crate::config::binds::config::Config; use crate::config::binds::config::Config;
use crate::state::state::AppState; use crate::state::state::AppState;
use crate::modes::handlers::event::EventOutcome;
use crate::ui::handlers::context::DialogPurpose; use crate::ui::handlers::context::DialogPurpose;
use crate::state::pages::auth::AuthState;
use crate::services::auth::AuthClient;
use crate::modes::handlers::event::EventOutcome;
use crate::tui::functions::common::login;
/// Handles key events specifically when a dialog is active. /// Handles key events specifically when a dialog is active.
/// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed), /// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed),
@@ -13,6 +16,8 @@ pub async fn handle_dialog_event(
event: &Event, event: &Event,
config: &Config, config: &Config,
app_state: &mut AppState, app_state: &mut AppState,
auth_state: &mut AuthState,
auth_client: &mut AuthClient,
) -> Option<Result<EventOutcome, Box<dyn std::error::Error>>> { ) -> Option<Result<EventOutcome, Box<dyn std::error::Error>>> {
if let Event::Key(key) = event { if let Event::Key(key) = event {
// Always allow Esc to dismiss // Always allow Esc to dismiss
@@ -38,7 +43,6 @@ pub async fn handle_dialog_event(
app_state.ui.dialog.dialog_active_button_index -= 1; app_state.ui.dialog.dialog_active_button_index -= 1;
} }
return Some(Ok(EventOutcome::Ok(String::new()))); return Some(Ok(EventOutcome::Ok(String::new())));
} }
"select" => { "select" => {
let selected_index = app_state.ui.dialog.dialog_active_button_index; let selected_index = app_state.ui.dialog.dialog_active_button_index;
@@ -50,8 +54,40 @@ pub async fn handle_dialog_event(
} }
}; };
// Handle Dialog Actions Directly Here
match purpose {
DialogPurpose::LoginSuccess => {
match selected_index {
0 => { // "Menu" button selected
// Hide dialog before calling action that might change state further
app_state.hide_dialog(); app_state.hide_dialog();
return Some(Ok(EventOutcome::DialogAction { purpose, selected_index })); let message = login::back_to_main(auth_state, app_state).await;
return Some(Ok(EventOutcome::Ok(message)));
}
1 => { // "Exit" button selected
app_state.hide_dialog();
return Some(Ok(EventOutcome::Exit("Exiting via dialog".to_string())));
}
_ => {
app_state.hide_dialog();
return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
}
}
}
DialogPurpose::LoginFailed => {
match selected_index {
0 => { // "OK" button selected
app_state.hide_dialog();
return Some(Ok(EventOutcome::Ok("Login failed dialog dismissed".to_string())));
}
_ => {
app_state.hide_dialog();
return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
}
}
}
// Add cases for other DialogPurpose variants here if needed
}
} }
_ => {} // Ignore other general actions when dialog is shown _ => {} // Ignore other general actions when dialog is shown
} }
@@ -63,4 +99,3 @@ pub async fn handle_dialog_event(
Some(Ok(EventOutcome::Ok(String::new()))) Some(Ok(EventOutcome::Ok(String::new())))
} }
} }

View File

@@ -12,7 +12,7 @@ use crate::state::canvas_state::CanvasState;
use crate::ui::handlers::rat_state::UiStateHandler; use crate::ui::handlers::rat_state::UiStateHandler;
use crate::ui::handlers::context::UiContext; use crate::ui::handlers::context::UiContext;
use crate::tui::functions::{intro, admin}; use crate::tui::functions::{intro, admin};
use crate::tui::functions::common::login; use crate::tui::functions::common::login;
use crate::modes::{ use crate::modes::{
common::command_mode, common::command_mode,
canvas::{edit, read_only, common_mode}, canvas::{edit, read_only, common_mode},
@@ -21,7 +21,6 @@ use crate::modes::{
use crate::config::binds::key_sequences::KeySequenceTracker; use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::modes::handlers::mode_manager::{ModeManager, AppMode}; use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
use crate::tui::functions::common::form::SaveOutcome; use crate::tui::functions::common::form::SaveOutcome;
use crate::ui::handlers::context::DialogPurpose;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub enum EventOutcome { pub enum EventOutcome {
@@ -29,7 +28,6 @@ pub enum EventOutcome {
Exit(String), Exit(String),
DataSaved(SaveOutcome, String), DataSaved(SaveOutcome, String),
ButtonSelected { context: UiContext, index: usize }, ButtonSelected { context: UiContext, index: usize },
DialogAction { purpose: DialogPurpose, selected_index: usize },
} }
pub struct EventHandler { pub struct EventHandler {
@@ -76,14 +74,18 @@ impl EventHandler {
// --- DIALOG MODALITY --- // --- DIALOG MODALITY ---
// If a dialog is showing, intercept and handle ONLY dialog inputs. // If a dialog is showing, intercept and handle ONLY dialog inputs.
if app_state.ui.dialog.dialog_show { if app_state.ui.dialog.dialog_show {
if let Some(dialog_result) = dialog::handle_dialog_event(&event, config, app_state).await { // Pass event handling to the dedicated dialog handler, including necessary state
if let Some(dialog_result) = dialog::handle_dialog_event(
&event, config, app_state, auth_state, &mut self.auth_client
).await {
// If the dialog handler consumed the event, return its result
return dialog_result; return dialog_result;
} }
// Fallback if dialog handler returned None or event wasn't handled
return Ok(EventOutcome::Ok(String::new())); return Ok(EventOutcome::Ok(String::new()));
} }
// --- END DIALOG MODALITY CHECK --- // --- END DIALOG MODALITY CHECK ---
if let Event::Key(key) = event { if let Event::Key(key) = event {
let key_code = key.code; let key_code = key.code;
let modifiers = key.modifiers; let modifiers = key.modifiers;
@@ -128,9 +130,7 @@ impl EventHandler {
message = format!("Admin Option {} selected", index); message = format!("Admin Option {} selected", index);
} }
UiContext::Dialog => { UiContext::Dialog => {
// Add specific dialog handling logic here message = "Internal error: Unexpected dialog state".to_string();
message = format!("Dialog Button {} selected", index);
app_state.hide_dialog(); // Example action
} }
} }
return Ok(EventOutcome::Ok(message)); // Return Ok with message return Ok(EventOutcome::Ok(message)); // Return Ok with message