From f6a4bbd8ac86e5e452b3af2a8655303553807cb4 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 25 Jan 2026 18:38:07 +0100 Subject: [PATCH] Frontend structure --- Cargo.toml | 5 ++-- examples/hello_world.rs | 10 ++----- src/component.rs | 2 +- src/frontend.rs | 58 +++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ src/macros.rs | 11 ++++++++ 6 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 src/frontend.rs create mode 100644 src/macros.rs diff --git a/Cargo.toml b/Cargo.toml index d9080c4..a40db29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,11 +6,10 @@ license = "Apache-2.0" description = "A TUI library for advanced uis" repository = "https://github.com/osui-rs/osui" homepage = "https://osui.netlify.app" -documentation = "https://osui.netlify.app/docs/" +documentation = "https://osui.netlify.app/docs/" [features] -no_rsx = [] -no_elem = [] +rsx = [] [lib] name = "osui" diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 87702ef..948c67c 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use osui::prelude::*; +use osui::{prelude::*, rsx}; #[derive(Debug, Clone)] pub struct KeyPress; @@ -94,13 +94,7 @@ fn app(cx: &Arc) -> View { ); } - Arc::new({ - let cx = cx.clone(); - - move |ctx| { - cx.draw_children(ctx); - } - }) + rsx! {}.view(cx.clone()) } fn my_component(cx: &Arc) -> View { diff --git a/src/component.rs b/src/component.rs index a65bde0..27f6c14 100644 --- a/src/component.rs +++ b/src/component.rs @@ -31,7 +31,7 @@ pub struct Context { component: Mutex, event_handlers: Mutex>>, view: Mutex, - scopes: Mutex>>, + pub(crate) scopes: Mutex>>, } impl Context { diff --git a/src/frontend.rs b/src/frontend.rs new file mode 100644 index 0000000..e90ea99 --- /dev/null +++ b/src/frontend.rs @@ -0,0 +1,58 @@ +use std::sync::Arc; + +use crate::{ + component::{Context, Scope}, + state::HookDependency, + View, +}; + +pub enum RsxScope { + Static(Arc), + Dynamic( + Arc) + Send + Sync>, + Vec>, + ), +} + +pub struct Rsx(Vec); + +impl Rsx { + pub fn new() -> Self { + Self(Vec::new()) + } + + pub fn static_scope(&mut self, scope: Arc) { + self.0.push(RsxScope::Static(scope)); + } + + pub fn dynamic_scope) + Send + Sync + 'static>( + &mut self, + drawer: F, + dependencies: Vec>, + ) { + self.0 + .push(RsxScope::Dynamic(Arc::new(drawer), dependencies)); + } + + pub fn view(&self, context: Arc) -> View { + for scope in &self.0 { + match scope { + RsxScope::Static(scope) => context.scopes.lock().unwrap().push(scope.clone()), + RsxScope::Dynamic(drawer, dependencies) => { + let drawer = drawer.clone(); + context.dyn_scope( + move |c| drawer(c), + &dependencies.iter().map(|d| d.as_ref()).collect::>(), + ); + } + } + } + + let context = context.clone(); + Arc::new({ + move |ctx| { + context.draw_children(ctx); + } + }) + } +} diff --git a/src/lib.rs b/src/lib.rs index 16af55e..1462c02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,8 @@ use crate::render::DrawContext; pub mod component; pub mod engine; +pub mod frontend; +pub mod macros; pub mod render; pub mod state; diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..331ef5c --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,11 @@ +#[macro_export] +macro_rules! rsx { + () => { + $crate::frontend::Rsx::new() + }; +} + +#[macro_export] +macro_rules! rsx_scope { + () => {}; +}