From 49bb99fedb30bc585965b50a9e85fa614236f864 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 24 Jan 2026 14:26:04 +0100 Subject: [PATCH] Simple test --- src/engine.rs | 11 +++++++---- src/render.rs | 16 +++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 7f4f5e4..4582e92 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -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), } } } diff --git a/src/render.rs b/src/render.rs index 01c5353..2502427 100644 --- a/src/render.rs +++ b/src/render.rs @@ -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)); + } }