Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(component): Add a mean for components to access raw key events #81

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions component-generated/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crossterm::event::KeyEvent;
use serde::{Deserialize, Serialize};
use strum::Display;

Expand All @@ -8,6 +9,7 @@ pub enum Action {
Resize(u16, u16),
Suspend,
Resume,
RawKeyEvent(KeyEvent),
Quit,
ClearScreen,
Error(String),
Expand Down
41 changes: 28 additions & 13 deletions component-generated/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,36 @@ impl App {
let Some(keymap) = self.config.keybindings.get(&self.mode) else {
return Ok(());
};
match keymap.get(&vec![key]) {
Some(action) => {
info!("Got action: {action:?}");
action_tx.send(action.clone())?;
if let Some(action) = keymap.get(&vec![key]) {
info!("Got action: {action:?}");
// Look for components in editing mode
// and that should receive a raw key event instead of the action.
for component in &self.components {
// Is it in editing mode and is the action not in the escape list ?
if component.is_editing() && !component.escape_editing_mode().contains(action) {
info!("Action was sent as raw key");
action_tx.send(Action::RawKeyEvent(key))?;
return Ok(());
}
}
// Send the actual action to components
action_tx.send(action.clone())?;
} else {
// If there is a component in editing mode, send the raw key
if self.components.iter().any(|c| c.is_editing()) {
info!("Got raw key: {key:?}");
action_tx.send(Action::RawKeyEvent(key))?;
return Ok(());
}
_ => {
// If the key was not handled as a single key action,
// then consider it for multi-key combinations.
self.last_tick_key_events.push(key);

// Check for multi-key combinations
if let Some(action) = keymap.get(&self.last_tick_key_events) {
info!("Got action: {action:?}");
action_tx.send(action.clone())?;
}
// If the key was not handled as a single key action,
// then consider it for multi-key combinations.
self.last_tick_key_events.push(key);

// Check for multi-key combinations
if let Some(action) = keymap.get(&self.last_tick_key_events) {
info!("Got action: {action:?}");
action_tx.send(action.clone())?;
}
}
Ok(())
Expand Down
14 changes: 14 additions & 0 deletions component-generated/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ pub trait Component {
let _ = area; // to appease clippy
Ok(())
}
/// Whether the app should send `Action::RawKeyEvent` or the corresponding `Action` variant.
///
/// # Returns
/// * `bool` - Whether the component is waiting only for raw key events actions or not.
fn is_editing(&self) -> bool {
false
}
/// Returns zero or more `Action` that should never be sent as `Action::RawKeyEvent` to this component even if `Self::is_editing` returns `true`.
///
/// # Returns
/// * `Vec<Action>` - A list of `Action` that should never be sent as `Action::RawKeyEvent`.
fn escape_editing_mode(&self) -> Vec<Action> {
vec![]
}
/// Handle incoming events and produce actions if necessary.
///
/// # Arguments
Expand Down
2 changes: 2 additions & 0 deletions component/template/src/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crossterm::event::KeyEvent;
use serde::{Deserialize, Serialize};
use strum::Display;

Expand All @@ -8,6 +9,7 @@ pub enum Action {
Resize(u16, u16),
Suspend,
Resume,
RawKeyEvent(KeyEvent),
Quit,
ClearScreen,
Error(String),
Expand Down
41 changes: 28 additions & 13 deletions component/template/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,36 @@ impl App {
let Some(keymap) = self.config.keybindings.get(&self.mode) else {
return Ok(());
};
match keymap.get(&vec![key]) {
Some(action) => {
info!("Got action: {action:?}");
action_tx.send(action.clone())?;
if let Some(action) = keymap.get(&vec![key]) {
info!("Got action: {action:?}");
// Look for components in editing mode
// and that should receive a raw key event instead of the action.
for component in &self.components {
// Is it in editing mode and is the action not in the escape list ?
if component.is_editing() && !component.escape_editing_mode().contains(action) {
info!("Action was sent as raw key");
action_tx.send(Action::RawKeyEvent(key))?;
return Ok(());
}
}
// Send the actual action to components
action_tx.send(action.clone())?;
} else {
// If there is a component in editing mode, send the raw key
if self.components.iter().any(|c| c.is_editing()) {
info!("Got raw key: {key:?}");
action_tx.send(Action::RawKeyEvent(key))?;
return Ok(());
}
_ => {
// If the key was not handled as a single key action,
// then consider it for multi-key combinations.
self.last_tick_key_events.push(key);

// Check for multi-key combinations
if let Some(action) = keymap.get(&self.last_tick_key_events) {
info!("Got action: {action:?}");
action_tx.send(action.clone())?;
}
// If the key was not handled as a single key action,
// then consider it for multi-key combinations.
self.last_tick_key_events.push(key);

// Check for multi-key combinations
if let Some(action) = keymap.get(&self.last_tick_key_events) {
info!("Got action: {action:?}");
action_tx.send(action.clone())?;
}
}
Ok(())
Expand Down
14 changes: 14 additions & 0 deletions component/template/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ pub trait Component {
let _ = area; // to appease clippy
Ok(())
}
/// Whether the app should send `Action::RawKeyEvent` or the corresponding `Action` variant.
///
/// # Returns
/// * `bool` - Whether the component is waiting only for raw key events actions or not.
fn is_editing(&self) -> bool {
false
}
/// Returns zero or more `Action` that should never be sent as `Action::RawKeyEvent` to this component even if `Self::is_editing` returns `true`.
///
/// # Returns
/// * `Vec<Action>` - A list of `Action` that should never be sent as `Action::RawKeyEvent`.
fn escape_editing_mode(&self) -> Vec<Action> {
vec![]
}
/// Handle incoming events and produce actions if necessary.
///
/// # Arguments
Expand Down