separate Engine from RenderTarget

This commit is contained in:
🪞👃🪞 2024-09-15 15:44:11 +03:00
parent 60acb20a57
commit ff6751d393
4 changed files with 70 additions and 56 deletions

View file

@ -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);