1use mgui_core::element::ElementTree;
10use mgui_core::{FramePaint, Size};
11
12use crate::backend_info::ImageTextureStats;
13use crate::renderer::RenderStats;
14use crate::task::TaskExecutor;
15pub use mgui_shell::{ImeEvent, ShellEvent, ShellMetrics, WindowShell};
16
17#[derive(Clone, Debug)]
23pub struct RunnerConfig {
24 pub title: String,
25 pub inner_size: Size,
27 pub physical_size: bool,
29 pub prefer_software: bool,
31 pub startup_smoke: bool,
33}
34
35impl RunnerConfig {
36 pub fn new(title: impl Into<String>, width: f32, height: f32) -> Self {
37 Self {
38 title: title.into(),
39 inner_size: Size::new(width, height),
40 physical_size: false,
41 prefer_software: false,
42 startup_smoke: false,
43 }
44 }
45
46 pub fn with_physical_size(mut self, physical: bool) -> Self {
48 self.physical_size = physical;
49 self
50 }
51
52 pub fn with_prefer_software(mut self, prefer: bool) -> Self {
54 self.prefer_software = prefer;
55 self
56 }
57
58 pub fn with_startup_smoke(mut self, smoke: bool) -> Self {
60 self.startup_smoke = smoke;
61 self
62 }
63}
64
65pub trait SoftwareBackend {
70 fn present(&mut self, frame: &FramePaint) -> Result<(), String>;
72 fn set_scale_factor(&mut self, scale_factor: f32);
73 fn set_logical_size(&mut self, size: Size);
74}
75
76#[doc(hidden)]
78pub trait RunnerBackend {
79 fn is_software(&self) -> bool;
80 fn image_texture_stats(&self) -> ImageTextureStats;
81 fn resolve_gpu_profile(&mut self) -> Option<u32>;
82 fn render_stats(&self) -> RenderStats;
84}
85
86pub struct AppContext<'a> {
90 pub shell: &'a dyn WindowShell,
92 backend: &'a mut dyn RunnerBackend,
93 pub tasks: &'a TaskExecutor,
95}
96
97impl<'a> AppContext<'a> {
98 #[doc(hidden)]
100 pub fn new(shell: &'a dyn WindowShell, backend: &'a mut dyn RunnerBackend, tasks: &'a TaskExecutor) -> Self {
101 Self { shell, backend, tasks }
102 }
103}
104
105impl AppContext<'_> {
106 pub fn is_software(&self) -> bool {
107 self.backend.is_software()
108 }
109
110 pub fn metrics(&self) -> ShellMetrics {
111 self.shell.metrics()
112 }
113
114 pub fn scale_factor(&self) -> f32 {
116 self.metrics().scale_factor
117 }
118
119 pub fn logical_size(&self) -> Size {
121 self.metrics().logical_size
122 }
123
124 pub fn request_redraw(&self) {
126 self.shell.request_redraw();
127 }
128
129 pub fn image_texture_stats(&self) -> ImageTextureStats {
131 self.backend.image_texture_stats()
132 }
133
134 pub fn resolve_gpu_profile(&mut self) -> Option<u32> {
136 self.backend.resolve_gpu_profile()
137 }
138
139 pub fn render_stats(&self) -> RenderStats {
141 self.backend.render_stats()
142 }
143}
144
145pub trait RunnerApp {
154 fn tree_mut(&mut self) -> &mut ElementTree;
156
157 fn mark_redraw(&mut self);
159
160 fn mark_rebuild(&mut self) {
163 self.mark_redraw();
164 }
165
166 fn frame(&mut self, ctx: &mut AppContext<'_>) -> Option<FramePaint>;
175
176 fn after_present(&mut self, _ctx: &mut AppContext<'_>) {}
178
179 fn on_event(&mut self, _ctx: &mut AppContext<'_>, _event: &ShellEvent) {}
183
184 fn on_resized(&mut self, _ctx: &mut AppContext<'_>, _logical_size: Size) {}
186
187 fn on_scale_factor_changed(&mut self, _ctx: &mut AppContext<'_>) {}
189
190 fn on_about_to_wait(&mut self, _ctx: &mut AppContext<'_>) -> bool {
192 false
193 }
194
195 fn next_wake(&mut self, _ctx: &mut AppContext<'_>) -> Option<std::time::Duration> {
200 None
201 }
202}