mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 20:26:42 +01:00
257 lines
8.3 KiB
Rust
257 lines
8.3 KiB
Rust
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: 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,
|
|
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 { mut x, mut y, width, height } = 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 => {
|
|
let (area, areas) = Row::draw(buf, area, &state.items, 0)?;
|
|
draw_box_styled(buf, area, selected)
|
|
},
|
|
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, selected: Option<Style>
|
|
) -> Usually<(Rect, Vec<Rect>)> {
|
|
let Rect { mut x, mut y, width, height } = area;
|
|
let mut w = 0u16;
|
|
let mut h = 0u16;
|
|
let mut frames = vec![];
|
|
for (i, device) in state.items.iter().enumerate() {
|
|
let midi_ins = device.midi_ins()?;
|
|
let midi_outs = device.midi_outs()?;
|
|
let audio_ins = device.audio_ins()?;
|
|
let audio_outs = device.audio_outs()?;
|
|
let width = width.saturating_sub(x);
|
|
let frame = device.render(buf, Rect { x, y, width, height })?;
|
|
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 { mut x, mut y, width, height } = area;
|
|
//let (area, areas) = Column::draw(buf, area, &state.items, 0)?;
|
|
let mut w = 0u16;
|
|
let mut y = area.y;
|
|
let mut frames = vec![];
|
|
for (i, device) in state.items.iter().enumerate() {
|
|
|
|
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::default().black().bold().on_green());
|
|
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::default().black().bold().on_red());
|
|
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::default().black().bold().on_green());
|
|
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::default().black().bold().on_red());
|
|
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)]
|
|
}))
|
|
}
|