#![cfg_attr(not(debug_assertions), deny(warnings))]
#![forbid(unsafe_code)]
#![warn(
clippy::all,
clippy::await_holding_lock,
clippy::char_lit_as_u8,
clippy::checked_conversions,
clippy::dbg_macro,
clippy::debug_assert_with_mut_call,
clippy::doc_markdown,
clippy::empty_enum,
clippy::enum_glob_use,
clippy::exit,
clippy::expl_impl_clone_on_copy,
clippy::explicit_deref_methods,
clippy::explicit_into_iter_loop,
clippy::fallible_impl_from,
clippy::filter_map_next,
clippy::float_cmp_const,
clippy::fn_params_excessive_bools,
clippy::if_let_mutex,
clippy::imprecise_flops,
clippy::inefficient_to_string,
clippy::invalid_upcast_comparisons,
clippy::large_types_passed_by_value,
clippy::let_unit_value,
clippy::linkedlist,
clippy::lossy_float_literal,
clippy::macro_use_imports,
clippy::manual_ok_or,
clippy::map_err_ignore,
clippy::map_flatten,
clippy::match_on_vec_items,
clippy::match_same_arms,
clippy::match_wildcard_for_single_variants,
clippy::mem_forget,
clippy::mismatched_target_os,
clippy::missing_errors_doc,
clippy::missing_safety_doc,
clippy::mut_mut,
clippy::mutex_integer,
clippy::needless_borrow,
clippy::needless_continue,
clippy::needless_pass_by_value,
clippy::option_option,
clippy::path_buf_push_overwrite,
clippy::ptr_as_ptr,
clippy::ref_option_ref,
clippy::rest_pat_in_fully_bound_structs,
clippy::same_functions_in_if_condition,
clippy::string_add_assign,
clippy::string_add,
clippy::string_lit_as_bytes,
clippy::string_to_string,
clippy::todo,
clippy::trait_duplication_in_bounds,
clippy::unimplemented,
clippy::unnested_or_patterns,
clippy::unused_self,
clippy::useless_transmute,
clippy::verbose_file_reads,
clippy::zero_sized_map_values,
future_incompatible,
nonstandard_style,
rust_2018_idioms
)]
#![allow(clippy::float_cmp)]
#![allow(clippy::manual_range_contains)]
pub use egui;
pub trait App {
fn update(&mut self, ctx: &egui::CtxRef, frame: &mut Frame<'_>);
fn setup(
&mut self,
_ctx: &egui::CtxRef,
_frame: &mut Frame<'_>,
_storage: Option<&dyn Storage>,
) {
}
fn warm_up_enabled(&self) -> bool {
false
}
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 max_size_points(&self) -> egui::Vec2 {
egui::Vec2::new(1024.0, 2048.0)
}
fn clear_color(&self) -> egui::Rgba {
egui::Color32::from_rgba_unmultiplied(12, 12, 12, 180).into()
}
}
#[derive(Clone)]
pub struct NativeOptions {
pub always_on_top: bool,
pub decorated: bool,
pub drag_and_drop_support: bool,
pub icon_data: Option<IconData>,
pub initial_window_size: Option<egui::Vec2>,
pub resizable: bool,
pub transparent: bool,
}
impl Default for NativeOptions {
fn default() -> Self {
Self {
always_on_top: false,
decorated: true,
drag_and_drop_support: false,
icon_data: None,
initial_window_size: None,
resizable: true,
transparent: false,
}
}
}
#[derive(Clone)]
pub struct IconData {
pub rgba: Vec<u8>,
pub width: u32,
pub height: u32,
}
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 dyn TextureAllocator {
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 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 prefer_dark_mode: Option<bool>,
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 = "ron")]
pub fn get_value<T: serde::de::DeserializeOwned>(storage: &dyn Storage, key: &str) -> Option<T> {
storage
.get_string(key)
.and_then(|value| ron::from_str(&value).ok())
}
#[cfg(feature = "ron")]
pub fn set_value<T: serde::Serialize>(storage: &mut dyn Storage, key: &str, value: &T) {
storage.set_string(
key,
ron::ser::to_string_pretty(value, Default::default()).unwrap(),
);
}
pub const APP_KEY: &str = "app";
#[cfg(feature = "http")]
pub mod http {
use std::collections::BTreeMap;
pub struct Request {
pub method: String,
pub url: String,
pub body: Vec<u8>,
pub headers: BTreeMap<String, String>,
}
impl Request {
pub fn create_headers_map(headers: &[(&str, &str)]) -> BTreeMap<String, String> {
headers
.iter()
.map(|e| (e.0.to_owned(), e.1.to_owned()))
.collect()
}
#[allow(clippy::needless_pass_by_value)]
pub fn get(url: impl ToString) -> Self {
Self {
method: "GET".to_owned(),
url: url.to_string(),
body: vec![],
headers: Request::create_headers_map(&[("Accept", "*/*")]),
}
}
#[allow(clippy::needless_pass_by_value)]
pub fn post(url: impl ToString, body: impl ToString) -> Self {
Self {
method: "POST".to_owned(),
url: url.to_string(),
body: body.to_string().into_bytes(),
headers: Request::create_headers_map(&[
("Accept", "*/*"),
("Content-Type", "text/plain; charset=utf-8"),
]),
}
}
}
pub struct Response {
pub url: String,
pub ok: bool,
pub status: u16,
pub status_text: String,
pub bytes: Vec<u8>,
pub headers: BTreeMap<String, String>,
}
impl Response {
pub fn text(&self) -> Option<String> {
String::from_utf8(self.bytes.clone()).ok()
}
pub fn content_type(&self) -> Option<String> {
self.headers.get("content-type").cloned()
}
}
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: &'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>,
}
}