RAUI is heavely inspired by React declarative UI composition and UE4 Slate widget components system.
The main idea behind RAUI architecture is to treat UI as another data that you transform into target renderable data format used by your rendering engine of choice.
It is the central point of user interrest. It performs whole UI processing logic. There you apply widget tree that wil be processed, send messages from host application to widgets and receive signals sent from widgets to host application.
rust
let mut application = Application::new();
let tree = widget! {
(app {
title = (title_bar: {"Hello".to_owned()})
content = (vertical_box [
(#{"hi"} button: {"Say hi!".to_owned()})
(#{"exit"} button: {"Close".to_owned()})
])
})
};
let mut renderer = HtmlRenderer::default();
application.apply(tree);
if let Ok(output) = application.render(&mut renderer) {
println!("{}", output);
}
Widgets are divided into three categories:
- Widget Node - used as source UI trees (variant that can be either a component, unit or none)
rust
widget! {
(app {
title = (title_bar: {"Hello".to_owned()})
content = (vertical_box [
(#{"hi"} button: {"Say hi!".to_owned()})
(#{"exit"} button: {"Close".to_owned()})
])
})
}
- Widget Component - you can think of them as Virtual DOM nodes, they store:
- pointer to component function (that process their data)
- unique key (that is a part of widget ID and will be used to tell the system if it should carry its state to next processing run)
- boxed cloneable properties data (if component is a function, then properties are function arguments)
- listed slots (simply: widget children)
- named slots (similar to listed slots: widget children, but these ones have names assigned to them, so you can access them by name instead of by index)
- Widget Unit - an atomic element that renderers use to convert into target renderable data format for rendering engine of choice.
rust
widget!{{{
TextBox {
text: "Hello World".to_owned(),
..Default::default()
}
}}}
Component functions are static functions that transforms input data (properties, state or neither of them) into output widget tree (usually used to simply wrap another components tree under one simple component, where at some point the simplest components returns final widget units). They work together as a chain of transforms - root component applies some properties into children components using data from its own properties or state. ```rust
struct AppProps { pub index: usize, } implementpropsdata!(AppProps);
widgetcomponent! {
app(props, namedslots) {
unpacknamedslots!(namedslots => { title, content });
let index = props.read::
widget! {
(#{index} vertical_box [
{title}
{content}
])
}
}
}
widgetcomponent! {
verticalbox(key, listedslots) {
let items = listedslots
.intoiter()
.map(|slot| FlexBoxItem {
slot: slot.tryinto().expect("Cannot convert slot to WidgetUnit!"),
..Default::default()
})
.collect::
widget! {{{
FlexBox {
items,
..Default::default()
}
}}}
}
}
This may bring up a question: _**"If i use only functions and no objects to tell how to visualize UI, how do i keep some data between each render run?"**_.
For that you use _states_. State is a data that is stored between each processing call as long as given widget is alive (that means: as long as widget id stays the same between two processing calls, to make sure your widget stays the same, you use keys - if no key is assigned, system will generate one for your widget but that will make it possible to die at any time if for example number of widget children changes in your common parent, your widget will change its id when key wasn't assigned).
Some additional notes: While you use _properties_ to send information down the tree and _states_ to store widget data between processing cals, you can communicate with another widgets and host application using messages and signals!
More than that, you can use hooks to listen for widget life cycle and perform actions there.
rust
struct ButtonState { pub pressed: bool, }
enum ButtonAction { Pressed, Released, }
widgethook! { usebutton(key, lifecycle) { let key = key.toowned(); lifecycle.mount(move |_, state, _, _| { drop(state.write(ButtonState { pressed: false })); }); } }
widgetcomponent! {
button(key, props, state, messenger, signals) [usebutton] {
let label = props.readclonedor_default::
while let Some(msg) = messenger.read() {
if let Some(msg) = msg.downcast_ref::<ButtonAction>() {
let pressed = match msg {
ButtonAction::Pressed => true,
ButtonAction::Released => false,
};
drop(state.write(ButtonState { pressed }));
drop(signals.write(Box::new(*msg)));
}
}
widget!{
(#{key} text: {label})
}
}
} ```
Hooks are used to put common widget logic into separate functions that can be chained in widgets and another hooks (you can build a reusable dependency chain of logic with that). Usually it is used to listen for life cycle events such as mount, change and unmount, additionally you can chain hooks to be processed sequentially in order they are chained in widgets and other hooks. ```rust widgethook! { useempty {} }
widgethook! { usebutton(lifecycle) [useempty] { lifecycle.mount(move |, state, _, _| { drop(state.write(ButtonState { pressed: false })); }); } }
widgetcomponent! {
button(key, props, state, messenger, signals) [usebutton] {
let label = props.readclonedor_default::
while let Some(msg) = messenger.read() {
if let Some(msg) = msg.downcast_ref::<ButtonAction>() {
let pressed = match msg {
ButtonAction::Pressed => true,
ButtonAction::Released => false,
};
drop(state.write(ButtonState { pressed }));
drop(signals.write(Box::new(*msg)));
}
}
widget!{
(#{key} text: {label})
}
}
}
``
What happens under the hood:
- Application calls
buttonon a node
-
buttoncalls
usebuttonhook
-
usebuttoncalls
useemptyhook
-
usebuttonlogic is executed
-
button` logic is executed
TODO
RAUI exposes API (Application::layout()
) to allow use of custom layout engines to perform widget tree positioning data, which is later used by custom UI renderers to specify boxes where given widgets should be placed.
Every call to perform layouting will store a layout data inside Application, you can always access that data at any time.
There is a DefaultLayoutEngine
that does this in a generic way.
If you find some part of its pipeline working different than what you've expected, feel free to create your custom layout engine!
rust
let mut application = Application::new();
application.apply(tree);
application.process();
if application.layout(view, &mut DefaultLayoutEngine).is_ok() {
println!("LAYOUT:\n{:#?}", application.layout_data());
}
There is a main raui
crate that contains all of the project sub-crates to allow easy access to all features needed at any time, each enabled using Cargo feature
flags (by default only raui-core
subcrate is enabled).
toml
[dependencies]
raui = { version = "*", features = ["all"] }
raui-core
- Core module that contains all RAUI essential logic.
toml
[dependencies]
raui-core = "*"
raui-binary-renderer
- Renders RAUI widget tree into binary format (binary
feature).
toml
[dependencies]
raui-binary-renderer = "*"
raui-html-renderer
- Renders RAUI widget tree into simple HTML format (html
feature).
toml
[dependencies]
raui-html-renderer = "*"
raui-json-renderer
- Renders RAUI widget tree into JSON format (json
feature).
toml
[dependencies]
raui-json-renderer = "*"
raui-ron-renderer
- Renders RAUI widget tree into RON format (ron
feature).
toml
[dependencies]
raui-ron-renderer = "*"
raui-yaml-renderer
- Renders RAUI widget tree into YAML format (yaml
feature).
toml
[dependencies]
raui-yaml-renderer = "*"
raui-ggez-renderer
- Renders RAUI widget tree with GGEZ renderer.
toml
[dependencies]
raui-ggez-renderer = "*"
RAUI is still in early development phase, so prepare for these changes until v1.0: - Create renderer for at least one popular Rust graphics engine. - Create TODO app as an example. - Reduce unnecessary allocations in processing pipeline. - Find a solution (or make it a feature) for moving from trait objects data into strongly typed data for properties and states. - Make C API bindings.