uzor_framework/handle/
mod.rs1mod app;
13
14pub use app::{
15 App, FrameTime, HookOp, HookOps, InitCx, IntentCx, OverlayCx, PanelCx, VisualView, Widgets,
16};
17pub(crate) use app::VisualQuery;
18
19use std::sync::mpsc::SyncSender;
20use std::sync::Arc;
21
22use arc_swap::ArcSwap;
23
24use crate::types::command::AppCommand;
25use crate::types::ids::Revision;
26use crate::types::snapshot::VisualSnapshot;
27use crate::types::spec::Spec;
28
29pub(crate) const INBOX_CAP: usize = 1024;
31
32#[derive(Clone)]
36pub struct Waker(Arc<dyn Fn() + Send + Sync>);
37
38impl Waker {
39 pub fn new(f: impl Fn() + Send + Sync + 'static) -> Self {
41 Self(Arc::new(f))
42 }
43
44 pub fn noop() -> Self {
46 Self(Arc::new(|| {}))
47 }
48
49 pub fn wake(&self) {
51 (self.0)();
52 }
53}
54
55impl std::fmt::Debug for Waker {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 f.debug_struct("Waker").finish_non_exhaustive()
58 }
59}
60
61#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
63pub enum HandleError {
64 #[error("inbox full")]
67 Full,
68 #[error("runtime stopped")]
70 Closed,
71}
72
73pub struct Handle<S: Spec> {
79 pub(crate) tx: SyncSender<AppCommand<S>>,
80 pub(crate) snap: Arc<ArcSwap<VisualSnapshot<S>>>,
81 pub(crate) waker: Waker,
82}
83
84impl<S: Spec> Clone for Handle<S> {
85 fn clone(&self) -> Self {
86 Self {
87 tx: self.tx.clone(),
88 snap: Arc::clone(&self.snap),
89 waker: self.waker.clone(),
90 }
91 }
92}
93
94impl<S: Spec> std::fmt::Debug for Handle<S> {
95 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
96 f.debug_struct("Handle")
97 .field("revision", &self.revision())
98 .finish_non_exhaustive()
99 }
100}
101
102impl<S: Spec> Handle<S> {
103 pub fn dispatch(&self, cmd: AppCommand<S>) -> Result<(), HandleError> {
107 self.tx.try_send(cmd).map_err(|e| match e {
108 std::sync::mpsc::TrySendError::Full(_) => HandleError::Full,
109 std::sync::mpsc::TrySendError::Disconnected(_) => HandleError::Closed,
110 })?;
111 self.waker.wake();
112 Ok(())
113 }
114
115 pub fn request_repaint(&self, win: crate::WindowId) -> Result<(), HandleError> {
119 self.dispatch(AppCommand::Cadence(crate::CadenceCmd::Invalidate {
120 win,
121 region: None,
122 bits: uzor::render::InvalidateBits::ALL,
123 }))
124 }
125
126 pub fn snapshot(&self) -> Arc<VisualSnapshot<S>> {
128 self.snap.load_full()
129 }
130
131 pub fn revision(&self) -> Revision {
134 self.snap.load().revision
135 }
136}