#![cfg_attr(not(debug_assertions), deny(warnings))] #![forbid(unsafe_code)]
#![warn(
clippy::all,
clippy::await_holding_lock,
clippy::dbg_macro,
clippy::doc_markdown,
clippy::empty_enum,
clippy::enum_glob_use,
clippy::exit,
clippy::filter_map_next,
clippy::fn_params_excessive_bools,
clippy::if_let_mutex,
clippy::imprecise_flops,
clippy::inefficient_to_string,
clippy::linkedlist,
clippy::lossy_float_literal,
clippy::macro_use_imports,
clippy::match_on_vec_items,
clippy::match_wildcard_for_single_variants,
clippy::mem_forget,
clippy::mismatched_target_os,
clippy::missing_errors_doc,
clippy::missing_safety_doc,
clippy::needless_borrow,
clippy::needless_continue,
clippy::needless_pass_by_value,
clippy::option_option,
clippy::pub_enum_variant_names,
clippy::rest_pat_in_fully_bound_structs,
clippy::todo,
clippy::unimplemented,
clippy::unnested_or_patterns,
clippy::verbose_file_reads,
future_incompatible,
missing_crate_level_docs,
missing_doc_code_examples,
missing_docs,
rust_2018_idioms,
unused_doc_comments
)]
pub use egui;
pub trait App {
fn update(&mut self, ctx: &egui::CtxRef, frame: &mut Frame<'_>);
fn setup(&mut self, _ctx: &egui::CtxRef) {}
fn warm_up_enabled(&self) -> bool {
false
}
fn load(&mut self, _storage: &dyn Storage) {}
fn save(&mut self, _storage: &mut dyn Storage) {}
fn on_exit(&mut self) {}
fn name(&self) -> &str;
fn auto_save_interval(&self) -> std::time::Duration {
std::time::Duration::from_secs(30)
}
fn is_resizable(&self) -> bool {
true
}
fn clear_color(&self) -> egui::Rgba {
egui::Color32::from_rgb(12, 12, 12).into()
}
}
pub struct Frame<'a>(backend::FrameBuilder<'a>);
impl<'a> Frame<'a> {
pub fn is_web(&self) -> bool {
self.info().web_info.is_some()
}
pub fn info(&self) -> &IntegrationInfo {
&self.0.info
}
pub fn tex_allocator(&mut self) -> &mut Option<&'a mut dyn TextureAllocator> {
&mut self.0.tex_allocator
}
pub fn quit(&mut self) {
self.0.output.quit = true;
}
pub fn set_window_size(&mut self, size: egui::Vec2) {
self.0.output.window_size = Some(size);
}
pub fn set_pixels_per_point(&mut self, pixels_per_point: f32) {
self.0.output.pixels_per_point = Some(pixels_per_point);
}
pub fn repaint_signal(&self) -> std::sync::Arc<dyn RepaintSignal> {
self.0.repaint_signal.clone()
}
#[cfg(feature = "http")]
pub fn http_fetch(
&self,
request: http::Request,
on_done: impl 'static + Send + FnOnce(Result<http::Response, http::Error>),
) {
self.0.http.fetch_dyn(request, Box::new(on_done))
}
}
#[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>,
}
pub trait TextureAllocator {
fn alloc_srgba_premultiplied(
&mut self,
size: (usize, usize),
srgba_pixels: &[egui::Color32],
) -> egui::TextureId;
fn free(&mut self, id: egui::TextureId);
}
pub trait RepaintSignal: Send + Sync {
fn request_repaint(&self);
}
pub trait Storage {
fn get_string(&self, key: &str) -> Option<String>;
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<String> {
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_pretty(value).unwrap());
}
pub const APP_KEY: &str = "app";
#[cfg(feature = "http")]
pub mod http {
pub struct Request {
pub method: String,
pub url: String,
pub body: String,
}
impl Request {
pub fn get(url: impl Into<String>) -> Self {
Self {
method: "GET".to_owned(),
url: url.into(),
body: "".to_string(),
}
}
pub fn post(url: impl Into<String>, body: impl Into<String>) -> Self {
Self {
method: "POST".to_owned(),
url: url.into(),
body: body.into(),
}
}
}
pub struct Response {
pub url: String,
pub ok: bool,
pub status: u16,
pub status_text: String,
pub header_content_type: String,
pub bytes: Vec<u8>,
pub text: Option<String>,
}
pub type Error = String;
}
pub mod backend {
use super::*;
#[cfg(feature = "http")]
pub trait Http {
fn fetch_dyn(
&self,
request: http::Request,
on_done: Box<dyn FnOnce(Result<http::Response, http::Error>) + Send>,
);
}
pub struct FrameBuilder<'a> {
pub info: IntegrationInfo,
pub tex_allocator: Option<&'a mut dyn TextureAllocator>,
#[cfg(feature = "http")]
pub http: std::sync::Arc<dyn backend::Http>,
pub output: &'a mut AppOutput,
pub repaint_signal: std::sync::Arc<dyn RepaintSignal>,
}
impl<'a> FrameBuilder<'a> {
pub fn build(self) -> Frame<'a> {
Frame(self)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AppOutput {
pub quit: bool,
pub window_size: Option<egui::Vec2>,
pub pixels_per_point: Option<f32>,
}
}