[tui] Add basic support for focusing widgets.

It's pretty bad but it allows to control which widget accepts input.
This commit is contained in:
2026-01-22 19:23:21 -05:00
parent a4413cd052
commit 0c87fda795
7 changed files with 143 additions and 30 deletions

View File

@@ -1,5 +1,5 @@
use crate::tui::app::{AppComponents, ComponentOf};
use crate::tui::component::{Action, Component};
use crate::tui::component::{Action, Component, ComponentState, Focus};
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
use ratatui::layout::Rect;
@@ -11,6 +11,7 @@ use tui_logger::{TuiLoggerLevelOutput, TuiLoggerSmartWidget, TuiWidgetEvent, Tui
/// The logger is bound to info!, debug!, error!, trace! macros within Tui::new().
pub struct Logger {
state: TuiWidgetState,
pub(crate) component_state: ComponentState,
}
impl<'a> ComponentOf<Logger> for AppComponents<'a> {
@@ -32,6 +33,7 @@ impl Logger {
pub fn new() -> Self {
Self {
state: TuiWidgetState::new(),
component_state: Default::default(),
}
}
}
@@ -64,6 +66,10 @@ impl Component for Logger {
"Logger"
}
fn is_active(&self) -> bool {
self.component_state.focus == Focus::Active
}
fn handle_event(&mut self, event: Event) -> anyhow::Result<Action> {
if let Some(key_event) = event.as_key_event() {
return self.handle_key_events(key_event);