use std::{cell::RefCell, rc::Rc}; use super::Platform; mod proxy; pub use proxy::*; #[derive(Clone, Copy)] pub struct AppRef<'a>(pub &'a AppHandle); impl<'a> types::AppRef for AppRef<'a> { fn terminate(&self) { // not surport } fn proxy(&self) -> Option { Some(AppProxy()) } } pub struct AppHandle(); impl types::AppHandle for AppHandle { fn singleton() -> Option { Some(Self()) } fn with(&self, mut fun: impl FnMut(::AppRef<'_>)) { fun(AppRef(self)); } } impl AppHandle { pub fn run(&self) { with_app(None, |app| { app.delegate.borrow().as_ref()?.on_launch(AppRef(self)); Some(()) }); } } pub type RcDynDelegate = Rc>; pub struct AppVars { delegate: RefCell>, } thread_local! { static CACHE:RefCell = RefCell::new(AppVars { delegate: RefCell::new(None), }); } pub fn with_app(v: Option, fun: impl Fn(&AppVars) -> T) -> T { if let Some(new) = v { CACHE.with_borrow_mut(|old| { *old.delegate.borrow_mut() = Some(new); }) } CACHE.with_borrow(|v| fun(v)) }