modularize core

This commit is contained in:
🪞👃🪞 2024-06-30 22:47:17 +03:00
parent a4061535b5
commit 2837ffff4a
43 changed files with 629 additions and 770 deletions

54
src/core/render.rs Normal file
View file

@ -0,0 +1,54 @@
use crate::core::*;
/// 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()))
}
}
}