mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
279 lines
9.4 KiB
Rust
279 lines
9.4 KiB
Rust
mod plugin;
|
|
pub use self::plugin::*;
|
|
mod sampler;
|
|
pub use self::sampler::*;
|
|
|
|
use crate::prelude::*;
|
|
|
|
pub struct Chain {
|
|
pub name: String,
|
|
pub focused: bool,
|
|
pub focus: usize,
|
|
pub items: Vec<Box<dyn Device>>,
|
|
pub view: ChainView
|
|
}
|
|
|
|
pub enum ChainView {
|
|
Hidden,
|
|
Compact,
|
|
Row,
|
|
Column,
|
|
}
|
|
|
|
impl Chain {
|
|
pub fn new (name: &str, items: Option<Vec<Box<dyn Device>>>) -> Result<DynamicDevice<Self>, Box<dyn Error>> {
|
|
Ok(DynamicDevice::new(render, handle, process, Self {
|
|
name: name.into(),
|
|
focused: false,
|
|
focus: 0,
|
|
items: items.unwrap_or_else(||vec![]),
|
|
view: ChainView::Column
|
|
}))
|
|
}
|
|
}
|
|
|
|
impl PortList for Chain {
|
|
fn midi_ins (&self) -> Usually<Vec<String>> {
|
|
if let Some(device) = self.items.get(0) {
|
|
device.midi_ins()
|
|
} else {
|
|
Ok(vec![])
|
|
}
|
|
}
|
|
fn audio_outs (&self) -> Usually<Vec<String>> {
|
|
if let Some(device) = self.items.get(self.items.len().saturating_sub(1)) {
|
|
device.audio_outs()
|
|
} else {
|
|
Ok(vec![])
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn process (_: &mut Chain, _: &Client, _: &ProcessScope) -> Control {
|
|
Control::Continue
|
|
}
|
|
|
|
pub fn render (state: &Chain, buf: &mut Buffer, area: Rect)
|
|
-> Usually<Rect>
|
|
{
|
|
let Rect { x, y, .. } = area;
|
|
let selected = Some(if state.focused {
|
|
Style::default().green().not_dim()
|
|
} else {
|
|
Style::default().green().dim()
|
|
});
|
|
Ok(match state.view {
|
|
ChainView::Hidden => Rect { x, y, width: 0, height: 0 },
|
|
ChainView::Compact => {
|
|
let area = Rect { x, y, width: (state.name.len() + 4) as u16, height: 3 };
|
|
buf.set_string(area.x + 2, area.y + 1, &state.name, Style::default());
|
|
draw_box_styled(buf, area, selected)
|
|
},
|
|
ChainView::Row => {
|
|
draw_box_styled(buf, area, selected);
|
|
let (area, _) = Row::draw(buf, area, &state.items, 0)?;
|
|
area
|
|
},
|
|
ChainView::Column => {
|
|
draw_as_column(state, buf, area, selected)?
|
|
},
|
|
})
|
|
//let area = Rect { x, y, width: 40, height: 30 };
|
|
//let mut y = area.y;
|
|
//buf.set_string(area.x, y, "│", Style::default().black());
|
|
//buf.set_string(area.x + area.width - 1, y, "│", Style::default().black());
|
|
//buf.set_string(area.x + 2, y, "Input...", Style::default().dim());
|
|
//let mut x = 0u16;
|
|
//for (i, device) in state.items.iter().enumerate() {
|
|
//let result = device.render(buf, Rect {
|
|
//x: area.x,
|
|
//y,
|
|
//width: area.width,
|
|
//height: 21//area.height.saturating_sub(y)
|
|
//})?;
|
|
//if i == state.focus {
|
|
//if state.focused {
|
|
//draw_box_styled(buf, result, Some(Style::default().green().not_dim()))
|
|
//} else {
|
|
//draw_box_styled_dotted(buf, result, Some(Style::default().green().dim()))
|
|
//};
|
|
//};
|
|
////let result = Rect { x: 0, y: 0, width: result.width, height: 21 };
|
|
//x = x.max(result.width);
|
|
//y = y + result.height;
|
|
////y = y + 1;
|
|
//buf.set_string(area.x, y, "│", Style::default().black());
|
|
//buf.set_string(area.x + area.width - 1, y, "│", Style::default().black());
|
|
//buf.set_string(area.x + 2, y, " Patch in ┐ │ └ Patch out", Style::default().dim());
|
|
//y = y + 1;
|
|
////buf.set_string(area.x, y, format!("{y}---BOT---"), Style::default().red());
|
|
////buf.set_string(area.x + area.width - 1, area.y + 1, "│", Style::default().black());
|
|
////buf.set_string(area.x + 2, y, "Patch...", Style::default().dim());
|
|
//}
|
|
//Ok(draw_box(buf, Rect {
|
|
//x: area.x,
|
|
//y: area.y,
|
|
//width: area.width,
|
|
//height: y - area.y,
|
|
//}))
|
|
}
|
|
|
|
pub fn draw_as_row (
|
|
state: &Chain, buf: &mut Buffer, area: Rect, _: Option<Style>
|
|
) -> Usually<(Rect, Vec<Rect>)> {
|
|
let Rect { mut x, y, width, height } = area;
|
|
x = x + 1;
|
|
let mut h = 0u16;
|
|
let mut frames = vec![];
|
|
for (i, device) in state.items.iter().enumerate() {
|
|
let mut x2 = 1u16;
|
|
let mut y2 = 1u16;
|
|
for port in device.midi_ins()?.iter() {
|
|
port.blit(buf, x, y + y2, Some(Style::default()));
|
|
x2 = x2.max(port.len() as u16);
|
|
y2 = y2 + 1;
|
|
}
|
|
for port in device.audio_ins()?.iter() {
|
|
port.blit(buf, x, y + y2, Some(Style::default()));
|
|
x2 = x2.max(port.len() as u16);
|
|
y2 = y2 + 1;
|
|
}
|
|
let width = width.saturating_sub(x).saturating_sub(x2);
|
|
let style = Some(if i == state.focus {
|
|
if state.focused {
|
|
Style::default().green().not_dim()
|
|
} else {
|
|
Style::default().green().dim()
|
|
}
|
|
} else {
|
|
Style::default().dim()
|
|
});
|
|
lozenge_left(buf, x + x2, y, height, style);
|
|
let frame = device.render(buf, Rect { x: x + x2, y, width, height })?;
|
|
lozenge_right(buf, x + x2 + frame.width - 1, y, height, style);
|
|
let mut y2 = 1u16;
|
|
for port in device.midi_outs()?.iter() {
|
|
port.blit(buf, x + x2 + frame.width, y + y2, Some(Style::default()));
|
|
x2 = x2.max(port.len() as u16);
|
|
y2 = y2 + 1;
|
|
}
|
|
for port in device.audio_outs()?.iter() {
|
|
port.blit(buf, x + x2 + frame.width, y + y2, Some(Style::default()));
|
|
x2 = x2.max(port.len() as u16);
|
|
y2 = y2 + 1;
|
|
}
|
|
frames.push(frame);
|
|
h = h.max(frame.height);
|
|
x = x + frame.width;
|
|
}
|
|
Ok((area, frames))
|
|
}
|
|
|
|
pub fn draw_as_column (
|
|
state: &Chain, buf: &mut Buffer, area: Rect, selected: Option<Style>
|
|
) -> Usually<Rect> {
|
|
let Rect { x, mut y, width, height } = area;
|
|
//let (area, areas) = Column::draw(buf, area, &state.items, 0)?;
|
|
let mut w = 0u16;
|
|
let mut frames = vec![];
|
|
for device in state.items.iter() {
|
|
let style_midi = Style::default().black().bold().on_green();
|
|
let style_audio = Style::default().black().bold().on_red();
|
|
let midi_ins = device.midi_ins()?;
|
|
let midi_outs = device.midi_outs()?;
|
|
let audio_ins = device.audio_ins()?;
|
|
let audio_outs = device.audio_outs()?;
|
|
y = y + midi_ins.len() as u16;
|
|
let frame = device.render(buf, Rect {
|
|
x, y, width, height: height.saturating_sub(y)
|
|
})?;
|
|
frames.push(frame);
|
|
w = w.max(frame.width);
|
|
y = y - midi_ins.len() as u16;
|
|
for port in midi_ins.iter() {
|
|
buf.set_string(x + frame.width - 10, y, &format!(" <i> MIDI {port} "), style_midi);
|
|
y = y + 1;
|
|
}
|
|
y = y - audio_ins.len() as u16;
|
|
for port in audio_ins.iter() {
|
|
buf.set_string(x + frame.width - 10, y, &format!(" <i> MIDI {port} "), style_audio);
|
|
y = y + 1;
|
|
}
|
|
y = y + frame.height - 1;
|
|
y = y + midi_outs.len() as u16;
|
|
for port in midi_outs.iter() {
|
|
buf.set_string(x + 2, y, &format!(" <o> MIDI {port} "), style_midi);
|
|
y = y + 1;
|
|
}
|
|
y = y + audio_outs.len() as u16;
|
|
for port in audio_outs.iter() {
|
|
buf.set_string(x + 2, y, &format!(" <o> Audio {port} "), style_audio);
|
|
y = y + 1;
|
|
}
|
|
}
|
|
draw_box_styled(buf, frames[state.focus], selected);
|
|
Ok(area)
|
|
}
|
|
|
|
impl Focus for Chain {
|
|
fn unfocus (&mut self) {
|
|
self.focused = false
|
|
}
|
|
fn focused (&self) -> Option<&Box<dyn Device>> {
|
|
match self.focused {
|
|
true => self.items.get(self.focus),
|
|
false => None
|
|
}
|
|
}
|
|
fn focused_mut (&mut self) -> Option<&mut Box<dyn Device>> {
|
|
match self.focused {
|
|
true => self.items.get_mut(self.focus),
|
|
false => None
|
|
}
|
|
}
|
|
fn handle_focus (&mut self, event: &FocusEvent) -> Usually<bool> {
|
|
Ok(match event {
|
|
FocusEvent::Backward => {
|
|
if self.focus == 0 {
|
|
self.focus = self.items.len();
|
|
}
|
|
self.focus = self.focus - 1;
|
|
true
|
|
},
|
|
FocusEvent::Forward => {
|
|
self.focus = self.focus + 1;
|
|
if self.focus >= self.items.len() {
|
|
self.focus = 0;
|
|
}
|
|
true
|
|
},
|
|
FocusEvent::Inward => {
|
|
self.focused = true;
|
|
self.items[self.focus].handle(&AppEvent::Focus)?;
|
|
true
|
|
},
|
|
FocusEvent::Outward => {
|
|
if self.focused {
|
|
self.focused = false;
|
|
self.items[self.focus].handle(&AppEvent::Blur)?;
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
},
|
|
})
|
|
}
|
|
}
|
|
|
|
pub fn handle (state: &mut Chain, event: &AppEvent) -> Usually<bool> {
|
|
handle_focus(state, event, keymap!(Chain {
|
|
[Up, NONE, "focus_up", "focus row above",
|
|
|s: &mut Chain|s.handle_focus(&FocusEvent::Backward)],
|
|
[Down, NONE, "focus_down", "focus row below",
|
|
|s: &mut Chain|s.handle_focus(&FocusEvent::Forward)],
|
|
[Enter, NONE, "focus_down", "focus row below",
|
|
|s: &mut Chain|s.handle_focus(&FocusEvent::Inward)],
|
|
[Esc, NONE, "focus_down", "focus row below",
|
|
|s: &mut Chain|s.handle_focus(&FocusEvent::Outward)]
|
|
}))
|
|
}
|