wip: and sweeps right through the codebase

This commit is contained in:
🪞👃🪞 2024-12-31 02:03:16 +01:00
parent d37bd3e0c5
commit c9b09b7dea
16 changed files with 370 additions and 625 deletions

View file

@ -1,27 +0,0 @@
use crate::*;
/// A UI component that can render itself as a [Render], and [Handle] input.
pub trait Component<E: Engine>: Render<E> + Handle<E> {}
/// Everything that implements [Render] and [Handle] is a [Component].
impl<E: Engine, C: Render<E> + Handle<E>> Component<E> for C {}
/// A component that can exit.
pub trait Exit: Send {
//fn exited (&self) -> bool;
//fn exit (&mut self);
//fn boxed (self) -> Box<dyn Exit> where Self: Sized + 'static {
//Box::new(self)
//}
}
/// Marker trait for [Component]s that can [Exit].
pub trait ExitableComponent<E>: Exit + Component<E> where E: Engine {
///// Perform type erasure for collecting heterogeneous components.
//fn boxed (self) -> Box<dyn ExitableComponent<E>> where Self: Sized + 'static {
//Box::new(self)
//}
}
/// All [Components]s that implement [Exit] implement [ExitableComponent].
impl<E: Engine, C: Component<E> + Exit> ExitableComponent<E> for C {}

View file

@ -63,6 +63,9 @@ pub trait Size<N: Coordinate> {
Ok(self)
}
}
#[inline] fn zero () -> [N;2] {
[N::zero(), N::zero()]
}
}
impl<N: Coordinate> Size<N> for (N, N) {
@ -129,6 +132,10 @@ pub trait Area<N: Coordinate>: Copy {
#[inline] fn shrink_y (&self, y: N) -> [N;4] {
[self.x(), self.y(), self.w(), self.h().minus(y)]
}
fn zero () -> [N;4] {
[N::zero(), N::zero(), N::zero(), N::zero()]
}
}
impl<N: Coordinate> Area<N> for (N, N, N, N) {

View file

@ -46,11 +46,11 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.0
}
fn render_in (&mut self, _: [u16;4], _: &impl Render<TestEngine>) -> Usually<()> {
Ok(())
fn place (&mut self, _: [u16;4], _: &impl Layout<TestEngine>) {
()
}
}
impl Render<TestEngine> for String {
impl Layout<TestEngine> for String {
fn render (&self, to: &mut TestOutput) {
to.area_mut().set_w(self.len() as u16);
}
@ -63,7 +63,7 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
use std::sync::{Arc, RwLock};
struct TestComponent(String);
impl Layout<Tui> for TestComponent {
fn layout (&self) -> Option<impl Render<Tui>> {
fn layout (&self) -> Option<impl Layout<Tui>> {
Some(self.0.as_str())
}
}

View file

@ -8,36 +8,51 @@ pub trait Output<E: Engine> {
/// Mutable pointer to area
fn area_mut (&mut self) -> &mut E::Area;
///// Render widget in area
fn render_in (&mut self, area: E::Area, widget: &impl Render<E>) -> Usually<()>;
fn place (&mut self, area: E::Area, content: &impl Content<E>);
#[inline] fn x (&self) -> E::Unit { self.area().x() }
#[inline] fn y (&self) -> E::Unit { self.area().y() }
#[inline] fn w (&self) -> E::Unit { self.area().w() }
#[inline] fn h (&self) -> E::Unit { self.area().h() }
#[inline] fn wh (&self) -> E::Size { self.area().wh().into() }
}
pub trait Layout<E: Engine>: Send + Sync {
fn layout (&self) -> Option<impl Render<E>> {
pub trait Content<E: Engine>: Send + Sync {
fn content (&self) -> Option<impl Content<E>> {
None::<()>
}
}
impl<E: Engine> Layout<E> for () {}
impl<E: Engine, L: Layout<E>> Layout<E> for &L {}
pub trait Render<E: Engine>: Send + Sync {
fn render (&self, _: &mut E::Output);
}
impl<E: Engine, L: Layout<E>> Render<E> for L {
fn render (&self, to: &mut E::Output) {
if let Some(content) = self.layout() {
content.render(to)
fn area (&self, area: E::Area) -> E::Area {
if let Some(content) = self.content() {
content.area(area)
} else {
[0.into(), 0.into(), 0.into(), 0.into()].into()
}
}
fn render (&self, output: &mut E::Output) {
if let Some(content) = self.content() {
output.place(self.area(output.area()), &content)
}
}
}
impl<E: Engine> Content<E> for () {}
impl<E: Engine, T: Content<E>> Content<E> for &T {}
//impl<E: Engine, R: Render<E>> Render<E> for &R {}
//pub trait Render<E: Engine>: Send + Sync {
//fn render (&self, _: &mut E::Output);
//}
//impl<E: Engine, L: Layout<E>> Render<E> for L {
//fn render (&self, to: &mut E::Output) {
//if let Some(content) = self.layout() {
//content.render(to)
//}
//}
//}
//impl<E: Engine, L: Layout<E>> Layout<E> for &L {}
///// Something that can be represented by a renderable component.

View file

@ -89,7 +89,7 @@ impl Tui {
}
}
pub trait TuiRun<R: Layout<Tui> + Handle<Tui> + Sized + 'static> {
pub trait TuiRun<R: Content<Tui> + Handle<Tui> + Sized + 'static> {
/// Run an app in the main loop.
fn run (&self, state: &Arc<RwLock<R>>) -> Usually<()>;
/// Spawn the input thread.
@ -98,7 +98,7 @@ pub trait TuiRun<R: Layout<Tui> + Handle<Tui> + Sized + 'static> {
fn run_output (&self, state: &Arc<RwLock<R>>, sleep: Duration) -> JoinHandle<()>;
}
impl<T: Layout<Tui> + Handle<Tui> + Sized + 'static> TuiRun<T> for Arc<RwLock<Tui>> {
impl<T: Content<Tui> + Handle<Tui> + Sized + 'static> TuiRun<T> for Arc<RwLock<Tui>> {
fn run (&self, state: &Arc<RwLock<T>>) -> Usually<()> {
let _input_thread = self.run_input(state, Duration::from_millis(100));
self.write().unwrap().setup()?;
@ -254,12 +254,11 @@ pub struct TuiOutput {
impl Output<Tui> for TuiOutput {
#[inline] fn area (&self) -> [u16;4] { self.area }
#[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area }
#[inline] fn render_in (&mut self, area: [u16;4], widget: &impl Render<Tui>) -> Usually<()> {
#[inline] fn place (&mut self, area: [u16;4], content: &impl Content<Tui>) {
let last = self.area();
*self.area_mut() = area;
widget.render(self);
content.render(self);
*self.area_mut() = last;
Ok(())
}
}
@ -330,13 +329,13 @@ pub fn half_block (lower: bool, upper: bool) -> Option<char> {
//impl<T: Content<Tui>> Render<Tui> for T {}
impl Render<Tui> for &str {
impl Content<Tui> for &str {
fn render (&self, to: &mut TuiOutput) {
to.blit(self, to.area.x(), to.area.y(), None)
}
}
impl Render<Tui> for String {
impl Content<Tui> for String {
fn render (&self, to: &mut TuiOutput) {
to.blit(self, to.area.x(), to.area.y(), None)
}