mod app;
pub use app::{
App, FrameTime, HookOp, HookOps, InitCx, IntentCx, OverlayCx, PanelCx, VisualView, Widgets,
};
use std::sync::mpsc::SyncSender;
use std::sync::Arc;
use arc_swap::ArcSwap;
use crate::types::command::AppCommand;
use crate::types::ids::Revision;
use crate::types::snapshot::VisualSnapshot;
use crate::types::spec::Spec;
pub(crate) const INBOX_CAP: usize = 1024;
#[derive(Clone)]
pub struct Waker(Arc<dyn Fn() + Send + Sync>);
impl Waker {
pub fn new(f: impl Fn() + Send + Sync + 'static) -> Self {
Self(Arc::new(f))
}
pub fn noop() -> Self {
Self(Arc::new(|| {}))
}
pub fn wake(&self) {
(self.0)();
}
}
impl std::fmt::Debug for Waker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Waker").finish_non_exhaustive()
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum HandleError {
#[error("inbox full")]
Full,
#[error("runtime stopped")]
Closed,
}
pub struct Handle<S: Spec> {
pub(crate) tx: SyncSender<AppCommand<S>>,
pub(crate) snap: Arc<ArcSwap<VisualSnapshot<S>>>,
pub(crate) waker: Waker,
}
impl<S: Spec> Clone for Handle<S> {
fn clone(&self) -> Self {
Self {
tx: self.tx.clone(),
snap: Arc::clone(&self.snap),
waker: self.waker.clone(),
}
}
}
impl<S: Spec> std::fmt::Debug for Handle<S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Handle")
.field("revision", &self.revision())
.finish_non_exhaustive()
}
}
impl<S: Spec> Handle<S> {
pub fn dispatch(&self, cmd: AppCommand<S>) -> Result<(), HandleError> {
self.tx.try_send(cmd).map_err(|e| match e {
std::sync::mpsc::TrySendError::Full(_) => HandleError::Full,
std::sync::mpsc::TrySendError::Disconnected(_) => HandleError::Closed,
})?;
self.waker.wake();
Ok(())
}
pub fn snapshot(&self) -> Arc<VisualSnapshot<S>> {
self.snap.load_full()
}
pub fn revision(&self) -> Revision {
self.snap.load().revision
}
}