diff --git a/client/src/modes/canvas/edit.rs b/client/src/modes/canvas/edit.rs index fa79376..2bc7c9d 100644 --- a/client/src/modes/canvas/edit.rs +++ b/client/src/modes/canvas/edit.rs @@ -57,7 +57,9 @@ pub async fn handle_edit_event( EventOutcome::Ok(msg) => msg, EventOutcome::Exit(msg) => format!("Exit requested: {}", msg), // Or handle differently EventOutcome::DataSaved(save_outcome, msg) => format!("Data saved ({:?}): {}", save_outcome, msg), - // Add other EventOutcome variants if necessary + EventOutcome::ButtonSelected { context, index } => { + "Unexpected action in edit mode".to_string() + } }) }?; return Ok(message); diff --git a/client/src/modes/general/navigation.rs b/client/src/modes/general/navigation.rs index 528671d..51dc1b7 100644 --- a/client/src/modes/general/navigation.rs +++ b/client/src/modes/general/navigation.rs @@ -6,7 +6,7 @@ use crate::state::state::AppState; use crate::state::pages::form::FormState; use crate::state::pages::auth::AuthState; use crate::state::canvas_state::CanvasState; -use crate::tui::functions::{intro, admin}; +use crate::ui::handlers::context::UiContext; use crate::modes::handlers::event::EventOutcome; pub async fn handle_navigation_event( @@ -37,10 +37,6 @@ pub async fn handle_navigation_event( previous_option(app_state); return Ok(EventOutcome::Ok(String::new())); } - "select" => { - select(app_state); - return Ok(EventOutcome::Ok("Selected".to_string())); - } "toggle_sidebar" => { toggle_sidebar(app_state); return Ok(EventOutcome::Ok(format!("Sidebar {}", @@ -59,6 +55,21 @@ pub async fn handle_navigation_event( handle_enter_command_mode(command_mode, command_input, command_message); return Ok(EventOutcome::Ok(String::new())); } + "select" => { + let (context, index) = if app_state.ui.show_intro { + (UiContext::Intro, app_state.ui.intro_state.selected_option) + } else if app_state.ui.show_login && app_state.ui.focus_outside_canvas { + (UiContext::Login, app_state.general.selected_item) + } else if app_state.ui.show_admin { + (UiContext::Admin, app_state.general.selected_item) + } else if app_state.ui.dialog.dialog_show { + (UiContext::Dialog, app_state.ui.dialog.dialog_active_button_index) + } else { + // Handle cases where select is pressed but no button context applies + return Ok(EventOutcome::Ok("Select (No Action)".to_string())); + }; + return Ok(EventOutcome::ButtonSelected { context, index }); + } _ => {} } } @@ -134,14 +145,6 @@ pub fn previous_option(app_state: &mut AppState) { } } -pub fn select(app_state: &mut AppState) { - if app_state.ui.show_intro { - intro::handle_intro_selection(app_state); - } else if app_state.ui.show_admin { - admin::handle_admin_selection(app_state); - } -} - pub fn toggle_sidebar(app_state: &mut AppState) { app_state.ui.show_sidebar = !app_state.ui.show_sidebar; } diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index d1b2790..461042c 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -10,6 +10,9 @@ use crate::state::pages::form::FormState; use crate::state::pages::auth::AuthState; use crate::state::canvas_state::CanvasState; use crate::ui::handlers::rat_state::UiStateHandler; +use crate::ui::handlers::context::UiContext; +use crate::tui::functions::{intro, admin}; + use crate::tui::functions::common::login; use crate::modes::{ common::command_mode, canvas::{edit, read_only, common_mode}, @@ -21,10 +24,10 @@ use crate::tui::functions::common::form::SaveOutcome; #[derive(Debug, Clone, PartialEq, Eq)] pub enum EventOutcome { - Ok(String), // Normal operation, display message - Exit(String), // Signal app exit, display message - DataSaved(SaveOutcome, String), // Data save attempted, include outcome and message - // Add other outcomes like QuitRequested, SaveAndQuitRequested later if needed + Ok(String), + Exit(String), + DataSaved(SaveOutcome, String), + ButtonSelected { context: UiContext, index: usize }, } pub struct EventHandler { @@ -81,7 +84,7 @@ impl EventHandler { match current_mode { AppMode::General => { - return navigation::handle_navigation_event( + let nav_outcome = navigation::handle_navigation_event( key, config, form_state, @@ -91,6 +94,42 @@ impl EventHandler { &mut self.command_input, &mut self.command_message, ).await; + match nav_outcome { + Ok(EventOutcome::ButtonSelected { context, index }) => { + let mut message = String::from("Selected"); // Default message + match context { + UiContext::Intro => { + intro::handle_intro_selection(app_state, index); // Pass index + message = format!("Intro Option {} selected", index); + } + UiContext::Login => { + message = match index { + 0 => login::save(auth_state, &mut self.auth_client, app_state).await?, + 1 => { + let msg = login::revert(auth_state, app_state).await; + // Optional: Add navigation logic here if revert should change screen + // app_state.ui.show_login = false; + // app_state.ui.show_intro = true; + msg + } + _ => "Invalid Login Option".to_string(), + }; + } + UiContext::Admin => { + // Assuming handle_admin_selection uses app_state.general.selected_item + admin::handle_admin_selection(app_state); + message = format!("Admin Option {} selected", index); + } + UiContext::Dialog => { + // Add specific dialog handling logic here + message = format!("Dialog Button {} selected", index); + app_state.hide_dialog(); // Example action + } + } + return Ok(EventOutcome::Ok(message)); // Return Ok with message + } + other => return other, // Pass through Ok, Err, DataSaved directly + } }, AppMode::ReadOnly => { diff --git a/client/src/tui/functions/intro.rs b/client/src/tui/functions/intro.rs index 923337d..4340ed2 100644 --- a/client/src/tui/functions/intro.rs +++ b/client/src/tui/functions/intro.rs @@ -1,7 +1,7 @@ use crate::state::state::AppState; -pub fn handle_intro_selection(app_state: &mut AppState) { - match app_state.ui.intro_state.selected_option { +pub fn handle_intro_selection(app_state: &mut AppState, index: usize) { // Add index parameter + match index { // Use index directly 0 => { // Continue app_state.ui.show_form = true; app_state.ui.show_admin = false; @@ -21,3 +21,4 @@ pub fn handle_intro_selection(app_state: &mut AppState) { } app_state.ui.show_intro = false; } + diff --git a/client/src/ui/handlers.rs b/client/src/ui/handlers.rs index e0b8cbe..20abda9 100644 --- a/client/src/ui/handlers.rs +++ b/client/src/ui/handlers.rs @@ -3,6 +3,8 @@ pub mod ui; pub mod render; pub mod rat_state; +pub mod context; pub use ui::run_ui; pub use rat_state::*; +pub use context::*; diff --git a/client/src/ui/handlers/context.rs b/client/src/ui/handlers/context.rs new file mode 100644 index 0000000..9607e10 --- /dev/null +++ b/client/src/ui/handlers/context.rs @@ -0,0 +1,8 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum UiContext { + Intro, + Login, + Admin, + Dialog, +} + diff --git a/client/src/ui/handlers/ui.rs b/client/src/ui/handlers/ui.rs index 432f400..c96832a 100644 --- a/client/src/ui/handlers/ui.rs +++ b/client/src/ui/handlers/ui.rs @@ -152,6 +152,9 @@ pub async fn run_ui() -> Result<(), Box> { } // No count update needed for UpdatedExisting or NoChange } + EventOutcome::ButtonSelected { context, index } => { + event_handler.command_message = "Internal error: Unexpected button state".to_string(); + } }, Err(e) => { // Handle errors from handle_event, e.g., log or display