Compare commits

...

5 Commits

Author SHA1 Message Date
filipriec
355aff3032 working perfectly well right now 2025-03-24 15:13:57 +01:00
filipriec
3bb771187a admin bug fixed, time for intro also 2025-03-24 15:09:35 +01:00
filipriec
aa3ff18f9c now for common in canvas 2025-03-24 14:54:02 +01:00
filipriec
1fc9a0e1ff reorganization, preserved functionality 2025-03-24 14:29:28 +01:00
filipriec
cdac78c1bc needs to reset the render but it finally works 2025-03-24 14:17:44 +01:00
5 changed files with 230 additions and 154 deletions

View File

@@ -1,9 +1,76 @@
// src/modes/canvas/common.rs
use crossterm::event::{KeyEvent};
use crate::config::binds::config::Config;
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::tui::terminal::core::TerminalCore;
use crate::tui::terminal::commands::CommandHandler;
use crate::ui::handlers::form::FormState;
use crate::state::state::AppState;
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
/// Main handler for common core actions
pub async fn handle_core_action(
action: &str,
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
command_handler: &mut CommandHandler,
terminal: &mut TerminalCore,
app_state: &mut AppState,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
match action {
"save" => {
let message = save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
Ok((false, message))
},
"force_quit" => {
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
Ok((should_exit, message))
},
"save_and_quit" => {
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
Ok((should_exit, message))
},
"revert" => {
let message = revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
Ok((false, message))
},
// We should never hit this case with proper filtering
_ => Ok((false, format!("Core action not handled: {}", action))),
}
}
/// Helper function to check if a key event should trigger a core action
pub fn is_core_action(config: &Config, key_code: crossterm::event::KeyCode, modifiers: crossterm::event::KeyModifiers) -> Option<String> {
// Check for core application actions (save, quit, etc.)
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
modifiers
) {
match action {
"save" | "force_quit" | "save_and_quit" | "revert" => {
return Some(action.to_string())
},
_ => {} // Other actions are handled by their respective mode handlers
}
}
None
}
/// Shared logic for saving the current form state
pub async fn save(
form_state: &mut FormState,

View File

@@ -1,33 +1,147 @@
// src/modes/general/navigation.rs
use crossterm::event::KeyEvent;
use crate::config::binds::config::Config;
use crate::state::state::AppState;
use crate::ui::handlers::form::FormState;
pub async fn handle_navigation_event(
key: KeyEvent,
config: &Config,
form_state: &mut FormState,
app_state: &mut AppState,
command_mode: &mut bool,
command_input: &mut String,
command_message: &mut String,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
match action {
"move_up" => {
move_up(app_state);
return Ok((false, String::new()));
}
"move_down" => {
let item_count = if app_state.ui.show_intro {
2 // Intro options count
} else {
app_state.profile_tree.profiles.len() // Admin panel items
};
move_down(app_state, item_count);
return Ok((false, String::new()));
}
"next_option" => {
next_option(app_state, 2); // Intro has 2 options
return Ok((false, String::new()));
}
"previous_option" => {
previous_option(app_state);
return Ok((false, String::new()));
}
"select" => {
select(app_state);
return Ok((false, "Selected".to_string()));
}
"toggle_sidebar" => {
toggle_sidebar(app_state);
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
"next_field" => {
next_field(form_state);
return Ok((false, String::new()));
}
"prev_field" => {
prev_field(form_state);
return Ok((false, String::new()));
}
"enter_command_mode" => {
handle_enter_command_mode(command_mode, command_input, command_message);
return Ok((false, String::new()));
}
_ => {}
}
}
Ok((false, String::new()))
}
pub fn move_up(app_state: &mut AppState) {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
if app_state.ui.show_intro {
app_state.ui.intro_state.previous_option();
} else if app_state.ui.show_admin {
// Assuming profile_tree.profiles is the list we're navigating
let profile_count = app_state.profile_tree.profiles.len();
if profile_count == 0 {
return;
}
// Use general state for tracking selection in admin panel
if app_state.general.selected_item == 0 {
app_state.general.selected_item = profile_count - 1;
} else {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
}
}
}
pub fn move_down(app_state: &mut AppState, item_count: usize) {
app_state.general.selected_item = app_state.general.selected_item.saturating_add(1);
if item_count > 0 {
app_state.general.selected_item = app_state.general.selected_item.min(item_count - 1);
if app_state.ui.show_intro {
app_state.ui.intro_state.next_option();
} else if app_state.ui.show_admin {
// Assuming profile_tree.profiles is the list we're navigating
let profile_count = app_state.profile_tree.profiles.len();
if profile_count == 0 {
return;
}
app_state.general.selected_item = (app_state.general.selected_item + 1) % profile_count;
}
}
pub fn next_option(app_state: &mut AppState, option_count: usize) {
app_state.general.current_option = app_state.general.current_option.saturating_add(1);
if option_count > 0 {
app_state.general.current_option = app_state.general.current_option.min(option_count - 1);
if app_state.ui.show_intro {
app_state.ui.intro_state.next_option();
} else {
// For other screens that might have options
app_state.general.current_option = (app_state.general.current_option + 1) % option_count;
}
}
pub fn previous_option(app_state: &mut AppState) {
app_state.general.current_option = app_state.general.current_option.saturating_sub(1);
if app_state.ui.show_intro {
app_state.ui.intro_state.previous_option();
} else {
// For other screens that might have options
if app_state.general.current_option == 0 {
// We'd need the option count here, but since it's not passed we can't wrap around correctly
// For now, just stay at 0
} else {
app_state.general.current_option -= 1;
}
}
}
pub fn select(app_state: &mut AppState) {
app_state.ui.show_form = app_state.ui.intro_state.selected_option == 0;
app_state.ui.show_admin = app_state.ui.intro_state.selected_option == 1;
app_state.ui.show_intro = false;
if app_state.ui.show_intro {
// Handle selection in intro screen
if app_state.ui.intro_state.selected_option == 0 {
// First option selected - show form
app_state.ui.show_form = true;
app_state.ui.show_admin = false;
} else {
// Second option selected - show admin
app_state.ui.show_form = false;
app_state.ui.show_admin = true;
}
app_state.ui.show_intro = false;
} else if app_state.ui.show_admin {
// Handle selection in admin panel
let profiles = &app_state.profile_tree.profiles;
if !profiles.is_empty() && app_state.general.selected_item < profiles.len() {
// Set the selected profile
app_state.selected_profile = Some(profiles[app_state.general.selected_item].name.clone());
}
}
}
pub fn toggle_sidebar(app_state: &mut AppState) {
@@ -42,25 +156,20 @@ pub fn next_field(form_state: &mut FormState) {
pub fn prev_field(form_state: &mut FormState) {
if !form_state.fields.is_empty() {
form_state.current_field = if form_state.current_field == 0 {
form_state.fields.len() - 1
if form_state.current_field == 0 {
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field - 1
};
form_state.current_field -= 1;
}
}
}
pub fn handle_enter_command_mode(event_handler: &mut crate::modes::handlers::event::EventHandler) {
event_handler.command_mode = true;
event_handler.command_input.clear();
event_handler.command_message.clear();
}
// Helper function for bounds checking in lists
pub fn clamp_index(selected: usize, item_count: usize) -> usize {
if item_count == 0 {
0
} else {
selected.min(item_count - 1)
}
pub fn handle_enter_command_mode(
command_mode: &mut bool,
command_input: &mut String,
command_message: &mut String
) {
*command_mode = true;
command_input.clear();
command_message.clear();
}

View File

@@ -12,8 +12,8 @@ use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::{
common::{command_mode},
canvas::{edit, read_only, common},
general::navigation,
};
use crate::modes::navigation;
use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
@@ -70,12 +70,15 @@ impl EventHandler {
// Mode-specific handling
match current_mode {
AppMode::General => {
return self.handle_general_mode(
return navigation::handle_navigation_event(
key,
config,
form_state,
app_state,
);
&mut self.command_mode,
&mut self.command_input,
&mut self.command_message,
).await;
},
AppMode::ReadOnly => {
@@ -122,7 +125,7 @@ impl EventHandler {
) {
match action {
"save" | "force_quit" | "save_and_quit" | "revert" => {
return self.handle_core_action(
return common::handle_core_action(
action,
form_state,
grpc_client,
@@ -181,7 +184,7 @@ impl EventHandler {
) {
match action {
"save" | "force_quit" | "save_and_quit" | "revert" => {
return self.handle_core_action(
return common::handle_core_action(
action,
form_state,
grpc_client,
@@ -240,107 +243,5 @@ impl EventHandler {
Ok((false, self.command_message.clone()))
}
// Helper method for handling general mode actions
fn handle_general_mode(
&mut self,
key: KeyEvent,
config: &Config,
form_state: &mut FormState,
app_state: &mut crate::state::state::AppState,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
match action {
"move_up" => {
navigation::move_up(app_state);
return Ok((false, String::new()));
}
"move_down" => {
let item_count = if app_state.ui.show_intro {
2 // Intro options count
} else {
app_state.profile_tree.profiles.len() // Admin panel items
};
navigation::move_down(app_state, item_count);
return Ok((false, String::new()));
}
"next_option" => {
navigation::next_option(app_state, 2); // Intro has 2 options
return Ok((false, String::new()));
}
"previous_option" => {
navigation::previous_option(app_state);
return Ok((false, String::new()));
}
"select" => {
navigation::select(app_state);
return Ok((false, "Selected".to_string()));
}
"toggle_sidebar" => {
navigation::toggle_sidebar(app_state);
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
"next_field" => {
navigation::next_field(form_state);
return Ok((false, String::new()));
}
"prev_field" => {
navigation::prev_field(form_state);
return Ok((false, String::new()));
}
"enter_command_mode" => {
navigation::handle_enter_command_mode(self);
return Ok((false, String::new()));
}
_ => {}
}
}
Ok((false, String::new()))
}
// Helper method for handling core application actions (not navigation)
async fn handle_core_action(
&mut self,
action: &str,
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
command_handler: &mut CommandHandler,
terminal: &mut TerminalCore,
app_state: &mut crate::state::state::AppState,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
match action {
"save" => {
let message = common::save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
Ok((false, message))
},
"force_quit" => {
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
Ok((should_exit, message))
},
"save_and_quit" => {
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
Ok((should_exit, message))
},
"revert" => {
let message = common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
Ok((false, message))
},
// We should never hit this case given our filtering above
_ => Ok((false, format!("Core action not handled: {}", action))),
}
}
}

View File

@@ -26,7 +26,7 @@ pub fn render_ui(
command_mode: bool,
command_message: &str,
app_state: &AppState,
intro_state: &intro::IntroState,
// intro_state parameter removed
) {
render_background(f, f.area(), theme);
@@ -41,31 +41,31 @@ pub fn render_ui(
let main_content_area = root[0];
if app_state.ui.show_intro {
intro_state.render(f, main_content_area, theme);
// Use app_state's intro_state directly
app_state.ui.intro_state.render(f, main_content_area, theme);
} else if app_state.ui.show_admin {
// Create temporary AdminPanelState for rendering
let mut admin_state = AdminPanelState::new(
if app_state.profile_tree.profiles.is_empty() {
// Fallback if admin_profiles is empty
app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect()
} else {
app_state.profile_tree.profiles.iter().map(|p| p.name.clone()).collect()
}
app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect()
);
// Set the selected item
// Set the selected item - FIXED
if !admin_state.profiles.is_empty() {
app_state.general.selected_item.min(admin_state.profiles.len().saturating_sub(1));
let selected_index = std::cmp::min(
app_state.general.selected_item,
admin_state.profiles.len() - 1
);
admin_state.list_state.select(Some(selected_index));
}
admin_state.render(
f,
main_content_area,
theme,
&app_state.profile_tree,
&app_state.profile_tree,
&app_state.selected_profile,
);
} else if app_state.ui.show_form {
@@ -76,10 +76,10 @@ pub fn render_ui(
if let Some(sidebar_rect) = sidebar_area {
sidebar::render_sidebar(
f,
sidebar_rect,
theme,
&app_state.profile_tree,
f,
sidebar_rect,
theme,
&app_state.profile_tree,
&app_state.selected_profile
);
}

View File

@@ -74,7 +74,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
event_handler.command_mode,
&event_handler.command_message,
&app_state,
&intro_state,
);
})?;