HUGE CHANGES TO MODESA

This commit is contained in:
filipriec
2025-03-23 15:11:43 +01:00
parent d1d33b5752
commit 4481560025
4 changed files with 93 additions and 72 deletions

View File

@@ -1,16 +1,16 @@
# config.toml # config.toml
[keybindings] [keybindings]
[keybindings.general]
enter_command_mode = [":", "ctrl+;"] enter_command_mode = [":", "ctrl+;"]
move_up = ["k", "Up"]
[keybindings.intro] move_down = ["j", "Down"]
next_option = ["j", "Right"] next_option = ["l", "Right"]
previous_option = ["k", "Left"] previous_option = ["h", "Left"]
select = ["Enter"] select = ["Enter"]
toggle_sidebar = ["ctrl+t"]
[keybindings.admin] next_field = ["Tab"]
move_up = ["k"] prev_field = ["Shift+Tab"]
move_down = ["j"]
[keybindings.common] [keybindings.common]
save = ["ctrl+s"] save = ["ctrl+s"]

View File

@@ -26,9 +26,7 @@ pub struct Config {
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct ModeKeybindings { pub struct ModeKeybindings {
#[serde(default)] #[serde(default)]
pub intro: HashMap<String, Vec<String>>, pub general: HashMap<String, Vec<String>>,
#[serde(default)]
pub admin: HashMap<String, Vec<String>>,
#[serde(default)] #[serde(default)]
pub read_only: HashMap<String, Vec<String>>, pub read_only: HashMap<String, Vec<String>>,
#[serde(default)] #[serde(default)]
@@ -52,20 +50,12 @@ impl Config {
Ok(config) Ok(config)
} }
/// Gets an action for a key in Intro mode, only checking intro and global bindings
pub fn get_intro_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> { pub fn get_general_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.intro, key, modifiers) self.get_action_for_key_in_mode(&self.keybindings.general, key, modifiers)
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers)) .or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
} }
/// Gets an action for a key in Admin mode, checking common and global bindings
pub fn get_admin_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.admin, key, modifiers)
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.common, key, modifiers))
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.intro, key, modifiers))
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Gets an action for a key in Read-Only mode, also checking common keybindings. /// Gets an action for a key in Read-Only mode, also checking common keybindings.
pub fn get_read_only_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> { pub fn get_read_only_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.read_only, key, modifiers) self.get_action_for_key_in_mode(&self.keybindings.read_only, key, modifiers)

View File

@@ -49,57 +49,86 @@ impl EventHandler {
app_state: &mut crate::state::state::AppState, app_state: &mut crate::state::state::AppState,
total_count: u64, total_count: u64,
current_position: &mut u64, current_position: &mut u64,
intro_state: &mut crate::components::intro::intro::IntroState,
) -> Result<(bool, String), Box<dyn std::error::Error>> { ) -> Result<(bool, String), Box<dyn std::error::Error>> {
if app_state.ui.show_intro { if let Event::Key(key) = event {
if let Event::Key(key) = event { let key_code = key.code;
let action = config.get_intro_action(key.code, key.modifiers); let modifiers = key.modifiers;
match action { // Handle general mode (replaces intro and admin)
Some("previous_option") => intro_state.previous_option(), if app_state.ui.show_general_mode {
Some("next_option") => intro_state.next_option(), if let Some(action) = config.get_general_action(key_code, modifiers) {
Some("select") => { match action {
app_state.ui.show_intro = false; "move_up" => {
app_state.ui.show_admin = intro_state.selected_option == 1; app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
}, return Ok((false, String::new()));
_ => {} // Ignore all other keys
}
}
return Ok((false, String::new()));
}
if let Event::Key(key) = event {
let key_code = key.code;
let modifiers = key.modifiers;
// Handle admin panel mode
if app_state.ui.show_admin {
if let Some(action) = config.get_admin_action(key_code, modifiers) {
match action {
"move_up" => {
// Handle up movement using app_state directly
app_state.admin_selected_item = app_state.admin_selected_item.saturating_sub(1);
}
"move_down" => {
// Handle down movement using app_state
app_state.admin_selected_item = app_state.admin_selected_item.saturating_add(1);
}
_ => {}
} }
return Ok((false, format!("Admin: {}", action))); "move_down" => {
app_state.general.selected_item = app_state.general.selected_item.saturating_add(1);
return Ok((false, String::new()));
}
"next_option" => {
app_state.general.current_option = app_state.general.current_option.saturating_add(1);
return Ok((false, String::new()));
}
"previous_option" => {
app_state.general.current_option = app_state.general.current_option.saturating_sub(1);
return Ok((false, String::new()));
}
"select" => {
// Handle selection based on current view
app_state.ui.show_general_mode = false;
return Ok((false, "Selected".to_string()));
}
"toggle_sidebar" => {
app_state.ui.show_sidebar = !app_state.ui.show_sidebar;
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
"next_field" => {
// Increment field navigation
if form_state.fields.len() > 0 {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
}
return Ok((false, String::new()));
}
"prev_field" => {
// Decrement field navigation
if form_state.fields.len() > 0 {
form_state.current_field = if form_state.current_field == 0 {
form_state.fields.len() - 1
} else {
form_state.current_field - 1
};
}
return Ok((false, String::new()));
}
"enter_command_mode" => {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
_ => {}
} }
return Ok((false, String::new()));
}
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
key_code,
modifiers,
) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
} }
// If no general action matched, return to stay in general mode
return Ok((false, String::new()));
}
// The rest of the function handles other modes as before
// Handle toggling sidebar which is common across modes
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
key_code,
modifiers,
) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
// Handle edit mode first to allow normal character input // Handle edit mode first to allow normal character input
if self.is_edit_mode { if self.is_edit_mode {

View File

@@ -10,6 +10,11 @@ pub struct UiState {
pub show_admin: bool, pub show_admin: bool,
} }
pub struct GeneralState {
pub selected_item: usize,
pub current_option: usize,
}
pub struct AppState { pub struct AppState {
// Core editor state // Core editor state
pub current_dir: String, pub current_dir: String,
@@ -17,11 +22,10 @@ pub struct AppState {
pub current_position: u64, pub current_position: u64,
pub profile_tree: ProfileTreeResponse, pub profile_tree: ProfileTreeResponse,
pub selected_profile: Option<String>, pub selected_profile: Option<String>,
pub admin_selected_item: usize, // Tracks selection in admin panel
pub admin_profiles: Vec<String>, // Stores admin panel data
// UI preferences // UI preferences
pub ui: UiState, pub ui: UiState,
pub general: GeneralState,
} }
impl AppState { impl AppState {
@@ -35,8 +39,6 @@ impl AppState {
current_position: 0, current_position: 0,
profile_tree: ProfileTreeResponse::default(), profile_tree: ProfileTreeResponse::default(),
selected_profile: None, selected_profile: None,
admin_selected_item: 0,
admin_profiles: Vec::new(),
ui: UiState::default(), ui: UiState::default(),
}) })
} }