mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
separate Engine from RenderTarget
This commit is contained in:
parent
60acb20a57
commit
ff6751d393
4 changed files with 70 additions and 56 deletions
|
|
@ -310,13 +310,13 @@ impl<E: Engine> Widget for JackDevice<E> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.state.read().unwrap().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
self.state.read().unwrap().render(to)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine> Handle<E> for JackDevice<E> {
|
||||
fn handle (&mut self, from: &E::HandleInput) -> Perhaps<E::Handled> {
|
||||
fn handle (&mut self, from: &E::Input) -> Perhaps<E::Handled> {
|
||||
self.state.write().unwrap().handle(from)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,38 +16,37 @@ pub trait Engine: Send + Sync + Sized {
|
|||
type Area: Area<Self::Unit> + From<[Self::Unit;4]> + Debug;
|
||||
type Size: Size<Self::Unit> + From<[Self::Unit;2]> + Debug;
|
||||
|
||||
type HandleInput;
|
||||
type Input;
|
||||
type Handled;
|
||||
type Output: RenderTarget<Self>;
|
||||
}
|
||||
|
||||
#[inline] fn area (&self) -> Self::Area;
|
||||
#[inline] fn area_mut (&mut self) -> &mut Self::Area;
|
||||
#[inline] fn render_in (
|
||||
&mut self, area: Self::Area, widget: &impl Widget<Engine = Self>
|
||||
) -> Perhaps<Self::Area> {
|
||||
let last = self.area();
|
||||
*self.area_mut() = area;
|
||||
let next = widget.render(self)?;
|
||||
*self.area_mut() = last;
|
||||
Ok(next)
|
||||
}
|
||||
pub trait RenderTarget<E: Engine> {
|
||||
fn area (&self)
|
||||
-> E::Area;
|
||||
fn area_mut (&mut self)
|
||||
-> &mut E::Area;
|
||||
fn render_in (&mut self, area: E::Area, widget: &impl Widget<Engine = E>)
|
||||
-> Perhaps<E::Area>;
|
||||
}
|
||||
|
||||
pub trait Widget: Send + Sync {
|
||||
type Engine: Engine;
|
||||
fn layout (&self, to: <<Self as Widget>::Engine as Engine>::Size) ->
|
||||
Perhaps<<<Self as Widget>::Engine as Engine>::Size>
|
||||
{
|
||||
fn layout (
|
||||
&self, to: <Self::Engine as Engine>::Size
|
||||
) -> Perhaps<<Self::Engine as Engine>::Size> {
|
||||
Ok(Some(to))
|
||||
}
|
||||
fn render (&self, to: &mut Self::Engine) ->
|
||||
Perhaps<<<Self as Widget>::Engine as Engine>::Area>;
|
||||
fn render (
|
||||
&self, to: &mut <Self::Engine as Engine>::Output
|
||||
) -> Perhaps<<<Self as Widget>::Engine as Engine>::Area>;
|
||||
}
|
||||
impl<'a, E: Engine> Widget for Box<dyn Widget<Engine = E> + 'a> {
|
||||
type Engine = E;
|
||||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
(**self).layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
|
|
@ -56,7 +55,7 @@ impl<E: Engine> Widget for &dyn Widget<Engine = E> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
(*self).layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
(*self).render(to)
|
||||
}
|
||||
}
|
||||
|
|
@ -65,7 +64,7 @@ impl<E: Engine> Widget for &mut dyn Widget<Engine = E> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
(**self).layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
(**self).render(to)
|
||||
}
|
||||
}
|
||||
|
|
@ -74,7 +73,7 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for Arc<W> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.as_ref().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
self.as_ref().render(to)
|
||||
}
|
||||
}
|
||||
|
|
@ -83,7 +82,7 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for Mutex<W> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.lock().unwrap().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
self.lock().unwrap().render(to)
|
||||
}
|
||||
}
|
||||
|
|
@ -92,7 +91,7 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for RwLock<W> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.read().unwrap().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
self.read().unwrap().render(to)
|
||||
}
|
||||
}
|
||||
|
|
@ -101,7 +100,7 @@ impl<E: Engine, W: Widget<Engine = E>> Widget for Option<W> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
Ok(self.as_ref().map(|widget|widget.layout(to)).transpose()?.flatten())
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
Ok(self.as_ref().map(|widget|widget.render(to)).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
|
@ -120,7 +119,7 @@ impl<E: Engine, W: Content<Engine = E>> Widget for W {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.content().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
match self.layout(to.area().wh().into())? {
|
||||
Some(wh) => to.render_in(to.area().clip(wh).into(), &self.content()),
|
||||
None => Ok(None)
|
||||
|
|
@ -154,17 +153,17 @@ impl<E: Engine, C: Component<E> + Exit> ExitableComponent<E> for C {}
|
|||
|
||||
/// Handle input
|
||||
pub trait Handle<E: Engine>: Send + Sync {
|
||||
fn handle (&mut self, context: &E::HandleInput) -> Perhaps<E::Handled>;
|
||||
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled>;
|
||||
}
|
||||
|
||||
impl<H, E: Engine> Handle<E> for &mut H where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E::HandleInput) -> Perhaps<E::Handled> {
|
||||
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
||||
(*self).handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, E: Engine> Handle<E> for Option<H> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E::HandleInput) -> Perhaps<E::Handled> {
|
||||
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
||||
if let Some(ref mut handle) = self {
|
||||
handle.handle(context)
|
||||
} else {
|
||||
|
|
@ -174,25 +173,25 @@ impl<H, E: Engine> Handle<E> for Option<H> where H: Handle<E> {
|
|||
}
|
||||
|
||||
impl<H, E: Engine> Handle<E> for Mutex<H> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E::HandleInput) -> Perhaps<E::Handled> {
|
||||
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
||||
self.lock().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, E: Engine> Handle<E> for Arc<Mutex<H>> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E::HandleInput) -> Perhaps<E::Handled> {
|
||||
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
||||
self.lock().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, E: Engine> Handle<E> for RwLock<H> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E::HandleInput) -> Perhaps<E::Handled> {
|
||||
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
||||
self.write().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
||||
impl<H, E: Engine> Handle<E> for Arc<RwLock<H>> where H: Handle<E> {
|
||||
fn handle (&mut self, context: &E::HandleInput) -> Perhaps<E::Handled> {
|
||||
fn handle (&mut self, context: &E::Input) -> Perhaps<E::Handled> {
|
||||
self.write().unwrap().handle(context)
|
||||
}
|
||||
}
|
||||
|
|
@ -450,7 +449,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Align<T> {
|
|||
//Self::SE(_) => { todo!() },
|
||||
//})
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
let outer_area = to.area();
|
||||
Ok(if let Some(inner_size) = self.layout(outer_area.wh().into())? {
|
||||
let inner_area = outer_area.clip(inner_size);
|
||||
|
|
@ -505,7 +504,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
|
|||
}.into()))
|
||||
}
|
||||
// TODO: 🡘 🡙 ←🡙→
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
Ok(self.layout(to.area().wh().into())?
|
||||
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
|
||||
.transpose()?.flatten())
|
||||
|
|
@ -537,7 +536,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
|
|||
Self::XY(w, h, _) => [to.w().min(w), to.h().min(h)],
|
||||
}.into()))
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
Ok(self.layout(to.area().wh().into())?
|
||||
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
|
||||
.transpose()?.flatten())
|
||||
|
|
@ -569,7 +568,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Grow<E::Unit, T> {
|
|||
Self::XY(w, h, _) => [to.w() + w, to.h() + h],
|
||||
}.into()))
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
Ok(self.layout(to.area().wh().into())?
|
||||
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
|
||||
.transpose()?.flatten())
|
||||
|
|
@ -601,7 +600,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Shrink<E::Unit, T> {
|
|||
Self::XY(w, h, _) => [to.w() - w, to.h() - h]
|
||||
}.into()))
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
Ok(self.layout(to.area().wh().into())?
|
||||
.map(|size|to.render_in(to.area().clip(size).into(), self.inner()))
|
||||
.transpose()?.flatten())
|
||||
|
|
@ -649,7 +648,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Inset<E::Unit, T> {
|
|||
Self::XY(x, y, ref inner) => Shrink::XY(x + x, y + y, inner as &dyn Widget<Engine = E>),
|
||||
}.layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
match *self {
|
||||
Self::X(x, ref inner) => Plus::X(x, inner as &dyn Widget<Engine = E>),
|
||||
Self::Y(y, ref inner) => Plus::Y(y, inner as &dyn Widget<Engine = E>),
|
||||
|
|
@ -667,7 +666,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
|
|||
Self::XY(x, y, ref inner) => Grow::XY(x + x, y + y, inner as &dyn Widget<Engine = E>),
|
||||
}.layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
match *self {
|
||||
Self::X(x, ref inner) => Plus::X(x, inner as &dyn Widget<Engine = E>),
|
||||
Self::Y(y, ref inner) => Plus::Y(y, inner as &dyn Widget<Engine = E>),
|
||||
|
|
@ -703,7 +702,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Plus<E::Unit, T> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.inner().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
let area = to.area();
|
||||
Ok(self.layout(area.wh().into())?
|
||||
.map(|size|to.render_in(match *self {
|
||||
|
|
@ -741,7 +740,7 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Minus<E::Unit, T> {
|
|||
fn layout (&self, to: E::Size) -> Perhaps<E::Size> {
|
||||
self.inner().layout(to)
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
fn render (&self, to: &mut E::Output) -> Perhaps<E::Area> {
|
||||
let area = to.area();
|
||||
Ok(self.layout(area.wh().into())?
|
||||
.map(|size|to.render_in(match *self {
|
||||
|
|
|
|||
|
|
@ -3,11 +3,11 @@ use crate::*;
|
|||
struct TestEngine([u16;4], Vec<Vec<char>>);
|
||||
|
||||
impl Engine for TestEngine {
|
||||
type Unit = u16;
|
||||
type Size = [Self::Unit;2];
|
||||
type Area = [Self::Unit;4];
|
||||
type HandleInput = Self;
|
||||
type Handled = bool;
|
||||
type Unit = u16;
|
||||
type Size = [Self::Unit;2];
|
||||
type Area = [Self::Unit;4];
|
||||
type Input = Self;
|
||||
type Handled = bool;
|
||||
fn exited (&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,11 +21,12 @@ pub struct Tui {
|
|||
}
|
||||
|
||||
impl Engine for Tui {
|
||||
type Unit = u16;
|
||||
type Size = [Self::Unit;2];
|
||||
type Area = [Self::Unit;4];
|
||||
type HandleInput = Self;
|
||||
type Handled = bool;
|
||||
type Unit = u16;
|
||||
type Size = [Self::Unit;2];
|
||||
type Area = [Self::Unit;4];
|
||||
type Input = Self;
|
||||
type Handled = bool;
|
||||
type Output = Self;
|
||||
fn exited (&self) -> bool {
|
||||
self.exited.fetch_and(true, Ordering::Relaxed)
|
||||
}
|
||||
|
|
@ -46,13 +47,27 @@ impl Engine for Tui {
|
|||
self.backend.show_cursor()?;
|
||||
disable_raw_mode().map_err(Into::into)
|
||||
}
|
||||
#[inline] fn area (&self) -> Self::Area {
|
||||
}
|
||||
impl RenderTarget<Tui> for Tui {
|
||||
#[inline] fn area (&self) -> <Self as Engine>::Area {
|
||||
self.area
|
||||
}
|
||||
#[inline] fn area_mut (&mut self) -> &mut Self::Area {
|
||||
#[inline] fn area_mut (&mut self) -> &mut <Self as Engine>::Area {
|
||||
&mut self.area
|
||||
}
|
||||
#[inline] fn render_in (
|
||||
&mut self,
|
||||
area: <Self as Engine>::Area,
|
||||
widget: &impl Widget<Engine = Self>
|
||||
) -> Perhaps<<Self as Engine>::Area> {
|
||||
let last = self.area();
|
||||
*self.area_mut() = area;
|
||||
let next = widget.render(self)?;
|
||||
*self.area_mut() = last;
|
||||
Ok(next)
|
||||
}
|
||||
}
|
||||
|
||||
impl Tui {
|
||||
/// Run the main loop.
|
||||
pub fn run <R: Component<Tui> + Sized + 'static> (
|
||||
|
|
@ -90,7 +105,7 @@ impl Tui {
|
|||
let event = TuiEvent::Input(::crossterm::event::read().unwrap());
|
||||
match event {
|
||||
key!(Ctrl-KeyCode::Char('c')) => {
|
||||
engine.write().unwrap().exited.store(true, Ordering::Relaxed);
|
||||
exited.store(true, Ordering::Relaxed);
|
||||
},
|
||||
_ => {
|
||||
*engine.write().unwrap().event.write().unwrap() = Some(event);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue