diff --git a/src/device.rs b/src/device.rs index d17939c6..1ee7cd1c 100644 --- a/src/device.rs +++ b/src/device.rs @@ -23,90 +23,6 @@ use ::jack::{AudioIn, AudioOut, MidiIn, MidiOut, Port, PortSpec, Client}; pub trait Device: Render + Handle + DevicePorts + Send + Sync {} -impl 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 { - 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 { - Ok(Rect { x: 0, y: 0, width: 0, height: 0 }) - } -} - -pub trait DevicePorts { - fn audio_ins (&self) -> Usually> { - Ok(vec![]) - } - fn audio_outs (&self) -> Usually> { - Ok(vec![]) - } - fn midi_ins (&self) -> Usually> { - Ok(vec![]) - } - fn midi_outs (&self) -> Usually> { - 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 { - fn render (&self, b: &mut Buffer, a: Rect) -> Usually { - (**self).render(b, a) - } -} - -pub struct DevicePort { - pub name: String, - pub port: Port, - pub connect: Vec, -} - -impl DevicePort { - pub fn new (client: &Client, name: &str, connect: &[&str]) -> Usually { - 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> { 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 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 { + 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 { + 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