Compare commits

...

4 Commits

Author SHA1 Message Date
filipriec
9917195fc4 disabling modes where they shouldnt be enabled BIG UPDATE 2025-03-23 20:07:47 +01:00
filipriec
fbcea1b270 trying to make the intro and admin with general keybindings 2025-03-23 19:17:42 +01:00
filipriec
87b07db26a completely broken intro or admin 2025-03-23 15:35:57 +01:00
filipriec
4481560025 HUGE CHANGES TO MODESA 2025-03-23 15:11:43 +01:00
9 changed files with 136 additions and 93 deletions

View File

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

View File

@@ -26,9 +26,7 @@ pub struct Config {
#[derive(Debug, Deserialize)]
pub struct ModeKeybindings {
#[serde(default)]
pub intro: HashMap<String, Vec<String>>,
#[serde(default)]
pub admin: HashMap<String, Vec<String>>,
pub general: HashMap<String, Vec<String>>,
#[serde(default)]
pub read_only: HashMap<String, Vec<String>>,
#[serde(default)]
@@ -52,19 +50,16 @@ impl 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> {
self.get_action_for_key_in_mode(&self.keybindings.intro, key, modifiers)
pub fn get_general_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
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))
}
/// 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))
}
/// Common actions for Edit/Read-only modes
pub fn get_common_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.common, key, modifiers)
}
/// 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> {
@@ -92,17 +87,17 @@ impl Config {
&self,
is_edit_mode: bool,
command_mode: bool,
show_intro: bool,
show_admin: bool,
key: KeyCode,
modifiers: KeyModifiers
) -> Option<&str> {
match (show_intro, show_admin, command_mode, is_edit_mode) {
(true, _, _, _) => self.get_intro_action(key, modifiers),
(_, true, _, _) => self.get_admin_action(key, modifiers),
(_, _, true, _) => self.get_command_action_for_key(key, modifiers),
(_, _, _, true) => self.get_edit_action_for_key(key, modifiers),
_ => self.get_read_only_action_for_key(key, modifiers),
match (command_mode, is_edit_mode) {
(true, _) => self.get_command_action_for_key(key, modifiers),
(_, true) => self.get_edit_action_for_key(key, modifiers)
.or_else(|| self.get_common_action(key, modifiers)),
_ => self.get_read_only_action_for_key(key, modifiers)
.or_else(|| self.get_common_action(key, modifiers))
// Add global bindings check for read-only mode
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers)),
}
}

View File

@@ -0,0 +1,2 @@
// src/client/modes/general.rs
pub mod navigation;

View File

View File

@@ -49,57 +49,98 @@ impl EventHandler {
app_state: &mut crate::state::state::AppState,
total_count: u64,
current_position: &mut u64,
intro_state: &mut crate::components::intro::intro::IntroState,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if app_state.ui.show_intro {
if let Event::Key(key) = event {
let action = config.get_intro_action(key.code, key.modifiers);
if let Event::Key(key) = event {
let key_code = key.code;
let modifiers = key.modifiers;
match action {
Some("previous_option") => intro_state.previous_option(),
Some("next_option") => intro_state.next_option(),
Some("select") => {
app_state.ui.show_intro = false;
app_state.ui.show_admin = intro_state.selected_option == 1;
},
_ => {} // 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);
}
_ => {}
// Handle general mode (replaces intro and admin)
if app_state.ui.show_intro || app_state.ui.show_admin {
if let Some(action) = config.get_general_action(key_code, modifiers) {
match action {
"move_up" => {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
return Ok((false, String::new()));
}
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" => {
if app_state.ui.show_intro {
app_state.ui.show_intro = false;
} else if app_state.ui.show_admin {
app_state.ui.show_admin = 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()));
}
_ => {}
}
}
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
&config.keybindings.global,
key_code,
modifiers
) {
self.command_mode = true;
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
if self.is_edit_mode {
@@ -141,17 +182,18 @@ impl EventHandler {
let context_action = config.get_action_for_current_context(
self.is_edit_mode,
self.command_mode,
app_state.ui.show_intro,
app_state.ui.show_admin,
key_code,
modifiers
);
// Block command mode entry from edit mode
if let Some("enter_command_mode") = context_action {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
if !self.is_edit_mode {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
}
if let Some(action) = config.get_action_for_key_in_mode(

View File

@@ -1,6 +1,8 @@
// src/client/modes/mod.rs
pub mod handlers;
pub mod canvas;
pub mod general;
pub use handlers::*;
pub use canvas::*;
pub use general::*;

View File

@@ -10,6 +10,11 @@ pub struct UiState {
pub show_admin: bool,
}
pub struct GeneralState {
pub selected_item: usize,
pub current_option: usize,
}
pub struct AppState {
// Core editor state
pub current_dir: String,
@@ -17,11 +22,10 @@ pub struct AppState {
pub current_position: u64,
pub profile_tree: ProfileTreeResponse,
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
pub ui: UiState,
pub general: GeneralState,
}
impl AppState {
@@ -35,9 +39,11 @@ impl AppState {
current_position: 0,
profile_tree: ProfileTreeResponse::default(),
selected_profile: None,
admin_selected_item: 0,
admin_profiles: Vec::new(),
ui: UiState::default(),
general: GeneralState {
selected_item: 0,
current_option: 0,
},
})
}

View File

@@ -45,21 +45,20 @@ pub fn render_ui(
} else if app_state.ui.show_admin {
// Create temporary AdminPanelState for rendering
let mut admin_state = AdminPanelState::new(
if app_state.admin_profiles.is_empty() {
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.admin_profiles.clone()
app_state.profile_tree.profiles.iter().map(|p| p.name.clone()).collect()
}
);
// Set the selected item
if !admin_state.profiles.is_empty() {
let safe_index = app_state.admin_selected_item.min(admin_state.profiles.len() - 1);
admin_state.list_state.select(Some(safe_index));
app_state.general.selected_item.min(admin_state.profiles.len().saturating_sub(1));
}
admin_state.render(

View File

@@ -30,11 +30,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
// Now create admin panel with profiles from app_state
if intro_state.selected_option == 1 {
app_state.ui.show_admin = true;
app_state.admin_profiles = app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect();
app_state.admin_selected_item = 0;
app_state.general.selected_item = 0;
app_state.general.current_option = 0;
}
// Fetch table structure at startup (one-time)
@@ -95,7 +92,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&mut app_state,
total_count,
&mut current_position,
&mut intro_state,
).await?;
app_state.current_position = current_position;