EdnViewData has to go?

This commit is contained in:
🪞👃🪞 2025-01-14 00:24:48 +01:00
parent ddcb967a2c
commit 585bba6666
6 changed files with 25 additions and 25 deletions

View file

@ -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 <S: AsRef<str>> (&$lt $self, edn: &$lt EdnItem<S>) -> 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 <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> 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 <S: AsRef<str>> (&'a self, _edn: &'a EdnItem<S>) -> Option<U> {
@ -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 <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> Option<U> {
(*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<T> {
fn get <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> Option<U> {
self.as_ref().map(|s|s.get(edn)).flatten()
}
fn get_or_fail <S: AsRef<str>> (&'a self, edn: &'a EdnItem<S>) -> U {
self.as_ref().map(|s|s.get_or_fail(edn)).expect("no provider")
}
}

View file

@ -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<C>: Command<C> {
fn from_edn <'a> (state: &C, head: &EdnItem<&str>, tail: &'a [EdnItem<String>]) -> Self;

View file

@ -1,4 +1,3 @@
use crate::*;
/// Event source
pub trait Input: Send + Sync + Sized {
/// Type of input event

View file

@ -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<dyn Render<TuiOut> + 'a>> for $State {
fn get <S: AsRef<str>> (&'a $self, edn: &'a EdnItem<S>) -> Option<Box<dyn Render<TuiOut> + '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> {

View file

@ -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<E: Output> {
fn size (&self) -> &Measure<E>;
}
#[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<E: Output> {
}
}
}
/// A widget that tracks its render width and height
#[derive(Default)]
pub struct Measure<E: Output> {
@ -23,14 +19,12 @@ pub struct Measure<E: Output> {
pub x: Arc<AtomicUsize>,
pub y: Arc<AtomicUsize>,
}
impl<E: Output> Content<E> for Measure<E> {
fn render (&self, to: &mut E) {
self.x.store(to.area().w().into(), Relaxed);
self.y.store(to.area().h().into(), Relaxed);
}
}
impl<E: Output> Clone for Measure<E> {
fn clone (&self) -> Self {
Self {
@ -40,7 +34,6 @@ impl<E: Output> Clone for Measure<E> {
}
}
}
impl<E: Output> std::fmt::Debug for Measure<E> {
fn fmt (&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
f.debug_struct("Measure")
@ -49,7 +42,6 @@ impl<E: Output> std::fmt::Debug for Measure<E> {
.finish()
}
}
impl<E: Output> Measure<E> {
pub fn w (&self) -> usize { self.x.load(Relaxed) }
pub fn h (&self) -> usize { self.y.load(Relaxed) }
@ -69,7 +61,6 @@ impl<E: Output> Measure<E> {
Bsp::b(Fill::xy(self), item)
}
}
///// A scrollable area.
//pub struct Scroll<E, F>(pub F, pub Direction, pub u64, PhantomData<E>)
//where

View file

@ -1 +1 @@
(fill/x :toolbar)
(fill/x (fixed/y 2 :toolbar))