wip: connect devices

This commit is contained in:
🪞👃🪞 2024-07-04 00:02:22 +03:00
parent 7f3425fe04
commit 394355331d
10 changed files with 235 additions and 145 deletions

View file

@ -1,6 +1,19 @@
use crate::core::*;
use ratatui::widgets::WidgetRef;
pub trait Blit {
// Render something to X, Y coordinates in a buffer, ignoring width/height.
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
}
impl<T: AsRef<str>> Blit for T {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
}
}
}
/// Trait for things that render to the display.
pub trait Render: Send {
// Render something to an area of the buffer.
@ -43,6 +56,12 @@ impl Render for Box<dyn Device> {
}
}
impl<T: Render> Render for Arc<Mutex<T>> {
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
self.lock().unwrap().render(b, a)
}
}
impl WidgetRef for &dyn Render {
fn render_ref (&self, area: Rect, buf: &mut Buffer) {
Render::render(*self, buf, area).expect("Failed to render device.");
@ -54,16 +73,3 @@ impl WidgetRef for dyn Render {
Render::render(self, buf, area).expect("Failed to render device.");
}
}
pub trait Blit {
// Render something to X, Y coordinates in a buffer, ignoring width/height.
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>);
}
impl<T: AsRef<str>> Blit for T {
fn blit (&self, buf: &mut Buffer, x: u16, y: u16, style: Option<Style>) {
if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, self.as_ref(), style.unwrap_or(Style::default()))
}
}
}