diff --git a/src/widgets/resources.rs b/src/widgets/resources.rs index afecb7f..84f4b8d 100644 --- a/src/widgets/resources.rs +++ b/src/widgets/resources.rs @@ -1,4 +1,7 @@ use bevy::prelude::*; +/// Terminal widget entity currently focused and handling input +/// Can be manipulated directly or you can request an entity be focused through +/// the `focus_widget` command. #[derive(Resource, Default, Deref, DerefMut, Debug)] pub struct FocusedWidget(pub Option); diff --git a/src/widgets/systems.rs b/src/widgets/systems.rs index 0638b01..c911b0b 100644 --- a/src/widgets/systems.rs +++ b/src/widgets/systems.rs @@ -2,17 +2,22 @@ use bevy::prelude::*; use crate::input::events::TerminalInputEvent; -use super::components::Widget; +use super::{components::Widget, resources::FocusedWidget}; -/// Invokes every enabled widget's `handle_events` methods for each incoming input event +/// Invokes focused widget's `handle_events` methods for each incoming input event pub fn widget_input_handling( mut widgets: Query<&mut Widget>, mut event_reader: EventReader, mut commands: Commands, + focused_widget: Res, ) { - for event in event_reader.read() { - for mut widget in widgets.iter_mut().filter(|widget| widget.enabled) { - widget.widget.handle_events(event, &mut commands); + if let Some(entity) = **focused_widget { + if let Ok(mut widget) = widgets.get_mut(entity) { + if widget.enabled == true { + for event in event_reader.read() { + widget.widget.handle_events(event, &mut commands); + } + } } } }