Frontend structure

This commit is contained in:
2026-01-25 18:38:07 +01:00
parent 4afe1c24c2
commit f6a4bbd8ac
6 changed files with 76 additions and 12 deletions
+1 -2
View File
@@ -9,8 +9,7 @@ homepage = "https://osui.netlify.app"
documentation = "https://osui.netlify.app/docs/"
[features]
no_rsx = []
no_elem = []
rsx = []
[lib]
name = "osui"
+2 -8
View File
@@ -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<Context>) -> View {
);
}
Arc::new({
let cx = cx.clone();
move |ctx| {
cx.draw_children(ctx);
}
})
rsx! {}.view(cx.clone())
}
fn my_component(cx: &Arc<Context>) -> View {
+1 -1
View File
@@ -31,7 +31,7 @@ pub struct Context {
component: Mutex<Component>,
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
view: Mutex<View>,
scopes: Mutex<Vec<Arc<Scope>>>,
pub(crate) scopes: Mutex<Vec<Arc<Scope>>>,
}
impl Context {
+58
View File
@@ -0,0 +1,58 @@
use std::sync::Arc;
use crate::{
component::{Context, Scope},
state::HookDependency,
View,
};
pub enum RsxScope {
Static(Arc<Scope>),
Dynamic(
Arc<dyn Fn(&Arc<Scope>) + Send + Sync>,
Vec<Box<dyn HookDependency>>,
),
}
pub struct Rsx(Vec<RsxScope>);
impl Rsx {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn static_scope(&mut self, scope: Arc<Scope>) {
self.0.push(RsxScope::Static(scope));
}
pub fn dynamic_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(
&mut self,
drawer: F,
dependencies: Vec<Box<dyn HookDependency>>,
) {
self.0
.push(RsxScope::Dynamic(Arc::new(drawer), dependencies));
}
pub fn view(&self, context: Arc<Context>) -> 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::<Vec<_>>(),
);
}
}
}
let context = context.clone();
Arc::new({
move |ctx| {
context.draw_children(ctx);
}
})
}
}
+2
View File
@@ -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;
+11
View File
@@ -0,0 +1,11 @@
#[macro_export]
macro_rules! rsx {
() => {
$crate::frontend::Rsx::new()
};
}
#[macro_export]
macro_rules! rsx_scope {
() => {};
}