wip: show single-device chain in launcher

This commit is contained in:
🪞👃🪞 2024-06-24 22:55:24 +03:00
parent 1f194dafd8
commit 2f52e97c58
7 changed files with 315 additions and 239 deletions

View file

@ -23,90 +23,6 @@ use ::jack::{AudioIn, AudioOut, MidiIn, MidiOut, Port, PortSpec, Client};
pub trait Device: Render + Handle + DevicePorts + Send + Sync {}
impl<T: Render + Handle + DevicePorts + Send + Sync> Device for T {}
pub trait Handle {
// Returns Ok(true) if the device handled the event.
// This is the mechanism which allows nesting of components;.
fn handle (&mut self, _e: &AppEvent) -> Usually<bool> {
Ok(false)
}
}
pub trait Render {
// Returns space used by component.
// This is insufficient but for the most basic dynamic layout algorithms.
fn render (&self, _b: &mut Buffer, _a: Rect) -> Usually<Rect> {
Ok(Rect { x: 0, y: 0, width: 0, height: 0 })
}
}
pub trait DevicePorts {
fn audio_ins (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn audio_outs (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn midi_ins (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn midi_outs (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn connect (&mut self, connect: bool, source: &str, target: &str)
-> Usually<()>
{
Ok(())
}
fn connect_all (&mut self, connections: &[(bool, &str, &str)])
-> Usually<()>
{
for (connect, source, target) in connections.iter() {
self.connect(*connect, source, target)?;
}
Ok(())
}
}
impl Render for Box<dyn Device> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
(**self).render(b, a)
}
}
pub struct DevicePort<T: PortSpec> {
pub name: String,
pub port: Port<T>,
pub connect: Vec<String>,
}
impl<T: PortSpec + Default> DevicePort<T> {
pub fn new (client: &Client, name: &str, connect: &[&str]) -> Usually<Self> {
let mut connects = vec![];
for port in connect.iter() {
connects.push(port.to_string());
}
Ok(Self {
name: name.to_string(),
port: client.register_port(name, T::default())?,
connect: connects,
})
}
}
impl WidgetRef for &dyn Render {
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
Render::render(*self, buf, area).expect("Failed to render device.");
}
}
impl WidgetRef for dyn Render {
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
Render::render(self, buf, area).expect("Failed to render device.");
}
}
pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn Error>> {
let device = Arc::new(Mutex::new(device));
let exited = Arc::new(AtomicBool::new(false));
@ -176,6 +92,105 @@ pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn E
Ok(())
}
impl<T: Render + Handle + DevicePorts + Send + Sync> Device for T {}
pub trait Handle {
// Handle an input event.
// Returns Ok(true) if the device handled the event.
// This is the mechanism which allows nesting of components;.
fn handle (&mut self, _e: &AppEvent) -> Usually<bool> {
Ok(false)
}
}
pub trait Render {
// Render something to an area of the buffer.
// Returns area used by component.
// This is insufficient but for the most basic dynamic layout algorithms.
fn render (&self, _b: &mut Buffer, _a: Rect) -> Usually<Rect> {
Ok(Rect { x: 0, y: 0, width: 0, height: 0 })
}
}
pub trait Blit {
// Render something to X, Y coordinates in a buffer, ignoring width/height.
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
}
impl<T: AsRef<str>> Blit for T {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
}
}
}
pub trait DevicePorts {
fn audio_ins (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn audio_outs (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn midi_ins (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn midi_outs (&self) -> Usually<Vec<String>> {
Ok(vec![])
}
fn connect (&mut self, connect: bool, source: &str, target: &str)
-> Usually<()>
{
Ok(())
}
fn connect_all (&mut self, connections: &[(bool, &str, &str)])
-> Usually<()>
{
for (connect, source, target) in connections.iter() {
self.connect(*connect, source, target)?;
}
Ok(())
}
}
impl Render for Box<dyn Device> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
(**self).render(b, a)
}
}
pub struct DevicePort<T: PortSpec> {
pub name: String,
pub port: Port<T>,
pub connect: Vec<String>,
}
impl<T: PortSpec + Default> DevicePort<T> {
pub fn new (client: &Client, name: &str, connect: &[&str]) -> Usually<Self> {
let mut connects = vec![];
for port in connect.iter() {
connects.push(port.to_string());
}
Ok(Self {
name: name.to_string(),
port: client.register_port(name, T::default())?,
connect: connects,
})
}
}
impl WidgetRef for &dyn Render {
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
Render::render(*self, buf, area).expect("Failed to render device.");
}
}
impl WidgetRef for dyn Render {
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
Render::render(self, buf, area).expect("Failed to render device.");
}
}
pub struct DynamicDevice<T> {
pub state: Arc<Mutex<T>>,
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect)->Usually<Rect> + Send>>,