wip: render: remove render! macro

This commit is contained in:
🪞👃🪞 2024-09-04 03:20:58 +03:00
parent bf165c6be1
commit 1d4db3c629
13 changed files with 337 additions and 304 deletions

View file

@ -106,8 +106,8 @@ pub trait ExitableComponent<T, U>: Exit + Component<T, U> {
impl<E: Exit + Component<T, U>, T, U> ExitableComponent<T, U> for E {} impl<E: Exit + Component<T, U>, T, U> ExitableComponent<T, U> for E {}
/// Run the main loop. /// Run the main loop.
pub fn run <'a, R> (state: Arc<RwLock<R>>) -> Usually<Arc<RwLock<R>>> pub fn run <R> (state: Arc<RwLock<R>>) -> Usually<Arc<RwLock<R>>>
where R: Render<TuiOutput<'a>, Rect> + Handle + Sized + 'static where R: for <'a> Render<TuiOutput<'a>, Rect> + Handle + Sized + 'static
{ {
let exited = Arc::new(AtomicBool::new(false)); let exited = Arc::new(AtomicBool::new(false));
let _input_thread = input_thread(&exited, &state); let _input_thread = input_thread(&exited, &state);

View file

@ -56,7 +56,7 @@ impl<'a, T, U> Collect<'a, T, U> for Split<'a, T, U> {
impl<'a> Render<TuiOutput<'a>, Rect> for Split<'a, TuiOutput<'a>, Rect> { impl<'a> Render<TuiOutput<'a>, Rect> for Split<'a, TuiOutput<'a>, Rect> {
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> { fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
Ok(Some(self.render_areas(to)?.0)) Ok(None)//Rect::default())//Some(self.render_areas(to)?.0))
} }
} }

View file

@ -1,6 +1,7 @@
use crate::*; use crate::*;
pub(crate) use ratatui::buffer::Cell; pub(crate) use ratatui::buffer::Cell;
use ratatui::backend::Backend;
pub struct TuiOutput<'a> { pub struct TuiOutput<'a> {
pub buffer: &'a mut Buffer, pub buffer: &'a mut Buffer,
@ -8,24 +9,29 @@ pub struct TuiOutput<'a> {
} }
/// Main thread render loop /// Main thread render loop
pub fn tui_render_thread <'a: 'static, T> ( pub fn tui_render_thread <T> (exited: &Arc<AtomicBool>, device: &Arc<RwLock<T>>)
exited: &Arc<AtomicBool>, device: &Arc<RwLock<T>>, -> Usually<JoinHandle<()>>
) -> Usually<JoinHandle<()>> where where
T: Render<TuiOutput<'a>, Rect> + 'static T: for <'a> Render<TuiOutput<'a>, Rect> + 'static
{ {
let exited = exited.clone(); let exited = exited.clone();
let device = device.clone(); let device = device.clone();
let mut terminal = ratatui::Terminal::new(CrosstermBackend::new(stdout()))?; let mut backend = CrosstermBackend::new(stdout());
let sleep = Duration::from_millis(20); let area = backend.size()?;
let mut buffers = [Buffer::empty(area), Buffer::empty(area)];
let mut index = 0;
let sleep = Duration::from_millis(20);
Ok(spawn(move || { Ok(spawn(move || {
let draw = |frame: &'a mut ratatui::Frame|{
let area = frame.size();
let buffer = frame.buffer_mut();
device.render(&mut TuiOutput { buffer, area }).expect("Failed to render content");
};
loop { loop {
if let Ok(_) = device.try_read() { if let Ok(device) = device.try_read() {
terminal.draw(draw).expect("Failed to render frame"); let mut target = TuiOutput { buffer: &mut buffers[index], area };
device.render(&mut target).expect("render failed");
let previous_buffer = &buffers[1 - index];
let current_buffer = &buffers[index];
let updates = previous_buffer.diff(current_buffer);
backend.draw(updates.into_iter());
buffers[1 - index].reset();
index = 1 - index;
} }
if exited.fetch_and(true, Ordering::Relaxed) { if exited.fetch_and(true, Ordering::Relaxed) {
break break

View file

@ -1,17 +1,19 @@
use crate::*; use crate::*;
render!(Mixer |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for Mixer {
let mut x = 0; fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
for channel in self.tracks.iter() { let mut x = 0;
x = x + channel.render(buf, Rect { for channel in self.tracks.iter() {
x: area.x + x, x = x + channel.render(buf, Rect {
y: area.y, x: area.x + x,
width: area.width, y: area.y,
height: area.height width: area.width,
})?.width; height: area.height
if x >= area.width { })?.width;
break if x >= area.width {
break
}
} }
Ok(area)
} }
Ok(area) }
});

View file

@ -9,9 +9,43 @@ pub struct Plugin {
pub mapping: bool, pub mapping: bool,
pub ports: JackPorts, pub ports: JackPorts,
} }
render!(Plugin = render_plugin);
handle!(Plugin |self, e| handle_keymap(self, e, KEYMAP_PLUGIN)); handle!(Plugin |self, e| handle_keymap(self, e, KEYMAP_PLUGIN));
process!(Plugin = Plugin::process); process!(Plugin = Plugin::process);
impl<'a> Render<TuiOutput<'a>, Rect> for Plugin {
fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
let Rect { x, y, height, .. } = area;
let mut width = 20u16;
match &state.plugin {
Some(PluginKind::LV2(LV2Plugin { port_list, instance, .. })) => {
let start = state.selected.saturating_sub((height as usize / 2).saturating_sub(1));
let end = start + height as usize - 2;
//draw_box(buf, Rect { x, y, width, height });
for i in start..end {
if let Some(port) = port_list.get(i) {
let value = if let Some(value) = instance.control_input(port.index) {
value
} else {
port.default_value
};
//let label = &format!("C·· M·· {:25} = {value:.03}", port.name);
let label = &format!("{:25} = {value:.03}", port.name);
width = width.max(label.len() as u16 + 4);
label.blit(buf, x + 2, y + 1 + i as u16 - start as u16, if i == state.selected {
Some(Style::default().green())
} else {
None
})?;
} else {
break
}
}
},
_ => {}
};
draw_header(state, buf, area.x, area.y, width)?;
Ok(Some(Rect { width, ..area }))
}
}
/// Supported plugin formats. /// Supported plugin formats.
pub enum PluginKind { pub enum PluginKind {
@ -21,7 +55,6 @@ pub enum PluginKind {
}, },
VST3, VST3,
} }
impl Plugin { impl Plugin {
pub fn new_lv2 (name: &str, path: &str) -> Usually<JackDevice> { pub fn new_lv2 (name: &str, path: &str) -> Usually<JackDevice> {
let plugin = LV2Plugin::new(path)?; let plugin = LV2Plugin::new(path)?;
@ -102,43 +135,6 @@ impl Plugin {
Control::Continue Control::Continue
} }
} }
pub fn render_plugin (state: &Plugin, buf: &mut Buffer, area: Rect)
-> Usually<Rect>
{
let Rect { x, y, height, .. } = area;
let mut width = 20u16;
match &state.plugin {
Some(PluginKind::LV2(LV2Plugin { port_list, instance, .. })) => {
let start = state.selected.saturating_sub((height as usize / 2).saturating_sub(1));
let end = start + height as usize - 2;
//draw_box(buf, Rect { x, y, width, height });
for i in start..end {
if let Some(port) = port_list.get(i) {
let value = if let Some(value) = instance.control_input(port.index) {
value
} else {
port.default_value
};
//let label = &format!("C·· M·· {:25} = {value:.03}", port.name);
let label = &format!("{:25} = {value:.03}", port.name);
width = width.max(label.len() as u16 + 4);
label.blit(buf, x + 2, y + 1 + i as u16 - start as u16, if i == state.selected {
Some(Style::default().green())
} else {
None
})?;
} else {
break
}
}
},
_ => {}
};
draw_header(state, buf, area.x, area.y, width)?;
Ok(Rect { width, ..area })
}
fn draw_header (state: &Plugin, buf: &mut Buffer, x: u16, y: u16, w: u16) -> Usually<Rect> { fn draw_header (state: &Plugin, buf: &mut Buffer, x: u16, y: u16, w: u16) -> Usually<Rect> {
let style = Style::default().gray(); let style = Style::default().gray();
let label1 = format!(" {}", state.name); let label1 = format!(" {}", state.name);

View file

@ -22,40 +22,42 @@ pub struct AddSampleModal {
exit!(AddSampleModal); exit!(AddSampleModal);
render!(AddSampleModal |self,buf,area|{ impl<'a> Render<TuiOutput<'a>, Rect> for AddSampleModal {
make_dim(buf); fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
let area = center_box( make_dim(to.buffer);
area, let area = center_box(
64.max(area.width.saturating_sub(8)), to.area,
20.max(area.width.saturating_sub(8)), 64.max(to.area.width.saturating_sub(8)),
); 20.max(to.area.width.saturating_sub(8)),
fill_fg(buf, area, Color::Reset); );
fill_bg(buf, area, Nord::bg_lo(true, true)); fill_fg(buf, area, Color::Reset);
fill_char(buf, area, ' '); fill_bg(buf, area, Nord::bg_lo(true, true));
format!("{}", &self.dir.to_string_lossy()) fill_char(buf, area, ' ');
.blit(buf, area.x+2, area.y+1, Some(Style::default().bold()))?; format!("{}", &self.dir.to_string_lossy())
"Select sample:" .blit(buf, area.x+2, area.y+1, Some(Style::default().bold()))?;
.blit(buf, area.x+2, area.y+2, Some(Style::default().bold()))?; "Select sample:"
for (i, (is_dir, name)) in self.subdirs.iter() .blit(buf, area.x+2, area.y+2, Some(Style::default().bold()))?;
.map(|path|(true, path)) for (i, (is_dir, name)) in self.subdirs.iter()
.chain(self.files.iter().map(|path|(false, path))) .map(|path|(true, path))
.enumerate() .chain(self.files.iter().map(|path|(false, path)))
.skip(self.offset) .enumerate()
{ .skip(self.offset)
if i >= area.height as usize - 4 { {
break if i >= area.height as usize - 4 {
break
}
let t = if is_dir { "" } else { "" };
let line = format!("{t} {}", name.to_string_lossy());
let line = &line[..line.len().min(area.width as usize - 4)];
line.blit(buf, area.x + 2, area.y + 3 + i as u16, Some(if i == self.cursor {
Style::default().green()
} else {
Style::default().white()
}))?;
} }
let t = if is_dir { "" } else { "" }; Lozenge(Style::default()).draw(buf, area)
let line = format!("{t} {}", name.to_string_lossy());
let line = &line[..line.len().min(area.width as usize - 4)];
line.blit(buf, area.x + 2, area.y + 3 + i as u16, Some(if i == self.cursor {
Style::default().green()
} else {
Style::default().white()
}))?;
} }
Lozenge(Style::default()).draw(buf, area) }
});
handle!(AddSampleModal |self,e|{ handle!(AddSampleModal |self,e|{
if handle_keymap(self, e, KEYMAP_ADD_SAMPLE)? { if handle_keymap(self, e, KEYMAP_ADD_SAMPLE)? {

View file

@ -15,30 +15,34 @@ pub struct Sampler {
} }
process!(Sampler = Sampler::process); process!(Sampler = Sampler::process);
handle!(Sampler |self, event| handle_keymap(self, event, KEYMAP_SAMPLER)); handle!(Sampler |self, event| handle_keymap(self, event, KEYMAP_SAMPLER));
render!(Sampler |self, buf, area| {
let Rect { x, y, height, .. } = area; impl<'a> Render<TuiOutput<'a>, Rect> for Sampler {
let style = Style::default().gray(); fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
let title = format!(" {} ({})", self.name, self.voices.read().unwrap().len()); let Rect { x, y, height, .. } = area;
title.blit(buf, x+1, y, Some(style.white().bold().not_dim()))?; let style = Style::default().gray();
let mut width = title.len() + 2; let title = format!(" {} ({})", self.name, self.voices.read().unwrap().len());
let mut y1 = 1; title.blit(buf, x+1, y, Some(style.white().bold().not_dim()))?;
let mut j = 0; let mut width = title.len() + 2;
for (note, sample) in self.mapped.iter() let mut y1 = 1;
.map(|(note, sample)|(Some(note), sample)) let mut j = 0;
.chain(self.unmapped.iter().map(|sample|(None, sample))) for (note, sample) in self.mapped.iter()
{ .map(|(note, sample)|(Some(note), sample))
if y1 >= height { .chain(self.unmapped.iter().map(|sample|(None, sample)))
break {
if y1 >= height {
break
}
let active = j == self.cursor.0;
width = width.max(draw_sample(buf, x, y + y1, note, &*sample.read().unwrap(), active)?);
y1 = y1 + 1;
j = j + 1;
} }
let active = j == self.cursor.0; let height = ((2 + y1) as u16).min(height);
width = width.max(draw_sample(buf, x, y + y1, note, &*sample.read().unwrap(), active)?); Ok(Rect { x, y, width: (width as u16).min(area.width), height })
y1 = y1 + 1;
j = j + 1;
} }
let height = ((2 + y1) as u16).min(height); }
Ok(Rect { x, y, width: (width as u16).min(area.width), height })
});
fn draw_sample ( fn draw_sample (
buf: &mut Buffer, x: u16, y: u16, note: Option<&u7>, sample: &Sample, focus: bool buf: &mut Buffer, x: u16, y: u16, note: Option<&u7>, sample: &Sample, focus: bool

View file

@ -1,35 +1,37 @@
use crate::*; use crate::*;
use tek_core::Direction; use tek_core::Direction;
render!(Track |self, buf, area| TrackView { impl<'a> Render<TuiOutput<'a>, Rect> for Track {
chain: Some(&self), fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
direction: tek_core::Direction::Right, TrackView {
focused: true, chain: Some(&self),
entered: true, direction: tek_core::Direction::Right,
//pub channels: u8, focused: true,
//pub input_ports: Vec<Port<AudioIn>>, entered: true,
//pub pre_gain_meter: f64, //pub channels: u8,
//pub gain: f64, //pub input_ports: Vec<Port<AudioIn>>,
//pub insert_ports: Vec<Port<AudioOut>>, //pub pre_gain_meter: f64,
//pub return_ports: Vec<Port<AudioIn>>, //pub gain: f64,
//pub post_gain_meter: f64, //pub insert_ports: Vec<Port<AudioOut>>,
//pub post_insert_meter: f64, //pub return_ports: Vec<Port<AudioIn>>,
//pub level: f64, //pub post_gain_meter: f64,
//pub pan: f64, //pub post_insert_meter: f64,
//pub output_ports: Vec<Port<AudioOut>>, //pub level: f64,
//pub post_fader_meter: f64, //pub pan: f64,
//pub route: String, //pub output_ports: Vec<Port<AudioOut>>,
}.render(buf, area)); //pub post_fader_meter: f64,
//pub route: String,
}.render(to)
}
}
pub struct TrackView<'a> { pub struct TrackView<'a> {
pub chain: Option<&'a Track>, pub chain: Option<&'a Track>,
pub direction: Direction, pub direction: Direction,
pub focused: bool, pub focused: bool,
pub entered: bool, pub entered: bool,
} }
impl<'a> Render<TuiOutput<'a>, Rect> for TrackView<'a> {
impl<'a> Render for TrackView<'a> { fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
fn render (&self, buf: &mut Buffer, mut area: Rect) -> Usually<Rect> {
if let Some(chain) = self.chain { if let Some(chain) = self.chain {
match self.direction { match self.direction {
Direction::Down => area.width = area.width.min(40), Direction::Down => area.width = area.width.min(40),

View file

@ -35,30 +35,32 @@ impl ArrangerRenameModal {
} }
} }
render!(ArrangerRenameModal |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for ArrangerRenameModal {
let y = area.y + area.height / 2; fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
let bg_area = Rect { let y = area.y + area.height / 2;
x: 1, let bg_area = Rect {
y: y - 1, x: 1,
width: area.width - 2, y: y - 1,
height: 3 width: area.width - 2,
}; height: 3
fill_bg(buf, bg_area, Nord::BG0); };
Lozenge(Style::default().bold().white().dim()).draw(buf, bg_area)?; fill_bg(buf, bg_area, Nord::BG0);
let label = match self.target { Lozenge(Style::default().bold().white().dim()).draw(buf, bg_area)?;
ArrangerFocus::Mix => "Rename project:", let label = match self.target {
ArrangerFocus::Track(_) => "Rename track:", ArrangerFocus::Mix => "Rename project:",
ArrangerFocus::Scene(_) => "Rename scene:", ArrangerFocus::Track(_) => "Rename track:",
ArrangerFocus::Clip(_, _) => "Rename clip:", ArrangerFocus::Scene(_) => "Rename scene:",
}; ArrangerFocus::Clip(_, _) => "Rename clip:",
let style = Some(Style::default().not_bold().white().not_dim()); };
label.blit(buf, area.x + 3, y, style)?; let style = Some(Style::default().not_bold().white().not_dim());
let style = Some(Style::default().bold().white().not_dim()); label.blit(buf, area.x + 3, y, style)?;
self.value.blit(buf, area.x + 3 + label.len() as u16 + 1, y, style)?; let style = Some(Style::default().bold().white().not_dim());
let style = Some(Style::default().bold().white().not_dim().reversed()); self.value.blit(buf, area.x + 3 + label.len() as u16 + 1, y, style)?;
"".blit(buf, area.x + 3 + label.len() as u16 + 1 + self.cursor as u16, y, style)?; let style = Some(Style::default().bold().white().not_dim().reversed());
Ok(area) "".blit(buf, area.x + 3 + label.len() as u16 + 1 + self.cursor as u16, y, style)?;
}); Ok(area)
}
}
handle!(ArrangerRenameModal |self, e| { handle!(ArrangerRenameModal |self, e| {
match e { match e {

View file

@ -19,25 +19,30 @@ impl ArrangerViewMode {
} }
} }
render!(Arranger |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for Arranger {
let area = Rect { fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
x: area.x + 1, width: area.width - 2, y: area.y + 1, height: area.height - 2 let area = Rect {
}; x: to.area.x + 1,
let area = match self.mode { width: to.area.width - 2,
ArrangerViewMode::Horizontal => y: to.area.y + 1,
super::arranger_view_h::draw(self, buf, area), height: to.area.height - 2
ArrangerViewMode::VerticalCompact1 => };
super::arranger_view_v::draw_compact_1(self, buf, area), let area = match self.mode {
ArrangerViewMode::VerticalCompact2 => ArrangerViewMode::Horizontal =>
super::arranger_view_v::draw_compact_2(self, buf, area), super::arranger_view_h::draw(self, to.buffer, area),
ArrangerViewMode::VerticalExpanded => ArrangerViewMode::VerticalCompact1 =>
super::arranger_view_v::draw_expanded(self, buf, area), super::arranger_view_v::draw_compact_1(self, to.buffer, area),
}?; ArrangerViewMode::VerticalCompact2 =>
let area = Rect { super::arranger_view_v::draw_compact_2(self, to.buffer, area),
x: area.x - 1, ArrangerViewMode::VerticalExpanded =>
width: area.width + 2, super::arranger_view_v::draw_expanded(self, to.buffer, area),
y: area.y - 1, }?;
height: area.height + 2, let area = Rect {
}; x: area.x - 1,
Lozenge(Style::default().fg(Nord::BG2)).draw(buf, area) width: area.width + 2,
}); y: area.y - 1,
height: area.height + 2,
};
Lozenge(Style::default().fg(Nord::BG2)).draw(to.buffer, area)
}
}

View file

@ -1,12 +1,14 @@
use crate::*; use crate::*;
render!(Sequencer |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for Sequencer {
self.horizontal_draw(buf, area)?; fn render (&self, target: &mut TuiOutput<'a>) -> Perhaps<Rect> {
if self.focused && self.entered { self.horizontal_draw(target)?;
Corners(Style::default().green().not_dim()).draw(buf, area)?; if self.focused && self.entered {
Corners(Style::default().green().not_dim()).draw(buf, area)?;
}
Ok(area)
} }
Ok(area) }
});
impl Sequencer { impl Sequencer {
/// Select which pattern to display. This pre-renders it to the buffer at full resolution. /// Select which pattern to display. This pre-renders it to the buffer at full resolution.

View file

@ -47,7 +47,7 @@ const STYLE_VALUE: Option<Style> = Some(Style {
struct SequenceName<'a>(&'a Sequencer); struct SequenceName<'a>(&'a Sequencer);
impl<'a> Render for SequenceName<'a> { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceName<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area; let Rect { x, y, .. } = area;
let frame = Rect { x, y, width: 10, height: 4 }; let frame = Rect { x, y, width: 10, height: 4 };
@ -60,7 +60,7 @@ impl<'a> Render for SequenceName<'a> {
struct SequenceRange; struct SequenceRange;
impl Render for SequenceRange { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceRange {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area; let Rect { x, y, .. } = area;
let frame = Rect { x, y, width: 10, height: 6 }; let frame = Rect { x, y, width: 10, height: 6 };
@ -75,7 +75,7 @@ impl Render for SequenceRange {
struct SequenceLoopRange; struct SequenceLoopRange;
impl Render for SequenceLoopRange { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceLoopRange {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area; let Rect { x, y, .. } = area;
let range = Rect { x, y, width: 10, height: 7 }; let range = Rect { x, y, width: 10, height: 7 };
@ -91,7 +91,7 @@ impl Render for SequenceLoopRange {
struct SequenceNoteRange; struct SequenceNoteRange;
impl Render for SequenceNoteRange { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceNoteRange {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let Rect { x, y, .. } = area; let Rect { x, y, .. } = area;
let range = Rect { x, y, width: 10, height: 9 }; let range = Rect { x, y, width: 10, height: 9 };
@ -109,7 +109,7 @@ impl Render for SequenceNoteRange {
struct SequenceKeys<'a>(&'a Sequencer); struct SequenceKeys<'a>(&'a Sequencer);
impl<'a> Render for SequenceKeys<'a> { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceKeys<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
if area.height < 2 { if area.height < 2 {
return Ok(area) return Ok(area)
@ -132,7 +132,7 @@ impl<'a> Render for SequenceKeys<'a> {
struct SequenceNotes<'a>(&'a Sequencer); struct SequenceNotes<'a>(&'a Sequencer);
impl<'a> Render for SequenceNotes<'a> { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceNotes<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
if area.height < 2 { if area.height < 2 {
return Ok(area) return Ok(area)
@ -160,7 +160,7 @@ impl<'a> Render for SequenceNotes<'a> {
struct SequenceCursor<'a>(&'a Sequencer); struct SequenceCursor<'a>(&'a Sequencer);
impl<'a> Render for SequenceCursor<'a> { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceCursor<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
if let (Some(time), Some(note)) = (self.0.time_axis.point, self.0.note_axis.point) { if let (Some(time), Some(note)) = (self.0.time_axis.point, self.0.note_axis.point) {
let x = area.x + Sequencer::H_KEYS_OFFSET as u16 + time as u16; let x = area.x + Sequencer::H_KEYS_OFFSET as u16 + time as u16;
@ -175,7 +175,7 @@ impl<'a> Render for SequenceCursor<'a> {
struct SequenceZoom<'a>(&'a Sequencer); struct SequenceZoom<'a>(&'a Sequencer);
impl<'a> Render for SequenceZoom<'a> { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceZoom<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
let quant = ppq_to_name(self.0.time_axis.scale); let quant = ppq_to_name(self.0.time_axis.scale);
let quant_x = area.x + area.width - 1 - quant.len() as u16; let quant_x = area.x + area.width - 1 - quant.len() as u16;
@ -186,8 +186,8 @@ impl<'a> Render for SequenceZoom<'a> {
struct SequenceTimer<'a>(&'a Sequencer, Arc<RwLock<Phrase>>); struct SequenceTimer<'a>(&'a Sequencer, Arc<RwLock<Phrase>>);
impl<'a> Render for SequenceTimer<'a> { impl<'a> Render<TuiOutput<'a>, Rect> for SequenceTimer<'a> {
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> { fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
let phrase = self.1.read().unwrap(); let phrase = self.1.read().unwrap();
let (time0, time_z, now) = ( let (time0, time_z, now) = (
self.0.time_axis.start, self.0.time_axis.scale, self.0.now % phrase.length self.0.time_axis.start, self.0.time_axis.scale, self.0.now % phrase.length

View file

@ -2,108 +2,120 @@ use crate::*;
const CORNERS: Corners = Corners(NOT_DIM_GREEN); const CORNERS: Corners = Corners(NOT_DIM_GREEN);
render!(TransportToolbar |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for TransportToolbar {
let mut area = area; fn render (&self, target: &mut TuiOutput<'a>) -> Perhaps<Rect> {
area.height = 2; let mut area = target.area;
let area = Split::right() area.height = 2;
.add_ref(&self.playing) let area = Split::right()
.add_ref(&self.bpm) .add_ref(&self.playing)
.add_ref(&self.quant) .add_ref(&self.bpm)
.add_ref(&self.sync) .add_ref(&self.quant)
.add_ref(&self.clock) .add_ref(&self.sync)
.render(buf, area)?; .add_ref(&self.clock)
//if self.is_focused() { .render(target)?;
//fill_bg(buf, area, COLOR_BG0); //if self.is_focused() {
//CORNERS_DIM.draw(buf, area)?; //fill_bg(buf, area, COLOR_BG0);
//} //CORNERS_DIM.draw(buf, area)?;
Ok(area) //}
}); Ok(area)
render!(TransportPlayPauseButton |self, buf, area| {
let Rect { x, y, .. } = area;
let Self { value, focused } = self;
let style = Some(match value {
Some(TransportState::Stopped) => GRAY_DIM.bold(),
Some(TransportState::Starting) => GRAY_NOT_DIM_BOLD,
Some(TransportState::Rolling) => WHITE_NOT_DIM_BOLD,
_ => unreachable!(),
});
let label = match value {
Some(TransportState::Rolling) => "▶ PLAYING",
Some(TransportState::Starting) => "READY ...",
Some(TransportState::Stopped) => "⏹ STOPPED",
_ => unreachable!(),
};
let mut area = label.blit(buf, x + 1, y, style)?;
area.width = area.width + 1;
area.height = area.height + 1;
if *focused {
let area = Rect { x: area.x - 1, width: area.width - 1, ..area };
CORNERS.draw(buf, area)?;
fill_bg(buf, area, COLOR_BG1);
} }
Ok(area) }
});
render!(TransportBPM |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for TransportPlayPauseButton {
let Rect { x, y, .. } = area; fn render (&self, target: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let Self { value, focused } = self; let Rect { x, y, .. } = target.area;
"BPM".blit(buf, x, y, Some(NOT_DIM))?; let Self { value, focused } = &self;
let width = format!("{}.{:03}", value, (value * 1000.0) % 1000.0).blit(buf, x, y + 1, Some(NOT_DIM_BOLD))?.width; let style = Some(match value {
let area = Rect { x, y, width: (width + 2).max(10), height: 2 }; Some(TransportState::Stopped) => GRAY_DIM.bold(),
if *focused { Some(TransportState::Starting) => GRAY_NOT_DIM_BOLD,
let area = Rect { x: area.x - 1, width: area.width - 1, ..area }; Some(TransportState::Rolling) => WHITE_NOT_DIM_BOLD,
CORNERS.draw(buf, area)?; _ => unreachable!(),
fill_bg(buf, area, COLOR_BG1); });
let label = match value {
Some(TransportState::Rolling) => "▶ PLAYING",
Some(TransportState::Starting) => "READY ...",
Some(TransportState::Stopped) => "⏹ STOPPED",
_ => unreachable!(),
};
let mut area = label.blit(target.buffer, x + 1, y, style)?.unwrap();
area.width = area.width + 1;
area.height = area.height + 1;
if *focused {
let area = Rect { x: area.x - 1, width: area.width - 1, ..area };
CORNERS.draw(target)?;
fill_bg(target.buffer, target.area, COLOR_BG1);
}
Ok(area)
} }
Ok(area) }
});
render!(TransportQuantize |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for TransportBPM {
let Rect { x, y, .. } = area; fn render (&self, target: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let Self { value, focused } = self; let Rect { x, y, .. } = area;
"QUANT".blit(buf, x, y, Some(NOT_DIM))?; let Self { value, focused } = self;
let width = ppq_to_name(*value as usize).blit(buf, x, y + 1, Some(NOT_DIM_BOLD))?.width; "BPM".blit(buf, x, y, Some(NOT_DIM))?;
let area = Rect { x, y, width: (width + 2).max(10), height: 2 }; let width = format!("{}.{:03}", value, (value * 1000.0) % 1000.0).blit(buf, x, y + 1, Some(NOT_DIM_BOLD))?.width;
if *focused { let area = Rect { x, y, width: (width + 2).max(10), height: 2 };
let area = Rect { x: area.x - 1, width: area.width - 1, ..area }; if *focused {
CORNERS.draw(buf, area)?; let area = Rect { x: area.x - 1, width: area.width - 1, ..area };
fill_bg(buf, area, COLOR_BG1); CORNERS.draw(buf, area)?;
fill_bg(buf, area, COLOR_BG1);
}
Ok(area)
} }
Ok(area) }
});
render!(TransportSync |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for TransportQuantize {
let Rect { x, y, .. } = area; fn render (&self, target: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let Self { value, focused } = self; let Rect { x, y, .. } = area;
"SYNC".blit(buf, x, y, Some(NOT_DIM))?; let Self { value, focused } = self;
let width = ppq_to_name(*value as usize).blit(buf, x, y + 1, Some(NOT_DIM_BOLD))?.width; "QUANT".blit(buf, x, y, Some(NOT_DIM))?;
let area = Rect { x, y, width: (width + 2).max(10), height: 2 }; let width = ppq_to_name(*value as usize).blit(buf, x, y + 1, Some(NOT_DIM_BOLD))?.width;
if *focused { let area = Rect { x, y, width: (width + 2).max(10), height: 2 };
let area = Rect { x: area.x - 1, width: area.width - 1, ..area }; if *focused {
CORNERS.draw(buf, area)?; let area = Rect { x: area.x - 1, width: area.width - 1, ..area };
fill_bg(buf, area, COLOR_BG1); CORNERS.draw(buf, area)?;
fill_bg(buf, area, COLOR_BG1);
}
Ok(area)
} }
Ok(area) }
});
render!(TransportClock |self, buf, area| { impl<'a> Render<TuiOutput<'a>, Rect> for TransportSync {
let Rect { x, y, width, .. } = area; fn render (&self, target: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let Self { frame, pulse, ppq, usecs, focused } = self; let Rect { x, y, .. } = area;
let (beats, pulses) = if *ppq > 0 { (pulse / ppq, pulse % ppq) } else { (0, 0) }; let Self { value, focused } = self;
let (bars, beats) = ((beats / 4) + 1, (beats % 4) + 1); "SYNC".blit(buf, x, y, Some(NOT_DIM))?;
let (seconds, msecs) = (usecs / 1000000, usecs / 1000 % 1000); let width = ppq_to_name(*value as usize).blit(buf, x, y + 1, Some(NOT_DIM_BOLD))?.width;
let (minutes, seconds) = (seconds / 60, seconds % 60); let area = Rect { x, y, width: (width + 2).max(10), height: 2 };
let timer = format!("{bars}.{beats}.{pulses:02}"); if *focused {
timer.blit(buf, x + width - timer.len() as u16 - 1, y + 0, Some(NOT_DIM))?; let area = Rect { x: area.x - 1, width: area.width - 1, ..area };
let timer = format!("{minutes}:{seconds:02}:{msecs:03}"); CORNERS.draw(buf, area)?;
timer.blit(buf, x + width - timer.len() as u16 - 1, y + 1, Some(NOT_DIM))?; fill_bg(buf, area, COLOR_BG1);
let mut area = area; }
area.width = area.width + 1; Ok(area)
if *focused {
let area = Rect { x: area.x - 1, ..area };
CORNERS.draw(buf, area)?;
fill_bg(buf, area, COLOR_BG1);
} }
Ok(area) }
});
impl<'a> Render<TuiOutput<'a>, Rect> for TransportClock {
fn render (&self, target: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let Rect { x, y, width, .. } = area;
let Self { frame, pulse, ppq, usecs, focused } = self;
let (beats, pulses) = if *ppq > 0 { (pulse / ppq, pulse % ppq) } else { (0, 0) };
let (bars, beats) = ((beats / 4) + 1, (beats % 4) + 1);
let (seconds, msecs) = (usecs / 1000000, usecs / 1000 % 1000);
let (minutes, seconds) = (seconds / 60, seconds % 60);
let timer = format!("{bars}.{beats}.{pulses:02}");
timer.blit(buf, x + width - timer.len() as u16 - 1, y + 0, Some(NOT_DIM))?;
let timer = format!("{minutes}:{seconds:02}:{msecs:03}");
timer.blit(buf, x + width - timer.len() as u16 - 1, y + 1, Some(NOT_DIM))?;
let mut area = area;
area.width = area.width + 1;
if *focused {
let area = Rect { x: area.x - 1, ..area };
CORNERS.draw(buf, area)?;
fill_bg(buf, area, COLOR_BG1);
}
Ok(area)
}
}