Simple test

This commit is contained in:
2026-01-24 14:26:04 +01:00
parent 979af999e4
commit 49bb99fedb
2 changed files with 18 additions and 9 deletions
+7 -4
View File
@@ -3,7 +3,7 @@ use std::sync::{Arc, Mutex};
use crate::{prelude::Context, DrawContext, View};
pub trait Engine {
fn render_view(&self, ctx: &DrawContext, view: View);
fn render_view(&self, ctx: &DrawContext, view: &View);
fn draw_context(&self, ctx: &DrawContext);
}
@@ -38,17 +38,19 @@ impl Console {
loop {
let (width, height) = crossterm::terminal::size().unwrap();
let mut ctx = DrawContext::new(width, height);
self.render_view(&mut ctx, cx.get_view());
self.render_view(&mut ctx, &cx.get_view());
self.draw_context(&ctx);
std::thread::sleep(std::time::Duration::from_millis(16));
}
}
}
impl Engine for Console {
fn render_view(&self, ctx: &DrawContext, view: View) {
fn render_view(&self, ctx: &DrawContext, view: &View) {
let mut context = DrawContext::new(ctx.allocated.width, ctx.allocated.height);
view(&mut context);
self.draw_context(&context);
}
fn draw_context(&self, ctx: &DrawContext) {
@@ -56,6 +58,7 @@ impl Engine for Console {
match inst {
crate::render::DrawInstruction::Text(point, text) => println!("{text}"),
crate::render::DrawInstruction::Child(point, child) => self.draw_context(child),
crate::render::DrawInstruction::View(point, view) => self.render_view(ctx, view),
}
}
}
+11 -5
View File
@@ -1,24 +1,25 @@
use std::fmt::Debug;
use crate::View;
#[derive(Debug, Clone)]
#[derive(Clone)]
pub enum DrawInstruction {
Text(Point, String),
View(Point, View),
Child(Point, DrawContext),
}
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Size {
pub width: u16,
pub height: u16,
}
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct Point {
pub x: u16,
pub y: u16,
}
#[derive(Debug, Clone)]
#[derive(Clone)]
pub struct DrawContext {
pub available: Size,
pub allocated: Size,
@@ -52,4 +53,9 @@ impl DrawContext {
self.drawing
.push(DrawInstruction::Text(point, text.to_string()));
}
pub fn draw_view(&mut self, point: Point, view: View) {
let ctx = DrawContext::new(self.allocated.width, self.allocated.height);
self.drawing.push(DrawInstruction::Child(point, ctx));
}
}