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 2 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
4 changes: 4 additions & 0 deletions component-generated/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ impl App {
let Some(keymap) = self.config.keybindings.get(&self.mode) else {
return Ok(());
};
if self.components.iter().any(|c| c.is_editing()) {
action_tx.send(Action::RawKeyEvent(key))?;
return Ok(());
}
kdheepak marked this conversation as resolved.
Show resolved Hide resolved
match keymap.get(&vec![key]) {
Some(action) => {
info!("Got action: {action:?}");
Expand Down
7 changes: 7 additions & 0 deletions component-generated/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ 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
}
/// 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
4 changes: 4 additions & 0 deletions component/template/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ impl App {
let Some(keymap) = self.config.keybindings.get(&self.mode) else {
return Ok(());
};
if self.components.iter().any(|c| c.is_editing()) {
action_tx.send(Action::RawKeyEvent(key))?;
return Ok(());
}
match keymap.get(&vec![key]) {
Some(action) => {
info!("Got action: {action:?}");
Expand Down
7 changes: 7 additions & 0 deletions component/template/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ 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
}
/// Handle incoming events and produce actions if necessary.
///
/// # Arguments
Expand Down