View render

This commit is contained in:
2026-01-24 14:37:19 +01:00
parent 49bb99fedb
commit 1f9bf48bb4
2 changed files with 33 additions and 20 deletions
+19 -11
View File
@@ -1,9 +1,9 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::{prelude::Context, DrawContext, View}; use crate::{prelude::Context, render::Area, DrawContext, View};
pub trait Engine { pub trait Engine {
fn render_view(&self, ctx: &DrawContext, view: &View); fn render_view(&self, area: &Area, view: &View) -> DrawContext;
fn draw_context(&self, ctx: &DrawContext); fn draw_context(&self, ctx: &DrawContext);
} }
@@ -37,10 +37,15 @@ 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); self.draw_context(&self.render_view(
&Area {
self.render_view(&mut ctx, &cx.get_view()); x: 0,
self.draw_context(&ctx); y: 0,
width,
height,
},
&cx.get_view(),
));
std::thread::sleep(std::time::Duration::from_millis(16)); std::thread::sleep(std::time::Duration::from_millis(16));
} }
@@ -48,17 +53,20 @@ impl Console {
} }
impl Engine for Console { impl Engine for Console {
fn render_view(&self, ctx: &DrawContext, view: &View) { fn render_view(&self, area: &Area, view: &View) -> DrawContext {
let mut context = DrawContext::new(ctx.allocated.width, ctx.allocated.height); let mut context = DrawContext::new(area.clone());
view(&mut context); view(&mut context);
context
} }
fn draw_context(&self, ctx: &DrawContext) { fn draw_context(&self, ctx: &DrawContext) {
for inst in &ctx.drawing { for inst in &ctx.drawing {
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), crate::render::DrawInstruction::View(area, view) => {
self.draw_context(&self.render_view(area, view))
}
} }
} }
} }
+14 -9
View File
@@ -3,7 +3,7 @@ use crate::View;
#[derive(Clone)] #[derive(Clone)]
pub enum DrawInstruction { pub enum DrawInstruction {
Text(Point, String), Text(Point, String),
View(Point, View), View(Area, View),
Child(Point, DrawContext), Child(Point, DrawContext),
} }
@@ -19,23 +19,29 @@ pub struct Point {
pub y: u16, pub y: u16,
} }
#[derive(Clone)]
pub struct Area {
pub x: u16,
pub y: u16,
pub width: u16,
pub height: u16,
}
#[derive(Clone)] #[derive(Clone)]
pub struct DrawContext { pub struct DrawContext {
pub available: Size, pub area: Area,
pub allocated: Size, pub allocated: Size,
pub parent: Point,
pub drawing: Vec<DrawInstruction>, pub drawing: Vec<DrawInstruction>,
} }
impl DrawContext { impl DrawContext {
pub fn new(width: u16, height: u16) -> Self { pub fn new(area: Area) -> Self {
Self { Self {
available: Size { width, height }, area,
allocated: Size { allocated: Size {
width: 0, width: 0,
height: 0, height: 0,
}, },
parent: Point { x: 0, y: 0 },
drawing: Vec::new(), drawing: Vec::new(),
} }
} }
@@ -54,8 +60,7 @@ impl DrawContext {
.push(DrawInstruction::Text(point, text.to_string())); .push(DrawInstruction::Text(point, text.to_string()));
} }
pub fn draw_view(&mut self, point: Point, view: View) { pub fn draw_view(&mut self, area: Area, view: View) {
let ctx = DrawContext::new(self.allocated.width, self.allocated.height); self.drawing.push(DrawInstruction::View(area, view));
self.drawing.push(DrawInstruction::Child(point, ctx));
} }
} }