gamechanging, commands works only on their windows properly well
This commit is contained in:
@@ -1,6 +1,13 @@
|
|||||||
# config.toml
|
# config.toml
|
||||||
[keybindings]
|
[keybindings]
|
||||||
|
|
||||||
|
# [keybindings.global]
|
||||||
|
|
||||||
|
[keybindings.intro]
|
||||||
|
next_option = ["j", "Right"]
|
||||||
|
previous_option = ["k", "Left"]
|
||||||
|
select = ["Enter"]
|
||||||
|
|
||||||
[keybindings.common]
|
[keybindings.common]
|
||||||
save = ["ctrl+s"]
|
save = ["ctrl+s"]
|
||||||
quit = ["ctrl+q"]
|
quit = ["ctrl+q"]
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// client/src/config/config.rs
|
// src/config/binds/config.rs
|
||||||
|
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
@@ -25,6 +25,8 @@ pub struct Config {
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct ModeKeybindings {
|
pub struct ModeKeybindings {
|
||||||
|
#[serde(default)]
|
||||||
|
pub intro: 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)]
|
||||||
@@ -33,7 +35,6 @@ pub struct ModeKeybindings {
|
|||||||
pub command: HashMap<String, Vec<String>>,
|
pub command: HashMap<String, Vec<String>>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub common: HashMap<String, Vec<String>>,
|
pub common: HashMap<String, Vec<String>>,
|
||||||
// Store top-level keybindings that aren't in a specific mode section
|
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
pub global: HashMap<String, Vec<String>>,
|
pub global: HashMap<String, Vec<String>>,
|
||||||
}
|
}
|
||||||
@@ -49,6 +50,18 @@ 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> {
|
||||||
|
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 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.common, 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)
|
||||||
@@ -70,6 +83,25 @@ impl Config {
|
|||||||
.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))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Context-aware keybinding resolution
|
||||||
|
pub fn get_action_for_current_context(
|
||||||
|
&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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Helper function to get an action for a key in a specific mode.
|
/// Helper function to get an action for a key in a specific mode.
|
||||||
pub fn get_action_for_key_in_mode<'a>(
|
pub fn get_action_for_key_in_mode<'a>(
|
||||||
&self,
|
&self,
|
||||||
|
|||||||
@@ -51,19 +51,16 @@ impl EventHandler {
|
|||||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||||
if app_state.ui.show_intro {
|
if app_state.ui.show_intro {
|
||||||
if let Event::Key(key) = event {
|
if let Event::Key(key) = event {
|
||||||
match key.code {
|
let action = config.get_intro_action(key.code, key.modifiers);
|
||||||
KeyCode::Left => intro_state.previous_option(),
|
|
||||||
KeyCode::Right => intro_state.next_option(),
|
match action {
|
||||||
KeyCode::Enter => {
|
Some("previous_option") => intro_state.previous_option(),
|
||||||
if intro_state.selected_option == 0 {
|
Some("next_option") => intro_state.next_option(),
|
||||||
app_state.ui.show_intro = false;
|
Some("select") => {
|
||||||
} else {
|
app_state.ui.show_intro = false;
|
||||||
app_state.ui.show_intro = false;
|
app_state.ui.show_admin = intro_state.selected_option == 1;
|
||||||
app_state.ui.show_admin = true;
|
},
|
||||||
}
|
_ => {} // Ignore all other keys
|
||||||
return Ok((false, String::new()));
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok((false, String::new()));
|
return Ok((false, String::new()));
|
||||||
|
|||||||
Reference in New Issue
Block a user