use mgui_core::element::ElementTree;
use mgui_core::{FramePaint, Size};
use crate::backend_info::ImageTextureStats;
use crate::renderer::RenderStats;
use crate::task::TaskExecutor;
pub use mgui_shell::{ImeEvent, ShellEvent, ShellMetrics, WindowShell};
#[derive(Clone, Debug)]
pub struct RunnerConfig {
pub title: String,
pub inner_size: Size,
pub physical_size: bool,
pub prefer_software: bool,
pub startup_smoke: bool,
}
impl RunnerConfig {
pub fn new(title: impl Into<String>, width: f32, height: f32) -> Self {
Self {
title: title.into(),
inner_size: Size::new(width, height),
physical_size: false,
prefer_software: false,
startup_smoke: false,
}
}
pub fn with_physical_size(mut self, physical: bool) -> Self {
self.physical_size = physical;
self
}
pub fn with_prefer_software(mut self, prefer: bool) -> Self {
self.prefer_software = prefer;
self
}
pub fn with_startup_smoke(mut self, smoke: bool) -> Self {
self.startup_smoke = smoke;
self
}
}
pub trait SoftwareBackend {
fn present(&mut self, frame: &FramePaint) -> Result<(), String>;
fn set_scale_factor(&mut self, scale_factor: f32);
fn set_logical_size(&mut self, size: Size);
}
#[doc(hidden)]
pub trait RunnerBackend {
fn is_software(&self) -> bool;
fn image_texture_stats(&self) -> ImageTextureStats;
fn resolve_gpu_profile(&mut self) -> Option<u32>;
fn render_stats(&self) -> RenderStats;
}
pub struct AppContext<'a> {
pub shell: &'a dyn WindowShell,
backend: &'a mut dyn RunnerBackend,
pub tasks: &'a TaskExecutor,
}
impl<'a> AppContext<'a> {
#[doc(hidden)]
pub fn new(shell: &'a dyn WindowShell, backend: &'a mut dyn RunnerBackend, tasks: &'a TaskExecutor) -> Self {
Self { shell, backend, tasks }
}
}
impl AppContext<'_> {
pub fn is_software(&self) -> bool {
self.backend.is_software()
}
pub fn metrics(&self) -> ShellMetrics {
self.shell.metrics()
}
pub fn scale_factor(&self) -> f32 {
self.metrics().scale_factor
}
pub fn logical_size(&self) -> Size {
self.metrics().logical_size
}
pub fn request_redraw(&self) {
self.shell.request_redraw();
}
pub fn image_texture_stats(&self) -> ImageTextureStats {
self.backend.image_texture_stats()
}
pub fn resolve_gpu_profile(&mut self) -> Option<u32> {
self.backend.resolve_gpu_profile()
}
pub fn render_stats(&self) -> RenderStats {
self.backend.render_stats()
}
}
pub trait RunnerApp {
fn tree_mut(&mut self) -> &mut ElementTree;
fn mark_redraw(&mut self);
fn mark_rebuild(&mut self) {
self.mark_redraw();
}
fn frame(&mut self, ctx: &mut AppContext<'_>) -> Option<FramePaint>;
fn after_present(&mut self, _ctx: &mut AppContext<'_>) {}
fn on_event(&mut self, _ctx: &mut AppContext<'_>, _event: &ShellEvent) {}
fn on_resized(&mut self, _ctx: &mut AppContext<'_>, _logical_size: Size) {}
fn on_scale_factor_changed(&mut self, _ctx: &mut AppContext<'_>) {}
fn on_about_to_wait(&mut self, _ctx: &mut AppContext<'_>) -> bool {
false
}
fn next_wake(&mut self, _ctx: &mut AppContext<'_>) -> Option<std::time::Duration> {
None
}
}