mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 04:36:45 +01:00
refactor: separate Render from Handle
This commit is contained in:
parent
d9b3bd150e
commit
72ead536be
12 changed files with 255 additions and 212 deletions
|
|
@ -20,6 +20,44 @@ pub use self::launcher::Launcher;
|
|||
|
||||
use crossterm::event;
|
||||
|
||||
pub trait Handle {
|
||||
// Returns Ok(true) if the device handled the event.
|
||||
// This is the mechanism which allows nesting of components;.
|
||||
fn handle (&mut self, _e: &AppEvent) -> Usually<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Render {
|
||||
// Returns space used by component.
|
||||
// This is insufficient but for the most basic dynamic layout algorithms.
|
||||
fn render (&self, _b: &mut Buffer, _a: Rect) -> Usually<Rect> {
|
||||
Ok(Rect { x: 0, y: 0, width: 0, height: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Box<dyn Device> {
|
||||
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
||||
(**self).render(b, a)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Device: Render + Handle + Send + Sync {}
|
||||
|
||||
impl<T: Render + Handle + Send + Sync> Device for T {}
|
||||
|
||||
impl WidgetRef for &dyn Render {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Render::render(*self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetRef for dyn Render {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Render::render(self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn Error>> {
|
||||
let device = Arc::new(Mutex::new(device));
|
||||
let exited = Arc::new(AtomicBool::new(false));
|
||||
|
|
@ -89,19 +127,6 @@ pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn E
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub trait Device: Send + Sync {
|
||||
// Returns Ok(true) if the device handled the event.
|
||||
// This is the mechanism which allows nesting of components;.
|
||||
fn handle (&mut self, _event: &AppEvent) -> Usually<bool> {
|
||||
Ok(false)
|
||||
}
|
||||
// Returns space used by component.
|
||||
// This is insufficient but for the most basic dynamic layout algorithms.
|
||||
fn render (&self, _buffer: &mut Buffer, _area: Rect) -> Usually<Rect> {
|
||||
Ok(Rect { x: 0, y: 0, width: 0, height: 0 })
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DynamicDevice<T> {
|
||||
pub state: Arc<Mutex<T>>,
|
||||
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect)->Usually<Rect> + Send>>,
|
||||
|
|
@ -110,10 +135,13 @@ pub struct DynamicDevice<T> {
|
|||
client: Option<DynamicAsyncClient>
|
||||
}
|
||||
|
||||
impl<T: Send + Sync> Device for DynamicDevice<T> {
|
||||
impl<T> Handle for DynamicDevice<T> {
|
||||
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
|
||||
self.handle.lock().unwrap()(&mut *self.state.lock().unwrap(), event)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Render for DynamicDevice<T> {
|
||||
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
|
||||
self.render.lock().unwrap()(&*self.state.lock().unwrap(), buf, area)
|
||||
}
|
||||
|
|
@ -162,18 +190,6 @@ impl<T: Send + Sync + 'static> DynamicDevice<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl WidgetRef for &dyn Device {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Device::render(*self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetRef for dyn Device {
|
||||
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
|
||||
Device::render(self, buf, area).expect("Failed to render device.");
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum AppEvent {
|
||||
/// Terminal input
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue