Component Module API Reference
The component module is the heart of OSUI's UI system, defining how reusable UI units are structured, manage their state, and interact.
ComponentImpl Trait
pub trait ComponentImpl: Send + Sync {
fn call(&self, cx: &Arc<Context>) -> View;
}
The ComponentImpl trait is implemented by all types that can act as an OSUI component.
call(&self, cx: &Arc<Context>) -> View: The core method that renders the component. It takes a reference to the component itself (self) and the component'sContext(cx), and returns aViewwhich contains the drawing instructions.
Implementations:
View: A bareView(anArc<dyn Fn(&mut DrawContext) + Send + Sync>) can itself be aComponentImpl, simply returning itself.Fn(&Arc<Context>) -> View: Any closure with this signature can also be aComponentImpl.#[component]macro: The#[component]macro automatically generates a struct and implementsComponentImplfor it, wrapping your component function.
Component Type Alias
pub type Component = Arc<dyn ComponentImpl>;
A convenience type alias for a ComponentImpl wrapped in an Arc, allowing for shared, thread-safe ownership.
EventHandler Type Alias
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Any) + Send + Sync>>;
A type alias for a mutex-protected, thread-safe, mutable closure that handles events. Event handlers receive the component's Context and a reference to the event data (as &dyn Any).
context Module
Contains the Context struct, which is central to each component instance.
Context Struct
pub struct Context {
component: AccessCell<Component>,
view: AccessCell<View>,
event_handlers: AccessCell<HashMap<TypeId, Vec<EventHandler>>>,
pub(crate) scopes: Mutex<Vec<Arc<Scope>>>,
executor: Arc<dyn CommandExecutor>,
}
The Context holds the runtime state and behavior for a specific component instance. It manages the component's View, its registered event handlers, and its child Scopes.
Methods:
fn new<F: ComponentImpl + 'static>(component: F, executor: Arc<dyn CommandExecutor>) -> Arc<Self>- Creates a new
ArcwrappedContextfor the given component and command executor.
- Creates a new
fn refresh(self: &Arc<Self>)- Re-renders the component by clearing existing event handlers and calling the component's
callmethod to produce a newView. This is typically called automatically by the engine orRsx.
- Re-renders the component by clearing existing event handlers and calling the component's
fn refresh_sync(self: &Arc<Self>)- Synchronously re-renders the component, blocking until the view closure has finished executing.
fn get_view(self: &Arc<Self>) -> View- Returns a clone of the component's current
View.
- Returns a clone of the component's current
fn on_event<T: Any + 'static, F: Fn(&Arc<Self>, &T) + Send + Sync + 'static>(self: &Arc<Self>, handler: F)- Registers an event handler
Ffor events of typeT. When an event of typeTisemitted to thisContext, the handlerFwill be invoked.
- Registers an event handler
fn emit_event<E: Send + Sync + Any + 'static>(self: &Arc<Self>, event: E)- Emits an event
E. All registered handlers for typeEon thisContextare called synchronously, and then the event is propagated to all child components.
- Emits an event
fn emit_event_threaded<E: Any + Send + Sync + Clone + 'static>(self: &Arc<Self>, event: &E)- Emits an event
E. Each registered handler for typeEon thisContextis called in a newly spawned thread. The event is then propagated to all child components (also usingemit_event_threaded). RequiresEto beClone.
- Emits an event
fn scope(self: &Arc<Self>) -> Arc<Scope>- Creates a new, empty child
Scopeand adds it to thisContext. Returns the newScope.
- Creates a new, empty child
fn dyn_scope<F: Fn(&Arc<Scope>) + Send + Sync + 'static>(self: &Arc<Self>, drawer: F, dependencies: &[&dyn HookDependency]) -> Arc<Scope>- Creates a new dynamic child
Scopethat re-renders (by callingdrawer) whenever any of itsdependencieschange.draweris also called immediately. Returns the newScope.
- Creates a new dynamic child
fn add_scope(self: &Arc<Self>, scope: Arc<Scope>)- Adds an already constructed
Scopeas a child to thisContext.
- Adds an already constructed
fn draw_children(self: &Arc<Self>, ctx: &mut DrawContext)- Iterates through all child
Scopes and their components, rendering them into the providedDrawContext. HandlesViewWrappers if present.
- Iterates through all child
fn get_executor(self: &Arc<Self>) -> Arc<dyn CommandExecutor>- Returns a clone of the
CommandExecutorassociated with thisContext.
- Returns a clone of the
fn execute<T: Command + 'static>(self: &Arc<Self>, command: T) -> crate::Result<()>- Executes a given
Commandusing the associatedCommandExecutor.
- Executes a given
fn stop(self: &Arc<Self>) -> crate::Result<()>- A convenience method to execute the
Stopcommand, terminating the application.
- A convenience method to execute the
scope Module
Contains the Scope struct, which organizes child components within a Context.
Scope Struct
pub struct Scope {
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
executor: Arc<dyn CommandExecutor>,
}
A Scope groups child components. Each entry in children consists of a child Context and an optional ViewWrapper that can modify its rendering.
Methods:
fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self>- Creates a new
ArcwrappedScopewith the givenCommandExecutor.
- Creates a new
fn child<F: ComponentImpl + 'static>(self: &Arc<Self>, child: F, view_wrapper: Option<ViewWrapper>)- Creates a new
Contextfor the providedchildcomponent, refreshes it, and adds it to thisScope's children, optionally with aViewWrapper.
- Creates a new
fn view(self: &Arc<Self>, view: View)- Creates a new
Contextdirectly from aView(without an explicitComponentImpl), refreshes it, and adds it to thisScope's children.
- Creates a new
This module forms the backbone of how component trees are constructed and managed in OSUI.
Next: Explore the Engine API.