mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 03:36:41 +01:00
513 lines
24 KiB
Rust
513 lines
24 KiB
Rust
use crate::*;
|
|
pub(crate) use std::fmt::Write;
|
|
pub(crate) use ::tengri::tui::ratatui::prelude::Position;
|
|
|
|
impl App {
|
|
|
|
pub fn update_clock (&self) {
|
|
ViewCache::update_clock(&self.view_cache, self.clock(), self.size.w() > 80)
|
|
}
|
|
}
|
|
|
|
#[tengri_proc::view(TuiOut)]
|
|
impl App {
|
|
pub fn view_nil (&self) -> impl Content<TuiOut> + use<'_> {
|
|
"nil"
|
|
}
|
|
pub fn view_status_v (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.update_clock();
|
|
let cache = self.view_cache.read().unwrap();
|
|
let theme = self.color;
|
|
let playing = self.clock().is_rolling();
|
|
Tui::bg(theme.darkest.rgb, Fixed::xy(20, 6, Outer(true, Style::default().fg(Tui::g(96))).enclose(
|
|
col!(
|
|
Fill::x(Align::w(Bsp::e(
|
|
Align::w(Tui::bg(if playing { Rgb(0, 128, 0) } else { Rgb(128, 64, 0) },
|
|
Either::new(false, // TODO
|
|
Thunk::new(move||Fixed::x(9, Either::new(playing,
|
|
Tui::fg(Rgb(0, 255, 0), " PLAYING "),
|
|
Tui::fg(Rgb(255, 128, 0), " STOPPED ")))
|
|
),
|
|
Thunk::new(move||Fixed::x(5, Either::new(playing,
|
|
Tui::fg(Rgb(0, 255, 0), Bsp::s(" 🭍🭑🬽 ", " 🭞🭜🭘 ",)),
|
|
Tui::fg(Rgb(255, 128, 0), Bsp::s(" ▗▄▖ ", " ▝▀▘ ",))))
|
|
)
|
|
)
|
|
)),
|
|
Bsp::s(
|
|
FieldH(theme, "Beat", cache.beat.view.clone()),
|
|
FieldH(theme, "Time", cache.time.view.clone()),
|
|
),
|
|
))),
|
|
Fill::x(Align::w(FieldH(theme, "BPM", cache.bpm.view.clone()))),
|
|
Fill::x(Align::w(FieldH(theme, "SR ", cache.sr.view.clone()))),
|
|
Fill::x(Align::w(FieldH(theme, "Buf", cache.buf.view.clone()))),
|
|
Fill::x(Align::w(FieldH(theme, "Lat", cache.lat.view.clone()))),
|
|
))))
|
|
}
|
|
pub fn view_status (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.update_clock();
|
|
let cache = self.view_cache.read().unwrap();
|
|
view_status(Some(self.project.selection.describe(self.tracks(), self.scenes())),
|
|
cache.sr.view.clone(), cache.buf.view.clone(), cache.lat.view.clone())
|
|
}
|
|
pub fn view_transport (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.update_clock();
|
|
let cache = self.view_cache.read().unwrap();
|
|
view_transport(self.project.clock.is_rolling(),
|
|
cache.bpm.view.clone(), cache.beat.view.clone(), cache.time.view.clone())
|
|
}
|
|
pub fn view_editor (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.editor()
|
|
}
|
|
pub fn view_editor_status (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.editor().map(|e|Bsp::s(e.clip_status(), e.edit_status()))
|
|
}
|
|
pub fn view_midi_ins_status (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.get_track().map(|track|{
|
|
let ins = track.sequencer.midi_ins.len() as u16;
|
|
Fixed::xy(20, 1 + ins, Outer(true, Style::default().fg(Tui::g(96))).enclose(
|
|
Fixed::xy(20, 1 + ins, FieldV(self.color, format!("MIDI ins: "),
|
|
Map::south(1, ||track.sequencer.midi_ins.iter(),
|
|
|port, index|Fill::x(Align::w(format!(" {index} {}", port.name()))))))))
|
|
})
|
|
}
|
|
pub fn view_midi_outs_status (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.get_track().map(|track|{
|
|
let outs = track.sequencer.midi_outs.len() as u16;
|
|
Fixed::xy(20, 1 + outs, Outer(true, Style::default().fg(Tui::g(96))).enclose(
|
|
Fixed::xy(20, 1 + outs, FieldV(self.color, format!("MIDI outs: "),
|
|
Map::south(1, ||track.sequencer.midi_outs.iter(),
|
|
|port, index|Fill::x(Align::w(format!(" {index} {}", port.name()))))))))
|
|
})
|
|
}
|
|
pub fn view_audio_ins_status (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.get_track()
|
|
.and_then(|track|track.devices.get(0))
|
|
.map(|device|{
|
|
let ins = device.audio_ins().len() as u16;
|
|
Fixed::xy(20, 1 + ins, Outer(true, Style::default().fg(Tui::g(96))).enclose(
|
|
Fixed::xy(20, 1 + ins, FieldV(self.color, format!("Audio ins: "),
|
|
Map::south(1, ||device.audio_ins().iter(),
|
|
|port, index|Fill::x(Align::w(format!(" {index} {}", port.name()))))))))})
|
|
}
|
|
pub fn view_audio_outs_status (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.get_track()
|
|
.and_then(|track|track.devices.last())
|
|
.map(|device|{
|
|
let outs = device.audio_outs().len() as u16;
|
|
Fixed::xy(20, 1 + outs, Outer(true, Style::default().fg(Tui::g(96))).enclose(
|
|
Fixed::xy(20, 1 + outs, FieldV(self.color, format!("Audio outs: "),
|
|
Map::south(1, ||device.audio_outs().iter(),
|
|
|port, index|Fill::x(Align::w(format!(" {index} {}", port.name()))))))))})
|
|
}
|
|
pub fn view_arranger (&self) -> impl Content<TuiOut> + use<'_> {
|
|
ArrangerView::new(&self.project, self.editor.as_ref())
|
|
}
|
|
pub fn view_arranger_scenes (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.scenes_view(&self.editor)
|
|
}
|
|
pub fn view_arranger_scene_names <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
|
|
let h = self.project.scenes.len() as u16 * 2;
|
|
let bg = self.color.darker.rgb;
|
|
Fixed::y(h, Tui::bg(bg, Align::w(Fill::x(Map::new(
|
|
||self.project.scenes.iter().skip(self.project.scene_scroll),
|
|
move|scene: &Scene, index|
|
|
Push::y(index as u16 * 2u16, Fixed::xy(20, 2,
|
|
Tui::bg(scene.color.dark.rgb, Align::nw(Bsp::e(
|
|
format!(" {index:2} "),
|
|
Tui::fg(Rgb(255, 255, 255),
|
|
Tui::bold(true, format!("{}", scene.name)))))))))))))
|
|
}
|
|
pub fn view_arranger_scene_clips <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
|
|
let h = self.project.scenes.len() as u16 * 2;
|
|
let bg = self.color.darker.rgb;
|
|
Fixed::y(h, Tui::bg(bg, Align::w(Fill::x(Map::new(
|
|
||self.project.scenes.iter().skip(self.project.scene_scroll),
|
|
move|scene: &'a Scene, index1|
|
|
Push::y(index1 as u16 * 2u16, Fixed::xy(20, 2,
|
|
Map::new(
|
|
move||scene.clips.iter().skip(self.project.track_scroll),
|
|
move|clip: &'a Option<Arc<RwLock<MidiClip>>>, index2|{
|
|
let (theme, text) = if let Some(clip) = clip {
|
|
let clip = clip.read().unwrap();
|
|
(clip.color, clip.name.clone())
|
|
} else {
|
|
(scene.color, Default::default())
|
|
};
|
|
Push::x(index2 as u16 * 14, Tui::bg(theme.dark.rgb, Bsp::e(
|
|
format!(" {index1:2} {index2:2} "),
|
|
Tui::fg(Rgb(255, 255, 255),
|
|
Tui::bold(true, format!("{}", text))))))
|
|
}))))))))
|
|
}
|
|
pub fn view_arranger_track_names (&self) -> impl Content<TuiOut> + use<'_> {
|
|
let mut max_outputs = 0u16;
|
|
for track in self.project.tracks.iter() {
|
|
max_outputs = max_outputs.max(track.sequencer.midi_outs.len() as u16);
|
|
}
|
|
Bsp::w(
|
|
Fixed::x(20, Tui::bg(self.color.darkest.rgb,
|
|
col!(Tui::bold(true, "[t]rack"), "[T] Add"))),
|
|
Align::w(Fixed::y(max_outputs + 1, Tui::bg(self.color.darker.rgb, Align::w(Fill::x(Map::new(
|
|
||self.project.tracks_with_sizes(&self.project.selection, None)
|
|
.skip(self.project.track_scroll),
|
|
move|(index, track, x1, x2): (usize, &Track, usize, usize), _|
|
|
Push::x(index as u16 * 14, Fixed::xy(track.width as u16, max_outputs + 1,
|
|
Tui::bg(track.color.dark.rgb, Align::nw(Bsp::s(
|
|
Tui::fg(Rgb(255, 255, 255), Tui::bold(true, format!("{}", track.name))),
|
|
format!("{index} {x1} {x2}")))))))))))))
|
|
}
|
|
pub fn view_arranger_track_outputs <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
|
|
let mut max_outputs = 0u16;
|
|
for track in self.project.tracks.iter() {
|
|
max_outputs = max_outputs.max(track.sequencer.midi_outs.len() as u16);
|
|
}
|
|
Bsp::w(
|
|
Fixed::x(20, Tui::bg(self.color.darkest.rgb,
|
|
col!(Tui::bold(true, "[o]utput"), "[O] Add"))),
|
|
Align::w(Fixed::y(max_outputs + 1, Tui::bg(self.color.darker.rgb, Align::w(Fill::x(Map::new(
|
|
||self.project.tracks_with_sizes(&self.project.selection, None)
|
|
.skip(self.project.track_scroll),
|
|
move|(index, track, x1, x2): (usize, &'a Track, usize, usize), _|
|
|
Push::x(x2 as u16, Tui::bg(track.color.dark.rgb, Fixed::xy(
|
|
track.width as u16,
|
|
max_outputs + 1,
|
|
Align::nw(Bsp::s(
|
|
format!("[mut] [sol]"),
|
|
Map::south(1, ||track.sequencer.midi_outs.iter(),
|
|
|port, index|Tui::fg(Rgb(255, 255, 255),
|
|
format!("{index}: {}", port.name())))))))))))))))
|
|
}
|
|
pub fn view_arranger_track_inputs <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
|
|
let mut max_inputs = 0u16;
|
|
for track in self.project.tracks.iter() {
|
|
max_inputs = max_inputs.max(track.sequencer.midi_ins.len() as u16);
|
|
}
|
|
Bsp::w(
|
|
Fixed::x(20, Tui::bg(self.color.darkest.rgb,
|
|
col!(Tui::bold(true, "[i]nputs"), "[I] Add"))),
|
|
Fill::x(Align::w(Fixed::y(max_inputs + 1, Tui::bg(self.color.darker.rgb, Align::w(Fill::x(Map::new(
|
|
||self.project.tracks_with_sizes(&self.project.selection, None)
|
|
.skip(self.project.track_scroll),
|
|
move|(index, track, x1, x2): (usize, &'a Track, usize, usize), _|
|
|
Push::x(x2 as u16, Fixed::xy(track.width as u16, max_inputs + 1,
|
|
Tui::bg(track.color.dark.rgb, Align::nw(Bsp::s(
|
|
format!("[rec] [mon]"),
|
|
Map::south(1, ||track.sequencer.midi_ins.iter(),
|
|
|port, index|Tui::fg(Rgb(255, 255, 255),
|
|
format!("{index}: {}", port.name()))))))))))))))))
|
|
}
|
|
pub fn view_arranger_track_devices <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
|
|
let mut max_devices = 2u16;
|
|
for track in self.project.tracks.iter() {
|
|
max_devices = max_devices.max(track.devices.len() as u16);
|
|
}
|
|
Bsp::w(
|
|
Fixed::x(20, Tui::bg(self.color.darkest.rgb,
|
|
col!(Tui::bold(true, "[d]evice"), "[D] Add"))),
|
|
Fixed::y(max_devices, Tui::bg(self.color.darker.rgb, Align::w(Fill::x(Map::new(
|
|
||self.project.tracks_with_sizes(&self.project.selection, None)
|
|
.skip(self.project.track_scroll),
|
|
move|(index, track, x1, x2): (usize, &'a Track, usize, usize), _|
|
|
Push::x(x2 as u16, Fixed::xy(track.width as u16, max_devices + 1,
|
|
Tui::bg(track.color.dark.rgb, Align::nw(Map::south(1, move||0..max_devices,
|
|
|_, index|format!("{index}: {}", "--------"))))))))))))
|
|
}
|
|
pub fn view_arranger_track_scenes <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
|
|
let mut max_devices = 0u16;
|
|
for track in self.project.tracks.iter() {
|
|
max_devices = max_devices.max(track.devices.len() as u16);
|
|
}
|
|
Bsp::w(
|
|
Fixed::x(20, Tui::bg(self.color.darkest.rgb,
|
|
col!(Tui::bold(true, "Devices"), "[d] Select", "[D] Add"))),
|
|
Fixed::y(max_devices + 1, Tui::bg(self.color.darker.rgb, Align::w(Fill::x(Map::new(
|
|
||self.project.tracks_with_sizes(&self.project.selection, None)
|
|
.skip(self.project.track_scroll),
|
|
move|(index, track, x1, x2): (usize, &'a Track, usize, usize), _|
|
|
Push::x(x2 as u16, Fixed::xy(track.width as u16, max_devices + 1,
|
|
Align::nw(Map::south(1, ||track.devices.iter(),
|
|
|device, index|format!("{index}: {}", device.name())))))))))))
|
|
}
|
|
pub fn view_pool (&self) -> impl Content<TuiOut> + use<'_> {
|
|
Fixed::x(20, Bsp::s(
|
|
Fill::x(Align::w(FieldH(self.color, "Clip pool:", ""))),
|
|
Fill::y(Align::n(Tui::bg(Rgb(0, 0, 0), Outer(true, Style::default().fg(Tui::g(96)))
|
|
.enclose(PoolView(&self.project.pool)))))))
|
|
}
|
|
pub fn view_samples_keys (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.sampler().map(|s|s.view_list(true, self.editor().unwrap()))
|
|
}
|
|
pub fn view_samples_grid (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.sampler().map(|s|s.view_grid())
|
|
}
|
|
pub fn view_sample_viewer (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.sampler().map(|s|s.view_sample(self.editor().unwrap().get_note_pos()))
|
|
}
|
|
pub fn view_sample_info (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.sampler().map(|s|s.view_sample_info(self.editor().unwrap().get_note_pos()))
|
|
}
|
|
pub fn view_sample_status (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.sampler().map(|s|s.view_sample_status(self.editor().unwrap().get_note_pos()))
|
|
}
|
|
pub fn view_meters_input (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.sampler().map(|s|s.view_meters_input())
|
|
}
|
|
pub fn view_meters_output (&self) -> impl Content<TuiOut> + use<'_> {
|
|
self.project.sampler().map(|s|s.view_meters_output())
|
|
}
|
|
pub fn view_dialog (&self) -> impl Content<TuiOut> + use<'_> {
|
|
When(self.dialog.is_some(), Bsp::b( "",
|
|
Fixed::xy(70, 23, Tui::fg_bg(Rgb(255,255,255), Rgb(16,16,16), Bsp::b(
|
|
Repeat(" "), Outer(true, Style::default().fg(Tui::g(96)))
|
|
.enclose(self.dialog.as_ref().map(|dialog|match dialog {
|
|
Dialog::Menu(_) =>
|
|
self.view_dialog_menu().boxed(),
|
|
Dialog::Help(offset) =>
|
|
self.view_dialog_help(*offset).boxed(),
|
|
Dialog::Browser(target, browser) =>
|
|
self.view_dialog_browser(target, browser).boxed(),
|
|
Dialog::Options =>
|
|
self.view_dialog_options().boxed(),
|
|
Dialog::Device(index) =>
|
|
self.view_dialog_device(*index).boxed(),
|
|
Dialog::Message(message) =>
|
|
self.view_dialog_message(message).boxed(),
|
|
}))
|
|
)))
|
|
))
|
|
}
|
|
}
|
|
|
|
impl App {
|
|
pub fn view_dialog_menu (&self) -> impl Content<TuiOut> {
|
|
let options = ||["Projects", "Settings", "Help", "Quit"].iter();
|
|
let option = |a,i|Tui::fg(Rgb(255,255,255), format!("{}", a));
|
|
Bsp::s(Tui::bold(true, "tek!"), Bsp::s("", Map::south(1, options, option)))
|
|
}
|
|
pub fn view_dialog_help <'a> (&'a self, offset: usize) -> impl Content<TuiOut> + use<'a> {
|
|
Bsp::s(Tui::bold(true, "Help"), Bsp::s("", Map::south(1,
|
|
move||self.config.keys.layers.iter()
|
|
.filter_map(|a|(a.0)(self).then_some(a.1))
|
|
.flat_map(|a|a)
|
|
.filter_map(|x|if let Value::Exp(_, iter)=x.value{ Some(iter) } else { None })
|
|
.skip(offset)
|
|
.take(20),
|
|
|mut b,i|Fixed::x(60, Align::w(Bsp::e("(", Bsp::e(
|
|
b.next().map(|t|Fixed::x(16, Align::w(Tui::fg(Rgb(64,224,0), format!("{}", t.value))))),
|
|
Bsp::e(" ", Align::w(format!("{}", b.0.0.trim()))))))))))
|
|
}
|
|
pub fn view_dialog_device (&self, index: usize) -> impl Content<TuiOut> + use<'_> {
|
|
let choices = ||self.device_kinds().iter();
|
|
let choice = move|label, i|
|
|
Fill::x(Tui::bg(if i == index { Rgb(64,128,32) } else { Rgb(0,0,0) },
|
|
Bsp::e(if i == index { "[ " } else { " " },
|
|
Bsp::w(if i == index { " ]" } else { " " },
|
|
label))));
|
|
Bsp::s(Tui::bold(true, "Add device"), Map::south(1, choices, choice))
|
|
}
|
|
pub fn view_dialog_message <'a> (&'a self, message: &'a Message) -> impl Content<TuiOut> + use<'a> {
|
|
Bsp::s(message, Bsp::s("", "[ OK ]"))
|
|
}
|
|
pub fn view_dialog_browser <'a> (&'a self, target: &BrowserTarget, browser: &'a Browser) -> impl Content<TuiOut> + use<'a> {
|
|
Bsp::s(
|
|
Padding::xy(3, 1, Fill::x(Align::w(FieldV(
|
|
self.color,
|
|
match target {
|
|
BrowserTarget::SaveProject => "Save project:",
|
|
BrowserTarget::LoadProject => "Load project:",
|
|
BrowserTarget::ImportSample(_) => "Import sample:",
|
|
BrowserTarget::ExportSample(_) => "Export sample:",
|
|
BrowserTarget::ImportClip(_) => "Import clip:",
|
|
BrowserTarget::ExportClip(_) => "Export clip:",
|
|
},
|
|
Shrink::x(3, Fixed::y(1, Tui::fg(Tui::g(96), RepeatH("🭻")))))))),
|
|
Outer(true, Style::default().fg(Tui::g(96)))
|
|
.enclose(Fill::xy(browser)))
|
|
}
|
|
pub fn view_dialog_load <'a> (&'a self, browser: &'a Browser) -> impl Content<TuiOut> + use<'a> {
|
|
Bsp::s(
|
|
Fill::x(Align::w(Margin::xy(1, 1, Bsp::e(
|
|
Tui::bold(true, " Load project: "),
|
|
Shrink::x(3, Fixed::y(1, RepeatH("🭻"))))))),
|
|
Outer(true, Style::default().fg(Tui::g(96)))
|
|
.enclose(Fill::xy(browser)))
|
|
}
|
|
pub fn view_dialog_export <'a> (&'a self, browser: &'a Browser) -> impl Content<TuiOut> + use<'a> {
|
|
Bsp::s(
|
|
Fill::x(Align::w(Margin::xy(1, 1, Bsp::e(
|
|
Tui::bold(true, " Export: "),
|
|
Shrink::x(3, Fixed::y(1, RepeatH("🭻"))))))),
|
|
Outer(true, Style::default().fg(Tui::g(96)))
|
|
.enclose(Fill::xy(browser)))
|
|
}
|
|
pub fn view_dialog_import <'a> (&'a self, browser: &'a Browser) -> impl Content<TuiOut> + use<'a> {
|
|
Bsp::s(
|
|
Fill::x(Align::w(Margin::xy(1, 1, Bsp::e(
|
|
Tui::bold(true, " Import: "),
|
|
Shrink::x(3, Fixed::y(1, RepeatH("🭻"))))))),
|
|
Outer(true, Style::default().fg(Tui::g(96)))
|
|
.enclose(Fill::xy(browser)))
|
|
}
|
|
pub fn view_dialog_options <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
|
|
"TODO"
|
|
}
|
|
}
|
|
|
|
impl ArrangerSceneRows for App {
|
|
fn arrangement (&self) -> &Arrangement {
|
|
self.project
|
|
}
|
|
fn scenes_height (&self) -> u16 {
|
|
self.project.scenes_height
|
|
}
|
|
fn width_side (&self) -> u16 {
|
|
20
|
|
}
|
|
fn width_mid (&self) -> u16 {
|
|
self.width().saturating_sub(self.width_side() * 2)
|
|
}
|
|
fn scene_selected (&self) -> Option<usize> {
|
|
self.project.selection.scene()
|
|
}
|
|
fn scene_last (&self) -> usize {
|
|
self.project.scenes.len().saturating_sub(1)
|
|
}
|
|
fn track_selected (&self) -> Option<usize> {
|
|
self.project.selection.track()
|
|
}
|
|
fn is_editing (&self) -> bool {
|
|
self.is_editing
|
|
}
|
|
}
|
|
|
|
pub(crate) fn heading <'a> (
|
|
key: &'a str,
|
|
label: &'a str,
|
|
count: usize,
|
|
content: impl Content<TuiOut> + Send + Sync + 'a,
|
|
editing: bool,
|
|
) -> impl Content<TuiOut> + 'a {
|
|
let count = format!("{count}");
|
|
Fill::xy(Align::w(Bsp::s(Fill::x(Align::w(button_3(key, label, count, editing))), content)))
|
|
}
|
|
|
|
/// Clear a pre-allocated buffer, then write into it.
|
|
#[macro_export] macro_rules! rewrite {
|
|
($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } }
|
|
}
|
|
|
|
#[derive(Debug, Default)] pub(crate) struct ViewMemo<T, U> {
|
|
pub(crate) value: T,
|
|
pub(crate) view: Arc<RwLock<U>>
|
|
}
|
|
|
|
impl<T: PartialEq, U> ViewMemo<T, U> {
|
|
fn new (value: T, view: U) -> Self {
|
|
Self { value, view: Arc::new(view.into()) }
|
|
}
|
|
pub(crate) fn update <R> (
|
|
&mut self,
|
|
newval: T,
|
|
render: impl Fn(&mut U, &T, &T)->R
|
|
) -> Option<R> {
|
|
if newval != self.value {
|
|
let result = render(&mut*self.view.write().unwrap(), &newval, &self.value);
|
|
self.value = newval;
|
|
return Some(result);
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
#[derive(Debug)] pub struct ViewCache {
|
|
pub(crate) sr: ViewMemo<Option<(bool, f64)>, String>,
|
|
pub(crate) buf: ViewMemo<Option<f64>, String>,
|
|
pub(crate) lat: ViewMemo<Option<f64>, String>,
|
|
pub(crate) bpm: ViewMemo<Option<f64>, String>,
|
|
pub(crate) beat: ViewMemo<Option<f64>, String>,
|
|
pub(crate) time: ViewMemo<Option<f64>, String>,
|
|
pub(crate) scns: ViewMemo<Option<(usize, usize)>, String>,
|
|
pub(crate) trks: ViewMemo<Option<(usize, usize)>, String>,
|
|
pub(crate) stop: Arc<str>,
|
|
pub(crate) edit: Arc<str>,
|
|
}
|
|
|
|
impl Default for ViewCache {
|
|
fn default () -> Self {
|
|
let mut beat = String::with_capacity(16);
|
|
write!(beat, "{}", Self::BEAT_EMPTY);
|
|
let mut time = String::with_capacity(16);
|
|
write!(time, "{}", Self::TIME_EMPTY);
|
|
let mut bpm = String::with_capacity(16);
|
|
write!(bpm, "{}", Self::BPM_EMPTY);
|
|
Self {
|
|
beat: ViewMemo::new(None, beat),
|
|
time: ViewMemo::new(None, time),
|
|
bpm: ViewMemo::new(None, bpm),
|
|
sr: ViewMemo::new(None, String::with_capacity(16)),
|
|
buf: ViewMemo::new(None, String::with_capacity(16)),
|
|
lat: ViewMemo::new(None, String::with_capacity(16)),
|
|
scns: ViewMemo::new(None, String::with_capacity(16)),
|
|
trks: ViewMemo::new(None, String::with_capacity(16)),
|
|
stop: "⏹".into(),
|
|
edit: "edit".into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ViewCache {
|
|
pub const BEAT_EMPTY: &'static str = "-.-.--";
|
|
pub const TIME_EMPTY: &'static str = "-.---s";
|
|
pub const BPM_EMPTY: &'static str = "---.---";
|
|
|
|
pub fn track_counter (cache: &Arc<RwLock<Self>>, track: usize, tracks: usize)
|
|
-> Arc<RwLock<String>>
|
|
{
|
|
let data = (track, tracks);
|
|
cache.write().unwrap().trks.update(Some(data), rewrite!(buf, "{}/{}", data.0, data.1));
|
|
cache.read().unwrap().trks.view.clone()
|
|
}
|
|
|
|
pub fn scene_add (cache: &Arc<RwLock<Self>>, scene: usize, scenes: usize, is_editing: bool)
|
|
-> impl Content<TuiOut>
|
|
{
|
|
let data = (scene, scenes);
|
|
cache.write().unwrap().scns.update(Some(data), rewrite!(buf, "({}/{})", data.0, data.1));
|
|
button_3("S", "add scene", cache.read().unwrap().scns.view.clone(), is_editing)
|
|
}
|
|
|
|
pub fn update_clock (cache: &Arc<RwLock<Self>>, clock: &Clock, compact: bool) {
|
|
let rate = clock.timebase.sr.get();
|
|
let chunk = clock.chunk.load(Relaxed) as f64;
|
|
let lat = chunk / rate * 1000.;
|
|
let delta = |start: &Moment|clock.global.usec.get() - start.usec.get();
|
|
let mut cache = cache.write().unwrap();
|
|
cache.buf.update(Some(chunk), rewrite!(buf, "{chunk}"));
|
|
cache.lat.update(Some(lat), rewrite!(buf, "{lat:.1}ms"));
|
|
cache.sr.update(Some((compact, rate)), |buf,_,_|{
|
|
buf.clear();
|
|
if compact {
|
|
write!(buf, "{:.1}kHz", rate / 1000.)
|
|
} else {
|
|
write!(buf, "{:.0}Hz", rate)
|
|
}
|
|
});
|
|
if let Some(now) = clock.started.read().unwrap().as_ref().map(delta) {
|
|
let pulse = clock.timebase.usecs_to_pulse(now);
|
|
let time = now/1000000.;
|
|
let bpm = clock.timebase.bpm.get();
|
|
cache.beat.update(Some(pulse), |buf, _, _|{
|
|
buf.clear();
|
|
clock.timebase.format_beats_1_to(buf, pulse)
|
|
});
|
|
cache.time.update(Some(time), rewrite!(buf, "{:.3}s", time));
|
|
cache.bpm.update(Some(bpm), rewrite!(buf, "{:.3}", bpm));
|
|
} else {
|
|
cache.beat.update(None, rewrite!(buf, "{}", ViewCache::BEAT_EMPTY));
|
|
cache.time.update(None, rewrite!(buf, "{}", ViewCache::TIME_EMPTY));
|
|
cache.bpm.update(None, rewrite!(buf, "{}", ViewCache::BPM_EMPTY));
|
|
}
|
|
}
|
|
}
|