From 2a62e53ed56b360eca62271ba4f76b7b59b52958 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 24 Jan 2026 01:00:21 +0100 Subject: [PATCH] Engine --- examples/hello_world.rs | 19 ++++---------- src/engine.rs | 58 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 ++++ 3 files changed, 68 insertions(+), 14 deletions(-) create mode 100644 src/engine.rs diff --git a/examples/hello_world.rs b/examples/hello_world.rs index aa430b8..7b6fdd5 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -12,23 +12,14 @@ impl Event for KeyPress { } fn main() { - let cx = Context::new(app); - cx.refresh(); + let console = Console::new(); - std::thread::spawn({ - let cx = cx.clone(); - move || loop { - crossterm::event::read().unwrap(); - cx.emit_event_threaded(&KeyPress); - } + console.thread(move |ctx| loop { + crossterm::event::read().unwrap(); + ctx.emit_event_threaded(&KeyPress); }); - let (width, height) = crossterm::terminal::size().unwrap(); - - loop { - cx.get_view()(&mut DrawContext::new(width, height)); - std::thread::sleep(std::time::Duration::from_millis(16)); - } + console.run(app); } fn app(cx: &Arc) -> View { diff --git a/src/engine.rs b/src/engine.rs new file mode 100644 index 0000000..22dfc45 --- /dev/null +++ b/src/engine.rs @@ -0,0 +1,58 @@ +use std::sync::{Arc, Mutex}; + +use crate::{prelude::Context, DrawContext, View}; + +pub trait Engine { + fn render(&self, ctx: &mut DrawContext, view: View); + fn draw_text(&self, ctx: &mut DrawContext, text: &str); +} + +pub struct Console { + threads: Mutex) + Send + Sync>>>, +} + +impl Console { + pub fn new() -> Self { + Self { + threads: Mutex::new(Vec::new()), + } + } + + pub fn thread) + Send + Sync + 'static>(&self, run: F) { + self.threads.lock().unwrap().push(Arc::new(run)); + } + + pub fn run) -> View + Send + Sync + 'static>(&self, component: F) { + let cx = Context::new(component); + cx.refresh(); + + for thread in self.threads.lock().unwrap().iter() { + let thread = thread.clone(); + + std::thread::spawn({ + let cx = cx.clone(); + move || thread(cx) + }); + } + + let (width, height) = crossterm::terminal::size().unwrap(); + + loop { + cx.get_view()(&mut DrawContext::new(width, height)); + std::thread::sleep(std::time::Duration::from_millis(16)); + } + } +} + +impl Engine for Console { + fn render(&self, ctx: &mut DrawContext, view: View) { + view(&mut DrawContext::new( + ctx.available.width, + ctx.available.height, + )); + } + + fn draw_text(&self, _ctx: &mut DrawContext, text: &str) { + println!("{}", text); + } +} diff --git a/src/lib.rs b/src/lib.rs index e9296d1..b07b882 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,12 @@ use std::sync::Arc; pub mod component; +pub mod engine; pub mod state; pub mod prelude { pub use crate::component::*; + pub use crate::engine::*; pub use crate::state::*; pub use crate::{DrawContext, View}; } @@ -23,6 +25,9 @@ pub struct DrawContext { used: Size, } +#[derive(Debug, Clone)] +pub struct RenderContext {} + impl DrawContext { pub fn new(width: u16, height: u16) -> Self { Self {