wip: remove redundant type param

This commit is contained in:
🪞👃🪞 2024-09-05 00:03:54 +03:00
parent c033a5618b
commit df3dac183e
12 changed files with 113 additions and 125 deletions

View file

@ -1,40 +1,40 @@
use crate::*;
pub enum Collected<'a, T, U> {
Box(Box<dyn Render<T, U> + 'a>),
Ref(&'a (dyn Render<T, U> + 'a)),
pub enum Collected<'a, E: Engine> {
Box(Box<dyn Render<E> + 'a>),
Ref(&'a (dyn Render<E> + 'a)),
}
impl<'a, T, U> Render<T, U> for Collected<'a, T, U> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<'a, E: Engine> Render<E> for Collected<'a, E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
match self {
Self::Box(item) => (*item).render(to),
Self::Ref(item) => (*item).render(to),
}
}
}
pub struct Collection<'a, T, U>(
pub Vec<Collected<'a, T, U>>
pub struct Collection<'a, E: Engine>(
pub Vec<Collected<'a, E>>
);
impl<'a, T, U> Collection<'a, T, U> {
impl<'a, E: Engine> Collection<'a, E> {
pub fn new () -> Self {
Self(vec![])
}
}
pub trait Collect<'a, T, U> {
fn add_box (self, item: Box<dyn Render<T, U> + 'a>) -> Self;
fn add_ref (self, item: &'a dyn Render<T, U>) -> Self;
fn add <R: Render<T, U> + Sized + 'a> (self, item: R) -> Self
pub trait Collect<'a, E: Engine> {
fn add_box (self, item: Box<dyn Render<E> + 'a>) -> Self;
fn add_ref (self, item: &'a dyn Render<E>) -> Self;
fn add <R: Render<E> + Sized + 'a> (self, item: R) -> Self
where Self: Sized
{
self.add_box(Box::new(item))
}
}
impl<'a, T, U> Collect<'a, T, U> for Collection<'a, T, U> {
fn add_box (mut self, item: Box<dyn Render<T, U> + 'a>) -> Self {
impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> {
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
self.0.push(Collected::Box(item));
self
}
fn add_ref (mut self, item: &'a dyn Render<T, U>) -> Self {
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
self.0.push(Collected::Ref(item));
self
}

View file

@ -1,7 +1,7 @@
use crate::*;
/// A UI component.
pub trait Component<E: Engine>: Render<E, E::Rendered> + Handle<E, E::Handled> {}
pub trait Component<E: Engine>: Render<E> + Handle<E> {}
/// Everything that implements [Render] and [Handle] is a [Component].
impl<E: Engine, C: Render<E, E::Rendered> + Handle<E, E::Handled>> Component<E> for C {}
impl<E: Engine, C: Render<E> + Handle<E>> Component<E> for C {}

View file

@ -9,15 +9,9 @@ pub trait App<T: Engine> {
pub trait Engine {
type Handled;
type Rendered;
fn setup (&mut self) -> Usually<()> {
Ok(())
}
fn teardown (&mut self) -> Usually<()> {
Ok(())
}
fn handle (&self, _: &mut impl Handle<Self, Self::Handled>)
-> Usually<()> where Self: Sized;
fn render (&mut self, _: &impl Render<Self, Self::Rendered>)
-> Usually<()> where Self: Sized;
fn exited (&self) -> bool;
fn setup (&mut self) -> Usually<()> { Ok(()) }
fn teardown (&mut self) -> Usually<()> { Ok(()) }
fn handle (&self, _: &mut impl Handle<Self>) -> Usually<()> where Self: Sized;
fn render (&mut self, _: &impl Render<Self>) -> Usually<()> where Self: Sized;
fn exited (&self) -> bool;
}

View file

@ -1,17 +1,17 @@
use crate::*;
/// A component that may contain [Focusable] components.
pub trait Focus <const N: usize, T, U>: Render<T, U> + Handle<T, U> {
pub trait Focus <const N: usize, E: Engine>: Render<E> + Handle<E> {
fn focus (&self) -> usize;
fn focus_mut (&mut self) -> &mut usize;
fn focusable (&self) -> [&dyn Focusable<T, U>;N];
fn focusable_mut (&mut self) -> [&mut dyn Focusable<T, U>;N];
fn focusable (&self) -> [&dyn Focusable<E>;N];
fn focusable_mut (&mut self) -> [&mut dyn Focusable<E>;N];
fn focused (&self) -> &dyn Focusable<T, U> {
fn focused (&self) -> &dyn Focusable<E> {
let focus = self.focus();
self.focusable()[focus]
}
fn focused_mut (&mut self) -> &mut dyn Focusable<T, U> {
fn focused_mut (&mut self) -> &mut dyn Focusable<E> {
let focus = self.focus();
self.focusable_mut()[focus]
}
@ -33,13 +33,13 @@ pub trait Focus <const N: usize, T, U>: Render<T, U> + Handle<T, U> {
}
/// A component that may be focused.
pub trait Focusable<T, U>: Render<T, U> + Handle<T, U> {
pub trait Focusable<E: Engine>: Render<E> + Handle<E> {
fn is_focused (&self) -> bool;
fn set_focused (&mut self, focused: bool);
}
impl<F: Focusable<T, U>, T, U> Focusable<T, U> for Option<F>
where Option<F>: Render<T, U>
impl<F: Focusable<E>, E: Engine> Focusable<E> for Option<F>
where Option<F>: Render<E>
{
fn is_focused (&self) -> bool {
match self {
@ -59,21 +59,21 @@ impl<F: Focusable<T, U>, T, U> Focusable<T, U> for Option<F>
($struct:ident ($focus:ident) : $count:expr => [
$($focusable:ident),*
]) => {
impl Focus<$count, T, U> for $struct {
impl Focus<$count, E> for $struct {
fn focus (&self) -> usize {
self.$focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.$focus
}
fn focusable (&self) -> [&dyn Focusable<T, U>;$count] {
fn focusable (&self) -> [&dyn Focusable<E>;$count] {
[
$(&self.$focusable as &dyn Focusable<T, U>,)*
$(&self.$focusable as &dyn Focusable<E>,)*
]
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<T, U>;$count] {
fn focusable_mut (&mut self) -> [&mut dyn Focusable<E>;$count] {
[
$(&mut self.$focusable as &mut dyn Focusable<T, U>,)*
$(&mut self.$focusable as &mut dyn Focusable<E>,)*
]
}
}

View file

@ -1,18 +1,18 @@
use crate::*;
/// Handle input
pub trait Handle<T, U>: Send + Sync {
fn handle (&mut self, context: &T) -> Perhaps<U>;
pub trait Handle<E: Engine>: Send + Sync {
fn handle (&mut self, context: &E) -> Perhaps<E::Handled>;
}
impl<H, T, U> Handle<T, U> for &mut H where H: Handle<T, U> {
fn handle (&mut self, context: &T) -> Perhaps<U> {
impl<H, E: Engine> Handle<E> for &mut H where H: Handle<E> {
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
(*self).handle(context)
}
}
impl<H, T, U> Handle<T, U> for Option<H> where H: Handle<T, U> {
fn handle (&mut self, context: &T) -> Perhaps<U> {
impl<H, E: Engine> Handle<E> for Option<H> where H: Handle<E> {
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
if let Some(ref mut handle) = self {
handle.handle(context)
} else {
@ -21,26 +21,26 @@ impl<H, T, U> Handle<T, U> for Option<H> where H: Handle<T, U> {
}
}
impl<H, T, U> Handle<T, U> for Mutex<H> where H: Handle<T, U> {
fn handle (&mut self, context: &T) -> Perhaps<U> {
impl<H, E: Engine> Handle<E> for Mutex<H> where H: Handle<E> {
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
self.lock().unwrap().handle(context)
}
}
impl<H, T, U> Handle<T, U> for Arc<Mutex<H>> where H: Handle<T, U> {
fn handle (&mut self, context: &T) -> Perhaps<U> {
impl<H, E: Engine> Handle<E> for Arc<Mutex<H>> where H: Handle<E> {
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
self.lock().unwrap().handle(context)
}
}
impl<H, T, U> Handle<T, U> for RwLock<H> where H: Handle<T, U> {
fn handle (&mut self, context: &T) -> Perhaps<U> {
impl<H, E: Engine> Handle<E> for RwLock<H> where H: Handle<E> {
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
self.write().unwrap().handle(context)
}
}
impl<H, T, U> Handle<T, U> for Arc<RwLock<H>> where H: Handle<T, U> {
fn handle (&mut self, context: &T) -> Perhaps<U> {
impl<H, E: Engine> Handle<E> for Arc<RwLock<H>> where H: Handle<E> {
fn handle (&mut self, context: &E) -> Perhaps<E::Handled> {
self.write().unwrap().handle(context)
}
}

View file

@ -11,7 +11,7 @@ pub trait Device<E: Engine>: Component<E> + Process {
/// All things that implement the required traits can be treated as `Device`.
impl<D, E: Engine> Device<E> for D where D: Component<E> + Process {}
impl<E: Engine> Render<E, E::Rendered> for Box<dyn Device<E>> {
impl<E: Engine> Render<E> for Box<dyn Device<E>> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
(**self).render(to)
}
@ -99,7 +99,7 @@ pub trait Process {
/// Just run thing with JACK. Returns the activated client.
pub fn jack_run <T, E: Engine> (name: &str, app: &Arc<RwLock<T>>) -> Usually<DynamicAsyncClient>
where T: Handle<E, E::Handled> + Process + Send + Sync + 'static
where T: Handle<E> + Process + Send + Sync + 'static
{
let options = ClientOptions::NO_START_SERVER;
let (client, _status) = Client::new(name, options)?;

View file

@ -15,12 +15,12 @@ impl<E: Engine> std::fmt::Debug for JackDevice<E> {
f.debug_struct("JackDevice").field("ports", &self.ports).finish()
}
}
impl<E: Engine> Render<E, E::Rendered> for JackDevice<E> {
impl<E: Engine> Render<E> for JackDevice<E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
self.state.read().unwrap().render(to)
}
}
impl<E: Engine> Handle<E, E::Handled> for JackDevice<E> {
impl<E: Engine> Handle<E> for JackDevice<E> {
fn handle (&mut self, from: &E) -> Perhaps<E::Handled> {
self.state.write().unwrap().handle(from)
}

View file

@ -1,13 +1,13 @@
use crate::*;
/// Render to output.
pub trait Render<T, U>: Send + Sync {
fn render (&self, to: &mut T) -> Perhaps<U>;
pub trait Render<E: Engine>: Send + Sync {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered>;
}
/// Options can be rendered optionally.
impl<R, T, U> Render<T, U> for Option<R> where R: Render<T, U> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<R, E: Engine> Render<E> for Option<R> where R: Render<E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
match self {
Some(component) => component.render(to),
None => Ok(None)
@ -16,43 +16,43 @@ impl<R, T, U> Render<T, U> for Option<R> where R: Render<T, U> {
}
/// Boxed references can be rendered.
impl<'a, T, U> Render<T, U> for Box<dyn Render<T, U> + 'a> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<'a, E: Engine> Render<E> for Box<dyn Render<E> + 'a> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
(**self).render(to)
}
}
/// Immutable references can be rendered.
impl<R, T, U> Render<T, U> for &R where R: Render<T, U> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<R, E: Engine> Render<E> for &R where R: Render<E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
(*self).render(to)
}
}
/// Mutable references can be rendered.
impl<R, T, U> Render<T, U> for &mut R where R: Render<T, U> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<R, E: Engine> Render<E> for &mut R where R: Render<E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
(**self).render(to)
}
}
/// Counted references can be rendered.
impl<R, T, U> Render<T, U> for Arc<R> where R: Render<T, U> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<R, E: Engine> Render<E> for Arc<R> where R: Render<E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
self.as_ref().render(to)
}
}
/// References behind a [Mutex] can be rendered.
impl<R, T, U> Render<T, U> for Mutex<R> where R: Render<T, U> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<R, E: Engine> Render<E> for Mutex<R> where R: Render<E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
self.lock().unwrap().render(to)
}
}
/// References behind a [RwLock] can be rendered.
impl<R, T, U> Render<T, U> for RwLock<R> where R: Render<T, U> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<R, E: Engine> Render<E> for RwLock<R> where R: Render<E> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
self.read().unwrap().render(to)
}
}
@ -62,8 +62,8 @@ impl<R, T, U> Render<T, U> for RwLock<R> where R: Render<T, U> {
/// Rendering unboxed closures should also be possible;
/// but in practice implementing the trait for an unboxed
/// `Fn` closure causes an impl conflict.
impl<'a, T, U> Render<T, U> for Box<dyn Fn(&mut T) -> Perhaps<U> + Send + Sync + 'a> {
fn render (&self, to: &mut T) -> Perhaps<U> {
impl<'a, E: Engine> Render<E> for Box<dyn Fn(&mut E) -> Perhaps<E::Rendered> + Send + Sync + 'a> {
fn render (&self, to: &mut E) -> Perhaps<E::Rendered> {
(*self)(to)
}
}

View file

@ -1,26 +1,26 @@
use crate::*;
pub struct Layered<'a, T, U>(Collection<'a, T, U>);
pub struct Layered<'a, E: Engine>(Collection<'a, E>);
impl<'a, T, U> Layered<'a, T, U> {
impl<'a, E: Engine> Layered<'a, E> {
pub fn new () -> Self {
Self(Collection::new())
}
}
impl<'a, T, U> Collect<'a, T, U> for Layered<'a, T, U> {
fn add_box (mut self, item: Box<dyn Render<T, U> + 'a>) -> Self {
impl<'a, E: Engine> Collect<'a, E> for Layered<'a, E> {
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
self.0 = self.0.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Render<T, U>) -> Self {
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
self.0 = self.0.add_ref(item);
self
}
}
impl<'a> Render<TuiContext, Rect> for Layered<'a, TuiContext, Rect> {
fn render (&self, to: &mut TuiContext) -> Perhaps<Rect> {
impl<'a> Render<Tui> for Layered<'a, Tui> {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
let area = to.area();
for layer in self.0.0.iter() {
layer.render(to)?;

View file

@ -17,13 +17,13 @@ impl Direction {
}
}
pub struct Split<'a, T, U> {
items: Collection<'a, T, U>,
pub struct Split<'a, E: Engine> {
items: Collection<'a, E>,
direction: Direction,
focus: Option<usize>
}
impl<'a, T, U> Split<'a, T, U> {
impl<'a, E: Engine> Split<'a, E> {
pub fn new (direction: Direction) -> Self {
Self {
items: Collection::new(),
@ -43,25 +43,25 @@ impl<'a, T, U> Split<'a, T, U> {
}
}
impl<'a, T, U> Collect<'a, T, U> for Split<'a, T, U> {
fn add_box (mut self, item: Box<dyn Render<T, U> + 'a>) -> Self {
impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> {
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
self.items = self.items.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Render<T, U>) -> Self {
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
self.items = self.items.add_ref(item);
self
}
}
impl<'a> Render<TuiContext, Rect> for Split<'a, TuiContext, Rect> {
fn render (&self, to: &mut TuiContext) -> Perhaps<Rect> {
impl<'a> Render<Tui> for Split<'a, Tui> {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
Ok(None)//Rect::default())//Some(self.render_areas(to)?.0))
}
}
impl<'a> Split<'a, TuiContext, Rect> {
pub fn render_areas (&self, to: &mut TuiContext) -> Usually<(Rect, Vec<Rect>)> {
impl<'a> Split<'a, Tui> {
pub fn render_areas (&self, to: &mut Tui) -> Usually<(Rect, Vec<Rect>)> {
Ok((Rect::default(), vec![]))
//let Rect { mut x, mut y, mut width, mut height } = to.area;
//let TuiOutput { buffer, area } = to;

View file

@ -12,7 +12,7 @@ use crossterm::terminal::{
enable_raw_mode, disable_raw_mode
};
pub struct TuiContext {
pub struct Tui {
exited: Arc<AtomicBool>,
buffer: usize,
buffers: [Buffer;2],
@ -21,7 +21,7 @@ pub struct TuiContext {
sleep: Duration,
poll: Duration,
}
impl Engine for TuiContext {
impl Engine for Tui {
type Handled = bool;
type Rendered = Rect;
fn exited (&self) -> bool {
@ -34,7 +34,7 @@ impl Engine for TuiContext {
fn teardown (&mut self) -> Usually<()> {
terminal_teardown()
}
fn handle (&self, state: &mut impl Handle<Self, bool>) -> Usually<()> {
fn handle (&self, state: &mut impl Handle<Self>) -> Usually<()> {
if ::crossterm::event::poll(self.poll).is_ok() {
let event = ::crossterm::event::read().unwrap();
if let Event::Key(KeyEvent {
@ -47,14 +47,14 @@ impl Engine for TuiContext {
}
Ok(())
}
fn render (&mut self, state: &impl Render<Self, Rect>) -> Usually<()> {
fn render (&mut self, state: &impl Render<Self>) -> Usually<()> {
state.render(self).expect("render failed");
Ok(())
}
}
impl TuiContext {
impl Tui {
/// Run the main loop.
pub fn run <R: Component<TuiContext> + Sized + 'static> (
pub fn run <R: Component<Tui> + Sized + 'static> (
state: Arc<RwLock<R>>
) -> Usually<Arc<RwLock<R>>> {
let backend = CrosstermBackend::new(stdout());
@ -149,7 +149,7 @@ pub trait TuiTarget {
fn buffer (&mut self) -> &mut Buffer;
}
impl TuiTarget for TuiContext {
impl TuiTarget for Tui {
fn area (&self) -> Rect {
self.area
}
@ -208,8 +208,8 @@ impl<T: AsRef<str>> Blit for T {
}
/// Rendering unit struct to Ratatui returns zero-sized [Rect] at render coordinates.
impl Render<TuiContext, Rect> for () {
fn render (&self, to: &mut TuiContext) -> Perhaps<Rect> {
impl Render<Tui> for () {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
Ok(Some(Rect { x: to.area.x, y: to.area.y, width: 0, height: 0 }))
}
}
@ -233,8 +233,8 @@ pub fn half_block (lower: bool, upper: bool) -> Option<char> {
pub struct FillBg(pub Color);
impl Render<TuiContext, Rect> for FillBg {
fn render (&self, to: &mut TuiContext) -> Perhaps<Rect> {
impl Render<Tui> for FillBg {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
to.fill_bg(to.area, self.0);
Ok(Some(to.area))
}
@ -251,7 +251,7 @@ pub trait BorderStyle {
const W: &'static str = "";
#[inline]
fn draw <'a> (&self, to: &mut TuiContext) -> Perhaps<Rect> {
fn draw <'a> (&self, to: &mut Tui) -> Perhaps<Rect> {
self.draw_horizontal(to, None)?;
self.draw_vertical(to, None)?;
self.draw_corners(to, None)?;
@ -259,7 +259,7 @@ pub trait BorderStyle {
}
#[inline]
fn draw_horizontal (&self, to: &mut TuiContext, style: Option<Style>) -> Usually<Rect> {
fn draw_horizontal (&self, to: &mut Tui, style: Option<Style>) -> Usually<Rect> {
let area = to.area();
let buf = to.buffer();
let style = style.or_else(||self.style_horizontal());
@ -279,7 +279,7 @@ pub trait BorderStyle {
}
#[inline]
fn draw_vertical (&self, to: &mut TuiContext, style: Option<Style>) -> Usually<Rect> {
fn draw_vertical (&self, to: &mut Tui, style: Option<Style>) -> Usually<Rect> {
let area = to.area();
let buf = to.buffer();
let style = style.or_else(||self.style_vertical());
@ -291,7 +291,7 @@ pub trait BorderStyle {
}
#[inline]
fn draw_corners (&self, to: &mut TuiContext, style: Option<Style>) -> Usually<Rect> {
fn draw_corners (&self, to: &mut Tui, style: Option<Style>) -> Usually<Rect> {
let area = to.area();
let buf = to.buffer();
let style = style.or_else(||self.style_corners());

View file

@ -1,22 +1,16 @@
use crate::*;
handle!(TransportToolbar |self, e| {
match e {
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::Right, .. })) => {
self.focus_next();
Ok(true)
},
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::Left, .. })) => {
self.focus_prev();
Ok(true)
},
AppEvent::Input(Event::Key(KeyEvent { code: KeyCode::Char(' '), .. })) => {
self.toggle_play();
Ok(true)
},
_ => self.focused_mut().handle(e)
impl Handle<Tui, bool> for TransportToolbar {
fn handle (&mut self, from: &Tui) -> Perhaps<bool> {
Ok(
from.key(KeyCode::Right).does(||self.focus_next())?
||
from.key(KeyCode::Left).does(||self.focus_prev())?
||
from.key(KeyCode::Char(' ')).does(||self.toggle_play())?
)
}
});
}
handle!(TransportPlayPauseButton |self, _e| Ok(false));