View plugins for literals

This commit is contained in:
2026-02-01 17:46:40 +01:00
parent 44a4837647
commit 76984187a0
3 changed files with 50 additions and 13 deletions
+25 -6
View File
@@ -146,14 +146,33 @@ fn emit_node_scope(node: &RsxNode) -> TokenStream {
fn emit_node(node: &RsxNode) -> TokenStream { fn emit_node(node: &RsxNode) -> TokenStream {
match node { match node {
RsxNode::Text { text, deps } => { RsxNode::Text {
text,
deps,
plugins,
} => {
let deps_emit = emit_deps(deps); let deps_emit = emit_deps(deps);
quote! { if plugins.len() == 0 {
scope.view(Arc::new({ quote! {
#deps_emit scope.view(Arc::new({
move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text)) #deps_emit
})); move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text))
}));
}
} else {
let plugins_emit = emit_plugins(plugins);
quote! {
scope.view_wrapped(Arc::new({
#deps_emit
move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!(#text))
}), std::sync::Arc::new(|ctx, view| {
let area = ctx.allocate(ctx.area.x, ctx.area.y, ctx.area.width, ctx.area.height);
ctx.draw_view(area, view.clone());
#plugins_emit
}));
}
} }
} }
+14 -5
View File
@@ -41,7 +41,11 @@ pub struct RsxProp {
/// AST node representing different RSX constructs /// AST node representing different RSX constructs
pub enum RsxNode { pub enum RsxNode {
/// String literal: `"text"` /// String literal: `"text"`
Text { text: LitStr, deps: Vec<Dep> }, Text {
plugins: Vec<ViewPlugin>,
text: LitStr,
deps: Vec<Dep>,
},
/// Expression node: `{expr}` /// Expression node: `{expr}`
Expr(Expr), Expr(Expr),
/// Component instantiation: `Component { prop: value, ... }` /// Component instantiation: `Component { prop: value, ... }`
@@ -212,6 +216,8 @@ impl Parse for RsxNode {
}); });
} }
let plugins = parse_plugins(input)?;
// "%$dep for $pat in $expr { $rsx }" // "%$dep for $pat in $expr { $rsx }"
if input.peek(LitStr) { if input.peek(LitStr) {
let text = input.parse()?; let text = input.parse()?;
@@ -229,16 +235,19 @@ impl Parse for RsxNode {
.collect(), .collect(),
); );
return Ok(RsxNode::Text { text, deps }); return Ok(RsxNode::Text {
text,
deps,
plugins,
});
} }
parse_component_invocation(input) parse_component_invocation(input, plugins)
} }
} }
/// Parses Path or Path { ... } into RsxNode::Component. /// Parses Path or Path { ... } into RsxNode::Component.
fn parse_component_invocation(input: ParseStream) -> Result<RsxNode> { fn parse_component_invocation(input: ParseStream, plugins: Vec<ViewPlugin>) -> Result<RsxNode> {
let plugins = parse_plugins(input)?;
let path: Path = input.parse()?; let path: Path = input.parse()?;
if !input.peek(Brace) { if !input.peek(Brace) {
+11 -2
View File
@@ -10,7 +10,7 @@ use crate::{engine::CommandExecutor, View, ViewWrapper};
use super::{context::Context, ComponentImpl}; use super::{context::Context, ComponentImpl};
/// A scope groups child components and manages their rendering /// A scope groups child components and manages their rendering
/// ///
/// Scopes form the hierarchical structure of a component tree. /// Scopes form the hierarchical structure of a component tree.
/// Each scope contains references to its child components and their /// Each scope contains references to its child components and their
/// optional view wrappers (for layout/styling). /// optional view wrappers (for layout/styling).
@@ -31,7 +31,7 @@ impl Scope {
} }
/// Adds a child component to this scope /// Adds a child component to this scope
/// ///
/// The view_wrapper is optional and can be used for layout or styling. /// The view_wrapper is optional and can be used for layout or styling.
pub fn child<F: ComponentImpl + 'static>( pub fn child<F: ComponentImpl + 'static>(
self: &Arc<Self>, self: &Arc<Self>,
@@ -53,4 +53,13 @@ impl Scope {
self.children.lock().unwrap().push((ctx, None)); self.children.lock().unwrap().push((ctx, None));
} }
/// Adds a view directly to this scope
pub fn view_wrapped(self: &Arc<Self>, view: View, view_wrapper: ViewWrapper) {
let ctx = Context::new(view, self.executor.clone());
ctx.refresh();
self.children.lock().unwrap().push((ctx, Some(view_wrapper)));
}
} }