Frontend Module API Reference
The frontend module is responsible for bridging the declarative rsx! macro syntax to the dynamic component rendering system. It defines how component hierarchies are constructed and managed before being translated into Views.
ToRsx Trait
pub trait ToRsx {
fn to_rsx(&self) -> Rsx;
}
The ToRsx trait is implemented by any type that can be converted into an Rsx object. This is crucial for embedding various types (like strings, numbers, or other Rsx instances) directly into the rsx! macro output using the @{expr} syntax.
Implementations:
&Rsx: Converts a reference to anRsxinto an ownedRsxby cloning its internal structure.T: std::fmt::Display: Any type that implementsstd::fmt::Display(e.g.,String,&str,i32,f64, etc.) automatically implementsToRsx. It converts the displayable value into aRsxcontaining a static scope that draws the text.
RsxScope Enum
#[derive(Clone)]
pub enum RsxScope {
Static(Arc<dyn Fn(&Arc<Scope>) + Send + Sync>),
Dynamic(
Arc<dyn Fn(&Arc<Scope>) + Send + Sync>,
Vec<Arc<dyn HookDependency>>,
),
Child(Rsx),
}
RsxScope represents the different kinds of renderable units that can be part of an Rsx hierarchy. These scopes dictate how and when their content is processed and updated.
Static(Arc<dyn Fn(&Arc<Scope>) + Send + Sync>):- Represents content that is processed only once. This is typically used for simple text literals or components that don't depend on reactive state within their
rsx!. - The contained closure is executed once to set up children within a new
Scope.
- Represents content that is processed only once. This is typically used for simple text literals or components that don't depend on reactive state within their
Dynamic(Arc<dyn Fn(&Arc<Scope>) + Send + Sync>, Vec<Arc<dyn HookDependency>>):- Represents content that needs to be re-evaluated and potentially re-rendered when certain
dependencieschange. This is used forrsx!blocks with@ifand@forthat declare dependencies (%dep). - The closure (
drawer) is executed initially and then whenever any of theHookDependencyinstances independenciesnotify an update.
- Represents content that needs to be re-evaluated and potentially re-rendered when certain
Child(Rsx):- Represents a nested
Rsxstructure. This is used when anRsxobject is embedded directly into anotherrsx!block (e.g., via@{other_rsx}or when passingchildrento a component).
- Represents a nested
Rsx Struct
#[derive(Clone)]
pub struct Rsx(Vec<RsxScope>);
The Rsx struct is a collection of RsxScopes, representing a declarative UI tree fragment. It's the primary output of the rsx! macro.
Methods:
fn new() -> Self- Creates a new empty
Rsxinstance.
- Creates a new empty
fn static_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(&mut self, scope: F)- Adds a
StaticRsxScopeto the collection. Thescopeclosure will be executed once to build up the content within a dedicatedScope.
- Adds a
fn dynamic_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(&mut self, drawer: F, dependencies: Vec<Arc<dyn HookDependency>>)- Adds a
DynamicRsxScopeto the collection. Thedrawerclosure will be executed initially and then on subsequent updates of the specifieddependencies.
- Adds a
fn child<R: ToRsx>(&mut self, child: R)- Adds a
ChildRsxScopeto the collection, converting the inputR(which must implementToRsx) into a nestedRsx.
- Adds a
fn generate_children(&self, context: &Arc<Context>)- Processes the internal
Vec<RsxScope>, converting each scope into actual componentContexts andScopes within the provided parentcontext. This method recursively builds the component tree.
- Processes the internal
fn view(&self, context: &Arc<Context>) -> View- The primary method used by components to turn their
rsx!output into a renderableView. - It first calls
generate_childrento build the component tree within the givencontext. - It then returns a
Viewclosure that, when executed, will instruct thecontexttodraw_children.
- The primary method used by components to turn their
The frontend module, through Rsx and RsxScope, provides the declarative interface and the necessary translation layer to OSUI's imperative rendering core.
Next: Explore the Render API.