From 585bba6666568c88452bb960ba1735a12810c32e Mon Sep 17 00:00:00 2001 From: unspeaker Date: Tue, 14 Jan 2025 00:24:48 +0100 Subject: [PATCH] EdnViewData has to go? --- edn/src/edn_provide.rs | 21 ++++++++++----------- input/src/edn_command.rs | 4 +--- input/src/input.rs | 1 - output/src/edn_view.rs | 13 +++++++++++++ output/src/measure.rs | 9 --------- tek/src/arranger-view.edn | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/edn/src/edn_provide.rs b/edn/src/edn_provide.rs index e47cb4ac..e80ad0b3 100644 --- a/edn/src/edn_provide.rs +++ b/edn/src/edn_provide.rs @@ -1,29 +1,21 @@ use crate::*; - /// Implement `EdnProvide` for a type and context #[macro_export] macro_rules! edn_provide { ($lt:lifetime: $type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { impl<$lt> EdnProvide<$lt, $type> for $State { fn get > (&$lt $self, edn: &$lt EdnItem) -> Option<$type> { - Some(match edn.to_ref() { - $(EdnItem::Sym($pat) => $expr),*, - _ => return None - }) + Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr),*, _ => return None }) } } }; ($type:ty:|$self:ident:$State:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { impl<'a> EdnProvide<'a, $type> for $State { fn get > (&'a $self, edn: &'a EdnItem) -> Option<$type> { - Some(match edn.to_ref() { - $(EdnItem::Sym($pat) => $expr),*, - _ => return None - }) + Some(match edn.to_ref() { $(EdnItem::Sym($pat) => $expr),*, _ => return None }) } } }; } - /// Map EDN tokens to parameters of a given type for a given context pub trait EdnProvide<'a, U>: Sized { fn get > (&'a self, _edn: &'a EdnItem) -> Option { @@ -33,7 +25,6 @@ pub trait EdnProvide<'a, U>: Sized { self.get(edn).expect("no value") } } - impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for &T { fn get > (&'a self, edn: &'a EdnItem) -> Option { (*self).get(edn) @@ -42,3 +33,11 @@ impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for &T { (*self).get_or_fail(edn) } } +impl<'a, T: EdnProvide<'a, U>, U> EdnProvide<'a, U> for Option { + fn get > (&'a self, edn: &'a EdnItem) -> Option { + self.as_ref().map(|s|s.get(edn)).flatten() + } + fn get_or_fail > (&'a self, edn: &'a EdnItem) -> U { + self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider") + } +} diff --git a/input/src/edn_command.rs b/input/src/edn_command.rs index f26e6dfd..0a139050 100644 --- a/input/src/edn_command.rs +++ b/input/src/edn_command.rs @@ -1,6 +1,5 @@ use crate::*; -use EdnItem::*; - +/** Implement `EdnCommand` for given `State` and `Command` */ #[macro_export] macro_rules! edn_command { ($Command:ty : |$state:ident:$State:ty| { $(( // identifier @@ -48,7 +47,6 @@ use EdnItem::*; let $arg: $type = EdnProvide::<$type>::get_or_fail($state, $arg); }; } - /// Turns an EDN symbol sequence into a command enum variant. pub trait EdnCommand: Command { fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem]) -> Self; diff --git a/input/src/input.rs b/input/src/input.rs index 615223ba..6c3c78b8 100644 --- a/input/src/input.rs +++ b/input/src/input.rs @@ -1,4 +1,3 @@ -use crate::*; /// Event source pub trait Input: Send + Sync + Sized { /// Type of input event diff --git a/output/src/edn_view.rs b/output/src/edn_view.rs index 1a0c6122..8aadc130 100644 --- a/output/src/edn_view.rs +++ b/output/src/edn_view.rs @@ -1,6 +1,19 @@ use crate::*; use std::marker::PhantomData; use EdnItem::*; +/// Implements `EdnProvide` for content components and expressions +#[macro_export] macro_rules! edn_provide_content { + (|$self:ident:$Stat:ty|{ $($pat:pat => $expr:expr),* $(,)? }) => { + impl<'a> EdnProvide<'a, Box + 'a>> for $State { + fn get > (&'a $self, edn: &'a EdnItem) -> Option + 'a>> { + Some(match edn.to_ref() { + $(EdnItem::Sym($pat) => $expr),*, + _ => return None + }) + } + } + } +} /// Renders from EDN source and context. #[derive(Default)] pub enum EdnView<'a, E: Output, T: EdnViewData<'a, E> + std::fmt::Debug> { diff --git a/output/src/measure.rs b/output/src/measure.rs index 7542cc75..c6bf83e0 100644 --- a/output/src/measure.rs +++ b/output/src/measure.rs @@ -1,13 +1,10 @@ use crate::*; use std::sync::{Arc, atomic::{AtomicUsize, Ordering::Relaxed}}; //use ratatui::prelude::{Style, Color}; - // TODO: 🡘 🡙 ←🡙→ indicator to expand window when too small - pub trait HasSize { fn size (&self) -> &Measure; } - #[macro_export] macro_rules! has_size { (<$E:ty>|$self:ident:$Struct:ident$(<$($L:lifetime),*$($T:ident$(:$U:path)?),*>)?|$cb:expr) => { impl $(<$($L),*$($T $(: $U)?),*>)? HasSize<$E> for $Struct $(<$($L),*$($T),*>)? { @@ -15,7 +12,6 @@ pub trait HasSize { } } } - /// A widget that tracks its render width and height #[derive(Default)] pub struct Measure { @@ -23,14 +19,12 @@ pub struct Measure { pub x: Arc, pub y: Arc, } - impl Content for Measure { fn render (&self, to: &mut E) { self.x.store(to.area().w().into(), Relaxed); self.y.store(to.area().h().into(), Relaxed); } } - impl Clone for Measure { fn clone (&self) -> Self { Self { @@ -40,7 +34,6 @@ impl Clone for Measure { } } } - impl std::fmt::Debug for Measure { fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> { f.debug_struct("Measure") @@ -49,7 +42,6 @@ impl std::fmt::Debug for Measure { .finish() } } - impl Measure { pub fn w (&self) -> usize { self.x.load(Relaxed) } pub fn h (&self) -> usize { self.y.load(Relaxed) } @@ -69,7 +61,6 @@ impl Measure { Bsp::b(Fill::xy(self), item) } } - ///// A scrollable area. //pub struct Scroll(pub F, pub Direction, pub u64, PhantomData) //where diff --git a/tek/src/arranger-view.edn b/tek/src/arranger-view.edn index 669e7852..39dbed09 100644 --- a/tek/src/arranger-view.edn +++ b/tek/src/arranger-view.edn @@ -1 +1 @@ -(fill/x :toolbar) +(fill/x (fixed/y 2 :toolbar))