diff --git a/crates/tek_core/src/collect.rs b/crates/tek_core/src/collect.rs index e89e5b6f..df77d585 100644 --- a/crates/tek_core/src/collect.rs +++ b/crates/tek_core/src/collect.rs @@ -1,40 +1,40 @@ use crate::*; -pub enum Collected<'a, T, U> { - Box(Box + 'a>), - Ref(&'a (dyn Render + 'a)), +pub enum Collected<'a, E: Engine> { + Box(Box + 'a>), + Ref(&'a (dyn Render + 'a)), } -impl<'a, T, U> Render for Collected<'a, T, U> { - fn render (&self, to: &mut T) -> Perhaps { +impl<'a, E: Engine> Render for Collected<'a, E> { + fn render (&self, to: &mut E) -> Perhaps { match self { Self::Box(item) => (*item).render(to), Self::Ref(item) => (*item).render(to), } } } -pub struct Collection<'a, T, U>( - pub Vec> +pub struct Collection<'a, E: Engine>( + pub Vec> ); -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 + 'a>) -> Self; - fn add_ref (self, item: &'a dyn Render) -> Self; - fn add + Sized + 'a> (self, item: R) -> Self +pub trait Collect<'a, E: Engine> { + fn add_box (self, item: Box + 'a>) -> Self; + fn add_ref (self, item: &'a dyn Render) -> Self; + fn add + 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 + 'a>) -> Self { +impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> { + fn add_box (mut self, item: Box + 'a>) -> Self { self.0.push(Collected::Box(item)); self } - fn add_ref (mut self, item: &'a dyn Render) -> Self { + fn add_ref (mut self, item: &'a dyn Render) -> Self { self.0.push(Collected::Ref(item)); self } diff --git a/crates/tek_core/src/component.rs b/crates/tek_core/src/component.rs index 0a52325b..952a4d90 100644 --- a/crates/tek_core/src/component.rs +++ b/crates/tek_core/src/component.rs @@ -1,7 +1,7 @@ use crate::*; /// A UI component. -pub trait Component: Render + Handle {} +pub trait Component: Render + Handle {} /// Everything that implements [Render] and [Handle] is a [Component]. -impl + Handle> Component for C {} +impl + Handle> Component for C {} diff --git a/crates/tek_core/src/engine.rs b/crates/tek_core/src/engine.rs index d8622819..191bc643 100644 --- a/crates/tek_core/src/engine.rs +++ b/crates/tek_core/src/engine.rs @@ -9,15 +9,9 @@ pub trait App { 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) - -> Usually<()> where Self: Sized; - fn render (&mut self, _: &impl Render) - -> 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) -> Usually<()> where Self: Sized; + fn render (&mut self, _: &impl Render) -> Usually<()> where Self: Sized; + fn exited (&self) -> bool; } diff --git a/crates/tek_core/src/focus.rs b/crates/tek_core/src/focus.rs index 11a139c1..b9d42ada 100644 --- a/crates/tek_core/src/focus.rs +++ b/crates/tek_core/src/focus.rs @@ -1,17 +1,17 @@ use crate::*; /// A component that may contain [Focusable] components. -pub trait Focus : Render + Handle { +pub trait Focus : Render + Handle { fn focus (&self) -> usize; fn focus_mut (&mut self) -> &mut usize; - fn focusable (&self) -> [&dyn Focusable;N]; - fn focusable_mut (&mut self) -> [&mut dyn Focusable;N]; + fn focusable (&self) -> [&dyn Focusable;N]; + fn focusable_mut (&mut self) -> [&mut dyn Focusable;N]; - fn focused (&self) -> &dyn Focusable { + fn focused (&self) -> &dyn Focusable { let focus = self.focus(); self.focusable()[focus] } - fn focused_mut (&mut self) -> &mut dyn Focusable { + fn focused_mut (&mut self) -> &mut dyn Focusable { let focus = self.focus(); self.focusable_mut()[focus] } @@ -33,13 +33,13 @@ pub trait Focus : Render + Handle { } /// A component that may be focused. -pub trait Focusable: Render + Handle { +pub trait Focusable: Render + Handle { fn is_focused (&self) -> bool; fn set_focused (&mut self, focused: bool); } -impl, T, U> Focusable for Option - where Option: Render +impl, E: Engine> Focusable for Option + where Option: Render { fn is_focused (&self) -> bool { match self { @@ -59,21 +59,21 @@ impl, T, U> Focusable for Option ($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;$count] { + fn focusable (&self) -> [&dyn Focusable;$count] { [ - $(&self.$focusable as &dyn Focusable,)* + $(&self.$focusable as &dyn Focusable,)* ] } - fn focusable_mut (&mut self) -> [&mut dyn Focusable;$count] { + fn focusable_mut (&mut self) -> [&mut dyn Focusable;$count] { [ - $(&mut self.$focusable as &mut dyn Focusable,)* + $(&mut self.$focusable as &mut dyn Focusable,)* ] } } diff --git a/crates/tek_core/src/handle.rs b/crates/tek_core/src/handle.rs index bcbf14ca..24767959 100644 --- a/crates/tek_core/src/handle.rs +++ b/crates/tek_core/src/handle.rs @@ -1,18 +1,18 @@ use crate::*; /// Handle input -pub trait Handle: Send + Sync { - fn handle (&mut self, context: &T) -> Perhaps; +pub trait Handle: Send + Sync { + fn handle (&mut self, context: &E) -> Perhaps; } -impl Handle for &mut H where H: Handle { - fn handle (&mut self, context: &T) -> Perhaps { +impl Handle for &mut H where H: Handle { + fn handle (&mut self, context: &E) -> Perhaps { (*self).handle(context) } } -impl Handle for Option where H: Handle { - fn handle (&mut self, context: &T) -> Perhaps { +impl Handle for Option where H: Handle { + fn handle (&mut self, context: &E) -> Perhaps { if let Some(ref mut handle) = self { handle.handle(context) } else { @@ -21,26 +21,26 @@ impl Handle for Option where H: Handle { } } -impl Handle for Mutex where H: Handle { - fn handle (&mut self, context: &T) -> Perhaps { +impl Handle for Mutex where H: Handle { + fn handle (&mut self, context: &E) -> Perhaps { self.lock().unwrap().handle(context) } } -impl Handle for Arc> where H: Handle { - fn handle (&mut self, context: &T) -> Perhaps { +impl Handle for Arc> where H: Handle { + fn handle (&mut self, context: &E) -> Perhaps { self.lock().unwrap().handle(context) } } -impl Handle for RwLock where H: Handle { - fn handle (&mut self, context: &T) -> Perhaps { +impl Handle for RwLock where H: Handle { + fn handle (&mut self, context: &E) -> Perhaps { self.write().unwrap().handle(context) } } -impl Handle for Arc> where H: Handle { - fn handle (&mut self, context: &T) -> Perhaps { +impl Handle for Arc> where H: Handle { + fn handle (&mut self, context: &E) -> Perhaps { self.write().unwrap().handle(context) } } diff --git a/crates/tek_core/src/jack_core.rs b/crates/tek_core/src/jack_core.rs index 1f7823ff..14ea9568 100644 --- a/crates/tek_core/src/jack_core.rs +++ b/crates/tek_core/src/jack_core.rs @@ -11,7 +11,7 @@ pub trait Device: Component + Process { /// All things that implement the required traits can be treated as `Device`. impl Device for D where D: Component + Process {} -impl Render for Box> { +impl Render for Box> { fn render (&self, to: &mut E) -> Perhaps { (**self).render(to) } @@ -99,7 +99,7 @@ pub trait Process { /// Just run thing with JACK. Returns the activated client. pub fn jack_run (name: &str, app: &Arc>) -> Usually - where T: Handle + Process + Send + Sync + 'static + where T: Handle + Process + Send + Sync + 'static { let options = ClientOptions::NO_START_SERVER; let (client, _status) = Client::new(name, options)?; diff --git a/crates/tek_core/src/jack_device.rs b/crates/tek_core/src/jack_device.rs index d4984f30..f2772a7e 100644 --- a/crates/tek_core/src/jack_device.rs +++ b/crates/tek_core/src/jack_device.rs @@ -15,12 +15,12 @@ impl std::fmt::Debug for JackDevice { f.debug_struct("JackDevice").field("ports", &self.ports).finish() } } -impl Render for JackDevice { +impl Render for JackDevice { fn render (&self, to: &mut E) -> Perhaps { self.state.read().unwrap().render(to) } } -impl Handle for JackDevice { +impl Handle for JackDevice { fn handle (&mut self, from: &E) -> Perhaps { self.state.write().unwrap().handle(from) } diff --git a/crates/tek_core/src/render.rs b/crates/tek_core/src/render.rs index 0a61678a..e0c54f6a 100644 --- a/crates/tek_core/src/render.rs +++ b/crates/tek_core/src/render.rs @@ -1,13 +1,13 @@ use crate::*; /// Render to output. -pub trait Render: Send + Sync { - fn render (&self, to: &mut T) -> Perhaps; +pub trait Render: Send + Sync { + fn render (&self, to: &mut E) -> Perhaps; } /// Options can be rendered optionally. -impl Render for Option where R: Render { - fn render (&self, to: &mut T) -> Perhaps { +impl Render for Option where R: Render { + fn render (&self, to: &mut E) -> Perhaps { match self { Some(component) => component.render(to), None => Ok(None) @@ -16,43 +16,43 @@ impl Render for Option where R: Render { } /// Boxed references can be rendered. -impl<'a, T, U> Render for Box + 'a> { - fn render (&self, to: &mut T) -> Perhaps { +impl<'a, E: Engine> Render for Box + 'a> { + fn render (&self, to: &mut E) -> Perhaps { (**self).render(to) } } /// Immutable references can be rendered. -impl Render for &R where R: Render { - fn render (&self, to: &mut T) -> Perhaps { +impl Render for &R where R: Render { + fn render (&self, to: &mut E) -> Perhaps { (*self).render(to) } } /// Mutable references can be rendered. -impl Render for &mut R where R: Render { - fn render (&self, to: &mut T) -> Perhaps { +impl Render for &mut R where R: Render { + fn render (&self, to: &mut E) -> Perhaps { (**self).render(to) } } /// Counted references can be rendered. -impl Render for Arc where R: Render { - fn render (&self, to: &mut T) -> Perhaps { +impl Render for Arc where R: Render { + fn render (&self, to: &mut E) -> Perhaps { self.as_ref().render(to) } } /// References behind a [Mutex] can be rendered. -impl Render for Mutex where R: Render { - fn render (&self, to: &mut T) -> Perhaps { +impl Render for Mutex where R: Render { + fn render (&self, to: &mut E) -> Perhaps { self.lock().unwrap().render(to) } } /// References behind a [RwLock] can be rendered. -impl Render for RwLock where R: Render { - fn render (&self, to: &mut T) -> Perhaps { +impl Render for RwLock where R: Render { + fn render (&self, to: &mut E) -> Perhaps { self.read().unwrap().render(to) } } @@ -62,8 +62,8 @@ impl Render for RwLock where R: Render { /// 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 for Box Perhaps + Send + Sync + 'a> { - fn render (&self, to: &mut T) -> Perhaps { +impl<'a, E: Engine> Render for Box Perhaps + Send + Sync + 'a> { + fn render (&self, to: &mut E) -> Perhaps { (*self)(to) } } diff --git a/crates/tek_core/src/render_layered.rs b/crates/tek_core/src/render_layered.rs index c3ef8236..92fe0546 100644 --- a/crates/tek_core/src/render_layered.rs +++ b/crates/tek_core/src/render_layered.rs @@ -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 + 'a>) -> Self { +impl<'a, E: Engine> Collect<'a, E> for Layered<'a, E> { + fn add_box (mut self, item: Box + 'a>) -> Self { self.0 = self.0.add_box(item); self } - fn add_ref (mut self, item: &'a dyn Render) -> Self { + fn add_ref (mut self, item: &'a dyn Render) -> Self { self.0 = self.0.add_ref(item); self } } -impl<'a> Render for Layered<'a, TuiContext, Rect> { - fn render (&self, to: &mut TuiContext) -> Perhaps { +impl<'a> Render for Layered<'a, Tui> { + fn render (&self, to: &mut Tui) -> Perhaps { let area = to.area(); for layer in self.0.0.iter() { layer.render(to)?; diff --git a/crates/tek_core/src/render_split.rs b/crates/tek_core/src/render_split.rs index f857929a..b7e07821 100644 --- a/crates/tek_core/src/render_split.rs +++ b/crates/tek_core/src/render_split.rs @@ -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 } -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 + 'a>) -> Self { +impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> { + fn add_box (mut self, item: Box + 'a>) -> Self { self.items = self.items.add_box(item); self } - fn add_ref (mut self, item: &'a dyn Render) -> Self { + fn add_ref (mut self, item: &'a dyn Render) -> Self { self.items = self.items.add_ref(item); self } } -impl<'a> Render for Split<'a, TuiContext, Rect> { - fn render (&self, to: &mut TuiContext) -> Perhaps { +impl<'a> Render for Split<'a, Tui> { + fn render (&self, to: &mut Tui) -> Perhaps { 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)> { +impl<'a> Split<'a, Tui> { + pub fn render_areas (&self, to: &mut Tui) -> Usually<(Rect, Vec)> { Ok((Rect::default(), vec![])) //let Rect { mut x, mut y, mut width, mut height } = to.area; //let TuiOutput { buffer, area } = to; diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index 4681164e..a5fcc4fb 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -12,7 +12,7 @@ use crossterm::terminal::{ enable_raw_mode, disable_raw_mode }; -pub struct TuiContext { +pub struct Tui { exited: Arc, 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) -> Usually<()> { + fn handle (&self, state: &mut impl Handle) -> 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) -> Usually<()> { + fn render (&mut self, state: &impl Render) -> Usually<()> { state.render(self).expect("render failed"); Ok(()) } } -impl TuiContext { +impl Tui { /// Run the main loop. - pub fn run + Sized + 'static> ( + pub fn run + Sized + 'static> ( state: Arc> ) -> Usually>> { 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> Blit for T { } /// Rendering unit struct to Ratatui returns zero-sized [Rect] at render coordinates. -impl Render for () { - fn render (&self, to: &mut TuiContext) -> Perhaps { +impl Render for () { + fn render (&self, to: &mut Tui) -> Perhaps { 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 { pub struct FillBg(pub Color); -impl Render for FillBg { - fn render (&self, to: &mut TuiContext) -> Perhaps { +impl Render for FillBg { + fn render (&self, to: &mut Tui) -> Perhaps { 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 { + fn draw <'a> (&self, to: &mut Tui) -> Perhaps { 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