mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
55 lines
1.5 KiB
Rust
55 lines
1.5 KiB
Rust
use crate::core::*;
|
|
use ratatui::widgets::WidgetRef;
|
|
|
|
/// Trait for things that render to the display.
|
|
pub trait Render {
|
|
// Render something to an area of the buffer.
|
|
// Returns area 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 })
|
|
}
|
|
fn min_width (&self) -> u16 {
|
|
0
|
|
}
|
|
fn max_width (&self) -> u16 {
|
|
u16::MAX
|
|
}
|
|
fn min_height (&self) -> u16 {
|
|
0
|
|
}
|
|
fn max_height (&self) -> u16 {
|
|
u16::MAX
|
|
}
|
|
}
|
|
|
|
impl Render for Box<dyn Device> {
|
|
fn render (&self, b: &mut Buffer, a: Rect) -> Usually<Rect> {
|
|
(**self).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.");
|
|
}
|
|
}
|
|
|
|
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 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()))
|
|
}
|
|
}
|
|
}
|