Frontend structure
This commit is contained in:
+1
-2
@@ -9,8 +9,7 @@ homepage = "https://osui.netlify.app"
|
||||
documentation = "https://osui.netlify.app/docs/"
|
||||
|
||||
[features]
|
||||
no_rsx = []
|
||||
no_elem = []
|
||||
rsx = []
|
||||
|
||||
[lib]
|
||||
name = "osui"
|
||||
|
||||
@@ -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
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#[macro_export]
|
||||
macro_rules! rsx {
|
||||
() => {
|
||||
$crate::frontend::Rsx::new()
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! rsx_scope {
|
||||
() => {};
|
||||
}
|
||||
Reference in New Issue
Block a user