Working UI engine

This commit is contained in:
2026-07-14 13:29:16 -04:00
parent 7b03e13123
commit 3428beef8e
6 changed files with 461 additions and 93 deletions
+64
View File
@@ -0,0 +1,64 @@
use std::{any::Any, sync::Arc};
use tokio::sync::{Mutex, MutexGuard, mpsc::Sender};
pub struct Refresh;
#[derive(Debug, Clone)]
pub struct Context {
pub(crate) tx: Sender<Box<dyn Any>>,
}
pub struct State<T> {
pub value: Arc<Mutex<T>>,
tx: Sender<Box<dyn Any>>,
}
pub struct StateGuard<'a, T> {
is_mutated: bool,
value: MutexGuard<'a, T>,
tx: &'a Sender<Box<dyn Any>>,
}
impl Context {
pub fn use_state<T>(&self, v: T) -> State<T> {
State {
value: Arc::new(Mutex::new(v)),
tx: self.tx.clone(),
}
}
}
impl<T> State<T> {
pub async fn lock<'a>(&'a self) -> StateGuard<'a, T> {
StateGuard {
value: self.value.lock().await,
is_mutated: false,
tx: &self.tx,
}
}
}
impl<'a, T> std::ops::Deref for StateGuard<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl<'a, T> std::ops::DerefMut for StateGuard<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
self.is_mutated = true;
&mut self.value
}
}
impl<'a, T> Drop for StateGuard<'a, T> {
fn drop(&mut self) {
if self.is_mutated {
self.tx.blocking_send(Box::new(Refresh)).unwrap()
}
}
}