DrawContext size

This commit is contained in:
2026-01-24 00:42:54 +01:00
parent 0a026c4ee7
commit c2b9bb3aa1
2 changed files with 41 additions and 5 deletions
+5 -4
View File
@@ -23,8 +23,10 @@ fn main() {
} }
}); });
let (width, height) = crossterm::terminal::size().unwrap();
loop { loop {
cx.get_view()(&mut DrawContext {}); cx.get_view()(&mut DrawContext::new(width, height));
std::thread::sleep(std::time::Duration::from_millis(16)); std::thread::sleep(std::time::Duration::from_millis(16));
} }
} }
@@ -35,13 +37,12 @@ fn app(cx: &Arc<Context>) -> View {
cx.on_event({ cx.on_event({
let count = count.clone(); let count = count.clone();
move |_cx, _event: &KeyPress| { move |_cx, _event: &KeyPress| {
#[allow(unused_mut)]
let mut count = count.get(); let mut count = count.get();
*count += 1; *count += 1;
} }
}); });
Arc::new(move |_| { Arc::new(move |ctx| {
println!("Count: {count}"); println!("Count: {count}, size: {:?}", ctx.available());
}) })
} }
+36 -1
View File
@@ -11,4 +11,39 @@ pub mod prelude {
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>; pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
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;
}
}