separate page

This commit is contained in:
filipriec
2025-08-31 21:25:43 +02:00
parent 4e041f36ce
commit 5b42da8290
4 changed files with 76 additions and 48 deletions

View File

@@ -385,8 +385,8 @@ impl EventHandler {
key_event, key_event,
config, config,
app_state, app_state,
admin_state,
buffer_state, buffer_state,
router,
&mut self.command_message, &mut self.command_message,
)? { )? {
return Ok(EventOutcome::Ok(self.command_message.clone())); return Ok(EventOutcome::Ok(self.command_message.clone()));

View File

@@ -4,9 +4,9 @@ use crossterm::event::KeyEvent;
use crate::buffer::state::BufferState; use crate::buffer::state::BufferState;
use crate::config::binds::config::Config; use crate::config::binds::config::Config;
use crate::pages::admin::AdminState;
use crate::pages::admin::main::logic::handle_admin_navigation; use crate::pages::admin::main::logic::handle_admin_navigation;
use crate::state::app::state::AppState; use crate::state::app::state::AppState;
use crate::pages::routing::{Router, Page};
/// Handle all Admin page-specific key events (movement + actions). /// Handle all Admin page-specific key events (movement + actions).
/// Returns true if the key was handled (so the caller should stop propagation). /// Returns true if the key was handled (so the caller should stop propagation).
@@ -14,46 +14,51 @@ pub fn handle_admin_event(
key_event: KeyEvent, key_event: KeyEvent,
config: &Config, config: &Config,
app_state: &mut AppState, app_state: &mut AppState,
admin_state: &mut AdminState,
buffer_state: &mut BufferState, buffer_state: &mut BufferState,
router: &mut Router,
command_message: &mut String, command_message: &mut String,
) -> Result<bool> { ) -> Result<bool> {
// 1) Map general action to MovementAction (same mapping used in event.rs) if let Page::Admin(admin_state) = &mut router.current {
let movement_action = if let Some(act) = // 1) Map general action to MovementAction (same mapping used in event.rs)
config.get_general_action(key_event.code, key_event.modifiers) let movement_action = if let Some(act) =
{ config.get_general_action(key_event.code, key_event.modifiers)
use crate::movement::MovementAction; {
match act { use crate::movement::MovementAction;
"up" => Some(MovementAction::Up), match act {
"down" => Some(MovementAction::Down), "up" => Some(MovementAction::Up),
"left" => Some(MovementAction::Left), "down" => Some(MovementAction::Down),
"right" => Some(MovementAction::Right), "left" => Some(MovementAction::Left),
"next" => Some(MovementAction::Next), "right" => Some(MovementAction::Right),
"previous" => Some(MovementAction::Previous), "next" => Some(MovementAction::Next),
"select" => Some(MovementAction::Select), "previous" => Some(MovementAction::Previous),
"esc" => Some(MovementAction::Esc), "select" => Some(MovementAction::Select),
_ => None, "esc" => Some(MovementAction::Esc),
} _ => None,
} else { }
None } else {
}; None
};
if let Some(ma) = movement_action { if let Some(ma) = movement_action {
if admin_state.handle_movement(app_state, ma) { if admin_state.handle_movement(app_state, ma) {
return Ok(true);
}
}
// 2) Rich Admin navigation (buttons, selections, etc.)
if handle_admin_navigation(
key_event,
config,
app_state,
buffer_state,
router,
command_message,
) {
return Ok(true); return Ok(true);
} }
}
// 2) Rich Admin navigation (buttons, selections, etc.) // If we reached here, nothing was handled
if handle_admin_navigation( return Ok(false);
key_event,
config,
app_state,
admin_state,
buffer_state,
command_message,
) {
return Ok(true);
} }
Ok(false) Ok(false)

View File

@@ -5,7 +5,8 @@ use crate::config::binds::config::Config;
use crate::buffer::state::{BufferState, AppView}; use crate::buffer::state::{BufferState, AppView};
use crate::pages::admin_panel::add_table::state::{AddTableState, LinkDefinition}; use crate::pages::admin_panel::add_table::state::{AddTableState, LinkDefinition};
use ratatui::widgets::ListState; use ratatui::widgets::ListState;
use crate::pages::admin_panel::add_logic::state::{AddLogicState, AddLogicFocus}; use crate::pages::admin_panel::add_logic::state::{AddLogicState, AddLogicFocus, AddLogicFormState};
use crate::pages::routing::{Page, Router};
// Helper functions list_select_next and list_select_previous remain the same // Helper functions list_select_next and list_select_previous remain the same
fn list_select_next(list_state: &mut ListState, item_count: usize) { fn list_select_next(list_state: &mut ListState, item_count: usize) {
@@ -36,11 +37,15 @@ pub fn handle_admin_navigation(
key: crossterm::event::KeyEvent, key: crossterm::event::KeyEvent,
config: &Config, config: &Config,
app_state: &mut AppState, app_state: &mut AppState,
admin_state: &mut AdminState,
buffer_state: &mut BufferState, buffer_state: &mut BufferState,
router: &mut Router,
command_message: &mut String, command_message: &mut String,
) -> bool { ) -> bool {
let action = config.get_general_action(key.code, key.modifiers).map(String::from); let action = config.get_general_action(key.code, key.modifiers).map(String::from);
let Page::Admin(admin_state) = &mut router.current else {
return false;
};
let current_focus = admin_state.current_focus; let current_focus = admin_state.current_focus;
let profile_count = app_state.profile_tree.profiles.len(); let profile_count = app_state.profile_tree.profiles.len();
let mut handled = false; let mut handled = false;
@@ -230,20 +235,20 @@ pub fn handle_admin_navigation(
if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) { if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) {
if let Some(t_idx) = admin_state.selected_table_index { if let Some(t_idx) = admin_state.selected_table_index {
if let Some(table) = profile.tables.get(t_idx) { if let Some(table) = profile.tables.get(t_idx) {
// Both profile and table are selected, proceed // Create AddLogic page with selected profile & table
admin_state.add_logic_state = AddLogicState { let add_logic_form = AddLogicFormState::new_with_table(
profile_name: profile.name.clone(), &config.editor,
selected_table_name: Some(table.name.clone()), profile.name.clone(),
selected_table_id: Some(table.id), // If you have table IDs Some(table.id),
editor_keybinding_mode: config.editor.keybinding_mode.clone(), table.name.clone(),
current_focus: AddLogicFocus::default(), );
..AddLogicState::default()
}; // Route to AddLogic
router.current = Page::AddLogic(add_logic_form);
// Store table info for later fetching // Store table info for later fetching
app_state.pending_table_structure_fetch = Some(( app_state.pending_table_structure_fetch = Some((
profile.name.clone(), profile.name.clone(),
table.name.clone() table.name.clone(),
)); ));
buffer_state.update_history(AppView::AddLogic); buffer_state.update_history(AppView::AddLogic);

View File

@@ -358,6 +358,24 @@ impl AddLogicFormState {
} }
} }
pub fn new_with_table(
editor_config: &EditorConfig,
profile_name: String,
table_id: Option<i64>,
table_name: String,
) -> Self {
let mut state = AddLogicState::new(editor_config);
state.profile_name = profile_name;
state.selected_table_id = table_id;
state.selected_table_name = Some(table_name);
let editor = FormEditor::new(state.clone());
Self {
state,
editor,
focus_outside_canvas: false,
}
}
pub fn from_state(state: AddLogicState) -> Self { pub fn from_state(state: AddLogicState) -> Self {
let editor = FormEditor::new(state.clone()); let editor = FormEditor::new(state.clone());
Self { Self {