bye DynamicDevice; now to reenable playback...

This commit is contained in:
🪞👃🪞 2024-07-03 21:18:39 +03:00
parent 55a85fafef
commit 9d46cb7619
4 changed files with 40 additions and 125 deletions

View file

@ -1,77 +0,0 @@
use crate::core::*;
pub trait Component: Render + Handle {}
impl<T: Render + Handle> Component for T {}
/// A UI component that may have presence on the JACK grap.
pub trait Device: Render + Handle + Ports + Send + Sync {
fn boxed (self) -> Box<dyn Device> where Self: Sized + 'static {
Box::new(self)
}
}
/// All things that implement the required traits can be treated as `Device`.
impl<T: Render + Handle + Ports + Send + Sync> Device for T {}
/// A device dynamicammy composed of state and handlers.
pub struct DynamicDevice<T> {
pub state: Arc<Mutex<T>>,
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect)->Usually<Rect> + Send>>,
pub handle: Arc<Mutex<Box<dyn FnMut(&mut T, &AppEvent)->Usually<bool> + Send>>>,
pub process: Arc<Mutex<Box<dyn FnMut(&mut T, &Client, &ProcessScope)->Control + Send>>>,
pub client: Option<DynamicAsyncClient>
}
impl<T> Handle for DynamicDevice<T> {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
self.handle.lock().unwrap()(&mut *self.state.lock().unwrap(), event)
}
}
impl<T: Send> Render for DynamicDevice<T> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
self.render.lock().unwrap()(&*self.state.lock().unwrap(), buf, area)
}
}
impl<T: Ports + Send + Sync + 'static> Ports for DynamicDevice<T> {}
impl<T: Send + Sync + 'static> DynamicDevice<T> {
pub fn new <'a, R, H, P> (render: R, handle: H, process: P, state: T) -> Self where
R: FnMut(&T, &mut Buffer, Rect) -> Usually<Rect> + Send + 'static,
H: FnMut(&mut T, &AppEvent) -> Usually<bool> + Send + 'static,
P: FnMut(&mut T, &Client, &ProcessScope) -> Control + Send + 'static,
{
Self {
state: Arc::new(Mutex::new(state)),
render: Mutex::new(Box::new(render)),
handle: Arc::new(Mutex::new(Box::new(handle))),
process: Arc::new(Mutex::new(Box::new(process))),
client: None,
}
}
pub fn state (&self) -> std::sync::MutexGuard<'_, T> {
self.state.lock().unwrap()
}
pub fn activate (mut self, client: Client) -> Usually<Self> {
self.client = Some(client.activate_async(Notifications(Box::new({
let state = self.state.clone();
let handle = self.handle.clone();
move|event|{
let mut state = state.lock().unwrap();
let mut handle = handle.lock().unwrap();
handle(&mut state, &event).unwrap();
}
}) as Box<dyn Fn(AppEvent) + Send + Sync>), ClosureProcessHandler::new(Box::new({
let state = self.state.clone();
let process = self.process.clone();
move|client: &Client, scope: &ProcessScope|{
let mut state = state.lock().unwrap();
let mut process = process.lock().unwrap();
(process)(&mut state, client, scope)
}
}) as BoxedProcessHandler))?);
Ok(self)
}
}