Skip to content

Instantly share code, notes, and snippets.

@PsichiX
Created October 12, 2023 02:26
Show Gist options
  • Save PsichiX/bba2c77ac247ae62acb523612a674f5a to your computer and use it in GitHub Desktop.
Save PsichiX/bba2c77ac247ae62acb523612a674f5a to your computer and use it in GitHub Desktop.
RAUI retained mode experiments
use raui::prelude::*;
use raui_quick_start::RauiQuickStartBuilder;
use std::any::Any;
#[derive(Default)]
struct AppView {
pub icons: View<ListView<IconView>>,
}
impl ViewState for AppView {
fn on_render(&self, context: WidgetContext) -> WidgetNode {
self.icons.component().key(context.key).into()
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
struct ListView<T: ViewState> {
pub items: Vec<View<T>>,
}
impl<T: ViewState> Default for ListView<T> {
fn default() -> Self {
Self {
items: Default::default(),
}
}
}
impl<T: ViewState> ViewState for ListView<T> {
fn on_render(&self, context: WidgetContext) -> WidgetNode {
make_widget!(vertical_box)
.key(context.key)
.listed_slots(self.items.iter().map(|item| item.component()))
.into()
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
struct IconView {
pub color: Color,
pub size: Scalar,
}
impl ViewState for IconView {
fn on_render(&self, context: WidgetContext) -> WidgetNode {
make_widget!(image_box)
.key(context.key)
.with_props(ImageBoxProps {
width: ImageBoxSizeValue::Exact(self.size),
height: ImageBoxSizeValue::Exact(self.size),
material: ImageBoxMaterial::Color(ImageBoxColor {
color: self.color,
..Default::default()
}),
..Default::default()
})
.into()
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
fn main() {
let app = View::new(AppView {
icons: View::new(ListView {
items: vec![
View::new(IconView {
color: Color {
r: 1.0,
g: 0.25,
b: 0.25,
a: 1.0,
},
size: 64.0,
}),
View::new(IconView {
color: Color {
r: 0.25,
g: 1.0,
b: 0.25,
a: 1.0,
},
size: 64.0,
}),
View::new(IconView {
color: Color {
r: 0.25,
g: 0.25,
b: 1.0,
a: 1.0,
},
size: 64.0,
}),
],
}),
});
RauiQuickStartBuilder::default()
.window_title("Retained mode UI".to_owned())
.widget_tree(app.component().key("app").into())
.build()
.unwrap()
.run()
.unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment