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}; use crate::{prelude::Context, DrawContext, View};
pub trait Engine { 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); fn draw_context(&self, ctx: &DrawContext);
} }
@@ -38,17 +38,19 @@ impl Console {
loop { loop {
let (width, height) = crossterm::terminal::size().unwrap(); let (width, height) = crossterm::terminal::size().unwrap();
let mut ctx = DrawContext::new(width, height); 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)); std::thread::sleep(std::time::Duration::from_millis(16));
} }
} }
} }
impl Engine for Console { 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); let mut context = DrawContext::new(ctx.allocated.width, ctx.allocated.height);
view(&mut context); view(&mut context);
self.draw_context(&context);
} }
fn draw_context(&self, ctx: &DrawContext) { fn draw_context(&self, ctx: &DrawContext) {
@@ -56,6 +58,7 @@ impl Engine for Console {
match inst { match inst {
crate::render::DrawInstruction::Text(point, text) => println!("{text}"), crate::render::DrawInstruction::Text(point, text) => println!("{text}"),
crate::render::DrawInstruction::Child(point, child) => self.draw_context(child), 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 { pub enum DrawInstruction {
Text(Point, String), Text(Point, String),
View(Point, View),
Child(Point, DrawContext), Child(Point, DrawContext),
} }
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct Size { pub struct Size {
pub width: u16, pub width: u16,
pub height: u16, pub height: u16,
} }
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct Point { pub struct Point {
pub x: u16, pub x: u16,
pub y: u16, pub y: u16,
} }
#[derive(Debug, Clone)] #[derive(Clone)]
pub struct DrawContext { pub struct DrawContext {
pub available: Size, pub available: Size,
pub allocated: Size, pub allocated: Size,
@@ -52,4 +53,9 @@ impl DrawContext {
self.drawing self.drawing
.push(DrawInstruction::Text(point, text.to_string())); .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));
}
} }