i dont know why that worked

This commit is contained in:
🪞👃🪞 2024-06-16 13:58:46 +03:00
parent 4ae62c5bc2
commit b73aa8a0dc
17 changed files with 552 additions and 481 deletions

View file

@ -16,8 +16,6 @@ pub use self::mixer::Mixer;
pub use self::looper::Looper;
pub use self::plugin::Plugin;
use crate::prelude::*;
use std::sync::mpsc;
use crossterm::event;
pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn Error>> {
@ -43,7 +41,7 @@ pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn E
}) => {
exited.store(true, Ordering::Relaxed);
},
_ => if device.lock().unwrap().handle(&EngineEvent::Input(event)).is_err() {
_ => if device.lock().unwrap().handle(&AppEvent::Input(event)).is_err() {
break
}
}
@ -90,7 +88,7 @@ pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn E
}
pub trait Device: Send + Sync {
fn handle (&mut self, _event: &EngineEvent) -> Usually<()> {
fn handle (&mut self, _event: &AppEvent) -> Usually<()> {
Ok(())
}
fn render (&self, _buffer: &mut Buffer, _area: Rect) -> Usually<Rect> {
@ -100,28 +98,32 @@ pub trait Device: Send + Sync {
}
pub struct DynamicDevice<T> {
pub state: Mutex<T>,
pub state: Arc<Mutex<T>>,
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect)->Usually<Rect> + Send>>,
pub handle: Mutex<Box<dyn FnMut(&mut T, &EngineEvent)->Usually<()> + Send>>,
pub process: Mutex<Box<dyn FnMut(&mut T) + Send>>
pub handle: Arc<Mutex<Box<dyn FnMut(&mut T, &AppEvent)->Usually<()> + Send>>>,
pub process: Arc<Mutex<Box<dyn FnMut(&mut T, &Client, &ProcessScope)->Control + Send>>>,
}
impl<T: Send + Sync> Device for DynamicDevice<T> {
fn handle (&mut self, event: &AppEvent) -> Usually<()> {
self.handle.lock().unwrap()(&mut *self.state.lock().unwrap(), event)
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
self.render.lock().unwrap()(&*self.state.lock().unwrap(), buf, area)
}
}
impl<T> DynamicDevice<T> {
fn new <'a, R, H, P> (
render: R,
handle: H,
process: P,
state: T
) -> Self where
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, &EngineEvent) -> Result<(), Box<dyn Error>> + Send + 'static,
P: FnMut(&mut T) + Send + 'static
H: FnMut(&mut T, &AppEvent) -> Result<(), Box<dyn Error>> + 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: Mutex::new(Box::new(handle)),
process: Mutex::new(Box::new(process)),
state: Mutex::new(state),
handle: Arc::new(Mutex::new(Box::new(handle))),
process: Arc::new(Mutex::new(Box::new(process))),
}
}
fn state (&self) -> std::sync::MutexGuard<'_, T> {
@ -129,12 +131,29 @@ impl<T> DynamicDevice<T> {
}
}
impl<T: Send + Sync> Device for DynamicDevice<T> {
fn handle (&mut self, event: &EngineEvent) -> Usually<()> {
self.handle.lock().unwrap()(&mut *self.state.lock().unwrap(), event)
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
self.render.lock().unwrap()(&*self.state.lock().unwrap(), buf, area)
impl<T: NotificationHandler + 'static> DynamicDevice<T> {
fn activate (&self, client: Client) -> Usually<()> {
let notifications = {
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()
}
};
let notifications = Notifications(Box::new(notifications));
let handler = 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);
client.activate_async(notifications, handler)?;
Ok(())
}
}
@ -150,16 +169,9 @@ impl WidgetRef for dyn Device {
}
}
pub struct Engine {
exited: Arc<AtomicBool>,
sender: Sender<Event>,
receiver: Receiver<Event>,
pub jack_client: Jack<Notifications>,
}
#[derive(Debug)]
pub enum EngineEvent {
/// An input event that must be handled.
pub enum AppEvent {
/// Terminal input
Input(::crossterm::event::Event),
/// Update values but not the whole form.
Update,
@ -169,6 +181,8 @@ pub enum EngineEvent {
Focus,
/// Device loses focus
Blur,
/// JACK notification
Jack(JackEvent)
}
pub fn activate_jack_client <N: NotificationHandler + Sync + 'static> (
@ -191,40 +205,64 @@ fn panic_hook (info: &std::panic::PanicInfo) {
.unwrap();
}
pub struct Notifications;//(mpsc::Sender<self::Event>);
#[derive(Debug)]
pub enum JackEvent {
ThreadInit,
Shutdown,
Freewheel,
SampleRate,
ClientRegistration,
PortRegistration,
PortRename,
PortsConnected,
GraphReorder,
XRun,
}
impl NotificationHandler for Notifications {
pub struct Notifications<T: Fn(AppEvent) + Send>(T);
impl<T: Fn(AppEvent) + Send> NotificationHandler for Notifications<T> {
fn thread_init (&self, _: &Client) {
self.0(AppEvent::Jack(JackEvent::ThreadInit));
}
fn shutdown (&mut self, status: ClientStatus, reason: &str) {
self.0(AppEvent::Jack(JackEvent::Shutdown));
}
fn freewheel (&mut self, _: &Client, is_enabled: bool) {
self.0(AppEvent::Jack(JackEvent::Freewheel));
}
fn sample_rate (&mut self, _: &Client, _: Frames) -> Control {
self.0(AppEvent::Jack(JackEvent::SampleRate));
Control::Quit
}
fn client_registration (&mut self, _: &Client, name: &str, is_reg: bool) {
self.0(AppEvent::Jack(JackEvent::ClientRegistration));
}
fn port_registration (&mut self, _: &Client, port_id: PortId, is_reg: bool) {
self.0(AppEvent::Jack(JackEvent::PortRegistration));
}
fn port_rename (&mut self, _: &Client, id: PortId, old: &str, new: &str) -> Control {
self.0(AppEvent::Jack(JackEvent::PortRename));
Control::Continue
}
fn ports_connected (&mut self, _: &Client, id_a: PortId, id_b: PortId, are: bool) {
self.0(AppEvent::Jack(JackEvent::PortsConnected));
}
fn graph_reorder (&mut self, _: &Client) -> Control {
self.0(AppEvent::Jack(JackEvent::GraphReorder));
Control::Continue
}
fn xrun (&mut self, _: &Client) -> Control {
self.0(AppEvent::Jack(JackEvent::XRun));
Control::Continue
}
}