mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
ughhh needs special case
This commit is contained in:
parent
cf1fd5b45a
commit
a362028ae7
4 changed files with 124 additions and 97 deletions
|
|
@ -92,28 +92,36 @@ impl<T: Context<U>, U> Context<U> for Option<T> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
/// Implement `Context` for a context and content type.
|
/// Implement `Context` for a context and the boolean type.
|
||||||
///
|
///
|
||||||
/// This enables support for layout expressions.
|
/// This enables support for boolean literals.
|
||||||
#[macro_export] macro_rules! provide_content {
|
#[macro_export] macro_rules! provide_bool {
|
||||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
// Provide a value that may also be a numeric literal in the EDN, to a generic implementation.
|
||||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
($type:ty:|$self:ident:<$T:ident:$Trait:path>|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||||
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<E> + 'a>> {
|
impl<$T: $Trait> Context<$type> for $T {
|
||||||
Some(match (atom.kind(), atom.text()) {
|
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||||
|
Some(match atom {
|
||||||
|
Value::Num(n) => match *n { 0 => false, _ => true },
|
||||||
|
Value::Sym(":false") | Value::Sym(":f") => false,
|
||||||
|
Value::Sym(":true") | Value::Sym(":t") => true,
|
||||||
|
$(Value::Sym($pat) => $expr,)*
|
||||||
|
_ => return Context::get(self, atom)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Provide a value that may also be a numeric literal in the EDN, to a concrete implementation.
|
||||||
|
($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||||
|
impl Context<$type> for $State {
|
||||||
|
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||||
|
Some(match atom {
|
||||||
|
Value::Num(n) => match *n { 0 => false, _ => true },
|
||||||
|
Value::Sym(":false") | Value::Sym(":f") => false,
|
||||||
|
Value::Sym(":true") | Value::Sym(":t") => true,
|
||||||
$(Value::Sym($pat) => $expr,)*
|
$(Value::Sym($pat) => $expr,)*
|
||||||
_ => return None
|
_ => return None
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
|
||||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
|
||||||
fn get (&'a $self, atom: &'a Value) -> Option<Box<dyn Render<$Output> + 'a>> {
|
|
||||||
Some(match (atom.kind(), atom.text()) {
|
|
||||||
$(Value::Sym($pat) => $expr,)*
|
|
||||||
_ => return None
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::*;
|
use crate::*;
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
|
#[derive(Copy, Clone, Debug, Default, PartialEq)] pub struct TokenIter<'a>(pub &'a str);
|
||||||
const_iter!(<'a>|self: TokenIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
|
const_iter!(<'a>|self: TokenIter<'a>| => Token<'a> => self.next_mut().map(|(result, _)|result));
|
||||||
impl<'a> From<&'a str> for TokenIter<'a> {fn from (source: &'a str) -> Self{Self::new(source)}}
|
impl<'a> From<&'a str> for TokenIter<'a> {fn from (source: &'a str) -> Self{Self::new(source)}}
|
||||||
impl<'a> TokenIter<'a> {
|
impl<'a> TokenIter<'a> {
|
||||||
|
|
|
||||||
|
|
@ -7,44 +7,35 @@ use Value::*;
|
||||||
/// * render callback (implementation of [Content])
|
/// * render callback (implementation of [Content])
|
||||||
/// * value providers (implementations of [Context])
|
/// * value providers (implementations of [Context])
|
||||||
#[macro_export] macro_rules! view {
|
#[macro_export] macro_rules! view {
|
||||||
($Output:ty: |$self:ident: $App:ty| $content:expr; {
|
($Output:ty: |$self:ident: $App:ty| $content:expr $(;{
|
||||||
$( $type:ty { $($sym:literal => $value:expr),* } );*
|
$( $type:ty { $($sym:literal => $value:expr),* } );*
|
||||||
}) => {
|
})?) => {
|
||||||
impl Content<$Output> for $App {
|
impl Content<$Output> for $App {
|
||||||
fn content(&$self) -> impl Render<$Output> { $content }
|
fn content(&$self) -> impl Render<$Output> { $content }
|
||||||
}
|
}
|
||||||
$(
|
$($(
|
||||||
impl Context<$type> for $App {
|
impl Context<$type> for $App {
|
||||||
fn get (&$self, atom: &Value) -> Option<$type> {
|
fn get (&$self, atom: &Value) -> Option<$type> {
|
||||||
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
Some(match atom.to_ref() { $(Atom::Sym($sym) => $value,)* _ => return None })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)*
|
)*)?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Implements `Context` for content components and expressions
|
|
||||||
#[macro_export] macro_rules! provide_content {
|
pub struct View<'a, T>(pub &'a T, pub TokenIter<'a>);
|
||||||
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
impl<'a, O: Output + 'a, T: ViewContext<'a, O>> Content<O> for View<'a, T> {
|
||||||
impl<'a, E: Output> Context<'a, Box<dyn Render<E> + 'a>> for $State {
|
fn content (&self) -> impl Render<O> {
|
||||||
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + 'a>> {
|
let iter = self.1.clone();
|
||||||
Some(match atom.to_ref() {
|
while let Some((Token { value, .. }, _)) = iter.next() {
|
||||||
$(Atom::Sym($pat) => $expr),*,
|
if let Some(content) = self.0.get_content(&value) {
|
||||||
_ => return None
|
return Some(content)
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
|
||||||
impl<'a> Context<'a, Box<dyn Render<$Output> + 'a>> for $State {
|
|
||||||
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + 'a>> {
|
|
||||||
Some(match atom.to_ref() {
|
|
||||||
$(Atom::Sym($pat) => $expr),*,
|
|
||||||
_ => return None
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Renders from EDN source and context.
|
/// Renders from EDN source and context.
|
||||||
///
|
///
|
||||||
/// Generic over:
|
/// Generic over:
|
||||||
|
|
@ -163,3 +154,28 @@ pub type AtomRenderCallback<'a, O, State> =
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Implement `Context` for a context and content type.
|
||||||
|
///
|
||||||
|
/// This enables support for layout expressions.
|
||||||
|
#[macro_export] macro_rules! provide_content {
|
||||||
|
(|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||||
|
impl<E: Output> Context<Box<dyn Render<E> + '_>> for $State {
|
||||||
|
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<E> + '_>> {
|
||||||
|
Some(match atom {
|
||||||
|
$(Value::Sym($pat) => $expr,)*
|
||||||
|
_ => return None
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
($Output:ty: |$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => {
|
||||||
|
impl Context<Box<dyn Render<$Output> + '_>> for $State {
|
||||||
|
fn get (&$self, atom: &Value) -> Option<Box<dyn Render<$Output> + '_>> {
|
||||||
|
Some(match atom {
|
||||||
|
$(Value::Sym($pat) => $expr,)*
|
||||||
|
_ => return None
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
115
tek/src/lib.rs
115
tek/src/lib.rs
|
|
@ -118,8 +118,6 @@ impl TekCli {
|
||||||
#[derive(Default, Debug)] struct Tek {
|
#[derive(Default, Debug)] struct Tek {
|
||||||
/// Must not be dropped for the duration of the process
|
/// Must not be dropped for the duration of the process
|
||||||
pub jack: Arc<RwLock<JackConnection>>,
|
pub jack: Arc<RwLock<JackConnection>>,
|
||||||
/// View definition
|
|
||||||
pub atom: String,
|
|
||||||
/// Source of time
|
/// Source of time
|
||||||
pub clock: Clock,
|
pub clock: Clock,
|
||||||
/// Theme
|
/// Theme
|
||||||
|
|
@ -143,11 +141,14 @@ impl TekCli {
|
||||||
pub editing: AtomicBool,
|
pub editing: AtomicBool,
|
||||||
pub history: Vec<TekCommand>,
|
pub history: Vec<TekCommand>,
|
||||||
|
|
||||||
keys: TokenIter<'static>,
|
/// View definition
|
||||||
keys_clip: TokenIter<'static>,
|
pub view: TokenIter<'static>,
|
||||||
keys_track: TokenIter<'static>,
|
// Input definitions
|
||||||
keys_scene: TokenIter<'static>,
|
pub keys: TokenIter<'static>,
|
||||||
keys_mix: TokenIter<'static>,
|
pub keys_clip: TokenIter<'static>,
|
||||||
|
pub keys_track: TokenIter<'static>,
|
||||||
|
pub keys_scene: TokenIter<'static>,
|
||||||
|
pub keys_mix: TokenIter<'static>,
|
||||||
}
|
}
|
||||||
has_size!(<TuiOut>|self: Tek|&self.size);
|
has_size!(<TuiOut>|self: Tek|&self.size);
|
||||||
has_clock!(|self: Tek|self.clock);
|
has_clock!(|self: Tek|self.clock);
|
||||||
|
|
@ -174,37 +175,36 @@ provide_num!(usize: |self: Tek| {
|
||||||
":track" => self.selected.track().unwrap_or(0),
|
":track" => self.selected.track().unwrap_or(0),
|
||||||
":track-next" => (self.selected.track().unwrap_or(0) + 1).min(self.tracks.len()),
|
":track-next" => (self.selected.track().unwrap_or(0) + 1).min(self.tracks.len()),
|
||||||
":track-prev" => self.selected.track().unwrap_or(0).saturating_sub(1) });
|
":track-prev" => self.selected.track().unwrap_or(0).saturating_sub(1) });
|
||||||
view!(TuiOut: |self: Tek| self.size.of(AtomView::from_source(self, self.atom.as_ref())); {
|
view!(TuiOut: |self: Tek| self.size.of(View(self, self.view)));
|
||||||
bool {};
|
provide_bool!(bool: |self: Tek| {});
|
||||||
isize {};
|
provide_num!(isize: |self: Tek| {});
|
||||||
Color {};
|
provide!(Color: |self: Tek| {});
|
||||||
Selection {};
|
provide!(Selection: |self: Tek| {});
|
||||||
Arc<RwLock<MidiClip>> {};
|
provide!(Arc<RwLock<MidiClip>>: |self: Tek| {});
|
||||||
Option<Arc<RwLock<MidiClip>>> {};
|
provide!(Option<Arc<RwLock<MidiClip>>>: |self: Tek| {});
|
||||||
// Provide sizes:
|
provide_num!(u16: |self: Tek| {
|
||||||
u16 {
|
":sidebar-w" => self.sidebar_w(),
|
||||||
":sidebar-w" => self.sidebar_w(),
|
":sample-h" => if self.is_editing() { 0 } else { 5 },
|
||||||
":sample-h" => if self.is_editing() { 0 } else { 5 },
|
":samples-w" => if self.is_editing() { 4 } else { 11 },
|
||||||
":samples-w" => if self.is_editing() { 4 } else { 11 },
|
":samples-y" => if self.is_editing() { 1 } else { 0 },
|
||||||
":samples-y" => if self.is_editing() { 1 } else { 0 },
|
":pool-w" => if self.is_editing() { 5 } else {
|
||||||
":pool-w" => if self.is_editing() { 5 } else {
|
let w = self.size.w();
|
||||||
let w = self.size.w();
|
if w > 60 { 20 } else if w > 40 { 15 } else { 10 } }
|
||||||
if w > 60 { 20 } else if w > 40 { 15 } else { 10 } } };
|
});
|
||||||
// Provide components:
|
provide_content!(TuiOut: |self: Tek| {
|
||||||
Box<dyn Render<TuiOut> + 'a> {
|
":editor" => (&self.editor).boxed(),
|
||||||
":editor" => (&self.editor).boxed(),
|
":pool" => self.view_pool().boxed(),
|
||||||
":pool" => self.view_pool().boxed(),
|
":sample" => self.view_sample(self.is_editing()).boxed(),
|
||||||
":sample" => self.view_sample(self.is_editing()).boxed(),
|
":sampler" => self.view_sampler(self.is_editing(), &self.editor).boxed(),
|
||||||
":sampler" => self.view_sampler(self.is_editing(), &self.editor).boxed(),
|
":status" => self.view_editor().boxed(),
|
||||||
":status" => self.view_editor().boxed(),
|
":toolbar" => self.view_clock().boxed(),
|
||||||
":toolbar" => self.view_clock().boxed(),
|
":tracks" => self.view_row(self.w(), 3, self.track_header(), self.track_cells()).boxed(),
|
||||||
":tracks" => self.view_row(self.w(), 3, self.track_header(), self.track_cells()).boxed(),
|
":inputs" => self.view_row(self.w(), 3, self.input_header(), self.input_cells()).boxed(),
|
||||||
":inputs" => self.view_row(self.w(), 3, self.input_header(), self.input_cells()).boxed(),
|
":outputs" => self.view_row(self.w(), 3, self.output_header(), self.output_cells()).boxed(),
|
||||||
":outputs" => self.view_row(self.w(), 3, self.output_header(), self.output_cells()).boxed(),
|
":scenes" => Outer(false, Style::default().fg(TuiTheme::g(0))).enclose_bg(self.view_row(
|
||||||
":scenes" => Outer(false, Style::default().fg(TuiTheme::g(0))).enclose_bg(self.view_row(
|
self.w(), self.size.h().saturating_sub(12) as u16,
|
||||||
self.w(), self.size.h().saturating_sub(12) as u16,
|
self.scene_header(), self.clip_columns()
|
||||||
self.scene_header(), self.clip_columns()
|
)).boxed() });
|
||||||
)).boxed() }});
|
|
||||||
impl Tek {
|
impl Tek {
|
||||||
fn new_clock (
|
fn new_clock (
|
||||||
jack: &Arc<RwLock<JackConnection>>,
|
jack: &Arc<RwLock<JackConnection>>,
|
||||||
|
|
@ -212,12 +212,17 @@ impl Tek {
|
||||||
midi_froms: &[PortConnection], midi_tos: &[PortConnection],
|
midi_froms: &[PortConnection], midi_tos: &[PortConnection],
|
||||||
) -> Usually<Self> {
|
) -> Usually<Self> {
|
||||||
let tek = Self {
|
let tek = Self {
|
||||||
atom: include_str!("./view_transport.edn").to_string(),
|
view: TokenIter(include_str!("./view_transport.edn")),
|
||||||
jack: jack.clone(),
|
jack: jack.clone(),
|
||||||
color: ItemPalette::random(),
|
color: ItemPalette::random(),
|
||||||
clock: Clock::new(jack, bpm),
|
clock: Clock::new(jack, bpm),
|
||||||
midi_ins: vec![JackPort::<MidiIn>::new(jack, "GlobalI", midi_froms)?],
|
midi_ins: vec![JackPort::<MidiIn>::new(jack, "GlobalI", midi_froms)?],
|
||||||
midi_outs: vec![JackPort::<MidiOut>::new(jack, "GlobalO", midi_tos)?],
|
midi_outs: vec![JackPort::<MidiOut>::new(jack, "GlobalO", midi_tos)?],
|
||||||
|
keys: TokenIter(KEYS_APP),
|
||||||
|
keys_clip: TokenIter(KEYS_CLIP),
|
||||||
|
keys_track: TokenIter(KEYS_TRACK),
|
||||||
|
keys_scene: TokenIter(KEYS_SCENE),
|
||||||
|
keys_mix: TokenIter(KEYS_MIX),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
tek.sync_lead(sync_lead);
|
tek.sync_lead(sync_lead);
|
||||||
|
|
@ -232,7 +237,7 @@ impl Tek {
|
||||||
let clip = MidiClip::new("Clip", true, 384usize, None, Some(ItemColor::random().into()));
|
let clip = MidiClip::new("Clip", true, 384usize, None, Some(ItemColor::random().into()));
|
||||||
let clip = Arc::new(RwLock::new(clip));
|
let clip = Arc::new(RwLock::new(clip));
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
atom: include_str!("./view_sequencer.edn").to_string(),
|
view: TokenIter(include_str!("./view_sequencer.edn")),
|
||||||
pool: Some((&clip).into()),
|
pool: Some((&clip).into()),
|
||||||
editor: Some((&clip).into()),
|
editor: Some((&clip).into()),
|
||||||
editing: false.into(),
|
editing: false.into(),
|
||||||
|
|
@ -248,7 +253,7 @@ impl Tek {
|
||||||
audio_froms: &[&[PortConnection];2], audio_tos: &[&[PortConnection];2],
|
audio_froms: &[&[PortConnection];2], audio_tos: &[&[PortConnection];2],
|
||||||
) -> Usually<Self> {
|
) -> Usually<Self> {
|
||||||
let app = Self {
|
let app = Self {
|
||||||
atom: include_str!("./view_groovebox.edn").to_string(),
|
view: TokenIter(include_str!("./view_groovebox.edn")),
|
||||||
sampler: Some(Sampler::new(jack, &"sampler", midi_froms, audio_froms, audio_tos)?),
|
sampler: Some(Sampler::new(jack, &"sampler", midi_froms, audio_froms, audio_tos)?),
|
||||||
..Self::new_sequencer(jack, bpm, sync_lead, sync_follow, midi_froms, midi_tos)?
|
..Self::new_sequencer(jack, bpm, sync_lead, sync_follow, midi_froms, midi_tos)?
|
||||||
};
|
};
|
||||||
|
|
@ -265,7 +270,7 @@ impl Tek {
|
||||||
scenes: usize, tracks: usize, track_width: usize,
|
scenes: usize, tracks: usize, track_width: usize,
|
||||||
) -> Usually<Self> {
|
) -> Usually<Self> {
|
||||||
let mut arranger = Self {
|
let mut arranger = Self {
|
||||||
atom: include_str!("./view_arranger.edn").to_string(),
|
view: TokenIter(include_str!("./view_arranger.edn")),
|
||||||
..Self::new_groovebox(
|
..Self::new_groovebox(
|
||||||
jack, bpm, sync_lead, sync_follow,
|
jack, bpm, sync_lead, sync_follow,
|
||||||
midi_froms, midi_tos, audio_froms, audio_tos,
|
midi_froms, midi_tos, audio_froms, audio_tos,
|
||||||
|
|
@ -600,19 +605,17 @@ handle!(TuiIn: |self: Tek, input|Ok({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Handle from root keymap
|
// Handle from root keymap
|
||||||
if let Some(command) = KeyMap::new(KEYS_APP)?.command::<_, TekCommand>(self, input) {
|
if let Some(command) = self.keys.command::<_, TekCommand, _>(self, input) {
|
||||||
if let Some(undo) = command.execute(self)? { self.history.push(undo); }
|
if let Some(undo) = command.execute(self)? { self.history.push(undo); }
|
||||||
return Ok(Some(true))
|
return Ok(Some(true))
|
||||||
}
|
}
|
||||||
// Handle from selection-dependent keymaps
|
// Handle from selection-dependent keymaps
|
||||||
if let Some(command) = KeyMap::new(match self.selected() {
|
if let Some(command) = match self.selected() {
|
||||||
Selection::Clip(t, s) => KEYS_CLIP,
|
Selection::Clip(_, _) => self.keys_clip,
|
||||||
Selection::Track(t) => KEYS_TRACK,
|
Selection::Track(_) => self.keys_track,
|
||||||
Selection::Scene(s) => KEYS_SCENE,
|
Selection::Scene(_) => self.keys_scene,
|
||||||
Selection::Mix => KEYS_MIX,
|
Selection::Mix => self.keys_mix,
|
||||||
})?.command::<_, TekCommand>(
|
}.command::<_, TekCommand, _>(self, input) {
|
||||||
self, input
|
|
||||||
) {
|
|
||||||
if let Some(undo) = command.execute(self)? { self.history.push(undo); }
|
if let Some(undo) = command.execute(self)? { self.history.push(undo); }
|
||||||
return Ok(Some(true))
|
return Ok(Some(true))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue