Implemented commands on scopes

This commit is contained in:
2026-01-28 19:33:12 +01:00
parent 715175c412
commit 63abf41094
4 changed files with 26 additions and 17 deletions
+5 -11
View File
@@ -28,21 +28,15 @@ fn emit_deps(deps: &[Dep]) -> TokenStream {
fn emit_node(node: &RsxNode) -> TokenStream { fn emit_node(node: &RsxNode) -> TokenStream {
match node { match node {
RsxNode::Text(text) => quote! { RsxNode::Text(text) => quote! {
{ r.static_scope(|scope| scope.view(Arc::new(move |ctx| {
let scope = osui::component::Scope::new();
scope.view(Arc::new(move |ctx| {
ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text)) ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text))
})); })));
r.static_scope(scope);
}
}, },
RsxNode::Component(name) => quote! { RsxNode::Component(name) => quote! {
{ r.static_scope(|scope| scope.view(Arc::new(move |ctx| {
let scope = osui::component::Scope::new(); scope.child(#name, None)
scope.child(#name, None); })));
r.static_scope(scope);
}
}, },
RsxNode::Mount(m) => quote! { RsxNode::Mount(m) => quote! {
+9 -1
View File
@@ -4,7 +4,7 @@ pub mod console;
pub use benchmark::*; pub use benchmark::*;
pub use console::*; pub use console::*;
use std::sync::Arc; use std::{any::Any, sync::Arc};
use crate::{prelude::Context, render::Area, DrawContext, View}; use crate::{prelude::Context, render::Area, DrawContext, View};
@@ -30,3 +30,11 @@ pub trait Engine {
fn render_view(&self, area: &Area, view: &View) -> DrawContext; fn render_view(&self, area: &Area, view: &View) -> DrawContext;
fn draw_context(&self, ctx: &DrawContext); fn draw_context(&self, ctx: &DrawContext);
} }
pub trait Command {
fn as_any(&self) -> &dyn Any;
}
pub trait CommandExecutor {
fn execute_command(&self, command: &Arc<dyn Command>) -> crate::Result<()>;
}
+8 -4
View File
@@ -7,7 +7,7 @@ use crate::{
}; };
pub enum RsxScope { pub enum RsxScope {
Static(Arc<Scope>), Static(Box<dyn Fn(&Arc<Scope>) + Send + Sync>),
Dynamic( Dynamic(
Arc<dyn Fn(&Arc<Scope>) + Send + Sync>, Arc<dyn Fn(&Arc<Scope>) + Send + Sync>,
Vec<Box<dyn HookDependency>>, Vec<Box<dyn HookDependency>>,
@@ -21,8 +21,8 @@ impl Rsx {
Self(Vec::new()) Self(Vec::new())
} }
pub fn static_scope(&mut self, scope: Arc<Scope>) { pub fn static_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(&mut self, scope: F) {
self.0.push(RsxScope::Static(scope)); self.0.push(RsxScope::Static(Box::new(scope)));
} }
pub fn dynamic_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>( pub fn dynamic_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(
@@ -37,7 +37,11 @@ impl Rsx {
pub fn view(&self, context: Arc<Context>) -> View { pub fn view(&self, context: Arc<Context>) -> View {
for scope in &self.0 { for scope in &self.0 {
match scope { match scope {
RsxScope::Static(scope) => context.scopes.lock().unwrap().push(scope.clone()), RsxScope::Static(scope_fn) => {
let scope = Scope::new();
(scope_fn)(&scope);
context.scopes.lock().unwrap().push(scope)
}
RsxScope::Dynamic(drawer, dependencies) => { RsxScope::Dynamic(drawer, dependencies) => {
let drawer = drawer.clone(); let drawer = drawer.clone();
context.dyn_scope( context.dyn_scope(
+3
View File
@@ -19,3 +19,6 @@ pub mod prelude {
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>; pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
pub type ViewWrapper = Arc<dyn Fn(&mut DrawContext, View) + Send + Sync>; pub type ViewWrapper = Arc<dyn Fn(&mut DrawContext, View) + Send + Sync>;
pub type Result<T> = std::result::Result<T, Error>;
pub enum Error {}