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