use super::*;
#[derive(Debug)]
pub struct AppCore
{
pub(crate) already_init: bool,
pub(crate) clipboard: AppClipboard,
pub(crate) graphics: Option<AppGraphics>,
pub(crate) input: AppInput,
pub(crate) window: AppWindow,
pub(crate) perf: AppPerf,
pub(crate) proxy: Option<EventLoopProxy>,
pub(crate) param: AppParamInternal,
}
pub(crate) static APP: Singleton<AppCore> = Singleton::new(AppCore::new);
#[inline(always)]
pub fn try_app() -> Option<impl DerefMut<Target = AppCore>>
{
APP.inner()
.try_lock()
.ok()
.map(|l| l.guard_map_mut(DerefMut::deref_mut))
}
#[inline(always)]
#[track_caller]
pub fn app() -> impl DerefMut<Target = AppCore> { try_app().expect("app already locked") }
impl AppCore
{
pub fn clipboard(&mut self) -> &mut AppClipboard { &mut self.clipboard }
pub fn input(&mut self) -> &mut AppInput { &mut self.input }
pub fn window(&mut self) -> &mut AppWindow { &mut self.window }
pub fn perf(&mut self) -> &mut AppPerf { &mut self.perf }
pub fn graphics(&mut self) -> &mut AppGraphics
{
self.graphics.as_mut().expect("graphics is not init")
}
pub(crate) fn proxy(&self) -> &EventLoopProxy
{
self.proxy.as_ref().expect("proxy is not init")
}
pub(crate) fn param(&self) -> &AppParamInternal { &self.param }
}
impl AppCore
{
pub fn exit(&mut self)
{
self.window.window = None;
self.graphics = None;
self.proxy = None;
}
pub(crate) fn send_event(&mut self, event: AppInternalEvent)
{
let _ = self.try_send_event(event);
}
pub(crate) fn try_send_event(&mut self, event: AppInternalEvent) -> Result<(), ()>
{
match &self.proxy
{
Some(v) => v.send_event(event).map_err(|_| ()),
None => Err(()),
}
}
}
impl AppCore
{
pub(crate) fn new() -> Self
{
Self {
param: ___(),
input: AppInput::new(),
window: AppWindow::new(),
clipboard: AppClipboard::new(),
perf: AppPerf::new(),
already_init: false,
graphics: None,
proxy: None,
}
}
pub(crate) fn init(&mut self, param: AppParamInternal, proxy: EventLoopProxy)
{
assert!(!self.already_init, "app is already init");
self.param = param;
self.window.param = self.param.window.clone();
self.proxy = Some(proxy);
self.already_init = true;
}
}