raui_app/app/
declarative.rs

1use crate::{Vertex, app::SharedApp, interactions::AppInteractionsEngine};
2use glutin::{event::Event, window::Window};
3use raui_core::{
4    application::Application,
5    interactive::default_interactions_engine::DefaultInteractionsEngine,
6    layout::CoordsMappingScaling,
7    view_model::ViewModel,
8    widget::{node::WidgetNode, utils::Color},
9};
10use spitfire_fontdue::TextRenderer;
11use spitfire_glow::{
12    app::{App, AppConfig, AppControl, AppState},
13    graphics::Graphics,
14};
15
16#[derive(Default)]
17pub struct DeclarativeApp {
18    shared: SharedApp,
19}
20
21impl DeclarativeApp {
22    pub fn simple(title: impl ToString, root: impl Into<WidgetNode>) {
23        App::<Vertex>::new(AppConfig::default().title(title)).run(Self::default().tree(root));
24    }
25
26    pub fn simple_scaled(
27        title: impl ToString,
28        scaling: CoordsMappingScaling,
29        root: impl Into<WidgetNode>,
30    ) {
31        App::<Vertex>::new(AppConfig::default().title(title))
32            .run(Self::default().coords_mapping_scaling(scaling).tree(root));
33    }
34
35    pub fn simple_fullscreen(title: impl ToString, root: impl Into<WidgetNode>) {
36        App::<Vertex>::new(AppConfig::default().title(title).fullscreen(true))
37            .run(Self::default().tree(root));
38    }
39
40    pub fn simple_fullscreen_scaled(
41        title: impl ToString,
42        scaling: CoordsMappingScaling,
43        root: impl Into<WidgetNode>,
44    ) {
45        App::<Vertex>::new(AppConfig::default().title(title).fullscreen(true))
46            .run(Self::default().coords_mapping_scaling(scaling).tree(root));
47    }
48
49    pub fn update(mut self, f: impl FnMut(&mut Application, &mut AppControl) + 'static) -> Self {
50        self.shared.on_update = Some(Box::new(f));
51        self
52    }
53
54    pub fn redraw(
55        mut self,
56        f: impl FnMut(f32, &mut Graphics<Vertex>, &mut TextRenderer<Color>, &mut AppControl) + 'static,
57    ) -> Self {
58        self.shared.on_redraw = Some(Box::new(f));
59        self
60    }
61
62    pub fn event(
63        mut self,
64        f: impl FnMut(&mut Application, Event<()>, &mut Window, &mut DefaultInteractionsEngine) -> bool
65        + 'static,
66    ) -> Self {
67        self.shared.on_event = Some(Box::new(f));
68        self
69    }
70
71    pub fn setup(mut self, mut f: impl FnMut(&mut Application)) -> Self {
72        f(&mut self.shared.application);
73        self
74    }
75
76    pub fn setup_interactions(mut self, mut f: impl FnMut(&mut AppInteractionsEngine)) -> Self {
77        f(&mut self.shared.interactions);
78        self
79    }
80
81    pub fn view_model(mut self, name: impl ToString, view_model: ViewModel) -> Self {
82        self.shared
83            .application
84            .view_models
85            .insert(name.to_string(), view_model);
86        self
87    }
88
89    pub fn tree(mut self, root: impl Into<WidgetNode>) -> Self {
90        self.shared.application.apply(root);
91        self
92    }
93
94    pub fn coords_mapping_scaling(mut self, value: CoordsMappingScaling) -> Self {
95        self.shared.coords_mapping_scaling = value;
96        self
97    }
98}
99
100impl AppState<Vertex> for DeclarativeApp {
101    fn on_init(&mut self, graphics: &mut Graphics<Vertex>, _: &mut AppControl) {
102        self.shared.init(graphics);
103    }
104
105    fn on_redraw(&mut self, graphics: &mut Graphics<Vertex>, control: &mut AppControl) {
106        self.shared.redraw(graphics, control);
107    }
108
109    fn on_event(&mut self, event: Event<()>, window: &mut Window) -> bool {
110        self.shared.event(event, window)
111    }
112}