From c2b9bb3aa19549488ea2bbd5aa468519980fb0ba Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 24 Jan 2026 00:42:54 +0100 Subject: [PATCH] DrawContext size --- examples/hello_world.rs | 9 +++++---- src/lib.rs | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 2a279b6..aa430b8 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -23,8 +23,10 @@ fn main() { } }); + let (width, height) = crossterm::terminal::size().unwrap(); + loop { - cx.get_view()(&mut DrawContext {}); + cx.get_view()(&mut DrawContext::new(width, height)); std::thread::sleep(std::time::Duration::from_millis(16)); } } @@ -35,13 +37,12 @@ fn app(cx: &Arc) -> View { cx.on_event({ let count = count.clone(); move |_cx, _event: &KeyPress| { - #[allow(unused_mut)] let mut count = count.get(); *count += 1; } }); - Arc::new(move |_| { - println!("Count: {count}"); + Arc::new(move |ctx| { + println!("Count: {count}, size: {:?}", ctx.available()); }) } diff --git a/src/lib.rs b/src/lib.rs index 6cb4c55..e9296d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,4 +11,39 @@ pub mod prelude { pub type View = Arc; -pub struct DrawContext {} +#[derive(Debug, Clone)] +pub struct Size { + pub width: u16, + pub height: u16, +} + +#[derive(Debug, Clone)] +pub struct DrawContext { + available: Size, + used: Size, +} + +impl DrawContext { + pub fn new(width: u16, height: u16) -> Self { + Self { + available: Size { width, height }, + used: Size { + width: 0, + height: 0, + }, + } + } + + pub fn available(&self) -> &Size { + &self.available + } + + pub fn used(&self) -> &Size { + &self.used + } + + pub fn allocate(&mut self, width: u16, height: u16) { + self.used.width += width; + self.used.height += height; + } +}