use super::braille::{BrailleContext, BrailleGrid};
use super::draw::DrawContext;
use crate::widget::traits::{RenderContext, View};
pub struct Canvas<F>
where
F: Fn(&mut DrawContext),
{
draw_fn: F,
}
impl<F> Canvas<F>
where
F: Fn(&mut DrawContext),
{
pub fn new(draw_fn: F) -> Self {
Self { draw_fn }
}
}
impl<F> View for Canvas<F>
where
F: Fn(&mut DrawContext),
{
fn render(&self, ctx: &mut RenderContext) {
let mut draw_ctx = DrawContext::new(ctx.buffer, ctx.area);
(self.draw_fn)(&mut draw_ctx);
}
}
pub fn canvas<F>(draw_fn: F) -> Canvas<F>
where
F: Fn(&mut DrawContext),
{
Canvas::new(draw_fn)
}
pub struct BrailleCanvas<F>
where
F: Fn(&mut BrailleContext),
{
draw_fn: F,
}
impl<F> BrailleCanvas<F>
where
F: Fn(&mut BrailleContext),
{
pub fn new(draw_fn: F) -> Self {
Self { draw_fn }
}
}
impl<F> View for BrailleCanvas<F>
where
F: Fn(&mut BrailleContext),
{
fn render(&self, ctx: &mut RenderContext) {
let mut grid = BrailleGrid::new(ctx.area.width, ctx.area.height);
let mut braille_ctx = BrailleContext::new(&mut grid);
(self.draw_fn)(&mut braille_ctx);
grid.render(ctx.buffer, ctx.area);
}
}
pub fn braille_canvas<F>(draw_fn: F) -> BrailleCanvas<F>
where
F: Fn(&mut BrailleContext),
{
BrailleCanvas::new(draw_fn)
}