wip: refactor arranger to device

This commit is contained in:
🪞👃🪞 2025-05-14 00:46:33 +03:00
parent fa73821a0b
commit 89288f2920
40 changed files with 2015 additions and 1919 deletions

View file

@ -2,8 +2,12 @@ use crate::*;
pub(crate) use std::fmt::Write;
pub(crate) use ::tengri::tui::ratatui::prelude::Position;
mod view_dialog; pub use self::view_dialog::*;
mod view_output; pub use self::view_output::*;
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 {
@ -23,7 +27,7 @@ impl App {
cache.bpm.view.clone(), cache.beat.view.clone(), cache.time.view.clone())
}
pub fn view_arranger (&self) -> impl Content<TuiOut> + use<'_> {
ArrangerView::new(self)
ArrangerView::new(self, self.editor.as_ref())
}
pub fn view_pool (&self) -> impl Content<TuiOut> + use<'_> {
self.pool().map(|p|Fixed::x(self.w_sidebar(), PoolView(self.is_editing(), p)))
@ -50,453 +54,75 @@ impl App {
self.sampler().map(|s|s.view_meters_output())
}
pub fn view_dialog (&self) -> impl Content<TuiOut> + use<'_> {
view_dialog(self)
}
}
impl App {
/// Spacing between tracks.
pub(crate) const TRACK_SPACING: usize = 0;
/// Default scene height.
pub(crate) const H_SCENE: usize = 2;
/// Default editor height.
pub(crate) const H_EDITOR: usize = 15;
pub(crate) fn inputs_with_sizes (&self) -> impl PortsSizes<'_> {
let mut y = 0;
self.midi_ins.iter().enumerate().map(move|(i, input)|{
let height = 1 + input.conn().len();
let data = (i, input.name(), input.conn(), y, y + height);
y += height;
data
})
}
pub(crate) fn tracks_with_sizes (&self) -> impl TracksSizes<'_> {
use Selection::*;
let mut x = 0;
let editing = self.is_editing();
let active = match self.selected() {
Track(t) if editing => Some(t),
TrackClip { track, .. } if editing => Some(track),
_ => None
};
let bigger = self.editor_w();
self.tracks().iter().enumerate().map(move |(index, track)|{
let width = if Some(index) == active.copied() { bigger } else { track.width.max(8) };
let data = (index, track, x, x + width);
x += width + App::TRACK_SPACING;
data
})
}
pub(crate) fn scenes_with_sizes (&self, editing: bool, height: usize, larger: usize)
-> impl ScenesSizes<'_>
{
use Selection::*;
let (selected_track, selected_scene) = match self.selected() {
Track(t) => (Some(*t), None),
Scene(s) => (None, Some(*s)),
TrackClip { track, scene } => (Some(*track), Some(*scene)),
_ => (None, None)
};
let mut y = 0;
self.scenes().iter().enumerate().map(move|(s, scene)|{
let active = editing && selected_track.is_some() && selected_scene == Some(s);
let height = if active { larger } else { height };
let data = (s, scene, y, y + height);
y += height;
data
})
}
pub fn update_clock (&self) {
ViewCache::update_clock(&self.view_cache, self.clock(), self.size.w() > 80)
}
}
pub(crate) struct ArrangerView<'a> {
app: &'a App,
is_editing: bool,
width: u16,
width_mid: u16,
width_side: u16,
inputs_count: usize,
inputs_height: u16,
outputs_count: usize,
outputs_height: u16,
scene_last: usize,
scene_count: usize,
scene_scroll: Fill<Fixed<u16, ScrollbarV>>,
scene_selected: Option<usize>,
scenes_height: u16,
track_scroll: Fill<Fixed<u16, ScrollbarH>>,
track_count: usize,
track_selected: Option<usize>,
tracks_height: u16,
show_debug_info: bool,
}
impl<'a> Content<TuiOut> for ArrangerView<'a> {
fn content (&self) -> impl Render<TuiOut> {
let ins = |x|Bsp::n(self.inputs(), x);
let tracks = |x|Bsp::s(self.tracks(), x);
let devices = |x|Bsp::s(self.devices(), x);
let outs = |x|Bsp::s(self.outputs(), x);
let bg = |x|Tui::bg(Reset, x);
//let track_scroll = |x|Bsp::s(&self.track_scroll, x);
//let scene_scroll = |x|Bsp::e(&self.scene_scroll, x);
outs(tracks(devices(ins(bg(self.scenes())))))
}
}
impl<'a> ArrangerView<'a> {
pub fn new (app: &'a App) -> Self {
Self {
app,
is_editing: app.is_editing(),
width: app.w(),
width_mid: app.w_tracks_area(),
width_side: app.w_sidebar(),
inputs_height: app.h_inputs(),
inputs_count: app.midi_ins.len(),
outputs_height: app.h_outputs(),
outputs_count: app.midi_outs.len(),
scenes_height: app.h_scenes_area(),
scene_selected: app.selected().scene(),
scene_count: app.scenes.len(),
scene_last: app.scenes.len().saturating_sub(1),
scene_scroll: Fill::y(Fixed::x(1, ScrollbarV {
offset: app.scene_scroll,
length: app.h_scenes_area() as usize,
total: app.h_scenes() as usize,
})),
tracks_height: app.h_tracks_area(),
track_count: app.tracks.len(),
track_selected: app.selected().track(),
track_scroll: Fill::x(Fixed::y(1, ScrollbarH {
offset: app.track_scroll,
length: app.h_tracks_area() as usize,
total: app.h_scenes() as usize,
})),
show_debug_info: false
}
}
/// Render input matrix.
pub(crate) fn inputs (&'a self) -> impl Content<TuiOut> + 'a {
Tui::bg(Reset, Bsp::s(
self.input_intos(),
Bsp::s(self.input_routes(), self.input_ports()),
When(app.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(app.dialog.as_ref().map(|dialog|match dialog {
Dialog::Menu =>
app.view_dialog_menu().boxed(),
Dialog::Help =>
app.view_dialog_help().boxed(),
Dialog::Save =>
app.view_dialog_save().boxed(),
Dialog::Load =>
app.view_dialog_load().boxed(),
Dialog::Options =>
app.view_dialog_options().boxed(),
Dialog::Device(index) =>
app.view_dialog_device(*index).boxed(),
Dialog::Message(message) =>
app.view_dialog_message(message).boxed(),
}))
)))
))
}
/// Render device switches.
pub(crate) fn devices (&'a self) -> impl Content<TuiOut> + 'a {
let Self { width_side, width_mid, track_count, track_selected, is_editing, .. } = self;
Tryptich::top(1)
.left(*width_side, button_3("d", "devices", format!("{}", 0), *is_editing))
.right(*width_side, button_2("D", "add device", *is_editing))
.middle(*width_mid, per_track_top(*width_mid, ||self.tracks_with_sizes_scrolled(),
move|index, track|{
let bg = if *track_selected == Some(index) {
track.color.light
} else {
track.color.base
};
let fg = Tui::g(224);
track.devices.get(0).map(|device|wrap(bg.rgb, fg, device.name()))
}))
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)))
}
/// Render track headers
pub(crate) fn tracks (&'a self) -> impl Content<TuiOut> + 'a {
let Self { width_side, width_mid, track_count, track_selected, is_editing, .. } = self;
Tryptich::center(3)
.left(*width_side, button_3("t", "track", format!("{}", *track_count), *is_editing))
.right(*width_side, button_2("T", "add track", *is_editing))
.middle(*width_mid, per_track(*width_mid, ||self.tracks_with_sizes_scrolled(),
|index, track|wrap(
if *track_selected == Some(index) {
track.color.light
} else {
track.color.base
}.rgb,
track.color.lightest.rgb,
Tui::bold(true, Fill::xy(Align::nw(&track.name)))
)))
pub fn view_dialog_help <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
Bsp::s(Tui::bold(true, "Help"), Bsp::s("", Map::south(1,
||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 })
.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()))))))))))
}
fn input_routes (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(self.inputs_height)
.left(self.width_side,
io_ports(Tui::g(224), Tui::g(32), ||self.app.inputs_with_sizes()))
.middle(self.width_mid,
per_track_top(self.width_mid, ||self.tracks_with_sizes_scrolled(),
move|_, &Track { color, .. }|io_conns(
color.dark.rgb,
color.darker.rgb,
||self.app.inputs_with_sizes()
)))
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))
}
fn input_ports (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(1)
.left(self.width_side,
button_3("i", "midi ins", format!("{}", self.inputs_count), self.is_editing))
.right(self.width_side,
button_2("I", "add midi in", self.is_editing))
.middle(self.width_mid,
per_track_top(
self.width_mid,
||self.tracks_with_sizes_scrolled(),
move|t, track|{
let rec = track.sequencer.recording;
let mon = track.sequencer.monitoring;
let rec = if rec { White } else { track.color.darkest.rgb };
let mon = if mon { White } else { track.color.darkest.rgb };
let bg = if self.track_selected == Some(t) {
track.color.light.rgb
} else {
track.color.base.rgb
};
//let bg2 = if t > 0 { track.color.base.rgb } else { Reset };
wrap(bg, Tui::g(224), Tui::bold(true, Fill::x(Bsp::e(
Tui::fg_bg(rec, bg, "Rec "),
Tui::fg_bg(mon, bg, "Mon "),
))))
}))
pub fn view_dialog_message <'a> (&'a self, message: &'a Message) -> impl Content<TuiOut> + use<'a> {
Bsp::s(message, Bsp::s("", "[ OK ]"))
}
fn input_intos (&'a self) -> impl Content<TuiOut> + 'a {
Tryptich::top(2)
.left(self.width_side,
Bsp::s(Align::e("Input:"), Align::e("Into clip:")))
.middle(self.width_mid,
per_track_top(
self.width_mid,
||self.tracks_with_sizes_scrolled(),
|_, _|Tui::bg(Reset, Align::c(Bsp::s(OctaveVertical::default(), " ------ ")))))
pub fn view_dialog_save <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
Bsp::s(
Fill::x(Align::w(Margin::xy(1, 1, Bsp::e(
Tui::bold(true, " Save project: "),
Shrink::x(3, Fixed::y(1, RepeatH("🭻"))))))),
Outer(true, Style::default().fg(Tui::g(96)))
.enclose(Fill::xy("todo file browser")))
}
/// Render scenes with clips
pub(crate) fn scenes (&'a self) -> impl Content<TuiOut> + 'a {
let Self {
width, width_side, width_mid,
scenes_height, scene_last, scene_selected,
track_selected, is_editing, app: App { editor, .. }, ..
} = self;
let scene_headers = Map::new(||self.scenes_with_scene_colors(),
move|(s, scene, y1, y2, previous): SceneWithColor, _|{
let height = (1 + y2 - y1) as u16;
let name = Some(scene.name.clone());
let content = Fill::x(Align::w(Tui::bold(true, Bsp::e("", name))));
let same_track = true;
let selected = same_track && *scene_selected == Some(s);
let neighbor = same_track && s > 0 && *scene_selected == Some(s - 1);
let is_last = *scene_last == s;
let theme = scene.color;
let fg = theme.lightest.rgb;
let bg = if selected { theme.light } else { theme.base }.rgb;
let hi = if let Some(previous) = previous {
if neighbor {
previous.light.rgb
} else {
previous.base.rgb
}
} else {
Reset
};
let lo = if is_last {
Reset
} else if selected {
theme.light.rgb
} else {
theme.base.rgb
};
Fill::x(map_south(y1 as u16, height, Fixed::y(height, Phat {
width: 0, height: 0, content, colors: [fg, bg, hi, lo]
})))
});
let scene_track_clips = per_track(*width_mid, ||self.tracks_with_sizes_scrolled(),
move|track_index, track|Map::new(move||self.scenes_with_track_colors(track_index),
move|(s, scene, y1, y2, previous): SceneWithColor<'a>, _|{
let (name, theme) = if let Some(clip) = &scene.clips[track_index] {
let clip = clip.read().unwrap();
(Some(clip.name.clone()), clip.color)
} else {
(None, ItemTheme::G[32])
};
let height = (1 + y2 - y1) as u16;
let content = Fill::x(Align::w(Tui::bold(true, Bsp::e("", name))));
let same_track = *track_selected == Some(track_index);
let selected = same_track && *scene_selected == Some(s);
let neighbor = same_track && s > 0 && *scene_selected == Some(s - 1);
let is_last = *scene_last == s;
let fg = theme.lightest.rgb;
let bg = if selected { theme.light } else { theme.base }.rgb;
let hi = if let Some(previous) = previous {
if neighbor {
previous.light.rgb
} else {
previous.base.rgb
}
} else {
Reset
};
let lo = if is_last {
Reset
} else if selected {
theme.light.rgb
} else {
theme.base.rgb
};
map_south(y1 as u16, height, Bsp::b(Fixed::y(height, Phat {
width: 0, height: 0, content, colors: [fg, bg, hi, lo]
}), When(
*is_editing && same_track && *scene_selected == Some(s),
editor
)))
}));
Tryptich::center(*scenes_height)
.left(*width_side, scene_headers)
.middle(*width_mid, scene_track_clips)
pub fn view_dialog_load <'a> (&'a self) -> 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("todo file browser")))
}
pub(crate) fn tracks_with_sizes_scrolled (&'a self)
-> impl TracksSizes<'a>
{
let width = self.width_mid;
self.app.tracks_with_sizes().map_while(move|(t, track, x1, x2)|{
(width > x2 as u16).then_some((t, track, x1, x2))
})
pub fn view_dialog_options <'a> (&'a self) -> impl Content<TuiOut> + use<'a> {
"TODO"
}
pub(crate) fn scenes_with_scene_colors (&self)
-> impl ScenesColors<'_>
{
self.app.scenes_with_sizes(self.is_editing, App::H_SCENE, App::H_EDITOR).map_while(
move|(s, scene, y1, y2)|if y2 as u16 > self.scenes_height {
None
} else {
Some((s, scene, y1, y2, if s == 0 {
None
} else {
Some(self.app.scenes()[s-1].color)
}))
})
}
pub(crate) fn scenes_with_track_colors (&self, track: usize)
-> impl ScenesColors<'_>
{
self.app.scenes_with_sizes(self.is_editing, App::H_SCENE, App::H_EDITOR).map_while(
move|(s, scene, y1, y2)|if y2 as u16 > self.scenes_height {
None
} else {
Some((s, scene, y1, y2, if s == 0 {
None
} else {
Some(self.app.scenes[s-1].clips[track].as_ref()
.map(|c|c.read().unwrap().color)
.unwrap_or(ItemTheme::G[32]))
}))
}
)
}
}
/// Iterator over scenes with their sizes and colors.
pub(crate) trait ScenesColors<'a> =
Iterator<Item=SceneWithColor<'a>>;
/// A scene with size and color.
pub(crate) type SceneWithColor<'a> =
(usize, &'a Scene, usize, usize, Option<ItemTheme>);
/// Define a type alias for iterators of sized items (columns).
macro_rules! def_sizes_iter {
($Type:ident => $($Item:ty),+) => {
pub(crate) trait $Type<'a> =
Iterator<Item=(usize, $(&'a $Item,)+ usize, usize)> + Send + Sync + 'a;
}
}
def_sizes_iter!(ScenesSizes => Scene);
def_sizes_iter!(TracksSizes => Track);
def_sizes_iter!(InputsSizes => JackMidiIn);
def_sizes_iter!(OutputsSizes => JackMidiOut);
def_sizes_iter!(PortsSizes => Arc<str>, [PortConnect]);
pub(crate) fn wrap (bg: Color, fg: Color, content: impl Content<TuiOut>) -> impl Content<TuiOut> {
let left = Tui::fg_bg(bg, Reset, Fixed::x(1, RepeatV("")));
let right = Tui::fg_bg(bg, Reset, Fixed::x(1, RepeatV("")));
Bsp::e(left, Bsp::w(right, Tui::fg_bg(fg, bg, content)))
}
pub(crate) fn button_2 <'a> (
key: impl Content<TuiOut> + 'a, label: impl Content<TuiOut> + 'a, editing: bool,
) -> impl Content<TuiOut> + 'a {
let key = Tui::fg_bg(Tui::g(0), Tui::orange(), Bsp::e(
Tui::fg_bg(Tui::orange(), Reset, ""),
Bsp::e(key, Tui::fg(Tui::g(96), ""))
));
let label = When::new(!editing, Tui::fg_bg(Tui::g(255), Tui::g(96), label));
Tui::bold(true, Bsp::e(key, label))
}
pub(crate) fn button_3 <'a, K, L, V> (
key: K,
label: L,
value: V,
editing: bool,
) -> impl Content<TuiOut> + 'a where
K: Content<TuiOut> + 'a,
L: Content<TuiOut> + 'a,
V: Content<TuiOut> + 'a,
{
let key = Tui::fg_bg(Tui::g(0), Tui::orange(),
Bsp::e(Tui::fg_bg(Tui::orange(), Reset, ""), Bsp::e(key, Tui::fg(if editing {
Tui::g(128)
} else {
Tui::g(96)
}, ""))));
let label = Bsp::e(
When::new(!editing, Bsp::e(
Tui::fg_bg(Tui::g(255), Tui::g(96), label),
Tui::fg_bg(Tui::g(128), Tui::g(96), ""),
)),
Bsp::e(
Tui::fg_bg(Tui::g(224), Tui::g(128), value),
Tui::fg_bg(Tui::g(128), Reset, ""),
));
Tui::bold(true, Bsp::e(key, label))
}
pub(crate) fn heading <'a> (
@ -510,57 +136,6 @@ pub(crate) fn heading <'a> (
Fill::xy(Align::w(Bsp::s(Fill::x(Align::w(button_3(key, label, count, editing))), content)))
}
pub(crate) fn io_ports <'a, T: PortsSizes<'a>> (
fg: Color, bg: Color, iter: impl Fn()->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Map::new(iter, move|(
index, name, connections, y, y2
): (usize, &'a Arc<str>, &'a [PortConnect], usize, usize), _|
map_south(y as u16, (y2-y) as u16, Bsp::s(
Fill::x(Tui::bold(true, Tui::fg_bg(fg, bg, Align::w(Bsp::e(" 󰣲 ", name))))),
Map::new(||connections.iter(), move|connect: &'a PortConnect, index|map_south(index as u16, 1,
Fill::x(Align::w(Tui::bold(false, Tui::fg_bg(fg, bg,
&connect.info)))))))))
}
pub(crate) fn io_conns <'a, T: PortsSizes<'a>> (
fg: Color, bg: Color, iter: impl Fn()->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Map::new(iter, move|(
index, name, connections, y, y2
): (usize, &'a Arc<str>, &'a [PortConnect], usize, usize), _|
map_south(y as u16, (y2-y) as u16, Bsp::s(
Fill::x(Tui::bold(true, wrap(bg, fg, Fill::x(Align::w("▞▞▞▞ ▞▞▞▞"))))),
Map::new(||connections.iter(), move|connect, index|map_south(index as u16, 1,
Fill::x(Align::w(Tui::bold(false, wrap(bg, fg, Fill::x(""))))))))))
}
pub(crate) fn per_track_top <'a, T: Content<TuiOut> + 'a, U: TracksSizes<'a>> (
width: u16,
tracks: impl Fn() -> U + Send + Sync + 'a,
callback: impl Fn(usize, &'a Track)->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
Align::x(Tui::bg(Reset, Map::new(tracks,
move|(index, track, x1, x2): (usize, &'a Track, usize, usize), _|{
let width = (x2 - x1) as u16;
map_east(x1 as u16, width, Fixed::x(width, Tui::fg_bg(
track.color.lightest.rgb,
track.color.base.rgb,
callback(index, track))))})))
}
pub(crate) fn per_track <'a, T: Content<TuiOut> + 'a, U: TracksSizes<'a>> (
width: u16,
tracks: impl Fn() -> U + Send + Sync + 'a,
callback: impl Fn(usize, &'a Track)->T + Send + Sync + 'a
) -> impl Content<TuiOut> + 'a {
per_track_top(
width,
tracks,
move|index, track|Fill::y(Align::y(callback(index, track)))
)
}
/// Clear a pre-allocated buffer, then write into it.
#[macro_export] macro_rules! rewrite {
($buf:ident, $($rest:tt)*) => { |$buf,_,_|{ $buf.clear(); write!($buf, $($rest)*) } }