wip: borrow checker battles

This commit is contained in:
🪞👃🪞 2024-09-04 16:57:48 +03:00
parent 1d4db3c629
commit 7fbb40fad6
38 changed files with 778 additions and 708 deletions

View file

@ -1,5 +1,4 @@
pub(crate) use tek_core::*;
pub(crate) use tek_core::ratatui::prelude::*;
pub(crate) use tek_core::crossterm::event::{KeyCode, KeyModifiers};
pub(crate) use tek_core::midly::{num::u7, live::LiveEvent, MidiMessage};
pub(crate) use tek_core::jack::*;
@ -14,7 +13,6 @@ submod! {
mixer
mixer_cli
mixer_handle
mixer_render
track
track_view
track_handle

View file

@ -1,13 +1,12 @@
use crate::*;
pub struct Mixer {
pub struct Mixer<T, U> {
pub name: String,
pub tracks: Vec<Track>,
pub tracks: Vec<Track<T, U>>,
pub selected_track: usize,
pub selected_column: usize,
}
impl Mixer {
impl<T, U> Mixer<T, U> {
pub fn new (name: &str) -> Usually<Self> {
let (client, _status) = Client::new(name, ClientOptions::NO_START_SERVER)?;
Ok(Self {
@ -22,17 +21,21 @@ impl Mixer {
self.tracks.push(track);
Ok(self)
}
pub fn track (&self) -> Option<&Track> {
pub fn track (&self) -> Option<&Track<T, U>> {
self.tracks.get(self.selected_track)
}
}
process!(Mixer = process);
fn process (
_: &mut Mixer,
_: &Client,
_: &ProcessScope
) -> Control {
Control::Continue
impl<'a> Render<TuiOutput<'a>, Rect> for Mixer<TuiOutput<'a>, Rect> {
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let mut tracks = Split::right();
for channel in self.tracks.iter() {
tracks = tracks.add_ref(channel)
}
tracks.render(to)
}
}
impl<T, U> Process for Mixer<T, U> {
fn process (&mut self, _: &Client, _: &ProcessScope) -> Control {
Control::Continue
}
}

View file

@ -1,6 +1,5 @@
use tek_core::clap::{self, Parser};
use crate::*;
#[derive(Debug, Parser)]
#[command(version, about, long_about = None)]
pub struct MixerCli {
@ -9,8 +8,7 @@ pub struct MixerCli {
/// Number of tracks
#[arg(short, long)] channels: Option<usize>,
}
impl Mixer {
impl<T, U> Mixer<T, U> {
pub fn from_args () -> Usually<Self> {
let args = MixerCli::parse();
let mut mix = Self::new("")?;

View file

@ -1,56 +1,57 @@
use crate::*;
handle!(Mixer = handle_mixer);
impl <T, U> Handle for Mixer<T, U> {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
if let AppEvent::Input(crossterm::event::Event::Key(event)) = event {
match event.code {
//KeyCode::Char('c') => {
//if event.modifiers == KeyModifiers::CONTROL {
//self.exit();
//}
//},
KeyCode::Down => {
self.selected_track = (self.selected_track + 1) % self.tracks.len();
println!("{}", self.selected_track);
return Ok(true)
},
KeyCode::Up => {
if self.selected_track == 0 {
self.selected_track = self.tracks.len() - 1;
} else {
self.selected_track -= 1;
}
println!("{}", self.selected_track);
return Ok(true)
},
KeyCode::Left => {
if self.selected_column == 0 {
self.selected_column = 6
} else {
self.selected_column -= 1;
}
return Ok(true)
},
KeyCode::Right => {
if self.selected_column == 6 {
self.selected_column = 0
} else {
self.selected_column += 1;
}
return Ok(true)
},
_ => {
println!("\n{event:?}");
}
}
}
Ok(false)
}
}
//pub const ACTIONS: [(&'static str, &'static str);2] = [
//("+/-", "Adjust"),
//("Ins/Del", "Add/remove track"),
//];
pub fn handle_mixer (state: &mut Mixer, event: &AppEvent) -> Usually<bool> {
if let AppEvent::Input(crossterm::event::Event::Key(event)) = event {
match event.code {
//KeyCode::Char('c') => {
//if event.modifiers == KeyModifiers::CONTROL {
//state.exit();
//}
//},
KeyCode::Down => {
state.selected_track = (state.selected_track + 1) % state.tracks.len();
println!("{}", state.selected_track);
return Ok(true)
},
KeyCode::Up => {
if state.selected_track == 0 {
state.selected_track = state.tracks.len() - 1;
} else {
state.selected_track -= 1;
}
println!("{}", state.selected_track);
return Ok(true)
},
KeyCode::Left => {
if state.selected_column == 0 {
state.selected_column = 6
} else {
state.selected_column -= 1;
}
return Ok(true)
},
KeyCode::Right => {
if state.selected_column == 6 {
state.selected_column = 0
} else {
state.selected_column += 1;
}
return Ok(true)
},
_ => {
println!("\n{event:?}");
}
}
}
Ok(false)
}

View file

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

View file

@ -12,12 +12,12 @@ pub struct Plugin {
handle!(Plugin |self, e| handle_keymap(self, e, KEYMAP_PLUGIN));
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;
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let Rect { x, y, height, .. } = to.area;
let mut width = 20u16;
match &state.plugin {
match &self.plugin {
Some(PluginKind::LV2(LV2Plugin { port_list, instance, .. })) => {
let start = state.selected.saturating_sub((height as usize / 2).saturating_sub(1));
let start = self.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 {
@ -30,11 +30,12 @@ impl<'a> Render<TuiOutput<'a>, Rect> for Plugin {
//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 {
let style = if i == self.selected {
Some(Style::default().green())
} else {
None
})?;
} ;
label.blit(to.buffer, x + 2, y + 1 + i as u16 - start as u16, style)?;
} else {
break
}
@ -42,8 +43,8 @@ impl<'a> Render<TuiOutput<'a>, Rect> for Plugin {
},
_ => {}
};
draw_header(state, buf, area.x, area.y, width)?;
Ok(Some(Rect { width, ..area }))
draw_header(self, to.buffer, to.area.x, to.area.y, width)?;
Ok(Some(Rect { width, ..to.area }))
}
}
@ -56,7 +57,7 @@ pub enum PluginKind {
VST3,
}
impl Plugin {
pub fn new_lv2 (name: &str, path: &str) -> Usually<JackDevice> {
pub fn new_lv2 <T, U> (name: &str, path: &str) -> Usually<JackDevice<T, U>> {
let plugin = LV2Plugin::new(path)?;
jack_from_lv2(name, &plugin.plugin)?
.run(|ports|Box::new(Self {

View file

@ -22,7 +22,7 @@ pub struct LV2Plugin {
}
impl LV2Plugin {
pub fn from_edn <'e> (args: &[Edn<'e>]) -> Usually<JackDevice> {
pub fn from_edn <'e, T, U> (args: &[Edn<'e>]) -> Usually<JackDevice<T, U>> {
let mut name = String::new();
let mut path = String::new();
edn!(edn in args {

View file

@ -23,20 +23,20 @@ pub struct AddSampleModal {
exit!(AddSampleModal);
impl<'a> Render<TuiOutput<'a>, Rect> for AddSampleModal {
fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
make_dim(to.buffer);
let area = center_box(
to.area,
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_char(buf, area, ' ');
fill_fg(to.buffer, area, Color::Reset);
fill_bg(to.buffer, area, Nord::bg_lo(true, true));
fill_char(to.buffer, area, ' ');
format!("{}", &self.dir.to_string_lossy())
.blit(buf, area.x+2, area.y+1, Some(Style::default().bold()))?;
.blit(to.buffer, area.x+2, area.y+1, Some(Style::default().bold()))?;
"Select sample:"
.blit(buf, area.x+2, area.y+2, Some(Style::default().bold()))?;
.blit(to.buffer, area.x+2, area.y+2, Some(Style::default().bold()))?;
for (i, (is_dir, name)) in self.subdirs.iter()
.map(|path|(true, path))
.chain(self.files.iter().map(|path|(false, path)))
@ -49,13 +49,13 @@ impl<'a> Render<TuiOutput<'a>, Rect> for AddSampleModal {
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 {
line.blit(to.buffer, 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)
Lozenge(Style::default()).draw(to)
}
}

View file

@ -19,11 +19,11 @@ process!(Sampler = Sampler::process);
handle!(Sampler |self, event| handle_keymap(self, event, KEYMAP_SAMPLER));
impl<'a> Render<TuiOutput<'a>, Rect> for Sampler {
fn render (&self, to: &mut TuiOutput<'a>) -> Usually<Rect> {
let Rect { x, y, height, .. } = area;
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
let Rect { x, y, height, .. } = to.area;
let style = Style::default().gray();
let title = format!(" {} ({})", self.name, self.voices.read().unwrap().len());
title.blit(buf, x+1, y, Some(style.white().bold().not_dim()))?;
title.blit(to.buffer, x+1, y, Some(style.white().bold().not_dim()))?;
let mut width = title.len() + 2;
let mut y1 = 1;
let mut j = 0;
@ -35,12 +35,14 @@ impl<'a> Render<TuiOutput<'a>, Rect> for Sampler {
break
}
let active = j == self.cursor.0;
width = width.max(draw_sample(buf, x, y + y1, note, &*sample.read().unwrap(), active)?);
width = width.max(
draw_sample(to.buffer, x, y + y1, note, &*sample.read().unwrap(), active)?
);
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 })
Ok(Some(Rect { x, y, width: (width as u16).min(to.area.width), height }))
}
}
@ -103,7 +105,7 @@ pub const KEYMAP_SAMPLER: &'static [KeyBinding<Sampler>] = keymap!(Sampler {
});
impl Sampler {
pub fn from_edn <'e> (args: &[Edn<'e>]) -> Usually<JackDevice> {
pub fn from_edn <'e, T, U> (args: &[Edn<'e>]) -> Usually<JackDevice<T, U>> {
let mut name = String::new();
let mut dir = String::new();
let mut samples = BTreeMap::new();
@ -132,7 +134,9 @@ impl Sampler {
Self::new(&name, Some(samples))
}
pub fn new (name: &str, mapped: Option<BTreeMap<u7, Arc<RwLock<Sample>>>>) -> Usually<JackDevice> {
pub fn new <T, U> (
name: &str, mapped: Option<BTreeMap<u7, Arc<RwLock<Sample>>>>
) -> Usually<JackDevice<T, U>> {
Jack::new(name)?
.midi_in("midi")
.audio_in("recL")

View file

@ -3,18 +3,16 @@ use tek_core::edn;
/// A sequencer track.
#[derive(Debug)]
pub struct Track {
pub struct Track<T, U> {
pub name: String,
/// Inputs and outputs of 1st and last device
pub ports: JackPorts,
/// Device chain
pub devices: Vec<JackDevice>,
pub devices: Vec<JackDevice<T, U>>,
/// Device selector
pub device: usize,
}
impl Track {
impl<T, U> Track<T, U> {
pub fn new (name: &str) -> Usually<Self> {
Ok(Self {
name: name.to_string(),
@ -23,15 +21,14 @@ impl Track {
device: 0,
})
}
fn get_device_mut (&self, i: usize) -> Option<RwLockWriteGuard<Box<dyn Device>>> {
fn get_device_mut (&self, i: usize) -> Option<RwLockWriteGuard<Box<dyn Device<T, U>>>> {
self.devices.get(i).map(|d|d.state.write().unwrap())
}
pub fn device_mut (&self) -> Option<RwLockWriteGuard<Box<dyn Device>>> {
pub fn device_mut (&self) -> Option<RwLockWriteGuard<Box<dyn Device<T, U>>>> {
self.get_device_mut(self.device)
}
/// Add a device to the end of the chain.
pub fn append_device (&mut self, device: JackDevice) -> Usually<&mut JackDevice> {
pub fn append_device (&mut self, device: JackDevice<T, U>) -> Usually<&mut JackDevice<T, U>> {
self.devices.push(device);
let index = self.devices.len() - 1;
Ok(&mut self.devices[index])
@ -57,7 +54,7 @@ impl Track {
let mut _gain = 0.0f64;
let mut track = Self::new("")?;
#[allow(unused_mut)]
let mut devices: Vec<JackDevice> = vec![];
let mut devices: Vec<JackDevice<T, U>> = vec![];
edn!(edn in args {
Edn::Map(map) => {
if let Some(Edn::Str(n)) = map.get(&Edn::Key(SYM_NAME)) {
@ -98,8 +95,7 @@ impl Track {
}
Ok(track)
}
pub fn add_device (&mut self, device: JackDevice) {
pub fn add_device (&mut self, device: JackDevice<T, U>) {
self.devices.push(device);
}
}

View file

@ -1,31 +1,42 @@
use crate::*;
handle!(Track |self, event| handle_keymap(self, event, KEYMAP_TRACK));
/// Key bindings for chain section.
pub const KEYMAP_TRACK: &'static [KeyBinding<Track>] = keymap!(Track {
[Up, NONE, "chain_cursor_up", "move cursor up", |_: &mut Track| {
Ok(true)
}],
[Down, NONE, "chain_cursor_down", "move cursor down", |_: &mut Track| {
Ok(true)
}],
[Left, NONE, "chain_cursor_left", "move cursor left", |app: &mut Track| {
//if let Some(track) = app.arranger.track_mut() {
//track.device = track.device.saturating_sub(1);
//return Ok(true)
//}
Ok(false)
}],
[Right, NONE, "chain_cursor_right", "move cursor right", |app: &mut Track| {
//if let Some(track) = app.arranger.track_mut() {
//track.device = (track.device + 1).min(track.devices.len().saturating_sub(1));
//return Ok(true)
//}
Ok(false)
}],
[Char('`'), NONE, "chain_mode_switch", "switch the display mode", |app: &mut Track| {
//app.chain_mode = !app.chain_mode;
Ok(true)
}],
});
impl <T, U> Handle for Track<T, U> {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
match event {
AppEvent::Input(crossterm::event::Event::Key(event)) => {
for (code, modifiers, _, _, command) in [
key!(Up, NONE, "chain_cursor_up", "move cursor up", || {
Ok(true)
}),
key!(Down, NONE, "chain_cursor_down", "move cursor down", || {
Ok(true)
}),
key!(Left, NONE, "chain_cursor_left", "move cursor left", || {
//if let Some(track) = app.arranger.track_mut() {
//track.device = track.device.saturating_sub(1);
//return Ok(true)
//}
Ok(false)
}),
key!(Right, NONE, "chain_cursor_right", "move cursor right", || {
//if let Some(track) = app.arranger.track_mut() {
//track.device = (track.device + 1).min(track.devices.len().saturating_sub(1));
//return Ok(true)
//}
Ok(false)
}),
key!(Char('`'), NONE, "chain_mode_switch", "switch the display mode", || {
//app.chain_mode = !app.chain_mode;
Ok(true)
}),
].iter() {
if *code == event.code && modifiers.bits() == event.modifiers.bits() {
return command()
}
}
return Ok(false)
},
_ => Ok(false)
}
}
}

View file

@ -1,7 +1,7 @@
use crate::*;
use tek_core::Direction;
impl<'a> Render<TuiOutput<'a>, Rect> for Track {
impl<'a> Render<TuiOutput<'a>, Rect> for Track<TuiOutput<'a>, Rect> {
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
TrackView {
chain: Some(&self),
@ -24,37 +24,37 @@ impl<'a> Render<TuiOutput<'a>, Rect> for Track {
}.render(to)
}
}
pub struct TrackView<'a> {
pub chain: Option<&'a Track>,
pub struct TrackView<'a, T, U> {
pub chain: Option<&'a Track<T, U>>,
pub direction: Direction,
pub focused: bool,
pub entered: bool,
}
impl<'a> Render<TuiOutput<'a>, Rect> for TrackView<'a> {
impl<'a> Render<TuiOutput<'a>, Rect> for TrackView<'a, TuiOutput<'a>, Rect> {
fn render (&self, to: &mut TuiOutput<'a>) -> Perhaps<Rect> {
if let Some(chain) = self.chain {
match self.direction {
Direction::Down => area.width = area.width.min(40),
Direction::Right => area.width = area.width.min(10),
Direction::Down => to.area.width = to.area.width.min(40),
Direction::Right => to.area.width = to.area.width.min(10),
_ => { unimplemented!() },
}
fill_bg(buf, area, Nord::bg_lo(self.focused, self.entered));
fill_bg(to.buffer, to.area, Nord::bg_lo(self.focused, self.entered));
let mut split = Split::new(self.direction);
for device in chain.devices.as_slice().iter() {
split = split.add_ref(device);
}
let (area, areas) = split.render_areas(buf, area)?;
let (area, areas) = split.render_areas(to)?;
if self.focused && self.entered && areas.len() > 0 {
Corners(Style::default().green().not_dim()).draw(buf, areas[0])?;
Corners(Style::default().green().not_dim()).draw(to.buffer, areas[0])?;
}
Ok(area)
Ok(Some(area))
} else {
let Rect { x, y, width, height } = area;
let Rect { x, y, width, height } = to.area;
let label = "No chain selected";
let x = x + (width - label.len() as u16) / 2;
let y = y + height / 2;
label.blit(buf, x, y, Some(Style::default().dim().bold()))?;
Ok(area)
label.blit(to.buffer, x, y, Some(Style::default().dim().bold()))?;
Ok(Some(to.area))
}
}
}