From 9bcb260cca8d48b97aeec224d7249e6dc6fc9639 Mon Sep 17 00:00:00 2001 From: Silas Bartha Date: Thu, 27 Mar 2025 17:19:11 -0400 Subject: [PATCH] WIP - widget focus --- src/lib.rs | 17 ++++------------- src/widgets/commands.rs | 23 +++++++++++++++++++++++ src/widgets/mod.rs | 6 ++++++ src/widgets/resources.rs | 4 ++++ 4 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 src/widgets/commands.rs create mode 100644 src/widgets/resources.rs diff --git a/src/lib.rs b/src/lib.rs index cd6519f..fd7686c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! Bevy plugin which allows a camera to render to a terminal window. use std::{ - fs::OpenOptions, io::{stdout, Write}, path::PathBuf, sync::{Arc, Mutex} + fs::OpenOptions, io::{stdout, Write}, path::PathBuf, }; use bevy::{ @@ -18,7 +18,6 @@ use bevy_headless_render::HeadlessRenderPlugin; use color_eyre::config::HookBuilder; pub use crossterm; use crossterm::{event::{DisableMouseCapture, PopKeyboardEnhancementFlags}, terminal::{disable_raw_mode, LeaveAlternateScreen}, ExecutableCommand}; -use once_cell::sync::Lazy; pub use ratatui; /// Functions and types related to capture and display of world to terminal @@ -30,8 +29,6 @@ pub mod input; /// Functions and types related to constructing and rendering TUI widgets pub mod widgets; -static LOG_PATH: Lazy>> = Lazy::new(|| Arc::new(Mutex::new(PathBuf::default()))); - /// Plugin providing terminal display functionality pub struct TerminalDisplayPlugin { /// Path to redirect tracing logs to. Defaults to "debug.log" @@ -48,19 +45,12 @@ impl Default for TerminalDisplayPlugin { impl Plugin for TerminalDisplayPlugin { fn build(&self, app: &mut App) { - *LOG_PATH - .lock() - .expect("Failed to get lock on log path mutex") = self.log_path.clone(); + let log_path = self.log_path.clone(); let log_file = OpenOptions::new() .write(true) .create(true) .truncate(true) - .open( - LOG_PATH - .lock() - .expect("Failed to get lock on log path mutex") - .clone(), - ) + .open(log_path) .unwrap(); let file_layer = tracing_subscriber::fmt::Layer::new() .with_writer(log_file) @@ -100,6 +90,7 @@ impl Plugin for TerminalDisplayPlugin { ) .insert_resource(display::resources::Terminal::default()) .insert_resource(input::resources::EventQueue::default()) + .init_resource::() .add_event::(); } } diff --git a/src/widgets/commands.rs b/src/widgets/commands.rs new file mode 100644 index 0000000..e5ee614 --- /dev/null +++ b/src/widgets/commands.rs @@ -0,0 +1,23 @@ +use bevy::prelude::*; + +use super::resources::FocusedWidget; + +struct FocusWidgetCommand(Entity); + +impl Command for FocusWidgetCommand { + fn apply(self, world: &mut World) { + **world.resource_mut::() = Some(self.0); + } +} + +/// Command interface for manipulating terminal widget resources +pub trait TerminalWidgetCommands { + /// Gives focus to the terminal widget on the provided entity. + fn focus_widget(&mut self, widget: Entity); +} + +impl<'w, 's> TerminalWidgetCommands for Commands<'w,'s> { + fn focus_widget(&mut self, widget: Entity) { + self.queue(FocusWidgetCommand(widget)); + } +} \ No newline at end of file diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index 7751b2d..4435af8 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -7,9 +7,15 @@ use crate::input::events::TerminalInputEvent; /// Components for this module pub mod components; +/// Resources for this module +pub mod resources; + /// Systems for this module pub(crate) mod systems; +/// Commands for this module +pub mod commands; + /// Trait which defines an interface for terminal widgets pub trait TerminalWidget: DowncastSync { /// Called every frame to render the widget diff --git a/src/widgets/resources.rs b/src/widgets/resources.rs new file mode 100644 index 0000000..afecb7f --- /dev/null +++ b/src/widgets/resources.rs @@ -0,0 +1,4 @@ +use bevy::prelude::*; + +#[derive(Resource, Default, Deref, DerefMut, Debug)] +pub struct FocusedWidget(pub Option);