use crate::Context;
pub trait App {
fn setup(&mut self, _ctx: &std::sync::Arc<Context>) {}
fn ui(
&mut self,
ctx: &std::sync::Arc<Context>,
integration_context: &mut IntegrationContext<'_>,
);
fn on_exit(&mut self, _storage: &mut dyn Storage) {}
}
pub struct IntegrationContext<'a> {
pub info: IntegrationInfo,
pub tex_allocator: Option<&'a mut dyn TextureAllocator>,
pub output: AppOutput,
}
#[derive(Clone, Debug)]
pub struct WebInfo {
pub web_location_hash: String,
}
#[derive(Clone, Debug)]
pub struct IntegrationInfo {
pub web_info: Option<WebInfo>,
pub cpu_usage: Option<f32>,
pub seconds_since_midnight: Option<f64>,
pub native_pixels_per_point: Option<f32>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AppOutput {
pub quit: bool,
pub window_size: Option<crate::Vec2>,
pub pixels_per_point: Option<f32>,
}
pub trait TextureAllocator {
fn new_texture_srgba_premultiplied(
&mut self,
size: (usize, usize),
pixels: &[crate::Srgba],
) -> crate::TextureId;
}
pub trait Storage {
fn get_string(&self, key: &str) -> Option<&str>;
fn set_string(&mut self, key: &str, value: String);
fn flush(&mut self);
}
#[derive(Clone, Default)]
pub struct DummyStorage {}
impl Storage for DummyStorage {
fn get_string(&self, _key: &str) -> Option<&str> {
None
}
fn set_string(&mut self, _key: &str, _value: String) {}
fn flush(&mut self) {}
}
#[cfg(feature = "serde_json")]
pub fn get_value<T: serde::de::DeserializeOwned>(storage: &dyn Storage, key: &str) -> Option<T> {
storage
.get_string(key)
.and_then(|value| serde_json::from_str(value).ok())
}
#[cfg(feature = "serde_json")]
pub fn set_value<T: serde::Serialize>(storage: &mut dyn Storage, key: &str, value: &T) {
storage.set_string(key, serde_json::to_string(value).unwrap());
}
pub const APP_KEY: &str = "app";