From 5db97825cc1475268729bbdab0ee7629e1697df4 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Thu, 24 Apr 2025 00:47:57 +0300 Subject: [PATCH] add defcom! macro --- crates/app/src/api.rs | 2 +- crates/app/src/keys.rs | 121 +++++++++++++++++++++++------------------ 2 files changed, 69 insertions(+), 54 deletions(-) diff --git a/crates/app/src/api.rs b/crates/app/src/api.rs index e552d01b..93475cc1 100644 --- a/crates/app/src/api.rs +++ b/crates/app/src/api.rs @@ -100,7 +100,7 @@ impose!([app: Tek] { Some(Self::Edit(None))) ("edit" [c: bool] Some(Self::Edit(c))) - ("color" [c: Color] + ("color" [] Some(Self::Color(ItemPalette::random()))) ("color" [c: Color] Some(Self::Color(c.map(ItemPalette::from).expect("no color")))) diff --git a/crates/app/src/keys.rs b/crates/app/src/keys.rs index 5edccb38..3a34135c 100644 --- a/crates/app/src/keys.rs +++ b/crates/app/src/keys.rs @@ -1,4 +1,5 @@ use crate::*; + handle!(TuiIn: |self: Tek, input|Ok({ // If editing, editor keys take priority if self.is_editing() { @@ -23,61 +24,75 @@ handle!(TuiIn: |self: Tek, input|Ok({ } None })); -#[derive(Clone, Debug)] pub enum TekCommand { - Clip(ClipCommand), - Clock(ClockCommand), - Color(ItemPalette), - Edit(Option), - Editor(MidiEditCommand), - Enqueue(Option>>), - History(isize), - Input(InputCommand), - Launch, - Output(OutputCommand), - Pool(PoolCommand), - Sampler(SamplerCommand), - Scene(SceneCommand), - Select(Selection), - StopAll, - Track(TrackCommand), - Zoom(Option), + +macro_rules! defcom { + ($($Command:ident { $( + $Variant:ident $(($($Param:ty),+))? + )* $(,)? })*) => { + $(#[derive(Clone, Debug)] pub enum $Command { + $($Variant $(($($Param),+))?),* + })* + } } -#[derive(Clone, Debug)] pub enum InputCommand { - Add -} -#[derive(Clone, Debug)] pub enum OutputCommand { - Add -} -#[derive(Clone, Debug)] pub enum TrackCommand { - Add, - Del(usize), - Stop(usize), - Swap(usize, usize), - SetSize(usize), - SetZoom(usize), - SetColor(usize, ItemPalette), - TogglePlay, - ToggleSolo, - ToggleRecord, - ToggleMonitor, -} -#[derive(Clone, Debug)] pub enum SceneCommand { - Add, - Del(usize), - Swap(usize, usize), - SetSize(usize), - SetZoom(usize), - SetColor(usize, ItemPalette), - Enqueue(usize), -} -#[derive(Clone, Debug)] pub enum ClipCommand { - Get(usize, usize), - Put(usize, usize, Option>>), - Enqueue(usize, usize), - Edit(Option>>), - SetLoop(usize, usize, bool), - SetColor(usize, usize, ItemPalette), + +defcom! { + TekCommand { + Clip(ClipCommand) + Clock(ClockCommand) + Color(ItemPalette) + Edit(Option) + Editor(MidiEditCommand) + Enqueue(Option>>) + History(isize) + Input(InputCommand) + Launch + Output(OutputCommand) + Pool(PoolCommand) + Sampler(SamplerCommand) + Scene(SceneCommand) + Select(Selection) + StopAll + Track(TrackCommand) + Zoom(Option) + } + InputCommand { + Add + } + OutputCommand { + Add + } + TrackCommand { + Add + Del(usize) + Stop(usize) + Swap(usize, usize) + SetSize(usize) + SetZoom(usize) + SetColor(usize, ItemPalette) + TogglePlay + ToggleSolo + ToggleRecord + ToggleMonitor + } + SceneCommand { + Add + Del(usize) + Swap(usize, usize) + SetSize(usize) + SetZoom(usize) + SetColor(usize, ItemPalette) + Enqueue(usize) + } + ClipCommand { + Get(usize, usize) + Put(usize, usize, Option>>) + Enqueue(usize, usize) + Edit(Option>>) + SetLoop(usize, usize, bool) + SetColor(usize, usize, ItemPalette) + } } + command!(|self: TekCommand, app: Tek|match self { Self::Zoom(_) => { println!("\n\rtodo: global zoom"); None }, Self::History(delta) => { println!("\n\rtodo: undo/redo"); None },