From d9535b707f53464bb6f00db61e43f399e01f1029 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 15 Sep 2024 15:57:24 +0300 Subject: [PATCH] add Input and Output traits to Engine --- crates/tek_core/src/engine.rs | 24 ++++++++++++++++-------- crates/tek_core/src/tui.rs | 29 +++++++++++++++++++---------- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/crates/tek_core/src/engine.rs b/crates/tek_core/src/engine.rs index c8498467..ef0b82a7 100644 --- a/crates/tek_core/src/engine.rs +++ b/crates/tek_core/src/engine.rs @@ -7,21 +7,29 @@ pub trait App { /// Platform backend. pub trait Engine: Send + Sync + Sized { - fn setup (&mut self) -> Usually<()> { Ok(()) } - fn exited (&self) -> bool; - fn teardown (&mut self) -> Usually<()> { Ok(()) } + type Input: Input; + type Handled; + type Output: Output; - /// Unit of distance. type Unit: Number; type Area: Area + From<[Self::Unit;4]> + Debug; type Size: Size + From<[Self::Unit;2]> + Debug; - type Input; - type Handled; - type Output: RenderTarget; + fn setup (&mut self) -> Usually<()> { Ok(()) } + fn exited (&self) -> bool; + fn teardown (&mut self) -> Usually<()> { Ok(()) } } -pub trait RenderTarget { +pub trait Input { + type Event; + fn event (&self) + -> Self::Event; + fn is_done (&self) + -> bool; + fn done (&self); +} + +pub trait Output { fn area (&self) -> E::Area; fn area_mut (&mut self) diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index b0240a75..4a2631ca 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -21,12 +21,12 @@ pub struct Tui { } impl Engine for Tui { - type Unit = u16; - type Size = [Self::Unit;2]; - type Area = [Self::Unit;4]; - type Input = Self; - type Handled = bool; - type Output = Self; + 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) } @@ -48,7 +48,19 @@ impl Engine for Tui { disable_raw_mode().map_err(Into::into) } } -impl RenderTarget for Tui { +impl Input for Tui { + type Event = TuiEvent; + fn event (&self) -> TuiEvent { + self.event.read().unwrap().clone().unwrap() + } + fn is_done (&self) -> bool { + self.exited.fetch_and(true, Ordering::Relaxed) + } + fn done (&self) { + self.exited.store(true, Ordering::Relaxed); + } +} +impl Output for Tui { #[inline] fn area (&self) -> ::Area { self.area } @@ -136,9 +148,6 @@ impl Tui { std::thread::sleep(sleep); }) } - pub fn event (&self) -> TuiEvent { - self.event.read().unwrap().clone().unwrap() - } pub fn buffer (&mut self) -> &mut Buffer { &mut self.buffers[self.buffer] }